Skip to content

Commit c1bc12b

Browse files
authored
[+] Resources Remover - Add resources remover (bulk destroy) (#293)
1 parent a07db20 commit c1bc12b

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44
### Added
55
- Technical - Add optional chaining plugin to babel.
6+
- Resources Remover - Add resources remover (bulk destroy).
67

78
### Changed
89
- Technical - Upgrade to babel 7 stable.

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ exports.init = (opts) => {
6363
exports.ResourceUpdater = require('./services/resource-updater');
6464
exports.ResourceRemover = require('./services/resource-remover');
6565
exports.ResourcesExporter = require('./services/resources-exporter');
66+
exports.ResourcesRemover = require('./services/resources-remover');
6667
exports.EmbeddedDocumentUpdater = require('./services/embedded-document-updater');
6768

6869
exports.HasManyGetter = require('./services/has-many-getter');

src/services/errors.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,13 @@ function InvalidFiltersFormatError(message) {
1212
}
1313
InvalidFiltersFormatError.prototype = new Error();
1414

15+
function InvalidParameterError(message) {
16+
this.name = 'InvalidParameterError';
17+
this.message = message || 'The given parameter is invalid.';
18+
this.stack = (new Error()).stack;
19+
}
20+
InvalidParameterError.prototype = new Error();
21+
1522
exports.NoMatchingOperatorError = NoMatchingOperatorError;
1623
exports.InvalidFiltersFormatError = InvalidFiltersFormatError;
24+
exports.InvalidParameterError = InvalidParameterError;

src/services/resources-remover.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { InvalidParameterError } = require('./errors');
2+
3+
function ResourcesRemover(model, ids) {
4+
this.perform = () => {
5+
if (!Array.isArray(ids) || !ids.length) {
6+
throw new InvalidParameterError('`ids` must be a non-empty array.');
7+
}
8+
9+
return model.deleteMany({ _id: ids });
10+
};
11+
}
12+
13+
module.exports = ResourcesRemover;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import mongoose from 'mongoose';
2+
import loadFixture from 'mongoose-fixture-loader';
3+
import Interface from 'forest-express';
4+
import ResourcesRemover from '../../../src/services/resources-remover';
5+
import mongooseConnect from '../../utils/mongoose-connect';
6+
import { InvalidParameterError } from '../../../src/services/errors';
7+
8+
describe('service > resources-remover', () => {
9+
let IslandModel;
10+
11+
beforeAll(() => {
12+
Interface.Schemas = {
13+
schemas: {
14+
Island: {
15+
name: 'Island',
16+
idField: '_id',
17+
searchFields: ['name'],
18+
fields: [
19+
{ field: '_id', type: 'String' },
20+
{ field: 'name', type: 'String' },
21+
],
22+
},
23+
},
24+
};
25+
26+
return mongooseConnect()
27+
.then(() => {
28+
const IslandSchema = mongoose.Schema({
29+
name: { type: String },
30+
});
31+
32+
IslandModel = mongoose.model('Island', IslandSchema);
33+
return IslandModel.remove({});
34+
});
35+
});
36+
37+
afterAll(() => mongoose.connection.close());
38+
39+
beforeEach(async () => {
40+
await IslandModel.remove({});
41+
await loadFixture(IslandModel, [
42+
{ name: 'Kauai', _id: '56cb91bdc3464f14678934ca' },
43+
{ name: 'Oahu', _id: '56cb91bdc3464f14678934cb' },
44+
]);
45+
});
46+
47+
48+
it('should throw error if ids is not an array or empty', () => {
49+
expect.assertions(3);
50+
expect(() => new ResourcesRemover(null, []).perform()).toThrow(InvalidParameterError);
51+
expect(() => new ResourcesRemover(null, 'foo').perform()).toThrow(InvalidParameterError);
52+
expect(() => new ResourcesRemover(null, {}).perform()).toThrow(InvalidParameterError);
53+
});
54+
55+
it('should remove one resource with existing ID', async () => {
56+
expect.assertions(1);
57+
await new ResourcesRemover(IslandModel, ['56cb91bdc3464f14678934ca']).perform();
58+
const documentsCount = await IslandModel.countDocuments();
59+
expect(documentsCount).toStrictEqual(1);
60+
});
61+
62+
it('should remove all resources with existing ID', async () => {
63+
expect.assertions(1);
64+
await new ResourcesRemover(IslandModel, ['56cb91bdc3464f14678934ca', '56cb91bdc3464f14678934cb']).perform();
65+
const documentsCount = await IslandModel.countDocuments();
66+
expect(documentsCount).toStrictEqual(0);
67+
});
68+
69+
it('should not remove resource with not existing ID', async () => {
70+
expect.assertions(1);
71+
await new ResourcesRemover(IslandModel, ['56cb91bdc3464f14678934cd']).perform();
72+
const documentsCount = await IslandModel.countDocuments();
73+
expect(documentsCount).toStrictEqual(2);
74+
});
75+
});

0 commit comments

Comments
 (0)