From a5db6a8b2ea922b93a93ece1b61623bcd404f203 Mon Sep 17 00:00:00 2001 From: Yavor Stoychev Date: Fri, 2 Jul 2021 13:59:51 +0300 Subject: [PATCH 1/5] paramsForServer as key plus value or result of a function --- src/service-module/service-module.getters.ts | 86 ++++++++++++------- .../service-module.getters.test.ts | 39 +++++++++ 2 files changed, 96 insertions(+), 29 deletions(-) diff --git a/src/service-module/service-module.getters.ts b/src/service-module/service-module.getters.ts index e7374984..980cbdcb 100644 --- a/src/service-module/service-module.getters.ts +++ b/src/service-module/service-module.getters.ts @@ -48,8 +48,29 @@ export default function makeServiceGetters() { // Set params.copies to true to include the copiesById records params.copies = params.hasOwnProperty('copies') ? params.copies : false - const { paramsForServer, whitelist, keyedById } = state - const q = _omit(params.query || {}, paramsForServer) + const { paramsForServer = [], whitelist, keyedById } = state + + const paramsForServerByValue = paramsForServer.filter(el => Array.isArray(el)) + + const q = paramsForServerByValue.reduce( + (acc, [key, filter]) => { + if (!acc[key]) return acc + + if ( + ((typeof filter === 'string' || typeof filter === 'number') && + acc[key] === filter) || + (typeof filter === 'function' && filter(acc[key])) + ) { + return _omit(acc, key) + } + + return acc + }, + _omit( + params.query || {}, + paramsForServer.filter(el => typeof el === 'string') + ) + ) const { query, filters } = filterQuery(q, { operators: additionalOperators.concat(whitelist) @@ -112,38 +133,45 @@ export default function makeServiceGetters() { return getters.find(params).total }, - get: ({ keyedById, tempsById, idField, tempIdField }) => ( - id, - params = {} - ) => { - if (isRef(id)) { - id = id.value - } - if (isRef(params)) { - params = params.value - } - const record = keyedById[id] && select(params, idField)(keyedById[id]) - if (record) { - return record - } - const tempRecord = - tempsById[id] && select(params, tempIdField)(tempsById[id]) - - return tempRecord || null - }, + get: + ({ keyedById, tempsById, idField, tempIdField }) => + (id, params = {}) => { + if (isRef(id)) { + id = id.value + } + if (isRef(params)) { + params = params.value + } + const record = keyedById[id] && select(params, idField)(keyedById[id]) + if (record) { + return record + } + const tempRecord = + tempsById[id] && select(params, tempIdField)(tempsById[id]) + + return tempRecord || null + }, getCopyById: state => id => { const copiesById = getCopiesById(state) return copiesById[id] }, - isCreatePendingById: ({ isIdCreatePending }: ServiceState) => (id: Id) => - isIdCreatePending.includes(id), - isUpdatePendingById: ({ isIdUpdatePending }: ServiceState) => (id: Id) => - isIdUpdatePending.includes(id), - isPatchPendingById: ({ isIdPatchPending }: ServiceState) => (id: Id) => - isIdPatchPending.includes(id), - isRemovePendingById: ({ isIdRemovePending }: ServiceState) => (id: Id) => - isIdRemovePending.includes(id), + isCreatePendingById: + ({ isIdCreatePending }: ServiceState) => + (id: Id) => + isIdCreatePending.includes(id), + isUpdatePendingById: + ({ isIdUpdatePending }: ServiceState) => + (id: Id) => + isIdUpdatePending.includes(id), + isPatchPendingById: + ({ isIdPatchPending }: ServiceState) => + (id: Id) => + isIdPatchPending.includes(id), + isRemovePendingById: + ({ isIdRemovePending }: ServiceState) => + (id: Id) => + isIdRemovePending.includes(id), isSavePendingById: (state: ServiceState, getters) => (id: Id) => getters.isCreatePendingById(id) || getters.isUpdatePendingById(id) || diff --git a/test/service-module/service-module.getters.test.ts b/test/service-module/service-module.getters.test.ts index 83e4c0b1..ea897f13 100644 --- a/test/service-module/service-module.getters.test.ts +++ b/test/service-module/service-module.getters.test.ts @@ -351,6 +351,45 @@ describe('Service Module - Getters', function () { assert(results.total === 1, 'total was correct') }) + it('find with paramsForServer option with specific value as string', function () { + const { state } = this + state.paramsForServer = [['_$client', '1']] + const params = { query: { test: false, _$client: '1' } } + const results = find(state)(params) + + assert(results.data.length === 1, 'the length was correct') + assert(results.data[0]._id === 3, 'the correct record was returned') + assert(results.limit === 0, 'limit was correct') + assert(results.skip === 0, 'skip was correct') + assert(results.total === 1, 'total was correct') + }) + + it('find with paramsForServer option with specific value as number', function () { + const { state } = this + state.paramsForServer = [['_$client', -1]] + const params = { query: { test: false, _$client: -1 } } + const results = find(state)(params) + + assert(results.data.length === 1, 'the length was correct') + assert(results.data[0]._id === 3, 'the correct record was returned') + assert(results.limit === 0, 'limit was correct') + assert(results.skip === 0, 'skip was correct') + assert(results.total === 1, 'total was correct') + }) + + it('find with paramsForServer option with specific value as function', function () { + const { state } = this + state.paramsForServer = [['_$client', value => value === 1]] + const params = { query: { test: false, _$client: 1 } } + const results = find(state)(params) + + assert(results.data.length === 1, 'the length was correct') + assert(results.data[0]._id === 3, 'the correct record was returned') + assert(results.limit === 0, 'limit was correct') + assert(results.skip === 0, 'skip was correct') + assert(results.total === 1, 'total was correct') + }) + it('find with non-whitelisted custom operator fails', function () { const { state } = this const params = { query: { $client: 'test' } } From 496d225740f35db9d83b14386c8f1d1418ffd9c8 Mon Sep 17 00:00:00 2001 From: Yavor Stoychev Date: Fri, 2 Jul 2021 14:22:52 +0300 Subject: [PATCH 2/5] skip the filtering if there is not filter provided --- src/service-module/service-module.getters.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/service-module/service-module.getters.ts b/src/service-module/service-module.getters.ts index 980cbdcb..20a5aa4d 100644 --- a/src/service-module/service-module.getters.ts +++ b/src/service-module/service-module.getters.ts @@ -50,11 +50,13 @@ export default function makeServiceGetters() { const { paramsForServer = [], whitelist, keyedById } = state - const paramsForServerByValue = paramsForServer.filter(el => Array.isArray(el)) + const paramsForServerByValue = paramsForServer.filter(el => + Array.isArray(el) + ) const q = paramsForServerByValue.reduce( (acc, [key, filter]) => { - if (!acc[key]) return acc + if (!acc[key] || (acc[key] && !filter)) return acc if ( ((typeof filter === 'string' || typeof filter === 'number') && From 8655d435a2b4feaf2fac7e216cd3286c2c03758c Mon Sep 17 00:00:00 2001 From: Yavor Stoychev Date: Fri, 2 Jul 2021 14:42:57 +0300 Subject: [PATCH 3/5] 3.17.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index bdc38732..0bc91251 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "feathers-vuex", - "version": "3.16.0", + "version": "3.17.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "3.16.0", + "version": "3.17.0", "license": "MIT", "dependencies": { "@feathersjs/adapter-commons": "^4.5.2", diff --git a/package.json b/package.json index f1c12a76..dc4830bb 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "feathers-vuex", "description": "FeathersJS, Vue, and Nuxt for the artisan developer", - "version": "3.16.0", + "version": "3.17.0", "homepage": "https:feathers-vuex.feathersjs-ecosystem.com", "main": "dist/", "module": "dist/", From 848371e78af04b6b3cb315e02f5c8b03bd3f6ce7 Mon Sep 17 00:00:00 2001 From: Yavor Stoychev Date: Fri, 2 Jul 2021 14:43:46 +0300 Subject: [PATCH 4/5] 3.17.1-0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0bc91251..00279dee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "feathers-vuex", - "version": "3.17.0", + "version": "3.17.1-0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "3.17.0", + "version": "3.17.1-0", "license": "MIT", "dependencies": { "@feathersjs/adapter-commons": "^4.5.2", diff --git a/package.json b/package.json index dc4830bb..b4aaac2a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "feathers-vuex", "description": "FeathersJS, Vue, and Nuxt for the artisan developer", - "version": "3.17.0", + "version": "3.17.1-0", "homepage": "https:feathers-vuex.feathersjs-ecosystem.com", "main": "dist/", "module": "dist/", From 9441dd3ef74dd767d9c6b1cb3676a202aa2b3064 Mon Sep 17 00:00:00 2001 From: Yavor Stoychev Date: Fri, 2 Jul 2021 15:22:19 +0300 Subject: [PATCH 5/5] filter parameter renamed to valueOrPredicate --- src/service-module/service-module.getters.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/service-module/service-module.getters.ts b/src/service-module/service-module.getters.ts index 20a5aa4d..ecf54865 100644 --- a/src/service-module/service-module.getters.ts +++ b/src/service-module/service-module.getters.ts @@ -55,13 +55,15 @@ export default function makeServiceGetters() { ) const q = paramsForServerByValue.reduce( - (acc, [key, filter]) => { - if (!acc[key] || (acc[key] && !filter)) return acc + (acc, [key, valueOrPredicate]) => { + if (!acc[key] || (acc[key] && !valueOrPredicate)) return acc if ( - ((typeof filter === 'string' || typeof filter === 'number') && - acc[key] === filter) || - (typeof filter === 'function' && filter(acc[key])) + ((typeof valueOrPredicate === 'string' || + typeof valueOrPredicate === 'number') && + acc[key] === valueOrPredicate) || + (typeof valueOrPredicate === 'function' && + valueOrPredicate(acc[key])) ) { return _omit(acc, key) }