Skip to content

Instantly share code, notes, and snippets.

@hellogerard
Created May 30, 2014 15:26
Show Gist options
  • Save hellogerard/bbd21255c8cd2f22310d to your computer and use it in GitHub Desktop.
Save hellogerard/bbd21255c8cd2f22310d to your computer and use it in GitHub Desktop.
Importing static data on Meteor startup.
#
# Using 'node-csv-npm' meteorite package
#
fs = Npm.require 'fs'
Meteor.startup ->
# LOAD ITEMS
dataDir = "#{fs.realpathSync process.cwd()}/assets/app/data"
opts = columns: true, trim: true
itemHandler = Meteor.bindEnvironment (row, num) ->
Item.create row
if not Item.count()
CSV().from("#{dataDir}/food_items.csv", opts).on 'record', itemHandler
# LOAD CATEGORIES
categoryHandler = Meteor.bindEnvironment (arr, count) ->
categories = _.uniq _.pluck(arr, 'category')
_.each categories, (category) ->
Category.create name: category
if not Category.count()
CSV().from("#{dataDir}/food_items.csv", opts).to.array categoryHandler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment