Skip to content

Collection

Overview

useCollection(options) manages a reactive, paginated collection backed by a FeathersJS service. It uses feathers-reactive to subscribe to real-time updates and supports both replace-on-page and append-on-scroll pagination strategies.

All option values that may change reactively should be passed as refs so that the collection automatically resets when they change.

Usage

javascript
import { ref } from 'vue'
import { useCollection } from '@kalisio/kdk/core.client'

const { items, nbTotalItems, nbPages, currentPage, resetCollection } = useCollection({
  service: ref('users'),
  nbItemsPerPage: ref(20),
  baseQuery: ref({ role: 'admin' })
})

Options

OptionTypeDefaultDescription
serviceRef<string>Name of the FeathersJS service.
contextIdRef<string>Optional context ID for contextual services.
baseQueryRef<Object>ref({})Base query merged into every find request.
filterQueryRef<Object>ref({})Filter query merged after baseQuery.
nbItemsPerPageRef<number>ref(12)Number of items per page. Set to 0 to disable pagination.
appendItemsRef<boolean>ref(false)When true, new page items are appended rather than replacing existing ones.
refreshThrottleRef<number>ref(500)Minimum milliseconds between consecutive refresh calls.
listStrategyRef<string>ref('smart')feathers-reactive list strategy.
processorRef<Function>ref()Optional function (items) => items applied to every batch of items before storing.
getServiceRef<Function>Optional factory function override for retrieving the service instance.

Exposed

NameTypeDescription
itemsRef<Array | null>The current page (or accumulated) items. null before first load.
nbTotalItemsRef<number>Total number of items in the collection (from service response).
currentPageRef<number>Current page number (1-based).
nbPagesComputedRef<number>Total number of pages based on nbTotalItems and nbItemsPerPage.
setCollectionItems(items)FunctionSets the items array, applying the processor if defined.
subscribe(query)FunctionSubscribes to the service with the given query via feathers-reactive.
unsubscribe()FunctionCancels the current feathers-reactive subscription.
getCollectionBaseQuery()FunctionReturns the current base query. Override to customise.
getCollectionFilterQuery()FunctionReturns the current filter query. Override to customise.
getCollectionPaginationQuery()FunctionReturns { $limit, $skip } for the current page. Returns {} when pagination is disabled.
resetCollection()FunctionResets pagination to page 1 and re-subscribes. No-op if items are null (initialising).
refreshCollection()FunctionThrottled version of the subscribe call with the full query.

Lifecycle

  • beforeMount: in appendItems mode, registers patched, updated, and removed event handlers to keep in-memory items up to date.
  • beforeUnmount: unsubscribes from feathers-reactive and removes event handlers.