node decorators


3.0.14 years ago5 years agoMinified + gzip package size for @decorators/mongoose in KB


Node Decorators

Project implements decorators for modern tools for NodeJS like:


npm install @decorators/mongoose --save



  • model(class: MongooseClass) - Helper function to generate model for class
  • schema(class: MongooseClass) - Helper function to generate plain schema
  • ref(collectionRef: string) - Helper function to define reference to another collection/model


  • @Model(name: string, options?: SchemaTypeOpts) - registers model with defined name and options
  • @Static() - registers static method (on static member)
  • @Query() - registers query
  • @Instance() - registers instance method
  • @Virtual() - registers virtual property
  • @Hook(hookType: string, actionType: string) - define mongoose lifecycle hook, where:
    • hookType = pre | post | etc.
    • actionType = save | find | etc.
    • for all other hooks you can check MongooseJS website
  • @SchemaField(options: schemaFieldDefinition) - registers schema field
  • @Static() - registers static property
  • @Index() - registers index property

Example Mongoose Model

Note: In order to get access to all proper methods, you have to extend mongoose Document in your class interface, like so:

import { Document } from 'mongoose';

class Animal {
  name: string;

  static spawn() {}

  scream() {}

  byName(name: string): Promise<Animal> {
    return this.find({ name });
interface Animal extends Document{}

export const AnimalModel: AnimalType = model(Animal);

// This type is also necessary to have all needed methods and properties of mongoose
// plus all static methods and properties of Animal class
type AnimalType = mongoose.Model<Animal> & typeof Animal;

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.