@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.
- package/.turbo/turbo-lint.log +1 -1
- package/.turbo/turbo-transpileBrowser.log +1 -1
- package/.turbo/turbo-transpileCjs.log +1 -1
- package/.turbo/turbo-transpileEsm.log +1 -1
- package/.turbo/turbo-transpileTypes.log +1 -1
- package/.turbo/turbo-typecheck.log +1 -1
- package/CHANGELOG.md +21 -0
- package/build/browser/index.js +14 -4
- package/build/browser/index.js.map +1 -1
- package/build/cjs/index.cjs +14 -4
- package/build/cjs/index.cjs.map +1 -1
- package/build/cjs/index.d.cts +413 -0
- package/build/esm/index.js +14 -4
- package/build/esm/index.js.map +1 -1
- package/build/types/__tests__/Metadata.test.d.ts +1 -0
- package/build/types/__tests__/Metadata.test.d.ts.map +1 -0
- package/build/types/types/ActivityEvent.d.ts +37 -0
- package/build/types/types/ActivityEvent.d.ts.map +1 -1
- package/build/types/types/DocumentRef.d.ts +185 -0
- package/build/types/types/DocumentRef.d.ts.map +1 -1
- package/build/types/types/DocumentSchema.d.ts +6 -0
- package/build/types/types/DocumentSchema.d.ts.map +1 -1
- package/build/types/types/Model.d.ts +9 -0
- package/build/types/types/Model.d.ts.map +1 -1
- package/build/types/types/PresenceEvent.d.ts +43 -0
- package/build/types/types/PresenceEvent.d.ts.map +1 -1
- package/build/types/types/RecordCollectionRef.d.ts +54 -0
- package/build/types/types/RecordCollectionRef.d.ts.map +1 -1
- package/build/types/types/RecordRef.d.ts +71 -0
- package/build/types/types/RecordRef.d.ts.map +1 -1
- package/build/types/types/Unsubscribe.d.ts +3 -0
- package/build/types/types/Unsubscribe.d.ts.map +1 -1
- package/build/types/types/UserRef.d.ts +5 -0
- package/build/types/types/UserRef.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/Metadata.test.ts +43 -0
- package/src/types/ActivityEvent.ts +37 -0
- package/src/types/DocumentRef.ts +192 -0
- package/src/types/DocumentSchema.ts +6 -0
- package/src/types/Metadata.ts +20 -5
- package/src/types/Model.ts +9 -0
- package/src/types/PresenceEvent.ts +43 -0
- package/src/types/RecordCollectionRef.ts +54 -0
- package/src/types/RecordRef.ts +74 -0
- package/src/types/Unsubscribe.ts +3 -0
- package/src/types/UserRef.ts +5 -0
package/src/types/DocumentRef.ts
CHANGED
|
@@ -25,35 +25,227 @@ import type { Unsubscribe } from "./Unsubscribe.js";
|
|
|
25
25
|
|
|
26
26
|
export type DocumentId = Flavored<"DocumentId">;
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Options for subscribing to presence events on a document.
|
|
30
|
+
*/
|
|
28
31
|
export interface PresenceSubscriptionOptions {
|
|
32
|
+
/**
|
|
33
|
+
* If true, presence events originating from the local user will be ignored.
|
|
34
|
+
*
|
|
35
|
+
* @default true
|
|
36
|
+
*/
|
|
29
37
|
readonly ignoreSelfUpdates?: boolean;
|
|
30
38
|
}
|
|
31
39
|
|
|
32
40
|
export const DocumentRefBrand: unique symbol = Symbol("pack:DocumentRef");
|
|
33
41
|
|
|
42
|
+
/**
|
|
43
|
+
* A reference to a document in the Pack system.
|
|
44
|
+
*
|
|
45
|
+
* A documentRef returned by various interfaces from the pack app instance or
|
|
46
|
+
* utilities such as react hooks from @palantir/pack.state.react provides
|
|
47
|
+
* methods to interact with the document, such as subscribing to & making
|
|
48
|
+
* changes to the document state and also related activity or presence events.
|
|
49
|
+
*
|
|
50
|
+
* A stable documentRef object is guaranteed for the same document id within the
|
|
51
|
+
* same app instance.
|
|
52
|
+
*/
|
|
34
53
|
export interface DocumentRef<D extends DocumentSchema = DocumentSchema> {
|
|
35
54
|
readonly id: DocumentId;
|
|
36
55
|
readonly schema: D;
|
|
37
56
|
readonly [DocumentRefBrand]: typeof DocumentRefBrand;
|
|
38
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Get a snapshot of the current document state.
|
|
60
|
+
*
|
|
61
|
+
* This is largely for debugging and dumps a json view of the whole document state.
|
|
62
|
+
*
|
|
63
|
+
* @experimental
|
|
64
|
+
*/
|
|
39
65
|
getDocSnapshot(): Promise<DocumentState<D>>;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Get or create a ref to the collection of all records for the specified
|
|
69
|
+
* model in this document.
|
|
70
|
+
*
|
|
71
|
+
* @param model The model type from the application's generated schema.
|
|
72
|
+
* @returns A stable {@link RecordCollectionRef} object providing an interface
|
|
73
|
+
* to interact with the records of the specified model type in this document.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* import { DocumentModel, MyModel } from "@myapp/schema";
|
|
78
|
+
* import { app } from "./appInstance";
|
|
79
|
+
*
|
|
80
|
+
* const docRef = app.state.createDocRef(DocumentModel, someDocumentId);
|
|
81
|
+
* const myRecordCollection = docRef.getRecords(MyModel);
|
|
82
|
+
*
|
|
83
|
+
* myRecordCollection.onItemsAdded((items) => {
|
|
84
|
+
* console.log("New records added:", items);
|
|
85
|
+
* })
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
40
88
|
getRecords<R extends Model>(model: R): RecordCollectionRef<R>;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Subscribe to activity events for the document.
|
|
92
|
+
*
|
|
93
|
+
* Activity is used by applications to describe changes to the document, for
|
|
94
|
+
* populating activity feeds, change histories, notifications and similar
|
|
95
|
+
* features.
|
|
96
|
+
*
|
|
97
|
+
* @see {withTransaction} for making edits that generate activity events.
|
|
98
|
+
*
|
|
99
|
+
* @returns An unsubscribe function.
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* const unsubscribe = docRef.onActivity((docRef, event) => {
|
|
103
|
+
* console.log("Activity event:", event);
|
|
104
|
+
* });
|
|
105
|
+
*
|
|
106
|
+
* // Submit an edit with a description to generate an activity event.
|
|
107
|
+
* docRef.withTransaction(() => {
|
|
108
|
+
* // make some edits to the document here
|
|
109
|
+
* }, {
|
|
110
|
+
* model: MyEventModel,
|
|
111
|
+
* data: {
|
|
112
|
+
* myDataField: "some value",
|
|
113
|
+
* foo: 42,
|
|
114
|
+
* },
|
|
115
|
+
* });
|
|
116
|
+
*
|
|
117
|
+
* // Later, to unsubscribe:
|
|
118
|
+
* unsubscribe();
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
41
121
|
onActivity(
|
|
42
122
|
callback: (docRef: DocumentRef<D>, event: ActivityEvent) => void,
|
|
43
123
|
): Unsubscribe;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Subscribe to metadata changes for the document.
|
|
127
|
+
*
|
|
128
|
+
* @returns An unsubscribe function.
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* const unsubscribe = docRef.onMetadataChange((docRef, metadata) => {
|
|
132
|
+
* console.log("Metadata changed:", metadata);
|
|
133
|
+
* });
|
|
134
|
+
*
|
|
135
|
+
* // Later, to unsubscribe:
|
|
136
|
+
* unsubscribe();
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
44
139
|
onMetadataChange(
|
|
45
140
|
callback: (docRef: DocumentRef<D>, metadata: DocumentMetadata) => void,
|
|
46
141
|
): Unsubscribe;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Subscribe to presence events for the document.
|
|
145
|
+
*
|
|
146
|
+
* Presence events are a way to broadcast and receive non-persisted awareness
|
|
147
|
+
* and presence events. For example, a user moving their cursor in a
|
|
148
|
+
* collaborative editor could be broadcast via custom presence events.
|
|
149
|
+
*
|
|
150
|
+
* @see {@link updateCustomPresence} to publish custom presence updates.
|
|
151
|
+
*
|
|
152
|
+
* @param callback The callback to invoke on presence events.
|
|
153
|
+
* @param options Options for the presence subscription.
|
|
154
|
+
* @returns An unsubscribe function.
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* const unsubscribe = docRef.onPresence((docRef, event) => {
|
|
158
|
+
* console.log("Presence event:", event);
|
|
159
|
+
* if (event.model === MyPresenceModel) {
|
|
160
|
+
* updateCursor((event.eventData as ModelData<MyPresenceModel>).cursorPosition);
|
|
161
|
+
* }
|
|
162
|
+
* }, { ignoreSelfUpdates: true });
|
|
163
|
+
*
|
|
164
|
+
* // Broadcast a presence update
|
|
165
|
+
* docRef.updateCustomPresence(MyPresenceModel, {
|
|
166
|
+
* cursorPosition: [42, 7],
|
|
167
|
+
* });
|
|
168
|
+
*
|
|
169
|
+
* // Later, to unsubscribe:
|
|
170
|
+
* unsubscribe();
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
47
173
|
onPresence(
|
|
48
174
|
callback: (docRef: DocumentRef<D>, event: PresenceEvent) => void,
|
|
49
175
|
options?: PresenceSubscriptionOptions,
|
|
50
176
|
): Unsubscribe;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Subscribe to be notified any time the document state changes.
|
|
180
|
+
* This is largely for testing purposes.
|
|
181
|
+
*
|
|
182
|
+
* @experimental
|
|
183
|
+
*
|
|
184
|
+
* @param callback The callback to invoke on state changes.
|
|
185
|
+
* @returns An unsubscribe function.
|
|
186
|
+
* @example
|
|
187
|
+
* ```ts
|
|
188
|
+
* const unsubscribe = docRef.onStateChange((docRef) => {
|
|
189
|
+
* console.log("Document state changed:", docRef);
|
|
190
|
+
* });
|
|
191
|
+
*
|
|
192
|
+
* // Later, to unsubscribe:
|
|
193
|
+
* unsubscribe();
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
51
196
|
onStateChange(
|
|
52
197
|
callback: (docRef: DocumentRef<D>) => void,
|
|
53
198
|
): Unsubscribe;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Broadcasts an update for the specified model as presence data to other
|
|
202
|
+
* subscribers.
|
|
203
|
+
*
|
|
204
|
+
* Presence data is ephemeral and not stored as part of the document state. It
|
|
205
|
+
* is intended for broadcasting transient user presence and awareness
|
|
206
|
+
* information. Each different model type used for presence is expected to
|
|
207
|
+
* update the latest 'presence state' for that model type.
|
|
208
|
+
*
|
|
209
|
+
* @see {@link onPresence} to subscribe to presence updates.
|
|
210
|
+
*
|
|
211
|
+
* @param model The model type to update presence for.
|
|
212
|
+
* @param eventData The new presence data for the model.
|
|
213
|
+
*/
|
|
54
214
|
updateCustomPresence<M extends Model = Model>(
|
|
55
215
|
model: M,
|
|
56
216
|
eventData: ModelData<M>,
|
|
57
217
|
): void;
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Execute one or more document edits within a transaction, optionally providing
|
|
221
|
+
* a description of the edit for activity tracking.
|
|
222
|
+
*
|
|
223
|
+
* All edits made within the provided function will be treated as a single
|
|
224
|
+
* atomic edit operation. If a description is provided, an activity event will
|
|
225
|
+
* be generated for the edit.
|
|
226
|
+
*
|
|
227
|
+
* If this is called within an existing transaction, the inner edits will be
|
|
228
|
+
* included in the outer transaction only, and the inner descriptions will be
|
|
229
|
+
* discarded.
|
|
230
|
+
*
|
|
231
|
+
* @see {@link onActivity} to subscribe to activity events on this document.
|
|
232
|
+
*
|
|
233
|
+
* @param fn A lambda including some document edits.
|
|
234
|
+
* @param description Optional description of the edit for activity tracking.
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```ts
|
|
238
|
+
* docRef.withTransaction(() => {
|
|
239
|
+
* const myRecords = docRef.getRecords(MyModel);
|
|
240
|
+
* myRecords.set("record-1", { field: "new value" });
|
|
241
|
+
* myRecords.delete("record-2");
|
|
242
|
+
* }, {
|
|
243
|
+
* model: MyEditEventModel,
|
|
244
|
+
* data: {
|
|
245
|
+
* summary: "Updated record-1 and deleted record-2",
|
|
246
|
+
* },
|
|
247
|
+
* });
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
58
250
|
withTransaction(fn: () => void, description?: EditDescription): void;
|
|
59
251
|
}
|
|
@@ -17,10 +17,16 @@
|
|
|
17
17
|
import type { WithMetadata } from "./Metadata.js";
|
|
18
18
|
import type { Model, ModelData } from "./Model.js";
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* The base type for an application sdk's generated document schema.
|
|
22
|
+
*/
|
|
20
23
|
export interface DocumentSchema extends WithMetadata<DocumentSchemaMetadata> {
|
|
21
24
|
readonly [modelName: string]: Model;
|
|
22
25
|
}
|
|
23
26
|
|
|
27
|
+
/**
|
|
28
|
+
* The plain object representation of the state of a document.
|
|
29
|
+
*/
|
|
24
30
|
export type DocumentState<S extends DocumentSchema> = {
|
|
25
31
|
readonly [K in Exclude<keyof S, symbol>]: { readonly [key: string]: ModelData<S[K]> };
|
|
26
32
|
};
|
package/src/types/Metadata.ts
CHANGED
|
@@ -21,10 +21,25 @@ export interface WithMetadata<T> {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
export function getMetadata<T>(obj: WithMetadata<T>): T {
|
|
24
|
-
//
|
|
25
|
-
const
|
|
26
|
-
if (
|
|
27
|
-
|
|
24
|
+
// First try the direct symbol access
|
|
25
|
+
const directMetadata = obj[Metadata];
|
|
26
|
+
if (directMetadata != null) {
|
|
27
|
+
return directMetadata;
|
|
28
28
|
}
|
|
29
|
-
|
|
29
|
+
|
|
30
|
+
// Fallback: search for a symbol with matching string representation
|
|
31
|
+
// If the different copies of this package are used, the symbol references will not match directly
|
|
32
|
+
const metadataString = Metadata.toString();
|
|
33
|
+
const symbolKeys = Object.getOwnPropertySymbols(obj);
|
|
34
|
+
|
|
35
|
+
for (const symbolKey of symbolKeys) {
|
|
36
|
+
if (symbolKey.toString() === metadataString) {
|
|
37
|
+
const fallbackMetadata = (obj as any)[symbolKey];
|
|
38
|
+
if (fallbackMetadata != null) {
|
|
39
|
+
return fallbackMetadata;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
throw new Error("Object does not have metadata");
|
|
30
45
|
}
|
package/src/types/Model.ts
CHANGED
|
@@ -32,6 +32,9 @@ export interface Model<T = unknown, Z extends ZodType<T> = ZodType<T>>
|
|
|
32
32
|
|
|
33
33
|
export type ModelData<M extends Model> = M["__type"];
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Describes an edit made to a document.
|
|
37
|
+
*/
|
|
35
38
|
export interface EditDescription<M extends Model = Model> {
|
|
36
39
|
readonly data: ModelData<M>;
|
|
37
40
|
readonly model: M;
|
|
@@ -47,6 +50,12 @@ export const ExternalRefType = {
|
|
|
47
50
|
export type ExternalRefType = typeof ExternalRefType[keyof typeof ExternalRefType];
|
|
48
51
|
|
|
49
52
|
export interface ModelMetadata<T = unknown> {
|
|
53
|
+
/**
|
|
54
|
+
* Which fields in the model are external references (e.g. UserRef, DocumentRef, etc).
|
|
55
|
+
*/
|
|
50
56
|
readonly externalRefFieldTypes?: Readonly<Record<keyof T, ExternalRefType>>;
|
|
57
|
+
/**
|
|
58
|
+
* The name of the model (should match the typescript symbol).
|
|
59
|
+
*/
|
|
51
60
|
readonly name: string;
|
|
52
61
|
}
|
|
@@ -27,20 +27,53 @@ export const PresenceEventDataType = {
|
|
|
27
27
|
export type PresenceEventDataType =
|
|
28
28
|
typeof PresenceEventDataType[keyof typeof PresenceEventDataType];
|
|
29
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
|
+
*/
|
|
30
36
|
export interface PresenceEventDataArrived {
|
|
31
37
|
readonly type: typeof PresenceEventDataType.ARRIVED;
|
|
32
38
|
}
|
|
33
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
|
+
*/
|
|
34
46
|
export interface PresenceEventDataDeparted {
|
|
35
47
|
readonly type: typeof PresenceEventDataType.DEPARTED;
|
|
36
48
|
}
|
|
37
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
|
+
*/
|
|
38
63
|
export interface PresenceEventDataCustom<M extends Model = Model> {
|
|
39
64
|
readonly type: typeof PresenceEventDataType.CUSTOM_EVENT;
|
|
40
65
|
readonly eventData: ModelData<M>;
|
|
41
66
|
readonly model: M;
|
|
42
67
|
}
|
|
43
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
|
+
*/
|
|
44
77
|
export interface PresenceEventUnknown {
|
|
45
78
|
readonly type: "unknown";
|
|
46
79
|
readonly rawType: string;
|
|
@@ -53,6 +86,16 @@ export type PresenceEventData =
|
|
|
53
86
|
| PresenceEventDataCustom
|
|
54
87
|
| PresenceEventUnknown;
|
|
55
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
|
+
*/
|
|
56
99
|
export interface PresenceEvent {
|
|
57
100
|
readonly userId: UserId;
|
|
58
101
|
readonly eventData: PresenceEventData;
|
|
@@ -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;
|
package/src/types/RecordRef.ts
CHANGED
|
@@ -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
|
}
|
package/src/types/Unsubscribe.ts
CHANGED
package/src/types/UserRef.ts
CHANGED
|
@@ -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;
|