SequelizeTable.js
3.67 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
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
}
// class methods
var classMethods = {
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) }
)
},
findAll: function(callback) {
sequelize.query(
Sequelize.sqlQueryFor('select', { table: tableName }),
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), order: 'id DESC', limit: 1
}), function(result) {
if (callback) callback(table.sqlResultToObject(result[0]))
}
)
},
sqlResultToObject: function(result) {
var object = new table(result)
object.id = result.id
return object
}
}
// instance methods
var instanceMethods = {
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
if(this.id == null)
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) {
SequelizeHelper.log(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
})
SequelizeHelper.Hash.forEach(instanceMethods, function(method, methodName) {
table.prototype[methodName] = method
})
return table
}