3.0.1 • Published 6 years ago

@decorators/mongoose v3.0.1

Weekly downloads
7
License
MIT
Repository
github
Last release
6 years ago

Node Decorators

Project implements decorators for modern tools for NodeJS like:

Installation

npm install @decorators/mongoose --save

API

Functions

  • 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

Decorators

Class
  • @Model(name: string, options?: SchemaTypeOpts) - registers model with defined name and options
Method
  • @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
Property
  • @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';

@Model('Animal')
class Animal {
  @SchemaField(String)
  name: string;

  @Static()
  static spawn() {}

  @Instance()
  scream() {}

  @Query()
  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;
3.0.1

6 years ago

3.0.0

6 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.1

8 years ago

1.0.0

8 years ago