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.
itemsByIdis an object used as the storage for items. They are keyed by idField to allow for quick lookup.itemsis a dynamically-computed array which holds the list of all records initemsById.itemIdsis 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.
tempsByIdis an object used as the storage for temps. They are keyed by__tempIdto allow for quick lookup.tempsis a dynamically-computed array which holds the list of all records intempsById.tempIdsis 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.
clonesByIdis an object used as the storage for clones. They are keyed by__cloneIdto allow for quick lookup.clonesis a dynamically-computed array which holds the list of all records inclonesById.cloneIdsis a dynamically-computed array which holds all keys inclonesById.clone(itemOrTemp)creates a copy of theitemOrTempand stores it inclonesById.commit(clone)copies the keys from the providedcloneonto its original record initems(ortemps).reset(clone)makes theclonematch 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 returnsnullif 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.datacan be an object or an array of objects.clearAll()removes all storeditems,temps, andclonesfrom the store.
Internal State
idFieldwill match theidFieldoption that you provided in the Model options.associationsstores a reference to eachassociateFindorassociateGetrelationship.whitelistwill match thewhitelistoption that you provided in the Model options. Thewhitelistmarks special query operators and filters to be allowed in store queries.