<commit_before># mongoose-schema-decorators
Define your mongoose models/schemas easily with @decorators

## Installation
```
npm install mongoose-schema-decorators --save
```

## API
#### Functions
* **buildSchema(target: Function, loadClass:boolean = true)** - helper function to generate mongoose schema
    * {Function} target  name of the decorated class
    * {boolean} loadClass indicating if load setters + getters, static methods, and instance methods from the target class to schema. Default to true

#### Decorators
* **@schema(options?: Mongoose.SchemaTypeOpts<any>)** - decorates a class for mongoose schema generation
* **@field(options?: Mongoose.SchemaTypeOpts<any>)** - decorates a property with schema options if any
* **@required** - decorates a property which is a required field in schema
* **@indexed** - decorates a property which is a indexed field in schema
* **@unique** - decorates a property which is unique field in schema
* **@statics** - decorates a property/method which to be registered as schema statics
* **@methods** - decorates a method which to be registered as schema instance methods
* **@virtuals(options?: any)** - decorates a getter/setter/property (virtual reference to build relationship between collections) as schema virtuals

## Sample usage
```
interface Customer {
  _id: number;
  name: string;
  createdDate: Date;

  readonly services: Service[];
  readonly myGetter: string;
}

type CustomerDocument = Customer & Mongoose.Document;

@schema({
  toJSON: {
    getters: true,
    virtuals: true
  }
})
class CustomerClass extends Mongoose.Model implements Customer {
  @indexed
  @unique
  _id: number;

  @field
  @indexed
  @unique
  name: string;

  @field(Date)
  createdDate: Date;

  @virtuals({
    ref: 'Service',
    localField: '_id',
    foreignField: 'customer',
    justOne: false
  })
  services: Service[];

  static Search(term: string): Promise<CustomerDocument[]> {
    let reg = new RegExp(term, 'i');
    return this.find({ name: reg }).exec();
  }

  get myGetter(): string {
    return 'sample getter';
  }
}

const CustomerSchema: Mongoose.Schema = buildSchema(CustomerClass);
```

## License
MIT
<commit_msg>Added npm version badge to README<commit_after># mongoose-schema-decorators

[![NPM version][npm-image]][npm-url]

Define your mongoose models/schemas easily with @decorators

## Installation
```
npm install mongoose-schema-decorators --save
```

## API
#### Functions
* **buildSchema(target: Function, loadClass:boolean = true)** - helper function to generate mongoose schema
    * {Function} target  name of the decorated class
    * {boolean} loadClass indicating if load setters + getters, static methods, and instance methods from the target class to schema. Default to true

#### Decorators
* **@schema(options?: Mongoose.SchemaTypeOpts<any>)** - decorates a class for mongoose schema generation
* **@field(options?: Mongoose.SchemaTypeOpts<any>)** - decorates a property with schema options if any
* **@required** - decorates a property which is a required field in schema
* **@indexed** - decorates a property which is a indexed field in schema
* **@unique** - decorates a property which is unique field in schema
* **@statics** - decorates a property/method which to be registered as schema statics
* **@methods** - decorates a method which to be registered as schema instance methods
* **@virtuals(options?: any)** - decorates a getter/setter/property (virtual reference to build relationship between collections) as schema virtuals

## Sample usage
```
interface Customer {
  _id: number;
  name: string;
  createdDate: Date;

  readonly services: Service[];
  readonly myGetter: string;
}

type CustomerDocument = Customer & Mongoose.Document;

@schema({
  toJSON: {
    getters: true,
    virtuals: true
  }
})
class CustomerClass extends Mongoose.Model implements Customer {
  @indexed
  @unique
  _id: number;

  @field
  @indexed
  @unique
  name: string;

  @field(Date)
  createdDate: Date;

  @virtuals({
    ref: 'Service',
    localField: '_id',
    foreignField: 'customer',
    justOne: false
  })
  services: Service[];

  static Search(term: string): Promise<CustomerDocument[]> {
    let reg = new RegExp(term, 'i');
    return this.find({ name: reg }).exec();
  }

  get myGetter(): string {
    return 'sample getter';
  }
}

const CustomerSchema: Mongoose.Schema = buildSchema(CustomerClass);
```

## License
MIT

[npm-url]: https://www.npmjs.com/package/mongoose-schema-decorators
[npm-image]: https://img.shields.io/npm/v/mongoose-schema-decorators.svg?style=flat-square

{'fraction_non_alphanumeric': 0.11821155943293347, 'fraction_numerical': 0.0, 'mean_word_length': 4.408018867924528, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2416157', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1322, 'n_words': 461}