Use String[] for types and add test for supported types

This commit is contained in:
Raymond Feng 2014-01-28 14:23:48 -08:00
parent cf200a2e27
commit da571c0c23
6 changed files with 110 additions and 16 deletions

View File

@ -17,12 +17,12 @@ function Connector(name, settings) {
*/ */
Connector.prototype.relational = false; Connector.prototype.relational = false;
/*! /**
* Get the connector type/subtype * Get types associated with the connector
* @returns {String} The type for the connector * @returns {String[]} The types for the connector
*/ */
Connector.prototype.getType = function() { Connector.prototype.getTypes = function() {
return 'db/nosql'; return ['db', 'nosql'];
}; };
/** /**
@ -33,6 +33,26 @@ Connector.prototype.getDefaultIdType = function() {
return String; return String;
}; };
/**
* Get the metadata for the connector
* @returns {Object} The metadata object
* @property {String} type The type for the backend
* @property {Function} defaultIdType The default id type
* @property {Boolean} [isRelational] If the connector represents a relational database
* @property {Object} schemaForSettings The schema for settings object
*/
Connector.prototype.getMedadata = function () {
if (!this._metadata) {
this._metadata = {
types: this.getTypes(),
defaultIdType: this.getDefaultIdType(),
isRelational: this.isRelational || (this.getTypes().indexOf('rdbms') !== -1),
schemaForSettings: {}
};
}
return this._metadata;
};
/** /**
* Execute a command with given parameters * Execute a command with given parameters
* @param {String} command The command such as SQL * @param {String} command The command such as SQL

View File

@ -39,8 +39,8 @@ Memory.prototype.getDefaultIdType = function() {
return Number; return Number;
}; };
Memory.prototype.getType = function() { Memory.prototype.getTypes = function() {
return 'db/nosql/memory'; return ['db', 'nosql', 'memory'];
}; };
Memory.prototype.connect = function (callback) { Memory.prototype.connect = function (callback) {

View File

@ -569,20 +569,53 @@ DataSource.prototype.mixin = function (ModelCtor) {
}); });
}; };
/**
* @see ModelBuilder.prototype.getModel
*/
DataSource.prototype.getModel = function (name, forceCreate) { DataSource.prototype.getModel = function (name, forceCreate) {
return this.modelBuilder.getModel(name, forceCreate); return this.modelBuilder.getModel(name, forceCreate);
}; };
/**
* @see ModelBuilder.prototype.getModelDefinition
*/
DataSource.prototype.getModelDefinition = function (name) { DataSource.prototype.getModelDefinition = function (name) {
return this.modelBuilder.getModelDefinition(name); return this.modelBuilder.getModelDefinition(name);
}; };
/** /**
* Get the data source type * Get the data source types
* @returns {String} The data source type, such as db/relational, db/nosql, rest * @returns {String[]} The data source type, such as ['db', 'nosql', 'mongodb'],
* ['rest'], or ['db', 'rdbms', 'mysql']
*/ */
DataSource.prototype.getType = function() { DataSource.prototype.getTypes = function () {
return this.connector && this.connector.getType(); var types = this.connector && this.connector.getTypes() || [];
if (typeof types === 'string') {
types = types.split(/[\s,\/]+/);
}
return types;
};
/**
* Check the data source supports the given types
* @param String|String[]) types A type name or an array of type names
* @return {Boolean} true if all types are supported by the data source
*/
DataSource.prototype.supportTypes = function (types) {
var supportedTypes = this.getTypes();
if (Array.isArray(types)) {
// Check each of the types
for (var i = 0; i < types.length; i++) {
if (supportedTypes.indexOf(types[i]) === -1) {
// Not supported
return false;
}
}
return true;
} else {
// The types is a string
return supportedTypes.indexOf(types) !== -1;
}
}; };
/** /**

View File

@ -71,6 +71,11 @@ ModelBuilder.prototype.getModel = function (name, forceCreate) {
return model; return model;
}; };
/**
* Get the model definition by name
* @param {String} name The model name
* @returns {ModelDefinition} The model definition
*/
ModelBuilder.prototype.getModelDefinition = function (name) { ModelBuilder.prototype.getModelDefinition = function (name) {
return this.definitions[name]; return this.definitions[name];
}; };

View File

@ -19,12 +19,12 @@ util.inherits(BaseSQL, Connector);
*/ */
BaseSQL.prototype.relational = true; BaseSQL.prototype.relational = true;
/*! /**
* Get the connector type/subtype * Get types associated with the connector
* @returns {string} * @returns {String[]} The types for the connector
*/ */
BaseSQL.prototype.getType = function() { BaseSQL.prototype.getTypes = function() {
return 'db/relational'; return ['db', 'rdbms', 'sql'];
}; };
/*! /*!

View File

@ -479,6 +479,42 @@ describe('Load models with base', function () {
}); });
}); });
describe('DataSource connector types', function() {
it('should return an array of types', function() {
var ds = new DataSource('memory');
var types = ds.getTypes();
assert.deepEqual(types, ['db', 'nosql', 'memory']);
});
it('should test supported types by string', function() {
var ds = new DataSource('memory');
var result = ds.supportTypes('db');
assert(result);
});
it('should test supported types by array', function() {
var ds = new DataSource('memory');
var result = ds.supportTypes(['db', 'memory']);
assert(result);
});
it('should test unsupported types by string', function() {
var ds = new DataSource('memory');
var result = ds.supportTypes('rdbms');
assert(!result);
});
it('should test unsupported types by array', function() {
var ds = new DataSource('memory');
var result = ds.supportTypes(['rdbms', 'memory']);
assert(!result);
result = ds.supportTypes(['rdbms']);
assert(!result);
});
});
describe('DataSource constructor', function () { describe('DataSource constructor', function () {
// Mocked require // Mocked require
var loader = function (name) { var loader = function (name) {