Comparing version 0.1.7 to 0.2.0
@@ -40,2 +40,3 @@ /* | ||
var tree = require( 'tree-kit' ) ; | ||
var ErrorStatus = require( 'error-status' ) ; | ||
@@ -74,2 +75,16 @@ | ||
driver.getIndexes = wrapper.bind( driver , Mongodb.getIndexes ) ; | ||
driver.dropIndex = wrapper.bind( driver , Mongodb.dropIndex ) ; | ||
driver.buildIndex = wrapper.bind( driver , Mongodb.buildIndex ) ; | ||
driver.get = wrapper.bind( driver , Mongodb.get ) ; | ||
driver.getUnique = wrapper.bind( driver , Mongodb.getUnique ) ; | ||
driver.create = wrapper.bind( driver , Mongodb.create ) ; | ||
driver.overwrite = wrapper.bind( driver , Mongodb.overwrite ) ; | ||
driver.update = wrapper.bind( driver , Mongodb.update ) ; | ||
driver.patch = wrapper.bind( driver , Mongodb.patch ) ; | ||
driver.delete = wrapper.bind( driver , Mongodb.delete ) ; | ||
driver.collect = wrapper.bind( driver , Mongodb.collect ) ; | ||
driver.find = wrapper.bind( driver , Mongodb.find ) ; | ||
return driver ; | ||
@@ -208,8 +223,55 @@ } | ||
// The main goal of the callback is mainly to rewrite errors | ||
function callbackWrapper( callback , error ) | ||
{ | ||
var args ; | ||
if ( ! error ) | ||
{ | ||
callback.apply( undefined , Array.prototype.slice.call( arguments , 1 ) ) ; | ||
return ; | ||
} | ||
switch ( error.code ) | ||
{ | ||
case 11000 : | ||
// Duplicate key | ||
arguments[ 1 ] = ErrorStatus.conflict( { message: "Duplicate" } ) ; | ||
break ; | ||
} | ||
callback.apply( undefined , Array.prototype.slice.call( arguments , 1 ) ) ; | ||
} | ||
// This is a small wrapper for specific function, to avoid having to boilerplate the same things again and again... | ||
function wrapper( method ) | ||
{ | ||
var callback ; | ||
if ( arguments.length > 1 ) | ||
{ | ||
// Replace the callback by the callback wrapper | ||
callback = arguments[ arguments.length - 1 ] = callbackWrapper.bind( this , arguments[ arguments.length - 1 ] ) ; | ||
} | ||
// If not ready, try to connect first, then retry | ||
if ( ! this.ready ) { return this.connect( Mongodb.prototype.wrapper.bind.apply( this , arguments ) , callback ) ; } | ||
// Call the specific method | ||
method.apply( this , Array.prototype.slice.call( arguments , 1 ) ) ; | ||
} ; | ||
/* Requests */ | ||
// Get *USER* indexes (not returning indexes on _id) | ||
Mongodb.prototype.getIndexes = function getIndexes( callback ) | ||
Mongodb.getIndexes = function getIndexes( callback ) | ||
{ | ||
// If not ready, try to connect | ||
if ( ! this.ready ) { return this.connect( Mongodb.prototype.getIndexes.bind( this , callback ) , callback ) ; } | ||
//console.log( mongodb.Collection.prototype ) ; | ||
@@ -275,7 +337,4 @@ | ||
// Drop an index of a collection | ||
Mongodb.prototype.dropIndex = function dropIndex( indexName , callback ) | ||
Mongodb.dropIndex = function dropIndex( indexName , callback ) | ||
{ | ||
// If not ready, try to connect | ||
if ( ! this.ready ) { return this.connect( Mongodb.prototype.dropIndex.bind( this , indexName , callback ) , callback ) ; } | ||
//console.log( "DRIVER: dropIndex():" , indexName ) ; | ||
@@ -289,7 +348,4 @@ | ||
// Index/re-index a collection | ||
Mongodb.prototype.buildIndex = function buildIndex( index , callback ) | ||
Mongodb.buildIndex = function buildIndex( index , callback ) | ||
{ | ||
// If not ready, try to connect | ||
if ( ! this.ready ) { return this.connect( Mongodb.prototype.buildIndex.bind( this , index , callback ) , callback ) ; } | ||
var options = tree.extend( null , { | ||
@@ -311,7 +367,4 @@ name: index.name , | ||
// Get a document by ID | ||
Mongodb.prototype.get = function get( id , callback ) | ||
Mongodb.get = function get( id , callback ) | ||
{ | ||
// If not ready, try to connect | ||
if ( ! this.ready ) { return this.connect( Mongodb.prototype.get.bind( this , id , callback ) , callback ) ; } | ||
if ( typeof id === 'string' ) { id = mongodb.ObjectID( id ) ; } | ||
@@ -324,7 +377,4 @@ this.mongoCollection.findOne( { _id : id } , callback ) ; | ||
// Get a document by a unique fingerprint | ||
Mongodb.prototype.getUnique = function getUnique( fingerprint , callback ) | ||
Mongodb.getUnique = function getUnique( fingerprint , callback ) | ||
{ | ||
// If not ready, try to connect | ||
if ( ! this.ready ) { return this.connect( Mongodb.prototype.getUnique.bind( this , fingerprint , callback ) , callback ) ; } | ||
if ( typeof fingerprint._id === 'string' ) { fingerprint._id = mongodb.ObjectID( fingerprint._id ) ; } | ||
@@ -337,7 +387,4 @@ this.mongoCollection.findOne( fingerprint , callback ) ; | ||
// Create (insert) a new document | ||
Mongodb.prototype.create = function create( rawDocument , callback ) | ||
Mongodb.create = function create( rawDocument , callback ) | ||
{ | ||
// If not ready, try to connect | ||
if ( ! this.ready ) { return this.connect( Mongodb.prototype.create.bind( this , rawDocument , callback ) , callback ) ; } | ||
if ( typeof rawDocument._id === 'string' ) { rawDocument._id = mongodb.ObjectID( rawDocument._id ) ; } | ||
@@ -350,7 +397,4 @@ this.mongoCollection.insert( rawDocument , callback ) ; | ||
// Overwrite a document: create if it does not exist or full update if it exists | ||
Mongodb.prototype.overwrite = function overwrite( rawDocument , callback ) | ||
Mongodb.overwrite = function overwrite( rawDocument , callback ) | ||
{ | ||
// If not ready, try to connect | ||
if ( ! this.ready ) { return this.connect( Mongodb.prototype.overwrite.bind( this , rawDocument , callback ) , callback ) ; } | ||
if ( typeof rawDocument._id === 'string' ) { rawDocument._id = mongodb.ObjectID( rawDocument._id ) ; } | ||
@@ -366,7 +410,4 @@ | ||
// Full update of a document | ||
Mongodb.prototype.update = function update( id , rawDocument , callback ) | ||
Mongodb.update = function update( id , rawDocument , callback ) | ||
{ | ||
// If not ready, try to connect | ||
if ( ! this.ready ) { return this.connect( Mongodb.prototype.update.bind( this , id , rawDocument , callback ) , callback ) ; } | ||
// Should not be updated | ||
@@ -382,7 +423,4 @@ delete rawDocument._id ; | ||
// Partial update (patch) of a document | ||
Mongodb.prototype.patch = function patch( id , rawDocument , callback ) | ||
Mongodb.patch = function patch( id , rawDocument , callback ) | ||
{ | ||
// If not ready, try to connect | ||
if ( ! this.ready ) { return this.connect( Mongodb.prototype.patch.bind( this , id , rawDocument , callback ) , callback ) ; } | ||
// Should not be updated | ||
@@ -398,7 +436,4 @@ delete rawDocument._id ; | ||
// Delete a document | ||
Mongodb.prototype.delete = function delete_( id , callback ) | ||
Mongodb.delete = function delete_( id , callback ) | ||
{ | ||
// If not ready, try to connect | ||
if ( ! this.ready ) { return this.connect( Mongodb.prototype.delete.bind( this , id , callback ) , callback ) ; } | ||
if ( typeof id === 'string' ) { id = mongodb.ObjectID( id ) ; } | ||
@@ -411,7 +446,4 @@ this.mongoCollection.remove( { _id: id } , { justOne: true } , callback ) ; | ||
// Get a batch of documents given some fingerprint | ||
Mongodb.prototype.collect = function collect( fingerprint , callback ) | ||
Mongodb.collect = function collect( fingerprint , callback ) | ||
{ | ||
// If not ready, try to connect | ||
if ( ! this.ready ) { return this.connect( Mongodb.prototype.collect.bind( this , fingerprint , callback ) , callback ) ; } | ||
//console.log( 'driver collect fingerprint' , fingerprint ) ; | ||
@@ -425,7 +457,4 @@ if ( typeof fingerprint._id === 'string' ) { fingerprint._id = mongodb.ObjectID( fingerprint._id ) ; } | ||
// Get a batch of documents given a query object | ||
Mongodb.prototype.find = function find( queryObject , callback ) | ||
Mongodb.find = function find( queryObject , callback ) | ||
{ | ||
// If not ready, try to connect | ||
if ( ! this.ready ) { return this.connect( Mongodb.prototype.find.bind( this , queryObject , callback ) , callback ) ; } | ||
// In others driver, the queryObject should be processed to construct a complex query. | ||
@@ -432,0 +461,0 @@ // But since roots-DB queries ARE MongoDB object's queries, there is nothing to do here. |
{ | ||
"name": "roots-db", | ||
"version": "0.1.7", | ||
"version": "0.2.0", | ||
"description": "Even more minimalistic ODM", | ||
@@ -5,0 +5,0 @@ "main": "lib/rootsDb.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2485
151254