Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

VirtualRowProps.md

114 lines3.2 KB

1---
2id: VirtualRowProps
3title: VirtualRowProps
4---
5​
6Defined in: [packages/db/src/virtual-props.ts:78](https://github.com/TanStack/db/blob/main/packages/db/src/virtual-props.ts#L78)
7​
8Virtual properties recognized on TanStack DB rows.
9​
10These properties are:
11- Computed (not stored in the data model)
12- Read-only (cannot be mutated directly)
13- Available in queries (WHERE, ORDER BY, SELECT)
14- Included when spreading rows (`...user`)
15​
16Structured whole-row selections and spreads retain source metadata.
17A functional selection retains it only when returning a source row directly.
18New functional objects and other projections receive the output Collection's
19metadata. Selected virtual names cannot change that attribution.
20​
21## Examples
22​
23```typescript
24// Accessing virtual properties on a row
25const user = collection.get('user-1')
26if (!user.$hasPendingWrites) {
27 console.log('No pending local optimistic writes for this row')
29if (user.$origin === 'local') {
30 console.log('Row has local attribution')
32```
33​
34```typescript
35// Using virtual properties in queries
36const ordersWithoutLocalWrites = createLiveQueryCollection({
37 query: (q) => q
38 .from({ order: orders })
39 .where(({ order }) => eq(order.$hasPendingWrites, false))
40})
41```
42​
43## Type Parameters
44​
45### TKey
46​
47`TKey` *extends* `string` \| `number` = `string` \| `number`
48​
49The type of the row's key (string or number)
50​
51## Properties
52​
53### $collectionId
54​
55```ts
56readonly $collectionId: string;
57```
58​
59Defined in: [packages/db/src/virtual-props.ts:116](https://github.com/TanStack/db/blob/main/packages/db/src/virtual-props.ts#L116)
60​
61The ID of the source collection this row originated from.
62​
63In joins, this can help identify which collection each row came from.
64Whole-row live queries preserve the upstream Collection's ID. Projected
65row roots otherwise use the live-query Collection's ID.
66​
67***
68​
69### $hasPendingWrites
70​
71```ts
72readonly $hasPendingWrites: boolean;
73```
74​
75Defined in: [packages/db/src/virtual-props.ts:87](https://github.com/TanStack/db/blob/main/packages/db/src/virtual-props.ts#L87)
76​
77Whether this row currently has pending local optimistic writes.
78​
79This describes the row's local optimistic state, not backend upload or
80acknowledgement. It is always `false` for local-only collections.
81​
82***
83​
84### $key
85​
86```ts
87readonly $key: TKey;
88```
89​
90Defined in: [packages/db/src/virtual-props.ts:107](https://github.com/TanStack/db/blob/main/packages/db/src/virtual-props.ts#L107)
91​
92The row's key (primary identifier).
93​
94This is the same value returned by `collection.config.getKey(row)`.
95Useful when you need the key in projections or computations.
96​
97***
98​
99### $origin
100​
101```ts
102readonly $origin: VirtualOrigin;
103```
104​
105Defined in: [packages/db/src/virtual-props.ts:99](https://github.com/TanStack/db/blob/main/packages/db/src/virtual-props.ts#L99)
106​
107Collection attribution for this row's current value. `'local'` covers
108optimistic rows and attributed source writes; `'remote'` covers source
109writes without local attribution. See [VirtualOrigin](../type-aliases/VirtualOrigin.md) for the
110key-and-timing rules, including truncate and overlapping mutations.
111​
112For local-only collections, this is always `'local'`.
113Whole-row live queries preserve source attribution. Projected row roots
114otherwise use the live-query Collection's attribution.