SequelizeTable.js
5.05 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
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.tableName = tableName
this.attributes = attributes
table.associations.forEach(function(association) {
self[association.name] = function(callback) {
var myTableName = tableName.toLowerCase().replace(/s$/, "")
if(association.type = 'hasMany') {
var whereConditions = [myTableName+"Id", self.id].join("=")
return association.table.findAll({where: whereConditions})
}
}
})
}
// class methods
var classMethods = {
associations: [],
sync: function(callback) {
var fields = ["id INT NOT NULL auto_increment PRIMARY KEY"]
SequelizeHelper.Hash.keys(attributes).forEach(function(name) { fields.push(name + " " + attributes[name]) })
sequelize.query(
Sequelize.sqlQueryFor( 'create', { table: tableName, fields: fields.join(', ') } ),
function() { if(callback) callback(table) }
)
},
drop: function(callback) {
var query = "DROP TABLE IF EXISTS " + tableName
sequelize.query(
Sequelize.sqlQueryFor('drop', { 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: tableName }) : { table: tableName }
sequelize.query(
Sequelize.sqlQueryFor('select', queryOptions),
function(result) {
var objects = []
result.forEach(function(resultSet) {
objects.push(table.sqlResultToObject(resultSet))
})
if(_callback) _callback(objects)
}
)
},
find: function(conditions, callback) {
sequelize.query(
Sequelize.sqlQueryFor('select', {
table: tableName, where: SequelizeHelper.SQL.hashToWhereConditions(conditions, this.attributes), order: 'id DESC', limit: 1
}), function(result) {
if (callback) callback(table.sqlResultToObject(result[0]))
}
)
},
// TODO: mysql library currently doesn't support MYSQL_DATE!!! don't merge if fixed
sqlResultToObject: function(result) {
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) {
this.associations.push({
name: assocName,
table: table,
type: 'hasMany'
})
return table
},
hasOne: function(assocName, table) {
this.associations.push({
name: assocName,
table: table,
type: 'hasOne'
})
return table
},
belongsTo: function(assocName, table) {
this.associations.push({
name: assocName,
table: table,
type: 'belongsTo'
})
return table
}
}
// instance methods
table.prototype = {
get values() {
var result = {}
var self = this
SequelizeHelper.Hash.keys(attributes).forEach(function(attribute) {
result[attribute] = self[attribute]
})
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: this.tableName, fields: SequelizeHelper.SQL.fieldsForInsertQuery(this), values: SequelizeHelper.SQL.valuesForInsertQuery(this)
})
} else
query = Sequelize.sqlQueryFor('update', { table: this.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(this.attributes).forEach(function(attribute) {
if(newValues[attribute])
self[attribute] = newValues[attribute]
})
this.save(callback)
},
destroy: function(callback) {
sequelize.query(
Sequelize.sqlQueryFor('delete', { table: this.tableName, id: this.id }),
callback
)
}
}
SequelizeHelper.Hash.forEach(classMethods, function(method, methodName) {
table[methodName] = method
})
return table
}