Skip to content

Commit

Permalink
Add server support for objectid
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAnthony committed May 8, 2020
1 parent 00bc1c1 commit e0103e5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Collection {

async findOne(document: string) {
const obj = await this._collection.findOne({
_id: new MongoDb.ObjectId(document)
_id: JsonEncoder.fromObjectId(document)
})
return JsonEncoder.encode(obj);
}
Expand All @@ -55,15 +55,15 @@ export class Collection {
// TODO: For now it makes it impossible to remove fields from object with a projection
const update = partial ? { '$set':newValue } : JsonEncoder.decode(newValue);
await this._collection.replaceOne({
_id: new MongoDb.ObjectId(document)
_id: JsonEncoder.fromObjectId(document)
}, update);

return JsonEncoder.encode(newValue);
}

async removeOne(document: string) {
await this._collection.deleteOne({
_id: new MongoDb.ObjectId(document)
_id: JsonEncoder.fromObjectId(document)
});
}

Expand Down
7 changes: 7 additions & 0 deletions lib/JsonEncoder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import * as MongoDb from 'mongodb';

const objid_re = new RegExp(/ObjectId\('?([0-9a-fA-F]{24})'?\)/);
export default class JsonEncoder {
static fromObjectId(obj: string) {
const match = objid_re.exec(obj);
if (match && match[1])
return new MongoDb.ObjectId(match[1]);
return obj;
}
static encode(obj: any) {
if (obj instanceof MongoDb.ObjectID) {
return {
Expand Down

0 comments on commit e0103e5

Please sign in to comment.