SequelizeTable.js
12.3 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
A.hasOne(B) => B.aId
A.belongsTo(B) => A.bId
A.hasMany(B) => B.aId
A.hasMany(B) + B.hasMany(A) => AB.aId + AB.bId
*/
SequelizeTable = function(sequelize, tableName, attributes) {
var table = function(values) {
var self = this
SequelizeHelper.Hash.forEach(values, function(value, key) {
if(attributes[key])
self[key] = value
})
this.id = null // specify id as null to declare this object as unsaved and as not present in the database
this.table = table
}
// class methods
var classMethods = {
associations: [],
attributes: attributes,
tableName: tableName,
isCrossAssociatedWith: function(associatedTable) {
var result = false
var myTableName = table.tableName
associatedTable.associations.forEach(function(association) {
if((association.table.tableName == myTableName) && (association.type == 'hasMany'))
result = true
})
return result
},
prepareAssociations: function(callback) {
table.associations.forEach(function(association) {
switch(association.type) {
case 'hasMany':
if(association.table.isCrossAssociatedWith(table)) {
// many to many relation
var _attributes = {}
_attributes[table.identifier] = Sequelize.INTEGER
_attributes[association.table.identifier] = Sequelize.INTEGER
sequelize.define(SequelizeHelper.SQL.manyToManyTableName(table, association.table), _attributes)
} else {
// one to many relation
association.table.attributes[table.identifier] = Sequelize.INTEGER
}
break
case 'hasOne':
// e.g. assocTable.myTableId = Sequelize.INTEGER
association.table.attributes[table.identifier] = Sequelize.INTEGER
break
case 'belongsTo':
// e.g. table.dayId = Sequelize.INTEGER
table.attributes[association.table.identifier] = Sequelize.INTEGER
break
}
})
if(callback) callback()
},
sync: function(callback) {
var fields = ["id INT NOT NULL auto_increment PRIMARY KEY"]
SequelizeHelper.Hash.forEach(table.attributes, function(type, name) {
fields.push(name + " " + type)
})
sequelize.query(
Sequelize.sqlQueryFor( 'create', { table: table.tableName, fields: fields.join(', ') } ),
function() { if(callback) callback(table) }
)
},
drop: function(callback) {
sequelize.query(
Sequelize.sqlQueryFor('drop', { table: table.tableName }),
function() { if(callback) callback(table) }
)
},
// TODO: mysql library currently doesn't support MYSQL_DATE!!! look for fix
findAll: function(options, callback) {
// use the first param as callback if it is no object (hash)
var _callback = (typeof options == 'object') ? callback : options
var queryOptions = (typeof options == 'object')
? SequelizeHelper.Hash.merge(options, { table: table.tableName })
: { table: table.tableName }
sequelize.query(
Sequelize.sqlQueryFor('select', queryOptions),
function(result) {
var objects = SequelizeHelper.Array.map(result, function(r) { return table.sqlResultToObject(r) })
if(_callback) _callback(objects)
}
)
},
find: function(conditions, callback) {
sequelize.query(
Sequelize.sqlQueryFor('select', {
table: table.tableName,
where: SequelizeHelper.SQL.hashToWhereConditions(conditions, table.attributes),
order: 'id DESC',
limit: 1
}), function(result) {
var _result = result[0] ? table.sqlResultToObject(result[0]) : null
if (callback) callback(_result)
}
)
},
// TODO: mysql library currently doesn't support MYSQL_DATE!!! don't merge if fixed
sqlResultToObject: function(result) {
if(typeof result == undefined) return null
var object = new table(SequelizeHelper.Hash.merge({createdAt: new Date(), updatedAt: new Date()}, result, true))
object.id = result.id
return object
},
hasMany: function(assocName, _table) {
table.associations.push({
name: assocName,
table: _table,
type: 'hasMany'
})
// don't check inside of method to increase performance
if(_table.isCrossAssociatedWith(table)) {
table.prototype[assocName] = function(callback) {
var Association = sequelize.tables[SequelizeHelper.SQL.manyToManyTableName(_table, table)].klass
var whereConditions = [table.identifier, this.id].join("=")
Association.findAll({ where: whereConditions }, function(result) {
if(result.length > 0) {
var ids = []
result.forEach(function(resultSet) { ids.push(resultSet.id) })
_table.findAll({where: "id IN (" + ids.join(",") + ")"}, callback)
} else {
if(callback) callback([])
}
})
}
table.prototype[SequelizeHelper.SQL.addPrefix('set', assocName)] = function(objects, callback) {
var self = this
var Association = sequelize.tables[SequelizeHelper.SQL.manyToManyTableName(_table, table)].klass
var currentAssociations = null
var objectIds = SequelizeHelper.Array.map(objects, function(obj) { return obj.id })
var getAssociatedObjects = function(callback) {
self[assocName](function(associations) {
currentAssociations = associations
callback(associations)
})
}
var deleteObsoleteAssociations = function(callback) {
var obsolete = []
currentAssociations.forEach(function(association) {
if(objectIds.indexOf(association.id) == -1) obsolete.push(association.id)
})
if(obsolete.length == 0)
callback([])
else
sequelize.query(
Sequelize.sqlQueryFor('delete', {table: Association.tableName, where: "id IN (" + obsolete.join(",") + ")", limit: null}),
function(){ callback(obsolete) }
)
}
var createNewAssociations = function(obsolete) {
var currentIds = SequelizeHelper.Array.map(currentAssociations, function(assoc) { return assoc.id })
var withoutExisting = SequelizeHelper.Array.reject(objects, function(o) {
currentIds.indexOf(o.id) > -1
})
var savings = []
withoutExisting.forEach(function(o) {
if((o instanceof _table) && (self.id != null) && (o.id != null)) {
var attributes = {}
attributes[self.table.identifier] = self.id
attributes[o.table.identifier] = o.id
savings.push({save: new Association(attributes)})
}
})
Sequelize.chainQueries(savings, function() {
getAssociatedObjects(callback)
})
}
getAssociatedObjects(function() {
deleteObsoleteAssociations(function(obsolete) {
createNewAssociations(obsolete)
})
})
}
} else {
table.prototype[assocName] = function(callback) {
var whereConditions = [table.identifier, this.id].join("=")
_table.findAll({where: whereConditions}, callback)
}
table.prototype[SequelizeHelper.SQL.addPrefix('set', assocName)] = function(objects, callback) {
var self = this
var objectIds = SequelizeHelper.Array.map(objects, function(obj) { return obj.id })
this[assocName](function(currentAssociations) {
var currentIds = SequelizeHelper.Array.map(currentAssociations, function(assoc) { return assoc.id })
var obsoleteAssociations = SequelizeHelper.Array.select(currentAssociations, function(assoc) { return objectsIds.indexOf(assoc.id) == -1 })
var queries = []
obsoleteAssociations.forEach(function(assoc) {
var attr = {}; attr[table.identifier] = null
queries.push({updateAttributes: assoc, params: [attr]})
})
var newAssociations = SequelizeHelper.Array.select(objects, function(o) { return currentIds.indexOf(o.id) == -1 })
newAssociations.forEach(function(assoc) {
var attr = {}; attr[table.identifier] = self.id
queries.push({updateAttributes: assoc, params: [attr]})
})
Sequelize.chainQueries(queries, function() {
self[assocName](callback)
})
})
}
}
return table
},
hasOne: function(assocName, _table) {
table.associations.push({
name: assocName,
table: _table,
type: 'hasOne'
})
table.prototype[assocName] = function(callback) {
var whereConditions = {}
whereConditions[table.identifier] = this.id
_table.find(whereConditions, callback)
}
table.prototype[SequelizeHelper.SQL.addPrefix('set', assocName)] = function(object, callback) {
var self = this
this[assocName](function(currentAssociation) {
var attr = {}
if(currentAssociation == null) {
attr[table.identifier] = self.id
object.updateAttributes(attr, callback)
} else {
if(object.id == currentAssociation.id) callback()
else {
attr[table.identifier] = null
currentAssociation.updateAttributes(attr, function() {
attr[table.identifier] = self.id
object.updateAttributes(attr, callback)
})
}
}
})
}
return table
},
belongsTo: function(assocName, _table) {
table.associations.push({
name: assocName,
table: _table,
type: 'belongsTo'
})
table.prototype[assocName] = function(callback) {
_table.find(this[_table.identifier], callback)
}
table.prototype[SequelizeHelper.SQL.addPrefix('set', assocName)] = function(object, callback) {
var attr = {}; attr[object.table.identifier] = object.id
var self = this
this.updateAttributes(attr, function() {
self[assocName](callback)
})
}
return table
}
}
// don't put this into the hash!
classMethods.identifier = SequelizeHelper.SQL.asTableIdentifier(classMethods.tableName)
// instance methods
table.prototype = {
get values() {
var result = {}
var self = this
SequelizeHelper.Hash.keys(table.attributes).forEach(function(attribute) {
result[attribute] = self[attribute] || null
})
return result
},
save: function(callback) {
var query = null
var self = this
this.updatedAt = new Date()
if(this.id == null) {
this.createdAt = new Date()
query = Sequelize.sqlQueryFor('insert', {
table: table.tableName,
fields: SequelizeHelper.SQL.fieldsForInsertQuery(this),
values: SequelizeHelper.SQL.valuesForInsertQuery(this)
})
} else
query = Sequelize.sqlQueryFor('update', { table: table.tableName, values: SequelizeHelper.SQL.valuesForUpdate(this), id: this.id })
sequelize.query(query, function() {
if(self.id == null) {
table.find(self.values, function(result) {
self.id = result.id
if(callback) callback(self)
})
} else {
if(callback) callback(self)
}
})
},
updateAttributes: function(newValues, callback) {
var self = this
SequelizeHelper.Hash.keys(table.attributes).forEach(function(attribute) {
if(newValues[attribute])
self[attribute] = newValues[attribute]
})
this.save(callback)
},
destroy: function(callback) {
sequelize.query(
Sequelize.sqlQueryFor('delete', { table: table.tableName, where: ['id', this.id].join("=") }),
callback
)
}
}
SequelizeHelper.Hash.forEach(classMethods, function(method, methodName) {
table[methodName] = method
})
return table
}