-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.js
122 lines (112 loc) · 3.48 KB
/
db.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const Sequelize = require('sequelize');
const uuid = require('node-uuid');
let config = require('./config');
config=config.sequelize;
function generateId() {
return uuid.v4();
}
var sequelize = new Sequelize(config.database, config.username, config.password, {
host: config.host,
dialect: config.dialect,
pool: {
max: 5,
min: 0,
idle: 10000
},
timezone: '+08:00' //东八时区
});
const ID_TYPE = Sequelize.STRING(50);
function defineModel(name, attributes) {
// var attrs = {};
// for (let key in attributes) {
// let value = attributes[key];
// if (typeof value === 'object' && value['type']) {
// value.allowNull = value.allowNull || false;
// attrs[key] = value;
// } else {
// attrs[key] = {
// type: value,
// allowNull: false
// };
// }
// }
// attrs.id = {
// type: ID_TYPE,
// primaryKey: true
// };
// attrs.createdAt = {
// type: Sequelize.BIGINT,
// allowNull: false
// };
// attrs.updatedAt = {
// type: Sequelize.BIGINT,
// allowNull: false
// };
// attrs.version = {
// type: Sequelize.BIGINT,
// allowNull: false
// };
// console.log('model defined for table: ' + name + '\n' + JSON.stringify(attrs, function (k, v) {
// if (k === 'type') {
// for (let key in Sequelize) {
// if (key === 'ABSTRACT' || key === 'NUMBER') {
// continue;
// }
// let dbType = Sequelize[key];
// if (typeof dbType === 'function') {
// if (v instanceof dbType) {
// if (v._length) {
// return `${dbType.key}(${v._length})`;
// }
// return dbType.key;
// }
// if (v === dbType) {
// return dbType.key;
// }
// }
// }
// }
// return v;
// }, ' '));
return sequelize.define(name, attributes, {
tableName: name,
paranoid: true
// timestamps: false,
// hooks: {
// beforeValidate: function (obj) {
// let now = Date.now();
// if (obj.isNewRecord) {
// console.log('will create entity...' + obj);
// if (!obj.id) {
// obj.id = generateId();
// }
// obj.createdAt = now;
// obj.updatedAt = now;
// obj.version = 0;
// } else {
// console.log('will update entity...');
// obj.updatedAt = now;
// obj.version++;
// }
// }
// }
});
}
const TYPES = ['STRING', 'INTEGER', 'BIGINT', 'TEXT', 'DOUBLE', 'DATEONLY', 'BOOLEAN'];
var exp = {
defineModel: defineModel,
sync: () => {
// only allow create ddl in non-production environment:
if (process.env.NODE_ENV !== 'production') {
sequelize.sync({ force: true });
} else {
throw new Error('Cannot sync() when NODE_ENV is set to \'production\'.');
}
}
};
for (let type of TYPES) {
exp[type] = Sequelize[type];
}
exp.ID = ID_TYPE;
exp.generateId = generateId;
module.exports = exp;