uuid-mongodb icon indicating copy to clipboard operation
uuid-mongodb copied to clipboard

Mongoose 5.13.8 troubles

Open Able1991 opened this issue 4 years ago • 2 comments
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?

Able1991 avatar Aug 25 '21 11:08 Able1991

Hey how did you fix this!!

Mod5ied avatar Jun 29 '24 15:06 Mod5ied

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);
    });
}

Able1991 avatar Aug 06 '24 07:08 Able1991