Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adapter-commons: add allowsMulti(method) to AdapterService #1431

Merged
merged 2 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions packages/adapter-commons/lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ const callMethod = (self, name, ...args) => {
return self[name](...args);
};

const checkMulti = (method, option) => {
if (option === true) {
return true;
}

return Array.isArray(option) ? option.includes(method) : false;
const alwaysMulti = {
find: true,
get: false,
update: false
};

module.exports = class AdapterService {
Expand Down Expand Up @@ -48,6 +46,22 @@ module.exports = class AdapterService {
return Object.assign(result, { paginate });
}

allowsMulti (method) {
const always = alwaysMulti[method];

if (typeof always !== 'undefined') {
return always;
}

const option = this.options.multi;

if (option === true || option === false) {
return option;
} else {
return option.includes(method);
}
}

find (params) {
return callMethod(this, '_find', params);
}
Expand All @@ -57,7 +71,7 @@ module.exports = class AdapterService {
}

create (data, params) {
if (Array.isArray(data) && !checkMulti('create', this.options.multi)) {
if (Array.isArray(data) && !this.allowsMulti('create')) {
return Promise.reject(new MethodNotAllowed(`Can not create multiple entries`));
}

Expand All @@ -75,15 +89,15 @@ module.exports = class AdapterService {
}

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

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

remove (id, params) {
if (id === null && !checkMulti('remove', this.options.multi)) {
if (id === null && !this.allowsMulti('remove')) {
return Promise.reject(new MethodNotAllowed(`Can not remove multiple entries`));
}

Expand Down
58 changes: 58 additions & 0 deletions packages/adapter-commons/test/service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,62 @@ describe('@feathersjs/adapter-commons/service', () => {
query: { $something: 'else' }
});
});

it('allowsMulti', () => {
context('with true', () => {
const service = new AdapterService({multi: true});

it('does return true for multible methodes', () => {
assert.equal(service.allowsMulti('patch'), true);
});

it('does return false for always non-multible methodes', () => {
assert.equal(service.allowsMulti('update'), false);
});

it('does return true for unknown methods', () => {
assert.equal(service.allowsMulti('other'), true);
});
});

context('with false', () => {
const service = new AdapterService({multi: false});

it('does return false for multible methodes', () => {
assert.equal(service.allowsMulti('remove'), false);
});

it('does return true for always multible methodes', () => {
assert.equal(service.allowsMulti('find'), true);
});

it('does return false for unknown methods', () => {
assert.equal(service.allowsMulti('other'), false);
});
});

context('with array', () => {
const service = new AdapterService({multi: ['create', 'get', 'other']});

it('does return true for specified multible methodes', () => {
assert.equal(service.allowsMulti('create'), true);
});

it('does return false for non-specified multible methodes', () => {
assert.equal(service.allowsMulti('patch'), false);
});

it('does return false for specified always multible methodes', () => {
assert.equal(service.allowsMulti('get'), false);
});

it('does return true for specified unknown methodes', () => {
assert.equal(service.allowsMulti('other'), true);
});

it('does return false for non-specified unknown methodes', () => {
assert.equal(service.allowsMulti('another'), false);
});
});
});
});