@palantir/pack.document-schema.model-types 0.1.0-beta.3 → 0.1.1

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 (49) hide show
  1. package/.turbo/turbo-lint.log +2 -2
  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 +27 -0
  8. package/build/browser/index.js +29 -5
  9. package/build/browser/index.js.map +1 -1
  10. package/build/cjs/index.cjs +30 -4
  11. package/build/cjs/index.cjs.map +1 -1
  12. package/build/cjs/index.d.cts +517 -39
  13. package/build/esm/index.js +29 -5
  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/index.d.ts +6 -2
  18. package/build/types/index.d.ts.map +1 -1
  19. package/build/types/types/ActivityEvent.d.ts +64 -0
  20. package/build/types/types/ActivityEvent.d.ts.map +1 -0
  21. package/build/types/types/DocumentRef.d.ts +199 -5
  22. package/build/types/types/DocumentRef.d.ts.map +1 -1
  23. package/build/types/types/DocumentSchema.d.ts +6 -0
  24. package/build/types/types/DocumentSchema.d.ts.map +1 -1
  25. package/build/types/types/Model.d.ts +13 -0
  26. package/build/types/types/Model.d.ts.map +1 -1
  27. package/build/types/types/PresenceEvent.d.ts +73 -0
  28. package/build/types/types/PresenceEvent.d.ts.map +1 -0
  29. package/build/types/types/RecordCollectionRef.d.ts +54 -0
  30. package/build/types/types/RecordCollectionRef.d.ts.map +1 -1
  31. package/build/types/types/RecordRef.d.ts +71 -0
  32. package/build/types/types/RecordRef.d.ts.map +1 -1
  33. package/build/types/types/Unsubscribe.d.ts +3 -0
  34. package/build/types/types/Unsubscribe.d.ts.map +1 -1
  35. package/build/types/types/UserRef.d.ts +5 -0
  36. package/build/types/types/UserRef.d.ts.map +1 -1
  37. package/package.json +5 -5
  38. package/src/__tests__/Metadata.test.ts +43 -0
  39. package/src/index.ts +19 -2
  40. package/src/types/ActivityEvent.ts +88 -0
  41. package/src/types/DocumentRef.ts +218 -8
  42. package/src/types/DocumentSchema.ts +6 -0
  43. package/src/types/Metadata.ts +20 -5
  44. package/src/types/Model.ts +14 -0
  45. package/src/types/PresenceEvent.ts +102 -0
  46. package/src/types/RecordCollectionRef.ts +54 -0
  47. package/src/types/RecordRef.ts +74 -0
  48. package/src/types/Unsubscribe.ts +3 -0
  49. package/src/types/UserRef.ts +5 -0
@@ -0,0 +1,102 @@
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 type { Model, ModelData } from "./Model.js";
18
+ import type { UserId } from "./UserRef.js";
19
+
20
+ export const PresenceEventDataType = {
21
+ ARRIVED: "presenceArrived",
22
+ DEPARTED: "presenceDeparted",
23
+ CUSTOM_EVENT: "customEvent",
24
+ UNKNOWN: "unknown",
25
+ } as const;
26
+
27
+ export type PresenceEventDataType =
28
+ typeof PresenceEventDataType[keyof typeof PresenceEventDataType];
29
+
30
+ /**
31
+ * Any client that subscribes to presence events via `DocumentRef.onPresence` will
32
+ * be considered 'present' on the document, and trigger an 'arrived' presence event.
33
+ * When they disconnect or unsubscribe from presence events, a 'departed' presence event
34
+ * will be triggered.
35
+ */
36
+ export interface PresenceEventDataArrived {
37
+ readonly type: typeof PresenceEventDataType.ARRIVED;
38
+ }
39
+
40
+ /**
41
+ * Any client that subscribes to presence events via `DocumentRef.onPresence` will
42
+ * be considered 'present' on the document, and trigger an 'arrived' presence event.
43
+ * When they disconnect or unsubscribe from presence events, a 'departed' presence event
44
+ * will be triggered.
45
+ */
46
+ export interface PresenceEventDataDeparted {
47
+ readonly type: typeof PresenceEventDataType.DEPARTED;
48
+ }
49
+
50
+ /**
51
+ * Application specific custom presence event data.
52
+ *
53
+ * Each different model type used for presence is expected to update the latest
54
+ * 'presence state' for that model type.
55
+ *
56
+ * For example, your app may have need to broadcast user cursor positions and
57
+ * selection ranges as presence data. You could define your schema to include a
58
+ * `CursorPosition` and `SelectionRange` record types, and set them
59
+ * independently via `{@link DocumentRef.updateCustomPresence}`. Each model type
60
+ * sent as a custom presence event should be considered a separate 'channel' of
61
+ * presence data on this document.
62
+ */
63
+ export interface PresenceEventDataCustom<M extends Model = Model> {
64
+ readonly type: typeof PresenceEventDataType.CUSTOM_EVENT;
65
+ readonly eventData: ModelData<M>;
66
+ readonly model: M;
67
+ }
68
+
69
+ /**
70
+ * Fallback for unrecognized activity event types.
71
+ *
72
+ * This allows some flexibility with new event types added to the platform.
73
+ * Likely unknown events represent a new platform event type and will be handled
74
+ * in a future release of pack libraries and can be safely ignored by
75
+ * applications.
76
+ */
77
+ export interface PresenceEventUnknown {
78
+ readonly type: "unknown";
79
+ readonly rawType: string;
80
+ readonly rawData: unknown;
81
+ }
82
+
83
+ export type PresenceEventData =
84
+ | PresenceEventDataArrived
85
+ | PresenceEventDataDeparted
86
+ | PresenceEventDataCustom
87
+ | PresenceEventUnknown;
88
+
89
+ /**
90
+ * An event representing a transient awareness or presence change for a user on this document.
91
+ * The presence channel is intended for ephemeral data such as user cursors, selections, or
92
+ * other live collaboration indicators.
93
+ *
94
+ * PresenceEvents are not persisted in document history.
95
+ *
96
+ * When a client goes offline, its presence is considered departed and any presence events
97
+ * associated with that user should be considered stale and / or cleared.
98
+ */
99
+ export interface PresenceEvent {
100
+ readonly userId: UserId;
101
+ readonly eventData: PresenceEventData;
102
+ }
@@ -22,27 +22,81 @@ export const RecordCollectionRefBrand: unique symbol = Symbol(
22
22
  "pack:RecordCollectionRef",
23
23
  );
24
24
 
25
+ /**
26
+ * A reference providing an API to interact with a collection of records in a document.
27
+ */
25
28
  export interface RecordCollectionRef<M extends Model = Model> {
26
29
  readonly docRef: DocumentRef;
27
30
  readonly model: M;
28
31
  readonly [RecordCollectionRefBrand]: typeof RecordCollectionRefBrand;
29
32
 
33
+ /**
34
+ * Delete a record from the collection (and the document).
35
+ *
36
+ * @param id - The ID of the record to delete.
37
+ * @returns A promise that resolves when the record is deleted.
38
+ */
30
39
  delete(id: RecordId): Promise<void>;
40
+ /**
41
+ * Get a {@link RecordRef} from the collection. This provides the main API for
42
+ * accessing and updating individual records in a document.
43
+ *
44
+ * @param id - The ID of the record to get.
45
+ * @returns The record reference, or undefined if it doesn't exist. The
46
+ * recordRef is a stable object and can be used for reference equality checks.
47
+ */
31
48
  get(id: RecordId): RecordRef<M> | undefined;
49
+ /**
50
+ * Check if a record exists in the collection.
51
+ *
52
+ * @param id - The ID of the record to check.
53
+ * @returns True if the record exists, false otherwise.
54
+ */
32
55
  has(id: RecordId): boolean;
56
+ /**
57
+ * Set the data for a record in the collection (creating it if it doesn't exist).
58
+ *
59
+ * @param id - The ID of the record to set.
60
+ * @param state - The data to set for the record.
61
+ * @returns A promise that resolves when the record is set.
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * const recordCollection = docRef.getRecords(MyModel);
66
+ * await recordCollection.set("record-id", { field: "value" });
67
+ * ```
68
+ */
33
69
  set(id: RecordId, state: ModelData<M>): Promise<void>;
34
70
  readonly size: number;
35
71
 
36
72
  [Symbol.iterator](): Iterator<RecordRef<M>>;
37
73
 
74
+ /**
75
+ * Subscribe to added items in the collection.
76
+ *
77
+ * @param callback - The callback to invoke when items are added.
78
+ * @returns An unsubscribe function.
79
+ */
38
80
  readonly onItemsAdded: (
39
81
  callback: (items: readonly RecordRef<M>[]) => void,
40
82
  ) => () => void;
41
83
 
84
+ /**
85
+ * Subscribe to changed items in the collection.
86
+ *
87
+ * @param callback - The callback to invoke when items are changed.
88
+ * @returns An unsubscribe function.
89
+ */
42
90
  readonly onItemsChanged: (
43
91
  callback: (items: readonly RecordRef<M>[]) => void,
44
92
  ) => () => void;
45
93
 
94
+ /**
95
+ * Subscribe to deleted items in the collection.
96
+ *
97
+ * @param callback - The callback to invoke when items are deleted.
98
+ * @returns An unsubscribe function.
99
+ */
46
100
  readonly onItemsDeleted: (
47
101
  callback: (items: readonly RecordRef<M>[]) => void,
48
102
  ) => () => void;
@@ -23,14 +23,88 @@ export type RecordId = Flavored<"RecordId">;
23
23
 
24
24
  export const RecordRefBrand: unique symbol = Symbol("pack:RecordRef");
25
25
 
26
+ /**
27
+ * A reference providing an API to interact with a specific record in a
28
+ * document. This is the main interface for accessing and updating individual
29
+ * records.
30
+ *
31
+ * These will be created by docRef or recordCollectionRef APIs for example and
32
+ * should not be created manually. RecordRefs are stable objects that can be
33
+ * used for reference equality checks.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * import { DocumentRef, DocumentSchema, MyModel } from "@myapp/schema";
38
+ * import { app } from "./appInstance";
39
+ *
40
+ * const docRef = app.getDocRef<DocumentSchema>(someDocumentId);
41
+ *
42
+ * const recordRef = docRef.getRecords(MyModel).set("my-record-id", { myFieldName: "some value", foo: 42 });
43
+ *
44
+ * // Or get a specific record.
45
+ * const recordRef = docRef.getRecords(MyModel).get("my-record-id");
46
+ */
26
47
  export interface RecordRef<M extends Model = Model> {
27
48
  readonly docRef: DocumentRef;
28
49
  readonly id: RecordId;
29
50
  readonly model: M;
30
51
  readonly [RecordRefBrand]: typeof RecordRefBrand;
31
52
 
53
+ /**
54
+ * Get the current state of the record in a plain object.
55
+ * If there is an active subscription to the document this is the current state of the record in memory.
56
+ * Otherwise, this will fetch the latest state from the server.
57
+ */
32
58
  getSnapshot(): Promise<ModelData<M>>;
59
+
60
+ /**
61
+ * Subscribe to changes in the record.
62
+ * @param callback The callback to invoke when the record changes.
63
+ * @returns An unsubscribe function.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * // Subscribe to changes
68
+ * recordRef.onChange((newSnapshot, recordRef) => {
69
+ * console.log("Record changed:", newSnapshot);
70
+ * });
71
+ *
72
+ * // Submit a change
73
+ * recordRef.set({ myFieldName: "new value" });
74
+ * ```
75
+ */
33
76
  onChange(callback: (snapshot: ModelData<M>, recordRef: RecordRef<M>) => void): Unsubscribe;
77
+
78
+ /**
79
+ * Subscribe to deletion of the record.
80
+ * @param callback The callback to invoke when the record is deleted.
81
+ * @returns An unsubscribe function.
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * // Subscribe to deletion
86
+ * recordRef.onDeleted((recordRef) => {
87
+ * console.log("Record deleted:", recordRef.id);
88
+ * });
89
+ *
90
+ * // Trigger the deletion
91
+ * docRef.getRecords(MyModel).delete(recordRef.id);
92
+ * ```
93
+ */
34
94
  onDeleted(callback: (recordRef: RecordRef<M>) => void): Unsubscribe;
95
+
96
+ /**
97
+ * Set the data for the record (creating it if it doesn't exist).
98
+ *
99
+ * @see {onChange} to subscribe to changes to the record.
100
+ *
101
+ * @param record - The plain object data to set for the record, corresponding to the model's schema.
102
+ * @returns An ignorable promise that resolves when the record is published.
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * await recordRef.set({ field: "value" });
107
+ * ```
108
+ */
35
109
  set(record: ModelData<M>): Promise<void>;
36
110
  }
@@ -14,4 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
+ /**
18
+ * A function that can be called to unsubscribe from a previously subscribed event interface.
19
+ */
17
20
  export type Unsubscribe = () => void;
@@ -20,6 +20,11 @@ export type UserId = Flavored<"pack:UserId">;
20
20
 
21
21
  export const UserRefBrand: unique symbol = Symbol("pack:UserRef");
22
22
 
23
+ /**
24
+ * A reference providing an API to interact with a user.
25
+ *
26
+ * @experimental
27
+ */
23
28
  export interface UserRef {
24
29
  readonly userId: UserId;
25
30
  readonly [UserRefBrand]: typeof UserRefBrand;