Skip to content

@s0nspark /upsert_tax_rules.coffee
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
got this working :)
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"
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.