# mongoose-extend

> extend mongoose with rich functionalities

Latest version **0.2.0** (published 2016-05-24) · ISC license · 0 weekly downloads

## Install

```sh
npm install mongoose-extend
pnpm add mongoose-extend
yarn add mongoose-extend
bun add mongoose-extend
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2016-05-24 |
| First published | 2016-03-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+7 in 1 direct dependencies) |
| Install scripts | no |
| Author | Hank |
| Maintainers | hankchiu |
| Keywords | mongoose, baseschema |

## Links

- npm: https://www.npmjs.com/package/mongoose-extend
- npm.io page: https://npm.io/package/mongoose-extend

## Dependencies (1)

- [mongoose](https://npm.io/package/mongoose.md) ^4.4.9

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.2.0 (latest) — 2016-05-24
- 0.1.2 — 2016-04-05
- 0.1.1 — 2016-03-27
- 0.0.1 — 2016-03-27

## README

# mongoose-extend
[![npm version](https://badge.fury.io/js/mongoose-extend.svg)](https://badge.fury.io/js/mongoose-extend)

Extended [mongoose](https://www.npmjs.com/package/mongoose) module with serveral useful features.

### Features
- Fully compatible with original mongoose.
- Handy functions to define hooks.
- Able to customize query results in a uniform way.

### How to use

Create a schema:
```js
const mongoose = require('mongoose-extend');

const userSchema = new mongoose.BaseSchema({
    username: 'string',
    password: {type: String, select: false}
});

// register the model:
mongoose.model('User', userSchema, 'User');
// or
userSchema.mongoose.model('User', userSchema, 'User');
```

Override the queryDecorator:
```js
userSchema.defineStatics({
    queryDecorator: function(q){
        return q.select('-password');
    }
});

// the result objects will not have password attribute
mongoose.model('User').dFind();
```

Define a hook:
```js
userSchema.preHook('save', function(){
    // `this` is a model instance
    if(this.password) this.password = bcrypt.hashSync(this.password, 1);
});
```

### APIs

##### mongoose.BaseSchema
> Extend `mongoose.Schema`


#### BaseSchema APIs
##### schema.defineMethods
> Define Model instance methods

##### schema.defineStatics
> Define Model static methods

##### schema.preHook
> Handy function to define `pre` hook
##### schema.postHook
> Handy function to define `post` hook

##### schema.mongoose
> Equals to `mongoose` object 


#### Default model attributes
##### model.objectId
> Virtual, equals to `model._id`
##### model.disabled
##### model.createdAt
##### model.updatedAt


#### Decorated Model query methods(static)
Original query methods(`find`, `findOne`, etc) have corresponding decorated methods which applied `Model.queryDecorator` to the query object before return.
##### Model.dFind
##### Model.dFindOne

##### Model.queryDecorator
> Used in all decorated query methods

### ToDos
- More decorated functions
- Handle errors in preHook, postHook.
- Handle preHook, postHook as query middleware and document middleware.

---
_Source: https://npm.io/package/mongoose-extend · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
