mongoose-gen
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -8,2 +8,3 @@ var util = require('./util.js'); | ||
var ObjectId = mongoose.Schema.ObjectId; | ||
var connection; | ||
@@ -33,2 +34,9 @@ | ||
var setConnection = function (_connection) { | ||
if (!_connection instanceof mongoose.Connection) | ||
throw new Error('mongoose.Connection expected but got '+_connection); | ||
connection = _connection; | ||
}; | ||
var get = function (param, key) { | ||
@@ -90,2 +98,3 @@ var fn = hash && hash[param] && hash[param][key]; | ||
if (!util.is('Object', descriptor)) throw new Error('expected object for param descriptor'); | ||
if (!connection) throw new Error('expected a mongoose.Connection params. Call setConnection() before schema()'); | ||
@@ -111,2 +120,3 @@ var definition = convert(descriptor); | ||
exports.setDefault = set('default'); | ||
exports.setConnection = setConnection; | ||
exports.schema = schema; |
@@ -5,3 +5,3 @@ { | ||
"description": "generates mongoose schemas from json documents, supports DBRefs and Arrays of DBRef", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"homepage": "https://github.com/topliceanu/mongoose-gen", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -23,7 +23,7 @@ mongoose-gen | ||
npm install | ||
npm test | ||
npm test | ||
Usage | ||
----- | ||
Usage Example | ||
------------- | ||
@@ -39,3 +39,3 @@ book.json | ||
index.js | ||
index.js | ||
@@ -45,3 +45,7 @@ var generator = require('mongoose-gen'); | ||
var mongoose = require('mongoose'); | ||
mongoose.connect('mongodb://localhost/test'); | ||
// configuration | ||
generator.setConnection(mongoose); // make sure you connected | ||
generator.setValidator('validateBookYear', function (value) { | ||
@@ -119,2 +123,18 @@ return true; | ||
API | ||
--- | ||
generator.setConnection(mongoose: mongoose.Connection): undefined // REQUIRED before compiling a json descriptor | ||
generator.setValidator(validator: Function): undefined | ||
generator.setDefault(default: Function): undefined | ||
generator.setSetter(getter: Function): undefined | ||
generator.setGetter(setter: Function): undefined | ||
generator.schema(name: String, json: Object): mongoose.Model | ||
Setters, Getters, Defaults and Validators | ||
@@ -121,0 +141,0 @@ ------------------------------- |
{ | ||
"prop1": {"type": "String", "lowercase": true, "uppercase": false, "trim": true, "match": "^test$", "enum": ["test-me", "test-you"], "required": true, "select": true, "index": true, "default": "defaultString", "get": "customGet", "set": "customSet", "validate": "customValidator"}, | ||
"prop1": {"type": "String", "lowercase": true, "uppercase": false, "trim": true, "match": "^default$", "enum": ["default"], "required": true, "select": true, "index": true, "default": "defaultString", "get": "customGet", "set": "customSet", "validate": "customValidator"}, | ||
"prop2": {"type": "Number", "min": 0, "max": 1000, "enum": [1,2,3,4,5], "required": true, "select": true, "index": true, "default": "defaultNumber", "get": "customGet", "set": "customSet", "validate": "customValidator"}, | ||
@@ -7,4 +7,4 @@ "prop3": {"type": "Boolean", "required": true, "select": true, "index": true, "default": "defaultBoolean", "get": "customGet", "set": "customSet", "validate": "customValidator"}, | ||
"prop5": {"type": "Buffer", "required": true, "select": true, "index": true, "default": "defaultBuffer", "get": "customGet", "set": "customSet", "validate": "customValidator"}, | ||
"prop6": {"type": "ObjectId", "ref": "SomeOtherType", "required": true, "select": true, "index": true, "get": "customGet", "set": "customSet", "validate": "customValidator"}, | ||
"prop6": {"type": "ObjectId", "ref": "SomeOtherType", "required": false, "select": true, "index": true, "get": "customGet", "set": "customSet", "validate": "customValidator"}, | ||
"prop7": {"type": "Mixed"} | ||
} |
@@ -97,2 +97,3 @@ var fs = require('fs'); | ||
describe('convert the json into a mongoose schema', function () { | ||
before( function (next) { | ||
@@ -103,5 +104,3 @@ var that = this; | ||
try { | ||
var object = JSON.parse(data); | ||
that.definition = generator._convert(object); | ||
that.Model = generator.schema('Test', object); | ||
that.descriptor = JSON.parse(data); | ||
return next(); | ||
@@ -115,7 +114,21 @@ } | ||
it('should not work if it does not have a connection', function () { | ||
assert.throws( function () { | ||
generator.schema('Test', this.descriptor); | ||
}); | ||
}); | ||
it('should set a correct connection object', function () { | ||
assert.doesNotThrow( function () { | ||
generator.setConnection(mongoose); | ||
}); | ||
}); | ||
it('should return a mongoose.Model instance', function () { | ||
var definition = generator._convert(this.descriptor); | ||
this.Model = generator.schema('Test', this.descriptor); | ||
assert.ok(util.is('Function', this.Model)); | ||
}); | ||
it('should permit creating a new object', function () { | ||
it('should permit creating a new object', function (done) { | ||
var doc = new this.Model(); | ||
@@ -127,3 +140,5 @@ assert.equal(doc.prop1, 'default'); | ||
assert.equal(doc.prop5.toString('UTF-8'), 'default'); | ||
doc.save(); | ||
done(); | ||
}); | ||
}); |
15401
227
178