Skip to content

Commit

Permalink
Merge pull request #12 from seegno/feature/plugin-options
Browse files Browse the repository at this point in the history
Add field and generateGuid plugin options
  • Loading branch information
nunorafaelrocha authored Jul 11, 2017
2 parents afd18f3 + e7d100e commit cb43052
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 44 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ yarn add objection-guid

```js
// Import the plugin.
const guid = require('objection-guid');
const guid = require('objection-guid')();
const Model = require('objection').Model;

// Mixin the plugin.
Expand All @@ -45,6 +45,21 @@ console.log(item.id);
// bbbe64b0-61a3-11e7-879a-67bb027591aa
```

## 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:
Expand Down
33 changes: 21 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,31 @@
const uuid = require('uuid');

/**
* Export `guid`
* Export `guid`.
*/

module.exports = Model => {
return class GuidIdPlugin extends Model {
module.exports = options => {
options = Object.assign({
field: 'id',
generateGuid: () => uuid.v4()
}, options);

/**
* Before insert.
*/
return Model => {
return class GuidIdPlugin extends Model {

$beforeInsert(context) {
const parent = super.$beforeInsert(context);
/**
* Before insert.
*/

return Promise.resolve(parent).then(() => {
this.id = uuid.v1();
});
}
$beforeInsert(context) {
const parent = super.$beforeInsert(context);

return Promise.resolve(parent)
.then(() => options.generateGuid(context))
.then(guid => {
this[options.field] = guid;
});
}
};
};
};
23 changes: 0 additions & 23 deletions test/fixtures/test-model.js

This file was deleted.

48 changes: 40 additions & 8 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,60 @@
'use strict';

/**
* Jest mocks.
*/

jest.mock('uuid', () => ({ v4: jest.fn(() => 'foobar') }));

/**
* Module dependencies.
*/

const TestModel = require('./fixtures/test-model');
const modelFactory = require('./utils/model-factory');

/**
* Test `guid` plugin.
*/

describe('GuidIdPlugin', () => {
let model;
describe('$beforeInsert', () => {
it('should add the `id` property to the model with the default value of generated guid', () => {
const model = modelFactory();
const parent = model.$beforeInsert();

beforeEach(() => {
model = new TestModel();
});
return Promise.resolve(parent).then(() => {
expect(model).toEqual({ id: 'foobar' });
});
});

it('should add the property provided as an option to the model', () => {
const model = modelFactory({ field: 'foo' });
const parent = model.$beforeInsert();

return Promise.resolve(parent).then(() => {
expect(model).toEqual({ foo: expect.any(String) });
});
});

it('should add the `id` property to the model and set the value as the result of the `generateGuid` function provided as an option', () => {
const model = modelFactory({ generateGuid: () => 'foo' });
const parent = model.$beforeInsert();

return Promise.resolve(parent).then(() => {
expect(model).toEqual({ id: 'foo' });
});
});

it('should add the property provided as option to the model and set the value as the result of the `generateGuid` function provided as an option', () => {
const model = modelFactory({
field: 'foo',
generateGuid: () => new Promise(resolve => resolve('bar'))
});

describe('$beforeInsert', () => {
it('should add `id` properties to the model', () => {
const parent = model.$beforeInsert();

return Promise.resolve(parent).then(() => {
expect(model).toEqual({ id: expect.any(String) });
expect(model).toEqual({ foo: 'bar' });
});
});
});
Expand Down
19 changes: 19 additions & 0 deletions test/utils/model-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

/**
* Module dependencies.
*/

const Model = require('objection').Model;

/**
* Export `TestModel`.
*/

module.exports = options => {
const guid = require('../../index')(options);

class TestModel extends guid(Model) {}

return new TestModel();
};

0 comments on commit cb43052

Please sign in to comment.