-
Notifications
You must be signed in to change notification settings - Fork 1
/
import.coffee
34 lines (30 loc) · 999 Bytes
/
import.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
config = require './config'
mongoose = require 'mongoose'
db = mongoose.connect config.db_url
csv = require 'csv'
properties = require './models/properties'
readings = require './models/readings'
add_doc = (Model, doc, cb) ->
m = new Model doc
m.save (err) ->
cb() if cb?
if err
console.log err
return err
else
return m
properties.Building.find {name: "50 West"}, {name:true, meters: true}, (err, docs) ->
building_id = docs[0]._id
building_name = docs[0].name
console.log "Building Name: #{building_name}"
meter_id = docs[0].meters[1]._id
meter_name = docs[0].meters[1].name
console.log "Meter Name: #{meter_name}"
csv()
.fromPath("#{__dirname}/import.csv")
.on 'data', (data, idx)->
meter_reading =
meterId: meter_id
createdAt: new Date("#{data[0]} #{data[1]}"),
kW: data[2]
add_doc readings.MeterReading, meter_reading, () -> console.log meter_reading