Feathers-Pinia Services
Service Interface Overview
The Feathers-Pinia Service Interface adds methods to the Feathers Service Interface, allowing the service to work as a functional replacement for a Model constructor. In short, in Feathers-Pinia v3 the service is the Model.
Here's an overview of the full Feathers-Pinia Service Interface:
const { api } = useFeathers()
const service = api.service('users')
// create data instances
service.new(data)
// api methods
service.find(params)
service.findOne(params) // unique to feathers-pinia
service.count(params)
service.get(id, params)
service.create(id, params)
service.patch(id, params)
service.remove(id, params)
// store methods
service.findInStore(params)
service.findOneInStore(params)
service.countInStore(params)
service.getFromStore(id, params)
service.createInStore(data, params) // data is a record or array or records
service.patchInStore(idData, params) // idData is one or more ids or records
service.removeFromStore(idData, params) // idData is one or more ids or records
// hybrid methods
service.useFind(params, options)
service.useGet(id, options)
service.useGetOnce(id, options)
// event methods
service.on(eventName, eventHandler)
service.emit(eventName, data)
service.removeListener(eventName, eventHandler)
The "new" Method
service.new(data)
The "new" method allows creation of model instances. It takes the place of calling new Model(data)
in previous Feathers-Pinia releases.
Customizing Instances
Customizing the default state of each instance is done through setupInstance
in each service's configuration.
Service Methods
Service methods are convenience wrappers around the Feathers Client service provided in the options.
find(params)
await todoStore.find({ query: {} })
Uses the Feathers Client to retrieve records from the API server. On an SSR server, find data will be marked as ssr: true
, which allows extra queries to be skipped on the client.
findOne(params)
await service.findOne({ query: {} })
Uses the Feathers Client to retrieve the first matching record from the API server. On an SSR server, find data will be marked as ssr: true
, which allows extra queries to be skipped on the client.
count(params)
await service.count({ query: { isComplete: false } })
Like find
, but returns the number of records that match the query. It does not return the actual records.
get(id, params)
await todoStore.get(1)
Uses the Feathers Client to retrieve a single record from the API server.
Uses the Feathers Client to send an update
request to the API server.
patch(id, data, params)
await todoStore.patch(1, { isComplete: true })
Uses the Feathers Client to send an patch
request to the API server.
remove(id, params)
await api.service.remove(id)
Uses the Feathers Client to send a remove
request to the API server.
Service Utils
These utilities use a combination of multiple store methods to eliminate boilerplate and improve developer experience.
Service Events
Services are EventEmitter
instances which emit service events when received. Services can be used as a data-layer Event Bus. You can even use custom event names:
service.on('custom-event', (data) => {
console.log(data) // { test: true }
})
service.emit('custom-event', { test: true })
service.on
Register event handlers to listen to events.
service.once
Register an event handler that only occurs once.
service.removeListener
Remove an event handler.