Permalink
Browse files

Added seeding

  • Loading branch information...
1 parent 1bd2707 commit 93f7ea405ddff4f6e196bd7fc4d51eb4850f430d @benjreinhart benjreinhart committed Mar 18, 2013
Showing with 80 additions and 4 deletions.
  1. +6 −0 Makefile
  2. +18 −0 core/db/examiner.coffee
  3. +56 −4 core/db/seeds.coffee
View
@@ -4,5 +4,11 @@ COFFEE=$(CWD)/node_modules/.bin/COFFEE
dev:
NODE_ENV=development node $(CWD)/start.js
+seed:
+ NODE_ENV=development APP_ROOT=$(CWD) $(COFFEE) $(CWD)/core/db/seeds.coffee
+
+examine:
+ NODE_ENV=development APP_ROOT=$(CWD) $(COFFEE) $(CWD)/core/db/examiner.coffee
+
prod:
NODE_ENV=production node $(CWD)/start.js
@@ -0,0 +1,18 @@
+{ APP_ROOT, RESOURCE } = process.env
+
+User = require "#{ APP_ROOT }/components/users/datastore"
+Edit = require "#{ APP_ROOT }/components/edits/datastore"
+
+users = ->
+ User.find (_, users) ->
+ console.log users
+ process.exit 0
+
+edits = ->
+ Edit.find (_, edits) ->
+ console.log edits
+ process.exit 0
+
+switch RESOURCE
+ when 'users' then users()
+ when 'edits' then edits()
View
@@ -2,11 +2,63 @@ async = require 'async'
{ APP_ROOT } = process.env
+User = require "#{ APP_ROOT }/components/users/datastore"
+Edit = require "#{ APP_ROOT }/components/edits/datastore"
+
console.log 'Seeding database...'
+deleteUsers = (cb) ->
+ User.remove().exec (err, count) ->
+ cb err if err?
+ console.log "Removed #{count} records from User table"
+ cb null
+
+users = (cb) ->
+ deleteUsers (err) ->
+ cb err if err?
+
+ dudes = [['Trey', 'Anastasio'], ['Mike', 'Gordon'], ['Jon', 'Fishman'], ['Page', 'McConnell']]
+
+ save = ([first, last], cb) ->
+ user =
+ lastName : last
+ firstName : first
+ email : "#{ first }@#{ last }.com"
+
+ (new User user).save cb
+
+ async.each dudes, save, cb
+
+deleteEdits = (cb) ->
+ Edit.remove().exec (err, count) ->
+ cb err if err?
+ console.log "Removed #{count} records from Edit table"
+ cb null
+
+edits = (cb) ->
+ deleteEdits (err) ->
+ cb err if err?
+
+ User.find (err, users) ->
+ return cb err if err?
+
+ save = (user, cb) ->
+ edit =
+ userId: user.get('_id')
+ authorId: user.get('_id')
+ original: "My first name is #{ user.get('lastName') }"
+ modified: "My last name is #{ user.get('lastName') }"
+ permalink: "http://stuff.com/stuff-#{ user.get('_id') }"
+
+ (new Edit edit).save cb
+
+ async.each users, save, cb
-# async.parallel [], (err) ->
-# throw err if err?
+async.series [
+ users
+ edits
+], (err) ->
+ throw err if err?
-# console.log "Cleared DB"
-# process.exit 0
+ console.log "Cleared DB"
+ process.exit 0

0 comments on commit 93f7ea4

Please sign in to comment.