Mongoose advanced custom schema object type

亡梦爱人 提交于 2019-11-30 15:17:11

问题


I couldn't find any example of an advanced custom schema type involving custom objects (or value-objects) in Mongoose >=4.4.

Imagine that I want to use a custom type like:

function Polygon(c) {
  this.bounds = [ /* some data */ ];
  this.npoints = /* ... */
  /* ... initialize polygon ... */
};

Polygon.prototype.area = function surfaceArea() { /**/ };

Polygon.prototype.toObject = function toObject() { return this.bounds; };

Next, I implement a custom SchemaType like:

function PolygonType(key, options) {
  mongoose.SchemaType.call(this, key, options, 'PolygonType');
}

PolygonType.prototype = Object.create(mongoose.SchemaType.prototype);

PolygonType.prototype.cast = function(val) {
  if (!val) return null;
  if (val instanceof Polygon) return val;
  return new Polygon(val)
}

PolygonType.prototype.default = function(val) {
  return new Polygon(val);
}

How can I assure that:

  1. Every time a new object is "hydrated" from db (mongoose init), I will have a Polygon instance and not a plain object. I understand it will use the cast function. assert(model.polygon instanceof Polygon)

  2. Every time I will save my Model the Polygon attribute should be encoded and stored as a plain object representation (Polygon.prototype.toObject()) that in this case is an Array object in mongodb.

  3. If I use model.toObject() it will recursively call the model.polygon.toObject() to have a full plain object representation of the document.

回答1:


I found a solution thanks to @vkarpov15 on github.com:

  1. SchemaType.prototype.cast() is needed to correctly hydrate the document model from raw mongodb representation, and throw an error in case of invalid data.

  2. To customize mongodb persistence, I had to implement a toBSON() function in my custom type object prototype (i.e. Polygon).

  3. model.toObject() / model.toJSON() currently doesn't call recursively toObject()/toJSON() on all children, but it looks like it will be fixed. But I could overload it as temporary workaround assigning a custom schema.methods.toObject() instance method.



来源:https://stackoverflow.com/questions/38094817/mongoose-advanced-custom-schema-object-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!