# bookshelf-registry

> A registry module for bookshelf.js

Latest version **1.0.0** (published 2015-11-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install bookshelf-registry
pnpm add bookshelf-registry
yarn add bookshelf-registry
bun add bookshelf-registry
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2015-11-14 |
| First published | 2015-11-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ryan Tablada |
| Maintainers | rtablada |
| Keywords | orm, bookshelf |

## Links

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

## Dependencies (2)

- [knex](https://npm.io/package/knex.md) ^0.9.0
- [bookshelf](https://npm.io/package/bookshelf.md) ^0.9.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2015-11-14

## README

# Bookshelf Registry

This package allows for a quick and simple syntax for looking up a KNEX connection and Bookshelf.js models.
This uses Node's `require` to prevent passing around a global connection object.

## Setting a Default KNEX Connection

The first part of the setup for the registry is to set the KNEX connection.
So for instance to use a sqlite connection:

```js
const registry = require('bookshelf-registry');

const knex = require('knex')({
  client: 'sqlite3',
  connection: {
    filename: `${__dirname}/dummy.sqlite`,
  },
});

registry.setConnection(knex);
```

This will set up connection that can be used to look up the connection, but more than that in a bit.
This also creates a new bookshelf instance that can be looked up as well.

## Getting the KNEX Connection

Just like the `setConnection`, looking up the existing connection is fairly simple.

```js
const registry = require('bookshelf-registry');

const knex = registry.getConnection();
```

Since node's `require` function is a shared singleton, looking up this connection can be anywhere in your codebase after `setConnection` has been executed.

## Defining Models

The only other API that this registry provides is the ability to create and look up models.
So to start, to create a `User` model:

```js
const registry = require('bookshelf-registry');

const UserModel = registry.model('User', {
  tableName: 'users',
});
```

If this looks just like Bookshelf, that's because it is.

## Grabbing Models

What if you want to use that User model somewhere else in your app?
Just use the model function to grab that defined user model:

```js
const registry = require('bookshelf-registry');

const UserModel = registry.model('User');

new UserModel({email: 'test@bookshelf.com'}).save().then(() => {
  cb();
});
```

## Contributing

I always love contributions, thoughts etc.
Please follow the [Contribution Guidelines](CONTRIBUTING.md)

## License

This library is distributed using the MIT License.
For more information see the [full license](LICENSE.md).

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