Skip to content

Instantly share code, notes, and snippets.

@yocontra
Created May 11, 2013 00:03
Show Gist options
  • Save yocontra/5558343 to your computer and use it in GitHub Desktop.
Save yocontra/5558343 to your computer and use it in GitHub Desktop.
orm = require 'orm'
async = require 'async'
fs = require 'fs'
{join} = require 'path'
modelDir = "./models"
modelDir = join __dirname, modelDir
module.exports =
create: (cb) ->
orm.connect "mysql://root@localhost/crudify", (err, db) ->
return cb err if err?
db.wipe = (cb) ->
async.parallel (m.remove.bind m for _, m of db.models), cb
return db
db.loadDir = (dir, cb) ->
load = (file, done) ->
modPath = join dir, file
mod = require modPath
mod db, done
fs.readdir dir, (err, files) ->
return cb err if err?
async.forEach files, load, cb
db.loadDir modelDir, (err) ->
return cb err if err?
cb null, db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment