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