da-mongo-rx
Advanced tools
Comparing version
@@ -15,2 +15,6 @@ | ||
} | ||
interface IFindAndModifyResult { | ||
doc: any; | ||
command: ICommandResult; | ||
} | ||
class Collection { | ||
@@ -24,3 +28,3 @@ private coll; | ||
update(query: any, upd: any): Rx.Observable<any>; | ||
findAndModify(upd: any): Rx.Observable<any>; | ||
findAndModify(upd: any): Rx.Observable<IFindAndModifyResult>; | ||
} | ||
@@ -33,4 +37,5 @@ class MongoDb { | ||
runCommand(command: any): Rx.Observable<ICommandResult>; | ||
lock(id: string, collName: string, replicas?: number): Rx.Observable<boolean>; | ||
} | ||
} | ||
export = mongoRx; |
@@ -51,3 +51,6 @@ | ||
Collection.prototype.findAndModify = function (upd) { | ||
return this.fromNode("findAndModify")(upd); | ||
return this.fromNode("findAndModify")(upd) | ||
.map(function (val) { | ||
return { doc: val[0], command: val[1] }; | ||
}); | ||
}; | ||
@@ -68,4 +71,15 @@ return Collection; | ||
MongoDb.prototype.runCommand = function (command) { | ||
return Rx.Observable.fromNodeCallback(this.db.runCommand)(command); | ||
return Rx.Observable.fromNodeCallback(this.db.runCommand, this.db)(command); | ||
}; | ||
MongoDb.prototype.lock = function (id, collName, replicas) { | ||
if (replicas === void 0) { replicas = 1; } | ||
var command = { | ||
insert: collName, | ||
documents: [{ _id: id }], | ||
ordered: false, | ||
writeConcern: { w: replicas } | ||
}; | ||
return this.runCommand(command) | ||
.map(function (r) { return r.ok && r.n == 1; }); | ||
}; | ||
return MongoDb; | ||
@@ -72,0 +86,0 @@ })(); |
{ | ||
"name": "da-mongo-rx", | ||
"version": "1.0.3", | ||
"version": "1.0.5", | ||
"description": "Thin layer on top of mongojs library exposes RxJS interfaces.", | ||
@@ -5,0 +5,0 @@ "main": "dist/mongo-rx.js", |
@@ -6,2 +6,5 @@ ///<reference path="../typings/tsd.d.ts"/> | ||
/** | ||
* Every method (even write action) must be subscribed to be executed. | ||
*/ | ||
module mongoRx { | ||
@@ -34,3 +37,8 @@ | ||
} | ||
export interface IFindAndModifyResult { | ||
doc : any | ||
command : ICommandResult | ||
} | ||
class Cursor implements ICursor { | ||
@@ -118,5 +126,9 @@ constructor(private cursor: any) { | ||
findAndModify(upd : any) : Rx.Observable<any> { | ||
return this.fromNode("findAndModify")(upd); | ||
findAndModify(upd : any) : Rx.Observable<IFindAndModifyResult> { | ||
return this.fromNode("findAndModify")(upd) | ||
.map<any>((val: any) => { | ||
return {doc : val[0], command : val[1]} | ||
}); | ||
} | ||
} | ||
@@ -158,4 +170,28 @@ | ||
runCommand(command: any) : Rx.Observable<ICommandResult> { | ||
return (<any>Rx.Observable).fromNodeCallback(this.db.runCommand)(command); | ||
} | ||
return (<any>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). | ||
*/ | ||
lock(id: string, collName: string, replicas: number = 1): Rx.Observable<boolean> { | ||
var command = { | ||
insert: collName, | ||
documents: [{_id: id}], | ||
ordered: false, | ||
writeConcern: {w: replicas} | ||
}; | ||
return this.runCommand(command) | ||
.map((r: any) => r.ok && r.n == 1); | ||
} | ||
} | ||
@@ -162,0 +198,0 @@ |
@@ -6,2 +6,3 @@ /// <reference path="../typings/tsd.d.ts"/> | ||
var expect = chai.expect; | ||
import Rx = require("rx"); | ||
@@ -12,6 +13,8 @@ const MONGO_URI = process.env.npm_config_MONGO_URI || process.env.npm_package_config_MONGO_URI; | ||
var db: mongoRx.MongoDb; | ||
var coll: mongoRx.Collection; | ||
before(() => { | ||
var db = new mongoRx.MongoDb(MONGO_URI, ["create"]); | ||
before((done) => { | ||
db = new mongoRx.MongoDb(MONGO_URI, ["create", "locker"]); | ||
coll = db.getCollection("create") | ||
coll.remove({}).concat(db.getCollection("locker").remove({})).subscribeOnCompleted(done); | ||
}); | ||
@@ -27,2 +30,11 @@ | ||
}) | ||
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); | ||
}) | ||
@@ -33,3 +45,18 @@ it("remove all tests record", (done) => { | ||
}) | ||
it("lock record", (done) => { | ||
Rx.Observable.concat( | ||
db.lock("111", "locker"), | ||
db.lock("111", "locker"), | ||
db.lock("111", "locker") | ||
) | ||
.toArray() | ||
.subscribe((val) => { | ||
expect(val).eqls([true, false, false]); | ||
}, null, done); | ||
}) | ||
}) |
@@ -10,3 +10,3 @@ /// <reference path="../typings/tsd.d.ts"/> | ||
describe("find tests", () => { | ||
var coll: mongoRx.Collection; | ||
@@ -46,3 +46,3 @@ before((done) => { | ||
}) | ||
}) |
570239
0.44%4290
1.63%