class-to-mongoose-schema
v2.0.1
Published
Enables inheritance of Mongoose Schema via JavaScript Classes
Downloads
18
Readme
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
import mongoose, { Schema } from 'mongoose'
import classToSchema from 'class-to-mongoose-schema'
export class Person {
firstName = String
lastName = String
static getAll() { // Example static method
return mongoose.model('Person').find()
}
}
const personSchema = classToSchema(new Person())
export { personSchema }
export default mongoose.model(Person.name, personSchema) // Person.name === 'Person'
// User.js
import mongoose, { Schema } from 'mongoose'
import classToSchema from 'class-to-mongoose-schema'
import { Person } from 'Person'
export class User extends Person {
username = String
password = String
secure(args) { // Example method
// ...
}
static getAll() { // Example static method
return mongoose.model('User').find()
}
}
const userSchema = classToSchema(new User())
export { userSchema }
export default mongoose.model(User.name, userSchema) // User.name === 'User'
Via Constructor (pass in arguments on object instantiation):
// Person.js
import mongoose, { Schema } from 'mongoose'
import classToSchema from 'class-to-mongoose-schema'
export class Person {
constructor(args) {
this.firstName = String
this.lastName = String
}
static getAll() { // Example static method
return mongoose.model('Person').find()
}
}
const personSchema = classToSchema(new Person(args))
export { personSchema }
export default mongoose.model(Person.name, personSchema) // Person.name === 'Person'
// User.js
import mongoose, { Schema } from 'mongoose'
import classToSchema from 'class-to-mongoose-schema'
import { Person } from 'Person'
export class User extends Person {
constructor(args) {
super(args)
this.username = String
this.password = String
}
secure(args) { // Example method
// ...
}
static getAll() { // Example static method
return mongoose.model('User').find()
}
}
const userSchema = classToSchema(new User(args))
export { userSchema }
export default mongoose.model(User.name, userSchema) // User.name === 'User'