Created
October 11, 2016 09:33
-
-
Save 4poc/7e26741e91ea3e6e403093c0105ec76e to your computer and use it in GitHub Desktop.
Buffer Schema Type in Mongoose: How to specify subtype
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const mongoose = require('mongoose'); | |
const Schema = mongoose.Schema; | |
const ObjectId = Schema.Types.ObjectId; | |
const MongooseBuffer = mongoose.Types.Buffer; | |
const uuid = require('node-uuid'); | |
const db = require('../lib/database'); | |
const BinTestSchema = new Schema({ | |
_id: { | |
type: Schema.Types.Buffer, | |
default : () => { | |
const buffer = uuid.v4(null, new Buffer(16)); | |
return new MongooseBuffer(buffer).toObject(0x03); | |
}, | |
get: (buffer) => uuid.unparse(buffer), | |
set: (string) => { | |
const buffer = uuid.parse(string); | |
return new MongooseBuffer(buffer).toObject(0x03); | |
} | |
} // results in: BinData(3,"2S2M2YPQRFGF6S5YTMDoOA==") | |
}); | |
module.exports = db.model('BinTest', BinTestSchema); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment