# captain-hook

> Pre- and Post- Create and Update Hooks for Mongoose ODM

Latest version **0.0.3** (published 2014-09-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install captain-hook
pnpm add captain-hook
yarn add captain-hook
bun add captain-hook
```

## 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.0.3 |
| Published | 2014-09-19 |
| First published | 2014-09-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Nathan Hackley |
| Maintainers | nathanhackley |
| Keywords | mongoose, odm, hooks, pre, post, create, update, plugin |

## Links

- npm: https://www.npmjs.com/package/captain-hook
- Repository: https://github.com/nathanhackley/captain-hook
- Issues: https://github.com/nathanhackley/captain-hook/issues
- npm.io page: https://npm.io/package/captain-hook

## Dependencies (1)

- [async](https://npm.io/package/async.md) ~0.9.0

## 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
- [@temporal-architect/claude-plugin](https://npm.io/package/@temporal-architect/claude-plugin.md) — 335 weekly downloads

## Recent versions

- 0.0.3 (latest) — 2014-09-19
- 0.0.2 — 2014-09-08
- 0.0.1 — 2014-09-05

## README

Captain Hook
============

*Pre- and Post- Create and Update Hooks for Mongoose ODM.*

[![Code Climate](https://codeclimate.com/github/nathanhackley/captain-hook/badges/gpa.svg)](https://codeclimate.com/github/nathanhackley/captain-hook)

#### Use-Case
Out of the box, Mongoose provides you with the handy `pre('save')` and `post('save')` methods. Unfortunately, these hooks run every time the instance is saved, whether it is being created or updated. Captain Hook extends Mongoose to give you greater control over this functionality.

## Usage

`npm install captain-hook --save`

In your Mongoose model:
```js
var captainHook  = require('captain-hook');
var userSchema = mongoose.Schema();

userSchema.plugin(captainHook);

// function to run before saving a new user instance
userSchema.preCreate(function(user, next){
  console.log("You are about to create a user:" + user.email);
  next();
})

// function to run after saving a new user instance
userSchema.postCreate(function(user, next){
  console.log("You just created a user:" + user.email);
  next();
})

// function to run before updating an existing user instance
userSchema.preUpdate(function(user, next){
  console.log("You are about to update a user:" + user.email);
  next();
})

// function to run before updating an existing user instance
userSchema.postUpdate(function(user, next){
  console.log("You just updated a user:" + user.email);
  next();
})

```

Multiple pre- and post- methods can be added for each timing (preCreate, postCreate, preUpdate, postUpdate) and the functions will be run in the order they are defined.

The `next()` callback must be called for every hook.

-------------------

### TO DO:
- Tests for hooks firing in the correct order
- More granular unit tests
    - schema.preCreate()
    - schema.methods.runPreCreate(), etc
    - schema.preCreateMethods array

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