From cba94a23fca5c64daa76dea4fd2779e24a3204b5 Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Tue, 16 Jul 2013 13:39:03 -0700 Subject: [PATCH] Add memory docs and test --- README.md | 122 +++++++++++++++++++++++++++++++++++++++----- test/memory.test.js | 35 +++++++++++++ 2 files changed, 144 insertions(+), 13 deletions(-) create mode 100644 test/memory.test.js diff --git a/README.md b/README.md index c8c3c781..9f43c8d0 100644 --- a/README.md +++ b/README.md @@ -205,10 +205,56 @@ Attach a model to a [DataSource](#data-source). Attaching a [DataSource](#data-s #### CRUD and Query Mixins -Mixins are added by attaching a vanilla model to a data source with a connector. Each [connector](#connectors) enables its own set of operations that are attached to a `Model` as methods. To see available methods for a data source with a connector call `dataSource.operations()`. +Mixins are added by attaching a vanilla model to a [data source](#data-source) with a [connector](#connectors). Each [connector](#connectors) enables its own set of operations that are mixed into a `Model` as methods. To see available methods for a data source call `dataSource.operations()`. + +Log the available methods for a memory data source. + + var ops = loopback + .createDataSource({connector: loopback.Memory}) + .operations(); + + console.log(Object.keys(ops)); + +Outputs: + + [ 'create', + 'updateOrCreate', + 'upsert', + 'findOrCreate', + 'exists', + 'findById', + 'find', + 'all', + 'findOne', + 'destroyAll', + 'deleteAll', + 'count', + 'include', + 'relationNameFor', + 'hasMany', + 'belongsTo', + 'hasAndBelongsToMany', + 'save', + 'isNewRecord', + 'destroy', + 'delete', + 'updateAttribute', + 'updateAttributes', + 'reload' ] + +Here is the definition of the `count()` operation. + + { + accepts: [ { arg: 'where', type: 'object' } ], + http: { verb: 'get', path: '/count' }, + remoteEnabled: true, + name: 'count' + } #### Static Methods +**Note:** These are the default mixin methods for a `Model` attached to a data source. See the specific connector for additional API documentation. + ##### Model.create(data, [callback]) Create an instance of Model with given data and save to the attached data source. Callback is optional. @@ -310,6 +356,8 @@ Setup the static model method to be exposed to clients as a [remote method](#rem #### Instance Methods +**Note:** These are the default mixin methods for a `Model` attached to a data source. See the specific connector for additional API documentation. + ##### model.save([options], [callback]) Save an instance of a Model to the attached data source. @@ -361,13 +409,18 @@ Both instance and static methods can be exposed to clients. A remote method must Expose a remote method. Product.stats = function(fn) { - myApi.getStats('products', fn); + var calc = require('./stats'); + + Product.find(function(err, products) { + var productStats = calc(products); + fn(null, productStats); + }); } loopback.remoteMethod( Product.stats, { - returns: {arg: 'stats', type: 'array'}, + returns: {arg: 'stats', type: 'object'}, http: {path: '/info', verb: 'get'} } ); @@ -715,16 +768,16 @@ Create a data source with a specific connector. See **available connectors** for **Available Connectors** - - [Oracle](http://github.com/strongloop/loopback-connectors/oracle) - - [In Memory](http://github.com/strongloop/loopback-connectors/memory) - - TODO - [REST](http://github.com/strongloop/loopback-connectors/rest) - - TODO - [MySQL](http://github.com/strongloop/loopback-connectors/mysql) - - TODO - [SQLite3](http://github.com/strongloop/loopback-connectors/sqlite) - - TODO - [Postgres](http://github.com/strongloop/loopback-connectors/postgres) - - TODO - [Redis](http://github.com/strongloop/loopback-connectors/redis) - - TODO - [MongoDB](http://github.com/strongloop/loopback-connectors/mongo) - - TODO - [CouchDB](http://github.com/strongloop/loopback-connectors/couch) - - TODO - [Firebird](http://github.com/strongloop/loopback-connectors/firebird) + - [In Memory](#memory-connector) + - [REST](http://github.com/strongloop/loopback-connector-rest) + - [Oracle](http://github.com/strongloop/loopback-connector-oracle) + - [MongoDB](http://github.com/strongloop/loopback-connector-mongodb) + - TODO - [MySQL](http://github.com/strongloop/loopback-connector-mysql) + - TODO - [SQLite3](http://github.com/strongloop/loopback-connector-sqlite) + - TODO - [Postgres](http://github.com/strongloop/loopback-connector-postgres) + - TODO - [Redis](http://github.com/strongloop/loopback-connector-redis) + - TODO - [CouchDB](http://github.com/strongloop/loopback-connector-couch) + - TODO - [Firebird](http://github.com/strongloop/loopback-connector-firebird) **Installing Connectors** @@ -736,6 +789,49 @@ Include the connector in your package.json dependencies and run `npm install`. } } +##### Memory Connector + +The built-in memory connector allows you to test your application without connecting to an actual persistent data source, such as a database. Although the memory connector is very well tested it is not recommended to be used in production. Creating a data source using the memory connector is very simple. + + // use the built in memory function + // to create a memory data source + var memory = loopback.memory(); + + // or create it using the standard + // data source creation api + var memory = loopback.createDataSource({ + connector: loopback.Memory + }); + + // create a model using the + // memory data source + var properties = { + name: String, + price: Number + }; + + var Product = memory.createModel('product', properties); + + Product.create([ + {name: 'apple', price: 0.79}, + {name: 'pear', price: 1.29}, + {name: 'orange', price: 0.59}, + ], count); + + function count() { + Product.count(console.log); // 3 + } + +###### Operations + +**CRUD / Query** + +The memory connector supports all the standard [query and crud operations](#crud-and-query-mixins) to allow you to test your models against an in memory data source. + +**GeoPoint Filtering** + +The memory connector also supports geo-filtering when using the `find()` operation with an attached model. See [GeoPoint](#geopoint) for more information on geo-filtering. + ### GeoPoint Use the `GeoPoint` class. diff --git a/test/memory.test.js b/test/memory.test.js new file mode 100644 index 00000000..88a3d176 --- /dev/null +++ b/test/memory.test.js @@ -0,0 +1,35 @@ +describe('Memory Connector', function(){ + it('Create a model using the memory connector', function(done) { + // use the built in memory function + // to create a memory data source + var memory = loopback.memory(); + + // or create it using the standard + // data source creation api + var memory = loopback.createDataSource({ + connector: loopback.Memory + }); + + // create a model using the + // memory data source + var properties = { + name: String, + price: Number + }; + + var Product = memory.createModel('product', properties); + + Product.create([ + {name: 'apple', price: 0.79}, + {name: 'pear', price: 1.29}, + {name: 'orange', price: 0.59}, + ], count); + + function count() { + Product.count(function (err, count) { + assert.equal(count, 3); + done(); + }); + } + }); +}); \ No newline at end of file