Merge pull request #197 from anatoliychakkaev/master
Implement schema.extendModel(model, props)
This commit is contained in:
commit
a1678a3802
|
@ -260,7 +260,6 @@ Schema.prototype.define = function defineClass(className, properties, settings)
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define single property named `prop` on `model`
|
* Define single property named `prop` on `model`
|
||||||
*
|
*
|
||||||
|
@ -276,6 +275,40 @@ Schema.prototype.defineProperty = function (model, prop, params) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extend existing model with bunch of properties
|
||||||
|
*
|
||||||
|
* @param {String} model - name of model
|
||||||
|
* @param {Object} props - hash of properties
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
*
|
||||||
|
* // Instead of doing this:
|
||||||
|
*
|
||||||
|
* // amend the content model with competition attributes
|
||||||
|
* db.defineProperty('Content', 'competitionType', { type: String });
|
||||||
|
* db.defineProperty('Content', 'expiryDate', { type: Date, index: true });
|
||||||
|
* db.defineProperty('Content', 'isExpired', { type: Boolean, index: true });
|
||||||
|
*
|
||||||
|
* // schema.extend allows to
|
||||||
|
* // extend the content model with competition attributes
|
||||||
|
* db.extendModel('Content', {
|
||||||
|
* competitionType: String,
|
||||||
|
* expiryDate: { type: Date, index: true },
|
||||||
|
* isExpired: { type: Boolean, index: true }
|
||||||
|
* });
|
||||||
|
*/
|
||||||
|
Schema.prototype.extendModel = function (model, props) {
|
||||||
|
var t = this;
|
||||||
|
Object.keys(props).forEach(function (propName) {
|
||||||
|
var definition = props[propName];
|
||||||
|
if (typeof definition === 'function' || typeof definition !== 'object') {
|
||||||
|
definition = {type: definition};
|
||||||
|
}
|
||||||
|
t.defineProperty(model, propName, definition);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Drop each model table and re-create.
|
* Drop each model table and re-create.
|
||||||
* This method make sense only for sql adapters.
|
* This method make sense only for sql adapters.
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
var jdb = require('jugglingdb'),
|
var jdb = require('../'),
|
||||||
Schema = jdb.Schema,
|
Schema = jdb.Schema,
|
||||||
test = jdb.test;
|
test = jdb.test;
|
||||||
|
|
||||||
|
|
|
@ -121,11 +121,17 @@ function testOrm(schema) {
|
||||||
approved: Boolean,
|
approved: Boolean,
|
||||||
joinedAt: Date,
|
joinedAt: Date,
|
||||||
age: Number,
|
age: Number,
|
||||||
passwd: { type: String, index: true },
|
passwd: { type: String, index: true }
|
||||||
|
});
|
||||||
|
|
||||||
|
schema.extendModel('User', {
|
||||||
settings: { type: Schema.JSON },
|
settings: { type: Schema.JSON },
|
||||||
extra: Object
|
extra: Object
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var newuser = new User({settings: {hey: 'you'}});
|
||||||
|
test.ok(newuser.settings);
|
||||||
|
|
||||||
Post = schema.define('Post', {
|
Post = schema.define('Post', {
|
||||||
title: { type: String, length: 255, index: true },
|
title: { type: String, length: 255, index: true },
|
||||||
subject: { type: String },
|
subject: { type: String },
|
||||||
|
|
Loading…
Reference in New Issue