不要怂,就是干,撸起袖子干!

Commit 42050b70 by Mick Hansen

fix(model): underscored should not override custom timestamp names, fixes #2641

1 parent d94bd9eb
...@@ -25,9 +25,6 @@ module.exports = (function() { ...@@ -25,9 +25,6 @@ module.exports = (function() {
var Model = function(name, attributes, options) { var Model = function(name, attributes, options) {
this.options = Utils._.extend({ this.options = Utils._.extend({
timestamps: true, timestamps: true,
createdAt: 'createdAt',
updatedAt: 'updatedAt',
deletedAt: 'deletedAt',
instanceMethods: {}, instanceMethods: {},
classMethods: {}, classMethods: {},
validate: {}, validate: {},
...@@ -140,14 +137,14 @@ module.exports = (function() { ...@@ -140,14 +137,14 @@ module.exports = (function() {
// Setup names of timestamp attributes // Setup names of timestamp attributes
this._timestampAttributes = {}; this._timestampAttributes = {};
if (this.options.timestamps) { if (this.options.timestamps) {
if (this.options.createdAt) { if (this.options.createdAt !== false) {
this._timestampAttributes.createdAt = Utils._.underscoredIf(this.options.createdAt, this.options.underscored); this._timestampAttributes.createdAt = this.options.createdAt || Utils._.underscoredIf('createdAt', this.options.underscored);
} }
if (this.options.updatedAt) { if (this.options.updatedAt !== false) {
this._timestampAttributes.updatedAt = Utils._.underscoredIf(this.options.updatedAt, this.options.underscored); this._timestampAttributes.updatedAt = this.options.updatedAt || Utils._.underscoredIf('updatedAt', this.options.underscored);
} }
if (this.options.paranoid && this.options.deletedAt) { if (this.options.paranoid !== false && this.options.deletedAt) {
this._timestampAttributes.deletedAt = Utils._.underscoredIf(this.options.deletedAt, this.options.underscored); this._timestampAttributes.deletedAt = this.options.deletedAt || Utils._.underscoredIf('deletedAt', this.options.underscored);
} }
} }
......
...@@ -519,9 +519,9 @@ module.exports = (function() { ...@@ -519,9 +519,9 @@ module.exports = (function() {
* @param {Boolean} [options.indexes[].unique=false] Should the index by unique? Can also be triggered by setting type to `UNIQUE` * @param {Boolean} [options.indexes[].unique=false] Should the index by unique? Can also be triggered by setting type to `UNIQUE`
* @param {Boolean} [options.indexes[].concurrently=false] PostgreSQL will build the index without taking any write locks. Postgres only * @param {Boolean} [options.indexes[].concurrently=false] PostgreSQL will build the index without taking any write locks. Postgres only
* @param {Array<String|Object} [options.indexes[].fields] An array of the fields to index. Each field can either be a string containing the name of the field, a sequelize object (e.g `sequelize.fn`), or an object with the following attributes: `attribute` (field name), `length` (create a prefix index of length chars), `order` (the direction the column should be sorted in), `collate` (the collation (sort order) for the column) * @param {Array<String|Object} [options.indexes[].fields] An array of the fields to index. Each field can either be a string containing the name of the field, a sequelize object (e.g `sequelize.fn`), or an object with the following attributes: `attribute` (field name), `length` (create a prefix index of length chars), `order` (the direction the column should be sorted in), `collate` (the collation (sort order) for the column)
* @param {String|Boolean} [options.createdAt] Override the name of the createdAt column if a string is provided, or disable it if false. Timestamps must be true * @param {String|Boolean} [options.createdAt] Override the name of the createdAt column if a string is provided, or disable it if false. Timestamps must be true. Not affected by underscored setting.
* @param {String|Boolean} [options.updatedAt] Override the name of the updatedAt column if a string is provided, or disable it if false. Timestamps must be true * @param {String|Boolean} [options.updatedAt] Override the name of the updatedAt column if a string is provided, or disable it if false. Timestamps must be true. Not affected by underscored setting.
* @param {String|Boolean} [options.deletedAt] Override the name of the deletedAt column if a string is provided, or disable it if false. Timestamps must be true * @param {String|Boolean} [options.deletedAt] Override the name of the deletedAt column if a string is provided, or disable it if false. Timestamps must be true. Not affected by underscored setting.
* @param {String} [options.tableName] Defaults to pluralized model name, unless freezeTableName is true, in which case it uses model name verbatim * @param {String} [options.tableName] Defaults to pluralized model name, unless freezeTableName is true, in which case it uses model name verbatim
* @param {Object} [options.getterMethods] Provide getter functions that work like those defined per column. If you provide a getter method with the same name as a column, it will be used to access the value of that column. If you provide a name that does not match a column, this function will act as a virtual getter, that can fetch multiple other values * @param {Object} [options.getterMethods] Provide getter functions that work like those defined per column. If you provide a getter method with the same name as a column, it will be used to access the value of that column. If you provide a name that does not match a column, this function will act as a virtual getter, that can fetch multiple other values
* @param {Object} [options.setterMethods] Provide setter functions that work like those defined per column. If you provide a setter method with the same name as a column, it will be used to update the value of that column. If you provide a name that does not match a column, this function will act as a virtual setter, that can act on and set other values, but will not be persisted * @param {Object} [options.setterMethods] Provide setter functions that work like those defined per column. If you provide a setter method with the same name as a column, it will be used to update the value of that column. If you provide a name that does not match a column, this function will act as a virtual setter, that can act on and set other values, but will not be persisted
......
...@@ -588,7 +588,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -588,7 +588,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
return this.sequelize.sync({force: true}).then(function() { return this.sequelize.sync({force: true}).then(function() {
return User.create({}, {logging: console.log}).then(function(user) { return User.create({}).then(function(user) {
expect(user).to.be.ok; expect(user).to.be.ok;
expect(user.created_time).to.be.ok; expect(user.created_time).to.be.ok;
expect(user.updated_time).to.be.ok; expect(user.updated_time).to.be.ok;
...@@ -596,6 +596,27 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -596,6 +596,27 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
it('works with custom timestamps and underscored', function() {
var User = this.sequelize.define('User', {
}, {
createdAt: 'createdAt',
updatedAt: 'updatedAt',
underscored: true
});
return this.sequelize.sync({force: true}).then(function() {
return User.create({}).then(function(user) {
expect(user).to.be.ok;
expect(user.createdAt).to.be.ok;
expect(user.updatedAt).to.be.ok;
expect(user.created_at).not.to.be.ok;
expect(user.updated_at).not.to.be.ok;
});
});
});
if (current.dialect.supports.transactions) { if (current.dialect.supports.transactions) {
it('supports transactions', function() { it('supports transactions', function() {
var self = this; var self = this;
......
'use strict';
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + '/../../../lib/data-types')
, current = Support.sequelize;
describe(Support.getTestDialectTeaser('Model'), function() {
describe('define', function () {
it('should allow custom timestamps with underscored: true', function () {
var Model
, instance;
Model = current.define('User', {}, {
createdAt : 'createdAt',
updatedAt : 'updatedAt',
timestamps : true,
underscored : true
});
expect(Model.rawAttributes.createdAt).to.be.defined;
expect(Model.rawAttributes.updatedAt).to.be.defined;
expect(Model._timestampAttributes.createdAt).to.equal('createdAt');
expect(Model._timestampAttributes.updatedAt).to.equal('updatedAt');
expect(Model.rawAttributes.created_at).not.to.be.defined;
expect(Model.rawAttributes.updated_at).not.to.be.defined;
});
});
});
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!