sequelize.js
3.52 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
/*
var sequelize = new require('sequelize').sequelize(database, username[, password])
var Tag = sequelize.define('Tag', {title: sequelize.TEXT, createdAt: sequelize.DATE})
var t = new Tag({title: 'Office-Stuff', createdAt: new Date()})
t.save(function() {
callback
})
*/
var log = function(obj){ var sys = require("sys"); sys.puts(sys.inspect(obj)) }
var keys = function(object) {
var results = []
for (var property in object)
results.push(property)
return results
}
var values = function(object) {
var result = []
keys(object).forEach(function(key) {
result.push(object[key])
})
return result
}
var evaluateTemplate = function(template, replacements) {
var result = template
keys(replacements).forEach(function(key) {
result = result.replace("%{" + key + "}", replacements[key])
})
return result
}
var mysql = require(__dirname + "/lib/node-mysql/mysql")
exports.Sequelize = function(database, username, password) {
this.STRING = 'VARCHAR(255)'
this.TEXT = 'VARCHAR(4000)'
this.INTEGER = 'INT'
this.config = {
database: database,
username: username,
password: password
}
this.connection = new mysql.Connection('localhost', this.config.username, this.config.password, this.config.database)
this.tables = {}
}
exports.Sequelize.prototype = {
asTableName: function(name) {
return name + "s"
},
define: function(name, attributes) {
var table = new TableWrapper(this, this.asTableName(name), attributes)
table.attributes = attributes
this.tables[name] = {constructor: table, attributes: attributes}
return table
},
get tableNames() {
return keys(this.tables)
},
query: function(queryString, callback) {
log("Executing the query: " + queryString)
this.connection.connect()
this.connection.query(queryString)
}
}
var TableWrapper = function(sequelize, tableName, attributes) {
var table = function(values) {
var self = this
keys(values).forEach(function(key) {
if(attributes[key])
self[key] = values[key]
})
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.sync = function() {
var fields = ["id INT"]
attributes.forEach(function(type, name) { fields.push(name + " " + type) })
var query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + fields.join(', ') + ")"
sequelize.query(query)
}
table.drop = function() {
var query = "DROP TABLE IF EXISTS " + tableName
sequelize.query(query)
}
table.prototype = {
get values() {
var result = {}
var self = this
keys(this.attributes).forEach(function(key) {
result[key] = self[key]
})
return result
},
get valuesAsString() {
var actualValues = this.values
var values = []
var self = this
keys(this.values).forEach(function(key) {
switch(self.attributes[key]) {
case sequelize.INT: actualValues[key]; break;
default "'" + actualValues[key] + "'"
}
var valueAsString =
values.push
})
}
save: function() {
var query = null
if(this.id == null) {
query = evaluateTemplate(
"INSERT INTO %{table} (%{fields}) VALUES (%{values})",
{ table: this.tableName, fields: keys(this.values).join(", "), values: this.valuesAsString }
)
} else {
}
sequelize.query(query)
}
}
return table
}