@alwatr/nitrobase-engine 7.2.0 → 7.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/README.md +4 -4
- package/dist/alwatr-nitrobase.d.ts +0 -1
- package/dist/alwatr-nitrobase.d.ts.map +1 -1
- package/dist/logger.d.ts.map +1 -1
- package/dist/main.cjs +446 -2
- package/dist/main.cjs.map +4 -4
- package/dist/main.mjs +424 -2
- package/dist/main.mjs.map +4 -4
- package/package.json +13 -17
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,33 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [7.3.0](https://github.com/Alwatr/nitrobase/compare/v7.2.1...v7.3.0) (2024-10-11)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* update `import`s & packages based on the latest changes of `nanolib` & prevent side-effects ([0d66f89](https://github.com/Alwatr/nitrobase/commit/0d66f894dc4ff615ab73ebd27c275f98dc384fbe)) by @mohammadhonarvar
|
|
11
|
+
|
|
12
|
+
### Code Refactoring
|
|
13
|
+
|
|
14
|
+
* update `import`s & packages based on the latest changes of `nanolib` ([ce990a2](https://github.com/Alwatr/nitrobase/commit/ce990a2fedc5545e971c3bb6e58b55bfba8c0bd9)) by @mohammadhonarvar
|
|
15
|
+
|
|
16
|
+
### Miscellaneous Chores
|
|
17
|
+
|
|
18
|
+
* **deps-dev:** bump the development-dependencies group with 10 updates ([7bdcf3f](https://github.com/Alwatr/nitrobase/commit/7bdcf3f47ddb8e1376a1c7ae6e221811182bae58)) by @dependabot[bot]
|
|
19
|
+
* **deps:** bump the alwatr-dependencies group with 10 updates ([60afdde](https://github.com/Alwatr/nitrobase/commit/60afdde98562f3023e6624ee0579d6bdee80bc32)) by @dependabot[bot]
|
|
20
|
+
* **deps:** bump the alwatr-dependencies group with 6 updates ([1cf29b2](https://github.com/Alwatr/nitrobase/commit/1cf29b20055554945cd669ea9d022ab7c503d9fc)) by @dependabot[bot]
|
|
21
|
+
|
|
22
|
+
## [7.2.1](https://github.com/Alwatr/nitrobase/compare/v7.2.0...v7.2.1) (2024-09-29)
|
|
23
|
+
|
|
24
|
+
### Miscellaneous Chores
|
|
25
|
+
|
|
26
|
+
* **deps-dev:** bump @types/node in the development-dependencies group ([9b146d2](https://github.com/Alwatr/nitrobase/commit/9b146d2f6cf7d1d79a2a6f46a5e8f50e7fb2ac75)) by @dependabot[bot]
|
|
27
|
+
* **engine:** change the license to AGPL-3.0 ([f9d89d5](https://github.com/Alwatr/nitrobase/commit/f9d89d5de592b4df397c9b15156166aed90fa8d1)) by @ArmanAsadian
|
|
28
|
+
|
|
29
|
+
### Dependencies update
|
|
30
|
+
|
|
31
|
+
* update ([90a0fe1](https://github.com/Alwatr/nitrobase/commit/90a0fe146eb703c183c116776d7c5748918282da)) by @
|
|
32
|
+
|
|
6
33
|
## [7.2.0](https://github.com/Alwatr/nitrobase/compare/v7.1.1...v7.2.0) (2024-09-24)
|
|
7
34
|
|
|
8
35
|
### Code Refactoring
|
package/README.md
CHANGED
|
@@ -80,10 +80,10 @@ The following companies, organizations, and individuals support Nitrobase ongoin
|
|
|
80
80
|
|
|
81
81
|
[](https://exirstudio.com)
|
|
82
82
|
|
|
83
|
-
|
|
83
|
+
### Contributing
|
|
84
84
|
|
|
85
|
-
|
|
85
|
+
Contributions are welcome! Please read our [contribution guidelines](https://github.com/Alwatr/.github/blob/next/CONTRIBUTING.md) before submitting a pull request.
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
### License
|
|
88
88
|
|
|
89
|
-
|
|
89
|
+
This project is licensed under the [AGPL-3.0 License](LICENSE).
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CollectionReference, DocumentReference } from '@alwatr/nitrobase-reference';
|
|
2
2
|
import { type StoreFileStat, type CollectionContext, type DocumentContext, type StoreFileId, type CollectionItem } from '@alwatr/nitrobase-types';
|
|
3
|
-
import type { JsonObject } from '@alwatr/type-helper';
|
|
4
3
|
/**
|
|
5
4
|
* AlwatrNitrobase configuration.
|
|
6
5
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"alwatr-nitrobase.d.ts","sourceRoot":"","sources":["../src/alwatr-nitrobase.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"alwatr-nitrobase.d.ts","sourceRoot":"","sources":["../src/alwatr-nitrobase.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,mBAAmB,EAAE,iBAAiB,EAAC,MAAM,6BAA6B,CAAC;AACnF,OAAO,EAIL,KAAK,aAAa,EAElB,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,KAAK,cAAc,EACpB,MAAM,yBAAyB,CAAC;AAMjC;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;;;OAIG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAE/B;;;OAGG;IACH,uBAAuB,CAAC,EAAE,OAAO,CAAC;CACnC;AAED;;;;;GAKG;AACH,qBAAa,eAAe;IA0Cd,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAzCrC;;;;OAIG;IACH,MAAM,CAAC,QAAQ,CAAC,OAAO,SAAuB;IAE9C;;OAEG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAMlC;IAEF;;;OAGG;IACH,OAAO,CAAC,QAAQ,CAAC;IAEjB;;OAEG;IACH,OAAO,CAAC,iBAAiB,CAA8D;IAEvF;;;;;;;;;;;OAWG;gBAC0B,QAAQ,EAAE,qBAAqB;IAO5D;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO;IAOvC;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,WAAW,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU,EAC3C,IAAI,EAAE,IAAI,CAAC,aAAa,EAAE,MAAM,CAAC,EACjC,WAAW,GAAE,eAAe,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,IAAW,GACpD,IAAI;IAWP;;;;;;;;;;;;;;;;;OAiBG;IACH,aAAa,CAAC,KAAK,SAAS,UAAU,GAAG,UAAU,EACjD,IAAI,EAAE,IAAI,CAAC,aAAa,EAAE,MAAM,CAAC,EACjC,WAAW,GAAE,iBAAiB,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,GAAG,IAAW,GAC1D,IAAI;IAWP;;;;;;OAMG;IACH,aAAa,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU,EAC7C,IAAI,EAAE,aAAa,EACnB,WAAW,GAAE,eAAe,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,iBAAiB,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,IAAW,GACnF,IAAI;IA6BP;;;;;;;;;;;;;;;;OAgBG;IACG,YAAY,CAAC,IAAI,SAAS,UAAU,EAAE,UAAU,EAAE,WAAW,GAAG,OAAO,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC;IA+BtG;;;;;;;;;;;;;;;;OAgBG;IACG,cAAc,CAAC,KAAK,SAAS,UAAU,EAAE,YAAY,EAAE,WAAW,GAAG,OAAO,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC;IAiC9G;;;;;;;;;OASG;IACH,WAAW,CAAC,OAAO,EAAE,WAAW,GAAG,IAAI;IAYvC;;;;;;;;;;;;;OAaG;IACG,WAAW,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IA0BtD;;;;;;;;OAQG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAU9B;;;;;OAKG;YACW,aAAa;IAS3B;;;;;;;;OAQG;IACH,OAAO,CAAC,cAAc;IAMtB;;;;;OAKG;cACa,aAAa,CAAC,CAAC,SAAS,UAAU,EAAE,IAAI,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,mBAAmB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAevH;;OAEG;IACH,OAAO,CAAC,YAAY;IAgBpB;;OAEG;IACH,OAAO,CAAC,UAAU;IAYlB;;;;;;;;;;OAUG;IACH,YAAY,IAAI,cAAc,CAAC,IAAI,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC,EAAE;CAInE"}
|
package/dist/logger.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,MAAM,uCAAiD,CAAC"}
|
package/dist/main.cjs
CHANGED
|
@@ -1,3 +1,447 @@
|
|
|
1
|
-
/* @alwatr/nitrobase-engine v7.
|
|
2
|
-
"use strict";
|
|
1
|
+
/* @alwatr/nitrobase-engine v7.3.0 */
|
|
2
|
+
"use strict";
|
|
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
|
+
AlwatrNitrobase: () => AlwatrNitrobase
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(main_exports);
|
|
27
|
+
|
|
28
|
+
// src/alwatr-nitrobase.ts
|
|
29
|
+
var import_nanolib2 = require("@alwatr/nanolib");
|
|
30
|
+
var import_exit_hook = require("@alwatr/nanolib/exit-hook");
|
|
31
|
+
var import_node_fs = require("@alwatr/nanolib/node-fs");
|
|
32
|
+
var import_nitrobase_helper = require("@alwatr/nitrobase-helper");
|
|
33
|
+
var import_nitrobase_reference = require("@alwatr/nitrobase-reference");
|
|
34
|
+
var import_nitrobase_types = require("@alwatr/nitrobase-types");
|
|
35
|
+
|
|
36
|
+
// src/logger.ts
|
|
37
|
+
var import_nanolib = require("@alwatr/nanolib");
|
|
38
|
+
__dev_mode__: import_nanolib.packageTracer.add("@alwatr/nitrobase-engine", "7.3.0");
|
|
39
|
+
var logger = /* @__PURE__ */ (0, import_nanolib.createLogger)("@alwatr/nitrobase-engine");
|
|
40
|
+
|
|
41
|
+
// src/alwatr-nitrobase.ts
|
|
42
|
+
__dev_mode__: logger.logFileModule?.("alwatr-nitrobase");
|
|
43
|
+
var _AlwatrNitrobase = class _AlwatrNitrobase {
|
|
44
|
+
/**
|
|
45
|
+
* Constructs an AlwatrNitrobase instance with the provided configuration.
|
|
46
|
+
*
|
|
47
|
+
* @param config__ The configuration of the AlwatrNitrobase engine.
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const alwatrStore = new AlwatrNitrobase({
|
|
51
|
+
* rootPath: './db',
|
|
52
|
+
* saveDebounce: 40,
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
constructor(config__) {
|
|
57
|
+
this.config__ = config__;
|
|
58
|
+
/**
|
|
59
|
+
* Keep all loaded nitrobase file context loaded in memory.
|
|
60
|
+
*/
|
|
61
|
+
this.cacheReferences__ = {};
|
|
62
|
+
var _a;
|
|
63
|
+
logger.logMethodArgs?.("new", config__);
|
|
64
|
+
(_a = this.config__).defaultChangeDebounce ?? (_a.defaultChangeDebounce = 40);
|
|
65
|
+
this.rootDb__ = this.loadRootDb__();
|
|
66
|
+
(0, import_exit_hook.exitHook)(this.exitHook__.bind(this));
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Checks if a nitrobase file with the given ID exists.
|
|
70
|
+
*
|
|
71
|
+
* @param storeId - The ID of the nitrobase file to check.
|
|
72
|
+
* @returns `true` if the nitrobase file exists, `false` otherwise.
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* if (!alwatrStore.hasStore('user1/profile')) {
|
|
76
|
+
* alwatrStore.defineDocument(...)
|
|
77
|
+
* }
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
hasStore(storeId) {
|
|
81
|
+
const id_ = (0, import_nitrobase_helper.getStoreId)(storeId);
|
|
82
|
+
const exists = this.rootDb__.hasItem(id_);
|
|
83
|
+
logger.logMethodFull?.("hasStore", id_, exists);
|
|
84
|
+
return exists;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Defines a new document with the given configuration and initial data.
|
|
88
|
+
* If a document with the same ID already exists, an error is thrown.
|
|
89
|
+
*
|
|
90
|
+
* @param stat nitrobase file stat
|
|
91
|
+
* @param initialData initial data for the document
|
|
92
|
+
* @template TDoc document data type
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* await alwatrStore.newDocument<Order>(
|
|
96
|
+
* {
|
|
97
|
+
* name: 'profile',
|
|
98
|
+
* region: Region.PerUser,
|
|
99
|
+
* ownerId: 'user1',
|
|
100
|
+
* },
|
|
101
|
+
* {
|
|
102
|
+
* name: 'Ali',
|
|
103
|
+
* email: 'ali@alwatr.io',
|
|
104
|
+
* }
|
|
105
|
+
* );
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
newDocument(stat, initialData = null) {
|
|
109
|
+
logger.logMethodArgs?.("newDocument", stat);
|
|
110
|
+
return this.newStoreFile_(
|
|
111
|
+
{
|
|
112
|
+
...stat,
|
|
113
|
+
type: import_nitrobase_types.StoreFileType.Document
|
|
114
|
+
},
|
|
115
|
+
initialData
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Defines a new collection with the given configuration and initial data.
|
|
120
|
+
* If a collection with the same ID already exists, an error is thrown.
|
|
121
|
+
*
|
|
122
|
+
* @param stat nitrobase file stat
|
|
123
|
+
* @param initialData initial data for the collection
|
|
124
|
+
* @template TItem collection item data type
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* await alwatrStore.newCollection<Order>(
|
|
128
|
+
* {
|
|
129
|
+
* name: 'orders',
|
|
130
|
+
* region: Region.PerUser,
|
|
131
|
+
* ownerId: 'user1',
|
|
132
|
+
* }
|
|
133
|
+
* );
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
newCollection(stat, initialData = null) {
|
|
137
|
+
logger.logMethodArgs?.("newCollection", stat);
|
|
138
|
+
return this.newStoreFile_(
|
|
139
|
+
{
|
|
140
|
+
...stat,
|
|
141
|
+
type: import_nitrobase_types.StoreFileType.Collection
|
|
142
|
+
},
|
|
143
|
+
initialData
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Defines a AlwatrNitrobaseFile with the given configuration and initial data.
|
|
148
|
+
*
|
|
149
|
+
* @param stat nitrobase file stat
|
|
150
|
+
* @param initialData initial data for the document
|
|
151
|
+
* @template TDoc document data type
|
|
152
|
+
*/
|
|
153
|
+
newStoreFile_(stat, initialData = null) {
|
|
154
|
+
logger.logMethodArgs?.("newStoreFile_", stat);
|
|
155
|
+
stat.changeDebounce ?? (stat.changeDebounce = this.config__.defaultChangeDebounce);
|
|
156
|
+
let fileStoreRef;
|
|
157
|
+
if (stat.type === import_nitrobase_types.StoreFileType.Document) {
|
|
158
|
+
fileStoreRef = import_nitrobase_reference.DocumentReference.newRefFromData(stat, initialData, this.storeChanged_.bind(this));
|
|
159
|
+
} else if (stat.type === import_nitrobase_types.StoreFileType.Collection) {
|
|
160
|
+
fileStoreRef = import_nitrobase_reference.CollectionReference.newRefFromData(stat, initialData, this.storeChanged_.bind(this));
|
|
161
|
+
} else {
|
|
162
|
+
logger.accident("newStoreFile_", "store_file_type_not_supported", stat);
|
|
163
|
+
throw new Error("store_file_type_not_supported", { cause: stat });
|
|
164
|
+
}
|
|
165
|
+
if (this.rootDb__.hasItem(fileStoreRef.id)) {
|
|
166
|
+
logger.accident("newStoreFile_", "store_file_already_defined", stat);
|
|
167
|
+
throw new Error("store_file_already_defined", { cause: stat });
|
|
168
|
+
}
|
|
169
|
+
this.rootDb__.addItem(fileStoreRef.id, stat);
|
|
170
|
+
this.cacheReferences__[fileStoreRef.id] = fileStoreRef;
|
|
171
|
+
this.storeChanged_(fileStoreRef);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Open a document with the given id and create and return a DocumentReference.
|
|
175
|
+
* If the document not exists or its not a document, an error is thrown.
|
|
176
|
+
*
|
|
177
|
+
* @template TDoc document data type
|
|
178
|
+
* @param documentId document id {@link StoreFileId}
|
|
179
|
+
* @returns document reference {@link DocumentReference}
|
|
180
|
+
* @example
|
|
181
|
+
* ```typescript
|
|
182
|
+
* const userProfile = await alwatrStore.openDocument<User>({
|
|
183
|
+
* name: 'user1/profile',
|
|
184
|
+
* region: Region.PerUser,
|
|
185
|
+
* ownerId: 'user1',
|
|
186
|
+
* });
|
|
187
|
+
* userProfile.update({name: 'ali'});
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
async openDocument(documentId) {
|
|
191
|
+
const id = (0, import_nitrobase_helper.getStoreId)(documentId);
|
|
192
|
+
logger.logMethodArgs?.("openDocument", id);
|
|
193
|
+
if (Object.hasOwn(this.cacheReferences__, id)) {
|
|
194
|
+
const ref = this.cacheReferences__[id];
|
|
195
|
+
if (!(ref instanceof import_nitrobase_reference.DocumentReference)) {
|
|
196
|
+
logger.accident("openDocument", "document_wrong_type", id);
|
|
197
|
+
throw new Error("document_wrong_type", { cause: id });
|
|
198
|
+
}
|
|
199
|
+
return this.cacheReferences__[id];
|
|
200
|
+
}
|
|
201
|
+
if (!this.rootDb__.hasItem(id)) {
|
|
202
|
+
logger.accident("openDocument", "document_not_found", id);
|
|
203
|
+
throw new Error("document_not_found", { cause: id });
|
|
204
|
+
}
|
|
205
|
+
const storeStat = this.rootDb__.getItemData(id);
|
|
206
|
+
if (storeStat.type != import_nitrobase_types.StoreFileType.Document) {
|
|
207
|
+
logger.accident("openDocument", "document_wrong_type", id);
|
|
208
|
+
throw new Error("document_wrong_type", { cause: id });
|
|
209
|
+
}
|
|
210
|
+
const context = await this.readContext__(storeStat);
|
|
211
|
+
const docRef = import_nitrobase_reference.DocumentReference.newRefFromContext(context, this.storeChanged_.bind(this));
|
|
212
|
+
this.cacheReferences__[id] = docRef;
|
|
213
|
+
return docRef;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Open a collection with the given id and create and return a CollectionReference.
|
|
217
|
+
* If the collection not exists or its not a collection, an error is thrown.
|
|
218
|
+
*
|
|
219
|
+
* @template TItem collection item data type
|
|
220
|
+
* @param collectionId collection id {@link StoreFileId}
|
|
221
|
+
* @returns collection reference {@link CollectionReference}
|
|
222
|
+
* @example
|
|
223
|
+
* ```typescript
|
|
224
|
+
* const orders = await alwatrStore.openCollection<Order>({
|
|
225
|
+
* name: 'orders',
|
|
226
|
+
* region: Region.PerUser,
|
|
227
|
+
* ownerId: 'user1',
|
|
228
|
+
* });
|
|
229
|
+
* orders.append({name: 'order 1'});
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
async openCollection(collectionId) {
|
|
233
|
+
const id = (0, import_nitrobase_helper.getStoreId)(collectionId);
|
|
234
|
+
logger.logMethodArgs?.("openCollection", id);
|
|
235
|
+
if (Object.hasOwn(this.cacheReferences__, id)) {
|
|
236
|
+
const ref = this.cacheReferences__[id];
|
|
237
|
+
if (!(ref instanceof import_nitrobase_reference.CollectionReference)) {
|
|
238
|
+
logger.accident("openCollection", "collection_wrong_type", id);
|
|
239
|
+
throw new Error("collection_wrong_type", { cause: id });
|
|
240
|
+
}
|
|
241
|
+
return this.cacheReferences__[id];
|
|
242
|
+
}
|
|
243
|
+
if (!this.rootDb__.hasItem(id)) {
|
|
244
|
+
logger.accident("openCollection", "collection_not_found", id);
|
|
245
|
+
throw new Error("collection_not_found", { cause: id });
|
|
246
|
+
}
|
|
247
|
+
const storeStat = this.rootDb__.getItemData(id);
|
|
248
|
+
if (storeStat.type != import_nitrobase_types.StoreFileType.Collection) {
|
|
249
|
+
logger.accident("openCollection", "collection_wrong_type", id);
|
|
250
|
+
throw new Error("collection_not_found", { cause: id });
|
|
251
|
+
}
|
|
252
|
+
const context = await this.readContext__(storeStat);
|
|
253
|
+
const colRef = import_nitrobase_reference.CollectionReference.newRefFromContext(context, this.storeChanged_.bind(this));
|
|
254
|
+
this.cacheReferences__[id] = colRef;
|
|
255
|
+
return colRef;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Unloads the nitrobase file with the given id from memory.
|
|
259
|
+
*
|
|
260
|
+
* @param storeId The unique identifier of the nitrobase file. {@link StoreFileId}
|
|
261
|
+
* @example
|
|
262
|
+
* ```typescript
|
|
263
|
+
* alwatrStore.unloadStore({name: 'user-list', region: Region.Secret});
|
|
264
|
+
* alwatrStore.hasStore({name: 'user-list', region: Region.Secret}); // true
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
unloadStore(storeId) {
|
|
268
|
+
const id_ = (0, import_nitrobase_helper.getStoreId)(storeId);
|
|
269
|
+
logger.logMethodArgs?.("unloadStore", id_);
|
|
270
|
+
const ref = this.cacheReferences__[id_];
|
|
271
|
+
if (ref === void 0) return;
|
|
272
|
+
if (ref.hasUnprocessedChanges_ === true) {
|
|
273
|
+
ref.updateDelayed_ = false;
|
|
274
|
+
this.storeChanged_(ref);
|
|
275
|
+
}
|
|
276
|
+
delete this.cacheReferences__[id_];
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Remove document or collection from nitrobase and delete the file from disk.
|
|
280
|
+
* If the file is not found, an error is thrown.
|
|
281
|
+
* If the file is not unloaded, it will be unloaded first.
|
|
282
|
+
* You don't need to await this method to complete unless you want to make sure the file is deleted on disk.
|
|
283
|
+
*
|
|
284
|
+
* @param storeId The ID of the file to delete. {@link StoreFileId}
|
|
285
|
+
* @returns A Promise that resolves when the file is deleted.
|
|
286
|
+
* @example
|
|
287
|
+
* ```typescript
|
|
288
|
+
* alwatrStore.removeStore({name: 'user-list', region: Region.Secret});
|
|
289
|
+
* alwatrStore.hasStore({name: 'user-list', region: Region.Secret}); // false
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
async removeStore(storeId) {
|
|
293
|
+
const id_ = (0, import_nitrobase_helper.getStoreId)(storeId);
|
|
294
|
+
logger.logMethodArgs?.("removeStore", id_);
|
|
295
|
+
if (!this.rootDb__.hasItem(id_)) {
|
|
296
|
+
logger.accident("removeStore", "document_not_found", id_);
|
|
297
|
+
throw new Error("document_not_found", { cause: id_ });
|
|
298
|
+
}
|
|
299
|
+
const ref = this.cacheReferences__[id_];
|
|
300
|
+
if (ref !== void 0) {
|
|
301
|
+
ref.freeze = true;
|
|
302
|
+
ref.updateDelayed_ = false;
|
|
303
|
+
ref.hasUnprocessedChanges_ = false;
|
|
304
|
+
delete this.cacheReferences__[id_];
|
|
305
|
+
}
|
|
306
|
+
const path = (0, import_nitrobase_helper.getStorePath)(this.rootDb__.getItemData(id_));
|
|
307
|
+
this.rootDb__.removeItem(id_);
|
|
308
|
+
await import_nanolib2.delay.by(0);
|
|
309
|
+
try {
|
|
310
|
+
await (0, import_node_fs.unlink)((0, import_node_fs.resolve)(this.config__.rootPath, path));
|
|
311
|
+
} catch (error) {
|
|
312
|
+
logger.error("removeStore", "remove_file_failed", error, { id: storeId, path });
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Saves all changes in the nitrobase.
|
|
317
|
+
*
|
|
318
|
+
* @returns A Promise that resolves when all changes are saved.
|
|
319
|
+
* @example
|
|
320
|
+
* ```typescript
|
|
321
|
+
* await alwatrStore.saveAll();
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
324
|
+
async saveAll() {
|
|
325
|
+
logger.logMethod?.("saveAll");
|
|
326
|
+
for (const ref of Object.values(this.cacheReferences__)) {
|
|
327
|
+
if (ref.hasUnprocessedChanges_ === true && ref.freeze !== true) {
|
|
328
|
+
ref.updateDelayed_ = false;
|
|
329
|
+
await this.storeChanged_(ref);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Reads the context from a given path or StoreFileStat object.
|
|
335
|
+
*
|
|
336
|
+
* @param path The path or StoreFileStat object from which to read the context.
|
|
337
|
+
* @returns A promise that resolves to the context object.
|
|
338
|
+
*/
|
|
339
|
+
async readContext__(path) {
|
|
340
|
+
if (typeof path !== "string") path = (0, import_nitrobase_helper.getStorePath)(path);
|
|
341
|
+
logger.logMethodArgs?.("readContext__", path);
|
|
342
|
+
logger.time?.(`readContext__time(${path})`);
|
|
343
|
+
const context = await (0, import_node_fs.readJson)((0, import_node_fs.resolve)(this.config__.rootPath, path));
|
|
344
|
+
logger.timeEnd?.(`readContext__time(${path})`);
|
|
345
|
+
return context;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Writes the context to the specified path.
|
|
349
|
+
*
|
|
350
|
+
* @template T The type of the context.
|
|
351
|
+
* @param path The path where the context will be written.
|
|
352
|
+
* @param context The context to be written.
|
|
353
|
+
* @param sync Indicates whether the write operation should be synchronous.
|
|
354
|
+
* @returns A promise that resolves when the write operation is complete.
|
|
355
|
+
*/
|
|
356
|
+
writeContext__(path, context) {
|
|
357
|
+
if (typeof path !== "string") path = (0, import_nitrobase_helper.getStorePath)(path);
|
|
358
|
+
logger.logMethodArgs?.("writeContext__", path);
|
|
359
|
+
return (0, import_node_fs.writeJson)((0, import_node_fs.resolve)(this.config__.rootPath, path), context);
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Write nitrobase file context.
|
|
363
|
+
*
|
|
364
|
+
* @param from nitrobase file reference
|
|
365
|
+
* @returns A promise that resolves when the write operation is complete.
|
|
366
|
+
*/
|
|
367
|
+
async storeChanged_(from) {
|
|
368
|
+
logger.logMethodArgs?.("storeChanged__", from.id);
|
|
369
|
+
const rev = from.getStoreMeta().rev;
|
|
370
|
+
try {
|
|
371
|
+
await this.writeContext__(from.path, from.getFullContext_());
|
|
372
|
+
if (rev === from.getStoreMeta().rev) {
|
|
373
|
+
from.hasUnprocessedChanges_ = false;
|
|
374
|
+
}
|
|
375
|
+
} catch (error) {
|
|
376
|
+
logger.error("storeChanged__", "write_context_failed", { id: from.id, error });
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Load storeFilesCollection or create new one.
|
|
381
|
+
*/
|
|
382
|
+
loadRootDb__() {
|
|
383
|
+
logger.logMethod?.("loadRootDb__");
|
|
384
|
+
const fullPath = (0, import_node_fs.resolve)(this.config__.rootPath, (0, import_nitrobase_helper.getStorePath)(_AlwatrNitrobase.rootDbStat__));
|
|
385
|
+
if (!(0, import_node_fs.existsSync)(fullPath)) {
|
|
386
|
+
if (this.config__.errorWhenNotInitialized === true) {
|
|
387
|
+
throw new Error("store_not_found", { cause: "Nitrobase not initialized" });
|
|
388
|
+
}
|
|
389
|
+
logger.banner("Initialize new alwatr-nitrobase");
|
|
390
|
+
return import_nitrobase_reference.CollectionReference.newRefFromData(_AlwatrNitrobase.rootDbStat__, null, this.storeChanged_.bind(this));
|
|
391
|
+
}
|
|
392
|
+
const context = (0, import_node_fs.readJson)(fullPath, true);
|
|
393
|
+
return import_nitrobase_reference.CollectionReference.newRefFromContext(context, this.storeChanged_.bind(this), "root-db");
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Save all nitrobase files.
|
|
397
|
+
*/
|
|
398
|
+
exitHook__() {
|
|
399
|
+
logger.logMethod?.("exitHook__");
|
|
400
|
+
for (const ref of Object.values(this.cacheReferences__)) {
|
|
401
|
+
logger.logProperty?.(`StoreFile.${ref.id}.hasUnprocessedChanges`, ref.hasUnprocessedChanges_);
|
|
402
|
+
if (ref.hasUnprocessedChanges_ === true && ref.freeze !== true) {
|
|
403
|
+
logger.incident?.("exitHook__", "rescue_unsaved_context", { id: ref.id });
|
|
404
|
+
(0, import_node_fs.writeJson)((0, import_node_fs.resolve)(this.config__.rootPath, ref.path), ref.getFullContext_(), true);
|
|
405
|
+
ref.hasUnprocessedChanges_ = false;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Get all nitrobase files.
|
|
411
|
+
*
|
|
412
|
+
* @returns all nitrobase files.
|
|
413
|
+
* @example
|
|
414
|
+
* ```typescript
|
|
415
|
+
* const storeList = alwatrStore.getStoreList();
|
|
416
|
+
* for (const nitrobase of storeList) {
|
|
417
|
+
* console.log(nitrobase.meta.id, nitrobase.data);
|
|
418
|
+
* }
|
|
419
|
+
*/
|
|
420
|
+
getStoreList() {
|
|
421
|
+
logger.logMethod?.("getStoreList");
|
|
422
|
+
return this.rootDb__.values();
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
/**
|
|
426
|
+
* The Alwatr Nitrobase version string.
|
|
427
|
+
*
|
|
428
|
+
* Use for nitrobase file format version for check compatibility.
|
|
429
|
+
*/
|
|
430
|
+
_AlwatrNitrobase.version = "7.3.0";
|
|
431
|
+
/**
|
|
432
|
+
* The root nitrobase file stat.
|
|
433
|
+
*/
|
|
434
|
+
_AlwatrNitrobase.rootDbStat__ = {
|
|
435
|
+
name: ".nitrobase",
|
|
436
|
+
region: import_nitrobase_types.Region.Secret,
|
|
437
|
+
type: import_nitrobase_types.StoreFileType.Collection,
|
|
438
|
+
extension: import_nitrobase_types.StoreFileExtension.Json,
|
|
439
|
+
changeDebounce: 40
|
|
440
|
+
};
|
|
441
|
+
var AlwatrNitrobase = _AlwatrNitrobase;
|
|
442
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
443
|
+
0 && (module.exports = {
|
|
444
|
+
AlwatrNitrobase
|
|
445
|
+
});
|
|
446
|
+
/*! For license information please see main.cjs.LEGAL.txt */
|
|
3
447
|
//# sourceMappingURL=main.cjs.map
|