Created
May 30, 2014 15:26
-
-
Save hellogerard/bbd21255c8cd2f22310d to your computer and use it in GitHub Desktop.
Importing static data on Meteor startup.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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