Tiny ES2015 class-based wrapper for mongoose model methods.
Note: Since v4.7.0, Mongoose already supports ES2015 classes (PR reference).
Installation
$ npm i mongoose mongoose-class-wrapper --save
Usage
You can use this plugin with latest Node.js versions (at least v4 LTS) as they support ES2015 class syntax (in strict mode). For previous versions use babel.
Basic Example
// Importing modulesconst mongoose = ;const loadClass = ;// for babel:// import mongoose from 'mongoose';// import loadClass from 'mongoose-class-wrapper';// for typescript:// import * as mongoose from 'mongoose';// import loadClass = require('mongoose-class-wrapper'); // Create mongoose schemavar userSchema = username: type: String unique: true required: true email: type: String unique: true required: true hashedPassword: type: String required: true salt: type: String required: true created: type: Date default: Datenow; // Create new class with model methods { return this_plainPassword; } { this_plainPassword = password; thissalt = Math + ''; thishashedPassword = this; } { return crypto; } static { return this; } //posible options, for instance if you want to ignore getter and setters putconst options= ignoreGettersAndSetters:true // Add methods from class to schemauserSchema; // Export mongoose model;
Inheritance Example
;; {} // Add schema to base classResourceschema = version: type: Number default: 0 // Add hooks to base classResourcehooks = pre: { thisversion += 1; ; } var schema = title: String author: String; {} // Add schema, hooks and methods from class to schemaschema; // Export mongoose model;
Experimental decorators
Experimental decorators for mongoose models are available in mongoose-decorators package.
License
This library is under the MIT License