Skip to content

Commit

Permalink
Merge pull request #588 from J3m5/service-module-getters-refactor
Browse files Browse the repository at this point in the history
service.module.getters refactor
  • Loading branch information
marshallswain authored Jul 3, 2021
2 parents 4ae03ba + 054a480 commit 7004220
Showing 1 changed file with 31 additions and 53 deletions.
84 changes: 31 additions & 53 deletions src/service-module/service-module.getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ eslint
@typescript-eslint/no-explicit-any: 0
*/
import sift from 'sift'
import { _ } from '@feathersjs/commons'
import { filterQuery, sorter, select } from '@feathersjs/adapter-commons'
import { globalModels as models } from './global-models'
import _get from 'lodash/get'
import _omit from 'lodash/omit'
import { isRef } from '@vue/composition-api'
import { unref } from '@vue/composition-api'
import { ServiceState } from '..'
import { Id } from '@feathersjs/feathers'

Expand All @@ -25,71 +23,59 @@ const getCopiesById = ({
if (keepCopiesInStore) {
return copiesById
} else {
const Model = _get(models, [serverAlias, 'byServicePath', servicePath])
const Model = models[serverAlias].byServicePath[servicePath]

return Model.copiesById
}
}

export default function makeServiceGetters() {
return {
list(state) {
return state.ids.map(id => state.keyedById[id])
},
find: state => params => {
if (isRef(params)) {
params = params.value
}
params = { ...params } || {}

// Set params.temps to true to include the tempsById records
params.temps = params.hasOwnProperty('temps') ? params.temps : false

// Set params.copies to true to include the copiesById records
params.copies = params.hasOwnProperty('copies') ? params.copies : false

const { paramsForServer, whitelist, keyedById } = state
list: state => Object.values(state.keyedById),
find: state => _params => {
const params = unref(_params) || {}

const {
paramsForServer,
whitelist,
keyedById,
idField,
tempsById
} = state
const q = _omit(params.query || {}, paramsForServer)

const { query, filters } = filterQuery(q, {
operators: additionalOperators.concat(whitelist)
})
let values = _.values(keyedById)

let values = Object.values(keyedById) as any

if (params.temps) {
values.push(..._.values(state.tempsById))
values.push(...(Object.values(tempsById) as any))
}

values = values.filter(sift(query))

if (params.copies) {
const { idField } = state
const copiesById = getCopiesById(state)
values.forEach((val, i, arr) => {
const copy = copiesById[val[idField]]
if (copy) {
// replace keyedById value with existing clone value
arr[i] = copy
}
})
// replace keyedById value with existing clone value
values = values.map(value => copiesById[value[idField]] || value)
}

const total = values.length

if (filters.$sort) {
if (filters.$sort !== undefined) {
values.sort(sorter(filters.$sort))
}

if (filters.$skip) {
values = values.slice(filters.$skip)
}

if (typeof filters.$limit !== 'undefined') {
values = values.slice(0, filters.$limit)
if (filters.$skip !== undefined && filters.$limit !== undefined) {
values = values.slice(filters.$skip, filters.$limit + filters.$skip)
} else if (filters.$skip !== undefined || filters.$limit !== undefined) {
values = values.slice(filters.$skip, filters.$limit)
}

if (filters.$select) {
values = values.map(value => _.pick(value, ...filters.$select.slice()))
values = select(params)(values)
}

return {
Expand All @@ -99,29 +85,21 @@ export default function makeServiceGetters() {
data: values
}
},
count: (state, getters) => params => {
if (isRef(params)) {
params = params.value
}
if (!params.query) {
throw 'params must contain a query-object'
}
count: (state, getters) => _params => {
const params = unref(_params) || {}

const cleanQuery = _omit(params.query, FILTERS)
params.query = cleanQuery

return getters.find(params).total
},
get: ({ keyedById, tempsById, idField, tempIdField }) => (
id,
params = {}
_id,
_params = {}
) => {
if (isRef(id)) {
id = id.value
}
if (isRef(params)) {
params = params.value
}
const id = unref(_id)
const params = unref(_params)

const record = keyedById[id] && select(params, idField)(keyedById[id])
if (record) {
return record
Expand Down

0 comments on commit 7004220

Please sign in to comment.