Skip to content

Commit

Permalink
feat: add support for Sequelize v7 (#124)
Browse files Browse the repository at this point in the history
* Add support for Sequelize v7

* Add v7 support for properties

---------

Co-authored-by: Sascha Depold <[email protected]>
  • Loading branch information
sdepold and Sascha Depold authored Dec 4, 2023
1 parent 061c6f0 commit dcddbd6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class Property extends BaseProperty {
if (this.sequelizePath.references === 'string') {
return this.sequelizePath.references as string;
} if (this.sequelizePath.references && typeof this.sequelizePath.references !== 'string') {
// Sequelize v7
if ((this.sequelizePath.references as any).table?.tableName) {
return (this.sequelizePath.references as any).table?.tableName as string;
}
// Sequelize v4+
if (this.sequelizePath.references.model && typeof this.sequelizePath.references.model !== 'string') {
return (this.sequelizePath.references?.model as any)?.tableName as string;
}
Expand Down
12 changes: 10 additions & 2 deletions src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ class Resource extends BaseResource {

rawAttributes(): Record<string, ModelAttributeColumnOptions> {
// different sequelize versions stores attributes in different places
// .modelDefinition.attributes => sequelize ^7.0.0
// .rawAttributes => sequelize ^5.0.0
// .attributes => sequelize ^4.0.0
return ((this.SequelizeModel as any).attributes
|| ((this.SequelizeModel as any).modelDefinition?.attributes && Object.fromEntries((this.SequelizeModel as any).modelDefinition?.attributes))
|| (this.SequelizeModel as any).rawAttributes) as Record<string, ModelAttributeColumnOptions>;
}

Expand All @@ -54,11 +56,17 @@ class Resource extends BaseResource {
}

name(): string {
return this.SequelizeModel.tableName;
// different sequelize versions stores attributes in different places
// .modelDefinition.table => sequelize ^7.0.0
// .tableName => sequelize ^4.0.0
return (
(this.SequelizeModel as any).modelDefinition?.table?.tableName
|| this.SequelizeModel.tableName
);
}

id(): string {
return this.SequelizeModel.tableName;
return this.name();
}

properties(): Array<BaseProperty> {
Expand Down

0 comments on commit dcddbd6

Please sign in to comment.