Update jsdocs

This commit is contained in:
Raymond Feng 2013-08-13 09:37:09 -07:00
parent 74460316b8
commit 6c0f2483b3
2 changed files with 297 additions and 128 deletions

View File

@ -1,4 +1,4 @@
/** /*!
* Module dependencies * Module dependencies
*/ */
var ModelBuilder = require('./model-builder.js').ModelBuilder; var ModelBuilder = require('./model-builder.js').ModelBuilder;
@ -19,7 +19,7 @@ var existsSync = fs.existsSync || path.existsSync;
*/ */
exports.DataSource = DataSource; exports.DataSource = DataSource;
/** /*!
* Helpers * Helpers
*/ */
var slice = Array.prototype.slice; var slice = Array.prototype.slice;
@ -30,8 +30,8 @@ var slice = Array.prototype.slice;
* All classes in single dataSource shares same connector type and * All classes in single dataSource shares same connector type and
* one database connection * one database connection
* *
* @param name - type of dataSource connector (mysql, mongoose, sequelize, redis) * @param {String} name - type of dataSource connector (mysql, mongoose, sequelize, redis)
* @param settings - any database-specific settings which we need to * @param {Object} settings - any database-specific settings which we need to
* establish connection (of course it depends on specific connector) * establish connection (of course it depends on specific connector)
* *
* - host * - host
@ -106,7 +106,7 @@ function DataSource(name, settings) {
}); });
} }
}.bind(this)); }.bind(this));
}; }
util.inherits(DataSource, ModelBuilder); util.inherits(DataSource, ModelBuilder);
@ -145,8 +145,15 @@ DataSource.prototype._setupConnector = function () {
}; };
}; };
} }
} };
/**
* Set up the data source
* @param {String} name The name
* @param {Object} settings The settings
* @returns {*}
* @private
*/
DataSource.prototype.setup = function(name, settings) { DataSource.prototype.setup = function(name, settings) {
var dataSource = this; var dataSource = this;
var connector; var connector;
@ -253,10 +260,10 @@ DataSource.prototype.setup = function(name, settings) {
}); });
} }
}; };
} };
/** /**
* Define class * Define a model class
* *
* @param {String} className * @param {String} className
* @param {Object} properties - hash of class properties in format * @param {Object} properties - hash of class properties in format
@ -286,13 +293,21 @@ DataSource.prototype.setup = function(name, settings) {
* }); * });
* ``` * ```
*/ */
DataSource.prototype.define = function defineClass(className, properties, settings) { DataSource.prototype.createModel = DataSource.prototype.define = function defineClass(className, properties, settings) {
var args = slice.call(arguments); var args = slice.call(arguments);
if (!className) throw new Error('Class name required'); if (!className) {
if (args.length == 1) properties = {}, args.push(properties); throw new Error('Class name required');
if (args.length == 2) settings = {}, args.push(settings); }
if (args.length === 1) {
properties = {};
args.push(properties);
}
if (args.length === 2) {
settings = {};
args.push(settings);
}
properties = properties || {}; properties = properties || {};
settings = settings || {}; settings = settings || {};
@ -314,11 +329,11 @@ DataSource.prototype.define = function defineClass(className, properties, settin
return NewClass; return NewClass;
}; };
// alias createModel
DataSource.prototype.createModel = DataSource.prototype.define;
/** /**
* Mixin DataAccessObject methods. * Mixin DataAccessObject methods.
*
* @param {Function} ModelCtor The model constructor
*/ */
DataSource.prototype.mixin = function (ModelCtor) { DataSource.prototype.mixin = function (ModelCtor) {
@ -360,6 +375,8 @@ DataSource.prototype.mixin = function (ModelCtor) {
/** /**
* Attach an existing model to a data source. * Attach an existing model to a data source.
*
* @param {Function} ModelCtor The model constructor
*/ */
DataSource.prototype.attach = function (ModelCtor) { DataSource.prototype.attach = function (ModelCtor) {
@ -391,7 +408,7 @@ DataSource.prototype.attach = function (ModelCtor) {
this.models[className] = ModelCtor; this.models[className] = ModelCtor;
return this; return this;
} };
/** /**
* Define single property named `prop` on `model` * Define single property named `prop` on `model`
@ -413,6 +430,7 @@ DataSource.prototype.defineProperty = function (model, prop, params) {
* This method make sense only for sql connectors. * This method make sense only for sql connectors.
* *
* @param {String} or {[String]} Models to be migrated, if not present, apply to all models * @param {String} or {[String]} Models to be migrated, if not present, apply to all models
* @param {Function} [cb] The callback function
* *
* @warning All data will be lost! Use autoupdate if you need your data. * @warning All data will be lost! Use autoupdate if you need your data.
*/ */
@ -434,6 +452,7 @@ DataSource.prototype.automigrate = function (models, cb) {
* This method make sense only for sql connectors. * This method make sense only for sql connectors.
* *
* @param {String} or {[String]} Models to be migrated, if not present, apply to all models * @param {String} or {[String]} Models to be migrated, if not present, apply to all models
* @param {Function} [cb] The callback function
*/ */
DataSource.prototype.autoupdate = function (models, cb) { DataSource.prototype.autoupdate = function (models, cb) {
this.freeze(); this.freeze();
@ -451,12 +470,17 @@ DataSource.prototype.autoupdate = function (models, cb) {
/** /**
* Discover existing database tables. * Discover existing database tables.
* This method returns an array of model objects, including {type, name, onwer} * This method returns an array of model objects, including {type, name, onwer}
* *
* @param options An object that contains the following settings: * `options`
* all: true - Discovering all models, false - Discovering the models owned by the current user *
* views: true - Including views, false - only tables * all: true - Discovering all models, false - Discovering the models owned by the current user
* limit: The page size * views: true - Including views, false - only tables
* offset: The starting index * limit: The page size
* offset: The starting index
*
* @param {Object} options The options
* @param {Function} [cb] The callback function
*
*/ */
DataSource.prototype.discoverModelDefinitions = function (options, cb) { DataSource.prototype.discoverModelDefinitions = function (options, cb) {
this.freeze(); this.freeze();
@ -468,6 +492,11 @@ DataSource.prototype.discoverModelDefinitions = function (options, cb) {
}; };
/**
* The synchronous version of discoverModelDefinitions
* @param {Object} options The options
* @returns {*}
*/
DataSource.prototype.discoverModelDefinitionsSync = function (options) { DataSource.prototype.discoverModelDefinitionsSync = function (options) {
this.freeze(); this.freeze();
if (this.connector.discoverModelDefinitionsSync) { if (this.connector.discoverModelDefinitionsSync) {
@ -478,20 +507,42 @@ DataSource.prototype.discoverModelDefinitionsSync = function (options) {
/** /**
* Discover properties for a given model. * Discover properties for a given model.
* The owner *
* @param table The table/view name * `property description`
* @param cb Callback *
* The method return an array of properties, including {owner, tableName, columnName, dataType, dataLength, nullable} * owner {String} The database owner or schema
* tableName {String} The table/view name
* columnName {String} The column name
* dataType {String} The data type
* dataLength {Number} The data length
* dataPrecision {Number} The numeric data precision
* dataScale {Number} The numeric data scale
* nullable {Boolean} If the data can be null
*
* `options`
*
* owner/schema The database owner/schema
*
* @param {String} modelName The table/view name
* @param {Object} options The options
* @param {Function} [cb] The callback function
*
*/ */
DataSource.prototype.discoverModelProperties = function (table, options, cb) { DataSource.prototype.discoverModelProperties = function (modelName, options, cb) {
this.freeze(); this.freeze();
if (this.connector.discoverModelProperties) { if (this.connector.discoverModelProperties) {
this.connector.discoverModelProperties(table, options, cb); this.connector.discoverModelProperties(modelName, options, cb);
} else if (cb) { } else if (cb) {
cb(); cb();
} }
}; };
/**
* The synchronous version of discoverModelProperties
* @param {String} modelName The table/view name
* @param {Object} options The options
* @returns {*}
*/
DataSource.prototype.discoverModelPropertiesSync = function (modelName, options) { DataSource.prototype.discoverModelPropertiesSync = function (modelName, options) {
this.freeze(); this.freeze();
if (this.connector.discoverModelPropertiesSync) { if (this.connector.discoverModelPropertiesSync) {
@ -504,15 +555,22 @@ DataSource.prototype.discoverModelPropertiesSync = function (modelName, options)
* Discover primary keys for a given owner/modelName * Discover primary keys for a given owner/modelName
* *
* Each primary key column description has the following columns: * Each primary key column description has the following columns:
* owner String => table schema (may be null)
* tableName String => table name
* columnName String => column name
* keySeq Number => sequence number within primary key( a value of 1 represents the first column of the primary key, a value of 2 would represent the second column within the primary key).
* pkName String => primary key name (may be null)
* *
* The owner, default to current user * owner {String} => table schema (may be null)
* @param modelName The table name * tableName {String} => table name
* @param cb Callback * columnName {String} => column name
* keySeq {Number} => sequence number within primary key( a value of 1 represents the first column of the primary key, a value of 2 would represent the second column within the primary key).
* pkName {String} => primary key name (may be null)
*
* The owner, default to current user
*
* `options`
*
* owner/schema The database owner/schema
*
* @param {String} modelName The model name
* @param {Object} options The options
* @param {Function} [cb] The callback function
*/ */
DataSource.prototype.discoverPrimaryKeys= function(modelName, options, cb) { DataSource.prototype.discoverPrimaryKeys= function(modelName, options, cb) {
this.freeze(); this.freeze();
@ -521,33 +579,45 @@ DataSource.prototype.discoverPrimaryKeys= function(modelName, options, cb) {
} else if (cb) { } else if (cb) {
cb(); cb();
} }
} };
/**
* The synchronous version of discoverPrimaryKeys
* @param {String} modelName The model name
* @param {Object} options The options
* @returns {*}
*/
DataSource.prototype.discoverPrimaryKeysSync= function(modelName, options) { DataSource.prototype.discoverPrimaryKeysSync= function(modelName, options) {
this.freeze(); this.freeze();
if (this.connector.discoverPrimaryKeysSync) { if (this.connector.discoverPrimaryKeysSync) {
return this.connector.discoverPrimaryKeysSync(modelName, options); return this.connector.discoverPrimaryKeysSync(modelName, options);
} }
return null; return null;
} };
/** /**
* Discover foreign keys for a given owner/modelName * Discover foreign keys for a given owner/modelName
* *
* fkOwner String => foreign key table schema (may be null) * `foreign key description`
* fkName String => foreign key name (may be null)
* fkTableName String => foreign key table name
* fkColumnName String => foreign key column name
* keySeq short => sequence number within a foreign key( a value of 1 represents the first column of the foreign key, a value of 2 would represent the second column within the foreign key).
* pkOwner String => primary key table schema being imported (may be null)
* pkName String => primary key name (may be null)
* pkTableName String => primary key table name being imported
* pkColumnName String => primary key column name being imported
* *
* fkOwner String => foreign key table schema (may be null)
* fkName String => foreign key name (may be null)
* fkTableName String => foreign key table name
* fkColumnName String => foreign key column name
* keySeq Number => sequence number within a foreign key( a value of 1 represents the first column of the foreign key, a value of 2 would represent the second column within the foreign key).
* pkOwner String => primary key table schema being imported (may be null)
* pkName String => primary key name (may be null)
* pkTableName String => primary key table name being imported
* pkColumnName String => primary key column name being imported
*
* `options`
*
* owner/schema The database owner/schema
*
* @param {String} modelName The model name
* @param {Object} options The options
* @param {Function} [cb] The callback function
* *
* @param modelName
* @param cb
*/ */
DataSource.prototype.discoverForeignKeys= function(modelName, options, cb) { DataSource.prototype.discoverForeignKeys= function(modelName, options, cb) {
this.freeze(); this.freeze();
@ -556,8 +626,15 @@ DataSource.prototype.discoverForeignKeys= function(modelName, options, cb) {
} else if (cb) { } else if (cb) {
cb(); cb();
} }
} };
/**
* The synchronous version of discoverForeignKeys
*
* @param {String} modelName The model name
* @param {Object} options The options
* @returns {*}
*/
DataSource.prototype.discoverForeignKeysSync= function(modelName, options) { DataSource.prototype.discoverForeignKeysSync= function(modelName, options) {
this.freeze(); this.freeze();
if (this.connector.discoverForeignKeysSync) { if (this.connector.discoverForeignKeysSync) {
@ -570,19 +647,25 @@ DataSource.prototype.discoverForeignKeysSync= function(modelName, options) {
* Retrieves a description of the foreign key columns that reference the given table's primary key columns (the foreign keys exported by a table). * Retrieves a description of the foreign key columns that reference the given table's primary key columns (the foreign keys exported by a table).
* They are ordered by fkTableOwner, fkTableName, and keySeq. * They are ordered by fkTableOwner, fkTableName, and keySeq.
* *
* fkOwner String => foreign key table schema (may be null) * `foreign key description`
* fkName String => foreign key name (may be null)
* fkTableName String => foreign key table name
* fkColumnName String => foreign key column name
* keySeq short => sequence number within a foreign key( a value of 1 represents the first column of the foreign key, a value of 2 would represent the second column within the foreign key).
* pkOwner String => primary key table schema being imported (may be null)
* pkName String => primary key name (may be null)
* pkTableName String => primary key table name being imported
* pkColumnName String => primary key column name being imported
* *
* fkOwner {String} => foreign key table schema (may be null)
* fkName {String} => foreign key name (may be null)
* fkTableName {String} => foreign key table name
* fkColumnName {String} => foreign key column name
* keySeq {Number} => sequence number within a foreign key( a value of 1 represents the first column of the foreign key, a value of 2 would represent the second column within the foreign key).
* pkOwner {String} => primary key table schema being imported (may be null)
* pkName {String} => primary key name (may be null)
* pkTableName {String} => primary key table name being imported
* pkColumnName {String} => primary key column name being imported
* *
* @param modelName * `options`
* @param cb *
* owner/schema The database owner/schema
*
* @param {String} modelName The model name
* @param {Object} options The options
* @param {Function} [cb] The callback function
*/ */
DataSource.prototype.discoverExportedForeignKeys= function(modelName, options, cb) { DataSource.prototype.discoverExportedForeignKeys= function(modelName, options, cb) {
this.freeze(); this.freeze();
@ -591,8 +674,14 @@ DataSource.prototype.discoverExportedForeignKeys= function(modelName, options, c
} else if (cb) { } else if (cb) {
cb(); cb();
} }
} };
/**
* The synchronous version of discoverExportedForeignKeys
* @param {String} modelName The model name
* @param {Object} options The options
* @returns {*}
*/
DataSource.prototype.discoverExportedForeignKeysSync= function(modelName, options) { DataSource.prototype.discoverExportedForeignKeysSync= function(modelName, options) {
this.freeze(); this.freeze();
if (this.connector.discoverExportedForeignKeysSync) { if (this.connector.discoverExportedForeignKeysSync) {
@ -621,6 +710,13 @@ function fromDBName(dbName, camelCase) {
return parts.join(''); return parts.join('');
} }
/**
* Discover one schema from the given model without following the associations
*
* @param {String} modelName The model name
* @param {Object} [options] The options
* @param {Function} [cb] The callback function
*/
DataSource.prototype.discoverSchema = function (modelName, options, cb) { DataSource.prototype.discoverSchema = function (modelName, options, cb) {
options = options || {}; options = options || {};
@ -646,8 +742,16 @@ DataSource.prototype.discoverSchema = function (modelName, options, cb) {
/** /**
* Discover schema from a given modelName/view * Discover schema from a given modelName/view
* *
* @param modelName * `options`
* @param cb *
* {String} owner/schema - The database owner/schema name
* {Boolean} associations - If relations (primary key/foreign key) are navigated
* {Boolean} all - If all owners are included
* {Boolean} views - If views are included
*
* @param {String} modelName The model name
* @param {Object} [options] The options
* @param {Function} [cb] The callback function
*/ */
DataSource.prototype.discoverSchemas = function (modelName, options, cb) { DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
options = options || {}; options = options || {};
@ -803,14 +907,21 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
}); });
} }
}); });
} };
/** /**
* Discover schema from a given table/view * Discover schema from a given table/view synchronously
* *
* @param modelName * `options`
* @param cb *
* {String} owner/schema - The database owner/schema name
* {Boolean} associations - If relations (primary key/foreign key) are navigated
* {Boolean} all - If all owners are included
* {Boolean} views - If views are included
*
* @param {String} modelName The model name
* @param {Object} [options] The options
*/ */
DataSource.prototype.discoverSchemasSync = function (modelName, options) { DataSource.prototype.discoverSchemasSync = function (modelName, options) {
var self = this; var self = this;
@ -939,14 +1050,21 @@ DataSource.prototype.discoverSchemasSync = function (modelName, options) {
return options.visited; return options.visited;
} }
} };
/** /**
* Discover and build models from the given owner/modelName * Discover and build models from the given owner/modelName
* *
* @param modelName * `options`
* @param options *
* @param cb * {String} owner/schema - The database owner/schema name
* {Boolean} associations - If relations (primary key/foreign key) are navigated
* {Boolean} all - If all owners are included
* {Boolean} views - If views are included
*
* @param {String} modelName The model name
* @param {Object} [options] The options
* @param {Function} [cb] The callback function
*/ */
DataSource.prototype.discoverAndBuildModels = function (modelName, options, cb) { DataSource.prototype.discoverAndBuildModels = function (modelName, options, cb) {
var self = this; var self = this;
@ -965,13 +1083,20 @@ DataSource.prototype.discoverAndBuildModels = function (modelName, options, cb)
var models = self.buildModels(schemaList); var models = self.buildModels(schemaList);
cb && cb(err, models); cb && cb(err, models);
}); });
} };
/** /**
* Discover and build models from the given owner/modelName synchronously * Discover and build models from the given owner/modelName synchronously
* *
* @param modelName * `options`
* @param options *
* {String} owner/schema - The database owner/schema name
* {Boolean} associations - If relations (primary key/foreign key) are navigated
* {Boolean} all - If all owners are included
* {Boolean} views - If views are included
*
* @param {String} modelName The model name
* @param {Object} [options] The options
*/ */
DataSource.prototype.discoverAndBuildModelsSync = function (modelName, options) { DataSource.prototype.discoverAndBuildModelsSync = function (modelName, options) {
var schemas = this.discoverSchemasSync(modelName, options); var schemas = this.discoverSchemasSync(modelName, options);
@ -984,18 +1109,27 @@ DataSource.prototype.discoverAndBuildModelsSync = function (modelName, options)
var models = this.buildModels(schemaList); var models = this.buildModels(schemaList);
return models; return models;
} };
/** /**
* Check whether migrations needed * Check whether migrations needed
* This method make sense only for sql connectors. * This method make sense only for sql connectors.
* @param {String[]} [models] A model name or an array of model names. If not present, apply to all models
*/ */
DataSource.prototype.isActual = function (cb) { DataSource.prototype.isActual = function (models, cb) {
this.freeze(); this.freeze();
if (this.connector.isActual) { if (this.connector.isActual) {
this.connector.isActual(cb); this.connector.isActual(models, cb);
} else if (cb) { } else {
cb(null, true); if ((!cb) && ('function' === typeof models)) {
cb = models;
models = undefined;
}
if (cb) {
process.nextTick(function() {
cb(null, true);
});
}
} }
}; };
@ -1023,7 +1157,7 @@ DataSource.prototype.freeze = function freeze() {
/** /**
* Return table name for specified `modelName` * Return table name for specified `modelName`
* @param {String} modelName * @param {String} modelName The model name
*/ */
DataSource.prototype.tableName = function (modelName) { DataSource.prototype.tableName = function (modelName) {
var settings = this.definitions[modelName].settings; var settings = this.definitions[modelName].settings;
@ -1036,8 +1170,8 @@ DataSource.prototype.tableName = function (modelName) {
/** /**
* Return column name for specified modelName and propertyName * Return column name for specified modelName and propertyName
* @param modelName * @param {String} modelName The model name
* @param propertyName * @param propertyName The property name
* @returns {String} columnName * @returns {String} columnName
*/ */
DataSource.prototype.columnName = function (modelName, propertyName) { DataSource.prototype.columnName = function (modelName, propertyName) {
@ -1054,8 +1188,8 @@ DataSource.prototype.columnName = function (modelName, propertyName) {
/** /**
* Return column metadata for specified modelName and propertyName * Return column metadata for specified modelName and propertyName
* @param modelName * @param {String} modelName The model name
* @param propertyName * @param propertyName The property name
* @returns {Object} column metadata * @returns {Object} column metadata
*/ */
DataSource.prototype.columnMetadata = function (modelName, propertyName) { DataSource.prototype.columnMetadata = function (modelName, propertyName) {
@ -1072,8 +1206,8 @@ DataSource.prototype.columnMetadata = function (modelName, propertyName) {
/** /**
* Return column names for specified modelName * Return column names for specified modelName
* @param modelName * @param {String} modelName The model name
* @returns {[String]} column names * @returns {String[]} column names
*/ */
DataSource.prototype.columnNames = function (modelName) { DataSource.prototype.columnNames = function (modelName) {
var props = this.definitions[modelName].properties; var props = this.definitions[modelName].properties;
@ -1090,17 +1224,17 @@ DataSource.prototype.columnNames = function (modelName) {
/** /**
* Find the ID column name * Find the ID column name
* @param modelName * @param {String} modelName The model name
* @returns {String} columnName for ID * @returns {String} columnName for ID
*/ */
DataSource.prototype.idColumnName = function(modelName) { DataSource.prototype.idColumnName = function(modelName) {
return this.columnName(modelName, this.idName(modelName)); return this.columnName(modelName, this.idName(modelName));
} };
/** /**
* Find the ID property name * Find the ID property name
* @param modelName * @param {String} modelName The model name
* @returns {String} property for ID * @returns {String} property name for ID
*/ */
DataSource.prototype.idName = function(modelName) { DataSource.prototype.idName = function(modelName) {
var props = this.definitions[modelName].properties; var props = this.definitions[modelName].properties;
@ -1110,12 +1244,12 @@ DataSource.prototype.idName = function(modelName) {
} }
} }
return null; return null;
} };
/** /**
* Find the ID property names sorted by the index * Find the ID property names sorted by the index
* @param modelName * @param {String} modelName The model name
* @returns {[String]} property names for IDs * @returns {String[]} property names for IDs
*/ */
DataSource.prototype.idNames = function (modelName) { DataSource.prototype.idNames = function (modelName) {
var ids = []; var ids = [];
@ -1132,13 +1266,14 @@ DataSource.prototype.idNames = function (modelName) {
return id.name; return id.name;
}); });
return names; return names;
} };
/** /**
* Define foreign key * Define foreign key to another model
* @param {String} className * @param {String} className The model name that owns the key
* @param {String} key - name of key field * @param {String} key - name of key field
* @param {String} foreignClassName The foreign model name
*/ */
DataSource.prototype.defineForeignKey = function defineForeignKey(className, key, foreignClassName) { DataSource.prototype.defineForeignKey = function defineForeignKey(className, key, foreignClassName) {
// quit if key already defined // quit if key already defined
@ -1167,6 +1302,7 @@ DataSource.prototype.defineForeignKey = function defineForeignKey(className, key
/** /**
* Close database connection * Close database connection
* @param {Fucntion} [cb] The callback function
*/ */
DataSource.prototype.disconnect = function disconnect(cb) { DataSource.prototype.disconnect = function disconnect(cb) {
var self = this; var self = this;
@ -1182,6 +1318,13 @@ DataSource.prototype.disconnect = function disconnect(cb) {
} }
}; };
/**
* Copy the model from Master
* @param {Function} Master The model constructor
* @returns {Function} The copy of the model constructor
*
* @private
*/
DataSource.prototype.copyModel = function copyModel(Master) { DataSource.prototype.copyModel = function copyModel(Master) {
var dataSource = this; var dataSource = this;
var className = Master.modelName; var className = Master.modelName;
@ -1221,6 +1364,11 @@ DataSource.prototype.copyModel = function copyModel(Master) {
return Slave; return Slave;
}; };
/**
*
* @returns {EventEmitter}
* @private
*/
DataSource.prototype.transaction = function() { DataSource.prototype.transaction = function() {
var dataSource = this; var dataSource = this;
var transaction = new EventEmitter; var transaction = new EventEmitter;
@ -1250,7 +1398,8 @@ DataSource.prototype.transaction = function() {
}; };
/** /**
* Enable a data source operation remotely. * Enable a data source operation to be remote.
* @param {String} operation The operation name
*/ */
DataSource.prototype.enableRemote = function (operation) { DataSource.prototype.enableRemote = function (operation) {
@ -1263,7 +1412,8 @@ DataSource.prototype.enableRemote = function (operation) {
} }
/** /**
* Disable a data source operation remotely. * Disable a data source operation to be remote.
* @param {String} operation The operation name
*/ */
DataSource.prototype.disableRemote = function (operation) { DataSource.prototype.disableRemote = function (operation) {
@ -1277,6 +1427,7 @@ DataSource.prototype.disableRemote = function (operation) {
/** /**
* Get an operation's metadata. * Get an operation's metadata.
* @param {String} operation The operation name
*/ */
DataSource.prototype.getOperation = function (operation) { DataSource.prototype.getOperation = function (operation) {
@ -1295,30 +1446,38 @@ DataSource.prototype.getOperation = function (operation) {
/** /**
* Get all operations. * Get all operations.
*/ */
DataSource.prototype.operations = function () { DataSource.prototype.operations = function () {
return this._operations; return this._operations;
} }
/** /**
* Define an operation. * Define an operation to the data source
* @param {String} name The operation name
* @param {Object} options The options
* @param [Function} fn The function
*/ */
DataSource.prototype.defineOperation = function (name, options, fn) { DataSource.prototype.defineOperation = function (name, options, fn) {
options.fn = fn; options.fn = fn;
options.name = name; options.name = name;
this._operations[name] = options; this._operations[name] = options;
} }
/**
* Check if the backend is a relational DB
* @returns {Boolean}
*/
DataSource.prototype.isRelational = function() { DataSource.prototype.isRelational = function() {
return this.connector && this.connector.relational; return this.connector && this.connector.relational;
} }
/** /**
* Define hidden property * Define a hidden property
* @param {Object} obj The property owner
* @param {String} key The property name
* @param {Mixed} value The default value
*/ */
function hiddenProperty(where, property, value) { function hiddenProperty(obj, key, value) {
Object.defineProperty(where, property, { Object.defineProperty(obj, key, {
writable: false, writable: false,
enumerable: false, enumerable: false,
configurable: false, configurable: false,
@ -1329,9 +1488,9 @@ function hiddenProperty(where, property, value) {
/** /**
* Define readonly property on object * Define readonly property on object
* *
* @param {Object} obj * @param {Object} obj The property owner
* @param {String} key * @param {String} key The property name
* @param {Mixed} value * @param {Mixed} value The default value
*/ */
function defineReadonlyProp(obj, key, value) { function defineReadonlyProp(obj, key, value) {
Object.defineProperty(obj, key, { Object.defineProperty(obj, key, {

View File

@ -1,4 +1,4 @@
/** /*!
* Module dependencies * Module dependencies
*/ */
@ -16,26 +16,34 @@ var introspect = require('./introspection')(ModelBuilder);
/** /**
* Export public API * Export public API
*/ */
exports.Schema = exports.ModelBuilder = ModelBuilder; exports.ModelBuilder = exports.Schema = ModelBuilder;
/** /*!
* Helpers * Helpers
*/ */
var slice = Array.prototype.slice; var slice = Array.prototype.slice;
/** /**
* ModelBuilder - Data Model Definition * ModelBuilder - A builder to define data models
*
* @constructor
*/ */
function ModelBuilder() { function ModelBuilder() {
// create blank models pool // create blank models pool
/**
* @property {Object} models Model constructors
*/
this.models = {}; this.models = {};
/**
* @property {Object} definitions Definitions of the models
*/
this.definitions = {}; this.definitions = {};
} }
util.inherits(ModelBuilder, EventEmitter); util.inherits(ModelBuilder, EventEmitter);
/** /**
* Define class * Define a model class
* *
* @param {String} className * @param {String} className
* @param {Object} properties - hash of class properties in format * @param {Object} properties - hash of class properties in format
@ -292,7 +300,7 @@ function standartize(properties, settings) {
* Define single property named `prop` on `model` * Define single property named `prop` on `model`
* *
* @param {String} model - name of model * @param {String} model - name of model
* @param {String} prop - name of propery * @param {String} prop - name of property
* @param {Object} params - property settings * @param {Object} params - property settings
*/ */
ModelBuilder.prototype.defineProperty = function (model, prop, params) { ModelBuilder.prototype.defineProperty = function (model, prop, params) {
@ -333,7 +341,6 @@ ModelBuilder.prototype.extendModel = function (model, props) {
}; };
ModelBuilder.prototype.copyModel = function copyModel(Master) { ModelBuilder.prototype.copyModel = function copyModel(Master) {
var dataSource = this; var dataSource = this;
var className = Master.modelName; var className = Master.modelName;
@ -365,8 +372,7 @@ ModelBuilder.prototype.copyModel = function copyModel(Master) {
}; };
/*!
/**
* Define hidden property * Define hidden property
*/ */
function hiddenProperty(where, property, value) { function hiddenProperty(where, property, value) {
@ -378,7 +384,7 @@ function hiddenProperty(where, property, value) {
}); });
} }
/** /*!
* Define readonly property on object * Define readonly property on object
* *
* @param {Object} obj * @param {Object} obj
@ -396,7 +402,7 @@ function defineReadonlyProp(obj, key, value) {
/** /**
* Resolve the type string to be a function, for example, 'String' to String * Resolve the type string to be a function, for example, 'String' to String
* @param type The type string, such as 'number', 'Number', 'boolean', or 'String'. It's case insensitive * @param {String} type The type string, such as 'number', 'Number', 'boolean', or 'String'. It's case insensitive
* @returns {Function} if the type is resolved * @returns {Function} if the type is resolved
*/ */
ModelBuilder.prototype.getSchemaType = function(type) { ModelBuilder.prototype.getSchemaType = function(type) {
@ -441,9 +447,9 @@ ModelBuilder.prototype.getSchemaType = function(type) {
/** /**
* Build a dataSource * Build a dataSource
* @param name The name of the dataSource * @param {String} name The name of the dataSource
* @param properties The properties of the dataSource * @param {Object} properties The properties of the dataSource
* @param associations An array of associations between models * @param {Object[]} associations An array of associations between models
* @returns {*} * @returns {*}
*/ */
ModelBuilder.prototype.buildSchema = function(name, properties, associations) { ModelBuilder.prototype.buildSchema = function(name, properties, associations) {
@ -478,10 +484,14 @@ ModelBuilder.prototype.buildSchema = function(name, properties, associations) {
/** /**
* Build models from dataSource definitions * Build models from dataSource definitions
* @param schemas The schemas can be one of the following three formats: *
* `schemas` can be one of the following:
*
* 1. An array of named dataSource definition JSON objects * 1. An array of named dataSource definition JSON objects
* 2. A dataSource definition JSON object * 2. A dataSource definition JSON object
* 3. A list of property definitions (anonymous) * 3. A list of property definitions (anonymous)
*
* @param {*} schemas The schemas
* @returns {Object} A map of model constructors keyed by model name * @returns {Object} A map of model constructors keyed by model name
*/ */
ModelBuilder.prototype.buildModels = function (schemas) { ModelBuilder.prototype.buildModels = function (schemas) {
@ -526,9 +536,9 @@ ModelBuilder.prototype.buildModels = function (schemas) {
/** /**
* Introspect the json document to build a corresponding model * Introspect the json document to build a corresponding model
* @param {String} name * @param {String} name The model name
* @param {Object} json * @param {Object} json The json object
* @param [Object} options * @param [Object} options The options
* @returns {} * @returns {}
*/ */
ModelBuilder.prototype.buildModelFromInstance = function(name, json, options) { ModelBuilder.prototype.buildModelFromInstance = function(name, json, options) {
@ -538,7 +548,7 @@ ModelBuilder.prototype.buildModelFromInstance = function(name, json, options) {
// Create a model for the generated schema // Create a model for the generated schema
return this.define(name, schema, options); return this.define(name, schema, options);
} };