Module Overview
The main module features the following exports:
ts
// Stores and Model Classes
export { setupFeathersPinia } from './setup'
export { defineStore } from './define-store'
export { defineAuthStore } from './define-auth-store'
export { makeServiceStore, BaseModel } from './service-store/index'
// Global Reference Objects
export { clients } from './clients'
export { models, registerClient } from './models'
// Common Tools
export { useFind } from './use-find'
export { useGet } from './use-get'
export { useClones } from './use-clones'
export { usePagination } from './use-pagination'
// Storage Utilities
export { syncWithStorage } from './storage-sync'
export { syncWithStorageCompressed } from './storage-sync-compressed'
export { clearStorage } from './clear-storage'
Stores and Model Classes
setupFeathersPiniaallows global configuration ofclientsfor all apps. It can also be used to support a common set of options for the returned, wrappeddefineStorefunction. Right now it's not that useful for SSR apps, which require an alternative configuration.defineStoresets up a single Pinia store for a single Feathers service.defineAuthStoresets up a single Pinia store for an authentication service. See Auth StoresBaseModelis the base class for working with Data Modeling.makeServiceStoreis an internal utility that assists in structuring service stores. Don't use it.
Global Reference Objects
clientsstores a reference to every Feathers client provided to eithersetupFeathersPiniaordefineStore.- After setup, you can reference a
FeathersClientat any time as shown below. This might come in handy if you need to fetch data that you don't want to be reactive. That data also won't end up in the store, so it would require refetching if not manually stored somewhere. You could use this with swrv.
tsimport { clients } from 'feathers-pinia' const { api } = clients const result = await api.service('items').find({ query: {} })- If you call your default client
api, you won't have to provide a customclientAliasoption to thedefineStorefunction. Learn about setting up FeathersClient instances in Setup.
- After setup, you can reference a
registerClientadds a client by name to theclientsobject.registerClient('api', feathersClientInstance).modelsstores a reference to every custom model provided todefineStore.
Common Tools
useFindgives you reactive data by providing it with a set ofparamslike what you would pass to a Feathers service'sfindmethod. This provides smart SWR (stale while revalidate) functionality, which we generally refer to as a fall-through cache. The cool thing withuseFindis that since it uses thefindInStoregetter under the hood, it can show records from other queries that match with the current query. Learn more in useFind.useGetgives you a reactivegetrequest. LikeuseFind, it's basically a fall-through cache, only its for individual records. Learn more in useGet.usePaginationis a composition api utility that handles typical pagination logic. See usePaginationuseClonesremoves boilerplate from the clone and commit pattern. It automatically clones all component props containing afeathers-piniainstance and provides a handysavefunction for each one. Thesavefunction performs automatic diffing to only patch data that has changed. See useClones
Storage Sync
syncWithStoragesynchronizes specific parts of a store's state intolocalStorageor any storage adapter you provide.syncWithStorageCompressedis just likesyncWithStorage, but it uses LZW compression to fit roughly 10MB of data into the 5MB localStorage limit. The downside is that the data is unreadable in devtools, so you generally only would use it for production.clearStoragejust clears data stored with the above utilities.
Learn more about these utilities in syncWithStorage