Sequelize.js
4.96 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
require(__dirname + "/SequelizeHelper")
require(__dirname + "/SequelizeTable")
Sequelize = function(database, username, password, options) {
this.config = {
database: database,
username: username,
password: password
}
this.tables = {}
this.options = options || {}
}
var classMethods = {
STRING: 'VARCHAR(255)',
TEXT: 'TEXT',
INTEGER: 'INT',
DATE: 'DATETIME',
sqlQueryFor: function(command, values) {
var query = null
switch(command) {
case 'create':
query = "CREATE TABLE IF NOT EXISTS %{table} (%{fields})"
break
case 'drop':
query = "DROP TABLE IF EXISTS %{table}"
break
case 'select':
values.fields = values.fields || "*"
query = "SELECT %{fields} FROM %{table}"
if(values.where) query += " WHERE %{where}"
if(values.order) query += " ORDER BY %{order}"
if(values.group) query += " GROUP BY %{group}"
if(values.limit) {
if(values.offset) query += " LIMIT %{offset}, %{limit}"
else query += " LIMIT %{limit}"
}
break
case 'insert':
query = "INSERT INTO %{table} (%{fields}) VALUES (%{values})"
break
case 'update':
query = "UPDATE %{table} SET %{values} WHERE id = %{id}"
break
case 'delete':
query = "DELETE FROM %{table} WHERE %{where}"
if(typeof values.limit == 'undefined') query += " LIMIT 1"
break
}
return SequelizeHelper.evaluateTemplate(query, values)
},
chainQueries: function(queries, callback) {
// queries = [{method: object}, {method: object, params: [1,2,3]}, {method: object}]
var executeQuery = function(index) {
var queryHash = queries[index]
var method = SequelizeHelper.Array.without(SequelizeHelper.Hash.keys(queryHash), "params")[0]
var object = queryHash[method]
var iterator = function() {
if(queries.length > (index + 1)) executeQuery(index + 1)
else if (callback) callback()
}
object[method].apply(object, SequelizeHelper.Array.join(queryHash.params || [], [iterator]))
}
if(queries.length > 0) executeQuery(0)
else if (callback) callback()
}
}
Sequelize.prototype = {
get tableNames() {
var result = []
SequelizeHelper.Hash.keys(this.tables).forEach(function(tableName) {
result.push(SequelizeHelper.SQL.asTableName(tableName))
})
return result
},
sync: function(callback) {
var finished = []
var tables = this.tables
SequelizeHelper.Hash.forEach(tables, function(table) {
table.klass.prepareAssociations()
})
if((SequelizeHelper.Hash.keys(this.tables).length == 0) && callback)
callback()
else
SequelizeHelper.Hash.forEach(tables, function(table) {
table.klass.sync(function() {
finished.push(true)
if((finished.length == SequelizeHelper.Hash.keys(tables).length) && callback)
callback()
})
})
},
drop: function(callback) {
var finished = []
var tables = this.tables
if((SequelizeHelper.Hash.keys(tables).length == 0) && callback) callback()
else
SequelizeHelper.Hash.forEach(tables, function(table, tableName) {
table.klass.drop(function() {
finished.push(true)
if(finished.length == SequelizeHelper.Hash.keys(tables).length)
if(callback) callback()
})
})
},
define: function(name, attributes) {
attributes.createdAt = 'DATETIME NOT NULL'
attributes.updatedAt = 'DATETIME NOT NULL'
var table = new SequelizeTable(this, SequelizeHelper.SQL.asTableName(name), attributes)
table.attributes = attributes
this.tables[name] = {klass: table, attributes: attributes}
table.sequelize = this
return table
},
query: function(queryString, callback) {
var fields = []
var values = []
var self = this
var connection = require(__dirname + "/../lib/nodejs-mysql-native/client").createTCPClient()
connection.auto_prepare = true
connection
.auth(this.config.database, this.config.username, this.config.password)
.addListener('authorized', function() {
if(!self.options.disableLogging)
SequelizeHelper.log("Executing the query: " + queryString)
connection
.query(queryString)
.on('row', function(r){ values.push(r) })
.on('field', function(f){ fields.push(f)})
.on('end', function(stats) {
if(callback) {
var result = []
values.forEach(function(valueArray) {
var mapping = {}
for(var i = 0; i < fields.length; i++)
mapping[fields[i].name] = valueArray[i]
result.push(mapping)
})
if(callback) callback(result, stats)
}
})
connection.close()
})
}
}
SequelizeHelper.Hash.forEach(classMethods, function(method, methodName) {
Sequelize[methodName] = method
})