Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
trying to wrap my head around async programming
util = require 'util'
mongoose = require 'mongoose'
csv = require 'csv'
#
# establish connection
#
exit = ->
util.print "we outta heah!\n"
mongoose.disconnect()
process.exit(0)
mongoose.connect "mongodb://localhost/mcgeecorp"
mongoose.connection.on 'open', () ->
util.print "mongodb is connected!\n"
mongoose.connection.on 'error', (err) ->
util.print err = "\n"
exit()
#
# define schema
#
Schema = mongoose.Schema
ObjectId = Schema.ObjectId
TaxRule = new Schema(
state:
type: String
index: true
unique: true
baseTaxablePercent: Number
defaultTaxType: String
kdTaxType: String
separateSalesTax: Boolean
separateTaxMaterials: Boolean
separateTaxLabor: Boolean
separateTaxFreight: Boolean
)
Rule = mongoose.model('TaxRule', TaxRule)
addRules = (records) ->
n_out = 0
addRule = (r) ->
n_out++
util.print n_out
Rule.update {state: r.state}, r, {upsert: true}, (err) ->
if err
util.print r.state + ": " + err + "\n"
else
util.print "processed " + r.state + " rule\n"
if n_out-- is 0
exit()
addRule(r) for r of records
#
# parse csv file
#
records = []
csv()
.fromPath(__dirname + "/data/tax_rules.csv", columns: true)
.on 'data', (data, index) ->
records << data
.on 'end', (count) ->
util.print "read " + count + " rules\n"
addRules(records)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.