Skip to content

Instantly share code, notes, and snippets.

@4poc
Created October 11, 2016 09:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4poc/7e26741e91ea3e6e403093c0105ec76e to your computer and use it in GitHub Desktop.
Save 4poc/7e26741e91ea3e6e403093c0105ec76e to your computer and use it in GitHub Desktop.
Buffer Schema Type in Mongoose: How to specify subtype
'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