feathers icon indicating copy to clipboard operation
feathers copied to clipboard

Memory - add selector option

Open DaddyWarbucks opened this issue 1 year ago • 0 comments

The memory package (and maybe other DB adapters?) should take a selector option. This service already takes a matcher and sorter option.

See: https://github.com/feathersjs/feathers/blob/09519fb1c454ea718747a09797bc6dfd3b6a79a3/packages/memory/src/index.ts#L25

My main use case is not wanting to JSON stringify/parse the results in large datasets to decrease the memory footprint. This particular app uses localStorage, disabled pagination, does not use $select, and has many thousands of records.

There are a number of other valuable uses cases such as using dot.notation in $select, not stringify-ing the results keeps Dates, etc.

// Return the referencial value. When using $select, you will get copies of the data because `base`
// will return a selected copy. But when not using $select it returns a reference to OG data.
// Beware mutability...but I am willing to take that risk in this case.
const selector = (data: any, params: any, ...args: string[]) => {
  const base = select(params, ...args)
  return base(data)
}

// Freeze the data
const selector = (data: any, params: any, ...args: string[]) => {
  const base = select(params, ...args)
  return Object.freeze({ ...base(data) })
}

// Custom dot.notation selection
const selector = (data: any, params: any, ...args: string[]) => {
  const base = selectWithDotNotation(params, ...args)
  return base(data)
}

I will try to open a PR for this when I get a chance.

DaddyWarbucks avatar Mar 26 '24 21:03 DaddyWarbucks