Standalone Data Stores
Creating a Data Store
import { useDataStore } from 'feathers-pinia'
import { createPinia, defineStore } from 'pinia'
const pinia = createPinia()
const useStore = defineStore('custom-tasks', () => {
const utils = useDataStore({
idField: 'id',
customSiftOperators: {},
setupInstance: (data: any, { api, service, servicePath }) => data
})
return { ...utils }
})
const store = useStore(pinia) // --> See API, below
// Adds HMR support
if (import.meta.hot)
import.meta.hot.accept(acceptHMRUpdate(useStore, import.meta.hot))
Similar to a Pinia store, the top-level of the store is a reactive
, which means nested computed
properties will be unwrapped, which means you don't have to access their contents using .value
, like you would with a Vue ref
or computed
, normally.
Options
idField
is the name of the unique identifier attribute on each record.customSiftOperators
is an optional object containing sift operators for using custom operators to query store data withfindInStore
.setupInstance
is a function that receives the instance data and allows you to customize it before returning it.
State
idField
will match theidField
option that you provided in the Model options.isSsr
exists as a utility attribute which can be used in custom store getters and actions.
Items
Items are records which have an unique idField.
itemsById
is an object used as the storage for items. They are keyed by idField to allow for quick lookup.items
is a dynamically-computed array which holds the list of all records initemsById
.itemIds
is a dynamically-computed array which holds all keys initemsById
.
Temps
Temporary records ("temps" for short) are records which are not created with an idField. They are automatically assigned a __tempId
during creation.
tempsById
is an object used as the storage for temps. They are keyed by__tempId
to allow for quick lookup.temps
is a dynamically-computed array which holds the list of all records intempsById
.tempIds
is a dynamically-computed array which holds all keys intempsById
.
Clones
Clones are copies of stored data from either items
or temps
. They have either an idField or a __tempId
.
clonesById
is an object used as the storage for clones. They are keyed by__cloneId
to allow for quick lookup.clones
is a dynamically-computed array which holds the list of all records inclonesById
.cloneIds
is a dynamically-computed array which holds all keys inclonesById
.clone(itemOrTemp)
creates a copy of theitemOrTemp
and stores it inclonesById
.commit(clone)
copies the keys from the providedclone
onto its original record initems
(ortemps
).reset(clone)
makes theclone
match the original record initems
(ortemps
).
The "new" Method
service.new(data)
Creates a new instance of this service's data type. Records are not automatically added to the store. You must call instance.createInStore()
to do that.
Storage Methods
The storage APIs include methods for adding data to and removing data from the internal storage. All of the below methods except clearAll
are also aliased directly on the Model. The clearAll
method is not aliased to make it explicitly obvious that you are clearing the store by calling Model.store.clearAll()
.
findInStore(params)
// returns reactive({ data, limit, skip, total })
service.findInStore(params)
Returns records from the store matching params.query
. The response is synchronous and always returns a results object with an array of data
. Paginated responses also include limit
, skip
, and total
. If you turn off pagination, only { data }
will be returned. All returned properties are computed properties.
findOneInStore(params)
// returns Computed<Record>
service.findOneInStore(params)
Returns the first record that matches params.query
. The response is synchronous and returns an object.
countInStore(params)
// returns Computed<number>
service.countInStore(params)
Returns the number of records in the store which match params.query
.
getFromStore(id, params)
getFromStore(id, params) => Computed<Record>
returns the record from the store with matchingid
, or returnsnull
if a record is not found.
createInStore(data)
service.createInStore(record)
service.createInStore(record as Id[])
Adds the data object or array to the correct internal storage (items or temps), depending on if an idField is present.
patchInStore(idOrItems, data, params)
// several overrides
service.patchInStore(id, data)
service.patchInStore(id as Id[], data)
service.patchInStore(item, data)
service.patchInStore(item as Record<string, any>[], data)
service.patchInStore(null, data, paramsWithQuery)
Updates each of the provided items or items that match the ids with the provided data.
removeFromStore(idOrItems, params)
// several overrides
service.removeFromStore(id, params)
service.removeFromStore(id as Id[], params)
service.removeFromStore(record, params)
service.removeFromStore(record as Record<string, any>[], params)
service.removeFromStore(null, paramsWithQuery)
Removes provided items or items that match provided ids from the store. You can also pass null
as the first argument and remove items based on a query.
clearAll()
service.clearAll()
Removes all stored items
, temps
, and clones
from the store.