From 1e1a227f869bb2c66a39246b9a8409252ddc00af Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Fri, 7 Jun 2013 17:37:30 -0700 Subject: [PATCH] Adding new data source --- README.md | 2 +- index.js | 14 ++++++++++- lib/application.js | 13 ---------- lib/asteroid.js | 24 ++++++++++++++++-- lib/connector.js | 54 ++++++++++++++++++++++++++++++++++++++++ test/asteroid.test.js | 7 +++++- test/data-source.test.js | 2 +- 7 files changed, 97 insertions(+), 19 deletions(-) create mode 100644 lib/connector.js diff --git a/README.md b/README.md index 35edf2bd..89971c0d 100644 --- a/README.md +++ b/README.md @@ -467,7 +467,7 @@ Define a data source for persisting models. password: 'password' }); -#### dataSource.createModel(name, options, settings) +#### dataSource.createModel(name, properties, settings) Define a model and attach it to a `DataSource`. diff --git a/index.js b/index.js index b465aeec..016e0d07 100644 --- a/index.js +++ b/index.js @@ -2,4 +2,16 @@ * asteroid ~ public api */ -module.exports = require('./lib/asteroid'); \ No newline at end of file +var asteroid = module.exports = require('./lib/asteroid'); + +/** + * Connector + */ + +asteroid.Connector = require('./lib/connector'); + +/** + * JugglingDB Connector + */ + +asteroid.JdbConnector = require('./lib/jdb-connector'); \ No newline at end of file diff --git a/lib/application.js b/lib/application.js index 9e0f108f..b44e30f4 100644 --- a/lib/application.js +++ b/lib/application.js @@ -93,17 +93,4 @@ app.remoteObjects = function () { app.remotes = function () { return this._remotes || (this._remotes = RemoteObjects.create()); -} - -/** - * Attach a remote data source. - * - * @param name {String} - * @param options {Object} - * @returns {DataSource} - */ - -app.dataSource = function (name, options) { - var dataSources = this.dataSources || (this.dataSources = {}); - return (dataSources[name] = new DataSource(options.adapter, options)); } \ No newline at end of file diff --git a/lib/asteroid.js b/lib/asteroid.js index c2e42b11..edba9bb5 100644 --- a/lib/asteroid.js +++ b/lib/asteroid.js @@ -6,7 +6,9 @@ var express = require('express') , fs = require('fs') , path = require('path') , proto = require('./application') - , utils = require('express/node_modules/connect').utils; + , utils = require('express/node_modules/connect').utils + , DataSource = require('jugglingdb').DataSource + , ModelBuilder = require('jugglingdb').ModelBuilder; /** * Expose `createApplication()`. @@ -66,4 +68,22 @@ fs.readdirSync(path.join(__dirname, 'middleware')).forEach(function (m) { * Error handler title */ -asteroid.errorHandler.title = 'Asteroid'; \ No newline at end of file +asteroid.errorHandler.title = 'Asteroid'; + +/** + * Create a data source with passing the provided options to the connector. + */ + +asteroid.createDataSource = function (options) { + var connector = options.connector; + var jdbAdapter = connector.jdbAdapter; + + if(jdbAdapter) { + // TODO remove jdb dependency + delete options.connector; + return new DataSource(jdbAdapter, options); + } else { + // TODO implement asteroid data source + throw Error('unsupported adapter') + } +} \ No newline at end of file diff --git a/lib/connector.js b/lib/connector.js new file mode 100644 index 00000000..cf224629 --- /dev/null +++ b/lib/connector.js @@ -0,0 +1,54 @@ +/** + * Expose `Connector`. + */ + +module.exports = Connector; + +/** + * Module dependencies. + */ + +var EventEmitter = require('events').EventEmitter + , debug = require('debug')('connector') + , util = require('util') + , inherits = util.inherits + , assert = require('assert'); + +/** + * Create a new `Connector` with the given `options`. + * + * @param {Object} options + * @return {Connector} + */ + +function Connector(options) { + EventEmitter.apply(this, arguments); + this.options = options; + + debug('created with options', options); +} + +/** + * Inherit from `EventEmitter`. + */ + +inherits(Connector, EventEmitter); + +/*! + * Create an adapter instance from a JugglingDB adapter. + */ + +Connector._createJDBAdapter = function (jdbModule) { + var fauxSchema = {}; + jdbModule.initialize(fauxSchema, function () { + // connected + }); +} + +/*! + * Add default crud operations from a JugglingDB adapter. + */ + +Connector.prototype._addCrudOperationsFromJDBAdapter = function (adapter) { + +} \ No newline at end of file diff --git a/test/asteroid.test.js b/test/asteroid.test.js index d9d1625f..404be6b0 100644 --- a/test/asteroid.test.js +++ b/test/asteroid.test.js @@ -1,5 +1,10 @@ describe('asteroid', function() { - + describe('asteroid.createDataSource(options)', function(){ + it('Create a data sources with a connector.', function(done) { + done(new Error('not implemented')); + }); + }); + describe('asteroid.remoteMethod(Model, fn, [options]);', function() { it("Expose a remote method.", function(done) { /* example - diff --git a/test/data-source.test.js b/test/data-source.test.js index b93a7c3f..1f4ec10c 100644 --- a/test/data-source.test.js +++ b/test/data-source.test.js @@ -1,6 +1,6 @@ describe('DataSource', function() { - describe('dataSource.createModel(name, options, settings)', function() { + describe('dataSource.createModel(name, properties, settings)', function() { it("Define a model and attach it to a `DataSource`.", function(done) { /* example - var Color = oracle.createModel('color', {name: String});