Skip to content

Commit

Permalink
fix #25: Address issues when cloning values of type MongoDB.ObjectID …
Browse files Browse the repository at this point in the history
…and MongoDB.Binary
  • Loading branch information
notheotherben committed Dec 15, 2015
1 parent 95a5dff commit 2a6b079
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 16 deletions.
9 changes: 8 additions & 1 deletion dist/lib/ModelHelpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/lib/ModelHelpers.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/lib/Transforms.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/lib/Transforms.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion lib/ModelHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,15 @@ export class ModelHelpers<TDocument extends { _id?: any }, TInstance> {
cloneDocument<T>(original: T): T {
return _.cloneDeep(original, (value) => {
if(Buffer.isBuffer(value)) {
return (<Buffer>value).slice();
return value;
}

if(value instanceof MongoDB.Binary) {
return value;
}

if(value instanceof MongoDB.ObjectID) {
return value;
}
});
}
Expand Down
9 changes: 4 additions & 5 deletions lib/Transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ export interface PropertyTransform<T> {
}

export const DefaultTransforms = {
ObjectID: <PropertyTransform<MongoDB.ObjectID | BSON.ObjectID>>{
fromDB: value => value && (<BSON.ObjectID>value)._bsontype == 'ObjectID' ? new MongoDB.ObjectID((<BSON.ObjectID>value).id).toHexString() : value,
ObjectID: <PropertyTransform<MongoDB.ObjectID>>{
fromDB: value => value instanceof MongoDB.ObjectID ? value.toHexString() : value,
toDB: value => typeof value === 'string' ? new MongoDB.ObjectID(value) : value
},
Binary: <PropertyTransform<MongoDB.Binary | BSON.Binary>>{
Binary: <PropertyTransform<MongoDB.Binary>>{
fromDB: value => {
if(!value) return null;
if((<BSON.Binary>value)._bsontype === "Binary")
return (<BSON.Binary>value).buffer;
if(value instanceof MongoDB.Binary) return (<any>value).buffer;

return value;
},
Expand Down
2 changes: 1 addition & 1 deletion test/Decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe("Decorators", () => {

describe("the ObjectID transform", () => {
it("should convert an ObjectID to a string", () => {
chai.expect(Test.transforms['_id'].fromDB({ _bsontype: 'ObjectID', id: 'aaaaaaaaaaaaaaaaaaaaaaaa' }, '_id', null)).to.eql('aaaaaaaaaaaaaaaaaaaaaaaa');
chai.expect(Test.transforms['_id'].fromDB(Iridium.toObjectID('aaaaaaaaaaaaaaaaaaaaaaaa'), '_id', null)).to.eql('aaaaaaaaaaaaaaaaaaaaaaaa');
});

it("should convert a string to an ObjectID", () => {
Expand Down
5 changes: 1 addition & 4 deletions test/Transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,7 @@ describe("Transforms", () => {
describe("the default Buffer transform", () => {
it("should convert a MongoDB BSON Binary object into a buffer", () => {
let transform = DefaultTransforms.Binary.fromDB;
let result = transform({
_bsontype: 'Binary',
buffer: new Buffer('test', 'utf8')
}, '_id', null);
let result = transform(new MongoDB.Binary(new Buffer('test', 'utf8')), '_id', null);

chai.expect(result).to.exist;
chai.expect(result.toString('utf8')).to.eql('test');
Expand Down

0 comments on commit 2a6b079

Please sign in to comment.