Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(model): handle true timestamp fields correctly #12580

Merged
merged 5 commits into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1107,16 +1107,28 @@ class Model {

// setup names of timestamp attributes
if (this.options.timestamps) {
for (const key of ['createdAt', 'updatedAt', 'deletedAt']) {
if (!['undefined', 'string', 'boolean'].includes(typeof this.options[key])) {
throw new Error(`Value for "${key}" option must be a string or a boolean, got ${typeof this.options[key]}`);
}
if (this.options[key] === '') {
throw new Error(`Value for "${key}" option cannot be an empty string`);
}
}

if (this.options.createdAt !== false) {
this._timestampAttributes.createdAt = this.options.createdAt || 'createdAt';
this._timestampAttributes.createdAt =
typeof this.options.createdAt === 'string' ? this.options.createdAt : 'createdAt';
this._readOnlyAttributes.add(this._timestampAttributes.createdAt);
}
if (this.options.updatedAt !== false) {
this._timestampAttributes.updatedAt = this.options.updatedAt || 'updatedAt';
this._timestampAttributes.updatedAt =
typeof this.options.updatedAt === 'string' ? this.options.updatedAt : 'updatedAt';
this._readOnlyAttributes.add(this._timestampAttributes.updatedAt);
}
if (this.options.paranoid && this.options.deletedAt !== false) {
this._timestampAttributes.deletedAt = this.options.deletedAt || 'deletedAt';
this._timestampAttributes.deletedAt =
typeof this.options.deletedAt === 'string' ? this.options.deletedAt : 'deletedAt';
this._readOnlyAttributes.add(this._timestampAttributes.deletedAt);
}
}
Expand Down
40 changes: 40 additions & 0 deletions test/integration/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,46 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(defaultFunction.callCount).to.equal(2);
});

it('should throw `TypeError` when value for updatedAt, createdAt, or deletedAt is neither string nor boolean', async function () {
const modelName = 'UserCol';
const attributes = { aNumber: Sequelize.INTEGER };

expect(() => {
this.sequelize.define(modelName, attributes, { timestamps: true, updatedAt: {} });
}).to.throw(Error, 'Value for "updatedAt" option must be a string or a boolean, got object');
expect(() => {
this.sequelize.define(modelName, attributes, { timestamps: true, createdAt: 100 });
}).to.throw(Error, 'Value for "createdAt" option must be a string or a boolean, got number');
expect(() => {
this.sequelize.define(modelName, attributes, { timestamps: true, deletedAt: () => {} });
}).to.throw(Error, 'Value for "deletedAt" option must be a string or a boolean, got function');
});

it('should allow me to use `true` as a value for updatedAt, createdAt, and deletedAt fields', async function () {
const UserTable = this.sequelize.define(
'UserCol',
{
aNumber: Sequelize.INTEGER
},
{
timestamps: true,
updatedAt: true,
createdAt: true,
deletedAt: true,
paranoid: true
}
);

await UserTable.sync({ force: true });
const user = await UserTable.create({ aNumber: 4 });
expect(user['true']).to.not.exist;
expect(user.updatedAt).to.exist;
expect(user.createdAt).to.exist;
await user.destroy();
await user.reload({ paranoid: false });
expect(user.deletedAt).to.exist;
});

it('should allow me to override updatedAt, createdAt, and deletedAt fields', async function () {
const UserTable = this.sequelize.define(
'UserCol',
Expand Down