Skip to content

Data Sources ​

@vuecs/data ships two loader-backed state containers:

  • defineDataCollection() — a paginated/filtered list of entities. Its return shape is a deliberate structural superset of @vuecs/list's ListState & ListMutators, so it binds straight to <VCList :state>.
  • defineDataRecord() — a single entity, for detail views and edit forms.

Both sit on one shared async core: counter-backed busy, a loading flag for the newest load, an error ref, token-guarded latest-wins loading, a mutate() wrapper for write gestures, and a dehydrate() / hydrate pair for SSR.

Where it sits vs defineList() ​

The two are complements, split on one question: do you already have the data, or do you fetch it?

defineList()defineDataCollection()
Package@vuecs/list@vuecs/data
Data sourceYou own it — a Ref, a getter, a store sliceThe container owns it; you supply a load adapter
AsyncNone. busy / total are values you pass inBuilt in — busy, error, latest-wins, mutate()
PaginationWhatever you put in the meta bagmeta bag + load(next) re-fires the adapter
SSRConsumer-sidedehydrate() / hydrate
Binds to <VCList :state>YesYes (structural superset)

Reach for defineList() when the data already lives somewhere else (a Pinia store, a parent component, a prop). Reach for defineDataCollection() when the component is the thing that fetches.

Installation ​

bash
npm install @vuecs/data

Peers: vue and @vuecs/core. There is no runtime dependency on @vuecs/list in either direction — the <VCList :state> binding works by shape.

Collection quickstart ​

The load adapter is the only required option. vuecs does not pick your HTTP client or query builder — a hand-rolled fetch is a complete adapter:

ts
import { defineDataCollection } from '@vuecs/data';

type User = {
    id: number;
    name: string;
    email: string;
};

// A plain type ALIAS, not an interface — see "Typing your meta bag" below.
type UserListMeta = {
    pagination: { limit: number; offset: number };
    search?: string;
};

const source = defineDataCollection<User, UserListMeta>({
    initialMeta: { pagination: { limit: 10, offset: 0 } },
    itemKey: 'id',

    load: async (meta) => {
        const params = new URLSearchParams({
            limit: String(meta.pagination.limit),
            offset: String(meta.pagination.offset),
        });

        if (meta.search) {
            params.set('q', meta.search);
        }

        const response = await fetch(`/api/users?${params}`);
        if (!response.ok) {
            throw new Error(`users: ${response.status}`);
        }

        const body = await response.json() as { data: User[]; total: number };

        // `total` is optional — until the server has reported one,
        // `source.total` falls back to `data.length`. Once reported, the
        // last known server total sticks: a later response that omits
        // `total` keeps it (a page fetch shouldn't wipe the count).
        return { data: body.data, total: body.total };
    },
});

With autoLoad left at its default (true), the first fetch fires on mount. The returned container exposes:

ts
source.data      // ComputedRef<User[]>
source.busy      // ComputedRef<boolean>  — any load or mutate() running
source.loading   // ComputedRef<boolean>  — the newest load is running
source.total     // ComputedRef<number>   — last reported server total, else data.length
source.isEmpty   // ComputedRef<boolean>  — !loading && total === 0
source.error     // Ref<unknown>
source.meta      // UserListMeta          — a reactive plain bag, NOT a ref

Binding to <VCList> ​

<VCList :state> accepts the source directly:

vue
<script setup lang="ts">
import { defineDataCollection } from '@vuecs/data';

const source = defineDataCollection<User, UserListMeta>({ /* … as above */ });
</script>

<template>
    <VCList :state="source">
        <template #default>
            <VCListBody>
                <template #item="{ data: user }">
                    <!-- VCListBody's #item slot is untyped (`unknown`) — cast to the row type -->
                    <VCListItem :data="user">
                        {{ (user as User).name }}
                    </VCListItem>
                </template>
            </VCListBody>

            <VCListEmpty />
            <VCListLoading />
        </template>
    </VCList>
</template>

<VCListEmpty> and <VCListLoading> self-gate off the same container's busy / data state, so loading and empty states need no extra wiring.

Wiring pagination ​

load(next) merges next into a draft of the meta bag and re-fires the adapter with it; the bag itself only takes the new value once that load succeeds (see Meta commits on success). <VCPagination> emits a PaginationMeta payload carrying the new limit / offset:

vue
<script setup lang="ts">
import type { PaginationMeta } from '@vuecs/pagination';

const source = defineDataCollection<User, UserListMeta>({ /* … */ });
const { busy, meta, total } = source;

const onPage = (page: PaginationMeta) => source.load({
    // The object form is a SHALLOW top-level merge — pass the whole
    // `pagination` sub-object, not just the key you changed.
    pagination: { limit: page.limit, offset: page.offset },
});
</script>

<template>
    <VCPagination
        :total="total"
        :limit="meta.pagination.limit"
        :offset="meta.pagination.offset"
        :busy="busy"
        @load="onPage"
    />
</template>

Destructuring total / busy in <script setup> is deliberate: they are ComputedRefs living on a plain object, so Vue's template auto-unwrapping only kicks in once they are top-level bindings. Written inline you would need source.total.value. meta is a plain reactive object, so meta.pagination.limit reads directly.

Restarting at the first page ​

When a filter or the page size changes, the list should go back to the first page. Watch those inputs and load with the offset reset:

ts
import { watch } from 'vue';

const props = defineProps<{ search?: string; limit: number }>();

const source = defineDataCollection<User, UserListMeta, { search?: string }>({
    initialMeta: { pagination: { limit: props.limit, offset: 0 } },
    load: (meta, query) => fetchUsers({ ...meta.pagination, search: query?.search ?? props.search }),
});

watch(() => [props.search, props.limit] as const, ([search, limit]) => {
    source.load({ pagination: { limit, offset: 0 } }, { search });
});

Typing quickly fires several loads in a row. You don't need to cancel them: only the newest request applies, and the older ones resolve superseded (see Latest-wins loading). Debounce the watched value (e.g. refDebounced from VueUse) if you also want fewer requests.

If the reset load fails, meta keeps the previous page (a failed load commits nothing, see Meta commits on success). So refresh() would retry with the old offset. Retry with the same call instead: source.load({ pagination: { limit, offset: 0 } }, { search }).

Two ways to write meta ​

ts
// Object form — shallow top-level merge. Typed as the FULL `Meta`, so
// every required key must be present.
source.load({ pagination: { limit: 25, offset: 0 } });

// Updater form — wholesale replace. Keys the returned object omits are
// DELETED from the bag.
source.load((current) => ({ ...current, search: 'ada' }));

// Re-fire with the current meta unchanged.
source.refresh();

The updater form spread over current is the idiomatic way to touch one key when Meta has several required ones — the object form's type demands them all, even though the runtime merge is shallow.

There is deliberately no deep-merge policy on meta. Composing a query (filters, sort, includes) is your layer, not vuecs's; the bag is forwarded to load verbatim.

Per-call input ​

Not everything a request needs belongs in meta. Pass it as the second argument to load() / refresh() and the loader receives it as its own second argument, untouched:

ts
type Query = { search?: string; sort?: string };

const source = defineDataCollection<User, UserListMeta, Query>({
    load: async (meta, query) => fetchUsers({ ...meta.pagination, ...query }),
});

source.load(undefined, { search: 'ada' });
source.refresh({ sort: 'name' });

The input is never stored — it lives for that one call. Anything that should persist across loads goes in meta.

Load more / infinite scroll ​

loadMore(next?, input?) is load() with one difference: the result is appended to the held rows instead of replacing them. Derive the next offset from the rows already held, from a "Load more" button or a scroll sentinel:

ts
const onReachEnd = async () => {
    const held = source.data.value.length;
    if (held >= source.total.value) {
        return;   // everything is held
    }

    const { limit } = source.meta.pagination;
    const outcome = await source.loadMore({ pagination: { limit, offset: held } });

    if (outcome.status === 'superseded') {
        // another load was running — call again if the sentinel is still visible
    }
};
  • Repeated rows don't double. A returned row whose identity is already held replaces it in place — the usual overlap when a row was inserted between two requests and shifted the offsets.
  • It never stacks. While any load is in flight, loadMore() starts nothing and resolves superseded once that load settled. A scroll trigger firing repeatedly can't skip a page, and a pending filter change can't have the next page of the old query appended to it.
  • A later load() wins. Changing a filter or resetting to the first page supersedes a pending loadMore() as usual; it resolves superseded and appends nothing.
  • meta keeps describing the query. Unlike load(), an append commits neither next nor the result's meta patch-back (total still updates). So refresh() or a filter change restarts from the first page, and the meta-reading hooks below keep seeing the query — derive the next page from data.value.length, not from meta.

Record quickstart ​

defineDataRecord() is the single-entity sibling. It drops the collection machinery (total, isEmpty, identity resolution, gate flags) and adds ensure() plus local mutators named for what they actually do.

ts
import { defineDataRecord } from '@vuecs/data';

type UserRecordMeta = { id: number };

const source = defineDataRecord<User, UserRecordMeta>({
    initialMeta: { id: props.userId },

    load: async (meta) => {
        const response = await fetch(`/api/users/${meta.id}`);
        if (!response.ok) {
            throw new Error(`user ${meta.id}: ${response.status}`);
        }

        return { data: await response.json() as User };
    },
});

source.data    // ComputedRef<User | undefined>
source.busy    // ComputedRef<boolean>
source.loading // ComputedRef<boolean>
source.error   // Ref<unknown>
source.meta    // UserRecordMeta — reactive plain bag

ensure() — fetch only when unresolved ​

ensure() resolves immediately when the record is already present, coalesces onto an in-flight load() when one is running, and otherwise fires a fresh load. It is the "make sure this is resolved before I use it" call:

ts
const openEditor = async () => {
    await source.ensure();
    // source.data.value is populated (or source.error holds the failure)
};

ensure() deliberately ignores a concurrent mutate() — a write in flight is not a load, so ensure() still fetches.

ensure(input) forwards an input to the loader. With one, "resolved" is stricter: the held record must come from a load with the same input, and ensure() only coalesces onto an in-flight load with the same input. Otherwise it loads:

ts
const source = defineDataRecord<User, Record<string, unknown>, number>({
    load: async (_meta, id) => ({ data: await fetchUser(id!) }),
    autoLoad: false,
});

await source.ensure(1);   // loads user 1
await source.ensure(1);   // { status: 'cached' }
await source.ensure(2);   // loads user 2 — not coalesced onto user 1

Inputs compare with Object.is, so pass a primitive (an id) or a stable reference — a fresh object literal never matches. ensure() without an input keeps the original contract: any held record or in-flight load satisfies it.

The source remembers which input produced the held record. A load sets it. dehydrate() writes it into the snapshot (snapshot.input) and hydrate restores it, so ensure(id) on the client resolves cached instead of refetching. For a record adopted through initialData, pass initialInput alongside it. set() and clear() forget it — a record you put in by hand didn't come from any input.

mutate() — write gestures under the source's accounting ​

mutate(fn) runs any consumer async operation while raising busy and recording failures into error. Unlike load(), it rethrows — an explicit user gesture should surface its failure to the caller:

ts
const save = async (patch: Partial<User>) => {
    const updated = await source.mutate(async () => {
        const response = await fetch(`/api/users/${source.meta.id}`, {
            method: 'PATCH',
            headers: { 'content-type': 'application/json' },
            body: JSON.stringify(patch),
        });

        if (!response.ok) {
            throw new Error(`save: ${response.status}`);
        }

        return await response.json() as User;
    });

    // Adopt the server's canonical record.
    source.set(updated);
};

Because mutate rethrows, wrap the call site in try / catch (or let it reject into an error boundary) — source.error is a mirror for rendering, not a substitute for handling.

Local mutators: set / merge / clear ​

These write local state only. They are what a realtime created / updated / deleted handler applies:

ts
source.set(record);              // replace wholesale
source.merge({ name: 'Ada' });   // deep-merge a patch (no-op when unresolved)
source.clear();                  // back to undefined

The names are deliberately not create / update / delete: the record variant has no ListMutators compatibility constraint, and the honest local-state names avoid implying an HTTP call.

Immediate writes ​

By default a mutator called during a load is buffered until the load settles. set() and clear() accept { immediate: true } to apply right away instead — for a write that must win over the response in flight, such as adopting the server's reply to a save:

ts
source.load();                                   // in flight
source.set(savedRecord, { immediate: true });    // applies now
// the in-flight load resolves { status: 'superseded' } and changes nothing

An immediate write first drains anything already buffered (so earlier writes stay earlier), then supersedes every in-flight load, so neither their data nor their meta can land afterwards. Those loads resolve superseded. ensure() called afterwards starts a fresh load rather than coalescing onto a superseded one.

Adoption via initialData ​

When a parent already has the record (a prop, a row clicked in a list), seed it and skip the mount-time fetch:

ts
const source = defineDataRecord<User>({
    initialData: props.user ?? undefined,   // autoLoad is skipped when set
    load: async () => ({ data: await fetchUser(props.userId) }),
});

initialData is aliased, not cloned

The source stores the value you pass by reference and never mutates it — but you should not mutate it either. Hand it a value you are done with, or clone at the call site.

hydrate is barely different: the collection copies the snapshot's array one level (the row objects inside stay shared), and the record adopts the snapshot's record object as-is. Either way, treat seeded state as owned by the source once handed over.

Semantics ​

Latest-wins loading ​

Every load() takes a monotonic token. When a response arrives, it is applied only if its token is still the latest — a slow first request that resolves after a faster second one is discarded, state and rejection alike. busy is counter-backed rather than boolean, so overlapping load() and mutate() calls compose instead of stomping each other; it stays true until the last in-flight operation settles.

This is the fix for two failure modes the hand-rolled containers this package replaces both had: stale responses clobbering newer state, and if (busy) return guards silently dropping user input.

busy vs loading ​

The two flags answer different questions:

Flagtrue while…Bind it to
busyany work runs: every load() (superseded ones too) and every mutate().Save buttons, a global progress bar, guards against leaving the page mid-write.
loadingonly the newest load runs.Skeletons, loading placeholders, a table's or card's busy prop — anything that says "the data on screen is about to change".

loading follows the same token as latest-wins: a load that a newer load() or an immediate write supersedes stops counting the moment it is superseded, and settling later never turns loading (or error) back on. A mutate() never sets loading — it is a write, not a load. So when the user pages quickly (load 1 slow, load 2 fast), loading goes false as soon as load 2 applies, while busy stays true until load 1 settles too.

isEmpty follows loading, not busy: an empty result shows the empty state even while a mutate() or a superseded load is still running.

<VCList :state> still reads busy for <VCListLoading> / <VCListEmpty>.

The error contract ​

CallOn startOn failure
load() / refresh()clears errorcaptures into error; the promise resolves { status: 'failed', error }
ensure()clears error when it actually fetchesas load()
mutate(fn)leaves error alonerecords into error and rethrows

load() never rejects. That is what makes autoLoad safe — a mount-time fetch cannot produce an unhandled rejection. Render source.error for the failed-load state, and catch around mutate() for gestures.

Load outcomes ​

error is shared, so it can't tell you whether your call failed or a later one did. The promise can: every load() / refresh() resolves to that call's own outcome.

statusMeaning
appliedThis call was the latest; its data and meta are committed.
supersededA newer load (or an immediate write) took over. This call changed nothing — whether it succeeded or failed.
failedThis call was the latest and its loader threw. error is the same value source.error now holds.
cachedensure() only — the held record already satisfied it; no load ran.
ts
import { DataLoadStatus } from '@vuecs/data';

const outcome = await source.load((current) => ({ ...current, search: 'ada' }));
if (outcome.status === DataLoadStatus.FAILED) {
    emit('failed', outcome.error);   // only ever for the latest load
}

The statuses are a DataLoadStatus enum; the status fields are typed as its string literals (`${DataLoadStatus}`), so comparing against the plain string ('failed') works too.

Meta commits on success ​

load(next) doesn't write next into meta up front. It resolves next onto a draft, hands the draft to the loader, and replaces the bag with it only when the call comes back applied — then applies the loader's meta patch-back on top. A load that fails or is superseded leaves meta at whatever the last successful request used:

ts
await source.load((current) => ({ ...current, search: 'x' }));   // loader throws
source.meta.search;                                               // unchanged — the failed search didn't stick
await source.load((current) => ({ ...current, pagination: { limit: 10, offset: 10 } }));
// this request does NOT carry the failed search

While the request is in flight meta still shows the previous value, so a pagination control bound to meta moves once the new page has actually arrived.

Back-to-back calls compose as you'd expect: each draft is built on the latest pending one, so a load that sets search followed by one that sets pagination sends both, and the winner commits both.

autoLoad ​

Defaults to true: one load() fires from onMounted. It is skipped when:

  • a hydrate snapshot seeded state (SSR handoff — see below), or
  • for defineDataRecord, when initialData was supplied.

autoLoad needs a component setup context. Constructing a source outside one (a module singleton, a store) dev-warns and does nothing — call load() yourself.

autoLoad is client-only (onMounted never runs during a server render). To load on the server, call load() inside your own onServerPrefetch — see SSR / hydration.

Gate flags ​

dedupCreated, filterDeleted, and mergeOnUpdated all default to false and carry exactly the semantics they have in defineList():

FlagEffect
dedupCreatedcreate(item) is a no-op when the item's identity is already in data
filterDeleteddelete(item) is a no-op when the item is not in data
mergeOnUpdatedupdate(item) deep-merges into the existing row instead of replacing it

Identity comes from the same ladder as defineList — itemId → itemKey → .id.

Patch-merge semantics ​

Two paths merge rather than replace: defineDataRecord's merge(partial), and defineDataCollection's update(item) when mergeOnUpdated is set. Both run through one configured merger:

  • Deep merge; the patch wins. On a conflicting key the incoming value replaces the stored one — a realtime update must actually update. Keys the patch does not mention survive.
  • Arrays REPLACE, they do not concatenate. Without this, repeated realtime patches would grow an array-valued field without bound.
  • Explicitly-undefined keys are skipped. merge({ name: undefined }) leaves name untouched. Pass null to clear a value — the same "only undefined falls through; null deliberately clears" doctrine the rest of vuecs uses for behavioral defaults.
  • Inputs are cloned. The result shares no nested reference with your patch or with the record it replaced, and your patch object is never mutated in place.

Everything else — set(), clear(), create(), delete(), and update() without mergeOnUpdated — replaces wholesale and never clones.

The structured-clone constraint ​

Cloning is implemented with structuredClone, so anything you pass through a merging path must be structured-cloneable:

  • Function-valued fields throw (DataCloneError). Entity rows carrying callbacks need set() (or a plain update()), not merge().
  • Class instances are flattened into plain objects — methods and prototype are lost. Date, RegExp, Map, Set, and nested plain objects/arrays all survive intact.
  • Top-level reactive proxies are unwrapped automatically (via toRaw), so handing merge() a reactive() form object works. A proxy nested inside a raw object is not unwrapped and still throws — keep raw entities in your source of truth.
  • Cost is O(record size) per merge. Fine for entity-shaped rows; consider batching if a very large record is patched at high frequency.

Mutator buffering during a load ​

A realtime event arriving mid-load() would otherwise be wiped out by the wholesale assignment of the response. So mutators called while a load is in flight are buffered and drained FIFO after every in-flight load settles — the response applies first, then the buffered operations in arrival order.

ts
source.load();          // in flight
source.create(row);     // buffered
source.delete(other);   // buffered
// response applies → create(row) → delete(other)

This covers create / update / delete on the collection and set / merge / clear on the record. The drain also runs when the load fails — events are never silently lost. The gate flags make every drain case land correctly: dedupCreated swallows a create the response already included, filterDeleted swallows a delete for a row the response no longer has, and mergeOnUpdated re-merges a stale version incoming-wins.

Known v1 limitation: the buffer is unbounded while a load never settles.

Mutation outcomes ​

Every mutator returns what it did, as a DataMutationStatus string literal:

ReturnMeaning
appliedState changed now.
noopNothing changed — a create dropped by dedupCreated or rejected by accept, an update or delete of an absent row, a merge() / clear() with no record held.
bufferedA load is in flight; the mutation replays once it settles.

A buffered mutation doesn't know yet whether it will change anything. Pass onSettled to find out once it actually runs. It fires synchronously for an unbuffered call:

ts
socket.on('user.created', (user) => {
    source.create(user, {
        onSettled: (status) => {
            if (status === 'applied') emit('created', user);
        },
    });
});

total auto-adjustment ​

When the server reported a total, create() and delete() adjust it by the delta between array lengths — never a hardcoded ±1. A deleted event for a row that is not on the current page therefore leaves total alone, instead of decrementing it toward a number that drifts further from truth with every off-page event.

When the loader never reported a total, source.total simply tracks data.length and no adjustment happens.

Realtime inserts: accept / insertPosition / capacity ​

By default create() appends the row and keeps it, whatever page or filter is on screen. Three options let a socket handler call the mutators blindly and still keep the view correct:

OptionEffect
accept(item, meta)Whether the row belongs to the current result set (e.g. matches the filters in meta). A create() it rejects is a noop. An update() that leaves a held row rejected removes it and decrements total.
insertPositionWhere create() inserts: 'start', 'end' (default), null for "belongs on another page" (not held, still counted in total), or (item, meta) => … to decide per row.
capacityThe most rows create() may leave held — typically the page size. A number, or (meta) => number | undefined. On overflow one tail row is dropped — the cap never shrinks a list loadMore() grew past it. A row whose own position lies past the cap is not held at all, but still increments total (a noop when no server total is known).
ts
type TicketMeta = {
    pagination: { limit: number; offset: number };
    status?: 'open' | 'closed';
    sort: 'newest' | 'oldest';
};

const source = defineDataCollection<Ticket, TicketMeta>({
    initialMeta: { pagination: { limit: 20, offset: 0 }, sort: 'newest' },
    itemKey: 'id',
    dedupCreated: true,
    load: fetchTickets,

    // Only rows matching the active filter belong on screen.
    accept: (ticket, meta) => !meta.status || ticket.status === meta.status,
    // Newest-first: a new ticket goes on top of page 1 and belongs on no
    // later page (`null` counts it without holding it). Oldest-first: it
    // goes last, and `capacity` keeps it off a full (non-last) page.
    insertPosition: (_ticket, meta) => {
        if (meta.sort === 'oldest') {
            return 'end';
        }

        return meta.pagination.offset === 0 ? 'start' : null;
    },
    // Keep one page's worth of rows.
    capacity: (meta) => meta.pagination.limit,
});

socket.on('ticket.created', (ticket) => source.create(ticket));
socket.on('ticket.updated', (ticket) => source.update(ticket));   // closes → leaves an 'open' view
socket.on('ticket.deleted', (ticket) => source.delete(ticket));

accept is one-way: an update() for a row that is not held but now matches stays a noop (its position is unknown), and a row removed by it leaves the page one short until the next load.

All three are evaluated when the mutation runs, against the committed meta. For a buffered event that is after the load it waited for has settled — so an insert that arrives while the user switches the sort lands according to the new sort, not the old one. Only create() honours capacity; loads hold whatever the loader returns.

Reactivity depth ​

By default the internal data ref is a shallowRef. Every mutation flow replaces the array (or record) wholesale, so per-row deep reactivity would buy nothing while costing a proxy per row and breaking loader row identity.

The practical consequence: rows are not deep-reactive. Mutating a field in place does not re-render.

ts
// ✗ no re-render
source.data.value[0].name = 'Ada';

// ✓ replace the row
source.update({ ...source.data.value[0], name: 'Ada' });

If your UI mutates the held data in place (a form bound straight to the record), opt into deep: true:

ts
const source = defineDataRecord<Policy>({ load, deep: true });

source.data.value!.parentId = 'abc';   // ✓ re-renders
source.merge({ name: 'x' });           // keys assigned onto the held record

With deep: true the ref is a ref, not a shallowRef. merge() (record) and update() (collection) then assign keys onto the held object instead of replacing it, so it keeps its identity. update() without mergeOnUpdated still has replace semantics: keys the incoming row lacks are deleted from the held row. set(), create() and loads still replace. The cost: data.value (and each row) is a reactive proxy, not the object your loader returned. dehydrate() reads through toRaw in both modes, so snapshots never carry proxies. Primitive rows have no keys to assign, so update() still replaces them.

The held object is written in place

Under deep: true, merge() / update() and any write through data.value mutate the object the source holds. That is whatever you handed in: the loader's result, initialData, a hydrated snapshot's data, or a set() / create() argument. Pass objects you are done with, or copy them first.

The same applies to the meta bag, which is shallowReactive — replace whole sub-objects (meta.pagination = { … }) rather than writing nested fields.

Typing your meta bag ​

Declare Meta as a type alias, not an interface:

ts
// ✓ binds to <VCList :state>
type UserListMeta = {
    pagination: { limit: number; offset: number };
};

// ✗ compiles here, fails at the <VCList :state="source"> call site
interface UserListMeta {
    pagination: { limit: number; offset: number };
}

<VCList>'s state prop pins the meta type to Record<string, unknown>. TypeScript gives object type aliases an implicit index signature and interfaces none, so an interface-typed Meta is not assignable and the binding errors. This is a pre-existing @vuecs/list constraint, not something @vuecs/data introduces — the drift guard in packages/data/test/types/list-compat.test-d.ts pins the behaviour.

If you must keep an interface (because it is declaration-merged elsewhere), add the index signature yourself: interface UserListMeta { [key: string]: unknown; … }.

SSR / hydration ​

load() is a plain promise with no lifecycle coupling, so it is callable from setup, onServerPrefetch, or a route guard. The handoff is dehydrate() on the server and hydrate on the client.

ts
import { onServerPrefetch } from 'vue';
import { defineDataCollection } from '@vuecs/data';

// Your SSR framework's serialized state store — Nuxt's `useState`,
// a Pinia store, `window.__INITIAL_STATE__`, … vuecs does not pick one.
const payload = useSnapshotStore();

const initialMeta = { pagination: { limit: 10, offset: 0 } };

// Derive the key from the serialized query so a different page or
// filter can't rehydrate the wrong snapshot.
const key = `users:${JSON.stringify(initialMeta)}`;

const source = defineDataCollection<User, UserListMeta>({
    initialMeta,
    hydrate: () => payload[key],
    load: fetchUsers,
});

onServerPrefetch(async () => {
    await source.load();
    payload[key] = source.dehydrate();
});

On the server, payload[key] is empty, so nothing hydrates; onServerPrefetch performs the fetch and stores the snapshot. On the client, the serialized snapshot seeds the state and autoLoad is skipped — no duplicate request on hydration.

Three details worth pinning:

  • hydrate is evaluated once, at construction. Pass the lazy getter form (() => payload[key]) so the store is read at the right moment; the snapshot may also be passed directly.
  • The snapshot is authoritative for meta. hydrate replaces the bag rather than merging onto initialMeta, so keys the snapshot dropped do not survive rehydration.
  • dehydrate() returns a fresh container with shared rows. The array (and the meta object) are new, but the rows are the plain objects your loader returned — mutating a row in place would still reach the live source. Serialization is your contract: JSON.stringify is safe only when rows and meta are JSON-compatible (Date becomes a string, Map/Set collapse to {}, undefined properties are dropped, BigInt throws). If your values aren't plain JSON, run the snapshot through your own serializer before storing it.

defineDataRecord follows the same contract, with one difference worth knowing: its snapshot is { data?, meta, input? }, and only meta is a fresh object — data is the live record reference itself, not a copy. input is the input that produced the held record, so a hydrated ensure(input) resolves cached (see ensure()); it must be JSON-compatible too. The practical rule is the same in both variants, just tighter here: serialize the snapshot, never mutate it in place.

Boundaries ​

@vuecs/data owns the state container and nothing else. Deliberately out of scope, and staying that way:

  • HTTP client — load is an opaque async adapter. fetch, axios, a generated SDK, all equivalent.
  • Query composition — filters, sorting, includes, and how they serialize live in your query layer. The meta bag is forwarded verbatim.
  • Realtime / WebSockets — no socket sugar. The mutators are the seam: a channel handler calls source.create(payload) / update / delete (or set / merge / clear), the buffering rules above make a mid-load event land correctly, and accept / insertPosition / capacity keep it on the right page.
  • Entity registries, realm scoping, emits — consumer-side. Compose your emits around the mutator calls: const onCreated = (e) => { source.create(e); emit('created', e); }.
  • Cursor pagination — offset/limit only for now; cursors ship when a consumer needs them.

API reference ​

defineDataCollection(options) ​

OptionTypeDefaultDescription
load(meta: Meta, input?: Input) => Promise<{ data: T[]; total?: number; meta?: Partial<Meta> }>—Required. The only adapter. meta is the draft this call commits on success; input is the call's per-call input. Optional meta in the result patches the bag back (server-corrected pagination).
initialMetaMeta{}Seed for the reactive meta bag.
initialDataT[][]Seed rows.
autoLoadbooleantrueFire one load() from onMounted.
hydrateSnapshot | (() => Snapshot | null | undefined)—SSR/persistence seed; skips autoLoad.
deepbooleanfalseDeep-reactive rows; update() assigns in place. See Reactivity depth.
itemId(item: T) => string | number—Identity resolver.
itemKeyExtract<keyof T, string | number> | ((item: T) => Extract<keyof T, string | number>)'id' fallbackIdentity by key. Constrained to string / number keys — symbol keys are rejected (they can't be safely cast to a property accessor at runtime).
dedupCreated / filterDeleted / mergeOnUpdatedbooleanfalseGate flags — see above.
accept(item: T, meta: Meta) => boolean—Result-set membership: rejected creates are noop, updates that leave the set remove the row. See Realtime inserts.
insertPosition'start' | 'end' | number | ((item: T, meta: Meta) => 'start' | 'end' | number)'end'Where create() inserts.
capacitynumber | ((meta: Meta) => number | undefined)—Most rows create() may leave held; overflow drops one tail row, never shrinking below what is held.
MemberTypeDescription
data / busy / loading / total / isEmptyComputedRefRead-only views. busy covers every load and mutate(); loading only the newest load (see busy vs loading); isEmpty is !loading && total === 0. total falls back to data.length until a server total has been reported, then keeps the last reported value.
metaMetaReactive plain bag (shallowReactive). Meta of the last load that applied.
errorRef<unknown>Last captured failure.
load(next?, input?)Promise<DataLoadOutcome>Draft meta + re-fire; commits on success. Never rejects.
refresh(input?)Promise<DataLoadOutcome>load() with meta unchanged.
loadMore(next?, input?)Promise<DataLoadOutcome>load(), but appends and commits no meta; resolves superseded without fetching while another load runs. See Load more.
mutate(fn)Promise<R>Run a write under busy/error accounting. Rethrows.
create / update / delete(item: T, { onSettled? }?) => 'applied' | 'buffered' | 'noop'Local mutators; buffered during a load. See Mutation outcomes.
applyCreate / applyUpdate / applyDelete(current: T[], item: T) => T[]Pure next-array builders.
findIndex / getItemKey / flags—Identity + flag introspection (part of the ListState contract).
dehydrate(){ data, total?, meta }JSON-able snapshot.

defineDataRecord(options) ​

OptionTypeDefaultDescription
load(meta: Meta, input?: Input) => Promise<{ data: T | undefined; meta?: Partial<Meta> }>—Required.
initialMetaMeta{}Seed for the meta bag.
initialDataT—Adoption path; skips autoLoad. Aliased, not cloned.
autoLoadbooleantrue
hydrateSnapshot | (() => Snapshot | null | undefined)—
initialInputInput—The input initialData corresponds to, so ensure(input) resolves cached.
deepbooleanfalseDeep-reactive record; merge() assigns in place.
MemberTypeDescription
dataComputedRef<T | undefined>
busy / loading / error / meta—As the collection variant.
load(next?, input?) / refresh(input?)Promise<DataLoadOutcome>As the collection variant.
ensure(input?)Promise<DataEnsureOutcome>Fetch only when unresolved for input; coalesces onto an in-flight load with the same input.
set / clear(…, { onSettled?, immediate? }?) => DataMutationStatusLocal mutators; buffered during a load unless immediate.
merge(partial, { onSettled? }?) => DataMutationStatusBuffered during a load.
mutate(fn)Promise<R>Rethrows.
dehydrate(){ data?, meta, input? }JSON-able snapshot; input is the input that produced the held record.

See also ​

  • List — the <VCList :state> binding and defineList()
  • Pagination — the @load payload wired above
  • Composables — the rest of vuecs's composable families

Released under the Apache 2.0 License.