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(datasource-sql): support for paranoid tables with snake case timestamp fields #1170

Merged
merged 1 commit into from
Sep 11, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions packages/datasource-sql/src/orm-builder/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import { LatestIntrospection, Table } from '../introspection/types';

type TableOrView = Table & { view?: boolean };

const createdAtFields = ['createdAt', 'created_at'];
const updatedAtFields = ['updatedAt', 'updated_at'];
const deletedAtFields = ['deletedAt', 'deleted_at'];
const timestampFields = [...createdAtFields, ...updatedAtFields];

const autoTimestampFieldsMap = {
created_at: 'createdAt',
updated_at: 'updatedAt',
deleted_at: 'deletedAt',
};

export default class ModelBuilder {
static defineModels(
sequelize: Sequelize,
Expand Down Expand Up @@ -39,6 +50,7 @@ export default class ModelBuilder {
timestamps: hasTimestamps,
paranoid: isParanoid,
schema: table.schema,
...this.getAutoTimestampFieldsOverride(table),
});

// @see https://sequelize.org/docs/v6/other-topics/legacy/#primary-keys
Expand All @@ -65,8 +77,9 @@ export default class ModelBuilder {

for (const column of table.columns) {
const isExplicit =
!(hasTimestamps && (column.name === 'updatedAt' || column.name === 'createdAt')) &&
!(isParanoid && column.name === 'deletedAt');
!(hasTimestamps && timestampFields.includes(column.name)) &&
!(isParanoid && deletedAtFields.includes(column.name));

const type = SequelizeTypeFactory.makeType(dialect, column.type, table.name, column.name);

if (column.defaultValue && column.isLiteralDefaultValue) {
Expand Down Expand Up @@ -150,12 +163,22 @@ export default class ModelBuilder {

private static hasTimestamps(table: Table): boolean {
return (
!!table.columns.find(c => c.name === 'createdAt') &&
!!table.columns.find(c => c.name === 'updatedAt')
!!table.columns.find(c => createdAtFields.includes(c.name)) &&
!!table.columns.find(c => updatedAtFields.includes(c.name))
);
}

private static isParanoid(table: Table): boolean {
return !!table.columns.find(c => c.name === 'deletedAt');
return !!table.columns.find(c => deletedAtFields.includes(c.name));
}

private static getAutoTimestampFieldsOverride(table: Table) {
return table.columns
.filter(column => !!autoTimestampFieldsMap[column.name])
.reduce((acc, column) => {
acc[autoTimestampFieldsMap[column.name]] = column.name;

return acc;
}, {});
}
}
48 changes: 48 additions & 0 deletions packages/datasource-sql/test/orm-builder/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,52 @@ describe('ModelBuilder', () => {
});
});
});

describe('when auto timestamp fields are in snake_case', () => {
it('should override the corresponding properties', () => {
const sequelize = new Sequelize('postgres://');
const columns = [
{
name: 'created_at',
allowNull: false,
autoIncrement: false,
primaryKey: false,
constraints: [],
defaultValue: null,
type: { type: 'scalar', subType: 'DATE' } as unknown as ColumnType,
isLiteralDefaultValue: false,
},
{
name: 'updated_at',
allowNull: false,
autoIncrement: false,
primaryKey: false,
constraints: [],
defaultValue: null,
type: { type: 'scalar', subType: 'DATE' } as unknown as ColumnType,
isLiteralDefaultValue: false,
},
{
name: 'deleted_at',
allowNull: false,
autoIncrement: false,
primaryKey: false,
constraints: [],
defaultValue: null,
type: { type: 'scalar', subType: 'DATE' } as unknown as ColumnType,
isLiteralDefaultValue: false,
},
];
const tables = [{ columns, name: 'aModel', schema: undefined, unique: [] }] as Table[];

ModelBuilder.defineModels(sequelize, () => {}, { ...defaultIntrospection, tables });

expect(sequelize.models.aModel).toBeDefined();
expect(sequelize.models.aModel.rawAttributes.created_at).toBeDefined();
expect(sequelize.models.aModel.rawAttributes.updated_at).toBeDefined();
expect(sequelize.models.aModel.rawAttributes.deleted_at).toBeDefined();
expect(sequelize.models.aModel.options.paranoid).toBeTruthy();
expect(sequelize.models.aModel.options.timestamps).toBeTruthy();
});
});
});
Loading