Skip to content

Commit

Permalink
refactor: Export MongoDB ObjectID and Binary types for lib/BSON
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Apr 12, 2016
1 parent 3ea4aa7 commit ca6bbff
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 33 deletions.
25 changes: 1 addition & 24 deletions lib/BSON.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1 @@
/**
* Various interfaces for low level BSON types used within Iridium.
*/

/**
* Represents a low level BSON value with its included _bsontype property.
*/
export interface Value {
_bsontype: string;
}

/**
* Represents a raw ObjectID object received from the database.
*/
export interface ObjectID extends Value {
id: string;
}

/**
* Represents a raw Binary object received from the database.
*/
export interface Binary extends Value {
buffer: Buffer;
}
export { ObjectID, Binary } from "mongodb";
17 changes: 8 additions & 9 deletions lib/Transforms.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as MongoDB from "mongodb";
import {ObjectID, Binary} from "./BSON";
import {Model} from "./Model";
import * as BSON from "./BSON";

export interface Transforms {
/**
Expand Down Expand Up @@ -37,20 +36,20 @@ export interface PropertyTransform<T> {
}

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

return value;
},
toDB: value => {
if(Buffer.isBuffer(value)) return new MongoDB.Binary(value);
if(Array.isArray(value)) return new MongoDB.Binary(new Buffer(value));
if(Buffer.isBuffer(value)) return new Binary(value);
if(Array.isArray(value)) return new Binary(new Buffer(value));
return null;
}
}
Expand Down

0 comments on commit ca6bbff

Please sign in to comment.