Skip to content

Commit

Permalink
feat(buffer): add support for subtype prop
Browse files Browse the repository at this point in the history
Fix #5530
  • Loading branch information
vkarpov15 committed Nov 12, 2017
1 parent 910e8b2 commit 89d2f3d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/schema/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ SchemaBuffer.prototype.cast = function(value, doc, init) {
if (Buffer.isBuffer(value)) {
if (!value || !value.isMongooseBuffer) {
value = new MongooseBuffer(value, [this.path, doc]);
if (this.options.subtype != null) {
value._subtype = this.options.subtype;
}
}

return value;
Expand All @@ -135,12 +138,36 @@ SchemaBuffer.prototype.cast = function(value, doc, init) {
value = [value];
}
ret = new MongooseBuffer(value, [this.path, doc]);
if (this.options.subtype != null) {
ret._subtype = this.options.subtype;
}
return ret;
}

throw new CastError('buffer', value, this.path);
};

/**
* Sets the default [subtype](https://studio3t.com/whats-new/best-practices-uuid-mongodb/)
* for this buffer. You can find a [list of allowed subtypes here](http://api.mongodb.com/python/current/api/bson/binary.html).
*
* ####Example:
*
* var s = new Schema({ uuid: { type: Buffer, subtype: 4 });
* var M = db.model('M', s);
* var m = new M({ uuid: 'test string' });
* m.uuid._subtype; // 4
*
* @param {Number} subtype the default subtype
* @return {SchemaType} this
* @api public
*/

SchemaBuffer.prototype.subtype = function(subtype) {
this.options.subtype = subtype;
return this;
};

/*!
* ignore
*/
Expand Down

0 comments on commit 89d2f3d

Please sign in to comment.