Installation
npm install --save class-to-mongoose-schema
or
yarn add class-to-mongoose-schema
Purpose of this Package
To enable inheritance of Mongoose Schema via JavaScript Classes.
Note: Only methods, variables and static methods are converted to the schema, static variables are ignored as there is no equivalent in Mongoose.
As of 2.x, mongoose
has become a peer dependency
Example
Via Properties:
// Person.js firstName = String lastName = String static { // Example static method return mongoose } const personSchema = // Person.name === 'Person'
// User.js username = String password = String { // Example method // ... } static { // Example static method return mongoose } const userSchema = // User.name === 'User'
Via Constructor (pass in arguments on object instantiation):
// Person.js { thisfirstName = String thislastName = String } static { // Example static method return mongoose } const personSchema = // Person.name === 'Person'
// User.js { superargs thisusername = String thispassword = String } { // Example method // ... } static { // Example static method return mongoose } const userSchema = // User.name === 'User'