Skip to content

Commit e688851

Browse files
vonagamdaffl
authored andcommitted
feat: adapter-commons: add allowsMulti(method) to AdapterService (#1431)
1 parent b5ee7e2 commit e688851

File tree

2 files changed

+81
-9
lines changed

2 files changed

+81
-9
lines changed

packages/adapter-commons/lib/service.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ const callMethod = (self, name, ...args) => {
99
return self[name](...args);
1010
};
1111

12-
const checkMulti = (method, option) => {
13-
if (option === true) {
14-
return true;
15-
}
16-
17-
return Array.isArray(option) ? option.includes(method) : false;
12+
const alwaysMulti = {
13+
find: true,
14+
get: false,
15+
update: false
1816
};
1917

2018
module.exports = class AdapterService {
@@ -48,6 +46,22 @@ module.exports = class AdapterService {
4846
return Object.assign(result, { paginate });
4947
}
5048

49+
allowsMulti (method) {
50+
const always = alwaysMulti[method];
51+
52+
if (typeof always !== 'undefined') {
53+
return always;
54+
}
55+
56+
const option = this.options.multi;
57+
58+
if (option === true || option === false) {
59+
return option;
60+
} else {
61+
return option.includes(method);
62+
}
63+
}
64+
5165
find (params) {
5266
return callMethod(this, '_find', params);
5367
}
@@ -57,7 +71,7 @@ module.exports = class AdapterService {
5771
}
5872

5973
create (data, params) {
60-
if (Array.isArray(data) && !checkMulti('create', this.options.multi)) {
74+
if (Array.isArray(data) && !this.allowsMulti('create')) {
6175
return Promise.reject(new MethodNotAllowed(`Can not create multiple entries`));
6276
}
6377

@@ -75,15 +89,15 @@ module.exports = class AdapterService {
7589
}
7690

7791
patch (id, data, params) {
78-
if (id === null && !checkMulti('patch', this.options.multi)) {
92+
if (id === null && !this.allowsMulti('patch')) {
7993
return Promise.reject(new MethodNotAllowed(`Can not patch multiple entries`));
8094
}
8195

8296
return callMethod(this, '_patch', id, data, params);
8397
}
8498

8599
remove (id, params) {
86-
if (id === null && !checkMulti('remove', this.options.multi)) {
100+
if (id === null && !this.allowsMulti('remove')) {
87101
return Promise.reject(new MethodNotAllowed(`Can not remove multiple entries`));
88102
}
89103

packages/adapter-commons/test/service.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,62 @@ describe('@feathersjs/adapter-commons/service', () => {
133133
query: { $something: 'else' }
134134
});
135135
});
136+
137+
it('allowsMulti', () => {
138+
context('with true', () => {
139+
const service = new AdapterService({multi: true});
140+
141+
it('does return true for multible methodes', () => {
142+
assert.equal(service.allowsMulti('patch'), true);
143+
});
144+
145+
it('does return false for always non-multible methodes', () => {
146+
assert.equal(service.allowsMulti('update'), false);
147+
});
148+
149+
it('does return true for unknown methods', () => {
150+
assert.equal(service.allowsMulti('other'), true);
151+
});
152+
});
153+
154+
context('with false', () => {
155+
const service = new AdapterService({multi: false});
156+
157+
it('does return false for multible methodes', () => {
158+
assert.equal(service.allowsMulti('remove'), false);
159+
});
160+
161+
it('does return true for always multible methodes', () => {
162+
assert.equal(service.allowsMulti('find'), true);
163+
});
164+
165+
it('does return false for unknown methods', () => {
166+
assert.equal(service.allowsMulti('other'), false);
167+
});
168+
});
169+
170+
context('with array', () => {
171+
const service = new AdapterService({multi: ['create', 'get', 'other']});
172+
173+
it('does return true for specified multible methodes', () => {
174+
assert.equal(service.allowsMulti('create'), true);
175+
});
176+
177+
it('does return false for non-specified multible methodes', () => {
178+
assert.equal(service.allowsMulti('patch'), false);
179+
});
180+
181+
it('does return false for specified always multible methodes', () => {
182+
assert.equal(service.allowsMulti('get'), false);
183+
});
184+
185+
it('does return true for specified unknown methodes', () => {
186+
assert.equal(service.allowsMulti('other'), true);
187+
});
188+
189+
it('does return false for non-specified unknown methodes', () => {
190+
assert.equal(service.allowsMulti('another'), false);
191+
});
192+
});
193+
});
136194
});

0 commit comments

Comments
 (0)