model.js
2.48 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
var Utils = require("./utils")
, Mixin = require("./association-mixin")
, QueryGenerator = require("./query-generator")
var Model = module.exports = function(values, options) {
var self = this
this.definition = null // will be set on Model.build or Model.create
this.attributes = []
this.options = options || {}
// 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 = { id: null }
if(this.options.timestamps) {
defaults[this.options.camelcase ? 'created_at' : 'createdAt'] = new Date()
defaults[this.options.camelcase ? 'updated_at' : 'updatedAt'] = new Date()
if(this.options.safeDelete)
defaults[this.options.camelcase ? 'deleted_at' : 'deletedAt'] = null
}
Utils._.map(defaults, function(value, attr) {
if(!self.hasOwnProperty(attr))
self.addAttribute(attr, value)
})
}
Utils.addEventEmitter(Model)
Utils._.map(Mixin.classMethods, function(fct, name) { Model[name] = fct })
Model.Events = {
insert: 'InsertQuery',
update: 'UpdateQuery',
destroy: 'DestroyQuery'
}
Model.prototype.addAttribute = function(attribute, value) {
this[attribute] = value
this.attributes.push(attribute)
}
Model.prototype.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)
}
Model.prototype.save = function() {
var attr = this.options.camelcase ? 'updated_at' : 'updatedAt'
if(this.hasOwnProperty(attr))
this[attr] = new Date()
if(this.isNewRecord)
return this.query(QueryGenerator.insertQuery(this.definition.tableName, this.values))
else
return this.query(QueryGenerator.updateQuery(this.definition.tableName, this.values, this.id))
}
Model.prototype.destroy = function() {
return this.query(QueryGenerator.deleteQuery(this.definition.tableName, this.id))
}
Model.prototype.__defineGetter__('isNewRecord', function() {
return this.id == null
})
Model.prototype.__defineGetter__('values', function() {
var result = {}
, self = this
this.attributes.forEach(function(attr) {
result[attr] = self[attr]
})
return result
})
/* Add the instance methods to Model */
Utils._.map(Mixin.instanceMethods, function(fct, name) { Model.prototype[name] = fct})