Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var loopback = require('loopback');
var path = require('path');
var app = module.exports = loopback();
var env = app.get('env');
var boot = require('loopback-boot');
var started = new Date();

Expand Down Expand Up @@ -36,7 +37,9 @@ require('./connector');
*/

app.use(loopback.favicon());
app.use(loopback.logger(app.get('env') === 'development' ? 'dev' : 'default'));
if(env !== 'test') {
app.use(loopback.logger(env === 'development' ? 'dev' : 'default'));
}
app.use(loopback.cookieParser(app.get('cookieSecret')));
app.use(loopback.token({model: app.models.accessToken}));
app.use(loopback.methodOverride());
Expand Down
2 changes: 1 addition & 1 deletion models.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"properties": {
"name": {"id": true, "type": "string"}
},
"public": false,
"public": true,
"dataSource": "db"
},
"PackageDefinition": {
Expand Down
77 changes: 63 additions & 14 deletions models/data-source-definition.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var app = require('../app');
var loopback = require('loopback');

/*
TODOs

- implement discovery
- implement connection testing

- add a flag indicating if discover is supported

*/
Expand Down Expand Up @@ -36,34 +35,70 @@ DataSourceDefinition.validatesPresenceOf('name', 'connector');
*/

DataSourceDefinition.prototype.testConnection = function(cb) {

var dataSource = this.toDataSource();
var timeout = DataSourceDefinition.settings.testConnectionTimeout || 60000;
dataSource.once('connected', function() {
clearTimeout(timer);
cb(null, true);
});
dataSource.once('error', cb);
var timer = setTimeout(function() {
cb(new Error('connection timed out after ' + timeout + 'ms'));
}, timeout);
dataSource.connect();
}

/**
* Get the schema for the given table / collection name.
* Discover the model definition by table name from this data source. Use the `name`
* provided by items from returned from `DataSourceDefinition.getSchema()`.
*
* @param {String} name The collection / table name.
* @callback {Function} callback
* @param {Error} err
* @param {DatabaseColumn[]} columns An array of columns
* @param {String} modelName The model name (usually from `DataSourceDefinition.getSchema()`.
* @options {Object} [options] Options; see below.
* @property {String} owner|schema Database owner or schema name.
* @property {Boolean} relations True if relations (primary key/foreign key) are navigated; false otherwise.
* @property {Boolean} all True if all owners are included; false otherwise.
* @property {Boolean} views True if views are included; false otherwise.
*/

DataSourceDefinition.prototype.getSchema = function(name, cb) {

DataSourceDefinition.prototype.discoverModelDefinition = function(name, options, cb) {
this.toDataSource().discoverSchemas(name, options, cb);
}

loopback.remoteMethod(DataSourceDefinition.prototype.discoverModelDefinition, {
http: {verb: 'get', path: '/definitions/:name'},
accepts: [
{ arg: 'name', type: 'string', http: { source: 'path' } },
{ arg: 'options', type: 'object' }
],
returns: { arg: 'definition', type: 'object' }
});

/**
* Discover model definitions available from this data source.
* Get a list of table / collection names, owners and types.
*
* @param {Object} options The options
* @param {Function} Callback function. Optional.
* @options {Object} options Discovery options. See below.
* @property {Boolean} all If true, discover all models; if false, discover only
* models owned by the current user.
* @property {Boolean} views If true, nclude views; if false, only tables.
* @property {Number} limit Page size
* @property {Number} offset Starting index
* @callback {Function} callback
* @param {Error} err
* @param {ModelDefinition[]} models An array of model definitions
*/

DataSourceDefinition.prototype.discoverModelDefinitions = function(cb) {

DataSourceDefinition.prototype.getSchema = function(options, cb) {
this.toDataSource().discoverModelDefinitions(options, cb);
}

loopback.remoteMethod(DataSourceDefinition.prototype.getSchema, {
http: {verb: 'get', path: '/schema'},
accepts: { arg: 'options', type: 'object' },
returns: { arg: 'schema', type: 'object' }
});

/**
* Run a migration on the data source. Creates indexes, tables, collections, etc.
*
Expand All @@ -77,6 +112,8 @@ DataSourceDefinition.prototype.automigrate = function() {

}

loopback.remoteMethod(DataSourceDefinition.prototype.automigrate);

/**
* Update existing tables / collections.
*
Expand All @@ -87,3 +124,15 @@ DataSourceDefinition.prototype.automigrate = function() {
DataSourceDefinition.prototype.autoupdate = function() {

}

loopback.remoteMethod(DataSourceDefinition.prototype.autoupdate);

/**
* Create a `loopback.DataSource` object from the `DataSourceDefinition`.
*
* @returns {DataSource}
*/

DataSourceDefinition.prototype.toDataSource = function() {
return loopback.createDataSource(this.name, this);
}
24 changes: 24 additions & 0 deletions models/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var ViewDefinition = app.models.ViewDefinition;
var TEMPLATE_DIR = path.join(__dirname, '..', 'templates');
var DEFAULT_TEMPLATE = 'api-server';
var debug = require('debug')('workspace');
var loopback = require('loopback');

/**
* Groups related LoopBack applications.
Expand All @@ -35,6 +36,10 @@ Workspace.getAvailableTemplates = function(cb) {
fs.readdir(TEMPLATE_DIR, cb);
}

loopback.remoteMethod(Workspace.getAvailableTemplates, {
http: {verb: 'get', path: '/component-templates'}
});

Workspace.addComponent = function(options, cb) {
var template;
var templateName = options.template || DEFAULT_TEMPLATE;
Expand Down Expand Up @@ -149,6 +154,11 @@ Workspace.addComponent = function(options, cb) {
async.parallel(steps, cb);
}

loopback.remoteMethod(Workspace.addComponent, {
http: {verb: 'post', path: '/component'},
accepts: {arg: 'options', type: 'object', http: {source: 'body'}}
});

/**
* In the attached `dataSource`, create a set of app definitions and
* corresponding workspace entities using the given template.
Expand All @@ -166,6 +176,15 @@ Workspace.createFromTemplate = function(templateName, name, cb) {
}, cb);
}

loopback.remoteMethod(Workspace.createFromTemplate, {
http: {verb: 'post', path: '/'},
accepts: [{
arg: 'templateName', type: 'string', http: {source: 'body'}
}, {
arg: 'name', type: 'string', http: {source: 'body'}
}]
});

/**
* @typedef {{name, description,supportedByStrongLoop}} ConnectorMeta
*/
Expand All @@ -183,3 +202,8 @@ var staticConnectorList = require('../available-connectors');
Workspace.listAvailableConnectors = function(cb) {
cb(null, staticConnectorList);
};

loopback.remoteMethod(Workspace.listAvailableConnectors, {
http: {verb: 'get', path: '/connectors'},
returns: {arg: 'connectors', type: 'array', root: true}
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"underscore.string": "~2.3.3",
"grunt": "~0.4.5",
"grunt-loopback-angular": "~1.1.0",
"ncp": "^0.5.1"
"ncp": "^0.5.1",
"supertest": "~0.13.0"
},
"optionalDependencies": {
"loopback-explorer": "~1.1.0"
Expand Down
Loading