Skip to content

Commit

Permalink
Model will provide a default _id type if one is not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Jun 18, 2015
1 parent a77716b commit 939193e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
9 changes: 9 additions & 0 deletions dist/lib/Model.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/Model.js.map

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions lib/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ export default class Model<TDocument extends { _id?: any }, TInstance> {
this._transforms = instanceType.transforms || {};
this._validators = instanceType.validators || [];
this._indexes = instanceType.indexes || [];

if(!this._schema._id) this._schema._id = MongoDB.ObjectID;

if(this._schema._id === MongoDB.ObjectID && !this._transforms['_id'])
this._transforms['_id'] = {
fromDB: value => value._bsontype == 'ObjectID' ? new MongoDB.ObjectID(value.id).toHexString() : value,
toDB: value => value && typeof value === 'string' ? new MongoDB.ObjectID(value) : value
};

if ((<Function>instanceType).prototype instanceof Instance)
this._Instance = ModelSpecificInstance(this, instanceType);
Expand Down

0 comments on commit 939193e

Please sign in to comment.