sequelize.js
3.44 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
var Utils = require("./utils")
, ModelFactory = require("./model-factory")
, DataTypes = require('./data-types')
, ModelManager = require("./model-manager")
, Migrator = require("./migrator")
, QueryInterface = require("./query-interface")
module.exports = (function() {
var Sequelize = function(database, username, password, options) {
this.options = Utils._.extend({
dialect: 'mysql',
host: 'localhost',
port: 3306,
define: {},
query: {},
sync: {}
}, options || {})
this.config = {
database: database,
username: username,
password: (( (["", null, false].indexOf(password) > -1) || (typeof password == 'undefined')) ? null : password),
host : this.options.host,
port : this.options.port
}
var ConnectorManager = require("./dialects/" + this.options.dialect + "/connector-manager")
this.modelManager = new ModelManager(this)
this.connectorManager = new ConnectorManager(this, this.config)
}
Sequelize.Utils = Utils
Sequelize.Utils._.map(DataTypes, function(sql, accessor) { Sequelize[accessor] = sql})
Sequelize.prototype.getQueryInterface = function() {
this.queryInterface = this.queryInterface || new QueryInterface(this)
return this.queryInterface
}
Sequelize.prototype.getMigrator = function(options, force) {
if(force)
this.migrator = new Migrator(this, options)
else
this.migrator = this.migrator || new Migrator(this, options)
return this.migrator
}
Sequelize.prototype.define = function(modelName, attributes, options) {
options = options || {}
if(this.options.define)
options = Sequelize.Utils.merge(options, this.options.define)
var factory = new ModelFactory(modelName, attributes, options)
this.modelManager.addModel(factory.init(this.modelManager))
return factory
}
Sequelize.prototype.import = function(path) {
var defineCall = require(path)
return defineCall(this, DataTypes)
}
Sequelize.prototype.migrate = function(options) {
this.getMigrator().migrate(options)
}
Sequelize.prototype.query = function(sql, callee, options) {
options = Utils._.extend(Utils._.clone(this.options.query), options || {})
options = Utils._.extend(options, {
logging: this.options.hasOwnProperty('logging') ? this.options.logging : true
})
return this.connectorManager.query(sql, callee, options)
}
Sequelize.prototype.sync = function(options) {
options = options || {}
if(this.options.sync)
options = Sequelize.Utils.merge(options, this.options.sync)
var self = this
var eventEmitter = new Utils.CustomEventEmitter(function() {
var chainer = new Utils.QueryChainer
self.modelManager.models.forEach(function(model) { chainer.add(model.sync(options)) })
chainer
.run()
.success(function() { eventEmitter.emit('success', null) })
.error(function(err) { eventEmitter.emit('failure', err) })
})
return eventEmitter.run()
}
Sequelize.prototype.drop = function() {
var self = this
return new Utils.CustomEventEmitter(function(emitter) {
var chainer = new Utils.QueryChainer
self.modelManager.models.forEach(function(model) { chainer.add(model.drop()) })
chainer
.run()
.success(function() { emitter.emit('success', null) })
.error(function(err) { emitter.emit('failure', err) })
}).run()
}
return Sequelize
})()