da-mongo-rx
Advanced tools
Comparing version 1.0.5 to 1.1.0
import Rx = require("rx"); | ||
/** | ||
* Every method (even write action) must be subscribed to be executed. | ||
*/ | ||
declare module mongoRx { | ||
/** | ||
* Chinable query object. | ||
*/ | ||
interface ICursor { | ||
@@ -8,5 +14,40 @@ sort(sort: any): ICursor; | ||
skip(count: number): ICursor; | ||
/** | ||
* Create stream of resulted query objects. | ||
* @return | ||
* Observable with streamed object | ||
*/ | ||
query<T>(): Rx.Observable<T>; | ||
/** | ||
* Return collection of resulted query objects at once. | ||
* @return | ||
* Collection of results | ||
*/ | ||
toArray<T>(): Rx.Observable<T[]>; | ||
} | ||
interface IBulkFind { | ||
remove(): any; | ||
removeOne(): any; | ||
replaceOne(): any; | ||
update(updaterParam: any): any; | ||
updateOne(updaterParam: any): any; | ||
upsert(upsertParam: any): any; | ||
} | ||
interface IBulkResult { | ||
writeErrors: any[]; | ||
writeConcernErrors: any[]; | ||
nInserted: number; | ||
nUpserted: number; | ||
nMatched: number; | ||
nModified: number; | ||
nRemoved: number; | ||
upserted: any[]; | ||
ok: number; | ||
} | ||
interface IBulk { | ||
execute(): Rx.Observable<any>; | ||
find(query: any): IBulkFind; | ||
insert(document: any): any; | ||
toString(): string; | ||
} | ||
interface ICommandResult { | ||
@@ -20,18 +61,78 @@ ok: boolean; | ||
} | ||
/** | ||
* Collection object. | ||
*/ | ||
class Collection { | ||
private coll; | ||
/** | ||
* Create collection object from mongojs collection. | ||
* @param coll | ||
* mongojs collection | ||
*/ | ||
constructor(coll: any); | ||
/** | ||
* Create query cursor object. | ||
* @param query | ||
* Mongo query json | ||
* @return Cursor object | ||
*/ | ||
find(query: any): ICursor; | ||
/** | ||
* Convert nodeCallback collection function to Rx.Observable object | ||
*/ | ||
private fromNode<T>(funcName); | ||
/** | ||
* Insert object into collection. | ||
* @param data | ||
* Object to insert | ||
* @return | ||
* Observable with created object | ||
*/ | ||
insert<T>(data: any): Rx.Observable<T>; | ||
/** | ||
* Remove objects by filter. | ||
* @param filter | ||
* Mongo filter json object. | ||
*/ | ||
remove(filter: any): Rx.Observable<any>; | ||
update(query: any, upd: any): Rx.Observable<any>; | ||
findAndModify(upd: any): Rx.Observable<IFindAndModifyResult>; | ||
/** | ||
* Start bulk operation | ||
*/ | ||
bulk(): IBulk; | ||
} | ||
/** | ||
* Database connection and operations | ||
*/ | ||
class MongoDb { | ||
private db; | ||
private collections; | ||
/** | ||
* Create database connection. | ||
* @param connectionString | ||
* Mongo db connection uri | ||
* @param collectionNames | ||
* Name of used collections | ||
*/ | ||
constructor(connectionString: string, collectionNames: string[]); | ||
/** | ||
* Get collection by name. | ||
* @param name | ||
* Name of the collection | ||
* @return Collection object. | ||
*/ | ||
getCollection(name: string): Collection; | ||
/** | ||
* Run some mongo command. | ||
*/ | ||
runCommand(command: any): Rx.Observable<ICommandResult>; | ||
/**Create record in collection | ||
* Returns true if record was created and not exists before | ||
* Returns false if record already existed. | ||
* params | ||
* @id Record Id. | ||
* @collName collection name. | ||
* @replicas number of replicas to write record (default 1). | ||
*/ | ||
lock(id: string, collName: string, replicas?: number): Rx.Observable<boolean>; | ||
@@ -38,0 +139,0 @@ } |
@@ -5,2 +5,5 @@ | ||
var RxNode = require("rx-node"); | ||
/** | ||
* Every method (even write action) must be subscribed to be executed. | ||
*/ | ||
var mongoRx; | ||
@@ -32,15 +35,68 @@ (function (mongoRx) { | ||
})(); | ||
var Bulk = (function () { | ||
function Bulk(coll) { | ||
this.bulk = coll.initializeOrderedBulkOp(); | ||
} | ||
/** | ||
* Convert nodeCallback collection function to Rx.Observable object | ||
*/ | ||
Bulk.prototype.fromNode = function (funcName) { | ||
return Rx.Observable.fromNodeCallback(this.bulk[funcName], this.bulk); | ||
}; | ||
Bulk.prototype.execute = function () { | ||
return this.fromNode("execute")(); | ||
}; | ||
Bulk.prototype.find = function (query) { | ||
return this.bulk.find(query); | ||
}; | ||
Bulk.prototype.insert = function (document) { | ||
return this.bulk.insert(document); | ||
}; | ||
Bulk.prototype.toString = function () { | ||
return this.bulk.toString(); | ||
}; | ||
return Bulk; | ||
})(); | ||
/** | ||
* Collection object. | ||
*/ | ||
var Collection = (function () { | ||
/** | ||
* Create collection object from mongojs collection. | ||
* @param coll | ||
* mongojs collection | ||
*/ | ||
function Collection(coll) { | ||
this.coll = coll; | ||
} | ||
/** | ||
* Create query cursor object. | ||
* @param query | ||
* Mongo query json | ||
* @return Cursor object | ||
*/ | ||
Collection.prototype.find = function (query) { | ||
return new Cursor(this.coll.find(query)); | ||
}; | ||
/** | ||
* Convert nodeCallback collection function to Rx.Observable object | ||
*/ | ||
Collection.prototype.fromNode = function (funcName) { | ||
return Rx.Observable.fromNodeCallback(this.coll[funcName], this.coll); | ||
}; | ||
/** | ||
* Insert object into collection. | ||
* @param data | ||
* Object to insert | ||
* @return | ||
* Observable with created object | ||
*/ | ||
Collection.prototype.insert = function (data) { | ||
return this.fromNode("insert")(data); | ||
}; | ||
/** | ||
* Remove objects by filter. | ||
* @param filter | ||
* Mongo filter json object. | ||
*/ | ||
Collection.prototype.remove = function (filter) { | ||
@@ -58,6 +114,22 @@ return this.fromNode("remove")(filter); | ||
}; | ||
/** | ||
* Start bulk operation | ||
*/ | ||
Collection.prototype.bulk = function () { | ||
return new Bulk(this.coll); | ||
}; | ||
return Collection; | ||
})(); | ||
mongoRx.Collection = Collection; | ||
/** | ||
* Database connection and operations | ||
*/ | ||
var MongoDb = (function () { | ||
/** | ||
* Create database connection. | ||
* @param connectionString | ||
* Mongo db connection uri | ||
* @param collectionNames | ||
* Name of used collections | ||
*/ | ||
function MongoDb(connectionString, collectionNames) { | ||
@@ -69,8 +141,25 @@ var _this = this; | ||
} | ||
/** | ||
* Get collection by name. | ||
* @param name | ||
* Name of the collection | ||
* @return Collection object. | ||
*/ | ||
MongoDb.prototype.getCollection = function (name) { | ||
return this.collections[name]; | ||
}; | ||
/** | ||
* Run some mongo command. | ||
*/ | ||
MongoDb.prototype.runCommand = function (command) { | ||
return Rx.Observable.fromNodeCallback(this.db.runCommand, this.db)(command); | ||
}; | ||
/**Create record in collection | ||
* Returns true if record was created and not exists before | ||
* Returns false if record already existed. | ||
* params | ||
* @id Record Id. | ||
* @collName collection name. | ||
* @replicas number of replicas to write record (default 1). | ||
*/ | ||
MongoDb.prototype.lock = function (id, collName, replicas) { | ||
@@ -77,0 +166,0 @@ if (replicas === void 0) { replicas = 1; } |
{ | ||
"name": "da-mongo-rx", | ||
"version": "1.0.5", | ||
"version": "1.1.0", | ||
"description": "Thin layer on top of mongojs library exposes RxJS interfaces.", | ||
@@ -10,7 +10,7 @@ "main": "dist/mongo-rx.js", | ||
"config": { | ||
"MONGO_URI": "127.0.0.1" | ||
"MONGO_URI_TEST": "127.0.0.1" | ||
}, | ||
"scripts": { | ||
"build": "gulp build", | ||
"test": "gulp test" | ||
"test": "mocha" | ||
}, | ||
@@ -17,0 +17,0 @@ "repository": { |
@@ -32,2 +32,40 @@ ///<reference path="../typings/tsd.d.ts"/> | ||
export interface IBulkFind { | ||
remove() : any | ||
removeOne() : any | ||
replaceOne() : any | ||
update(updaterParam: any) : any | ||
updateOne(updaterParam: any) : any | ||
upsert(upsertParam: any) : any | ||
} | ||
export interface IBulkResult { | ||
writeErrors: any[], | ||
writeConcernErrors: any[], | ||
nInserted: number, | ||
nUpserted: number, | ||
nMatched: number, | ||
nModified: number, | ||
nRemoved: number, | ||
upserted: any[], | ||
ok: number | ||
} | ||
export interface IBulk { | ||
execute() : Rx.Observable<any> | ||
find(query: any) : IBulkFind | ||
insert(document: any) : any | ||
toString(): string | ||
} | ||
export interface ICommandResult { | ||
@@ -72,2 +110,35 @@ ok : boolean | ||
class Bulk implements IBulk { | ||
private bulk: any; | ||
constructor(coll: any) { | ||
this.bulk = coll.initializeOrderedBulkOp(); | ||
} | ||
/** | ||
* Convert nodeCallback collection function to Rx.Observable object | ||
*/ | ||
private fromNode<T>(funcName: string) : (arg1?: any, arg2?: any, arg3?: any) => Rx.Observable<T> { | ||
return (<any>Rx.Observable).fromNodeCallback(this.bulk[funcName], this.bulk); | ||
} | ||
execute() : Rx.Observable<any> { | ||
return this.fromNode<any>("execute")(); | ||
} | ||
find(query: any) : IBulkFind { | ||
return this.bulk.find(query); | ||
} | ||
insert(document: any) : void { | ||
return this.bulk.insert(document); | ||
} | ||
toString(): string { | ||
return this.bulk.toString(); | ||
} | ||
} | ||
/** | ||
@@ -133,2 +204,9 @@ * Collection object. | ||
/** | ||
* Start bulk operation | ||
*/ | ||
bulk() : IBulk { | ||
return new Bulk(this.coll); | ||
} | ||
} | ||
@@ -194,4 +272,2 @@ | ||
} | ||
} | ||
@@ -198,0 +274,0 @@ |
@@ -8,4 +8,11 @@ /// <reference path="../typings/tsd.d.ts"/> | ||
const MONGO_URI = process.env.npm_config_MONGO_URI || process.env.npm_package_config_MONGO_URI; | ||
function getEnvVar(name: string) : string { | ||
return process.env[name] || | ||
process.env["npm_config_" + name] || | ||
process.env["npm_package_config_" + name]; | ||
} | ||
const MONGO_URI = getEnvVar("MONGO_URI_TEST"); | ||
describe("create / remove tests", () => { | ||
@@ -17,4 +24,5 @@ | ||
db = new mongoRx.MongoDb(MONGO_URI, ["create", "locker"]); | ||
coll = db.getCollection("create") | ||
coll.remove({}).concat(db.getCollection("locker").remove({})).subscribeOnCompleted(done); | ||
coll = db.getCollection("create"); | ||
coll.remove({}).concat(db.getCollection("locker").remove({})).subscribe( | ||
() => {}, done, done); | ||
}); | ||
@@ -28,18 +36,27 @@ | ||
expect(val.test).to.eq("some"); | ||
}, null, done); | ||
}, null, done); | ||
describe("find and modify", () => { | ||
it("findAnModify", (done) => { | ||
coll.findAndModify({query : {test: "some"}, update : {test : "some1"}, new : true}) | ||
.subscribe((val) => { | ||
expect(val).has.property("doc"); | ||
expect(val.doc).has.property("test", "some1"); | ||
}, | ||
done, done); | ||
describe("remove records", () => { | ||
it("remove all tests record", (done) => { | ||
coll.remove({}) | ||
.subscribe(() => {}, done, done) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
it("findAnModify", (done) => { | ||
coll.findAndModify({query : {test: "some"}, update : {test : "some1"}, new : true}) | ||
.subscribe((val) => { | ||
expect(val).has.property("doc"); | ||
expect(val.doc).has.property("test", "some1"); | ||
}, | ||
null, done); | ||
}) | ||
it("remove all tests record", (done) => { | ||
coll.remove({}) | ||
.subscribeOnCompleted(done) | ||
}) | ||
@@ -55,3 +72,3 @@ it("lock record", (done) => { | ||
expect(val).eqls([true, false, false]); | ||
}, null, done); | ||
}, done, done); | ||
@@ -58,0 +75,0 @@ }) |
@@ -7,4 +7,10 @@ /// <reference path="../typings/tsd.d.ts"/> | ||
const MONGO_URI = process.env.npm_config_MONGO_URI || process.env.npm_package_config_MONGO_URI; | ||
function getEnvVar(name: string) : string { | ||
return process.env[name] || | ||
process.env["npm_config_" + name] || | ||
process.env["npm_package_config_" + name]; | ||
} | ||
const MONGO_URI = getEnvVar("MONGO_URI_TEST"); | ||
describe("find tests", () => { | ||
@@ -11,0 +17,0 @@ |
@@ -8,3 +8,3 @@ { | ||
"noImplicitAny": true, | ||
"removeComments": true, | ||
"removeComments": false, | ||
"noLib": false, | ||
@@ -11,0 +11,0 @@ "emitDecoratorMetadata": false, |
Sorry, the diff of this file is not supported yet
579852
40
4603