lx-mongodb
Litixsoft backend driver for mongoDb based on mongojs.
Install
Documentation
http://www.litixsoft.de/products-lxmongodb.html (german)
Examples
Simple query
var lxDb = db = lxDb repo = lxDb; // get all usersrepo; // add userrepo;
Simple repository with JSON schema
var lxDb = ; exports { var { return properties: _id: type: 'string' required: false format: 'mongo-id' key: true email: type: 'string' required: true format: 'email' userName: type: 'string' required: true sort: 1 ; } baseRepo = lxDb; return baseRepo;};
var lxDb = db = lxDb userRepo = ; // get all usersuserRepo; // create new useruserRepo;
Repository with validation
var lxDb = ; exports { var { return properties: _id: type: 'string' required: false format: 'mongo-id' key: true email: type: 'string' required: true format: 'email' userName: type: 'string' required: true sort: 1 birthdate: type: 'string' format: 'date-time' ; } baseRepo = lxDb; // validation function for the userName baseRepo { if !doc ; return; var query = userName: docuserName _id: $ne: typeof doc_id === 'string' ? baseRepo : doc_id ; baseRepo; }; // validation function baseRepo { var userNameCheck = true; // check is update if isUpdate for var schemaProp in schemaproperties if schemaproperties if !doc schemapropertiesschemaProprequired = false; if !doc userNameCheck = false; // json schema validate var valResult = val; // register async validator if userNameCheck valasyncValidate; // async validate valasyncValidate; }; return baseRepo;};
var lxDb = db = lxDb userRepo = ; // create new useruserRepo; // JSON schema validation of the useruserRepo; // async schema validation of the useruserRepo;
API
Database connection
Base repository
- BaseRepository
- aggregate
- createNewId
- convertId
- count
- find
- findOne
- findOneById
- getCollection
- getSchema
- getValidationOptions
- insert
- remove
- update
Gridfs base repository
Database connection #### getDb(connectionString, collections, gridFsCollections) Creates a connection to the mongoDb using a connection string. The db and the connections are stored in memory.
Note: When querying the metadata of gridFsCollections, you need to add them to the collections array. See examples.
Arguments
- connectionString
{!string}
- The connection string. - collections
{!Array.<string>}
- The names of the mongo collections. - gridFsCollections
{Array.<string>=}
- The names of the gridfs collections.
var lxDb = db = lxDb;
// with gridfs collectionsvar lxDb = db = lxDb;
// with gridfs collections and collection for querying the gridfs metadatavar lxDb = db = lxDb;
Base repository #### BaseRepository(collection, schema) Creates a new repository with the base mongoDb operations. Each mongoDb collection uses the base repository. If you need all the functions of the native mongoDb js driver, you can call `getCollection()` on the base repository to get the mongoDb collection.
Arguments
- collection
{!Object}
- The mongoDb colllection. - schema
{(Object|Function)=}
- The JSON schema. Is used to get the id field and the default sorting.
var lxDb = db = lxDb postRepo = lxDb;
--
#### aggregate(pipeline, options, callback) Execute an aggregation framework pipeline against the collection, needs MongoDB >= 2.1.Arguments
- pipeline
{!Array}
- The aggregation framework pipeline. - options
{Object=}
- The additional options. - callback
{function(err, res)}
- The callback function.
var lxDb = db = lxDb repo = lxDb pipeline = $project: age: 1 $group: _id: age: '$age' count: $sum: 1 $project: _id: '$_id' age: '$age' count: '$count' ; repo;
--
#### convertId(id) Converts a `string` to a mongo `ObjectID` and vice versa.Arguments
- id
{Object|string}
- The id to convert.
var repo = _id = repo idString = repo; typeof idString === 'string'; // true
--
#### createNewId() Returns a new mongo ObjectId.var repo = _id = repo; _id instanceof ObjectID === true; // true
--
#### count(query, options, callback) Returns the number of records.Arguments
- query
{Object|function(err, res)=}
- The query. - options
{Object|function(err, res)=}
- The options object or the callback. - callback
{!function(err, res)}
- The callback function.
var repo = ; // count allrepo; // count with queryrepo; // count all with optionsrepo;
--
#### find(query, options, callback) Gets the records from the mongoDb.Arguments
- query
{Object|function(err, res)=}
- The query. - options
{Object|function(err, res)=}
- The mongoDb query options. - options.fields
{(Array|Object)=}
- The fields which should be returned by the query. - options.limit
{Number=}
- The number of records which should be returned by the query. - options.skip
{Number=}
- The number of records which should be skipped by the query. - options.sort
{(Array|String|Object)=}
- The sorting of the returned records. - callback
{!function(err, res)}
- The callback function.
var repo = ; // find allrepo; // find and convert mongo-idrepo; // find with queryrepo; // find all with optionsrepo;
--
#### findOne(query, options, callback) Gets one record from the mongoDb.Arguments
- query
{!Object}
- The query. - options
{Object|function(err, res)=}
- The mongoDb query options or the callback. - options.fields
{(Array|Object)=}
- The fields which should be returned by the query. - options.limit
{Number=}
- The number of records which should be returned by the query. - options.skip
{Number=}
- The number of records which should be skipped by the query. - options.sort
{(Array|String|Object)=}
- The sorting of the returned records. - callback
{!function(err, res)}
- The callback function.
var repo = ; repo; repo;
--
#### findOneById(id, options, callback) Gets one record by id from the mongoDb.Arguments
- id
{!Object|string}
- The id. - options
{Object|function(err, res)}
- The mongoDb query options or the callback. - options.fields
{(Array|Object)=}
- The fields which should be returned by the query. - callback
{!function(err, res)}
- The callback function.
var repo = myId = repo; repo; repo;
--
#### getCollection() Returns the mongoDb collection object.var repo = myCollection = repo; collection == myCollection; // true
--
#### getSchema() Returns the JSON schema.var repo = mySchema = repo; schema == mySchema; // true
--
#### getValidationOptions() Returns an object with the validation options. This is especially useful in combination with [lx-valid](https://github.com/litixsoft/lx-valid). The method `convert()` can also be used by other schema validators.var repo = options = repo; optionsdeleteUnknownProperties === true; // truetypeof optionsconvert === 'function'; // true
--
#### insert(doc, options, callback) Creates one or more records in the mongoDb.Arguments
- doc
{!Object|!Array}
- The document/s. - options
{Object|function(err, res)=}
- The options object or the callback. - callback
{function(err, res)=}
- The callback function.
var repo = ; // insertrepo; // insert with callbackrepo; // insert with options and callbackrepo
--
#### remove(query, options, callback) Deletes one or more records in the mongoDb.Arguments
- query
{Object=}
- The query. - options
{Object=}
- The mongo options. - callback
{function(err, res)=}
- The callback function.
var repo = ; // remove allrepo; // remove with callback and coverting of mongo-idrepo; // remove with optionsrepo;
--
#### update(query, update, options, callback) Updates one or more records in the mongoDb.Arguments
- query
{!Object}
- The query. - update
{!Object}
- The data to update. - options
{Object=}
- The options for multi update. - callback
{!function(err, res)}
- The callback function.
var repo = ; repo; repo; repo;
GridFs base repository #### GridFsBaseRepo(collection) Creates a new repository with the base mongoDb gridfs operations.
Arguments
- collection
{!Object}
- The mongoDb gridfs colllection.
var lxDb = db = lxDb documentsRepo = lxDb;
--
#### convertId(id) Converts a `string` to a mongo `ObjectID` and vice versa.Arguments
- id
{Object|string}
- The id to convert.
var repo = _id = repo idString = repo; typeof idString === 'string'; // true
--
#### delete(id, callback) Deletes a file from the collection.Arguments
- id
{Object|string}
- The id of the file. - callback
{function(err, res)}
- The callback function.
var repo = ; repo;
--
#### get(id, callback) Gets a file from the collection.Arguments
- id
{Object|string}
- The id of the file. - callback
{function(err, res)}
- The callback function.
var repo = ; repo;
--
#### getCollection() Returns the mongoDb gridfs collection object.var repo = myCollection = repo; collection == myCollection; // true#### Returns an object with the validation options This is especially useful in combination with lx-validhttps://github.com/litixsoft/lx-valid).The method `convert()` can also be used by other schema validators ```jsvar repo = GridFsBaseRepo(collection), options = repo.getValidationOptions(); options.deleteUnknownProperties === true; // trueoptions.trim === true; // trueoptions.strictRequired === true; // truetypeof options.convert === 'function'; // true
--
#### put(data, options, callback) Inserts a file to the collection.Arguments
- data
{Object}
- The buffer with the binary data of the file. - options
{Object}
- The options for the file. - callback
{function(err, res)}
- The callback function.
var repo = ; repo;
Contributing
In lieu of a formal styleguide take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Please create descriptive commit messages. We use a git hook to validate the commit messages against these rules. Lint and test your code using grunt.
Author
License
Copyright (C) 2013-2014 Litixsoft GmbH info@litixsoft.de Licensed under the MIT license.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.