@alwatr/nitrobase-reference 8.0.0 → 9.1.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.
@@ -0,0 +1,429 @@
1
+ import {delay} from '@alwatr/delay';
2
+ import {createLogger} from '@alwatr/logger';
3
+ import {getStoreId, getStorePath} from '@alwatr/nitrobase-helper';
4
+ import {StoreFileType, StoreFileExtension, type StoreFileId, type DocumentContext, type StoreFileMeta} from '@alwatr/nitrobase-types';
5
+
6
+ import {logger} from './logger.js';
7
+
8
+ __dev_mode__: logger.logFileModule?.('document-reference');
9
+
10
+ /**
11
+ * Represents a reference to a document of the AlwatrNitrobase.
12
+ * Provides methods to interact with the document, such as get, set, update and save.
13
+ */
14
+ export class DocumentReference<TDoc extends JsonObject = JsonObject> {
15
+ /**
16
+ * Alwatr nitrobase engine version string.
17
+ */
18
+ public static readonly version = __package_version__;
19
+
20
+ /**
21
+ * Alwatr nitrobase engine file format version number.
22
+ */
23
+ public static readonly fileFormatVersion = 3;
24
+
25
+ /**
26
+ * Creates new DocumentReference instance from stat and initial data.
27
+ *
28
+ * @param statId the document stat.
29
+ * @param data the document data.
30
+ * @param updatedCallback the callback to invoke when the document changed.
31
+ * @template TDoc The document data type.
32
+ * @returns A new document reference class.
33
+ */
34
+ public static newRefFromData<TDoc extends JsonObject>(
35
+ statId: StoreFileId,
36
+ data: TDoc,
37
+ updatedCallback: (from: DocumentReference<TDoc>) => unknown,
38
+ debugDomain?: string,
39
+ ): DocumentReference<TDoc> {
40
+ logger.logMethodArgs?.('doc.newRefFromData', statId);
41
+
42
+ const now = Date.now();
43
+ const initialContext: DocumentContext<TDoc> = {
44
+ ok: true,
45
+ meta: {
46
+ ...statId,
47
+ rev: 1,
48
+ updated: now,
49
+ created: now,
50
+ type: StoreFileType.Document,
51
+ extension: StoreFileExtension.Json,
52
+ fv: DocumentReference.fileFormatVersion,
53
+ extra: {},
54
+ },
55
+ data,
56
+ };
57
+
58
+ return new DocumentReference(initialContext, updatedCallback, debugDomain);
59
+ }
60
+
61
+ /**
62
+ * Creates new DocumentReference instance from DocumentContext.
63
+ *
64
+ * @param context the document context.
65
+ * @param updatedCallback the callback to invoke when the document changed.
66
+ * @template TDoc The document data type.
67
+ * @returns A new document reference class.
68
+ */
69
+ public static newRefFromContext<TDoc extends JsonObject>(
70
+ context: DocumentContext<TDoc>,
71
+ updatedCallback: (from: DocumentReference<TDoc>) => unknown,
72
+ debugDomain?: string,
73
+ ): DocumentReference<TDoc> {
74
+ logger.logMethodArgs?.('doc.newRefFromContext', context.meta);
75
+ return new DocumentReference(context, updatedCallback, debugDomain);
76
+ }
77
+
78
+ /**
79
+ * Validates the document context and try to migrate it to the latest version.
80
+ */
81
+ private validateContext__(): void {
82
+ this.logger__.logMethod?.('validateContext__');
83
+
84
+ if (this.context__.ok !== true) {
85
+ this.logger__.accident?.('validateContext__', 'store_not_ok');
86
+ throw new Error('store_not_ok', {cause: {context: this.context__}});
87
+ }
88
+
89
+ if (this.context__.meta === undefined) {
90
+ this.logger__.accident?.('validateContext__', 'store_meta_undefined');
91
+ throw new Error('store_meta_undefined', {cause: {context: this.context__}});
92
+ }
93
+
94
+ if (this.context__.meta.type !== StoreFileType.Document) {
95
+ this.logger__.accident?.('validateContext__', 'document_type_invalid', this.context__.meta);
96
+ throw new Error('document_type_invalid', {cause: this.context__.meta});
97
+ }
98
+
99
+ if (this.context__.meta.fv !== DocumentReference.fileFormatVersion) {
100
+ this.logger__.incident?.('validateContext__', 'store_file_version_incompatible', {
101
+ old: this.context__.meta.fv,
102
+ new: DocumentReference.fileFormatVersion,
103
+ });
104
+ this.migrateContext__();
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Migrate the document context to the latest.
110
+ */
111
+ private migrateContext__(): void {
112
+ if (this.context__.meta.fv === DocumentReference.fileFormatVersion) return;
113
+
114
+ this.logger__.logMethod?.('migrateContext__');
115
+
116
+ if (this.context__.meta.fv > DocumentReference.fileFormatVersion) {
117
+ this.logger__.accident('migrateContext__', 'store_version_incompatible', this.context__.meta);
118
+ throw new Error('store_version_incompatible', {cause: this.context__.meta});
119
+ }
120
+
121
+ if (this.context__.meta.fv === 1) {
122
+ // migrate from v1 to v2
123
+ // this.context__.meta.schemaVer = 0
124
+ this.context__.meta.fv = 2;
125
+ }
126
+
127
+ if (this.context__.meta.fv === 2) {
128
+ // migrate from v1 to v3
129
+ if (this.context__.meta.schemaVer === undefined || this.context__.meta.schemaVer === 0) {
130
+ this.context__.meta.schemaVer = 1;
131
+ }
132
+ delete (this.context__.meta as DictionaryOpt)['ver'];
133
+ this.context__.meta.extra ??= {};
134
+ this.context__.meta.fv = 3;
135
+ }
136
+
137
+ this.updated__();
138
+ }
139
+
140
+ /**
141
+ * The ID of the document nitrobase file.
142
+ */
143
+ public readonly id: string;
144
+
145
+ /**
146
+ * The location path of the document nitrobase file.
147
+ */
148
+ public readonly path: string;
149
+
150
+ /**
151
+ * Indicates whether the document has unsaved changes.
152
+ */
153
+ public hasUnprocessedChanges_ = false;
154
+
155
+ /**
156
+ * Logger instance for this document.
157
+ */
158
+ private logger__;
159
+
160
+ /**
161
+ * Create a new document reference.
162
+ * Document reference have methods to get, set, update and save the AlwatrNitrobase Document.
163
+ *
164
+ * @param context__ Document's context filled from the Alwatr Nitrobase (parent).
165
+ * @param updatedCallback__ updated callback to invoke when the document is updated from the Alwatr Nitrobase (parent).
166
+ * @template TDoc The document data type.
167
+ */
168
+ constructor(
169
+ private readonly context__: DocumentContext<TDoc>,
170
+ private readonly updatedCallback__: (from: DocumentReference<TDoc>) => unknown,
171
+ debugDomain?: string,
172
+ ) {
173
+ this.id = getStoreId(this.context__.meta);
174
+ this.path = getStorePath(this.context__.meta);
175
+
176
+ debugDomain ??= this.id.slice(0, 20);
177
+ this.logger__ = createLogger(`doc:${debugDomain}`);
178
+
179
+ this.logger__.logMethodArgs?.('new', {path: this.path});
180
+
181
+ this.validateContext__();
182
+ }
183
+
184
+ /**
185
+ * Get nitrobase schema version
186
+ *
187
+ * @returns nitrobase schema version
188
+ */
189
+ public get schemaVer(): number {
190
+ return this.context__.meta.schemaVer ?? 1;
191
+ }
192
+
193
+ /**
194
+ * Set nitrobase schema version for migrate
195
+ */
196
+ public set schemaVer(ver: number) {
197
+ this.logger__.logMethodArgs?.('set schemaVer', {old: this.context__.meta.schemaVer, new: ver});
198
+ this.context__.meta.schemaVer = ver;
199
+ this.updated__();
200
+ }
201
+
202
+ /**
203
+ * Indicates whether the document data is frozen and cannot be saved.
204
+ */
205
+ private _freeze = false;
206
+
207
+ /**
208
+ * Gets the freeze status of the document data.
209
+ *
210
+ * @returns `true` if the document data is frozen, `false` otherwise.
211
+ *
212
+ * @example
213
+ * ```typescript
214
+ * const isFrozen = documentRef.freeze;
215
+ * console.log(isFrozen); // Output: false
216
+ * ```
217
+ */
218
+ public get freeze(): boolean {
219
+ return this._freeze;
220
+ }
221
+
222
+ /**
223
+ * Sets the freeze status of the document data.
224
+ *
225
+ * @param value - The freeze status to set.
226
+ *
227
+ * @example
228
+ * ```typescript
229
+ * documentRef.freeze = true;
230
+ * console.log(documentRef.freeze); // Output: true
231
+ * ```
232
+ */
233
+ public set freeze(value: boolean) {
234
+ this.logger__.logMethodArgs?.('freeze changed', {value});
235
+ this._freeze = value;
236
+ }
237
+
238
+ /**
239
+ * Retrieves the document's data.
240
+ *
241
+ * @returns The document's data.
242
+ *
243
+ * @example
244
+ * ```typescript
245
+ * const documentData = documentRef.getData();
246
+ * ```
247
+ */
248
+ public getData(): TDoc {
249
+ this.logger__.logMethod?.('getData');
250
+ return this.context__.data;
251
+ }
252
+
253
+ /**
254
+ * Retrieves the document's metadata.
255
+ *
256
+ * @returns The document's metadata.
257
+ *
258
+ * @example
259
+ * ```typescript
260
+ * const documentMeta = documentRef.getStoreMeta();
261
+ * ```
262
+ */
263
+ public getStoreMeta(): Readonly<StoreFileMeta> {
264
+ this.logger__.logMethod?.('getStoreMeta');
265
+ return this.context__.meta;
266
+ }
267
+
268
+ /**
269
+ * Sets the document's data. replacing the existing data.
270
+ *
271
+ * @param data The new document data.
272
+ *
273
+ * @example
274
+ * ```typescript
275
+ * documentRef.replaceData({ a: 1, b: 2, c: 3 });
276
+ * ```
277
+ */
278
+ public replaceData(data: TDoc): void {
279
+ this.logger__.logMethodArgs?.('replaceData', data);
280
+ (this.context__.data as unknown) = data;
281
+ this.updated__();
282
+ }
283
+
284
+ /**
285
+ * Updates document's data by merging a partial update into the document's data.
286
+ *
287
+ * @param data The part of data to merge into the document's data.
288
+ *
289
+ * @example
290
+ * ```typescript
291
+ * documentRef.mergeData({ c: 4 });
292
+ * ```
293
+ */
294
+ public mergeData(data: Partial<TDoc>): void {
295
+ this.logger__.logMethodArgs?.('mergeData', data);
296
+ Object.assign(this.context__.data, data);
297
+ this.updated__();
298
+ }
299
+
300
+ /**
301
+ * Requests the Alwatr Nitrobase to save the document.
302
+ * Saving may take some time in Alwatr Nitrobase due to the use of throttling.
303
+ *
304
+ * @example
305
+ * ```typescript
306
+ * documentRef.save();
307
+ * ```
308
+ */
309
+ public save(): void {
310
+ this.logger__.logMethod?.('save');
311
+ this.updated__();
312
+ }
313
+
314
+ /**
315
+ * Requests the Alwatr Nitrobase to save the document immediately.
316
+ *
317
+ * @example
318
+ * ```typescript
319
+ * documentRef.saveImmediate();
320
+ * ```
321
+ */
322
+ public saveImmediate(): void {
323
+ this.logger__.logMethod?.('saveImmediate');
324
+ this.updated__(/* immediate: */ true);
325
+ }
326
+
327
+ /**
328
+ * Retrieves the full context of the document.
329
+ *
330
+ * @returns The full context of the document.
331
+ *
332
+ * @example
333
+ * ```typescript
334
+ * const context = documentRef.getFullContext_();
335
+ * ```
336
+ */
337
+ public getFullContext_(): Readonly<DocumentContext<TDoc>> {
338
+ this.logger__.logMethod?.('getFullContext_');
339
+ return this.context__;
340
+ }
341
+
342
+ public updateDelayed_ = false;
343
+
344
+ /**
345
+ * Update the document metadata and invoke the updated callback.
346
+ * This method is throttled to prevent multiple updates in a short time.
347
+ */
348
+ private async updated__(immediate = false): Promise<void> {
349
+ this.logger__.logMethodArgs?.('updated__', {immediate, delayed: this.updateDelayed_});
350
+
351
+ this.hasUnprocessedChanges_ = true;
352
+
353
+ if (immediate !== true && this.updateDelayed_ === true) return;
354
+ // else
355
+
356
+ this.updateDelayed_ = true;
357
+
358
+ if (immediate === true || this.context__.meta.changeDebounce === undefined) {
359
+ await delay.nextMacrotask();
360
+ }
361
+ else {
362
+ await delay.by(this.context__.meta.changeDebounce);
363
+ }
364
+
365
+ if (this.updateDelayed_ !== true) return; // another parallel update finished!
366
+ this.updateDelayed_ = false;
367
+
368
+ this.refreshMetadata_();
369
+
370
+ if (this._freeze === true) return; // prevent save if frozen
371
+ this.updatedCallback__(this);
372
+ }
373
+
374
+ /**
375
+ * Refresh/recalculate the document's metadata timestamp and revision.
376
+ */
377
+ protected refreshMetadata_(): void {
378
+ this.logger__.logMethod?.('refreshMetadata_');
379
+ this.context__.meta.updated = Date.now();
380
+ this.context__.meta.rev++;
381
+ }
382
+
383
+ /**
384
+ * Retrieves the document's extra metadata.
385
+ *
386
+ * @returns The document's extra metadata.
387
+ *
388
+ * @example
389
+ * ```typescript
390
+ * const colExtraMeta = documentRef.getExtraMeta();
391
+ * ```
392
+ */
393
+ public getExtraMeta<T extends JsonObject>(): T {
394
+ this.logger__.logMethod?.('getExtraMeta');
395
+ return this.context__.meta.extra as T;
396
+ }
397
+
398
+ /**
399
+ * Sets/replace the document's extra metadata.
400
+ *
401
+ * @param extraMeta The new document's extra metadata.
402
+ *
403
+ * @example
404
+ * ```typescript
405
+ * documentRef.replaceExtraMeta({ a: 1, b: 2, c: 3 });
406
+ * ```
407
+ */
408
+ public replaceExtraMeta<T extends JsonObject>(extraMeta: T): void {
409
+ this.logger__.logMethodArgs?.('replaceExtraMeta', extraMeta);
410
+ this.context__.meta.extra = extraMeta;
411
+ this.updated__();
412
+ }
413
+
414
+ /**
415
+ * Updates document's extra metadata by merging a partial update.
416
+ *
417
+ * @param extraMeta The part of extra metadata to merge into the document's extra metadata.
418
+ *
419
+ * @example
420
+ * ```typescript
421
+ * documentRef.mergeExtraMeta({ c: 4 });
422
+ * ```
423
+ */
424
+ public mergeExtraMeta<T extends JsonObject>(extraMeta: Partial<T>): void {
425
+ this.logger__.logMethodArgs?.('mergeExtraMeta', extraMeta);
426
+ Object.assign(this.context__.meta.extra, extraMeta);
427
+ this.updated__();
428
+ }
429
+ }
package/src/logger.ts ADDED
@@ -0,0 +1,4 @@
1
+ import {createLogger} from '@alwatr/logger';
2
+
3
+
4
+ export const logger = /* #__PURE__ */ createLogger(__package_name__);
package/src/main.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './collection-reference.js';
2
+ export * from './document-reference.js';