From 30247e71edfd0433b8c7183333e7f1613d191d44 Mon Sep 17 00:00:00 2001 From: Ritchie Date: Thu, 20 Jun 2013 10:17:55 -0700 Subject: [PATCH 1/4] Doc updates --- README.md | 17 +++++++++++++++++ lib/application.js | 1 + 2 files changed, 18 insertions(+) diff --git a/README.md b/README.md index 9e563206..1d7758c5 100644 --- a/README.md +++ b/README.md @@ -492,6 +492,23 @@ Synchronously Discover a set of models based on tables or collections in a data var models = oracle.discoverAndBuildModelsSync('MYORG'); var ProductModel = models.Product; + +#### dataSource.discoverModelDefinitions([owner], fn) + +Discover a set of model definitions based on tables or collections in a data source. + + oracle.discoverModelDefinitions(null, function (err, models) { + models.forEach(function (def) { + // def.name ~ the model name + oracle.discoverSchema(null, def.name, function (err, schema) { + console.log(schema); + }); + }); + }); + +#### dataSource.discoverSchema([owner], name, fn) + +Discover the schema of a data source. #### dataSource.defineOperation(name, options, fn) diff --git a/lib/application.js b/lib/application.js index 8b2b8131..5094ae74 100644 --- a/lib/application.js +++ b/lib/application.js @@ -61,6 +61,7 @@ app._models = []; app.model = function (Model) { this._models.push(Model); + Model.shared = true; Model.app = this; if(Model._remoteHooks) { Model._remoteHooks.emit('attached', app); From db7a86321b7273dc2e0fd3c25d1c16cb4c9dddfe Mon Sep 17 00:00:00 2001 From: Ritchie Date: Thu, 20 Jun 2013 14:17:38 -0700 Subject: [PATCH 2/4] Remove discover sugar apis --- README.md | 187 ++++++++++++++++++++++++--------------- test/data-source.test.js | 47 ---------- 2 files changed, 118 insertions(+), 116 deletions(-) diff --git a/README.md b/README.md index e47598cd..03fd7128 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,7 @@ Validate the model instance. Attach a model to a [DataSource](#data-source). Attaching a [DataSource](#data-source) updates the model with additional methods and behaviors. var oracle = asteroid.createDataSource({ - connector: require('asteroid-oracle'), + connector: require('asteroid-connector-oracle'), host: '111.22.333.44', database: 'MYDB', username: 'username', @@ -224,6 +224,8 @@ Update when record with id=data.id found, insert otherwise. **Note:** no setters Define a static model method. + + User.login = function (username, password, fn) { var passwordHash = hashPassword(password); this.findOne({username: username}, function (err, user) { @@ -243,7 +245,7 @@ Define a static model method. }); } -Setup the static model method to be exposed to clients as a [remote method](#remote-method). +Setup the static model method to be exposed to clients as a [remote method](#remote-method). asteroid.remoteMethod( User.login, @@ -303,7 +305,7 @@ Define a remote model instance method. #### Remote Methods -Both instance and static methods can be exposed to clients. A remote method must accept a callback with the conventional `fn(err, result, ...)` signature. +Both instance and static methods can be exposed to clients. A remote method must accept a callback with the conventional `fn(err, result, ...)` signature. ##### asteroid.remoteMethod(fn, [options]); @@ -439,25 +441,29 @@ Define a "one to many" relationship. Query and create the related models. Book.create(function(err, book) { - // using 'chapters' scope for build: - var c = book.chapters.build({name: 'Chapter 1'}); + // create a chapter instance + // ready to be saved in the data source + var chapter = book.chapters.build({name: 'Chapter 1'}); - // same as: - c = new Chapter({name: 'Chapter 1', bookId: book.id}); + // save the new chapter + chapter.save(); - // using 'chapters' scope for create: - book.chapters.create(); - - // same as: - Chapter.create({bookId: book.id}); + // you can also call the Chapter.create method with + // the `chapters` property which will build a chapter + // instance and save the it in the data source + book.chapters.create({name: 'Chapter 2'}, function(err, savedChapter) { + // this callback is optional + }); - // using scope for querying: + // query chapters for the book using the book.chapters(function(err, chapters) { - /* all chapters with bookId = book.id */ + // all chapters with bookId = book.id + console.log(chapters); }); book.chapters({where: {name: 'test'}, function(err, chapters) { // all chapters with bookId = book.id and name = 'test' + console.log(chapters); }); }); @@ -468,7 +474,6 @@ TODO: implement / document #### Shared Methods Any static or instance method can be decorated as `shared`. These methods are exposed over the provided transport (eg. [asteroid.rest](#rest)). - ### Data Source @@ -490,28 +495,11 @@ Define a model and attach it to a `DataSource`. var Color = oracle.createModel('color', {name: String}); -#### dataSource.discoverAndBuildModels(owner, tableOrView, options, fn) +#### dataSource.discoverModelDefinitions([username], fn) -Discover a set of models based on tables or collections in a data source. - - oracle.discoverAndBuildModels('MYORG', function(err, models) { - var ProductModel = models.Product; - }); - -**Note:** The `models` will contain all properties and options discovered from the data source. It will also automatically discover and create relationships. - -#### dataSource.discoverAndBuildModelsSync(owner, tableOrView, options) +Discover a set of model definitions (table or collection names) based on tables or collections in a data source. -Synchronously Discover a set of models based on tables or collections in a data source. - - var models = oracle.discoverAndBuildModelsSync('MYORG'); - var ProductModel = models.Product; - -#### dataSource.discoverModelDefinitions([owner], fn) - -Discover a set of model definitions based on tables or collections in a data source. - - oracle.discoverModelDefinitions(null, function (err, models) { + oracle.discoverModelDefinitions(function (err, models) { models.forEach(function (def) { // def.name ~ the model name oracle.discoverSchema(null, def.name, function (err, schema) { @@ -522,39 +510,100 @@ Discover a set of model definitions based on tables or collections in a data sou #### dataSource.discoverSchema([owner], name, fn) -Discover the schema of a data source. +Discover the schema of a specific table or collection. -#### dataSource.defineOperation(name, options, fn) +**Example schema from oracle connector:** -Define a new operation available to all model's attached to the data source. - - var maps = asteroid.createDataSource({ - connector: require('asteroid-rest'), - url: 'http://api.googleapis.com/maps/api' - }); - - rest.defineOperation('geocode', { - url: '/geocode/json', - verb: 'get', - accepts: [ - {arg: 'address', type: 'string'}, - {arg: 'sensor', default: 'true'} - ], - returns: {arg: 'location', type: asteroid.GeoPoint, transform: transform}, - json: true, - enableRemote: true - }); - - function transform(res) { - var geo = res.body.results[0].geometry; - return new asteroid.GeoPoint({lat: geo.lat, long: geo.lng}); + { + "name": "Product", + "options": { + "idInjection": false, + "oracle": { + "schema": "BLACKPOOL", + "table": "PRODUCT" + } + }, + "properties": { + "id": { + "type": "String", + "required": true, + "length": 20, + "id": 1, + "oracle": { + "columnName": "ID", + "dataType": "VARCHAR2", + "dataLength": 20, + "nullable": "N" + } + }, + "name": { + "type": "String", + "required": false, + "length": 64, + "oracle": { + "columnName": "NAME", + "dataType": "VARCHAR2", + "dataLength": 64, + "nullable": "Y" + } + }, + "audibleRange": { + "type": "Number", + "required": false, + "length": 22, + "oracle": { + "columnName": "AUDIBLE_RANGE", + "dataType": "NUMBER", + "dataLength": 22, + "nullable": "Y" + } + }, + "effectiveRange": { + "type": "Number", + "required": false, + "length": 22, + "oracle": { + "columnName": "EFFECTIVE_RANGE", + "dataType": "NUMBER", + "dataLength": 22, + "nullable": "Y" + } + }, + "rounds": { + "type": "Number", + "required": false, + "length": 22, + "oracle": { + "columnName": "ROUNDS", + "dataType": "NUMBER", + "dataLength": 22, + "nullable": "Y" + } + }, + "extras": { + "type": "String", + "required": false, + "length": 64, + "oracle": { + "columnName": "EXTRAS", + "dataType": "VARCHAR2", + "dataLength": 64, + "nullable": "Y" + } + }, + "fireModes": { + "type": "String", + "required": false, + "length": 64, + "oracle": { + "columnName": "FIRE_MODES", + "dataType": "VARCHAR2", + "dataLength": 64, + "nullable": "Y" + } + } + } } - - var GeoCoder = rest.createModel('geocoder'); - - GeoCoder.geocode('123 fake street', function(err, point) { - console.log(point.lat, point.long); // 24.224424 44.444445 - }); #### dataSource.enableRemote(operation) @@ -568,7 +617,7 @@ Disable remote access to a data source operation. Each [connector](#connector) h // all rest data source operations are // disabled by default var oracle = asteroid.createDataSource({ - connector: require('asteroid-oracle'), + connector: require('asteroid-connector-oracle'), host: '...', ... }); @@ -634,11 +683,11 @@ Include the connector in your package.json dependencies and run `npm install`. { "dependencies": { - "asteroid-oracle": "latest" + "asteroid-connector-oracle": "latest" } } -### GeoPoint +### GeoPoint TODO Embed a latitude / longitude point in a [Model](#model). @@ -696,7 +745,7 @@ Various APIs in Asteroid accept type descriptions (eg. [remote methods](#remote- - `Array` - JSON array - `Date` - a JavaScript date object - `Buffer` - a node.js Buffer object - - [GeoPoint](#geopoint) - an asteroid GeoPoint object. + - [GeoPoint](#geopoint) - an asteroid GeoPoint object. TODO ### REST Router diff --git a/test/data-source.test.js b/test/data-source.test.js index ae2684c5..bef7bfea 100644 --- a/test/data-source.test.js +++ b/test/data-source.test.js @@ -34,53 +34,6 @@ describe('DataSource', function() { }); }); - // describe('dataSource.discover(options, fn)', function() { - // it("Discover an object containing properties and settings for an existing data source.", function(done) { - // /* example - - // oracle.discover({owner: 'MYORG'}, function(err, tables) { - // var productSchema = tables.PRODUCTS; - // var ProductModel = oracle.createModel('product', productSchema.properties, productSchema.settings); - // }); - // - // */ - // done(new Error('test not implemented')); - // }); - // }); - // - // describe('dataSource.discoverSync(options)', function() { - // it("Synchronously discover an object containing properties and settings for an existing data source tables or collections.", function(done) { - // /* example - - // var tables = oracle.discover({owner: 'MYORG'}); - // var productSchema = tables.PRODUCTS; - // var ProductModel = oracle.createModel('product', productSchema.properties, productSchema.settings); - // - // */ - // done(new Error('test not implemented')); - // }); - // }); - - // describe('dataSource.discoverModels(options, fn) ', function() { - // it("Discover a set of models based on tables or collections in a data source.", function(done) { - // /* example - - // oracle.discoverModels({owner: 'MYORG'}, function(err, models) { - // var ProductModel = models.Product; - // }); - // - // */ - // done(new Error('test not implemented')); - // }); - // }); - // - // describe('dataSource.discoverModelsSync(options)', function() { - // it("Synchronously Discover a set of models based on tables or collections in a data source.", function(done) { - // /* example - - // var models = oracle.discoverModels({owner: 'MYORG'}); - // var ProductModel = models.Product; - // */ - // done(new Error('test not implemented')); - // }); - // }); - describe('dataSource.operations()', function() { it("List the enabled and disabled operations.", function() { // assert the defaults From 9be5123d9eaa34d27dcb8257aec929043c9c15d1 Mon Sep 17 00:00:00 2001 From: Ritchie Date: Thu, 20 Jun 2013 17:31:23 -0700 Subject: [PATCH 3/4] Update doc example for findOne() --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 03fd7128..49ea4b17 100644 --- a/README.md +++ b/README.md @@ -208,11 +208,11 @@ Find instance by id. console.info(user.id); // 23 }); -##### Model.findOne(filter, callback) +##### Model.findOne(where, callback) -Find a single instance that matches the given filter. +Find a single instance that matches the given where expression. - User.find(23, function(err, user) { + User.findOne({id: 23}, function(err, user) { console.info(user.id); // 23 }); From 17015eef9b30507f11eda531f7e1ed236f7095e8 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 20 Jun 2013 17:53:25 -0700 Subject: [PATCH 4/4] Rename oracle connector --- example/simple-data-source/app-oracle.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/example/simple-data-source/app-oracle.js b/example/simple-data-source/app-oracle.js index d9cb4040..6f1beb66 100644 --- a/example/simple-data-source/app-oracle.js +++ b/example/simple-data-source/app-oracle.js @@ -5,7 +5,7 @@ app.use(asteroid.rest()); var dataSource = app.dataSource('db', - { adapter: 'oracle', + { adapter: require('asteroid-connector-oracle'), host: '166.78.158.45', database: 'XE', username: 'strongloop', @@ -29,4 +29,4 @@ dataSource.autoupdate(function (err, data) { app.listen(3000); -console.log('a list of colors is available at http://localhost:300/colors'); \ No newline at end of file +console.log('a list of colors is available at http://localhost:300/colors'); diff --git a/package.json b/package.json index 23b3999f..812e60a0 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,6 @@ "supertest": "latest" }, "optionalDependencies": { - "jugglingdb-oracle": "git+ssh://git@github.com:strongloop/jugglingdb-oracle.git" + "asteroid-connector-oracle": "git+ssh://git@github.com:strongloop/asteroid-connector-oracle.git" } }