npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongo-schematic-class

v1.0.4

Published

Mongoose Schema builder for ES6 classes that supports multilevel inheritance.

Downloads

6

Readme

codecov CircleCI

SchematicClass

SchematicClass is a way to build your mongoose schemas and models using native classes. Additionally, SchematicClass extends mongoose's native discriminators to support multilevel discriminators (inheritance).

Installation

npm

npm install --save mongo-schematic-class

yarn

yarn add mongo-schematic-class

Features

  • [x] Full support of mongoose's native features
  • [x] Multilevel discriminators

Example Usage

const { createModel } = require('mongo-schematic-class');

class Vehicle {
  // Define the structure of the Schema.
  static get __context() {
    return {
      vin:  {
        type:     String,
        required: true,
        unique:   true,
      },
      milesTraveled: {
        type:     Number,
        default:  0,
      }
    };
  }

  // This instance method will be added to the Schema and be inherited by all subclasses.
  drive(miles) {
    this.milesTraveled += miles;
  }
}

// Create the Vehicle model.
createModel(Vehicle);

class GasVehicle extends Vehicle {
  // Define the structure of the Schema.
  static get __context() {
    return {
      tankSize: {
        type:     Number,
        required: true,
      },
      mpg:      {
        type:     Number,
        required: true,
      },
    }
  }

  // Define the virtuals that should be added to the Schema.  These are not suggested,
  // but supported for backwards compatibility.
  static get __virtuals() {
    return {
      drivingRange: {
        get:  function getDrivingRange() {
          return (this.tankSize * this.mpg);
        },
      },
    };
  }

  // If you define a virtual on the Schema, you should define a similar one on the class.
  get drivingRange() {
    return (this.tankSize * this.mpg);
  }
}

// Create the GasVehicle model.
createModel(GasVehicle);

API

createModel(Class: SchematicClass, modelName?: string): Model

Creates a Model for the specified SchematicClass. This is the only method that has to be called on the SchematicClass.

Arguments

| Type | Description| |------|------------| | SchematicClass | The SchematicClass for which the Model should be created. | | string | Optional. The name of the Model. If omitted, the name of the SchematicClass is used. |

How to define a SchematicClass

SchematicClass works by looking at static properties on the provided class to build the mongoose schema. The following properties are supported:

| Name | Type | Required | Description | |------|------|----------|-------------| | __context | { [ propertyName: string ]: any } | true | This is effectively the schema for your class. You'll define all of the instance properties on your class as well as their type. Any mongoose supported type is supported. | | __pre | { [ eventName: string ]: (...args: any[]) => any } | false | The prehooks to define on the schema. | | __post | { [ eventName: string ]: (...args: any[]) => any } | false | The posthooks to define on the schema. | | __virtuals | { [ virtualName: string ]: { get?: () => any, set?: (val: any) => void } } | false | The virtuals to define for this class. Virtuals are generally discouraged when using SchematicClass as it requires duplication of code (you must define a getter and setter twice, once for the Model and once for the class). Instead, you can use an instance method to get a similar effect. | | __foreignKeys | { [ foreignKeyName: string ]: { ref: string | Model, localField: string | (() => string), foreignField: string | (() => string), justOne?: boolean, count?: boolean } } | false | Foreign keys to define on the schema. Foreign keys in mongoose can be used to populate documents based on values other than the _id field. See the mongoose documentation for further information. | | __queryHelpers | { [ queryName: string ]: (this: Model, ...args: any[]) => DocumentQuery } | false | Defines a mongoose query helper on the schema. |

Inheritable Properties

One of the main benefits of mongo-schematic-class is the support for multilevel discriminators (inheritance). The following properties defined on a SchematicClass are inheritable:

  • Instance Properties (__context)
  • *Middleware (__pre & __post)
  • Virtuals (__virtuals)
  • Foreign Keys (__foreignKeys)
  • Query Helpers (__queryHelpers)
  • Instance Methods

*Unlike other inheritable properties, middleware cannot be overridden. If you define a pre hook on your superclass and the same pre hook on the subclass, both pre hooks will be executed.

Static Properties Added to SchematicClasses (and Their Schemas)

While building the schema and creating the model, mongo-schematic-class will add the following static properties to your SchematicClass.

  • __schema - This is the schema generated when you execute buildSchema() or createModel()
  • __model - This is the model created when you execute createModel()
  • __inherits - This is the SchematicClass's parent class. This is added when you call inherits() or createModel().
  • isInstanceOf() - This is a helper method that should be used in place of the instanceof operator when trying to determine if an object is an instance of a SchematicClass. This works for both instances of the Model and instances of the actual SchematicClass. isInstanceOf() will follow the inheritance chain.

Advanced API

buildSchema(Class: SchematicClass)

Builds the Mongoose schema as configured on the SchematicClass. After running this, the __schema property will be added to Class.

Arguments

| Type | Description| |------|------------| | SchematicClass | The SchematicClass for which a schema should be built. |

Return Value

| Type | Description| |------|------------| | Model | The model created for Class. |

inherits(Subclass: SchematicClass, Superclass: SchematicClass)

Instructs mongo-schematic-class that Subclass inherits from Superclass. Both Subclass and Superclass must be a SchematicClass. If this method is not called when inheriting a Superclass, buildSchema() will automatically detect that Subclass inherits from Superclass and call this method.

After calling this, the __inherits property will be added to Subclass.

Arguments

| Type | Description| |------|------------| | SchematicClass | The subclass inheriting from Superclass | | SchematicClass | The superclass from which subclass inherits. |