Sequelize.js
3.17 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
require(__dirname + "/SequelizeHelper")
require(__dirname + "/SequelizeTable")
Sequelize = function(database, username, password) {
this.config = {
database: database,
username: username,
password: password
}
this.tables = {}
}
var classMethods = {
STRING: 'VARCHAR(255)',
TEXT: 'VARCHAR(4000)',
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 id = %{id} LIMIT 1"
break
}
return SequelizeHelper.evaluateTemplate(query, values)
}
}
Sequelize.prototype = {
get tableNames() {
var result = []
SequelizeHelper.Hash.keys(this.tables).forEach(function(tableName) {
result.push(SequelizeHelper.SQL.asTableName(tableName))
})
return result
},
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] = {constructor: 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() {
SequelizeHelper.log("Executing the query: " + queryString)
connection
.execute(queryString)
.addListener('row', function(r){ values.push(r) })
.addListener('field', function(f){ fields.push(f)})
.addListener('end', function() {
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)
})
callback(result)
}
})
connection.close()
})
}
}
SequelizeHelper.Hash.forEach(classMethods, function(method, methodName) {
Sequelize[methodName] = method
})