https://github.com/seegno/objection-guid
Automatic guid for Objection.js
https://github.com/seegno/objection-guid
guid id objection objection-guid objectionjs plugin plugins
Last synced: 5 months ago
JSON representation
Automatic guid for Objection.js
- Host: GitHub
- URL: https://github.com/seegno/objection-guid
- Owner: seegno
- Created: 2017-07-05T17:08:06.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2022-02-03T15:01:10.000Z (over 4 years ago)
- Last Synced: 2025-10-25T06:08:21.026Z (8 months ago)
- Topics: guid, id, objection, objection-guid, objectionjs, plugin, plugins
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/objection-guid
- Size: 93.8 KB
- Stars: 35
- Watchers: 2
- Forks: 10
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Automatic guid for Objection.js
[](https://npmjs.org/package/objection-guid)

[](https://travis-ci.org/seegno/objection-guid)
[](https://coveralls.io/github/seegno/objection-guid?branch=master)
This plugin adds an automatic guid to your [Objection.js](https://github.com/Vincit/objection.js/) models using the [uuid](https://github.com/kelektiv/node-uuid) package for guid generation.
## Installation
### NPM
```sh
npm i objection-guid
```
### Yarn
```sh
yarn add objection-guid
```
## Usage
### Generate guid
```js
// Import the plugin.
const guid = require('objection-guid')();
const Model = require('objection').Model;
// Mixin the plugin.
class Item extends guid(Model) {
static get tableName() {
return 'Item';
}
}
const item = await Item.query().insert({
name: 'foo'
});
console.log(item.id);
// bbbe64b0-61a3-11e7-879a-67bb027591aa
```
### Custom values
If your model already has a value for the configured field, the value is not overwritten.
## Options
**field:** Overrides the default field name of the generated guid. (Default: `id`)
**generateGuid:** Overrides the default function that generates a guid. This function can be a promise. (Default: generates UUIDs v4)
These options can be provided when instantiating the plugin:
```js
const guid = require('objection-guid')({
field: 'foo',
generateGuid: () => 'bar'
});
```
## Tests
Run the tests from the root directory:
```sh
npm test
```
You can find Jest documentation [here](https://facebook.github.io/jest/docs/getting-started.html).
## Contributing & Development
#### Contributing
Found a bug or want to suggest something? Take a look first on the current and closed [issues](https://github.com/seegno/objection-guid/issues). If it is something new, please [submit an issue](https://github.com/seegno/objection-guid/issues/new).
#### Develop
It will be awesome if you can help us evolve `objection-guid`. Want to help?
1. [Fork it](https://github.com/seegno/objection-guid).
2. `npm install`.
3. Hack away.
4. Run the tests: `npm test`.
5. Create a [Pull Request](https://github.com/seegno/objection-guid/compare).