Skip to content

Commit dcddbd6

Browse files
sdepoldSascha Depold
and
Sascha Depold
authored
feat: add support for Sequelize v7 (#124)
* Add support for Sequelize v7 * Add v7 support for properties --------- Co-authored-by: Sascha Depold <[email protected]>
1 parent 061c6f0 commit dcddbd6

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/property.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class Property extends BaseProperty {
7373
if (this.sequelizePath.references === 'string') {
7474
return this.sequelizePath.references as string;
7575
} if (this.sequelizePath.references && typeof this.sequelizePath.references !== 'string') {
76+
// Sequelize v7
77+
if ((this.sequelizePath.references as any).table?.tableName) {
78+
return (this.sequelizePath.references as any).table?.tableName as string;
79+
}
80+
// Sequelize v4+
7681
if (this.sequelizePath.references.model && typeof this.sequelizePath.references.model !== 'string') {
7782
return (this.sequelizePath.references?.model as any)?.tableName as string;
7883
}

src/resource.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ class Resource extends BaseResource {
3737

3838
rawAttributes(): Record<string, ModelAttributeColumnOptions> {
3939
// different sequelize versions stores attributes in different places
40+
// .modelDefinition.attributes => sequelize ^7.0.0
4041
// .rawAttributes => sequelize ^5.0.0
4142
// .attributes => sequelize ^4.0.0
4243
return ((this.SequelizeModel as any).attributes
44+
|| ((this.SequelizeModel as any).modelDefinition?.attributes && Object.fromEntries((this.SequelizeModel as any).modelDefinition?.attributes))
4345
|| (this.SequelizeModel as any).rawAttributes) as Record<string, ModelAttributeColumnOptions>;
4446
}
4547

@@ -54,11 +56,17 @@ class Resource extends BaseResource {
5456
}
5557

5658
name(): string {
57-
return this.SequelizeModel.tableName;
59+
// different sequelize versions stores attributes in different places
60+
// .modelDefinition.table => sequelize ^7.0.0
61+
// .tableName => sequelize ^4.0.0
62+
return (
63+
(this.SequelizeModel as any).modelDefinition?.table?.tableName
64+
|| this.SequelizeModel.tableName
65+
);
5866
}
5967

6068
id(): string {
61-
return this.SequelizeModel.tableName;
69+
return this.name();
6270
}
6371

6472
properties(): Array<BaseProperty> {

0 commit comments

Comments
 (0)