Skip to content

Commit

Permalink
feat: support for optional bulk update slug regeneration
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrodconnolly committed Mar 6, 2021
1 parent 1ad7192 commit b357502
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 29 deletions.
9 changes: 6 additions & 3 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ SequelizeSlugify.slugifyModel(User, {
column: 'slug',
incrementalSeparator: '-',
passTransaction: true,
paranoid: true
paranoid: true,
bulkUpdate: false
});
```
### Available Options
Expand All @@ -24,10 +25,12 @@ SequelizeSlugify.slugifyModel(User, {
| `slugOptions` | `{}` | | `{lower:true}` | Pass additional options for slug generation as defined by [`sluglife`][SL] |
| `overwrite` | `boolean` | | `true` | Update the slug if the source fields change after initial generation |
| `column` | `string` | | `slug` | Specify the column to store the slug value |
| `incrementalSeparator` | | | `-` | Specify the separator between the slug, and the duplicate count |
| `incrementalSeparator` | `string` | | `-` | Specify the separator between the slug, and the duplicate count |
| `passTransaction` | `boolean` | | `true` | Pass the current transaction object in to the plugin |
| `paranoid` | `boolean` | | `true` | Whether the duplication check will use a paranoid query or not, for determining the next unique slug. |

| `bulkUpdate` | `boolean` | | `false` | Automatically enable `individualHooks` during bulk updates. Read the following information: [Note-1][N1] [Note-2][N2] |


[SL]: https://github.com/jarrodconnolly/sluglife#options "Slug Life Options"
[N1]: https://sequelize.org/master/manual/hooks.html#model-hooks "individualHooks Note 1"
[N2]: https://sequelize.org/master/manual/hooks.html#many-to-many-associations "individualHooks Node 2"
58 changes: 34 additions & 24 deletions jest-tests/slug-bulk-update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
const SequelizeSlugify = require('../index');

let User;
// let userData;

describe('bulk update', () => {
beforeEach(async () => {
User = await global.generateModel();
// userData = global.createUser();
});

it('should create a slug during a bulk create', async () => {
Expand All @@ -23,26 +21,38 @@ describe('bulk update', () => {
expect(results[0].slug).toBe(`${user1.givenName.toLowerCase()}`);
expect(results[1].slug).toBe(`${user2.givenName.toLowerCase()}`);
});
});

// xit('should bulk update a slug from the Model', function () {
// SequelizeSlugify.slugifyModel(User, {
// source: ['givenName'],
// });
//
// return User.create({
// givenName: 'Woibrert',
// familyName: 'Hamazoni',
// }).then(function () {
// return User.update({
// givenName: 'Hazzah',
// }, {where: {givenName: 'Woibrert'}});
// }).then(function () {
// return User.findOne({givenName: 'Hazzah'});
// }).then(function (user) {
// return expect(user.slug).to.equal('hazzah');
// });
// });
//

// });
it('should bulk update a slug from the Model', async ()=> {
SequelizeSlugify.slugifyModel(User, {
source: ['givenName'],
bulkUpdate: true
});

const userData = global.createUser();
await User.create(userData);
const newGivenName = global.generateGivenName();

await User.update({givenName: newGivenName}, {where: {givenName: userData.givenName}});

const foundUser = await User.findOne({where:{givenName:newGivenName}});
expect(foundUser.slug).toBe(newGivenName.toLowerCase());
});

it('should bulk update with manual individualHooks', async ()=> {
SequelizeSlugify.slugifyModel(User, {
source: ['givenName'],
});

const userData = global.createUser();
await User.create(userData);
const newGivenName = global.generateGivenName();

await User.update({givenName: newGivenName}, {
where: {givenName: userData.givenName},
individualHooks: true
});

const foundUser = await User.findOne({where:{givenName:newGivenName}});
expect(foundUser.slug).toBe(newGivenName.toLowerCase());
});
});
16 changes: 14 additions & 2 deletions lib/sequelize-slugify.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class SequelizeSlugify {
column: 'slug',
incrementalSeparator: '-',
passTransaction: true,
paranoid: true
paranoid: true,
bulkUpdate: false
};

const slugifyOptions = {...DEFAULT_OPTIONS, ...options};
Expand Down Expand Up @@ -144,6 +145,13 @@ class SequelizeSlugify {
return Promise.all(instances.map(handleSlugify));
};

const handleBeforeBulkUpdate = function (sequelizeOptions) {
sequelizeOptions.individualHooks = true;
};
const handleAfterBulkUpdate = function (sequelizeOptions) {
sequelizeOptions.individualHooks = false;
};

model.prototype.regenerateSlug = function (transaction) {
const sequelizeOptions = {
transaction
Expand All @@ -154,8 +162,12 @@ class SequelizeSlugify {
// attach model callbacks
model.addHook('beforeCreate', handleSlugify);
model.addHook('beforeUpdate', handleSlugify);
//Model.addHook('beforeBulkUpdate', handleSlugifyBulk);
model.addHook('beforeBulkCreate', handleSlugifyBulkCreate);

if(slugifyOptions.bulkUpdate === true) {
model.addHook('beforeBulkUpdate', handleBeforeBulkUpdate);
model.addHook('afterBulkUpdate', handleAfterBulkUpdate);
}
}
}

Expand Down

0 comments on commit b357502

Please sign in to comment.