2011-10-08 17:11:26 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
var AbstractClass = require('./abstract-class').AbstractClass;
|
|
|
|
var util = require('util');
|
2011-10-23 19:43:53 +00:00
|
|
|
var path = require('path');
|
2012-07-02 11:59:44 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
var existsSync = fs.existsSync || path.existsSync;
|
2011-10-08 17:11:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export public API
|
|
|
|
*/
|
|
|
|
exports.Schema = Schema;
|
|
|
|
// exports.AbstractClass = AbstractClass;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helpers
|
|
|
|
*/
|
|
|
|
var slice = Array.prototype.slice;
|
|
|
|
|
|
|
|
/**
|
2012-03-27 14:22:24 +00:00
|
|
|
* Schema - adapter-specific classes factory.
|
|
|
|
*
|
|
|
|
* All classes in single schema shares same adapter type and
|
|
|
|
* one database connection
|
|
|
|
*
|
2011-10-08 17:11:26 +00:00
|
|
|
* @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)
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
|
|
|
* - 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
|
|
|
|
* });
|
|
|
|
* ```
|
2011-10-08 17:11:26 +00:00
|
|
|
*/
|
|
|
|
function Schema(name, settings) {
|
2011-11-11 13:16:09 +00:00
|
|
|
var schema = this;
|
2011-10-08 17:11:26 +00:00
|
|
|
// just save everything we get
|
|
|
|
this.name = name;
|
|
|
|
this.settings = settings;
|
|
|
|
|
2012-04-10 14:30:55 +00:00
|
|
|
// Disconnected by default
|
|
|
|
this.connected = false;
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
// 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;
|
2012-07-02 11:59:44 +00:00
|
|
|
if (existsSync(__dirname + '/adapters/' + name + '.js')) {
|
2011-10-08 17:11:26 +00:00
|
|
|
adapter = require('./adapters/' + name);
|
2011-10-23 19:43:53 +00:00
|
|
|
} else {
|
2011-10-08 17:11:26 +00:00
|
|
|
try {
|
|
|
|
adapter = require(name);
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error('Adapter ' + name + ' is not defined, try\n npm install ' + name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter.initialize(this, function () {
|
|
|
|
|
2012-03-11 04:48:38 +00:00
|
|
|
// we have an adaper now?
|
|
|
|
if (!this.adapter) {
|
|
|
|
throw new Error('Adapter is not defined correctly: it should create `adapter` member of schema');
|
|
|
|
}
|
2011-11-11 13:16:09 +00:00
|
|
|
|
2012-03-11 04:48:38 +00:00
|
|
|
this.adapter.log = function (query, start) {
|
|
|
|
schema.log(query, start);
|
|
|
|
};
|
2011-11-11 13:16:09 +00:00
|
|
|
|
2012-03-11 04:48:38 +00:00
|
|
|
this.adapter.logger = function (query) {
|
|
|
|
var t1 = Date.now();
|
|
|
|
var log = this.log;
|
|
|
|
return function (q) {
|
|
|
|
log(q || query, t1);
|
|
|
|
};
|
2011-11-11 13:16:09 +00:00
|
|
|
};
|
2012-03-11 04:48:38 +00:00
|
|
|
|
|
|
|
this.connected = true;
|
|
|
|
this.emit('connected');
|
|
|
|
|
|
|
|
}.bind(this));
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2011-11-21 21:40:50 +00:00
|
|
|
util.inherits(Schema, require('events').EventEmitter);
|
2011-10-08 17:11:26 +00:00
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
function Text() {}
|
2011-10-08 17:11:26 +00:00
|
|
|
Schema.Text = Text;
|
2012-08-13 06:16:01 +00:00
|
|
|
function JSON() {}
|
|
|
|
Schema.JSON = JSON;
|
2011-10-08 17:11:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Define class
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
* ```
|
2012-04-07 13:43:15 +00:00
|
|
|
* var User = schema.define('User', {
|
2012-03-27 14:22:24 +00:00
|
|
|
* 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 }
|
|
|
|
* });
|
|
|
|
* ```
|
2011-10-08 17:11:26 +00:00
|
|
|
*/
|
|
|
|
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
|
2012-01-09 12:59:58 +00:00
|
|
|
var newClass = function ModelConstructor(data) {
|
|
|
|
if (!(this instanceof ModelConstructor)) {
|
|
|
|
return new ModelConstructor(data);
|
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
AbstractClass.call(this, data);
|
|
|
|
};
|
|
|
|
|
|
|
|
hiddenProperty(newClass, 'schema', schema);
|
|
|
|
hiddenProperty(newClass, 'modelName', className);
|
|
|
|
hiddenProperty(newClass, 'cache', {});
|
2012-03-16 14:42:02 +00:00
|
|
|
hiddenProperty(newClass, 'mru', []);
|
2011-10-08 17:11:26 +00:00
|
|
|
|
|
|
|
// setup inheritance
|
|
|
|
newClass.__proto__ = AbstractClass;
|
2011-10-10 13:22:51 +00:00
|
|
|
util.inherits(newClass, AbstractClass);
|
2011-10-08 17:11:26 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-03-10 07:55:25 +00:00
|
|
|
Schema.prototype.tableName = function (modelName) {
|
|
|
|
return this.definitions[modelName].settings.table = this.definitions[modelName].settings.table || modelName
|
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Define foreign key
|
|
|
|
* @param {String} className
|
|
|
|
* @param {String} key - name of key field
|
|
|
|
*/
|
2011-10-08 17:11:26 +00:00
|
|
|
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;
|
2011-10-23 19:43:53 +00:00
|
|
|
this.definitions[className].properties[key] = {type: keyType};
|
2011-10-08 17:11:26 +00:00
|
|
|
}.bind(this));
|
|
|
|
} else {
|
2011-10-23 19:43:53 +00:00
|
|
|
this.definitions[className].properties[key] = {type: Number};
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Close database connection
|
|
|
|
*/
|
2011-10-21 12:46:09 +00:00
|
|
|
Schema.prototype.disconnect = function disconnect() {
|
|
|
|
if (typeof this.adapter.disconnect === 'function') {
|
|
|
|
this.adapter.disconnect();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Define hidden property
|
|
|
|
*/
|
2011-10-10 13:22:51 +00:00
|
|
|
function hiddenProperty(where, property, value) {
|
|
|
|
Object.defineProperty(where, property, {
|
|
|
|
writable: false,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: false,
|
|
|
|
value: value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|