@osdk/react 2.94.0 → 2.94.1-main-718be7c10524d5aa3086fbfd38785c0abd0a2c87

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/build/browser/new/platform-apis/admin/useCurrentFoundryUser.js.map +1 -1
  3. package/build/browser/new/platform-apis/admin/useFoundryUser.js.map +1 -1
  4. package/build/browser/new/platform-apis/admin/useFoundryUsersList.js.map +1 -1
  5. package/build/browser/new/useLinks.js.map +1 -1
  6. package/build/browser/new/useObjectSet.js.map +1 -1
  7. package/build/browser/new/useOsdkAggregation.js +2 -5
  8. package/build/browser/new/useOsdkAggregation.js.map +1 -1
  9. package/build/browser/new/useOsdkObjects.js.map +1 -1
  10. package/build/browser/util/UserAgent.js +1 -1
  11. package/build/browser/util/UserAgent.js.map +1 -1
  12. package/build/cjs/{chunk-EJT5W6SI.cjs → chunk-4PFYL4UI.cjs} +3 -3
  13. package/build/cjs/chunk-4PFYL4UI.cjs.map +1 -0
  14. package/build/cjs/index.cjs +15 -15
  15. package/build/cjs/public/experimental.cjs +16 -16
  16. package/build/cjs/public/experimental.d.cts +9 -12
  17. package/build/cjs/public/platform-apis.d.cts +1 -4
  18. package/build/esm/new/platform-apis/admin/useCurrentFoundryUser.js.map +1 -1
  19. package/build/esm/new/platform-apis/admin/useFoundryUser.js.map +1 -1
  20. package/build/esm/new/platform-apis/admin/useFoundryUsersList.js.map +1 -1
  21. package/build/esm/new/useLinks.js.map +1 -1
  22. package/build/esm/new/useObjectSet.js.map +1 -1
  23. package/build/esm/new/useOsdkAggregation.js +2 -5
  24. package/build/esm/new/useOsdkAggregation.js.map +1 -1
  25. package/build/esm/new/useOsdkObjects.js.map +1 -1
  26. package/build/esm/util/UserAgent.js +1 -1
  27. package/build/esm/util/UserAgent.js.map +1 -1
  28. package/build/types/new/platform-apis/admin/useCurrentFoundryUser.d.ts +0 -1
  29. package/build/types/new/platform-apis/admin/useCurrentFoundryUser.d.ts.map +1 -1
  30. package/build/types/new/platform-apis/admin/useFoundryUser.d.ts +1 -2
  31. package/build/types/new/platform-apis/admin/useFoundryUser.d.ts.map +1 -1
  32. package/build/types/new/platform-apis/admin/useFoundryUsersList.d.ts +0 -1
  33. package/build/types/new/platform-apis/admin/useFoundryUsersList.d.ts.map +1 -1
  34. package/build/types/new/useLinks.d.ts +2 -2
  35. package/build/types/new/useObjectSet.d.ts +2 -2
  36. package/build/types/new/useOsdkAggregation.d.ts +3 -6
  37. package/build/types/new/useOsdkAggregation.d.ts.map +1 -1
  38. package/build/types/new/useOsdkObjects.d.ts +2 -2
  39. package/docs/actions.mdx +314 -0
  40. package/docs/aggregations.mdx +256 -0
  41. package/docs/cache-invalidation.mdx +229 -0
  42. package/docs/devtools/cache.mdx +29 -0
  43. package/docs/devtools/components.mdx +40 -0
  44. package/docs/devtools/console.mdx +29 -0
  45. package/docs/devtools/index.mdx +151 -0
  46. package/docs/devtools/overview.mdx +45 -0
  47. package/docs/functions.mdx +246 -0
  48. package/docs/index.mdx +62 -0
  49. package/docs/links.mdx +270 -0
  50. package/docs/meta.json +30 -0
  51. package/docs/object-sets.mdx +296 -0
  52. package/docs/objects.mdx +489 -0
  53. package/docs/optimistic-updates.mdx +164 -0
  54. package/docs/platform-apis.mdx +296 -0
  55. package/docs/testing.mdx +348 -0
  56. package/package.json +6 -6
  57. package/build/cjs/chunk-EJT5W6SI.cjs.map +0 -1
  58. package/docs/actions.md +0 -432
  59. package/docs/advanced-queries.md +0 -678
  60. package/docs/cache-management.md +0 -222
  61. package/docs/getting-started.md +0 -319
  62. package/docs/platform-apis.md +0 -234
  63. package/docs/prerequisites.md +0 -48
  64. package/docs/querying-data.md +0 -810
@@ -0,0 +1,296 @@
1
+ ---
2
+ title: Work with object sets
3
+ description: Observe composed object sets with useObjectSet, combine them with union, intersect, and subtract, and compute derived properties on the server.
4
+ ---
5
+
6
+ An object set describes a group of objects as a query: a type, filters, set operations, and link traversals. `useObjectSet` observes any object set you build with the client, and `withProperties` adds values that the server computes for each object.
7
+
8
+ ## When to use `useObjectSet`
9
+
10
+ Start with [`useOsdkObjects`](./objects.mdx). It covers filtering, sorting, pagination, pivots, derived properties, and streaming from an object type. Reach for `useObjectSet` when you already have an `ObjectSet`:
11
+
12
+ - You combined sets with `union`, `intersect`, or `subtract`.
13
+ - You pivoted across several links.
14
+ - Another hook or component handed you one, such as `objectSet` from a `useOsdkObjects` result.
15
+
16
+ ## Build an object set
17
+
18
+ Get the client with `useOsdkClient` inside the component, and call it with an object type. The result is an `ObjectSet` that you can refine with `where`, `pivotTo`, and the set operations. Building it does not send a request; the hook does.
19
+
20
+ ```tsx title="src/OpenRoles.tsx"
21
+ import { Employee } from "@my-app/sdk";
22
+ import { useObjectSet, useOsdkClient } from "@osdk/react";
23
+ import { useMemo } from "react";
24
+
25
+ export function OpenRoles() {
26
+ const client = useOsdkClient();
27
+ const engineers = useMemo(() => client(Employee).where({ department: "Engineering" }), [client]);
28
+
29
+ const { data, isLoading, error, fetchMore } = useObjectSet(engineers, {
30
+ where: { jobTitle: { $isNull: false } },
31
+ orderBy: { startDate: "desc" },
32
+ $select: ["fullName", "jobTitle", "startDate"],
33
+ pageSize: 25,
34
+ });
35
+
36
+ return (
37
+ <div>
38
+ {error && <p role="alert">{error.message}</p>}
39
+ {isLoading && !data && <p>Loading…</p>}
40
+ <ul>
41
+ {data?.map((employee) => (
42
+ <li key={employee.$primaryKey}>
43
+ {employee.fullName}, {employee.jobTitle}
44
+ </li>
45
+ ))}
46
+ </ul>
47
+ {fetchMore && <button onClick={() => void fetchMore()}>Load more</button>}
48
+ </div>
49
+ );
50
+ }
51
+ ```
52
+
53
+ Use the client from `useOsdkClient` rather than importing one from a module, so the object set runs against the same client and cache as `OsdkProvider`. `where`, `orderBy`, `pageSize`, `$select`, `autoFetchMore`, `enabled`, and `streamUpdates` behave as they do in [`useOsdkObjects`](./objects.mdx). `useObjectSet` also accepts `undefined`, which disables the query until you have a set.
54
+
55
+ When the options change, `useObjectSet` keeps returning the previous `data` while the new query loads, with `isLoading` set to `true`. Changing to an object set that starts from a different object type clears it. A set pivoted from the same starting type does not count as a different type.
56
+
57
+ ## Combine object sets
58
+
59
+ `union`, `intersect`, and `subtract` are methods on every object set. Each takes one or more sets of the same type and returns a new set:
60
+
61
+ | Method | Result |
62
+ | --- | --- |
63
+ | `a.union(b, c)` | Objects in any of the sets. |
64
+ | `a.intersect(b, c)` | Objects in every set. |
65
+ | `a.subtract(b, c)` | Objects in `a` that are in none of the others. |
66
+
67
+ ```tsx title="src/RetentionWatchList.tsx"
68
+ import { Employee } from "@my-app/sdk";
69
+ import { useObjectSet, useOsdkClient } from "@osdk/react";
70
+ import { useMemo } from "react";
71
+
72
+ export function RetentionWatchList() {
73
+ const client = useOsdkClient();
74
+ const watchList = useMemo(() => {
75
+ const highEarners = client(Employee).where({ salary: { $gte: 150000 } });
76
+ const recentHires = client(Employee).where({ startDate: { $gte: "2025-01-01" } });
77
+ const leadership = client(Employee).where({ jobTitle: { $containsAnyTerm: "director vp" } });
78
+ return highEarners.union(recentHires).subtract(leadership);
79
+ }, [client]);
80
+
81
+ const { data } = useObjectSet(watchList, { orderBy: { fullName: "asc" } });
82
+
83
+ return (
84
+ <ul>
85
+ {data?.map((employee) => (
86
+ <li key={employee.$primaryKey}>{employee.fullName}</li>
87
+ ))}
88
+ </ul>
89
+ );
90
+ }
91
+ ```
92
+
93
+ The same operations are available as `useObjectSet` options, each an array of object sets. `useObjectSet(highEarners, { union: [recentHires], subtract: [leadership] })` is equivalent to the set above.
94
+
95
+ `useOsdkObjects` and `useOsdkAggregation` take an `intersectWith` option instead: an array of `{ where }` clauses on the same type. Use it when every set you would intersect is a filter on that type, and `useObjectSet` when the sets come from elsewhere, such as a pivot.
96
+
97
+ ## Keep object sets stable
98
+
99
+ `useObjectSet` and `useOsdkAggregation` compare object sets by their definition, not by reference. An object set rebuilt on every render with the same filters does not trigger a new request.
100
+
101
+ Your own code may still compare by reference: a `useMemo` or `useEffect` dependency array, or a memoized child component that takes the set as a prop, such as [`ObjectTable`](/components/object-table). `useStableObjectSet` returns the same `ObjectSet` instance until its definition changes, including any `where`, set operation, or pivot composed onto it:
102
+
103
+ ```tsx title="src/DepartmentPicker.tsx"
104
+ import { Employee } from "@my-app/sdk";
105
+ import { useObjectSet, useOsdkClient, useStableObjectSet } from "@osdk/react";
106
+ import { useEffect, useState } from "react";
107
+
108
+ export function DepartmentPicker({ department }: { department: string }) {
109
+ const client = useOsdkClient();
110
+ const employees = useStableObjectSet(client(Employee).where({ department }));
111
+ const [selected, setSelected] = useState<ReadonlySet<number>>(new Set());
112
+
113
+ useEffect(() => {
114
+ setSelected(new Set());
115
+ }, [employees]);
116
+
117
+ const { data } = useObjectSet(employees, { $select: ["fullName"] });
118
+
119
+ return (
120
+ <ul>
121
+ {data?.map((employee) => (
122
+ <li key={employee.$primaryKey}>
123
+ <label>
124
+ <input
125
+ type="checkbox"
126
+ checked={selected.has(employee.$primaryKey)}
127
+ onChange={() =>
128
+ setSelected((prev) => {
129
+ const next = new Set(prev);
130
+ if (!next.delete(employee.$primaryKey)) next.add(employee.$primaryKey);
131
+ return next;
132
+ })
133
+ }
134
+ />
135
+ {employee.fullName}
136
+ </label>
137
+ </li>
138
+ ))}
139
+ </ul>
140
+ );
141
+ }
142
+ ```
143
+
144
+ The effect clears the selection only when `department` changes. Without `useStableObjectSet`, `employees` would be a new instance on every render, and the effect would run on every render. It also saves you from listing every input to the set in a `useMemo` dependency array.
145
+
146
+ ## Add derived properties
147
+
148
+ `withProperties` defines properties that the server computes for each object and returns alongside its regular properties. Pass it to `useOsdkObjects` or `useObjectSet`. Each entry is a function that receives a builder for the object and returns a definition:
149
+
150
+ ```tsx title="src/ManagerTable.tsx"
151
+ import { Employee } from "@my-app/sdk";
152
+ import { useOsdkObjects } from "@osdk/react";
153
+
154
+ export function ManagerTable() {
155
+ const { data } = useOsdkObjects(Employee, {
156
+ where: { department: "Engineering" },
157
+ withProperties: {
158
+ leadName: (employee) => employee.pivotTo("lead").selectProperty("fullName"),
159
+ officeCity: (employee) => employee.pivotTo("office").selectProperty("city"),
160
+ reportCount: (employee) => employee.pivotTo("reports").aggregate("$count"),
161
+ avgReportSalary: (employee) => employee.pivotTo("reports").aggregate("salary:avg"),
162
+ },
163
+ });
164
+
165
+ return (
166
+ <table>
167
+ <tbody>
168
+ {data?.map((employee) => (
169
+ <tr key={employee.$primaryKey}>
170
+ <td>{employee.fullName}</td>
171
+ <td>{employee.leadName ?? "None"}</td>
172
+ <td>{employee.officeCity}</td>
173
+ <td>{employee.reportCount}</td>
174
+ <td>{employee.avgReportSalary?.toFixed(0) ?? "No reports"}</td>
175
+ </tr>
176
+ ))}
177
+ </tbody>
178
+ </table>
179
+ );
180
+ }
181
+ ```
182
+
183
+ Each derived property is typed on the returned objects. Define them in the hook's `withProperties` option: if you call `.withProperties()` on the object set you pass to `useObjectSet` instead, the server still computes them but the returned objects are not typed with them.
184
+
185
+ ### The builder
186
+
187
+ | Method | Returns | Description |
188
+ | --- | --- | --- |
189
+ | `pivotTo(link)` | A builder for the linked type | Follow a link. After a link to many objects, you can only `aggregate`. |
190
+ | `where(clause)` | The same builder, filtered | Narrow the objects before you select or aggregate. |
191
+ | `selectProperty(property)` | A definition | The value of one property. Available on the starting object and after links to one object. |
192
+ | `aggregate(specifier, opts?)` | A definition | A value computed over the objects. |
193
+
194
+ `aggregate` takes `"$count"` or a `"property:metric"` string:
195
+
196
+ | Metric | Property types | Value | `opts` |
197
+ | --- | --- | --- | --- |
198
+ | `$count` | | `number` | |
199
+ | `sum`, `avg` | Numeric | `number \| undefined` | |
200
+ | `min`, `max` | Numeric, date, timestamp | Same as the property, or `undefined` | |
201
+ | `approximateDistinct`, `exactDistinct` | All | `number` | |
202
+ | `approximatePercentile` | Numeric | `number \| undefined` | `{ percentile: number }` |
203
+ | `collectList`, `collectSet` | Strings, numbers, dates, timestamps, booleans, geo, attachments | Array of the property's type, or `undefined` | `{ limit: number }` |
204
+
205
+ `sum`, `avg`, `min`, and `max` over no objects have no value, so render a fallback for them. `$count` is `0`.
206
+
207
+ Numeric definitions chain `add`, `subtract`, `multiply`, `divide`, `min`, `max`, `abs`, and `negate`, which take a number or another numeric definition. Date and timestamp definitions chain `min`, `max`, and `extractPart("DAYS" | "MONTHS" | "QUARTERS" | "YEARS")`.
208
+
209
+ ```tsx
210
+ const { data } = useOsdkObjects(Employee, {
211
+ withProperties: {
212
+ seniorReports: (employee) =>
213
+ employee
214
+ .pivotTo("reports")
215
+ .where({ jobTitle: { $startsWith: "Senior" } })
216
+ .aggregate("$count"),
217
+ reportPayroll: (employee) => employee.pivotTo("reports").aggregate("salary:sum"),
218
+ salaryAfterRaise: (employee) => employee.selectProperty("salary").multiply(1.05),
219
+ startYear: (employee) => employee.selectProperty("startDate").extractPart("YEARS"),
220
+ topReportTitles: (employee) =>
221
+ employee.pivotTo("reports").aggregate("jobTitle:collectSet", { limit: 5 }),
222
+ },
223
+ });
224
+ ```
225
+
226
+ ### Filter on a derived property
227
+
228
+ `where` can reference derived properties defined in the same options. For TypeScript to infer them, annotate the builder parameter with `DerivedProperty.Builder` from `@osdk/client`:
229
+
230
+ ```tsx title="src/ManagerList.tsx"
231
+ import { Employee } from "@my-app/sdk";
232
+ import type { DerivedProperty } from "@osdk/client";
233
+ import { useOsdkObjects } from "@osdk/react";
234
+
235
+ export function ManagerList() {
236
+ const { data } = useOsdkObjects(Employee, {
237
+ withProperties: {
238
+ reportCount: (employee: DerivedProperty.Builder<Employee, false>) =>
239
+ employee.pivotTo("reports").aggregate("$count"),
240
+ },
241
+ where: { reportCount: { $gt: 0 } },
242
+ });
243
+
244
+ return (
245
+ <ul>
246
+ {data?.map((manager) => (
247
+ <li key={manager.$primaryKey}>
248
+ {manager.fullName} ({manager.reportCount} reports)
249
+ </li>
250
+ ))}
251
+ </ul>
252
+ );
253
+ }
254
+ ```
255
+
256
+ The `false` type argument says that the builder has not yet followed a link to many objects. `orderBy` accepts only the type's own properties, so you cannot sort by a derived property.
257
+
258
+ Queries with derived properties do not receive real-time updates. See [Stream real-time updates](./objects.mdx#stream-real-time-updates).
259
+
260
+ ## Reference
261
+
262
+ ### `useObjectSet(objectSet, options?)`
263
+
264
+ `objectSet` is an `ObjectSet` or `undefined`. When it is `undefined`, the hook does not query.
265
+
266
+ | Option | Type | Default | Description |
267
+ | --- | --- | --- | --- |
268
+ | `where` | `WhereClause<T>` | | Filter the set. Can reference properties defined in `withProperties`. |
269
+ | `withProperties` | `DerivedProperty.Clause` | | Properties computed on the server, typed on the results. |
270
+ | `union` | `ObjectSet<T>[]` | | Add the objects in these sets. |
271
+ | `intersect` | `ObjectSet<T>[]` | | Keep only objects that are also in these sets. |
272
+ | `subtract` | `ObjectSet<T>[]` | | Remove the objects in these sets. |
273
+ | `pivotTo` | Link name | | Follow a link. Prefer calling `pivotTo` on the object set, which types the results as the linked type. |
274
+ | `orderBy` | `{ [property]?: "asc" \| "desc" }` | | Sort order, in key order. |
275
+ | `pageSize` | `number` | `100` | Objects per page. When several components observe the same query, requests use the largest `pageSize` among them. |
276
+ | `autoFetchMore` | `boolean \| number` | | Load pages automatically: `true` for all, a number for at least that many. |
277
+ | `$select` | `PropertyKeys<T>[]` | Server default set | Properties to load. Does not narrow the returned type. |
278
+ | `streamUpdates` | `boolean` | `false` | Subscribe to live changes. Not with `pivotTo` or `withProperties`. |
279
+ | `enabled` | `boolean` | `true` | When `false`, no request is made and `data` is `undefined`. |
280
+ | `dedupeIntervalMs` | `number` | `2000` | Identical queries within this window share one request. |
281
+
282
+ | Return | Type | Description |
283
+ | --- | --- | --- |
284
+ | `data` | `Osdk.Instance<T>[] \| undefined` | Loaded objects, with derived properties. Keeps the previous result while a changed query loads. |
285
+ | `isLoading` | `boolean` | `true` until the current query has loaded or failed. |
286
+ | `error` | `Error \| undefined` | The last error, if the query failed. |
287
+ | `isOptimistic` | `boolean` | Whether the list includes unconfirmed optimistic changes. |
288
+ | `fetchMore` | `(() => Promise<void>) \| undefined` | Load the next page. `undefined` when there are no more pages. |
289
+ | `hasMore` | `boolean` | Whether more pages exist. |
290
+ | `totalCount` | `string \| undefined` | Total matching objects, when the server reports it. |
291
+ | `objectSet` | `ObjectSet<T> \| undefined` | The final object set after all options are applied. |
292
+ | `refetch` | `() => Promise<void>` | Invalidate the object type and refetch. |
293
+
294
+ ### `useStableObjectSet(objectSet)`
295
+
296
+ Returns `objectSet`, or the instance returned on an earlier render if the definition is unchanged. Accepts and returns `ObjectSet | undefined`.