forked from 1602/jugglingdb
/
schema.js
316 lines (277 loc) · 8.12 KB
/
schema.js
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* Module dependencies
*/
var AbstractClass = require('./abstract-class').AbstractClass;
var util = require('util');
var path = require('path');
var fs = require('fs');
var existsSync = fs.existsSync || path.existsSync;
/**
* Export public API
*/
exports.Schema = Schema;
// exports.AbstractClass = AbstractClass;
/**
* Helpers
*/
var slice = Array.prototype.slice;
/**
* Schema - adapter-specific classes factory.
*
* All classes in single schema shares same adapter type and
* one database connection
*
* @param name - type of schema adapter (mysql, mongoose, sequelize, redis)
* @param settings - any database-specific settings which we need to
* establish connection (of course it depends on specific adapter)
*
* - host
* - port
* - username
* - password
* - database
* - debug {Boolean} = false
*
* @example Schema creation, waiting for connection callback
* ```
* var schema = new Schema('mysql', { database: 'myapp_test' });
* schema.define(...);
* schema.on('connected', function () {
* // work with database
* });
* ```
*/
function Schema(name, settings) {
var schema = this;
// just save everything we get
this.name = name;
this.settings = settings;
// Disconnected by default
this.connected = false;
// create blank models pool
this.models = {};
this.definitions = {};
// and initialize schema using adapter
// this is only one initialization entry point of adapter
// this module should define `adapter` member of `this` (schema)
var adapter;
if (existsSync(__dirname + '/adapters/' + name + '.js')) {
adapter = require('./adapters/' + name);
} else {
try {
adapter = require(name);
} catch (e) {
throw new Error('Adapter ' + name + ' is not defined, try\n npm install ' + name);
}
}
adapter.initialize(this, function () {
// we have an adaper now?
if (!this.adapter) {
throw new Error('Adapter is not defined correctly: it should create `adapter` member of schema');
}
this.adapter.log = function (query, start) {
schema.log(query, start);
};
this.adapter.logger = function (query) {
var t1 = Date.now();
var log = this.log;
return function (q) {
log(q || query, t1);
};
};
this.connected = true;
this.emit('connected');
}.bind(this));
};
util.inherits(Schema, require('events').EventEmitter);
function Text() {}
Schema.Text = Text;
/**
* Define class
*
* @param {String} className
* @param {Object} properties - hash of class properties in format
* `{property: Type, property2: Type2, ...}`
* or
* `{property: {type: Type}, property2: {type: Type2}, ...}`
* @param {Object} settings - other configuration of class
* @return newly created class
*
* @example simple case
* ```
* var User = schema.define('User', {
* email: String,
* password: String,
* birthDate: Date,
* activated: Boolean
* });
* ```
* @example more advanced case
* ```
* var User = schema.defind('User', {
* email: { type: String, limit: 150, index: true },
* password: { type: String, limit: 50 },
* birthDate: Date,
* registrationDate: {type: Date, default: function () { return new Date }},
* activated: { type: Boolean, default: false }
* });
* ```
*/
Schema.prototype.define = function defineClass(className, properties, settings) {
var schema = this;
var args = slice.call(arguments);
if (!className) throw new Error('Class name required');
if (args.length == 1) properties = {}, args.push(properties);
if (args.length == 2) settings = {}, args.push(settings);
standartize(properties, settings);
// every class can receive hash of data as optional param
var newClass = function ModelConstructor(data) {
if (!(this instanceof ModelConstructor)) {
return new ModelConstructor(data);
}
AbstractClass.call(this, data);
};
hiddenProperty(newClass, 'schema', schema);
hiddenProperty(newClass, 'modelName', className);
hiddenProperty(newClass, 'cache', {});
hiddenProperty(newClass, 'mru', []);
// setup inheritance
newClass.__proto__ = AbstractClass;
util.inherits(newClass, AbstractClass);
// store class in model pool
this.models[className] = newClass;
this.definitions[className] = {
properties: properties,
settings: settings
};
// pass controll to adapter
this.adapter.define({
model: newClass,
properties: properties,
settings: settings
});
return newClass;
function standartize(properties, settings) {
Object.keys(properties).forEach(function (key) {
var v = properties[key];
if (typeof v === 'function') {
properties[key] = { type: v };
}
});
// TODO: add timestamps fields
// when present in settings: {timestamps: true}
// or {timestamps: {created: 'created_at', updated: false}}
// by default property names: createdAt, updatedAt
}
};
/**
* Define single property named `prop` on `model`
*
* @param {String} model - name of model
* @param {String} prop - name of propery
* @param {Object} params - property settings
*/
Schema.prototype.defineProperty = function (model, prop, params) {
this.definitions[model].properties[prop] = params;
if (this.adapter.defineProperty) {
this.adapter.defineProperty(model, prop, params);
}
};
/**
* Drop each model table and re-create.
* This method make sense only for sql adapters.
*
* @warning All data will be lost! Use autoupdate if you need your data.
*/
Schema.prototype.automigrate = function (cb) {
this.freeze();
if (this.adapter.automigrate) {
this.adapter.automigrate(cb);
} else if (cb) {
cb();
}
};
/**
* Update existing database tables.
* This method make sense only for sql adapters.
*/
Schema.prototype.autoupdate = function (cb) {
this.freeze();
if (this.adapter.autoupdate) {
this.adapter.autoupdate(cb);
} else if (cb) {
cb();
}
};
/**
* Check whether migrations needed
* This method make sense only for sql adapters.
*/
Schema.prototype.isActual = function (cb) {
this.freeze();
if (this.adapter.isActual) {
this.adapter.isActual(cb);
} else if (cb) {
cb(null, true);
}
};
/**
* Log benchmarked message. Do not redefine this method, if you need to grab
* chema logs, use `schema.on('log', ...)` emitter event
*
* @private used by adapters
*/
Schema.prototype.log = function (sql, t) {
this.emit('log', sql, t);
};
/**
* Freeze schema. Behavior depends on adapter
*/
Schema.prototype.freeze = function freeze() {
if (this.adapter.freezeSchema) {
this.adapter.freezeSchema();
}
}
/**
* Return table name for specified `modelName`
* @param {String} modelName
*/
Schema.prototype.tableName = function (modelName) {
return this.definitions[modelName].settings.table = this.definitions[modelName].settings.table || modelName
};
/**
* Define foreign key
* @param {String} className
* @param {String} key - name of key field
*/
Schema.prototype.defineForeignKey = function defineForeignKey(className, key) {
// return if already defined
if (this.definitions[className].properties[key]) return;
if (this.adapter.defineForeignKey) {
this.adapter.defineForeignKey(className, key, function (err, keyType) {
if (err) throw err;
this.definitions[className].properties[key] = {type: keyType};
}.bind(this));
} else {
this.definitions[className].properties[key] = {type: Number};
}
};
/**
* Close database connection
*/
Schema.prototype.disconnect = function disconnect() {
if (typeof this.adapter.disconnect === 'function') {
this.adapter.disconnect();
}
};
/**
* Define hidden property
*/
function hiddenProperty(where, property, value) {
Object.defineProperty(where, property, {
writable: false,
enumerable: false,
configurable: false,
value: value
});
}