v2.0.0-pre.18
BaseModel Stores
Model Functions come with their own built-in stores. The BaseModel store API is a subset of the FeathersModel store.
Related reading:
Creating a BaseModel Store
BaseModel stores are created only when you create a BaseModel Function using useBaseModel. The default store is found at Model.store
:
Note about Feathers Types
Replace my-feathers-api
in the below example with the package installed from your Feathers v5 Dove API. You can also provide manual types that describe the shape of your data.
ts
import type { Tasks, TasksData, TasksQuery } from 'my-feathers-api'
import { type ModelInstance, useBaseModel, useInstanceDefaults } from 'feathers-pinia'
const modelFn = (data: ModelInstance<Tasks>) => {
const withDefaults = useInstanceDefaults({ description: '', isComplete: false }, data)
return withDefaults
}
const Task = useBaseModel<Tasks, TasksQuery, typeof modelFn>({ name: 'Task', idField: '_id' }, modelFn)
console.log(Task.store) // --> See API, below
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.
API
Here is the API for BaseModel stores, grouped by functionality.
Items
Items are records which have an idField, which is usually assigned by a server, though not required.
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
).
Storage
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 records from the store matchingparams.query
. The response is synchronous and always returns a results object with an array ofdata
. Paginated responses also includelimit
,skip
, andtotal
. If you turn off pagination, only{ data }
will be returned.countInStore(params)
returns the number of records in the store which matchparams.query
.getFromStore(id, params)
returns the record from the store with matchingid
, or returnsnull
if a record is not found.addToStore(data)
adds the data object or array to the correct internal storage (items or temps), depending on if an idField is present.removeFromStore(data)
removes any data with matchingdata[idField]
from the store.data
can be an object or an array of objects.clearAll()
removes all storeditems
,temps
, andclones
from the store.
Internal State
idField
will match theidField
option that you provided in the Model options.associations
stores a reference to eachassociateFind
orassociateGet
relationship.whitelist
will match thewhitelist
option that you provided in the Model options. Thewhitelist
marks special query operators and filters to be allowed in store queries.