model.js
5.29 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var Utils = require("./utils")
, Mixin = require("./associations/mixin")
module.exports = (function() {
var Model = function(values, options) {
var self = this
this.__definition = null // will be set in Model.build
this.attributes = []
this.__options = options || {}
initAttributes.call(this, values)
}
Utils.addEventEmitter(Model)
Utils._.extend(Model.prototype, Mixin.prototype)
Model.Events = {
insert: 'InsertQuery',
update: 'UpdateQuery',
destroy: 'DestroyQuery'
}
Model.prototype.__defineGetter__('QueryGenerator', function() {
return this.__definition.QueryGenerator
})
Model.prototype.save = function() {
var attr = this.__options.underscored ? 'updated_at' : 'updatedAt'
if(this.hasOwnProperty(attr))
this[attr] = new Date()
if(this.isNewRecord) {
var self = this
var eventEmitter = new Utils.CustomEventEmitter(function() {
query.call(self, self.QueryGenerator.insertQuery(self.__definition.tableName, self.values))
.on('success', function(obj) {
obj.isNewRecord = false
eventEmitter.emit('success', obj)
})
.on('failure', function(err) { eventEmitter.emit('failure', err) })
})
return eventEmitter.run()
} else {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
return query.call(this, this.QueryGenerator.updateQuery(this.__definition.tableName, this.values, identifier))
}
}
Model.prototype.updateAttributes = function(updates) {
var self = this
var readOnlyAttributes = Utils._.keys(this.__definition.primaryKeys)
readOnlyAttributes.push('id')
readOnlyAttributes.push('createdAt')
readOnlyAttributes.push('updatedAt')
readOnlyAttributes.push('deletedAt')
Utils._.each(updates, function(value, attr) {
var updateAllowed = (
(readOnlyAttributes.indexOf(attr) == -1) &&
(readOnlyAttributes.indexOf(Utils._.underscored(attr)) == -1) &&
(self.attributes.indexOf(attr) > -1)
)
updateAllowed && (self[attr] = value)
})
return this.save()
}
Model.prototype.destroy = function() {
if(this.__options.timestamps && this.__options.paranoid) {
this[this.__options.underscored ? 'deleted_at' : 'deletedAt'] = new Date()
return this.save()
} else {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
return query.call(this, this.QueryGenerator.deleteQuery(this.__definition.tableName, identifier))
}
}
Model.prototype.__defineGetter__("identifiers", function() {
var primaryKeys = Utils._.keys(this.__definition.primaryKeys)
, result = {}
, self = this
if(!this.__definition.hasPrimaryKeys)
primaryKeys = ['id']
primaryKeys.forEach(function(identifier) {
result[identifier] = self[identifier]
})
return result
})
Model.prototype.__defineGetter__('isDeleted', function() {
var result = this.__options.timestamps && this.__options.paranoid
result = result && this[this.__options.underscored ? 'deleted_at' : 'deletedAt'] != null
return result
})
Model.prototype.__defineGetter__('values', function() {
var result = {}
, self = this
this.attributes.forEach(function(attr) {
result[attr] = self[attr]
})
return result
})
Model.prototype.__defineGetter__('primaryKeyValues', function() {
var result = {}
, self = this
Utils._.each(this.__definition.primaryKeys, function(_, attr) {
result[attr] = self[attr]
})
return result
})
Model.prototype.equals = function(other) {
var result = true
, self = this
Utils._.each(this.values, function(value, key) {
result = result && (value == other[key])
})
return result
}
Model.prototype.equalsOneOf = function(others) {
var result = false
, self = this
others.forEach(function(other) { result = result || self.equals(other) })
return result
}
Model.prototype.addAttribute = function(attribute, value) {
this[attribute] = value
this.attributes.push(attribute)
}
// private
var initAttributes = function(values) {
var self = this
// add all passed values to the model and store the attribute names in this.attributes
Utils._.map(values, function(value, key) { self.addAttribute(key, value) })
// set id to null if not passed as value
// a newly created model has no id
var defaults = this.__options.hasPrimaryKeys ? {} : { id: null }
if(this.__options.timestamps) {
defaults[this.__options.underscored ? 'created_at' : 'createdAt'] = new Date()
defaults[this.__options.underscored ? 'updated_at' : 'updatedAt'] = new Date()
if(this.__options.paranoid)
defaults[this.__options.underscored ? 'deleted_at' : 'deletedAt'] = null
}
Utils._.map(defaults, function(value, attr) {
if(!self.hasOwnProperty(attr))
self.addAttribute(attr, value)
})
}
var query = function() {
var args = Utils._.map(arguments, function(arg, _) { return arg })
, s = this.__definition.modelManager.sequelize
args.push(this)
return s.query.apply(s, args)
}
/* Add the instance methods to Model */
Utils._.extend(Model.prototype, Mixin.prototype)
return Model
})()