@sv443-network/coreutils 3.2.0 → 3.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 +23 -0
- package/dist/CoreUtils.cjs +249 -225
- package/dist/CoreUtils.min.cjs +3 -3
- package/dist/CoreUtils.min.mjs +3 -3
- package/dist/CoreUtils.min.umd.js +3 -3
- package/dist/CoreUtils.mjs +249 -225
- package/dist/CoreUtils.umd.js +764 -792
- package/dist/lib/DataStore.d.ts +32 -4
- package/package.json +1 -1
package/dist/lib/DataStore.d.ts
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
* @module DataStore
|
|
3
3
|
* This module contains the DataStore class, which is a general purpose, sync and async persistent database for JSON-serializable data - [see the documentation for more info](https://github.com/Sv443-Network/CoreUtils/blob/main/docs.md#class-datastore)
|
|
4
4
|
*/
|
|
5
|
+
import { MigrationError } from "./Errors.ts";
|
|
5
6
|
import type { DataStoreEngine } from "./DataStoreEngine.ts";
|
|
6
7
|
import type { LooseUnion, Prettify } from "./types.ts";
|
|
8
|
+
import { NanoEmitter, type NanoEmitterOptions } from "./NanoEmitter.ts";
|
|
7
9
|
/** Function that takes the data in the old format and returns the data in the new format. Also supports an asynchronous migration. */
|
|
8
10
|
type MigrationFunc = (oldData: any) => any | Promise<any>;
|
|
9
11
|
/** Dictionary of format version numbers and the function that migrates to them from the previous whole integer */
|
|
@@ -45,7 +47,7 @@ export type DataStoreOptions<TData extends DataStoreData, TMemCache extends bool
|
|
|
45
47
|
* A dictionary of functions that can be used to migrate data from older versions to newer ones.
|
|
46
48
|
* The keys of the dictionary should be the format version that the functions can migrate to, from the previous whole integer value.
|
|
47
49
|
* The values should be functions that take the data in the old format and return the data in the new format.
|
|
48
|
-
* The functions will be run in order from the oldest to the newest version.
|
|
50
|
+
* The functions will be run in order from the oldest (smallest number) to the newest (biggest number) version.
|
|
49
51
|
* If the current format version is not in the dictionary, no migrations will be run.
|
|
50
52
|
*/
|
|
51
53
|
migrations?: DataMigrationsDict;
|
|
@@ -68,6 +70,8 @@ export type DataStoreOptions<TData extends DataStoreData, TMemCache extends bool
|
|
|
68
70
|
* Note: this will break backwards compatibility with any previously saved data, so only change this if you know what you're doing and ideally before any non-volatile data is saved by the end user.
|
|
69
71
|
*/
|
|
70
72
|
keyPrefix?: string;
|
|
73
|
+
/** Options for the internal NanoEmitter instance. */
|
|
74
|
+
nanoEmitterOptions?: NanoEmitterOptions;
|
|
71
75
|
} & ({
|
|
72
76
|
encodeData?: never;
|
|
73
77
|
decodeData?: never;
|
|
@@ -105,6 +109,27 @@ export type DataStoreOptions<TData extends DataStoreData, TMemCache extends bool
|
|
|
105
109
|
* Make sure to only use JSON-serializable types here, otherwise unexpected behavior may occur!
|
|
106
110
|
*/
|
|
107
111
|
export type DataStoreData = object;
|
|
112
|
+
/** Map of event names and their corresponding listener function signatures for the {@linkcode DataStore} class. */
|
|
113
|
+
export type DataStoreEventMap<TData> = {
|
|
114
|
+
/** Emitted whenever the data is loaded from persistent storage with {@linkcode DataStore.loadData()}. */
|
|
115
|
+
loadData: (data: TData) => void;
|
|
116
|
+
/** Emitted when the data is updated with {@linkcode DataStore.setData()} or {@linkcode DataStore.runMigrations()} */
|
|
117
|
+
updateData: (newData: TData) => void;
|
|
118
|
+
/** Emitted when the memory cache was updated with {@linkcode DataStore.setData()}, before the data is saved to persistent storage. Not emitted if `memoryCache` is set to `false`. */
|
|
119
|
+
updateDataSync: (newData: TData) => void;
|
|
120
|
+
/** Emitted for every called migration function with the resulting data. */
|
|
121
|
+
migrateData: (migratedTo: number, migratedData: unknown, isFinalMigration: boolean) => void;
|
|
122
|
+
/** Emitted for every successfully migrated old ID. Gets passed the old and new ID. */
|
|
123
|
+
migrateId: (oldId: string, newId: string) => void;
|
|
124
|
+
/** Emitted whenever the data is reset to the default value with {@linkcode DataStore.saveDefaultData()} (will not be called on the initial population of persistent storage with the default data in {@linkcode DataStore.loadData()}). */
|
|
125
|
+
setDefaultData: (defaultData: TData) => void;
|
|
126
|
+
/** Emitted after the data was deleted from persistent storage with {@linkcode DataStore.deleteData()}. */
|
|
127
|
+
deleteData: () => void;
|
|
128
|
+
/** Emitted when an error occurs at any point. */
|
|
129
|
+
error: (error: Error) => void;
|
|
130
|
+
/** Emitted only when an error occurs during a migration function. */
|
|
131
|
+
migrationError: (migratingTo: number, error: MigrationError) => void;
|
|
132
|
+
};
|
|
108
133
|
/**
|
|
109
134
|
* Manages a hybrid synchronous & asynchronous persistent JSON database that is cached in memory and persistently saved across sessions using one of the preset DataStoreEngines or your own one.
|
|
110
135
|
* Supports migrating data from older format versions to newer ones and populating the cache with default data if no persistent data is found.
|
|
@@ -119,7 +144,7 @@ export type DataStoreData = object;
|
|
|
119
144
|
* @template TData The type of the data that is saved in persistent storage for the currently set format version
|
|
120
145
|
* (TODO:FIXME: will be automatically inferred from `defaultData` if not provided)
|
|
121
146
|
*/
|
|
122
|
-
export declare class DataStore<TData extends DataStoreData, TMemCache extends boolean = true> {
|
|
147
|
+
export declare class DataStore<TData extends DataStoreData, TMemCache extends boolean = true> extends NanoEmitter<DataStoreEventMap<TData>> {
|
|
123
148
|
readonly id: string;
|
|
124
149
|
readonly formatVersion: number;
|
|
125
150
|
readonly defaultData: TData;
|
|
@@ -164,8 +189,11 @@ export declare class DataStore<TData extends DataStoreData, TMemCache extends bo
|
|
|
164
189
|
getData(this: DataStore<TData, true>): TData;
|
|
165
190
|
/** Saves the data synchronously to the in-memory cache and asynchronously to the persistent storage */
|
|
166
191
|
setData(data: TData): Promise<void>;
|
|
167
|
-
/**
|
|
168
|
-
|
|
192
|
+
/**
|
|
193
|
+
* Saves the default data passed in the constructor synchronously to the in-memory cache and asynchronously to persistent storage.
|
|
194
|
+
* @param emitEvent Whether to emit the `setDefaultData` event - set to `false` to prevent event emission (used internally during initial population in {@linkcode loadData()})
|
|
195
|
+
*/
|
|
196
|
+
saveDefaultData(emitEvent?: boolean): Promise<void>;
|
|
169
197
|
/**
|
|
170
198
|
* Call this method to clear all persistently stored data associated with this DataStore instance, including the storage container (if supported by the DataStoreEngine).
|
|
171
199
|
* The in-memory cache will be left untouched, so you may still access the data with {@linkcode getData()}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sv443-network/coreutils",
|
|
3
3
|
"libName": "@sv443-network/coreutils",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.3.0",
|
|
5
5
|
"description": "Cross-platform, general-purpose, JavaScript core library for Node, Deno and the browser. Intended to be used in conjunction with `@sv443-network/userutils` and `@sv443-network/djsutils`, but can be used independently as well.",
|
|
6
6
|
"main": "dist/CoreUtils.cjs",
|
|
7
7
|
"module": "dist/CoreUtils.mjs",
|