Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheriting Mongoose schemas

I wanted to make a base 'Entity Schema', and other model entities would inherit from it. I did it, kinda, but then strange thing happened.

Those are my schemas:

  • AbstractEntitySchema
  • MessageSchema
  • UserSchema
  • RoomSchema

File: https://github.com/mihaelamj/nodechat/blob/master/models/db/mongo/schemas.js

But in MongoDB, they are all saved in the same document store: 'entity models' not separate ones, like Messages, Users.. Did I get what was supposed to happen, but not what I wanted, separate stores? If so I will just make a basic JSON/object as entity and append the appropriate properties for each entity. Or is there a better way? Thanks.

like image 823
Mihaela Avatar asked Feb 12 '16 11:02

Mihaela


People also ask

Do you need a schema for Mongoose?

Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

What are Mongoose schemas?

In mongoose, a schema represents the structure of a particular document, either completely or just a portion of the document. It's a way to express expected properties and values as well as constraints and indexes. A model defines a programming interface for interacting with the database (read, insert, update, etc).

How does Mongoose schema work?

With Mongoose, you would define a Schema object in your application code that maps to a collection in your MongoDB database. The Schema object defines the structure of the documents in your collection. Then, you need to create a Model object out of the schema. The model is used to interact with the collection.

What is Mongoose schema path?

You can think of a Mongoose schema as the configuration object for a Mongoose model. A SchemaType is then a configuration object for an individual property. A SchemaType says what type a given path should have, whether it has any getters/setters, and what values are valid for that path.


2 Answers

Since ES6 this works as well:

var ImageSchema: Schema = new Schema({
    ...CommonMetadataSchema.obj,
    src: String,
    description: String,
});
like image 98
Adrian Moisa Avatar answered Sep 27 '22 19:09

Adrian Moisa


If you want multiple overlapping models with different MongoDB collections, then you use this approach:

function extendSchema (Schema, definition, options) {
  return new mongoose.Schema(
    Object.assign({}, Schema.obj, definition),
    options
  );
}

Example

const extendSchema = require('mongoose-extend-schema');

const UserSchema = new mongoose.Schema({
  firstname: {type: String},
  lastname: {type: String}
});

const ClientSchema = extendSchema(UserSchema, {
  phone: {type: String, required: true}
});

You simply extend the original object the schema was created with and recreate a new schema on its basis. This is some sort of abstract schema which you inherit from.

Check this npm module: https://www.npmjs.com/package/mongoose-extend-schema

like image 33
Do Async Avatar answered Sep 27 '22 18:09

Do Async