uuid-mongodb
uuid-mongodb copied to clipboard
Mongoose 5.13.8 troubles
trafficstars
Hello there.
Not work implementation from examples with MUUID.from(this._id).toString();
this._id - is mongo lib object
Now if fix with
_id: {
type: Buffer,
subtype: 4,
default: () => MUUID.v1(),
},
....
// virtual getter for custom _id
schema
.virtual("id")
.get(function () {
return MUUID.from(this._id.toBSON()).toString();
})
.set(function (val) {
this._id = MUUID.from(val);
});
Is this a normal solution, or is it necessary to do something else?
Hey how did you fix this!!
Hey how did you fix this!!
import MUUID from "uuid-mongodb";
export default function uuidPlugin(schema) {
schema.add({
_id: {
type: Buffer,
subtype: 4,
default: () => MUUID.v1(),
},
});
// no need for auto getter for _id will add a virtual later
schema.set("id", false);
// virtual getter for custom _id
schema
.virtual("id")
.get(function () {
return MUUID.from(this._id.toBSON()).toString();
})
.set(function (val) {
this._id = MUUID.from(val);
});
}