Asteroid 0.7 updates
This commit is contained in:
parent
36c22ab972
commit
c87abafec8
|
@ -60,6 +60,40 @@ function DataSource(name, settings) {
|
||||||
// default DataAccessObject
|
// default DataAccessObject
|
||||||
this.DataAccessObject = this.constructor.DataAccessObject;
|
this.DataAccessObject = this.constructor.DataAccessObject;
|
||||||
this.DataAccessObject.call(this, arguments);
|
this.DataAccessObject.call(this, arguments);
|
||||||
|
|
||||||
|
// operation metadata
|
||||||
|
this._operations = {};
|
||||||
|
|
||||||
|
// define DataAccessObject methods
|
||||||
|
Object.keys(this.DataAccessObject).forEach(function (name) {
|
||||||
|
var fn = this.DataAccessObject[name];
|
||||||
|
|
||||||
|
if(typeof fn === 'function') {
|
||||||
|
this.defineOperation(name, {
|
||||||
|
accepts: fn.accepts,
|
||||||
|
returns: fn.returns,
|
||||||
|
http: fn.http,
|
||||||
|
remoteEnabled: fn.share,
|
||||||
|
fn: fn
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}.bind(this));
|
||||||
|
|
||||||
|
// define DataAccessObject.prototype methods
|
||||||
|
Object.keys(this.DataAccessObject.prototype).forEach(function (name) {
|
||||||
|
var fn = this.DataAccessObject.prototype[name];
|
||||||
|
|
||||||
|
if(typeof fn === 'function') {
|
||||||
|
this.defineOperation(name, {
|
||||||
|
prototype: true,
|
||||||
|
accepts: fn.accepts,
|
||||||
|
returns: fn.returns,
|
||||||
|
http: fn.http,
|
||||||
|
remoteEnabled: fn.share,
|
||||||
|
fn: fn
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}.bind(this));
|
||||||
};
|
};
|
||||||
|
|
||||||
util.inherits(DataSource, ModelBuilder);
|
util.inherits(DataSource, ModelBuilder);
|
||||||
|
@ -76,6 +110,14 @@ for (var m in ModelBuilder) {
|
||||||
|
|
||||||
DataSource.prototype.setup = function(name, settings) {
|
DataSource.prototype.setup = function(name, settings) {
|
||||||
var schema = this;
|
var schema = this;
|
||||||
|
var adapter;
|
||||||
|
|
||||||
|
// support single settings object
|
||||||
|
if(typeof name === 'object') {
|
||||||
|
settings = name;
|
||||||
|
name = undefined;
|
||||||
|
adapter = settings.connector || settings.adapter;
|
||||||
|
}
|
||||||
|
|
||||||
// just save everything we get
|
// just save everything we get
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -85,11 +127,10 @@ DataSource.prototype.setup = function(name, settings) {
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
this.connecting = false;
|
this.connecting = false;
|
||||||
|
|
||||||
if (name) {
|
if (name && !adapter) {
|
||||||
// and initialize schema using adapter
|
// and initialize schema using adapter
|
||||||
// this is only one initialization entry point of adapter
|
// this is only one initialization entry point of adapter
|
||||||
// this module should define `adapter` member of `this` (schema)
|
// this module should define `adapter` member of `this` (schema)
|
||||||
var adapter;
|
|
||||||
if (typeof name === 'object') {
|
if (typeof name === 'object') {
|
||||||
adapter = name;
|
adapter = name;
|
||||||
this.name = adapter.name;
|
this.name = adapter.name;
|
||||||
|
@ -161,6 +202,15 @@ DataSource.prototype.setup = function(name, settings) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the connector.
|
||||||
|
*/
|
||||||
|
|
||||||
|
DataSource.prototype.connector = function () {
|
||||||
|
// alias for adapter compatibility
|
||||||
|
return this.adapter;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define class
|
* Define class
|
||||||
*
|
*
|
||||||
|
@ -220,15 +270,38 @@ DataSource.prototype.define = function defineClass(className, properties, settin
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// alias createModel
|
||||||
|
DataSource.prototype.createModel = DataSource.prototype.define;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mixin DataAccessObject methods.
|
* Mixin DataAccessObject methods.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
DataSource.prototype.mixin = function (ModelCtor) {
|
DataSource.prototype.mixin = function (ModelCtor) {
|
||||||
|
var ops = this.operations();
|
||||||
|
|
||||||
// inherit DataAccessObject methods
|
// mixin enabled operations as alias functions
|
||||||
jutil.mixin(ModelCtor, this.DataAccessObject);
|
Object.keys(ops).forEach(function (name) {
|
||||||
|
console.log(name);
|
||||||
|
|
||||||
|
var op = ops[name];
|
||||||
|
var fn = op.fn;
|
||||||
|
var scope;
|
||||||
|
|
||||||
|
if(op.enabled) {
|
||||||
|
scope = op.prototype ? ModelCtor.prototype : ModelCtor;
|
||||||
|
var sfn = scope[name] = function () {
|
||||||
|
fn.apply(this, arguments);
|
||||||
|
}
|
||||||
|
Object.keys(op).forEach(function (key) {
|
||||||
|
if(key !== 'fn' && key !== 'prototype') {
|
||||||
|
if(typeof op[key] !== 'undefined') {
|
||||||
|
sfn[key] = op[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1071,6 +1144,92 @@ DataSource.prototype.transaction = function() {
|
||||||
return transaction;
|
return transaction;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable a data source operation.
|
||||||
|
*/
|
||||||
|
|
||||||
|
DataSource.prototype.enable = function (operation) {
|
||||||
|
var op = this.getOperation(operation);
|
||||||
|
if(op) {
|
||||||
|
op.enabled = true;
|
||||||
|
} else {
|
||||||
|
throw new Error(operation + ' is provided by the attached connector');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable a data source operation remotely.
|
||||||
|
*/
|
||||||
|
|
||||||
|
DataSource.prototype.enableRemote = function (operation) {
|
||||||
|
var op = this.getOperation(operation);
|
||||||
|
if(op) {
|
||||||
|
op.remoteEnabled = true;
|
||||||
|
} else {
|
||||||
|
throw new Error(operation + ' is provided by the attached connector');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disable a data source operation.
|
||||||
|
*/
|
||||||
|
|
||||||
|
DataSource.prototype.disable = function (operation) {
|
||||||
|
var op = this.getOperation(operation);
|
||||||
|
if(op) {
|
||||||
|
op.enabled = false;
|
||||||
|
} else {
|
||||||
|
throw new Error(operation + ' is provided by the attached connector');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disable a data source operation remotely.
|
||||||
|
*/
|
||||||
|
|
||||||
|
DataSource.prototype.disableRemote = function (operation) {
|
||||||
|
var op = this.getOperation(operation);
|
||||||
|
if(op) {
|
||||||
|
op.remoteEnabled = false;
|
||||||
|
} else {
|
||||||
|
throw new Error(operation + ' is provided by the attached connector');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get an operation's metadata.
|
||||||
|
*/
|
||||||
|
|
||||||
|
DataSource.prototype.getOperation = function (operation) {
|
||||||
|
var ops = this.operations();
|
||||||
|
|
||||||
|
for(var i = 0; i < ops.length; i++) {
|
||||||
|
var op = ops[i];
|
||||||
|
|
||||||
|
if(op.name === operation) {
|
||||||
|
return op;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all operations.
|
||||||
|
*/
|
||||||
|
|
||||||
|
DataSource.prototype.operations = function () {
|
||||||
|
return this._operations;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define an operation.
|
||||||
|
*/
|
||||||
|
|
||||||
|
DataSource.prototype.defineOperation = function (name, options, fn) {
|
||||||
|
options.fn = fn;
|
||||||
|
options.enabled = true;
|
||||||
|
this._operations[name] = options;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define hidden property
|
* Define hidden property
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue