Skip to content

Commit

Permalink
feat: option to allow specifying the default precision for auto-gener…
Browse files Browse the repository at this point in the history
…ated timestamp columns (#16330)

Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
  • Loading branch information
papandreou and WikiRik authored Sep 11, 2023
1 parent df30331 commit db7fe12
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/core/src/model-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,11 @@ Timestamp attributes are managed automatically by Sequelize, and their nullabili
- or disable the automatic timestamp attributes.`);
}

const { defaultTimestampPrecision } = this.#sequelize.options;

this.rawAttributes[attributeName] = {
// @ts-expect-error -- this property is not mandatory in timestamp attributes
type: DataTypes.DATE(6),
type: typeof defaultTimestampPrecision === 'number' ? DataTypes.DATE(defaultTimestampPrecision) : DataTypes.DATE,
...this.rawAttributes[attributeName],
allowNull,
_autoGenerated: true,
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/sequelize.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,14 @@ export interface Options extends Logging {
*/
keepDefaultTimezone?: boolean;

/**
* The precision for the `createdAt`/`updatedAt`/`deletedAt` DATETIME columns that Sequelize adds to models.
* Can be a number between 0 and 6, or null to use the default precision of the database. Defaults to 6.
*
* @default 6
*/
defaultTimestampPrecision?: number | null;

/**
* A flag that defines if null values should be passed to SQL queries or not.
*
Expand Down Expand Up @@ -462,6 +470,7 @@ export interface NormalizedOptions extends RequiredBy<Options,
| 'timezone'
| 'disableClsTransactions'
| 'defaultTransactionNestMode'
| 'defaultTimestampPrecision'
> {
readonly replication: NormalizedReplicationOptions;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export class Sequelize extends SequelizeTypeScript {
* @param {object} [options.sync={}] Default options for sequelize.sync
* @param {string} [options.timezone='+00:00'] The timezone used when converting a date from the database into a JavaScript date. The timezone is also used to SET TIMEZONE when connecting to the server, to ensure that the result of NOW, CURRENT_TIMESTAMP and other time related functions have in the right timezone. For best cross platform performance use the format +/-HH:MM. Will also accept string versions of timezones supported by Intl.Locale (e.g. 'America/Los_Angeles'); this is useful to capture daylight savings time changes.
* @param {boolean} [options.keepDefaultTimezone=false] A flag that defines if the default timezone is used to convert dates from the database.
* @param {number|null} [options.defaultTimestampPrecision] The precision for the `createdAt`/`updatedAt`/`deletedAt` DATETIME columns that Sequelize adds to models. Can be a number between 0 and 6, or null to use the default precision of the database. Defaults to 6.
* @param {string|boolean} [options.clientMinMessages='warning'] (Deprecated) The PostgreSQL `client_min_messages` session parameter. Set to `false` to not override the database's default.
* @param {boolean} [options.standardConformingStrings=true] The PostgreSQL `standard_conforming_strings` session parameter. Set to `false` to not set the option. WARNING: Setting this to false may expose vulnerabilities and is not recommended!
* @param {Function} [options.logging=console.log] A function that gets executed every time Sequelize would log something. Function may receive multiple parameters but only first one is printed by `console.log`. To print all values use `(...msg) => console.log(msg)`
Expand Down Expand Up @@ -273,6 +274,7 @@ export class Sequelize extends SequelizeTypeScript {
logQueryParameters: false,
disableClsTransactions: false,
defaultTransactionNestMode: TransactionNestMode.reuse,
defaultTimestampPrecision: 6,
...options,
pool: defaults(options.pool || {}, {
max: 5,
Expand Down
42 changes: 42 additions & 0 deletions packages/core/test/unit/model/define.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,5 +325,47 @@ describe('Model', () => {
}
});
});

describe('with defaultTimestampPrecision', () => {
describe('not specified', () => {
it('should add the automatic timestamp columns with the default precision of 6', async () => {
const newSequelize = createSequelizeInstance();
const MyModel = newSequelize.define('MyModel', {}, { paranoid: true });

const { physicalAttributes } = MyModel.modelDefinition;
expect(physicalAttributes.get('createdAt')).to.have.nested.property('type.options.precision', 6);
expect(physicalAttributes.get('updatedAt')).to.have.nested.property('type.options.precision', 6);
expect(physicalAttributes.get('deletedAt')).to.have.nested.property('type.options.precision', 6);
});
});

describe('set to a number', () => {
it('should add the automatic timestamp columns with the specified precision', async () => {
const newSequelize = createSequelizeInstance({
defaultTimestampPrecision: 4,
});
const MyModel = newSequelize.define('MyModel', {}, { paranoid: true });

const { physicalAttributes } = MyModel.modelDefinition;
expect(physicalAttributes.get('createdAt')).to.have.nested.property('type.options.precision', 4);
expect(physicalAttributes.get('updatedAt')).to.have.nested.property('type.options.precision', 4);
expect(physicalAttributes.get('deletedAt')).to.have.nested.property('type.options.precision', 4);
});
});

describe('set to null', () => {
it('should add the automatic timestamp columns with no specified precision', async () => {
const newSequelize = createSequelizeInstance({
defaultTimestampPrecision: null,
});
const MyModel = newSequelize.define('MyModel', {}, { paranoid: true });

const { physicalAttributes } = MyModel.modelDefinition;
expect(physicalAttributes.get('createdAt')).to.have.nested.property('type.options.precision', undefined);
expect(physicalAttributes.get('updatedAt')).to.have.nested.property('type.options.precision', undefined);
expect(physicalAttributes.get('deletedAt')).to.have.nested.property('type.options.precision', undefined);
});
});
});
});
});

0 comments on commit db7fe12

Please sign in to comment.