Add $now as shortcut default value for date property
This commit is contained in:
parent
b4189bf997
commit
aba7d0dc10
|
@ -208,6 +208,7 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
|
||||||
for (k = 0; k < size; k++) {
|
for (k = 0; k < size; k++) {
|
||||||
p = keys[k];
|
p = keys[k];
|
||||||
propVal = self.__data[p];
|
propVal = self.__data[p];
|
||||||
|
var type = properties[p].type;
|
||||||
|
|
||||||
// Set default values
|
// Set default values
|
||||||
if (propVal === undefined) {
|
if (propVal === undefined) {
|
||||||
|
@ -222,6 +223,8 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
|
||||||
} else {
|
} else {
|
||||||
def = def();
|
def = def();
|
||||||
}
|
}
|
||||||
|
} else if (type.name === 'Date' && def === '$now') {
|
||||||
|
def = new Date();
|
||||||
}
|
}
|
||||||
// FIXME: We should coerce the value
|
// FIXME: We should coerce the value
|
||||||
// will implement it after we refactor the PropertyDefinition
|
// will implement it after we refactor the PropertyDefinition
|
||||||
|
@ -230,7 +233,6 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle complex types (JSON/Object)
|
// Handle complex types (JSON/Object)
|
||||||
var type = properties[p].type;
|
|
||||||
if (!BASE_TYPES[type.name]) {
|
if (!BASE_TYPES[type.name]) {
|
||||||
|
|
||||||
if (typeof self.__data[p] !== 'object' && self.__data[p]) {
|
if (typeof self.__data[p] !== 'object' && self.__data[p]) {
|
||||||
|
|
|
@ -442,6 +442,35 @@ describe('manipulation', function () {
|
||||||
person.isNewRecord().should.be.true;
|
person.isNewRecord().should.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should report current date when using $now as default value for date property',
|
||||||
|
function (done) {
|
||||||
|
var CustomModel = db.define('CustomModel', {
|
||||||
|
createdAt: { type: Date, default: '$now' }
|
||||||
|
});
|
||||||
|
|
||||||
|
var now = Date.now();
|
||||||
|
|
||||||
|
var myCustomModel = CustomModel.create(function (err, m) {
|
||||||
|
m.createdAt.should.be.instanceOf(Date);
|
||||||
|
(m.createdAt >= now).should.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should report \'$now\' when using $now as default value for string property',
|
||||||
|
function (done) {
|
||||||
|
var CustomModel = db.define('CustomModel', {
|
||||||
|
now: { type: String, default: '$now' }
|
||||||
|
});
|
||||||
|
|
||||||
|
var myCustomModel = CustomModel.create(function (err, m) {
|
||||||
|
m.now.should.be.instanceOf(String);
|
||||||
|
m.now.should.equal('$now');
|
||||||
|
});
|
||||||
|
|
||||||
|
done();
|
||||||
|
});
|
||||||
// it('should work when constructor called as function', function() {
|
// it('should work when constructor called as function', function() {
|
||||||
// var p = Person({name: 'John Resig'});
|
// var p = Person({name: 'John Resig'});
|
||||||
// p.should.be.an.instanceOf(Person);
|
// p.should.be.an.instanceOf(Person);
|
||||||
|
|
Loading…
Reference in New Issue