This plugin adds an automatic guid to your Objection.js models using the uuid package for guid generation.
npm i objection-guid
yarn add objection-guid
// 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
If your model already has a value for the configured field, the value is not overwritten.
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:
const guid = require('objection-guid')({
field: 'foo',
generateGuid: () => 'bar'
});
Run the tests from the root directory:
npm test
You can find Jest documentation here.
Found a bug or want to suggest something? Take a look first on the current and closed issues. If it is something new, please submit an issue.
It will be awesome if you can help us evolve objection-guid
. Want to help?
- Fork it.
npm install
.- Hack away.
- Run the tests:
npm test
. - Create a Pull Request.