# bookshelf-mass-assignment

> A Bookshelf plugin that prevents mass-assignment vulnerabilities

Latest version **0.1.2** (published 2016-07-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install bookshelf-mass-assignment
pnpm add bookshelf-mass-assignment
yarn add bookshelf-mass-assignment
bun add bookshelf-mass-assignment
```

## 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.1.2 |
| Published | 2016-07-17 |
| First published | 2016-07-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Brian Tan |
| Maintainers | brtan |
| Keywords | bookshelf, nodejs, javascript |

## Links

- npm: https://www.npmjs.com/package/bookshelf-mass-assignment
- Repository: https://github.com/tanbrian/bookshelf-mass-assignment
- Homepage: https://github.com/tanbrian/bookshelf-mass-assignment#readme
- Issues: https://github.com/tanbrian/bookshelf-mass-assignment/issues
- npm.io page: https://npm.io/package/bookshelf-mass-assignment

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.13.1
- [bluebird](https://npm.io/package/bluebird.md) ^3.4.1

## Recent versions

- 0.1.2 (latest) — 2016-07-17
- 0.1.1 — 2016-07-17
- 0.1.0 — 2016-07-17
- 0.0.1 — 2016-07-17

## README

## bookshelf-mass-assignment

[![Build Status](https://travis-ci.org/tanbrian/bookshelf-mass-assignment.svg?branch=master)](https://travis-ci.org/tanbrian/bookshelf-mass-assignment)

A Bookshelf plugin that provides `fillable` and `guarded` properties on the model to prevent certain properties from being mass-assignable. Inspired by [Laravel's Eloquent ORM](https://laravel.com/docs/4.2/eloquent#mass-assignment).

### Installation

Install the module from `npm`:

```
npm install bookshelf-mass-assignment
```

### Usage

Initialize the plugin with:

```javascript
const bookshelf = require('bookshelf');
bookshelf.plugin('bookshelf-mass-assignment');
```

Then in your Bookshelf models, you can use the `fillable` or `guarded` properties.

```javascript
const User = bookshelf.Model.extend({
  tableName: 'users',

  // Specifies that only these user properties are mass-assignable.
  fillable: ['email', 'first_name', 'last_name']
});
```

The `fillable` property serves as a list of whitelisted user attributes that can be assigned to the model. Alternatively, you can use the `guarded` property to specify a blacklist of user attributes that cannot be assigned:

```javascript
const User = bookshelf.Model.extend({
  tableName: 'users',

  // Specifies that these properties are not mass-assignable.
  guarded: ['id', 'is_admin']
});
```

Then save the model as usual:

```javascript
new User().save({ first_name: 'Bob', is_admin: true })
  .then(user => console.log('Successfully saved user!'))
  .catch(err => console.log(err.message)); // Outputs: "Couldn't save model! Attributes are invalid."
```

If you don't want a hard error to be thrown when protected attributes are present, then add `silent: true` to the `options` object in `save`. This will ignore any attributes not in `fillable` if `fillable` is provided, or will ignore any attributes in `guarded` if `guarded` is provided:

```javascript
new User().save({ first_name: 'Bob', is_admin: true }, { silent: true })
  .then(user => console.log('Successfully saved user!')) // Only saved { first_name: 'Bob' }.
  .catch(err => console.log(err.message));
```

Note that you can only use either `fillable` or `guarded`, not both.

### License

Licensed under the terms of the [MIT License](LICENSE).

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