From fcddf136a22245b309e6784a5ea3dfd5ad5edc72 Mon Sep 17 00:00:00 2001 From: Benjamin Pannell Date: Thu, 18 Jun 2015 14:38:43 +0200 Subject: [PATCH] ObjectID decorator now populates a transform --- dist/lib/Decorators.js | 4 ++++ lib/Decorators.ts | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/dist/lib/Decorators.js b/dist/lib/Decorators.js index c1d0be0..27aeef4 100644 --- a/dist/lib/Decorators.js +++ b/dist/lib/Decorators.js @@ -57,6 +57,10 @@ exports.Transform = Transform; function ObjectID(target, name) { target.constructor.schema = _.clone(target.constructor.schema || {}); target.constructor.schema[name] = MongoDB.ObjectID; + target.constructor.transforms[name] = { + fromDB: function (value) { return value._bsontype == 'ObjectID' ? new MongoDB.ObjectID(value.id).toHexString() : value; }, + toDB: function (value) { return value && typeof value === 'string' ? new MongoDB.ObjectID(value) : value; } + }; } exports.ObjectID = ObjectID; diff --git a/lib/Decorators.ts b/lib/Decorators.ts index 90ce0ec..9257395 100644 --- a/lib/Decorators.ts +++ b/lib/Decorators.ts @@ -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 = _.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 + }; } \ No newline at end of file