public
Created

got this working :)

  • Download Gist
upsert_tax_rules.coffee
CoffeeScript
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
util = require 'util'
mongoose = require 'mongoose'
csv = require 'csv'
 
#
# establish connection
#
 
exit = ->
util.print "cleaning up!\n"
mongoose.disconnect()
process.exit(0)
 
mongoose.connect "mongodb://localhost/taxes"
 
mongoose.connection.on 'open', () ->
util.print "mongodb is connected!\n"
parseCSV()
 
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)
 
#
# parse csv file
#
 
parseCSV = ->
workers = 0
parser = csv().fromPath(__dirname + "/data/tax_rules.csv", columns: true)
parser.on 'data', (data, index) ->
workers++
Rule.update {state: data.state}, data, {upsert: true}, (err) ->
if err
util.print data.state + ": " + err + "\n"
else
util.print "processed " + data.state + " rule\n"
if --workers is 0
exit()
parser.on 'end', (count) ->
util.print "found " + count + " rules\n"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.