Permalink
Browse files

Allow Kin to take a reference to Mongoose. In the future we should ma…

…ke this more generic to allow support for other DBs.
  • Loading branch information...
1 parent 8faff84 commit 1fdb3d75387edda1da7c34fdac1a461b5f058aa0 @timoxley timoxley committed Dec 16, 2011
Showing with 16 additions and 9 deletions.
  1. +1 −1 index.js
  2. +8 −6 lib/kin.js
  3. +4 −0 tests/examples/kin-examples.js
  4. +3 −2 tests/kin_test.js
View
@@ -1 +1 @@
-module.exports = require('./lib/kin');
+module.exports = require('./lib/kin')
View
@@ -4,11 +4,12 @@ var path = require('path'),
async = require('async'),
_ = require('underscore')
-var Kin = function() {
+// if you want to use mongoose with kin, pass your mongoose instance in
+var Kin = function(mongoose) {
// constructor
+ this.mongoose = mongoose
this._blueprints = {}
}
-
Kin.prototype.generate = function(type, overrideProperties, callback) {
// properties is optional
if (overrideProperties.length == 2 && typeof overrideProperties == 'function') {
@@ -27,10 +28,11 @@ Kin.prototype.generate = function(type, overrideProperties, callback) {
var Model
if (modelType) {
if (typeof modelType == 'string') {
- modelType = require('mongoose').models[modelType]
- if (!modelType) {
- return callback(new Error('Invalid string model in '+type+' blueprint: ' + modelType))
- }
+ // mongoose instance must be passed in
+ modelType = this.mongoose && this.mongoose.models[modelType]
+ if (!modelType) {
+ return callback(new Error('Invalid string model in '+type+' blueprint: ' + this.blueprint(type)._model))
+ }
}
Model = this.generator(modelType)
}
@@ -220,6 +220,10 @@ var GeneratedUserWithDocumentsExample =
var mongoose = require('mongoose')
mongoose.connect('kin_examples')
+// If you want to use mongoose with Kin, you need to pass your reference to mongoose to Kin
+
+kin.mongoose = mongoose
+
/* Some fixtures we prepared earlier */
User = require('../fixtures').User // Same as our user before, except as a Mongoose model
Stream = require('../fixtures').Stream // Imagine a stream is like an RSS feed containing 'Activities'
View
@@ -4,7 +4,8 @@ var testCase = require('nodeunit').testCase,
EventEmitter = require('events').EventEmitter,
Kin = require('../lib/kin'),
uuid = require('node-uuid'),
- ObjectId = require('mongoose').Types.ObjectId,
+ mongoose = require('mongoose'),
+ ObjectId = mongoose.Types.ObjectId,
Faker = require('Faker'),
_ = require('underscore')
@@ -17,7 +18,7 @@ var kin
var tests = testCase({
setUp: function(callback) {
- kin = new Kin()
+ kin = new Kin(mongoose)
// mock 'models'
User = function(properties) {
this._id = uuid()

0 comments on commit 1fdb3d7

Please sign in to comment.