-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate relation associations
- Loading branch information
Victor Korzunin
committed
Sep 5, 2021
1 parent
23b1ece
commit 637e367
Showing
4 changed files
with
116 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,39 @@ | ||
import {Optional, Sequelize, ModelAttributes, Model, DataTypes} from 'sequelize'; | ||
import { Sequelize, Model, DataTypes, ModelCtor } from 'sequelize'; | ||
|
||
export const {{model.name}}Factory = (sequelize: Sequelize) => { | ||
const {{model.name}} = sequelize.define('{{model.name}}', { | ||
{{#each scalarFields}} | ||
{{name}}: { | ||
type: {{#if isList}}DataTypes.ARRAY(DataTypes.{{type}}){{else}}DataTypes.{{type}}{{/if}},{{#if (eq allowNull false)}} | ||
allowNull: {{allowNull}},{{/if}}{{#if isId}} | ||
primaryKey: {{isId}},{{/if}}{{#if isAutoincrement}} | ||
autoIncrement: {{isAutoincrement}},{{/if}}{{#if isUnique}} | ||
unique: {{isUnique}},{{/if}} | ||
class {{model.name}} extends Model { | ||
{{#if relationFields}} | ||
static associate(models: Record<string, ModelCtor<Model>>) { | ||
{{#each relationFields}} | ||
this.belongsTo(models.{{name}}, { targetKey: '{{targetKey}}', foreignKey: '{{foreignKey}}' }); | ||
{{/each}} | ||
} | ||
{{/if}} | ||
} | ||
|
||
{{model.name}}.init( | ||
{ | ||
{{#each scalarFields}} | ||
{{name}}: { | ||
type: {{#if isList}}DataTypes.ARRAY(DataTypes.{{type}}){{else}}DataTypes.{{type}}{{/if}},{{#if (eq allowNull false)}} | ||
allowNull: {{allowNull}},{{/if}}{{#if isId}} | ||
primaryKey: {{isId}},{{/if}}{{#if isAutoincrement}} | ||
autoIncrement: {{isAutoincrement}},{{/if}}{{#if isUnique}} | ||
unique: {{isUnique}},{{/if}} | ||
}, | ||
{{/each}} | ||
}, | ||
{{/each}} | ||
}, { | ||
tableName: '{{#if model.dbName}}{{model.dbName}}{{else}}{{model.name}}{{/if}}', | ||
timestamps: {{or (or hasCreatedAt hasUpdatedAt) hasDeletedAt}},{{#if (or (or hasCreatedAt hasUpdatedAt) hasDeletedAt)}}{{#if (eq hasCreatedAt false)}} | ||
createdAt: false,{{/if}}{{#if (eq hasUpdatedAt false)}} | ||
updatedAt: false,{{/if}}{{!-- {{#if (eq hasDeletedAt false)}} | ||
deletedAt: false,{{/if}} --}}{{#if hasDeletedAt}} | ||
paranoid: true,{{/if}}{{/if}} | ||
}); | ||
{ | ||
sequelize, | ||
modelName: '{{model.name}}', | ||
tableName: '{{#if model.dbName}}{{model.dbName}}{{else}}{{model.name}}{{/if}}', | ||
timestamps: {{or (or hasCreatedAt hasUpdatedAt) hasDeletedAt}},{{#if (or (or hasCreatedAt hasUpdatedAt) hasDeletedAt)}}{{#if (eq hasCreatedAt false)}} | ||
createdAt: false,{{/if}}{{#if (eq hasUpdatedAt false)}} | ||
updatedAt: false,{{/if}}{{!-- {{#if (eq hasDeletedAt false)}} | ||
deletedAt: false,{{/if}} --}}{{#if hasDeletedAt}} | ||
paranoid: true,{{/if}}{{/if}} | ||
} | ||
); | ||
|
||
return {{model.name}}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,31 @@ | ||
import {Optional, Sequelize, ModelAttributes, Model, DataTypes} from 'sequelize'; | ||
import { Sequelize, Model, DataTypes, ModelCtor } from 'sequelize'; | ||
|
||
export const PostFactory = (sequelize: Sequelize) => { | ||
const Post = sequelize.define('Post', { | ||
id: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
userId: { | ||
type: DataTypes.INTEGER, | ||
class Post extends Model { | ||
static associate(models: Record<string, ModelCtor<Model>>) { | ||
this.belongsTo(models.User, { targetKey: 'id', foreignKey: 'userId' }); | ||
} | ||
} | ||
|
||
Post.init( | ||
{ | ||
id: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
userId: { | ||
type: DataTypes.INTEGER, | ||
}, | ||
}, | ||
}, { | ||
tableName: 'Posts', | ||
timestamps: false, | ||
}); | ||
{ | ||
sequelize, | ||
modelName: 'Post', | ||
tableName: 'Posts', | ||
timestamps: false, | ||
} | ||
); | ||
|
||
return Post; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,55 @@ | ||
import {Optional, Sequelize, ModelAttributes, Model, DataTypes} from 'sequelize'; | ||
import { Sequelize, Model, DataTypes, ModelCtor } from 'sequelize'; | ||
|
||
export const UserFactory = (sequelize: Sequelize) => { | ||
const User = sequelize.define('User', { | ||
id: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
email: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
unique: true, | ||
}, | ||
weight: { | ||
type: DataTypes.FLOAT, | ||
}, | ||
is18: { | ||
type: DataTypes.BOOLEAN, | ||
}, | ||
name: { | ||
type: DataTypes.STRING, | ||
}, | ||
successorId: { | ||
type: DataTypes.INTEGER, | ||
}, | ||
keywords: { | ||
type: DataTypes.ARRAY(DataTypes.STRING), | ||
allowNull: false, | ||
}, | ||
biography: { | ||
type: DataTypes.JSONB, | ||
allowNull: false, | ||
}, | ||
}, { | ||
tableName: 'User', | ||
timestamps: true, | ||
updatedAt: false, | ||
}); | ||
class User extends Model { | ||
static associate(models: Record<string, ModelCtor<Model>>) { | ||
this.belongsTo(models.User, { targetKey: 'id', foreignKey: 'successorId' }); | ||
this.belongsTo(models.User, { targetKey: '', foreignKey: '' }); | ||
} | ||
} | ||
|
||
User.init( | ||
{ | ||
id: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
email: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
unique: true, | ||
}, | ||
weight: { | ||
type: DataTypes.FLOAT, | ||
}, | ||
is18: { | ||
type: DataTypes.BOOLEAN, | ||
}, | ||
name: { | ||
type: DataTypes.STRING, | ||
}, | ||
successorId: { | ||
type: DataTypes.INTEGER, | ||
}, | ||
keywords: { | ||
type: DataTypes.ARRAY(DataTypes.STRING), | ||
allowNull: false, | ||
}, | ||
biography: { | ||
type: DataTypes.JSONB, | ||
allowNull: false, | ||
}, | ||
}, | ||
{ | ||
sequelize, | ||
modelName: 'User', | ||
tableName: 'User', | ||
timestamps: true, | ||
updatedAt: false, | ||
} | ||
); | ||
|
||
return User; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters