question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Advanced custom schema type for custom objects or value-objects

See original GitHub issue

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

I already posted a question on StackOverflow without success: http://stackoverflow.com/questions/38094817/mongoose-advanced-custom-schema-object-type.

Resuming, how can I achieve that:

  1. Every time a new model is “hydrated” from db (mongoose init), the field will be an instance of my custom object and not a plain object. For this I understand it will use the SchemaType.prototype.cast function.
  2. Every time I will save my Model the custom type field should be encoded and stored in a custom way (as plain object, array, string or any BSON type).
  3. If I use model.toObject() it will recursively call the model.myfield.toObject() to have a full plain object representation of the document.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

3reactions
vkarpov15commented, Nov 5, 2016

I added the toBSON() check in 1a93d1f. Here’s a better example of how to do the polygon type you’re looking at:

'use strict';

var assert = require('assert');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/gh4356');
mongoose.set('debug', true);

class Polygon {
  constructor(v) {
    this.v = v;
  }

  toBSON() {
    return this.v;
  }
}

class PolygonSchema extends mongoose.SchemaType {
  cast(v) {
    return new Polygon(v);
  }
}

mongoose.Schema.Types.Polygon = PolygonSchema;

const schema = new Schema({ test: Polygon });
const M = mongoose.model('Test', schema);

M.create({ test: 5 }).then(doc => {
  console.log('done', doc);
  console.log(doc.test.toBSON());
  process.exit(0);
});

Output:

$ node gh-4356.js 
(node:14709) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
Mongoose: tests.insert({ test: 5, _id: ObjectId("581e684cc4d5933975526934"), __v: 0 })
done { __v: 0, test: Polygon { v: 5 }, _id: 581e684cc4d5933975526934 }
5
0reactions
vkarpov15commented, Feb 26, 2018

@NikhilAshodariya I opened up a separate issue for this #6174.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Mongoose advanced custom schema object type
To customize mongodb persistence, I had to implement a toBSON() function in my custom type object prototype (i.e. Polygon ). model.toObject() / ...
Read more >
Creating a schema for a custom object
A schema defines the named properties of a custom object. The schema doesn't contain information about any specific object. It just describes the...
Read more >
Custom field object type and Jira Automation - Forge
Hello, I am trying to use Custom field object types with Jira Automation and smart values. For example, I would like a “Create...
Read more >
Schema Builder Custom Object Definition - Salesforce Help
Makes the data in the custom object records available for reporting purposes. To create reports on custom objects, choose the Other Reports report...
Read more >
Advanced field value conversion using custom mapping types
We create a simple entity with a field $point which holds a value object Point representing the latitude and longitude of the position....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found