@palantir/pack.document-schema.model-types 0.1.0 → 0.2.0

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 (46) hide show
  1. package/.turbo/turbo-lint.log +1 -1
  2. package/.turbo/turbo-transpileBrowser.log +1 -1
  3. package/.turbo/turbo-transpileCjs.log +1 -1
  4. package/.turbo/turbo-transpileEsm.log +1 -1
  5. package/.turbo/turbo-transpileTypes.log +1 -1
  6. package/.turbo/turbo-typecheck.log +1 -1
  7. package/CHANGELOG.md +21 -0
  8. package/build/browser/index.js +14 -4
  9. package/build/browser/index.js.map +1 -1
  10. package/build/cjs/index.cjs +14 -4
  11. package/build/cjs/index.cjs.map +1 -1
  12. package/build/cjs/index.d.cts +413 -0
  13. package/build/esm/index.js +14 -4
  14. package/build/esm/index.js.map +1 -1
  15. package/build/types/__tests__/Metadata.test.d.ts +1 -0
  16. package/build/types/__tests__/Metadata.test.d.ts.map +1 -0
  17. package/build/types/types/ActivityEvent.d.ts +37 -0
  18. package/build/types/types/ActivityEvent.d.ts.map +1 -1
  19. package/build/types/types/DocumentRef.d.ts +185 -0
  20. package/build/types/types/DocumentRef.d.ts.map +1 -1
  21. package/build/types/types/DocumentSchema.d.ts +6 -0
  22. package/build/types/types/DocumentSchema.d.ts.map +1 -1
  23. package/build/types/types/Model.d.ts +9 -0
  24. package/build/types/types/Model.d.ts.map +1 -1
  25. package/build/types/types/PresenceEvent.d.ts +43 -0
  26. package/build/types/types/PresenceEvent.d.ts.map +1 -1
  27. package/build/types/types/RecordCollectionRef.d.ts +54 -0
  28. package/build/types/types/RecordCollectionRef.d.ts.map +1 -1
  29. package/build/types/types/RecordRef.d.ts +71 -0
  30. package/build/types/types/RecordRef.d.ts.map +1 -1
  31. package/build/types/types/Unsubscribe.d.ts +3 -0
  32. package/build/types/types/Unsubscribe.d.ts.map +1 -1
  33. package/build/types/types/UserRef.d.ts +5 -0
  34. package/build/types/types/UserRef.d.ts.map +1 -1
  35. package/package.json +3 -3
  36. package/src/__tests__/Metadata.test.ts +43 -0
  37. package/src/types/ActivityEvent.ts +37 -0
  38. package/src/types/DocumentRef.ts +192 -0
  39. package/src/types/DocumentSchema.ts +6 -0
  40. package/src/types/Metadata.ts +20 -5
  41. package/src/types/Model.ts +9 -0
  42. package/src/types/PresenceEvent.ts +43 -0
  43. package/src/types/RecordCollectionRef.ts +54 -0
  44. package/src/types/RecordRef.ts +74 -0
  45. package/src/types/Unsubscribe.ts +3 -0
  46. package/src/types/UserRef.ts +5 -0
@@ -1 +1 @@
1
- {"mappings":"AAgBA,cAAc,gBAAgB;AAC9B,cAAc,OAAO,iBAAiB;AACtC,cAAc,cAAc;AAE5B,YAAY,kBAAkB,SAAS;AAEvC,OAAO,cAAM;UACX,cAAc;UACd,SAAS;;AAGX,iBAAiB,wBAAwB,UAAU,QAAQ,OAAO;UACvD,aAAa,sBAAsB;UACnC,OAAO;UACP,WAAW,UAAU;;AAKhC,iBAAiB,yBAAyB;UAC/B,MAAM;UACN;UACA;;AAGX,YAAY,oBAAoB,0BAA0B;AAE1D,iBAAiB,cAAc;UACpB;UACA,WAAW;UACX;UACA,WAAW;UACX,SAAS;UACT","names":[],"sources":["../../../src/types/ActivityEvent.ts"],"version":3,"file":"ActivityEvent.d.ts"}
1
+ {"mappings":"AAgBA,cAAc,gBAAgB;AAC9B,cAAc,OAAO,iBAAiB;AACtC,cAAc,cAAc;AAE5B,YAAY,kBAAkB,SAAS;AAEvC,OAAO,cAAM;UACX,cAAc;UACd,SAAS;;;;;;;;;;;;;;;;;;;;;;;AAwBX,iBAAiB,wBAAwB,UAAU,QAAQ,OAAO;UACvD,aAAa,sBAAsB;UACnC,OAAO;UACP,WAAW,UAAU;;;;;;;;;;AAahC,iBAAiB,yBAAyB;UAC/B,MAAM;UACN;UACA;;AAGX,YAAY,oBAAoB,0BAA0B;;;;;;;;AAS1D,iBAAiB,cAAc;;UAEpB;UACA,WAAW;UACX;UACA,WAAW;UACX,SAAS;UACT","names":[],"sources":["../../../src/types/ActivityEvent.ts"],"version":3,"file":"ActivityEvent.d.ts"}
@@ -7,20 +7,205 @@ import type { PresenceEvent } from "./PresenceEvent.js";
7
7
  import type { RecordCollectionRef } from "./RecordCollectionRef.js";
8
8
  import type { Unsubscribe } from "./Unsubscribe.js";
9
9
  export type DocumentId = Flavored<"DocumentId">;
10
+ /**
11
+ * Options for subscribing to presence events on a document.
12
+ */
10
13
  export interface PresenceSubscriptionOptions {
14
+ /**
15
+ * If true, presence events originating from the local user will be ignored.
16
+ *
17
+ * @default true
18
+ */
11
19
  readonly ignoreSelfUpdates?: boolean;
12
20
  }
13
21
  export declare const DocumentRefBrand: unique symbol;
22
+ /**
23
+ * A reference to a document in the Pack system.
24
+ *
25
+ * A documentRef returned by various interfaces from the pack app instance or
26
+ * utilities such as react hooks from @palantir/pack.state.react provides
27
+ * methods to interact with the document, such as subscribing to & making
28
+ * changes to the document state and also related activity or presence events.
29
+ *
30
+ * A stable documentRef object is guaranteed for the same document id within the
31
+ * same app instance.
32
+ */
14
33
  export interface DocumentRef<D extends DocumentSchema = DocumentSchema> {
15
34
  readonly id: DocumentId;
16
35
  readonly schema: D;
17
36
  readonly [DocumentRefBrand]: typeof DocumentRefBrand;
37
+ /**
38
+ * Get a snapshot of the current document state.
39
+ *
40
+ * This is largely for debugging and dumps a json view of the whole document state.
41
+ *
42
+ * @experimental
43
+ */
18
44
  getDocSnapshot(): Promise<DocumentState<D>>;
45
+ /**
46
+ * Get or create a ref to the collection of all records for the specified
47
+ * model in this document.
48
+ *
49
+ * @param model The model type from the application's generated schema.
50
+ * @returns A stable {@link RecordCollectionRef} object providing an interface
51
+ * to interact with the records of the specified model type in this document.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * import { DocumentModel, MyModel } from "@myapp/schema";
56
+ * import { app } from "./appInstance";
57
+ *
58
+ * const docRef = app.state.createDocRef(DocumentModel, someDocumentId);
59
+ * const myRecordCollection = docRef.getRecords(MyModel);
60
+ *
61
+ * myRecordCollection.onItemsAdded((items) => {
62
+ * console.log("New records added:", items);
63
+ * })
64
+ * ```
65
+ */
19
66
  getRecords<R extends Model>(model: R): RecordCollectionRef<R>;
67
+ /**
68
+ * Subscribe to activity events for the document.
69
+ *
70
+ * Activity is used by applications to describe changes to the document, for
71
+ * populating activity feeds, change histories, notifications and similar
72
+ * features.
73
+ *
74
+ * @see {withTransaction} for making edits that generate activity events.
75
+ *
76
+ * @returns An unsubscribe function.
77
+ * @example
78
+ * ```ts
79
+ * const unsubscribe = docRef.onActivity((docRef, event) => {
80
+ * console.log("Activity event:", event);
81
+ * });
82
+ *
83
+ * // Submit an edit with a description to generate an activity event.
84
+ * docRef.withTransaction(() => {
85
+ * // make some edits to the document here
86
+ * }, {
87
+ * model: MyEventModel,
88
+ * data: {
89
+ * myDataField: "some value",
90
+ * foo: 42,
91
+ * },
92
+ * });
93
+ *
94
+ * // Later, to unsubscribe:
95
+ * unsubscribe();
96
+ * ```
97
+ */
20
98
  onActivity(callback: (docRef: DocumentRef<D>, event: ActivityEvent) => void): Unsubscribe;
99
+ /**
100
+ * Subscribe to metadata changes for the document.
101
+ *
102
+ * @returns An unsubscribe function.
103
+ * @example
104
+ * ```ts
105
+ * const unsubscribe = docRef.onMetadataChange((docRef, metadata) => {
106
+ * console.log("Metadata changed:", metadata);
107
+ * });
108
+ *
109
+ * // Later, to unsubscribe:
110
+ * unsubscribe();
111
+ * ```
112
+ */
21
113
  onMetadataChange(callback: (docRef: DocumentRef<D>, metadata: DocumentMetadata) => void): Unsubscribe;
114
+ /**
115
+ * Subscribe to presence events for the document.
116
+ *
117
+ * Presence events are a way to broadcast and receive non-persisted awareness
118
+ * and presence events. For example, a user moving their cursor in a
119
+ * collaborative editor could be broadcast via custom presence events.
120
+ *
121
+ * @see {@link updateCustomPresence} to publish custom presence updates.
122
+ *
123
+ * @param callback The callback to invoke on presence events.
124
+ * @param options Options for the presence subscription.
125
+ * @returns An unsubscribe function.
126
+ * @example
127
+ * ```ts
128
+ * const unsubscribe = docRef.onPresence((docRef, event) => {
129
+ * console.log("Presence event:", event);
130
+ * if (event.model === MyPresenceModel) {
131
+ * updateCursor((event.eventData as ModelData<MyPresenceModel>).cursorPosition);
132
+ * }
133
+ * }, { ignoreSelfUpdates: true });
134
+ *
135
+ * // Broadcast a presence update
136
+ * docRef.updateCustomPresence(MyPresenceModel, {
137
+ * cursorPosition: [42, 7],
138
+ * });
139
+ *
140
+ * // Later, to unsubscribe:
141
+ * unsubscribe();
142
+ * ```
143
+ */
22
144
  onPresence(callback: (docRef: DocumentRef<D>, event: PresenceEvent) => void, options?: PresenceSubscriptionOptions): Unsubscribe;
145
+ /**
146
+ * Subscribe to be notified any time the document state changes.
147
+ * This is largely for testing purposes.
148
+ *
149
+ * @experimental
150
+ *
151
+ * @param callback The callback to invoke on state changes.
152
+ * @returns An unsubscribe function.
153
+ * @example
154
+ * ```ts
155
+ * const unsubscribe = docRef.onStateChange((docRef) => {
156
+ * console.log("Document state changed:", docRef);
157
+ * });
158
+ *
159
+ * // Later, to unsubscribe:
160
+ * unsubscribe();
161
+ * ```
162
+ */
23
163
  onStateChange(callback: (docRef: DocumentRef<D>) => void): Unsubscribe;
164
+ /**
165
+ * Broadcasts an update for the specified model as presence data to other
166
+ * subscribers.
167
+ *
168
+ * Presence data is ephemeral and not stored as part of the document state. It
169
+ * is intended for broadcasting transient user presence and awareness
170
+ * information. Each different model type used for presence is expected to
171
+ * update the latest 'presence state' for that model type.
172
+ *
173
+ * @see {@link onPresence} to subscribe to presence updates.
174
+ *
175
+ * @param model The model type to update presence for.
176
+ * @param eventData The new presence data for the model.
177
+ */
24
178
  updateCustomPresence<M extends Model = Model>(model: M, eventData: ModelData<M>): void;
179
+ /**
180
+ * Execute one or more document edits within a transaction, optionally providing
181
+ * a description of the edit for activity tracking.
182
+ *
183
+ * All edits made within the provided function will be treated as a single
184
+ * atomic edit operation. If a description is provided, an activity event will
185
+ * be generated for the edit.
186
+ *
187
+ * If this is called within an existing transaction, the inner edits will be
188
+ * included in the outer transaction only, and the inner descriptions will be
189
+ * discarded.
190
+ *
191
+ * @see {@link onActivity} to subscribe to activity events on this document.
192
+ *
193
+ * @param fn A lambda including some document edits.
194
+ * @param description Optional description of the edit for activity tracking.
195
+ *
196
+ * @example
197
+ * ```ts
198
+ * docRef.withTransaction(() => {
199
+ * const myRecords = docRef.getRecords(MyModel);
200
+ * myRecords.set("record-1", { field: "new value" });
201
+ * myRecords.delete("record-2");
202
+ * }, {
203
+ * model: MyEditEventModel,
204
+ * data: {
205
+ * summary: "Updated record-1 and deleted record-2",
206
+ * },
207
+ * });
208
+ * ```
209
+ */
25
210
  withTransaction(fn: () => void, description?: EditDescription): void;
26
211
  }
@@ -1 +1 @@
1
- {"mappings":"AAgBA,cAAc,gBAAgB;AAC9B,cAAc,qBAAqB;AACnC,cAAc,wBAAwB;AACtC,cAAc,gBAAgB,qBAAqB;AACnD,cAAc,iBAAiB,OAAO,iBAAiB;AACvD,cAAc,qBAAqB;AACnC,cAAc,2BAA2B;AACzC,cAAc,mBAAmB;AAEjC,YAAY,aAAa,SAAS;AAElC,iBAAiB,4BAA4B;UAClC;;AAGX,OAAO,cAAMA;AAEb,iBAAiB,YAAY,UAAU,iBAAiB,gBAAgB;UAC7D,IAAI;UACJ,QAAQ;WACP,0BAA0B;CAEpC,kBAAkB,QAAQ,cAAc;CACxC,WAAW,UAAU,OAAO,OAAO,IAAI,oBAAoB;CAC3D,WACE,WAAW,QAAQ,YAAY,IAAI,OAAO,yBACzC;CACH,iBACE,WAAW,QAAQ,YAAY,IAAI,UAAU,4BAC5C;CACH,WACE,WAAW,QAAQ,YAAY,IAAI,OAAO,wBAC1C,UAAU,8BACT;CACH,cACE,WAAW,QAAQ,YAAY,cAC9B;CACH,qBAAqB,UAAU,QAAQ,OACrC,OAAO,GACP,WAAW,UAAU;CAEvB,gBAAgB,gBAAgB,cAAc","names":["DocumentRefBrand: unique symbol"],"sources":["../../../src/types/DocumentRef.ts"],"version":3,"file":"DocumentRef.d.ts"}
1
+ {"mappings":"AAgBA,cAAc,gBAAgB;AAC9B,cAAc,qBAAqB;AACnC,cAAc,wBAAwB;AACtC,cAAc,gBAAgB,qBAAqB;AACnD,cAAc,iBAAiB,OAAO,iBAAiB;AACvD,cAAc,qBAAqB;AACnC,cAAc,2BAA2B;AACzC,cAAc,mBAAmB;AAEjC,YAAY,aAAa,SAAS;;;;AAKlC,iBAAiB,4BAA4B;;;;;;UAMlC;;AAGX,OAAO,cAAMA;;;;;;;;;;;;AAab,iBAAiB,YAAY,UAAU,iBAAiB,gBAAgB;UAC7D,IAAI;UACJ,QAAQ;WACP,0BAA0B;;;;;;;;CASpC,kBAAkB,QAAQ,cAAc;;;;;;;;;;;;;;;;;;;;;;CAuBxC,WAAW,UAAU,OAAO,OAAO,IAAI,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiC3D,WACE,WAAW,QAAQ,YAAY,IAAI,OAAO,yBACzC;;;;;;;;;;;;;;;CAgBH,iBACE,WAAW,QAAQ,YAAY,IAAI,UAAU,4BAC5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgCH,WACE,WAAW,QAAQ,YAAY,IAAI,OAAO,wBAC1C,UAAU,8BACT;;;;;;;;;;;;;;;;;;;CAoBH,cACE,WAAW,QAAQ,YAAY,cAC9B;;;;;;;;;;;;;;;CAgBH,qBAAqB,UAAU,QAAQ,OACrC,OAAO,GACP,WAAW,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkCvB,gBAAgB,gBAAgB,cAAc","names":["DocumentRefBrand: unique symbol"],"sources":["../../../src/types/DocumentRef.ts"],"version":3,"file":"DocumentRef.d.ts"}
@@ -1,8 +1,14 @@
1
1
  import type { WithMetadata } from "./Metadata.js";
2
2
  import type { Model, ModelData } from "./Model.js";
3
+ /**
4
+ * The base type for an application sdk's generated document schema.
5
+ */
3
6
  export interface DocumentSchema extends WithMetadata<DocumentSchemaMetadata> {
4
7
  readonly [modelName: string]: Model;
5
8
  }
9
+ /**
10
+ * The plain object representation of the state of a document.
11
+ */
6
12
  export type DocumentState<S extends DocumentSchema> = { readonly [K in Exclude<keyof S, symbol>] : {
7
13
  readonly [key: string]: ModelData<S[K]>;
8
14
  } };
@@ -1 +1 @@
1
- {"mappings":"AAgBA,cAAc,oBAAoB;AAClC,cAAc,OAAO,iBAAiB;AAEtC,iBAAiB,uBAAuB,aAAa,wBAAwB;+BAC7C;;AAGhC,YAAY,cAAc,UAAU,8BACxB,KAAK,cAAc,cAAa;yBAA0B,UAAU,EAAE;;AAGlF,iBAAiB,uBAAuB;UAC7B","names":[],"sources":["../../../src/types/DocumentSchema.ts"],"version":3,"file":"DocumentSchema.d.ts"}
1
+ {"mappings":"AAgBA,cAAc,oBAAoB;AAClC,cAAc,OAAO,iBAAiB;;;;AAKtC,iBAAiB,uBAAuB,aAAa,wBAAwB;+BAC7C;;;;;AAMhC,YAAY,cAAc,UAAU,8BACxB,KAAK,cAAc,cAAa;yBAA0B,UAAU,EAAE;;AAGlF,iBAAiB,uBAAuB;UAC7B","names":[],"sources":["../../../src/types/DocumentSchema.ts"],"version":3,"file":"DocumentSchema.d.ts"}
@@ -13,6 +13,9 @@ export interface Model<
13
13
  readonly zodSchema: Readonly<Z>;
14
14
  }
15
15
  export type ModelData<M extends Model> = M["__type"];
16
+ /**
17
+ * Describes an edit made to a document.
18
+ */
16
19
  export interface EditDescription<M extends Model = Model> {
17
20
  readonly data: ModelData<M>;
18
21
  readonly model: M;
@@ -25,6 +28,12 @@ export declare const ExternalRefType: {
25
28
  };
26
29
  export type ExternalRefType = typeof ExternalRefType[keyof typeof ExternalRefType];
27
30
  export interface ModelMetadata<T = unknown> {
31
+ /**
32
+ * Which fields in the model are external references (e.g. UserRef, DocumentRef, etc).
33
+ */
28
34
  readonly externalRefFieldTypes?: Readonly<Record<keyof T, ExternalRefType>>;
35
+ /**
36
+ * The name of the model (should match the typescript symbol).
37
+ */
29
38
  readonly name: string;
30
39
  }
@@ -1 +1 @@
1
- {"mappings":"AAgBA,cAAc,eAAe;AAC7B,cAAc,oBAAoB;;;;;;AAQlC,iBAAiB;CAAM;CAAa,UAAU,QAAQ,KAAK,QAAQ;UACzD,aAAa,cAAc,IACrC;UACW,QAAQ;UACR,WAAW,SAAS;;AAG/B,YAAY,UAAU,UAAU,SAAS,EAAE;AAE3C,iBAAiB,gBAAgB,UAAU,QAAQ,OAAO;UAC/C,MAAM,UAAU;UAChB,OAAO;;AAGlB,OAAO,cAAM;UACX,SAAS;UACT,WAAW;UACX,YAAY;UACZ,UAAU;;AAGZ,YAAY,yBAAyB,6BAA6B;AAElE,iBAAiB,cAAc,aAAa;UACjC,wBAAwB,SAAS,aAAa,GAAG;UACjD","names":[],"sources":["../../../src/types/Model.ts"],"version":3,"file":"Model.d.ts"}
1
+ {"mappings":"AAgBA,cAAc,eAAe;AAC7B,cAAc,oBAAoB;;;;;;AAQlC,iBAAiB;CAAM;CAAa,UAAU,QAAQ,KAAK,QAAQ;UACzD,aAAa,cAAc,IACrC;UACW,QAAQ;UACR,WAAW,SAAS;;AAG/B,YAAY,UAAU,UAAU,SAAS,EAAE;;;;AAK3C,iBAAiB,gBAAgB,UAAU,QAAQ,OAAO;UAC/C,MAAM,UAAU;UAChB,OAAO;;AAGlB,OAAO,cAAM;UACX,SAAS;UACT,WAAW;UACX,YAAY;UACZ,UAAU;;AAGZ,YAAY,yBAAyB,6BAA6B;AAElE,iBAAiB,cAAc,aAAa;;;;UAIjC,wBAAwB,SAAS,aAAa,GAAG;;;;UAIjD","names":[],"sources":["../../../src/types/Model.ts"],"version":3,"file":"Model.d.ts"}
@@ -7,23 +7,66 @@ export declare const PresenceEventDataType: {
7
7
  readonly UNKNOWN: "unknown";
8
8
  };
9
9
  export type PresenceEventDataType = typeof PresenceEventDataType[keyof typeof PresenceEventDataType];
10
+ /**
11
+ * Any client that subscribes to presence events via `DocumentRef.onPresence` will
12
+ * be considered 'present' on the document, and trigger an 'arrived' presence event.
13
+ * When they disconnect or unsubscribe from presence events, a 'departed' presence event
14
+ * will be triggered.
15
+ */
10
16
  export interface PresenceEventDataArrived {
11
17
  readonly type: typeof PresenceEventDataType.ARRIVED;
12
18
  }
19
+ /**
20
+ * Any client that subscribes to presence events via `DocumentRef.onPresence` will
21
+ * be considered 'present' on the document, and trigger an 'arrived' presence event.
22
+ * When they disconnect or unsubscribe from presence events, a 'departed' presence event
23
+ * will be triggered.
24
+ */
13
25
  export interface PresenceEventDataDeparted {
14
26
  readonly type: typeof PresenceEventDataType.DEPARTED;
15
27
  }
28
+ /**
29
+ * Application specific custom presence event data.
30
+ *
31
+ * Each different model type used for presence is expected to update the latest
32
+ * 'presence state' for that model type.
33
+ *
34
+ * For example, your app may have need to broadcast user cursor positions and
35
+ * selection ranges as presence data. You could define your schema to include a
36
+ * `CursorPosition` and `SelectionRange` record types, and set them
37
+ * independently via `{@link DocumentRef.updateCustomPresence}`. Each model type
38
+ * sent as a custom presence event should be considered a separate 'channel' of
39
+ * presence data on this document.
40
+ */
16
41
  export interface PresenceEventDataCustom<M extends Model = Model> {
17
42
  readonly type: typeof PresenceEventDataType.CUSTOM_EVENT;
18
43
  readonly eventData: ModelData<M>;
19
44
  readonly model: M;
20
45
  }
46
+ /**
47
+ * Fallback for unrecognized activity event types.
48
+ *
49
+ * This allows some flexibility with new event types added to the platform.
50
+ * Likely unknown events represent a new platform event type and will be handled
51
+ * in a future release of pack libraries and can be safely ignored by
52
+ * applications.
53
+ */
21
54
  export interface PresenceEventUnknown {
22
55
  readonly type: "unknown";
23
56
  readonly rawType: string;
24
57
  readonly rawData: unknown;
25
58
  }
26
59
  export type PresenceEventData = PresenceEventDataArrived | PresenceEventDataDeparted | PresenceEventDataCustom | PresenceEventUnknown;
60
+ /**
61
+ * An event representing a transient awareness or presence change for a user on this document.
62
+ * The presence channel is intended for ephemeral data such as user cursors, selections, or
63
+ * other live collaboration indicators.
64
+ *
65
+ * PresenceEvents are not persisted in document history.
66
+ *
67
+ * When a client goes offline, its presence is considered departed and any presence events
68
+ * associated with that user should be considered stale and / or cleared.
69
+ */
27
70
  export interface PresenceEvent {
28
71
  readonly userId: UserId;
29
72
  readonly eventData: PresenceEventData;
@@ -1 +1 @@
1
- {"mappings":"AAgBA,cAAc,OAAO,iBAAiB;AACtC,cAAc,cAAc;AAE5B,OAAO,cAAM;UACX,SAAS;UACT,UAAU;UACV,cAAc;UACd,SAAS;;AAGX,YAAY,+BACH,mCAAmC;AAE5C,iBAAiB,yBAAyB;UAC/B,aAAa,sBAAsB;;AAG9C,iBAAiB,0BAA0B;UAChC,aAAa,sBAAsB;;AAG9C,iBAAiB,wBAAwB,UAAU,QAAQ,OAAO;UACvD,aAAa,sBAAsB;UACnC,WAAW,UAAU;UACrB,OAAO;;AAGlB,iBAAiB,qBAAqB;UAC3B,MAAM;UACN;UACA;;AAGX,YAAY,oBACR,2BACA,4BACA,0BACA;AAEJ,iBAAiB,cAAc;UACpB,QAAQ;UACR,WAAW","names":[],"sources":["../../../src/types/PresenceEvent.ts"],"version":3,"file":"PresenceEvent.d.ts"}
1
+ {"mappings":"AAgBA,cAAc,OAAO,iBAAiB;AACtC,cAAc,cAAc;AAE5B,OAAO,cAAM;UACX,SAAS;UACT,UAAU;UACV,cAAc;UACd,SAAS;;AAGX,YAAY,+BACH,mCAAmC;;;;;;;AAQ5C,iBAAiB,yBAAyB;UAC/B,aAAa,sBAAsB;;;;;;;;AAS9C,iBAAiB,0BAA0B;UAChC,aAAa,sBAAsB;;;;;;;;;;;;;;;AAgB9C,iBAAiB,wBAAwB,UAAU,QAAQ,OAAO;UACvD,aAAa,sBAAsB;UACnC,WAAW,UAAU;UACrB,OAAO;;;;;;;;;;AAWlB,iBAAiB,qBAAqB;UAC3B,MAAM;UACN;UACA;;AAGX,YAAY,oBACR,2BACA,4BACA,0BACA;;;;;;;;;;;AAYJ,iBAAiB,cAAc;UACpB,QAAQ;UACR,WAAW","names":[],"sources":["../../../src/types/PresenceEvent.ts"],"version":3,"file":"PresenceEvent.d.ts"}
@@ -2,17 +2,71 @@ import type { DocumentRef } from "./DocumentRef.js";
2
2
  import type { Model, ModelData } from "./Model.js";
3
3
  import type { RecordId, RecordRef } from "./RecordRef.js";
4
4
  export declare const RecordCollectionRefBrand: unique symbol;
5
+ /**
6
+ * A reference providing an API to interact with a collection of records in a document.
7
+ */
5
8
  export interface RecordCollectionRef<M extends Model = Model> {
6
9
  readonly docRef: DocumentRef;
7
10
  readonly model: M;
8
11
  readonly [RecordCollectionRefBrand]: typeof RecordCollectionRefBrand;
12
+ /**
13
+ * Delete a record from the collection (and the document).
14
+ *
15
+ * @param id - The ID of the record to delete.
16
+ * @returns A promise that resolves when the record is deleted.
17
+ */
9
18
  delete(id: RecordId): Promise<void>;
19
+ /**
20
+ * Get a {@link RecordRef} from the collection. This provides the main API for
21
+ * accessing and updating individual records in a document.
22
+ *
23
+ * @param id - The ID of the record to get.
24
+ * @returns The record reference, or undefined if it doesn't exist. The
25
+ * recordRef is a stable object and can be used for reference equality checks.
26
+ */
10
27
  get(id: RecordId): RecordRef<M> | undefined;
28
+ /**
29
+ * Check if a record exists in the collection.
30
+ *
31
+ * @param id - The ID of the record to check.
32
+ * @returns True if the record exists, false otherwise.
33
+ */
11
34
  has(id: RecordId): boolean;
35
+ /**
36
+ * Set the data for a record in the collection (creating it if it doesn't exist).
37
+ *
38
+ * @param id - The ID of the record to set.
39
+ * @param state - The data to set for the record.
40
+ * @returns A promise that resolves when the record is set.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const recordCollection = docRef.getRecords(MyModel);
45
+ * await recordCollection.set("record-id", { field: "value" });
46
+ * ```
47
+ */
12
48
  set(id: RecordId, state: ModelData<M>): Promise<void>;
13
49
  readonly size: number;
14
50
  [Symbol.iterator](): Iterator<RecordRef<M>>;
51
+ /**
52
+ * Subscribe to added items in the collection.
53
+ *
54
+ * @param callback - The callback to invoke when items are added.
55
+ * @returns An unsubscribe function.
56
+ */
15
57
  readonly onItemsAdded: (callback: (items: readonly RecordRef<M>[]) => void) => () => void;
58
+ /**
59
+ * Subscribe to changed items in the collection.
60
+ *
61
+ * @param callback - The callback to invoke when items are changed.
62
+ * @returns An unsubscribe function.
63
+ */
16
64
  readonly onItemsChanged: (callback: (items: readonly RecordRef<M>[]) => void) => () => void;
65
+ /**
66
+ * Subscribe to deleted items in the collection.
67
+ *
68
+ * @param callback - The callback to invoke when items are deleted.
69
+ * @returns An unsubscribe function.
70
+ */
17
71
  readonly onItemsDeleted: (callback: (items: readonly RecordRef<M>[]) => void) => () => void;
18
72
  }
@@ -1 +1 @@
1
- {"mappings":"AAgBA,cAAc,mBAAmB;AACjC,cAAc,OAAO,iBAAiB;AACtC,cAAc,UAAU,iBAAiB;AAEzC,OAAO,cAAMA;AAIb,iBAAiB,oBAAoB,UAAU,QAAQ,OAAO;UACnD,QAAQ;UACR,OAAO;WACN,kCAAkC;CAE5C,OAAO,IAAI,WAAW;CACtB,IAAI,IAAI,WAAW,UAAU;CAC7B,IAAI,IAAI;CACR,IAAI,IAAI,UAAU,OAAO,UAAU,KAAK;UAC/B;EAER,OAAO,aAAa,SAAS,UAAU;UAE/B,eACP,WAAW,gBAAgB,UAAU;UAG9B,iBACP,WAAW,gBAAgB,UAAU;UAG9B,iBACP,WAAW,gBAAgB,UAAU","names":["RecordCollectionRefBrand: unique symbol"],"sources":["../../../src/types/RecordCollectionRef.ts"],"version":3,"file":"RecordCollectionRef.d.ts"}
1
+ {"mappings":"AAgBA,cAAc,mBAAmB;AACjC,cAAc,OAAO,iBAAiB;AACtC,cAAc,UAAU,iBAAiB;AAEzC,OAAO,cAAMA;;;;AAOb,iBAAiB,oBAAoB,UAAU,QAAQ,OAAO;UACnD,QAAQ;UACR,OAAO;WACN,kCAAkC;;;;;;;CAQ5C,OAAO,IAAI,WAAW;;;;;;;;;CAStB,IAAI,IAAI,WAAW,UAAU;;;;;;;CAO7B,IAAI,IAAI;;;;;;;;;;;;;;CAcR,IAAI,IAAI,UAAU,OAAO,UAAU,KAAK;UAC/B;EAER,OAAO,aAAa,SAAS,UAAU;;;;;;;UAQ/B,eACP,WAAW,gBAAgB,UAAU;;;;;;;UAS9B,iBACP,WAAW,gBAAgB,UAAU;;;;;;;UAS9B,iBACP,WAAW,gBAAgB,UAAU","names":["RecordCollectionRefBrand: unique symbol"],"sources":["../../../src/types/RecordCollectionRef.ts"],"version":3,"file":"RecordCollectionRef.d.ts"}
@@ -4,13 +4,84 @@ import type { Model, ModelData } from "./Model.js";
4
4
  import type { Unsubscribe } from "./Unsubscribe.js";
5
5
  export type RecordId = Flavored<"RecordId">;
6
6
  export declare const RecordRefBrand: unique symbol;
7
+ /**
8
+ * A reference providing an API to interact with a specific record in a
9
+ * document. This is the main interface for accessing and updating individual
10
+ * records.
11
+ *
12
+ * These will be created by docRef or recordCollectionRef APIs for example and
13
+ * should not be created manually. RecordRefs are stable objects that can be
14
+ * used for reference equality checks.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { DocumentRef, DocumentSchema, MyModel } from "@myapp/schema";
19
+ * import { app } from "./appInstance";
20
+ *
21
+ * const docRef = app.getDocRef<DocumentSchema>(someDocumentId);
22
+ *
23
+ * const recordRef = docRef.getRecords(MyModel).set("my-record-id", { myFieldName: "some value", foo: 42 });
24
+ *
25
+ * // Or get a specific record.
26
+ * const recordRef = docRef.getRecords(MyModel).get("my-record-id");
27
+ */
7
28
  export interface RecordRef<M extends Model = Model> {
8
29
  readonly docRef: DocumentRef;
9
30
  readonly id: RecordId;
10
31
  readonly model: M;
11
32
  readonly [RecordRefBrand]: typeof RecordRefBrand;
33
+ /**
34
+ * Get the current state of the record in a plain object.
35
+ * If there is an active subscription to the document this is the current state of the record in memory.
36
+ * Otherwise, this will fetch the latest state from the server.
37
+ */
12
38
  getSnapshot(): Promise<ModelData<M>>;
39
+ /**
40
+ * Subscribe to changes in the record.
41
+ * @param callback The callback to invoke when the record changes.
42
+ * @returns An unsubscribe function.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * // Subscribe to changes
47
+ * recordRef.onChange((newSnapshot, recordRef) => {
48
+ * console.log("Record changed:", newSnapshot);
49
+ * });
50
+ *
51
+ * // Submit a change
52
+ * recordRef.set({ myFieldName: "new value" });
53
+ * ```
54
+ */
13
55
  onChange(callback: (snapshot: ModelData<M>, recordRef: RecordRef<M>) => void): Unsubscribe;
56
+ /**
57
+ * Subscribe to deletion of the record.
58
+ * @param callback The callback to invoke when the record is deleted.
59
+ * @returns An unsubscribe function.
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * // Subscribe to deletion
64
+ * recordRef.onDeleted((recordRef) => {
65
+ * console.log("Record deleted:", recordRef.id);
66
+ * });
67
+ *
68
+ * // Trigger the deletion
69
+ * docRef.getRecords(MyModel).delete(recordRef.id);
70
+ * ```
71
+ */
14
72
  onDeleted(callback: (recordRef: RecordRef<M>) => void): Unsubscribe;
73
+ /**
74
+ * Set the data for the record (creating it if it doesn't exist).
75
+ *
76
+ * @see {onChange} to subscribe to changes to the record.
77
+ *
78
+ * @param record - The plain object data to set for the record, corresponding to the model's schema.
79
+ * @returns An ignorable promise that resolves when the record is published.
80
+ *
81
+ * @example
82
+ * ```ts
83
+ * await recordRef.set({ field: "value" });
84
+ * ```
85
+ */
15
86
  set(record: ModelData<M>): Promise<void>;
16
87
  }
@@ -1 +1 @@
1
- {"mappings":"AAgBA,cAAc,gBAAgB;AAC9B,cAAc,mBAAmB;AACjC,cAAc,OAAO,iBAAiB;AACtC,cAAc,mBAAmB;AAEjC,YAAY,WAAW,SAAS;AAEhC,OAAO,cAAMA;AAEb,iBAAiB,UAAU,UAAU,QAAQ,OAAO;UACzC,QAAQ;UACR,IAAI;UACJ,OAAO;WACN,wBAAwB;CAElC,eAAe,QAAQ,UAAU;CACjC,SAAS,WAAW,UAAU,UAAU,IAAI,WAAW,UAAU,cAAc;CAC/E,UAAU,WAAW,WAAW,UAAU,cAAc;CACxD,IAAI,QAAQ,UAAU,KAAK","names":["RecordRefBrand: unique symbol"],"sources":["../../../src/types/RecordRef.ts"],"version":3,"file":"RecordRef.d.ts"}
1
+ {"mappings":"AAgBA,cAAc,gBAAgB;AAC9B,cAAc,mBAAmB;AACjC,cAAc,OAAO,iBAAiB;AACtC,cAAc,mBAAmB;AAEjC,YAAY,WAAW,SAAS;AAEhC,OAAO,cAAMA;;;;;;;;;;;;;;;;;;;;;;AAuBb,iBAAiB,UAAU,UAAU,QAAQ,OAAO;UACzC,QAAQ;UACR,IAAI;UACJ,OAAO;WACN,wBAAwB;;;;;;CAOlC,eAAe,QAAQ,UAAU;;;;;;;;;;;;;;;;;CAkBjC,SAAS,WAAW,UAAU,UAAU,IAAI,WAAW,UAAU,cAAc;;;;;;;;;;;;;;;;;CAkB/E,UAAU,WAAW,WAAW,UAAU,cAAc;;;;;;;;;;;;;;CAexD,IAAI,QAAQ,UAAU,KAAK","names":["RecordRefBrand: unique symbol"],"sources":["../../../src/types/RecordRef.ts"],"version":3,"file":"RecordRef.d.ts"}
@@ -1 +1,4 @@
1
+ /**
2
+ * A function that can be called to unsubscribe from a previously subscribed event interface.
3
+ */
1
4
  export type Unsubscribe = () => void;
@@ -1 +1 @@
1
- {"mappings":"AAgBA,YAAY","names":[],"sources":["../../../src/types/Unsubscribe.ts"],"version":3,"file":"Unsubscribe.d.ts"}
1
+ {"mappings":";;;AAmBA,YAAY","names":[],"sources":["../../../src/types/Unsubscribe.ts"],"version":3,"file":"Unsubscribe.d.ts"}
@@ -1,6 +1,11 @@
1
1
  import type { Flavored } from "@palantir/pack.core";
2
2
  export type UserId = Flavored<"pack:UserId">;
3
3
  export declare const UserRefBrand: unique symbol;
4
+ /**
5
+ * A reference providing an API to interact with a user.
6
+ *
7
+ * @experimental
8
+ */
4
9
  export interface UserRef {
5
10
  readonly userId: UserId;
6
11
  readonly [UserRefBrand]: typeof UserRefBrand;
@@ -1 +1 @@
1
- {"mappings":"AAgBA,cAAc,gBAAgB;AAE9B,YAAY,SAAS,SAAS;AAE9B,OAAO,cAAMA;AAEb,iBAAiB,QAAQ;UACd,QAAQ;WACP,sBAAsB;UACvB,MAAM,oBAAoB","names":["UserRefBrand: unique symbol"],"sources":["../../../src/types/UserRef.ts"],"version":3,"file":"UserRef.d.ts"}
1
+ {"mappings":"AAgBA,cAAc,gBAAgB;AAE9B,YAAY,SAAS,SAAS;AAE9B,OAAO,cAAMA;;;;;;AAOb,iBAAiB,QAAQ;UACd,QAAQ;WACP,sBAAsB;UACvB,MAAM,oBAAoB","names":["UserRefBrand: unique symbol"],"sources":["../../../src/types/UserRef.ts"],"version":3,"file":"UserRef.d.ts"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@palantir/pack.document-schema.model-types",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Minimal types supporting generated PACK schemas",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -28,7 +28,7 @@
28
28
  }
29
29
  },
30
30
  "dependencies": {
31
- "@palantir/pack.core": "~0.1.0"
31
+ "@palantir/pack.core": "~0.2.0"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "zod": "^4.1.7"
@@ -37,7 +37,7 @@
37
37
  "rimraf": "^6.0.1",
38
38
  "tslib": "^2.8.1",
39
39
  "typescript": "^5.9.2",
40
- "@palantir/pack.monorepo.tsconfig": "~0.4.0"
40
+ "@palantir/pack.monorepo.tsconfig": "~0.4.3"
41
41
  },
42
42
  "publishConfig": {
43
43
  "access": "public"
@@ -0,0 +1,43 @@
1
+ /*
2
+ * Copyright 2025 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { describe, expect, it } from "vitest";
18
+ import { getMetadata, Metadata, type WithMetadata } from "../types/Metadata.js";
19
+
20
+ describe("Metadata Lookup Tests", () => {
21
+ it("should retrieve metadata using direct symbol access", () => {
22
+ const obj: WithMetadata<{ name: string }> = {
23
+ [Metadata]: { name: "TestModel" },
24
+ };
25
+
26
+ const result = getMetadata(obj);
27
+
28
+ expect(result).toEqual({ name: "TestModel" });
29
+ });
30
+
31
+ it("should fallback to string-based symbol matching for cross-package scenarios", () => {
32
+ // Simulate a different copy of the Metadata symbol (as if from a different package instance)
33
+ const differentMetadataSymbol = Symbol("@palantir/pack.document-schema/metadata");
34
+
35
+ const obj = {
36
+ [differentMetadataSymbol]: { version: 2 },
37
+ } as WithMetadata<{ version: number }>;
38
+
39
+ const result = getMetadata(obj);
40
+
41
+ expect(result).toEqual({ version: 2 });
42
+ });
43
+ });
@@ -25,6 +25,27 @@ export const ActivityEventDataType = {
25
25
  UNKNOWN: "unknown",
26
26
  } as const;
27
27
 
28
+ /**
29
+ * Application specific custom activity event data, as described in a transaction edit,
30
+ * using an application sdk's generated model types.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * const unsubscribe = docRef.onActivity((docRef, event) => {
35
+ * console.log("Activity event:", event);
36
+ * });
37
+ * // Submit an edit with a description to generate an activity event.
38
+ * docRef.withTransaction(() => {
39
+ * // make some edits to the document here
40
+ * }, {
41
+ * model: MyEventModel,
42
+ * data: {
43
+ * myDataField: "some value",
44
+ * foo: 42,
45
+ * },
46
+ * });
47
+ * ```
48
+ */
28
49
  export interface ActivityEventDataCustom<M extends Model = Model> {
29
50
  readonly type: typeof ActivityEventDataType.CUSTOM_EVENT;
30
51
  readonly model: M;
@@ -33,6 +54,14 @@ export interface ActivityEventDataCustom<M extends Model = Model> {
33
54
 
34
55
  // TODO: add standard document activity events (need to be added to api types)
35
56
 
57
+ /**
58
+ * Fallback for unrecognized activity event types.
59
+ *
60
+ * This allows some flexibility with new event types added to the platform.
61
+ * Likely unknown events represent a new platform event type and will be handled
62
+ * in a future release of pack libraries and can be safely ignored by
63
+ * applications.
64
+ */
36
65
  export interface ActivityEventDataUnknown {
37
66
  readonly type: "unknown";
38
67
  readonly rawType: string;
@@ -41,7 +70,15 @@ export interface ActivityEventDataUnknown {
41
70
 
42
71
  export type ActivityEventData = ActivityEventDataCustom | ActivityEventDataUnknown;
43
72
 
73
+ /**
74
+ * An event representing an activity that has occurred on a document. This
75
+ * includes standard document events as well as custom application-specific
76
+ * events describing document edits.
77
+ *
78
+ * ActivityEvents are useful for building activity feeds, or notifications.
79
+ */
44
80
  export interface ActivityEvent {
81
+ /** Multiple events with the same aggregationKey may be collapsed. */
45
82
  readonly aggregationKey: string;
46
83
  readonly createdBy: UserId;
47
84
  readonly createdInstant: number;