Last active
August 29, 2015 14:11
-
-
Save gergelyke/3dd1fce2bf936c4b1ec0 to your computer and use it in GitHub Desktop.
User model
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
var db = require('db'); | |
// do some init here, or connect | |
db.init(); | |
var userModel = require('User')({ | |
db: db | |
}); | |
userModel.create(function (err, user) { | |
}); |
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
var test = require('tape'); | |
var userModel = require('User'); | |
test('it creates a user with id', function (t) { | |
var user = { | |
id: 1 | |
}; | |
var fakeDb = { | |
query: function (done) { | |
done(null, user); | |
} | |
} | |
userModel({ | |
db: fakeDb | |
}).create(function (err, user) { | |
t.equal(user.id, 1, 'User id should match'); | |
t.end(); | |
}) | |
}); |
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
function userModel (options) { | |
var db; | |
if (!options.db) { | |
throw new Error('Options.db is required'); | |
} | |
db = options.db; | |
return { | |
create: function (done) { | |
db.query('INSERT ...', done); | |
} | |
} | |
} | |
module.exports = userModel; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment