Skip to content

Commit

Permalink
ObjectID decorator now populates a transform
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Jun 18, 2015
1 parent e091734 commit fcddf13
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions dist/lib/Decorators.js

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

4 changes: 4 additions & 0 deletions lib/Decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ export function Transform(fromDB: (value: any) => any, toDB: (value: any) => any
export function ObjectID(target: { constructor: typeof Instance }, name: string) {
target.constructor.schema = <Schema>_.clone(target.constructor.schema || {});
target.constructor.schema[name] = MongoDB.ObjectID;
target.constructor.transforms[name] = {
fromDB: value => value._bsontype == 'ObjectID' ? new MongoDB.ObjectID(value.id).toHexString() : value,
toDB: value => value && typeof value === 'string' ? new MongoDB.ObjectID(value) : value
};
}

0 comments on commit fcddf13

Please sign in to comment.