-
-
Notifications
You must be signed in to change notification settings - Fork 527
Expand file tree
/
Copy pathmodel.js
More file actions
48 lines (42 loc) · 1.51 KB
/
model.js
File metadata and controls
48 lines (42 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
import { Model, DataTypes } from 'sequelize';
<% if (isTypescriptProject) { %>
import sequelize from './connection';
<% }else{ %>
const sequelize = require('./connection');
<% } %>
<% if (isTypescriptProject) { %>
export interface <%= name %>Attributes {
<% attributes.forEach(function(attribute, index) { %>
<%= attribute.fieldName %>: <%= attribute.tsType %>;
<% }) %>
}
<% } %>
class <%= name %> extends Model<%= isTypescriptProject ? `<${name}Attributes> implements ${name}Attributes` : '' %> {
<% if (isTypescriptProject) { %>
<% attributes.forEach(function(attribute, index) { %>
<%= attribute.fieldName %><%=isTypescriptProject ? `!: ${attribute.tsType}` : '' %>;
<% }) %>
<% } %>
}
<%= name %>.init({
<% attributes.forEach(function(attribute, index) { %>
<%= attribute.fieldName %>: DataTypes.<%= attribute.dataFunction ? `${attribute.dataFunction.toUpperCase()}(DataTypes.${attribute.dataType.toUpperCase()})` : attribute.dataValues ? `${attribute.dataType.toUpperCase()}(${attribute.dataValues})` : attribute.dataType.toUpperCase() %>
<%= (Object.keys(attributes).length - 1) > index ? ',' : '' %>
<% }) %>
}, {
sequelize,
modelName: '<%= name %>',
<%= underscored ? 'underscored: true,' : '' %>
});
// Associations
// <%= name %>.belongsTo(TargetModel, {
// as: 'custom_name',
// foreignKey: {
// name: 'foreign_key_column_name',
// allowNull: false,
// },
// onDelete: "RESTRICT",
// foreignKeyConstraint: true,
// });
export default <%= name %>;