# objection-db-errors

> db-errors plugin for objection.js

Latest version **1.1.2** (published 2019-11-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install objection-db-errors
pnpm add objection-db-errors
yarn add objection-db-errors
bun add objection-db-errors
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2019-11-18 |
| First published | 2018-01-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 4.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 68 |
| Author | Sami Koskimäki |
| Maintainers | koskimas |

## Links

- npm: https://www.npmjs.com/package/objection-db-errors
- Repository: https://github.com/vincit/objection-db-errors
- Homepage: https://github.com/vincit/objection-db-errors#readme
- Issues: https://github.com/vincit/objection-db-errors/issues
- npm.io page: https://npm.io/package/objection-db-errors

## Dependencies (1)

- [db-errors](https://npm.io/package/db-errors.md) ^0.2.1

## Recent versions

- 1.1.2 (latest) — 2019-11-18
- 1.1.1 — 2019-04-08
- 1.1.0 — 2019-04-04
- 1.0.0 — 2018-01-11

## README

# [db-errors](https://github.com/Vincit/db-errors) plugin for objection

This plugin maps database errors using the [db-errors](https://github.com/Vincit/db-errors) library. By default
objection throws whatever the database client throws. The default errors are usually difficult to reason with.
This plugin along with the [db-errors](https://github.com/Vincit/db-errors) library try to provide more
manageable errors.

Every error class found in `require('db-errors')` can also be found in `require('objection-db-errors')`. See
[db-errors](https://github.com/Vincit/db-errors) for detailed documentation.

Error handling is something that should not need a plugin. The goal is to eventually merge this into objection core if
people find this plugin useful.

## Example usage

```js
const { Model } = require('objection');
const { DBErrors } = require('objection-db-errors');

// Usually you want to map each model class's errors. Easiest way to do this
// is to create a common superclass for all your models.
class BaseModel extends DBErrors(Model) {

}

module.exports = {
  BaseModel
};
```

```js
const { BaseModel } = require('./BaseModel')

class Person extends BaseModel {
  static get tableName() {
    return 'Person';
  }
}

module.exports = {
  Person
};
```

```js
const { UniqueViolationError } = require('objection-db-errors');

async function something() {
  await Person.query().insert({ id: 1 });

  try {
    // Try to insert a model with an existing primary key.
    await Person.query().insert({ id: 1 });
  } catch (err) {
    console.log(err instanceof UniqueViolationError); // true
  }
}
```

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