@alwatr/nitrobase-reference 7.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +268 -0
- package/LICENSE +661 -0
- package/README.md +89 -0
- package/dist/collection-reference.d.ts +384 -0
- package/dist/collection-reference.d.ts.map +1 -0
- package/dist/document-reference.d.ts +226 -0
- package/dist/document-reference.d.ts.map +1 -0
- package/dist/logger.d.ts +2 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/main.cjs +3 -0
- package/dist/main.cjs.map +7 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.mjs +3 -0
- package/dist/main.mjs.map +7 -0
- package/package.json +78 -0
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Nitrobase
|
|
2
|
+
|
|
3
|
+
**Extremely Fast and Compact JSON-Based In-Memory Database with Nginx Integration**
|
|
4
|
+
|
|
5
|
+
Nitrobase is a blazingly fast, lightweight database built on JSON. It stores data entirely in memory for lightning-quick access, while also providing a JSON file backup for persistence. You can easily serve your data over the web using our high-performance accelerated Nginx server.
|
|
6
|
+
|
|
7
|
+
## Key Features
|
|
8
|
+
|
|
9
|
+
* **In-Memory Performance:** All data is stored in RAM, ensuring extremely fast reads and writes.
|
|
10
|
+
* **JSON Simplicity:** Data is stored and managed in a straightforward JSON format.
|
|
11
|
+
* **File Backup:** Automatic JSON file backup ensures data persistence.
|
|
12
|
+
* **Nginx Integration:** Seamlessly serve your data over the web using accelerated Nginx.
|
|
13
|
+
* **Compact Storage:** Efficient storage format minimizes disk space usage.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @alwatr/nitrobase
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Getting Started
|
|
22
|
+
|
|
23
|
+
### Create a Collection
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
import { AlwatrNitrobase, Region } from '@alwatr/nitrobase';
|
|
27
|
+
|
|
28
|
+
const alwatrStore = new AlwatrNitrobase({
|
|
29
|
+
rootPath: './db',
|
|
30
|
+
defaultChangeDebounce: 2_000,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const postsCollectionId = {
|
|
34
|
+
name: 'post',
|
|
35
|
+
region: Region.PerUser,
|
|
36
|
+
ownerId: 'user_123',
|
|
37
|
+
schemaVer: 2,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
alwatrStore.newCollection(postsCollectionId);
|
|
41
|
+
|
|
42
|
+
const postsCollection = await alwatrStore.openCollection(postsCollectionId);
|
|
43
|
+
|
|
44
|
+
postsCollection.addItem('post1', {
|
|
45
|
+
title: 'My First Post',
|
|
46
|
+
content: 'This is the content of my first post.'
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Create a Document
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
import { AlwatrNitrobase, Region } from '@alwatr/nitrobase';
|
|
54
|
+
|
|
55
|
+
const alwatrStore = new AlwatrNitrobase({
|
|
56
|
+
rootPath: './db',
|
|
57
|
+
defaultChangeDebounce: 2_000,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const docId = {
|
|
61
|
+
name: 'posts/my-first-post',
|
|
62
|
+
region: Region.Authenticated,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
alwatrStore.newDocument(docId, {
|
|
66
|
+
title: 'My First Post',
|
|
67
|
+
content: 'This is the content of my first post.'
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const myPost = await alwatrStore.openDocument(docId);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Demo Code
|
|
74
|
+
|
|
75
|
+
Explore the provided demo code (`collection.mjs`, `document.mjs`, `benchmark.mjs`) to see Alwatr Nitrobase in action and gain a deeper understanding of its capabilities.
|
|
76
|
+
|
|
77
|
+
## Sponsors
|
|
78
|
+
|
|
79
|
+
The following companies, organizations, and individuals support Nitrobase ongoing maintenance and development. Become a Sponsor to get your logo on our README and website.
|
|
80
|
+
|
|
81
|
+
[](https://exirstudio.com)
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
This project is licensed under the AGPL-3.0 License.
|
|
86
|
+
|
|
87
|
+
## Contributing
|
|
88
|
+
|
|
89
|
+
Contributions are welcome! Please feel free to submit issues and pull requests.
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import { type StoreFileId, type CollectionContext, type CollectionItem, type CollectionItemMeta, type StoreFileMeta } from '@alwatr/nitrobase-types';
|
|
2
|
+
import type { JsonObject } from '@alwatr/type-helper';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a reference to a collection of the AlwatrNitrobase.
|
|
5
|
+
* Provides methods to interact with the collection, such as retrieving, creating, updating, and deleting items.
|
|
6
|
+
*
|
|
7
|
+
* @template TItem - The data type of the collection items.
|
|
8
|
+
*/
|
|
9
|
+
export declare class CollectionReference<TItem extends JsonObject = JsonObject> {
|
|
10
|
+
private context__;
|
|
11
|
+
private updatedCallback__;
|
|
12
|
+
/**
|
|
13
|
+
* Alwatr nitrobase engine version string.
|
|
14
|
+
*/
|
|
15
|
+
static readonly version: string;
|
|
16
|
+
/**
|
|
17
|
+
* Alwatr nitrobase engine file format version number.
|
|
18
|
+
*/
|
|
19
|
+
static readonly fileFormatVersion = 3;
|
|
20
|
+
/**
|
|
21
|
+
* Creates new CollectionReference instance from stat.
|
|
22
|
+
*
|
|
23
|
+
* @param stat the collection stat.
|
|
24
|
+
* @param initialData the collection data.
|
|
25
|
+
* @param updatedCallback the callback to invoke when the collection changed.
|
|
26
|
+
* @template TItem The collection item data type.
|
|
27
|
+
* @returns A new collection reference class.
|
|
28
|
+
*/
|
|
29
|
+
static newRefFromData<TItem extends JsonObject>(stat: StoreFileId, initialData: CollectionContext<TItem>['data'] | null, updatedCallback: (from: CollectionReference<TItem>) => void, debugDomain?: string): CollectionReference<TItem>;
|
|
30
|
+
/**
|
|
31
|
+
* Creates new CollectionReference instance from CollectionContext.
|
|
32
|
+
*
|
|
33
|
+
* @param context the collection context.
|
|
34
|
+
* @param updatedCallback the callback to invoke when the collection changed.
|
|
35
|
+
* @template TItem The collection item data type.
|
|
36
|
+
* @returns A new collection reference class.
|
|
37
|
+
*/
|
|
38
|
+
static newRefFromContext<TItem extends JsonObject>(context: CollectionContext<TItem>, updatedCallback: (from: CollectionReference<TItem>) => void, debugDomain?: string): CollectionReference<TItem>;
|
|
39
|
+
/**
|
|
40
|
+
* Validates the collection context and try to migrate it to the latest version.
|
|
41
|
+
*/
|
|
42
|
+
private validateContext__;
|
|
43
|
+
/**
|
|
44
|
+
* Migrate the collection context to the latest.
|
|
45
|
+
*/
|
|
46
|
+
private migrateContext__;
|
|
47
|
+
/**
|
|
48
|
+
* The ID of the collection nitrobase file.
|
|
49
|
+
*/
|
|
50
|
+
readonly id: string;
|
|
51
|
+
/**
|
|
52
|
+
* The location path of the collection nitrobase file.
|
|
53
|
+
*/
|
|
54
|
+
readonly path: string;
|
|
55
|
+
/**
|
|
56
|
+
* Indicates whether the collection has unsaved changes.
|
|
57
|
+
*/
|
|
58
|
+
hasUnprocessedChanges_: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Logger instance for this collection.
|
|
61
|
+
*/
|
|
62
|
+
private logger__;
|
|
63
|
+
/**
|
|
64
|
+
* Collection reference have methods to get, set, update and save the Alwatr Nitrobase Collection.
|
|
65
|
+
* This class is dummy in saving and loading the collection from file.
|
|
66
|
+
* It's the responsibility of the Alwatr Nitrobase to save and load the collection.
|
|
67
|
+
*
|
|
68
|
+
* @param context__ Collection's context filled from the Alwatr Nitrobase (parent).
|
|
69
|
+
* @param updatedCallback__ updated callback to invoke when the collection is updated from the Alwatr Nitrobase (parent).
|
|
70
|
+
* @template TItem - Items data type.
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const collectionRef = alwatrStore.col('blog/posts');
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
constructor(context__: CollectionContext<TItem>, updatedCallback__: (from: CollectionReference<TItem>) => void, debugDomain?: string);
|
|
77
|
+
/**
|
|
78
|
+
* Get nitrobase schema version
|
|
79
|
+
*
|
|
80
|
+
* @returns nitrobase schema version
|
|
81
|
+
*/
|
|
82
|
+
get schemaVer(): number;
|
|
83
|
+
/**
|
|
84
|
+
* Set nitrobase schema version for migrate
|
|
85
|
+
*/
|
|
86
|
+
set schemaVer(ver: number);
|
|
87
|
+
/**
|
|
88
|
+
* Indicates whether the collection data is frozen and cannot be saved.
|
|
89
|
+
*/
|
|
90
|
+
private _freeze;
|
|
91
|
+
/**
|
|
92
|
+
* Gets the freeze status of the collection data.
|
|
93
|
+
*
|
|
94
|
+
* @returns `true` if the collection data is frozen, `false` otherwise.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* const isFrozen = collectionRef.freeze;
|
|
99
|
+
* console.log(isFrozen); // Output: false
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
get freeze(): boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Sets the freeze status of the collection data.
|
|
105
|
+
*
|
|
106
|
+
* @param value - The freeze status to set.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* collectionRef.freeze = true;
|
|
111
|
+
* console.log(collectionRef.freeze); // Output: true
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
set freeze(value: boolean);
|
|
115
|
+
/**
|
|
116
|
+
* Checks if an item exists in the collection.
|
|
117
|
+
*
|
|
118
|
+
* @param itemId - The ID of the item.
|
|
119
|
+
* @returns `true` if the item with the given ID exists in the collection, `false` otherwise.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* const doesExist = collectionRef.hasItem('item1');
|
|
124
|
+
*
|
|
125
|
+
* if (doesExist) {
|
|
126
|
+
* collectionRef.addItem('item1', { key: 'value' });
|
|
127
|
+
* }
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
hasItem(itemId: string | number): boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Retrieves the metadata of the nitrobase file.
|
|
133
|
+
*
|
|
134
|
+
* @returns The metadata of the nitrobase file.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const metadata = collectionRef.getStoreMeta();
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
getStoreMeta(): Readonly<StoreFileMeta>;
|
|
142
|
+
/**
|
|
143
|
+
* Retrieves an item from the collection. If the item does not exist, an error is thrown.
|
|
144
|
+
*
|
|
145
|
+
* @param itemId - The ID of the item.
|
|
146
|
+
* @returns The item with the given ID.
|
|
147
|
+
*/
|
|
148
|
+
private item__;
|
|
149
|
+
/**
|
|
150
|
+
* Retrieves an item's metadata from the collection. If the item does not exist, an error is thrown.
|
|
151
|
+
*
|
|
152
|
+
* @param itemId - The ID of the item.
|
|
153
|
+
* @returns The metadata of the item with the given ID.
|
|
154
|
+
* @example
|
|
155
|
+
* ```typescript
|
|
156
|
+
* const itemMeta = collectionRef.getItemMeta('item1');
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
getItemMeta(itemId: string | number): Readonly<CollectionItemMeta>;
|
|
160
|
+
/**
|
|
161
|
+
* Retrieves an item's data from the collection. If the item does not exist, an error is thrown.
|
|
162
|
+
*
|
|
163
|
+
* @param itemId - The ID of the item.
|
|
164
|
+
* @returns The data of the item with the given ID.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```typescript
|
|
168
|
+
* const itemData = collectionRef.getItemData('item1');
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
getItemData(itemId: string | number): TItem;
|
|
172
|
+
/**
|
|
173
|
+
* Direct access to an item.
|
|
174
|
+
* If the item does not exist, `undefined` is returned.
|
|
175
|
+
* **USE WITH CAUTION!**
|
|
176
|
+
*
|
|
177
|
+
* @param itemId - The ID of the item.
|
|
178
|
+
* @returns The data of the item with the given ID or `undefined` if the item does not exist.
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```typescript
|
|
182
|
+
* collectionRef.getItemContext_('item1')?.data.name = 'test2';
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
getItemContext_(itemId: string | number): CollectionItem<TItem> | undefined;
|
|
186
|
+
/**
|
|
187
|
+
* Add a new item to the collection.
|
|
188
|
+
* If an item with the given ID already exists, an error is thrown.
|
|
189
|
+
*
|
|
190
|
+
* @param itemId - The ID of the item to create.
|
|
191
|
+
* @param data - The initial data of the item.
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```typescript
|
|
195
|
+
* collectionRef.addItem('item1', { key: 'value' });
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
addItem(itemId: string | number, data: TItem): void;
|
|
199
|
+
/**
|
|
200
|
+
* Appends the given data to the collection with auto increment ID.
|
|
201
|
+
*
|
|
202
|
+
* @param data - The data to append.
|
|
203
|
+
* @returns The ID of the appended item.
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* const newId = collectionRef.appendItem({ key: 'value' });
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
appendItem(data: TItem): string | number;
|
|
211
|
+
/**
|
|
212
|
+
* Removes an item from the collection.
|
|
213
|
+
*
|
|
214
|
+
* @param itemId - The ID of the item to delete.
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```typescript
|
|
218
|
+
* collectionRef.removeItem('item1');
|
|
219
|
+
* collectionRef.hasItem('item1'); // Output: false
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
removeItem(itemId: string | number): void;
|
|
223
|
+
/**
|
|
224
|
+
* Sets an item's data in the collection. Replaces the item's data with the given data.
|
|
225
|
+
*
|
|
226
|
+
* @param itemId - The ID of the item to set.
|
|
227
|
+
* @param data - The data to set for the item.
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* ```typescript
|
|
231
|
+
* collectionRef.replaceItemData('item1', { a: 1, b: 2, c: 3 });
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
replaceItemData(itemId: string | number, data: TItem): void;
|
|
235
|
+
/**
|
|
236
|
+
* Updates an item in the collection by merging a partial update into the item's data.
|
|
237
|
+
*
|
|
238
|
+
* @param itemId - The ID of the item to update.
|
|
239
|
+
* @param data - The part of data to merge into the item's data.
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```typescript
|
|
243
|
+
* collectionRef.mergeItemData(itemId, partialUpdate);
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
mergeItemData(itemId: string | number, data: Partial<TItem>): void;
|
|
247
|
+
/**
|
|
248
|
+
* Requests the Alwatr Nitrobase to save the collection.
|
|
249
|
+
* Saving may take some time in Alwatr Nitrobase due to the use of throttling.
|
|
250
|
+
*
|
|
251
|
+
* @example
|
|
252
|
+
* ```typescript
|
|
253
|
+
* collectionRef.save();
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
save(): void;
|
|
257
|
+
/**
|
|
258
|
+
* Requests the Alwatr Nitrobase to save the collection immediately.
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* ```typescript
|
|
262
|
+
* collectionRef.saveImmediate();
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
saveImmediate(): void;
|
|
266
|
+
/**
|
|
267
|
+
* Retrieves the IDs of all items in the collection in array.
|
|
268
|
+
* Impact performance if the collection is large, use `ids()` instead.
|
|
269
|
+
*
|
|
270
|
+
* @returns Array of IDs of all items in the collection.
|
|
271
|
+
* @example
|
|
272
|
+
* ```typescript
|
|
273
|
+
* const ids = collectionRef.keys();
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
276
|
+
keys(): string[];
|
|
277
|
+
/**
|
|
278
|
+
* Retrieves all items in the collection in array.
|
|
279
|
+
* Impact performance if the collection is large, use `items()` instead.
|
|
280
|
+
*
|
|
281
|
+
* @returns Array of all items in the collection.
|
|
282
|
+
* @example
|
|
283
|
+
* ```typescript
|
|
284
|
+
* const items = collectionRef.values();
|
|
285
|
+
* console.log('meta: %o', items[0].meta);
|
|
286
|
+
* console.log('data: %o', items[0].data);
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
values(): CollectionItem<TItem>[];
|
|
290
|
+
/**
|
|
291
|
+
* Retrieves the IDs of all items in the collection.
|
|
292
|
+
* Use this method instead of `keys()` if the collection is large.
|
|
293
|
+
* This method is a generator and can be used in `for...of` loops.
|
|
294
|
+
* @returns Generator of IDs of all items in the collection.
|
|
295
|
+
* @example
|
|
296
|
+
* ```typescript
|
|
297
|
+
* for (const id of collectionRef.ids()) {
|
|
298
|
+
* const doc = collectionRef.get(id);
|
|
299
|
+
* }
|
|
300
|
+
* ```
|
|
301
|
+
*/
|
|
302
|
+
ids(): Generator<string, void, void>;
|
|
303
|
+
/**
|
|
304
|
+
* Retrieves all items in the collection.
|
|
305
|
+
* Use this method instead of `values()` if the collection is large.
|
|
306
|
+
* This method is a generator and can be used in `for...of` loops.
|
|
307
|
+
* @returns Generator of all items in the collection.
|
|
308
|
+
* @example
|
|
309
|
+
* ```typescript
|
|
310
|
+
* for (const item of collectionRef.items()) {
|
|
311
|
+
* console.log(item.data);
|
|
312
|
+
* }
|
|
313
|
+
*/
|
|
314
|
+
items(): Generator<CollectionItem<TItem>, void, void>;
|
|
315
|
+
/**
|
|
316
|
+
* Retrieves the full context of the collection.
|
|
317
|
+
*
|
|
318
|
+
* @returns The full context of the collection.
|
|
319
|
+
*
|
|
320
|
+
* @example
|
|
321
|
+
* ```typescript
|
|
322
|
+
* const context = collectionRef.getFullContext_();
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
325
|
+
getFullContext_(): Readonly<CollectionContext<TItem>>;
|
|
326
|
+
updateDelayed_: boolean;
|
|
327
|
+
/**
|
|
328
|
+
* Update the document metadata and invoke the updated callback.
|
|
329
|
+
* This method is throttled to prevent multiple updates in a short time.
|
|
330
|
+
*
|
|
331
|
+
* @param itemId - The ID of the item to update.
|
|
332
|
+
*/
|
|
333
|
+
private updated__;
|
|
334
|
+
/**
|
|
335
|
+
* Refresh/recalculate the collection's metadata timestamp and revision.
|
|
336
|
+
*
|
|
337
|
+
* @param itemId - The ID of the item to update.
|
|
338
|
+
*/
|
|
339
|
+
protected refreshMeta_(itemId: string | number | null): void;
|
|
340
|
+
/**
|
|
341
|
+
* Generates the next auto increment ID.
|
|
342
|
+
*
|
|
343
|
+
* @returns The next auto increment ID.
|
|
344
|
+
* @example
|
|
345
|
+
* ```typescript
|
|
346
|
+
* const nextId = this.nextAutoIncrementId_();
|
|
347
|
+
* ```
|
|
348
|
+
*/
|
|
349
|
+
private nextAutoIncrementId__;
|
|
350
|
+
/**
|
|
351
|
+
* Retrieves the collection's extra metadata.
|
|
352
|
+
*
|
|
353
|
+
* @returns The collection's extra metadata.
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* ```typescript
|
|
357
|
+
* const colExtraMeta = collectionRef.getExtraMeta();
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
360
|
+
getExtraMeta<T extends JsonObject>(): T;
|
|
361
|
+
/**
|
|
362
|
+
* Sets/replace the collection's extra metadata.
|
|
363
|
+
*
|
|
364
|
+
* @param extraMeta The new collection's extra metadata.
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```typescript
|
|
368
|
+
* collectionRef.replaceExtraMeta({ a: 1, b: 2, c: 3 });
|
|
369
|
+
* ```
|
|
370
|
+
*/
|
|
371
|
+
replaceExtraMeta<T extends JsonObject>(extraMeta: T): void;
|
|
372
|
+
/**
|
|
373
|
+
* Updates collection's extra metadata by merging a partial update.
|
|
374
|
+
*
|
|
375
|
+
* @param extraMeta The part of extra metadata to merge into the collection's extra metadata.
|
|
376
|
+
*
|
|
377
|
+
* @example
|
|
378
|
+
* ```typescript
|
|
379
|
+
* collectionRef.mergeExtraMeta({ c: 4 });
|
|
380
|
+
* ```
|
|
381
|
+
*/
|
|
382
|
+
mergeExtraMeta<T extends JsonObject>(extraMeta: Partial<T>): void;
|
|
383
|
+
}
|
|
384
|
+
//# sourceMappingURL=collection-reference.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collection-reference.d.ts","sourceRoot":"","sources":["../src/collection-reference.ts"],"names":[],"mappings":"AAEA,OAAO,EAGL,KAAK,WAAW,EAChB,KAAK,iBAAiB,EACtB,KAAK,cAAc,EACnB,KAAK,kBAAkB,EACvB,KAAK,aAAa,EACnB,MAAM,yBAAyB,CAAC;AAKjC,OAAO,KAAK,EAAa,UAAU,EAAC,MAAM,qBAAqB,CAAC;AAIhE;;;;;GAKG;AACH,qBAAa,mBAAmB,CAAC,KAAK,SAAS,UAAU,GAAG,UAAU;IAiKlE,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,iBAAiB;IAjK3B;;OAEG;IACH,MAAM,CAAC,QAAQ,CAAC,OAAO,SAAuB;IAE9C;;OAEG;IACH,MAAM,CAAC,QAAQ,CAAC,iBAAiB,KAAK;IAEtC;;;;;;;;OAQG;IACH,MAAM,CAAC,cAAc,CAAC,KAAK,SAAS,UAAU,EAC5C,IAAI,EAAE,WAAW,EACjB,WAAW,EAAE,iBAAiB,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,GAAG,IAAI,EACpD,eAAe,EAAE,CAAC,IAAI,EAAE,mBAAmB,CAAC,KAAK,CAAC,KAAK,IAAI,EAC3D,WAAW,CAAC,EAAE,MAAM,GACnB,mBAAmB,CAAC,KAAK,CAAC;IAuB7B;;;;;;;OAOG;IACH,MAAM,CAAC,iBAAiB,CAAC,KAAK,SAAS,UAAU,EAC/C,OAAO,EAAE,iBAAiB,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AY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,IAAI;IAY5D;;;;;;;;OAQG;IACH,OAAO,CAAC,qBAAqB;IAS7B;;;;;;;;;OASG;IACH,YAAY,CAAC,CAAC,SAAS,UAAU,KAAK,CAAC;IAKvC;;;;;;;;;OASG;IACH,gBAAgB,CAAC,CAAC,SAAS,UAAU,EAAE,SAAS,EAAE,CAAC,GAAG,IAAI;IAM1D;;;;;;;;;OASG;IACH,cAAc,CAAC,CAAC,SAAS,UAAU,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI;CAKlE"}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { type StoreFileId, type DocumentContext, type StoreFileMeta } from '@alwatr/nitrobase-types';
|
|
2
|
+
import type { JsonObject } from '@alwatr/type-helper';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a reference to a document of the AlwatrNitrobase.
|
|
5
|
+
* Provides methods to interact with the document, such as get, set, update and save.
|
|
6
|
+
*/
|
|
7
|
+
export declare class DocumentReference<TDoc extends JsonObject = JsonObject> {
|
|
8
|
+
private readonly context__;
|
|
9
|
+
private readonly updatedCallback__;
|
|
10
|
+
/**
|
|
11
|
+
* Alwatr nitrobase engine version string.
|
|
12
|
+
*/
|
|
13
|
+
static readonly version: string;
|
|
14
|
+
/**
|
|
15
|
+
* Alwatr nitrobase engine file format version number.
|
|
16
|
+
*/
|
|
17
|
+
static readonly fileFormatVersion = 3;
|
|
18
|
+
/**
|
|
19
|
+
* Creates new DocumentReference instance from stat and initial data.
|
|
20
|
+
*
|
|
21
|
+
* @param statId the document stat.
|
|
22
|
+
* @param initialData the document data.
|
|
23
|
+
* @param updatedCallback the callback to invoke when the document changed.
|
|
24
|
+
* @template TDoc The document data type.
|
|
25
|
+
* @returns A new document reference class.
|
|
26
|
+
*/
|
|
27
|
+
static newRefFromData<TDoc extends JsonObject>(statId: StoreFileId, initialData: TDoc, updatedCallback: (from: DocumentReference<TDoc>) => unknown, debugDomain?: string): DocumentReference<TDoc>;
|
|
28
|
+
/**
|
|
29
|
+
* Creates new DocumentReference instance from DocumentContext.
|
|
30
|
+
*
|
|
31
|
+
* @param context the document context.
|
|
32
|
+
* @param updatedCallback the callback to invoke when the document changed.
|
|
33
|
+
* @template TDoc The document data type.
|
|
34
|
+
* @returns A new document reference class.
|
|
35
|
+
*/
|
|
36
|
+
static newRefFromContext<TDoc extends JsonObject>(context: DocumentContext<TDoc>, updatedCallback: (from: DocumentReference<TDoc>) => unknown, debugDomain?: string): DocumentReference<TDoc>;
|
|
37
|
+
/**
|
|
38
|
+
* Validates the document context and try to migrate it to the latest version.
|
|
39
|
+
*/
|
|
40
|
+
private validateContext__;
|
|
41
|
+
/**
|
|
42
|
+
* Migrate the document context to the latest.
|
|
43
|
+
*/
|
|
44
|
+
private migrateContext__;
|
|
45
|
+
/**
|
|
46
|
+
* The ID of the document nitrobase file.
|
|
47
|
+
*/
|
|
48
|
+
readonly id: string;
|
|
49
|
+
/**
|
|
50
|
+
* The location path of the document nitrobase file.
|
|
51
|
+
*/
|
|
52
|
+
readonly path: string;
|
|
53
|
+
/**
|
|
54
|
+
* Indicates whether the document has unsaved changes.
|
|
55
|
+
*/
|
|
56
|
+
hasUnprocessedChanges_: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Logger instance for this document.
|
|
59
|
+
*/
|
|
60
|
+
private logger__;
|
|
61
|
+
/**
|
|
62
|
+
* Create a new document reference.
|
|
63
|
+
* Document reference have methods to get, set, update and save the AlwatrNitrobase Document.
|
|
64
|
+
*
|
|
65
|
+
* @param context__ Document's context filled from the Alwatr Nitrobase (parent).
|
|
66
|
+
* @param updatedCallback__ updated callback to invoke when the document is updated from the Alwatr Nitrobase (parent).
|
|
67
|
+
* @template TDoc The document data type.
|
|
68
|
+
*/
|
|
69
|
+
constructor(context__: DocumentContext<TDoc>, updatedCallback__: (from: DocumentReference<TDoc>) => unknown, debugDomain?: string);
|
|
70
|
+
/**
|
|
71
|
+
* Get nitrobase schema version
|
|
72
|
+
*
|
|
73
|
+
* @returns nitrobase schema version
|
|
74
|
+
*/
|
|
75
|
+
get schemaVer(): number;
|
|
76
|
+
/**
|
|
77
|
+
* Set nitrobase schema version for migrate
|
|
78
|
+
*/
|
|
79
|
+
set schemaVer(ver: number);
|
|
80
|
+
/**
|
|
81
|
+
* Indicates whether the document data is frozen and cannot be saved.
|
|
82
|
+
*/
|
|
83
|
+
private _freeze;
|
|
84
|
+
/**
|
|
85
|
+
* Gets the freeze status of the document data.
|
|
86
|
+
*
|
|
87
|
+
* @returns `true` if the document data is frozen, `false` otherwise.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const isFrozen = documentRef.freeze;
|
|
92
|
+
* console.log(isFrozen); // Output: false
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
get freeze(): boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Sets the freeze status of the document data.
|
|
98
|
+
*
|
|
99
|
+
* @param value - The freeze status to set.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* documentRef.freeze = true;
|
|
104
|
+
* console.log(documentRef.freeze); // Output: true
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
set freeze(value: boolean);
|
|
108
|
+
/**
|
|
109
|
+
* Retrieves the document's data.
|
|
110
|
+
*
|
|
111
|
+
* @returns The document's data.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* const documentData = documentRef.getData();
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
getData(): TDoc;
|
|
119
|
+
/**
|
|
120
|
+
* Retrieves the document's metadata.
|
|
121
|
+
*
|
|
122
|
+
* @returns The document's metadata.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* const documentMeta = documentRef.getStoreMeta();
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
getStoreMeta(): Readonly<StoreFileMeta>;
|
|
130
|
+
/**
|
|
131
|
+
* Sets the document's data. replacing the existing data.
|
|
132
|
+
*
|
|
133
|
+
* @param data The new document data.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* documentRef.replaceData({ a: 1, b: 2, c: 3 });
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
replaceData(data: TDoc): void;
|
|
141
|
+
/**
|
|
142
|
+
* Updates document's data by merging a partial update into the document's data.
|
|
143
|
+
*
|
|
144
|
+
* @param data The part of data to merge into the document's data.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* documentRef.mergeData({ c: 4 });
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
mergeData(data: Partial<TDoc>): void;
|
|
152
|
+
/**
|
|
153
|
+
* Requests the Alwatr Nitrobase to save the document.
|
|
154
|
+
* Saving may take some time in Alwatr Nitrobase due to the use of throttling.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* documentRef.save();
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
save(): void;
|
|
162
|
+
/**
|
|
163
|
+
* Requests the Alwatr Nitrobase to save the document immediately.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```typescript
|
|
167
|
+
* documentRef.saveImmediate();
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
saveImmediate(): void;
|
|
171
|
+
/**
|
|
172
|
+
* Retrieves the full context of the document.
|
|
173
|
+
*
|
|
174
|
+
* @returns The full context of the document.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* const context = documentRef.getFullContext_();
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
getFullContext_(): Readonly<DocumentContext<TDoc>>;
|
|
182
|
+
updateDelayed_: boolean;
|
|
183
|
+
/**
|
|
184
|
+
* Update the document metadata and invoke the updated callback.
|
|
185
|
+
* This method is throttled to prevent multiple updates in a short time.
|
|
186
|
+
*/
|
|
187
|
+
private updated__;
|
|
188
|
+
/**
|
|
189
|
+
* Refresh/recalculate the document's metadata timestamp and revision.
|
|
190
|
+
*/
|
|
191
|
+
protected refreshMetadata_(): void;
|
|
192
|
+
/**
|
|
193
|
+
* Retrieves the document's extra metadata.
|
|
194
|
+
*
|
|
195
|
+
* @returns The document's extra metadata.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```typescript
|
|
199
|
+
* const colExtraMeta = documentRef.getExtraMeta();
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
getExtraMeta<T extends JsonObject>(): T;
|
|
203
|
+
/**
|
|
204
|
+
* Sets/replace the document's extra metadata.
|
|
205
|
+
*
|
|
206
|
+
* @param extraMeta The new document's extra metadata.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* documentRef.replaceExtraMeta({ a: 1, b: 2, c: 3 });
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
replaceExtraMeta<T extends JsonObject>(extraMeta: T): void;
|
|
214
|
+
/**
|
|
215
|
+
* Updates document's extra metadata by merging a partial update.
|
|
216
|
+
*
|
|
217
|
+
* @param extraMeta The part of extra metadata to merge into the document's extra metadata.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```typescript
|
|
221
|
+
* documentRef.mergeExtraMeta({ c: 4 });
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
mergeExtraMeta<T extends JsonObject>(extraMeta: Partial<T>): void;
|
|
225
|
+
}
|
|
226
|
+
//# sourceMappingURL=document-reference.d.ts.map
|