@alwatr/nitrobase-reference 7.8.0 → 7.10.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.
- package/CHANGELOG.md +38 -0
- package/LICENSE +373 -661
- package/README.md +0 -6
- package/dist/collection-reference.d.ts.map +1 -1
- package/dist/document-reference.d.ts.map +1 -1
- package/dist/logger.d.ts.map +1 -1
- package/dist/main.cjs +3 -968
- package/dist/main.cjs.map +3 -3
- package/dist/main.mjs +3 -944
- package/dist/main.mjs.map +3 -3
- package/package.json +42 -44
package/dist/main.cjs
CHANGED
|
@@ -1,969 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
"
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
-
|
|
21
|
-
// src/main.ts
|
|
22
|
-
var main_exports = {};
|
|
23
|
-
__export(main_exports, {
|
|
24
|
-
CollectionReference: () => CollectionReference,
|
|
25
|
-
DocumentReference: () => DocumentReference
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(main_exports);
|
|
28
|
-
|
|
29
|
-
// src/collection-reference.ts
|
|
30
|
-
var import_nanolib2 = require("@alwatr/nanolib");
|
|
31
|
-
var import_nitrobase_helper = require("@alwatr/nitrobase-helper");
|
|
32
|
-
var import_nitrobase_types = require("@alwatr/nitrobase-types");
|
|
33
|
-
|
|
34
|
-
// src/logger.ts
|
|
35
|
-
var import_nanolib = require("@alwatr/nanolib");
|
|
36
|
-
__dev_mode__: import_nanolib.packageTracer.add("@alwatr/nitrobase-reference", "7.8.0");
|
|
37
|
-
var logger = /* @__PURE__ */ (0, import_nanolib.createLogger)("@alwatr/nitrobase-reference");
|
|
38
|
-
|
|
39
|
-
// src/collection-reference.ts
|
|
40
|
-
__dev_mode__: logger.logFileModule?.("collection-reference");
|
|
41
|
-
var _CollectionReference = class _CollectionReference {
|
|
42
|
-
/**
|
|
43
|
-
* Collection reference have methods to get, set, update and save the Alwatr Nitrobase Collection.
|
|
44
|
-
* This class is dummy in saving and loading the collection from file.
|
|
45
|
-
* It's the responsibility of the Alwatr Nitrobase to save and load the collection.
|
|
46
|
-
*
|
|
47
|
-
* @param context__ Collection's context filled from the Alwatr Nitrobase (parent).
|
|
48
|
-
* @param updatedCallback__ updated callback to invoke when the collection is updated from the Alwatr Nitrobase (parent).
|
|
49
|
-
* @template TItem - Items data type.
|
|
50
|
-
* @example
|
|
51
|
-
* ```typescript
|
|
52
|
-
* const collectionRef = alwatrStore.col('blog/posts');
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
constructor(context__, updatedCallback__, debugDomain) {
|
|
56
|
-
this.context__ = context__;
|
|
57
|
-
this.updatedCallback__ = updatedCallback__;
|
|
58
|
-
/**
|
|
59
|
-
* Indicates whether the collection has unsaved changes.
|
|
60
|
-
*/
|
|
61
|
-
this.hasUnprocessedChanges_ = false;
|
|
62
|
-
/**
|
|
63
|
-
* Indicates whether the collection data is frozen and cannot be saved.
|
|
64
|
-
*/
|
|
65
|
-
this._freeze = false;
|
|
66
|
-
this.updateDelayed_ = false;
|
|
67
|
-
this.id = (0, import_nitrobase_helper.getStoreId)(this.context__.meta);
|
|
68
|
-
this.path = (0, import_nitrobase_helper.getStorePath)(this.context__.meta);
|
|
69
|
-
debugDomain ?? (debugDomain = this.id.slice(0, 20));
|
|
70
|
-
this.logger__ = (0, import_nanolib2.createLogger)(`col:${debugDomain}`);
|
|
71
|
-
this.logger__.logMethodArgs?.("new", { id: this.id });
|
|
72
|
-
this.validateContext__();
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Creates new CollectionReference instance from stat.
|
|
76
|
-
*
|
|
77
|
-
* @param stat the collection stat.
|
|
78
|
-
* @param initialData the collection data.
|
|
79
|
-
* @param updatedCallback the callback to invoke when the collection changed.
|
|
80
|
-
* @template TItem The collection item data type.
|
|
81
|
-
* @returns A new collection reference class.
|
|
82
|
-
*/
|
|
83
|
-
static newRefFromData(stat, updatedCallback, debugDomain) {
|
|
84
|
-
logger.logMethodArgs?.("col.newRefFromData", stat);
|
|
85
|
-
const now = Date.now();
|
|
86
|
-
const initialContext = {
|
|
87
|
-
ok: true,
|
|
88
|
-
meta: {
|
|
89
|
-
...stat,
|
|
90
|
-
rev: 1,
|
|
91
|
-
updated: now,
|
|
92
|
-
created: now,
|
|
93
|
-
lastAutoId: 0,
|
|
94
|
-
type: import_nitrobase_types.StoreFileType.Collection,
|
|
95
|
-
extension: import_nitrobase_types.StoreFileExtension.Json,
|
|
96
|
-
fv: _CollectionReference.fileFormatVersion,
|
|
97
|
-
extra: {}
|
|
98
|
-
},
|
|
99
|
-
data: {}
|
|
100
|
-
};
|
|
101
|
-
return new _CollectionReference(initialContext, updatedCallback, debugDomain);
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Creates new CollectionReference instance from CollectionContext.
|
|
105
|
-
*
|
|
106
|
-
* @param context the collection context.
|
|
107
|
-
* @param updatedCallback the callback to invoke when the collection changed.
|
|
108
|
-
* @template TItem The collection item data type.
|
|
109
|
-
* @returns A new collection reference class.
|
|
110
|
-
*/
|
|
111
|
-
static newRefFromContext(context, updatedCallback, debugDomain) {
|
|
112
|
-
logger.logMethodArgs?.("col.newRefFromContext", context.meta);
|
|
113
|
-
return new _CollectionReference(context, updatedCallback, debugDomain);
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Validates the collection context and try to migrate it to the latest version.
|
|
117
|
-
*/
|
|
118
|
-
validateContext__() {
|
|
119
|
-
this.logger__.logMethod?.("validateContext__");
|
|
120
|
-
if (this.context__.ok !== true) {
|
|
121
|
-
this.logger__.accident?.("validateContext__", "store_not_ok");
|
|
122
|
-
throw new Error("store_not_ok", { cause: { context: this.context__ } });
|
|
123
|
-
}
|
|
124
|
-
if (this.context__.meta === void 0) {
|
|
125
|
-
this.logger__.accident?.("validateContext__", "store_meta_undefined");
|
|
126
|
-
throw new Error("store_meta_undefined", { cause: { context: this.context__ } });
|
|
127
|
-
}
|
|
128
|
-
if (this.context__.meta.type !== import_nitrobase_types.StoreFileType.Collection) {
|
|
129
|
-
this.logger__.accident?.("validateContext__", "collection_type_invalid", this.context__.meta);
|
|
130
|
-
throw new Error("collection_type_invalid", { cause: this.context__.meta });
|
|
131
|
-
}
|
|
132
|
-
if (this.context__.meta.fv !== _CollectionReference.fileFormatVersion) {
|
|
133
|
-
this.logger__.incident?.("validateContext__", "store_file_version_incompatible", {
|
|
134
|
-
old: this.context__.meta.fv,
|
|
135
|
-
new: _CollectionReference.fileFormatVersion
|
|
136
|
-
});
|
|
137
|
-
this.migrateContext__();
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Migrate the collection context to the latest.
|
|
142
|
-
*/
|
|
143
|
-
migrateContext__() {
|
|
144
|
-
var _a;
|
|
145
|
-
if (this.context__.meta.fv === _CollectionReference.fileFormatVersion) return;
|
|
146
|
-
this.logger__.logMethod?.("migrateContext__");
|
|
147
|
-
if (this.context__.meta.fv > _CollectionReference.fileFormatVersion) {
|
|
148
|
-
this.logger__.accident("migrateContext__", "store_version_incompatible", this.context__.meta);
|
|
149
|
-
throw new Error("store_version_incompatible", { cause: this.context__.meta });
|
|
150
|
-
}
|
|
151
|
-
if (this.context__.meta.fv === 1) {
|
|
152
|
-
this.context__.meta.fv = 2;
|
|
153
|
-
}
|
|
154
|
-
if (this.context__.meta.fv === 2) {
|
|
155
|
-
if (this.context__.meta.schemaVer === void 0 || this.context__.meta.schemaVer === 0) {
|
|
156
|
-
this.context__.meta.schemaVer = 1;
|
|
157
|
-
}
|
|
158
|
-
delete this.context__.meta["ver"];
|
|
159
|
-
(_a = this.context__.meta).extra ?? (_a.extra = {});
|
|
160
|
-
this.context__.meta.fv = 3;
|
|
161
|
-
}
|
|
162
|
-
this.updated__();
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Get nitrobase schema version
|
|
166
|
-
*
|
|
167
|
-
* @returns nitrobase schema version
|
|
168
|
-
*/
|
|
169
|
-
get schemaVer() {
|
|
170
|
-
return this.context__.meta.schemaVer ?? 1;
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Set nitrobase schema version for migrate
|
|
174
|
-
*/
|
|
175
|
-
set schemaVer(ver) {
|
|
176
|
-
this.logger__.logMethodArgs?.("set schemaVer", { old: this.context__.meta.schemaVer, new: ver });
|
|
177
|
-
this.context__.meta.schemaVer = ver;
|
|
178
|
-
this.updated__();
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Gets the freeze status of the collection data.
|
|
182
|
-
*
|
|
183
|
-
* @returns `true` if the collection data is frozen, `false` otherwise.
|
|
184
|
-
*
|
|
185
|
-
* @example
|
|
186
|
-
* ```typescript
|
|
187
|
-
* const isFrozen = collectionRef.freeze;
|
|
188
|
-
* console.log(isFrozen); // Output: false
|
|
189
|
-
* ```
|
|
190
|
-
*/
|
|
191
|
-
get freeze() {
|
|
192
|
-
return this._freeze;
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Sets the freeze status of the collection data.
|
|
196
|
-
*
|
|
197
|
-
* @param value - The freeze status to set.
|
|
198
|
-
*
|
|
199
|
-
* @example
|
|
200
|
-
* ```typescript
|
|
201
|
-
* collectionRef.freeze = true;
|
|
202
|
-
* console.log(collectionRef.freeze); // Output: true
|
|
203
|
-
* ```
|
|
204
|
-
*/
|
|
205
|
-
set freeze(value) {
|
|
206
|
-
this.logger__.logMethodArgs?.("freeze changed", { value });
|
|
207
|
-
this._freeze = value;
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Checks if an item exists in the collection.
|
|
211
|
-
*
|
|
212
|
-
* @param itemId - The ID of the item.
|
|
213
|
-
* @returns `true` if the item with the given ID exists in the collection, `false` otherwise.
|
|
214
|
-
*
|
|
215
|
-
* @example
|
|
216
|
-
* ```typescript
|
|
217
|
-
* const doesExist = collectionRef.hasItem('item1');
|
|
218
|
-
*
|
|
219
|
-
* if (doesExist) {
|
|
220
|
-
* collectionRef.addItem('item1', { key: 'value' });
|
|
221
|
-
* }
|
|
222
|
-
* ```
|
|
223
|
-
*/
|
|
224
|
-
hasItem(itemId) {
|
|
225
|
-
const exists = Object.hasOwn(this.context__.data, itemId);
|
|
226
|
-
this.logger__.logMethodFull?.("hasItem", itemId, exists);
|
|
227
|
-
return exists;
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Retrieves the metadata of the nitrobase file.
|
|
231
|
-
*
|
|
232
|
-
* @returns The metadata of the nitrobase file.
|
|
233
|
-
*
|
|
234
|
-
* @example
|
|
235
|
-
* ```typescript
|
|
236
|
-
* const metadata = collectionRef.getStoreMeta();
|
|
237
|
-
* ```
|
|
238
|
-
*/
|
|
239
|
-
getStoreMeta() {
|
|
240
|
-
this.logger__.logMethod?.("getStoreMeta");
|
|
241
|
-
return this.context__.meta;
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Retrieves an item from the collection. If the item does not exist, an error is thrown.
|
|
245
|
-
*
|
|
246
|
-
* @param itemId - The ID of the item.
|
|
247
|
-
* @returns The item with the given ID.
|
|
248
|
-
*/
|
|
249
|
-
item__(itemId) {
|
|
250
|
-
const item = this.context__.data[itemId];
|
|
251
|
-
if (item === void 0) {
|
|
252
|
-
this.logger__.accident("item__", "collection_item_not_found", { itemId });
|
|
253
|
-
throw new Error("collection_item_not_found", { cause: { itemId } });
|
|
254
|
-
}
|
|
255
|
-
return item;
|
|
256
|
-
}
|
|
257
|
-
/**
|
|
258
|
-
* Retrieves an item's metadata from the collection. If the item does not exist, an error is thrown.
|
|
259
|
-
*
|
|
260
|
-
* @param itemId - The ID of the item.
|
|
261
|
-
* @returns The metadata of the item with the given ID.
|
|
262
|
-
* @example
|
|
263
|
-
* ```typescript
|
|
264
|
-
* const itemMeta = collectionRef.getItemMeta('item1');
|
|
265
|
-
* ```
|
|
266
|
-
*/
|
|
267
|
-
getItemMeta(itemId) {
|
|
268
|
-
const meta = this.item__(itemId).meta;
|
|
269
|
-
this.logger__.logMethodFull?.("getItemMeta", itemId, meta);
|
|
270
|
-
return meta;
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* Retrieves an item's data from the collection. If the item does not exist, an error is thrown.
|
|
274
|
-
*
|
|
275
|
-
* @param itemId - The ID of the item.
|
|
276
|
-
* @returns The data of the item with the given ID.
|
|
277
|
-
*
|
|
278
|
-
* @example
|
|
279
|
-
* ```typescript
|
|
280
|
-
* const itemData = collectionRef.getItemData('item1');
|
|
281
|
-
* ```
|
|
282
|
-
*/
|
|
283
|
-
getItemData(itemId) {
|
|
284
|
-
this.logger__.logMethodArgs?.("getItemData", itemId);
|
|
285
|
-
return this.item__(itemId).data;
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Direct access to an item.
|
|
289
|
-
* If the item does not exist, `undefined` is returned.
|
|
290
|
-
* **USE WITH CAUTION!**
|
|
291
|
-
*
|
|
292
|
-
* @param itemId - The ID of the item.
|
|
293
|
-
* @returns The data of the item with the given ID or `undefined` if the item does not exist.
|
|
294
|
-
*
|
|
295
|
-
* @example
|
|
296
|
-
* ```typescript
|
|
297
|
-
* collectionRef.getItemContext_('item1')?.data.name = 'test2';
|
|
298
|
-
* ```
|
|
299
|
-
*/
|
|
300
|
-
getItemContext_(itemId) {
|
|
301
|
-
this.logger__.logMethodArgs?.("getItemContext_", itemId);
|
|
302
|
-
return this.context__.data[itemId];
|
|
303
|
-
}
|
|
304
|
-
/**
|
|
305
|
-
* Add a new item to the collection.
|
|
306
|
-
* If an item with the given ID already exists, an error is thrown.
|
|
307
|
-
*
|
|
308
|
-
* @param itemId - The ID of the item to create.
|
|
309
|
-
* @param data - The initial data of the item.
|
|
310
|
-
*
|
|
311
|
-
* @example
|
|
312
|
-
* ```typescript
|
|
313
|
-
* collectionRef.addItem('item1', { key: 'value' });
|
|
314
|
-
* ```
|
|
315
|
-
*/
|
|
316
|
-
addItem(itemId, data) {
|
|
317
|
-
this.logger__.logMethodArgs?.("addItem", { itemId, data });
|
|
318
|
-
if (this.hasItem(itemId)) {
|
|
319
|
-
this.logger__.accident("addItem", "collection_item_exist", { itemId });
|
|
320
|
-
throw new Error("collection_item_exist", { cause: { itemId } });
|
|
321
|
-
}
|
|
322
|
-
const now = Date.now();
|
|
323
|
-
this.context__.data[itemId] = {
|
|
324
|
-
meta: {
|
|
325
|
-
id: itemId,
|
|
326
|
-
// other prop calc in updateMeta__
|
|
327
|
-
rev: 0,
|
|
328
|
-
created: now,
|
|
329
|
-
updated: now
|
|
330
|
-
},
|
|
331
|
-
data
|
|
332
|
-
};
|
|
333
|
-
this.updated__(itemId);
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* Appends the given data to the collection with auto increment ID.
|
|
337
|
-
*
|
|
338
|
-
* @param data - The data to append.
|
|
339
|
-
* @returns The ID of the appended item.
|
|
340
|
-
*
|
|
341
|
-
* @example
|
|
342
|
-
* ```typescript
|
|
343
|
-
* const newId = collectionRef.appendItem({ key: 'value' });
|
|
344
|
-
* ```
|
|
345
|
-
*/
|
|
346
|
-
appendItem(data) {
|
|
347
|
-
this.logger__.logMethodArgs?.("appendItem", data);
|
|
348
|
-
const id = this.nextAutoIncrementId__();
|
|
349
|
-
this.addItem(id, data);
|
|
350
|
-
return id;
|
|
351
|
-
}
|
|
352
|
-
/**
|
|
353
|
-
* Removes an item from the collection.
|
|
354
|
-
*
|
|
355
|
-
* @param itemId - The ID of the item to delete.
|
|
356
|
-
*
|
|
357
|
-
* @example
|
|
358
|
-
* ```typescript
|
|
359
|
-
* collectionRef.removeItem('item1');
|
|
360
|
-
* collectionRef.hasItem('item1'); // Output: false
|
|
361
|
-
* ```
|
|
362
|
-
*/
|
|
363
|
-
removeItem(itemId) {
|
|
364
|
-
this.logger__.logMethodArgs?.("removeItem", itemId);
|
|
365
|
-
delete this.context__.data[itemId];
|
|
366
|
-
this.updated__();
|
|
367
|
-
}
|
|
368
|
-
/**
|
|
369
|
-
* Sets an item's data in the collection. Replaces the item's data with the given data.
|
|
370
|
-
*
|
|
371
|
-
* @param itemId - The ID of the item to set.
|
|
372
|
-
* @param data - The data to set for the item.
|
|
373
|
-
*
|
|
374
|
-
* @example
|
|
375
|
-
* ```typescript
|
|
376
|
-
* collectionRef.replaceItemData('item1', { a: 1, b: 2, c: 3 });
|
|
377
|
-
* ```
|
|
378
|
-
*/
|
|
379
|
-
replaceItemData(itemId, data) {
|
|
380
|
-
this.logger__.logMethodArgs?.("replaceItemData", { itemId, data });
|
|
381
|
-
this.item__(itemId).data = data;
|
|
382
|
-
this.updated__(itemId);
|
|
383
|
-
}
|
|
384
|
-
/**
|
|
385
|
-
* Updates an item in the collection by merging a partial update into the item's data.
|
|
386
|
-
*
|
|
387
|
-
* @param itemId - The ID of the item to update.
|
|
388
|
-
* @param data - The part of data to merge into the item's data.
|
|
389
|
-
*
|
|
390
|
-
* @example
|
|
391
|
-
* ```typescript
|
|
392
|
-
* collectionRef.mergeItemData(itemId, partialUpdate);
|
|
393
|
-
* ```
|
|
394
|
-
*/
|
|
395
|
-
mergeItemData(itemId, data) {
|
|
396
|
-
this.logger__.logMethodArgs?.("mergeItemData", { itemId, data });
|
|
397
|
-
Object.assign(this.item__(itemId).data, data);
|
|
398
|
-
this.updated__(itemId);
|
|
399
|
-
}
|
|
400
|
-
/**
|
|
401
|
-
* Requests the Alwatr Nitrobase to save the collection.
|
|
402
|
-
* Saving may take some time in Alwatr Nitrobase due to the use of throttling.
|
|
403
|
-
*
|
|
404
|
-
* @example
|
|
405
|
-
* ```typescript
|
|
406
|
-
* collectionRef.save();
|
|
407
|
-
* ```
|
|
408
|
-
*/
|
|
409
|
-
save(itemId) {
|
|
410
|
-
this.logger__.logMethod?.("save");
|
|
411
|
-
this.updated__(itemId, false);
|
|
412
|
-
}
|
|
413
|
-
/**
|
|
414
|
-
* Requests the Alwatr Nitrobase to save the collection immediately.
|
|
415
|
-
*
|
|
416
|
-
* @example
|
|
417
|
-
* ```typescript
|
|
418
|
-
* collectionRef.saveImmediate();
|
|
419
|
-
* ```
|
|
420
|
-
*/
|
|
421
|
-
saveImmediate(itemId) {
|
|
422
|
-
this.logger__.logMethod?.("saveImmediate");
|
|
423
|
-
this.updated__(itemId, true);
|
|
424
|
-
}
|
|
425
|
-
/**
|
|
426
|
-
* Retrieves the IDs of all items in the collection in array.
|
|
427
|
-
* Impact performance if the collection is large, use `ids()` instead.
|
|
428
|
-
*
|
|
429
|
-
* @returns Array of IDs of all items in the collection.
|
|
430
|
-
* @example
|
|
431
|
-
* ```typescript
|
|
432
|
-
* const ids = collectionRef.keys();
|
|
433
|
-
* ```
|
|
434
|
-
*/
|
|
435
|
-
keys() {
|
|
436
|
-
this.logger__.logMethod?.("keys");
|
|
437
|
-
return Object.keys(this.context__.data);
|
|
438
|
-
}
|
|
439
|
-
/**
|
|
440
|
-
* Retrieves all items in the collection in array.
|
|
441
|
-
* Impact performance if the collection is large, use `items()` instead.
|
|
442
|
-
*
|
|
443
|
-
* @returns Array of all items in the collection.
|
|
444
|
-
* @example
|
|
445
|
-
* ```typescript
|
|
446
|
-
* const items = collectionRef.values();
|
|
447
|
-
* console.log('meta: %o', items[0].meta);
|
|
448
|
-
* console.log('data: %o', items[0].data);
|
|
449
|
-
* ```
|
|
450
|
-
*/
|
|
451
|
-
values() {
|
|
452
|
-
this.logger__.logMethod?.("values");
|
|
453
|
-
return Object.values(this.context__.data);
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* Retrieves the IDs of all items in the collection.
|
|
457
|
-
* Use this method instead of `keys()` if the collection is large.
|
|
458
|
-
* This method is a generator and can be used in `for...of` loops.
|
|
459
|
-
* @returns Generator of IDs of all items in the collection.
|
|
460
|
-
* @example
|
|
461
|
-
* ```typescript
|
|
462
|
-
* for (const id of collectionRef.ids()) {
|
|
463
|
-
* const doc = collectionRef.get(id);
|
|
464
|
-
* }
|
|
465
|
-
* ```
|
|
466
|
-
*/
|
|
467
|
-
*ids() {
|
|
468
|
-
this.logger__.logMethod?.("ids");
|
|
469
|
-
for (const id in this.context__.data) {
|
|
470
|
-
yield id;
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
/**
|
|
474
|
-
* Retrieves all items in the collection.
|
|
475
|
-
* Use this method instead of `values()` if the collection is large.
|
|
476
|
-
* This method is a generator and can be used in `for...of` loops.
|
|
477
|
-
* @returns Generator of all items in the collection.
|
|
478
|
-
* @example
|
|
479
|
-
* ```typescript
|
|
480
|
-
* for (const item of collectionRef.items()) {
|
|
481
|
-
* console.log(item.data);
|
|
482
|
-
* }
|
|
483
|
-
*/
|
|
484
|
-
*items() {
|
|
485
|
-
this.logger__.logMethod?.("items");
|
|
486
|
-
for (const id in this.context__.data) {
|
|
487
|
-
yield this.context__.data[id];
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
/**
|
|
491
|
-
* Retrieves the full context of the collection.
|
|
492
|
-
*
|
|
493
|
-
* @returns The full context of the collection.
|
|
494
|
-
*
|
|
495
|
-
* @example
|
|
496
|
-
* ```typescript
|
|
497
|
-
* const context = collectionRef.getFullContext_();
|
|
498
|
-
* ```
|
|
499
|
-
*/
|
|
500
|
-
getFullContext_() {
|
|
501
|
-
this.logger__.logMethod?.("getFullContext_");
|
|
502
|
-
return this.context__;
|
|
503
|
-
}
|
|
504
|
-
/**
|
|
505
|
-
* Update the document metadata and invoke the updated callback.
|
|
506
|
-
* This method is throttled to prevent multiple updates in a short time.
|
|
507
|
-
*
|
|
508
|
-
* @param itemId - The ID of the item to update.
|
|
509
|
-
*/
|
|
510
|
-
async updated__(itemId = null, immediate = false) {
|
|
511
|
-
this.logger__.logMethodArgs?.("updated__", { id: itemId, immediate, delayed: this.updateDelayed_ });
|
|
512
|
-
this.hasUnprocessedChanges_ = true;
|
|
513
|
-
if (itemId !== null) this.refreshMeta_(itemId);
|
|
514
|
-
if (immediate === false && this.updateDelayed_ === true) return;
|
|
515
|
-
this.updateDelayed_ = true;
|
|
516
|
-
if (immediate === true || this.context__.meta.changeDebounce === void 0) {
|
|
517
|
-
await import_nanolib2.delay.immediate();
|
|
518
|
-
} else {
|
|
519
|
-
await import_nanolib2.delay.by(this.context__.meta.changeDebounce);
|
|
520
|
-
}
|
|
521
|
-
if (this.updateDelayed_ !== true) return;
|
|
522
|
-
this.updateDelayed_ = false;
|
|
523
|
-
if (itemId === null) this.refreshMeta_(itemId);
|
|
524
|
-
if (this._freeze === true) return;
|
|
525
|
-
this.updatedCallback__(this);
|
|
526
|
-
}
|
|
527
|
-
/**
|
|
528
|
-
* Refresh/recalculate the collection's metadata timestamp and revision.
|
|
529
|
-
*
|
|
530
|
-
* @param itemId - The ID of the item to update.
|
|
531
|
-
*/
|
|
532
|
-
refreshMeta_(itemId) {
|
|
533
|
-
this.logger__.logMethodArgs?.("refreshMeta_", { id: itemId });
|
|
534
|
-
const now = Date.now();
|
|
535
|
-
this.context__.meta.rev++;
|
|
536
|
-
this.context__.meta.updated = now;
|
|
537
|
-
if (itemId !== null) {
|
|
538
|
-
const itemMeta = this.item__(itemId).meta;
|
|
539
|
-
itemMeta.rev++;
|
|
540
|
-
itemMeta.updated = now;
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
/**
|
|
544
|
-
* Generates the next auto increment ID.
|
|
545
|
-
*
|
|
546
|
-
* @returns The next auto increment ID.
|
|
547
|
-
* @example
|
|
548
|
-
* ```typescript
|
|
549
|
-
* const nextId = this.nextAutoIncrementId_();
|
|
550
|
-
* ```
|
|
551
|
-
*/
|
|
552
|
-
nextAutoIncrementId__() {
|
|
553
|
-
this.logger__.logMethod?.("nextAutoIncrementId__");
|
|
554
|
-
const meta = this.context__.meta;
|
|
555
|
-
do {
|
|
556
|
-
meta.lastAutoId++;
|
|
557
|
-
} while (meta.lastAutoId in this.context__.data);
|
|
558
|
-
return meta.lastAutoId;
|
|
559
|
-
}
|
|
560
|
-
/**
|
|
561
|
-
* Retrieves the collection's extra metadata.
|
|
562
|
-
*
|
|
563
|
-
* @returns The collection's extra metadata.
|
|
564
|
-
*
|
|
565
|
-
* @example
|
|
566
|
-
* ```typescript
|
|
567
|
-
* const colExtraMeta = collectionRef.getExtraMeta();
|
|
568
|
-
* ```
|
|
569
|
-
*/
|
|
570
|
-
getExtraMeta() {
|
|
571
|
-
this.logger__.logMethod?.("getExtraMeta");
|
|
572
|
-
return this.context__.meta.extra;
|
|
573
|
-
}
|
|
574
|
-
/**
|
|
575
|
-
* Sets/replace the collection's extra metadata.
|
|
576
|
-
*
|
|
577
|
-
* @param extraMeta The new collection's extra metadata.
|
|
578
|
-
*
|
|
579
|
-
* @example
|
|
580
|
-
* ```typescript
|
|
581
|
-
* collectionRef.replaceExtraMeta({ a: 1, b: 2, c: 3 });
|
|
582
|
-
* ```
|
|
583
|
-
*/
|
|
584
|
-
replaceExtraMeta(extraMeta) {
|
|
585
|
-
this.logger__.logMethodArgs?.("replaceExtraMeta", extraMeta);
|
|
586
|
-
this.context__.meta.extra = extraMeta;
|
|
587
|
-
this.updated__();
|
|
588
|
-
}
|
|
589
|
-
/**
|
|
590
|
-
* Updates collection's extra metadata by merging a partial update.
|
|
591
|
-
*
|
|
592
|
-
* @param extraMeta The part of extra metadata to merge into the collection's extra metadata.
|
|
593
|
-
*
|
|
594
|
-
* @example
|
|
595
|
-
* ```typescript
|
|
596
|
-
* collectionRef.mergeExtraMeta({ c: 4 });
|
|
597
|
-
* ```
|
|
598
|
-
*/
|
|
599
|
-
mergeExtraMeta(extraMeta) {
|
|
600
|
-
this.logger__.logMethodArgs?.("mergeExtraMeta", extraMeta);
|
|
601
|
-
Object.assign(this.context__.meta.extra, extraMeta);
|
|
602
|
-
this.updated__();
|
|
603
|
-
}
|
|
604
|
-
};
|
|
605
|
-
/**
|
|
606
|
-
* Alwatr nitrobase engine version string.
|
|
607
|
-
*/
|
|
608
|
-
_CollectionReference.version = "7.8.0";
|
|
609
|
-
/**
|
|
610
|
-
* Alwatr nitrobase engine file format version number.
|
|
611
|
-
*/
|
|
612
|
-
_CollectionReference.fileFormatVersion = 3;
|
|
613
|
-
var CollectionReference = _CollectionReference;
|
|
614
|
-
|
|
615
|
-
// src/document-reference.ts
|
|
616
|
-
var import_nanolib3 = require("@alwatr/nanolib");
|
|
617
|
-
var import_nitrobase_helper2 = require("@alwatr/nitrobase-helper");
|
|
618
|
-
var import_nitrobase_types2 = require("@alwatr/nitrobase-types");
|
|
619
|
-
__dev_mode__: logger.logFileModule?.("document-reference");
|
|
620
|
-
var _DocumentReference = class _DocumentReference {
|
|
621
|
-
/**
|
|
622
|
-
* Create a new document reference.
|
|
623
|
-
* Document reference have methods to get, set, update and save the AlwatrNitrobase Document.
|
|
624
|
-
*
|
|
625
|
-
* @param context__ Document's context filled from the Alwatr Nitrobase (parent).
|
|
626
|
-
* @param updatedCallback__ updated callback to invoke when the document is updated from the Alwatr Nitrobase (parent).
|
|
627
|
-
* @template TDoc The document data type.
|
|
628
|
-
*/
|
|
629
|
-
constructor(context__, updatedCallback__, debugDomain) {
|
|
630
|
-
this.context__ = context__;
|
|
631
|
-
this.updatedCallback__ = updatedCallback__;
|
|
632
|
-
/**
|
|
633
|
-
* Indicates whether the document has unsaved changes.
|
|
634
|
-
*/
|
|
635
|
-
this.hasUnprocessedChanges_ = false;
|
|
636
|
-
/**
|
|
637
|
-
* Indicates whether the document data is frozen and cannot be saved.
|
|
638
|
-
*/
|
|
639
|
-
this._freeze = false;
|
|
640
|
-
this.updateDelayed_ = false;
|
|
641
|
-
this.id = (0, import_nitrobase_helper2.getStoreId)(this.context__.meta);
|
|
642
|
-
this.path = (0, import_nitrobase_helper2.getStorePath)(this.context__.meta);
|
|
643
|
-
debugDomain ?? (debugDomain = this.id.slice(0, 20));
|
|
644
|
-
this.logger__ = (0, import_nanolib3.createLogger)(`doc:${debugDomain}`);
|
|
645
|
-
this.logger__.logMethodArgs?.("new", { path: this.path });
|
|
646
|
-
this.validateContext__();
|
|
647
|
-
}
|
|
648
|
-
/**
|
|
649
|
-
* Creates new DocumentReference instance from stat and initial data.
|
|
650
|
-
*
|
|
651
|
-
* @param statId the document stat.
|
|
652
|
-
* @param data the document data.
|
|
653
|
-
* @param updatedCallback the callback to invoke when the document changed.
|
|
654
|
-
* @template TDoc The document data type.
|
|
655
|
-
* @returns A new document reference class.
|
|
656
|
-
*/
|
|
657
|
-
static newRefFromData(statId, data, updatedCallback, debugDomain) {
|
|
658
|
-
logger.logMethodArgs?.("doc.newRefFromData", statId);
|
|
659
|
-
const now = Date.now();
|
|
660
|
-
const initialContext = {
|
|
661
|
-
ok: true,
|
|
662
|
-
meta: {
|
|
663
|
-
...statId,
|
|
664
|
-
rev: 1,
|
|
665
|
-
updated: now,
|
|
666
|
-
created: now,
|
|
667
|
-
type: import_nitrobase_types2.StoreFileType.Document,
|
|
668
|
-
extension: import_nitrobase_types2.StoreFileExtension.Json,
|
|
669
|
-
fv: _DocumentReference.fileFormatVersion,
|
|
670
|
-
extra: {}
|
|
671
|
-
},
|
|
672
|
-
data
|
|
673
|
-
};
|
|
674
|
-
return new _DocumentReference(initialContext, updatedCallback, debugDomain);
|
|
675
|
-
}
|
|
676
|
-
/**
|
|
677
|
-
* Creates new DocumentReference instance from DocumentContext.
|
|
678
|
-
*
|
|
679
|
-
* @param context the document context.
|
|
680
|
-
* @param updatedCallback the callback to invoke when the document changed.
|
|
681
|
-
* @template TDoc The document data type.
|
|
682
|
-
* @returns A new document reference class.
|
|
683
|
-
*/
|
|
684
|
-
static newRefFromContext(context, updatedCallback, debugDomain) {
|
|
685
|
-
logger.logMethodArgs?.("doc.newRefFromContext", context.meta);
|
|
686
|
-
return new _DocumentReference(context, updatedCallback, debugDomain);
|
|
687
|
-
}
|
|
688
|
-
/**
|
|
689
|
-
* Validates the document context and try to migrate it to the latest version.
|
|
690
|
-
*/
|
|
691
|
-
validateContext__() {
|
|
692
|
-
this.logger__.logMethod?.("validateContext__");
|
|
693
|
-
if (this.context__.ok !== true) {
|
|
694
|
-
this.logger__.accident?.("validateContext__", "store_not_ok");
|
|
695
|
-
throw new Error("store_not_ok", { cause: { context: this.context__ } });
|
|
696
|
-
}
|
|
697
|
-
if (this.context__.meta === void 0) {
|
|
698
|
-
this.logger__.accident?.("validateContext__", "store_meta_undefined");
|
|
699
|
-
throw new Error("store_meta_undefined", { cause: { context: this.context__ } });
|
|
700
|
-
}
|
|
701
|
-
if (this.context__.meta.type !== import_nitrobase_types2.StoreFileType.Document) {
|
|
702
|
-
this.logger__.accident?.("validateContext__", "document_type_invalid", this.context__.meta);
|
|
703
|
-
throw new Error("document_type_invalid", { cause: this.context__.meta });
|
|
704
|
-
}
|
|
705
|
-
if (this.context__.meta.fv !== _DocumentReference.fileFormatVersion) {
|
|
706
|
-
this.logger__.incident?.("validateContext__", "store_file_version_incompatible", {
|
|
707
|
-
old: this.context__.meta.fv,
|
|
708
|
-
new: _DocumentReference.fileFormatVersion
|
|
709
|
-
});
|
|
710
|
-
this.migrateContext__();
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
|
-
/**
|
|
714
|
-
* Migrate the document context to the latest.
|
|
715
|
-
*/
|
|
716
|
-
migrateContext__() {
|
|
717
|
-
var _a;
|
|
718
|
-
if (this.context__.meta.fv === _DocumentReference.fileFormatVersion) return;
|
|
719
|
-
this.logger__.logMethod?.("migrateContext__");
|
|
720
|
-
if (this.context__.meta.fv > _DocumentReference.fileFormatVersion) {
|
|
721
|
-
this.logger__.accident("migrateContext__", "store_version_incompatible", this.context__.meta);
|
|
722
|
-
throw new Error("store_version_incompatible", { cause: this.context__.meta });
|
|
723
|
-
}
|
|
724
|
-
if (this.context__.meta.fv === 1) {
|
|
725
|
-
this.context__.meta.fv = 2;
|
|
726
|
-
}
|
|
727
|
-
if (this.context__.meta.fv === 2) {
|
|
728
|
-
if (this.context__.meta.schemaVer === void 0 || this.context__.meta.schemaVer === 0) {
|
|
729
|
-
this.context__.meta.schemaVer = 1;
|
|
730
|
-
}
|
|
731
|
-
delete this.context__.meta["ver"];
|
|
732
|
-
(_a = this.context__.meta).extra ?? (_a.extra = {});
|
|
733
|
-
this.context__.meta.fv = 3;
|
|
734
|
-
}
|
|
735
|
-
this.updated__();
|
|
736
|
-
}
|
|
737
|
-
/**
|
|
738
|
-
* Get nitrobase schema version
|
|
739
|
-
*
|
|
740
|
-
* @returns nitrobase schema version
|
|
741
|
-
*/
|
|
742
|
-
get schemaVer() {
|
|
743
|
-
return this.context__.meta.schemaVer ?? 1;
|
|
744
|
-
}
|
|
745
|
-
/**
|
|
746
|
-
* Set nitrobase schema version for migrate
|
|
747
|
-
*/
|
|
748
|
-
set schemaVer(ver) {
|
|
749
|
-
this.logger__.logMethodArgs?.("set schemaVer", { old: this.context__.meta.schemaVer, new: ver });
|
|
750
|
-
this.context__.meta.schemaVer = ver;
|
|
751
|
-
this.updated__();
|
|
752
|
-
}
|
|
753
|
-
/**
|
|
754
|
-
* Gets the freeze status of the document data.
|
|
755
|
-
*
|
|
756
|
-
* @returns `true` if the document data is frozen, `false` otherwise.
|
|
757
|
-
*
|
|
758
|
-
* @example
|
|
759
|
-
* ```typescript
|
|
760
|
-
* const isFrozen = documentRef.freeze;
|
|
761
|
-
* console.log(isFrozen); // Output: false
|
|
762
|
-
* ```
|
|
763
|
-
*/
|
|
764
|
-
get freeze() {
|
|
765
|
-
return this._freeze;
|
|
766
|
-
}
|
|
767
|
-
/**
|
|
768
|
-
* Sets the freeze status of the document data.
|
|
769
|
-
*
|
|
770
|
-
* @param value - The freeze status to set.
|
|
771
|
-
*
|
|
772
|
-
* @example
|
|
773
|
-
* ```typescript
|
|
774
|
-
* documentRef.freeze = true;
|
|
775
|
-
* console.log(documentRef.freeze); // Output: true
|
|
776
|
-
* ```
|
|
777
|
-
*/
|
|
778
|
-
set freeze(value) {
|
|
779
|
-
this.logger__.logMethodArgs?.("freeze changed", { value });
|
|
780
|
-
this._freeze = value;
|
|
781
|
-
}
|
|
782
|
-
/**
|
|
783
|
-
* Retrieves the document's data.
|
|
784
|
-
*
|
|
785
|
-
* @returns The document's data.
|
|
786
|
-
*
|
|
787
|
-
* @example
|
|
788
|
-
* ```typescript
|
|
789
|
-
* const documentData = documentRef.getData();
|
|
790
|
-
* ```
|
|
791
|
-
*/
|
|
792
|
-
getData() {
|
|
793
|
-
this.logger__.logMethod?.("getData");
|
|
794
|
-
return this.context__.data;
|
|
795
|
-
}
|
|
796
|
-
/**
|
|
797
|
-
* Retrieves the document's metadata.
|
|
798
|
-
*
|
|
799
|
-
* @returns The document's metadata.
|
|
800
|
-
*
|
|
801
|
-
* @example
|
|
802
|
-
* ```typescript
|
|
803
|
-
* const documentMeta = documentRef.getStoreMeta();
|
|
804
|
-
* ```
|
|
805
|
-
*/
|
|
806
|
-
getStoreMeta() {
|
|
807
|
-
this.logger__.logMethod?.("getStoreMeta");
|
|
808
|
-
return this.context__.meta;
|
|
809
|
-
}
|
|
810
|
-
/**
|
|
811
|
-
* Sets the document's data. replacing the existing data.
|
|
812
|
-
*
|
|
813
|
-
* @param data The new document data.
|
|
814
|
-
*
|
|
815
|
-
* @example
|
|
816
|
-
* ```typescript
|
|
817
|
-
* documentRef.replaceData({ a: 1, b: 2, c: 3 });
|
|
818
|
-
* ```
|
|
819
|
-
*/
|
|
820
|
-
replaceData(data) {
|
|
821
|
-
this.logger__.logMethodArgs?.("replaceData", data);
|
|
822
|
-
this.context__.data = data;
|
|
823
|
-
this.updated__();
|
|
824
|
-
}
|
|
825
|
-
/**
|
|
826
|
-
* Updates document's data by merging a partial update into the document's data.
|
|
827
|
-
*
|
|
828
|
-
* @param data The part of data to merge into the document's data.
|
|
829
|
-
*
|
|
830
|
-
* @example
|
|
831
|
-
* ```typescript
|
|
832
|
-
* documentRef.mergeData({ c: 4 });
|
|
833
|
-
* ```
|
|
834
|
-
*/
|
|
835
|
-
mergeData(data) {
|
|
836
|
-
this.logger__.logMethodArgs?.("mergeData", data);
|
|
837
|
-
Object.assign(this.context__.data, data);
|
|
838
|
-
this.updated__();
|
|
839
|
-
}
|
|
840
|
-
/**
|
|
841
|
-
* Requests the Alwatr Nitrobase to save the document.
|
|
842
|
-
* Saving may take some time in Alwatr Nitrobase due to the use of throttling.
|
|
843
|
-
*
|
|
844
|
-
* @example
|
|
845
|
-
* ```typescript
|
|
846
|
-
* documentRef.save();
|
|
847
|
-
* ```
|
|
848
|
-
*/
|
|
849
|
-
save() {
|
|
850
|
-
this.logger__.logMethod?.("save");
|
|
851
|
-
this.updated__();
|
|
852
|
-
}
|
|
853
|
-
/**
|
|
854
|
-
* Requests the Alwatr Nitrobase to save the document immediately.
|
|
855
|
-
*
|
|
856
|
-
* @example
|
|
857
|
-
* ```typescript
|
|
858
|
-
* documentRef.saveImmediate();
|
|
859
|
-
* ```
|
|
860
|
-
*/
|
|
861
|
-
saveImmediate() {
|
|
862
|
-
this.logger__.logMethod?.("saveImmediate");
|
|
863
|
-
this.updated__(
|
|
864
|
-
/* immediate: */
|
|
865
|
-
true
|
|
866
|
-
);
|
|
867
|
-
}
|
|
868
|
-
/**
|
|
869
|
-
* Retrieves the full context of the document.
|
|
870
|
-
*
|
|
871
|
-
* @returns The full context of the document.
|
|
872
|
-
*
|
|
873
|
-
* @example
|
|
874
|
-
* ```typescript
|
|
875
|
-
* const context = documentRef.getFullContext_();
|
|
876
|
-
* ```
|
|
877
|
-
*/
|
|
878
|
-
getFullContext_() {
|
|
879
|
-
this.logger__.logMethod?.("getFullContext_");
|
|
880
|
-
return this.context__;
|
|
881
|
-
}
|
|
882
|
-
/**
|
|
883
|
-
* Update the document metadata and invoke the updated callback.
|
|
884
|
-
* This method is throttled to prevent multiple updates in a short time.
|
|
885
|
-
*/
|
|
886
|
-
async updated__(immediate = false) {
|
|
887
|
-
this.logger__.logMethodArgs?.("updated__", { immediate, delayed: this.updateDelayed_ });
|
|
888
|
-
this.hasUnprocessedChanges_ = true;
|
|
889
|
-
if (immediate !== true && this.updateDelayed_ === true) return;
|
|
890
|
-
this.updateDelayed_ = true;
|
|
891
|
-
if (immediate === true || this.context__.meta.changeDebounce === void 0) {
|
|
892
|
-
await import_nanolib3.delay.immediate();
|
|
893
|
-
} else {
|
|
894
|
-
await import_nanolib3.delay.by(this.context__.meta.changeDebounce);
|
|
895
|
-
}
|
|
896
|
-
if (this.updateDelayed_ !== true) return;
|
|
897
|
-
this.updateDelayed_ = false;
|
|
898
|
-
this.refreshMetadata_();
|
|
899
|
-
if (this._freeze === true) return;
|
|
900
|
-
this.updatedCallback__(this);
|
|
901
|
-
}
|
|
902
|
-
/**
|
|
903
|
-
* Refresh/recalculate the document's metadata timestamp and revision.
|
|
904
|
-
*/
|
|
905
|
-
refreshMetadata_() {
|
|
906
|
-
this.logger__.logMethod?.("refreshMetadata_");
|
|
907
|
-
this.context__.meta.updated = Date.now();
|
|
908
|
-
this.context__.meta.rev++;
|
|
909
|
-
}
|
|
910
|
-
/**
|
|
911
|
-
* Retrieves the document's extra metadata.
|
|
912
|
-
*
|
|
913
|
-
* @returns The document's extra metadata.
|
|
914
|
-
*
|
|
915
|
-
* @example
|
|
916
|
-
* ```typescript
|
|
917
|
-
* const colExtraMeta = documentRef.getExtraMeta();
|
|
918
|
-
* ```
|
|
919
|
-
*/
|
|
920
|
-
getExtraMeta() {
|
|
921
|
-
this.logger__.logMethod?.("getExtraMeta");
|
|
922
|
-
return this.context__.meta.extra;
|
|
923
|
-
}
|
|
924
|
-
/**
|
|
925
|
-
* Sets/replace the document's extra metadata.
|
|
926
|
-
*
|
|
927
|
-
* @param extraMeta The new document's extra metadata.
|
|
928
|
-
*
|
|
929
|
-
* @example
|
|
930
|
-
* ```typescript
|
|
931
|
-
* documentRef.replaceExtraMeta({ a: 1, b: 2, c: 3 });
|
|
932
|
-
* ```
|
|
933
|
-
*/
|
|
934
|
-
replaceExtraMeta(extraMeta) {
|
|
935
|
-
this.logger__.logMethodArgs?.("replaceExtraMeta", extraMeta);
|
|
936
|
-
this.context__.meta.extra = extraMeta;
|
|
937
|
-
this.updated__();
|
|
938
|
-
}
|
|
939
|
-
/**
|
|
940
|
-
* Updates document's extra metadata by merging a partial update.
|
|
941
|
-
*
|
|
942
|
-
* @param extraMeta The part of extra metadata to merge into the document's extra metadata.
|
|
943
|
-
*
|
|
944
|
-
* @example
|
|
945
|
-
* ```typescript
|
|
946
|
-
* documentRef.mergeExtraMeta({ c: 4 });
|
|
947
|
-
* ```
|
|
948
|
-
*/
|
|
949
|
-
mergeExtraMeta(extraMeta) {
|
|
950
|
-
this.logger__.logMethodArgs?.("mergeExtraMeta", extraMeta);
|
|
951
|
-
Object.assign(this.context__.meta.extra, extraMeta);
|
|
952
|
-
this.updated__();
|
|
953
|
-
}
|
|
954
|
-
};
|
|
955
|
-
/**
|
|
956
|
-
* Alwatr nitrobase engine version string.
|
|
957
|
-
*/
|
|
958
|
-
_DocumentReference.version = "7.8.0";
|
|
959
|
-
/**
|
|
960
|
-
* Alwatr nitrobase engine file format version number.
|
|
961
|
-
*/
|
|
962
|
-
_DocumentReference.fileFormatVersion = 3;
|
|
963
|
-
var DocumentReference = _DocumentReference;
|
|
964
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
965
|
-
0 && (module.exports = {
|
|
966
|
-
CollectionReference,
|
|
967
|
-
DocumentReference
|
|
968
|
-
});
|
|
1
|
+
/** 📦 @alwatr/nitrobase-reference v7.10.1 */
|
|
2
|
+
__dev_mode__: console.debug("📦 @alwatr/nitrobase-reference v7.10.1");
|
|
3
|
+
"use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from==="object"||typeof from==="function"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:true}),mod);var main_exports={};__export(main_exports,{CollectionReference:()=>CollectionReference,DocumentReference:()=>DocumentReference});module.exports=__toCommonJS(main_exports);var import_delay=require("@alwatr/delay");var import_logger2=require("@alwatr/logger");var import_nitrobase_helper=require("@alwatr/nitrobase-helper");var import_nitrobase_types=require("@alwatr/nitrobase-types");var import_logger=require("@alwatr/logger");var logger=(0,import_logger.createLogger)("@alwatr/nitrobase-reference");__dev_mode__:logger.logFileModule?.("collection-reference");var CollectionReference=class _CollectionReference{constructor(context__,updatedCallback__,debugDomain){this.context__=context__;this.updatedCallback__=updatedCallback__;this.hasUnprocessedChanges_=false;this._freeze=false;this.updateDelayed_=false;this.id=(0,import_nitrobase_helper.getStoreId)(this.context__.meta);this.path=(0,import_nitrobase_helper.getStorePath)(this.context__.meta);debugDomain??=this.id.slice(0,20);this.logger__=(0,import_logger2.createLogger)(`col:${debugDomain}`);this.logger__.logMethodArgs?.("new",{id:this.id});this.validateContext__()}static{this.version="7.10.1"}static{this.fileFormatVersion=3}static newRefFromData(stat,updatedCallback,debugDomain){logger.logMethodArgs?.("col.newRefFromData",stat);const now=Date.now();const initialContext={ok:true,meta:{...stat,rev:1,updated:now,created:now,lastAutoId:0,type:import_nitrobase_types.StoreFileType.Collection,extension:import_nitrobase_types.StoreFileExtension.Json,fv:_CollectionReference.fileFormatVersion,extra:{}},data:{}};return new _CollectionReference(initialContext,updatedCallback,debugDomain)}static newRefFromContext(context,updatedCallback,debugDomain){logger.logMethodArgs?.("col.newRefFromContext",context.meta);return new _CollectionReference(context,updatedCallback,debugDomain)}validateContext__(){this.logger__.logMethod?.("validateContext__");if(this.context__.ok!==true){this.logger__.accident?.("validateContext__","store_not_ok");throw new Error("store_not_ok",{cause:{context:this.context__}})}if(this.context__.meta===void 0){this.logger__.accident?.("validateContext__","store_meta_undefined");throw new Error("store_meta_undefined",{cause:{context:this.context__}})}if(this.context__.meta.type!==import_nitrobase_types.StoreFileType.Collection){this.logger__.accident?.("validateContext__","collection_type_invalid",this.context__.meta);throw new Error("collection_type_invalid",{cause:this.context__.meta})}if(this.context__.meta.fv!==_CollectionReference.fileFormatVersion){this.logger__.incident?.("validateContext__","store_file_version_incompatible",{old:this.context__.meta.fv,new:_CollectionReference.fileFormatVersion});this.migrateContext__()}}migrateContext__(){if(this.context__.meta.fv===_CollectionReference.fileFormatVersion)return;this.logger__.logMethod?.("migrateContext__");if(this.context__.meta.fv>_CollectionReference.fileFormatVersion){this.logger__.accident("migrateContext__","store_version_incompatible",this.context__.meta);throw new Error("store_version_incompatible",{cause:this.context__.meta})}if(this.context__.meta.fv===1){this.context__.meta.fv=2}if(this.context__.meta.fv===2){if(this.context__.meta.schemaVer===void 0||this.context__.meta.schemaVer===0){this.context__.meta.schemaVer=1}delete this.context__.meta["ver"];this.context__.meta.extra??={};this.context__.meta.fv=3}this.updated__()}get schemaVer(){return this.context__.meta.schemaVer??1}set schemaVer(ver){this.logger__.logMethodArgs?.("set schemaVer",{old:this.context__.meta.schemaVer,new:ver});this.context__.meta.schemaVer=ver;this.updated__()}get freeze(){return this._freeze}set freeze(value){this.logger__.logMethodArgs?.("freeze changed",{value});this._freeze=value}hasItem(itemId){const exists=Object.hasOwn(this.context__.data,itemId);this.logger__.logMethodFull?.("hasItem",itemId,exists);return exists}getStoreMeta(){this.logger__.logMethod?.("getStoreMeta");return this.context__.meta}item__(itemId){const item=this.context__.data[itemId];if(item===void 0){this.logger__.accident("item__","collection_item_not_found",{itemId});throw new Error("collection_item_not_found",{cause:{itemId}})}return item}getItemMeta(itemId){const meta=this.item__(itemId).meta;this.logger__.logMethodFull?.("getItemMeta",itemId,meta);return meta}getItemData(itemId){this.logger__.logMethodArgs?.("getItemData",itemId);return this.item__(itemId).data}getItemContext_(itemId){this.logger__.logMethodArgs?.("getItemContext_",itemId);return this.context__.data[itemId]}addItem(itemId,data){this.logger__.logMethodArgs?.("addItem",{itemId,data});if(this.hasItem(itemId)){this.logger__.accident("addItem","collection_item_exist",{itemId});throw new Error("collection_item_exist",{cause:{itemId}})}const now=Date.now();this.context__.data[itemId]={meta:{id:itemId,rev:0,created:now,updated:now},data};this.updated__(itemId)}appendItem(data){this.logger__.logMethodArgs?.("appendItem",data);const id=this.nextAutoIncrementId__();this.addItem(id,data);return id}removeItem(itemId){this.logger__.logMethodArgs?.("removeItem",itemId);delete this.context__.data[itemId];this.updated__()}replaceItemData(itemId,data){this.logger__.logMethodArgs?.("replaceItemData",{itemId,data});this.item__(itemId).data=data;this.updated__(itemId)}mergeItemData(itemId,data){this.logger__.logMethodArgs?.("mergeItemData",{itemId,data});Object.assign(this.item__(itemId).data,data);this.updated__(itemId)}save(itemId){this.logger__.logMethod?.("save");this.updated__(itemId,false)}saveImmediate(itemId){this.logger__.logMethod?.("saveImmediate");this.updated__(itemId,true)}keys(){this.logger__.logMethod?.("keys");return Object.keys(this.context__.data)}values(){this.logger__.logMethod?.("values");return Object.values(this.context__.data)}*ids(){this.logger__.logMethod?.("ids");for(const id in this.context__.data){yield id}}*items(){this.logger__.logMethod?.("items");for(const id in this.context__.data){yield this.context__.data[id]}}getFullContext_(){this.logger__.logMethod?.("getFullContext_");return this.context__}async updated__(itemId=null,immediate=false){this.logger__.logMethodArgs?.("updated__",{id:itemId,immediate,delayed:this.updateDelayed_});this.hasUnprocessedChanges_=true;if(itemId!==null)this.refreshMeta_(itemId);if(immediate===false&&this.updateDelayed_===true)return;this.updateDelayed_=true;if(immediate===true||this.context__.meta.changeDebounce===void 0){await import_delay.delay.nextMacrotask()}else{await import_delay.delay.by(this.context__.meta.changeDebounce)}if(this.updateDelayed_!==true)return;this.updateDelayed_=false;if(itemId===null)this.refreshMeta_(itemId);if(this._freeze===true)return;this.updatedCallback__(this)}refreshMeta_(itemId){this.logger__.logMethodArgs?.("refreshMeta_",{id:itemId});const now=Date.now();this.context__.meta.rev++;this.context__.meta.updated=now;if(itemId!==null){const itemMeta=this.item__(itemId).meta;itemMeta.rev++;itemMeta.updated=now}}nextAutoIncrementId__(){this.logger__.logMethod?.("nextAutoIncrementId__");const meta=this.context__.meta;do{meta.lastAutoId++}while(meta.lastAutoId in this.context__.data);return meta.lastAutoId}getExtraMeta(){this.logger__.logMethod?.("getExtraMeta");return this.context__.meta.extra}replaceExtraMeta(extraMeta){this.logger__.logMethodArgs?.("replaceExtraMeta",extraMeta);this.context__.meta.extra=extraMeta;this.updated__()}mergeExtraMeta(extraMeta){this.logger__.logMethodArgs?.("mergeExtraMeta",extraMeta);Object.assign(this.context__.meta.extra,extraMeta);this.updated__()}};var import_delay2=require("@alwatr/delay");var import_logger4=require("@alwatr/logger");var import_nitrobase_helper2=require("@alwatr/nitrobase-helper");var import_nitrobase_types2=require("@alwatr/nitrobase-types");__dev_mode__:logger.logFileModule?.("document-reference");var DocumentReference=class _DocumentReference{constructor(context__,updatedCallback__,debugDomain){this.context__=context__;this.updatedCallback__=updatedCallback__;this.hasUnprocessedChanges_=false;this._freeze=false;this.updateDelayed_=false;this.id=(0,import_nitrobase_helper2.getStoreId)(this.context__.meta);this.path=(0,import_nitrobase_helper2.getStorePath)(this.context__.meta);debugDomain??=this.id.slice(0,20);this.logger__=(0,import_logger4.createLogger)(`doc:${debugDomain}`);this.logger__.logMethodArgs?.("new",{path:this.path});this.validateContext__()}static{this.version="7.10.1"}static{this.fileFormatVersion=3}static newRefFromData(statId,data,updatedCallback,debugDomain){logger.logMethodArgs?.("doc.newRefFromData",statId);const now=Date.now();const initialContext={ok:true,meta:{...statId,rev:1,updated:now,created:now,type:import_nitrobase_types2.StoreFileType.Document,extension:import_nitrobase_types2.StoreFileExtension.Json,fv:_DocumentReference.fileFormatVersion,extra:{}},data};return new _DocumentReference(initialContext,updatedCallback,debugDomain)}static newRefFromContext(context,updatedCallback,debugDomain){logger.logMethodArgs?.("doc.newRefFromContext",context.meta);return new _DocumentReference(context,updatedCallback,debugDomain)}validateContext__(){this.logger__.logMethod?.("validateContext__");if(this.context__.ok!==true){this.logger__.accident?.("validateContext__","store_not_ok");throw new Error("store_not_ok",{cause:{context:this.context__}})}if(this.context__.meta===void 0){this.logger__.accident?.("validateContext__","store_meta_undefined");throw new Error("store_meta_undefined",{cause:{context:this.context__}})}if(this.context__.meta.type!==import_nitrobase_types2.StoreFileType.Document){this.logger__.accident?.("validateContext__","document_type_invalid",this.context__.meta);throw new Error("document_type_invalid",{cause:this.context__.meta})}if(this.context__.meta.fv!==_DocumentReference.fileFormatVersion){this.logger__.incident?.("validateContext__","store_file_version_incompatible",{old:this.context__.meta.fv,new:_DocumentReference.fileFormatVersion});this.migrateContext__()}}migrateContext__(){if(this.context__.meta.fv===_DocumentReference.fileFormatVersion)return;this.logger__.logMethod?.("migrateContext__");if(this.context__.meta.fv>_DocumentReference.fileFormatVersion){this.logger__.accident("migrateContext__","store_version_incompatible",this.context__.meta);throw new Error("store_version_incompatible",{cause:this.context__.meta})}if(this.context__.meta.fv===1){this.context__.meta.fv=2}if(this.context__.meta.fv===2){if(this.context__.meta.schemaVer===void 0||this.context__.meta.schemaVer===0){this.context__.meta.schemaVer=1}delete this.context__.meta["ver"];this.context__.meta.extra??={};this.context__.meta.fv=3}this.updated__()}get schemaVer(){return this.context__.meta.schemaVer??1}set schemaVer(ver){this.logger__.logMethodArgs?.("set schemaVer",{old:this.context__.meta.schemaVer,new:ver});this.context__.meta.schemaVer=ver;this.updated__()}get freeze(){return this._freeze}set freeze(value){this.logger__.logMethodArgs?.("freeze changed",{value});this._freeze=value}getData(){this.logger__.logMethod?.("getData");return this.context__.data}getStoreMeta(){this.logger__.logMethod?.("getStoreMeta");return this.context__.meta}replaceData(data){this.logger__.logMethodArgs?.("replaceData",data);this.context__.data=data;this.updated__()}mergeData(data){this.logger__.logMethodArgs?.("mergeData",data);Object.assign(this.context__.data,data);this.updated__()}save(){this.logger__.logMethod?.("save");this.updated__()}saveImmediate(){this.logger__.logMethod?.("saveImmediate");this.updated__(true)}getFullContext_(){this.logger__.logMethod?.("getFullContext_");return this.context__}async updated__(immediate=false){this.logger__.logMethodArgs?.("updated__",{immediate,delayed:this.updateDelayed_});this.hasUnprocessedChanges_=true;if(immediate!==true&&this.updateDelayed_===true)return;this.updateDelayed_=true;if(immediate===true||this.context__.meta.changeDebounce===void 0){await import_delay2.delay.nextMacrotask()}else{await import_delay2.delay.by(this.context__.meta.changeDebounce)}if(this.updateDelayed_!==true)return;this.updateDelayed_=false;this.refreshMetadata_();if(this._freeze===true)return;this.updatedCallback__(this)}refreshMetadata_(){this.logger__.logMethod?.("refreshMetadata_");this.context__.meta.updated=Date.now();this.context__.meta.rev++}getExtraMeta(){this.logger__.logMethod?.("getExtraMeta");return this.context__.meta.extra}replaceExtraMeta(extraMeta){this.logger__.logMethodArgs?.("replaceExtraMeta",extraMeta);this.context__.meta.extra=extraMeta;this.updated__()}mergeExtraMeta(extraMeta){this.logger__.logMethodArgs?.("mergeExtraMeta",extraMeta);Object.assign(this.context__.meta.extra,extraMeta);this.updated__()}};0&&(module.exports={CollectionReference,DocumentReference});
|
|
969
4
|
//# sourceMappingURL=main.cjs.map
|