@sv443-network/coreutils 3.0.0 → 3.0.2
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 +14 -0
- package/dist/CoreUtils.cjs +1 -2
- package/dist/CoreUtils.min.cjs +1 -1
- package/dist/CoreUtils.min.mjs +1 -1
- package/dist/CoreUtils.min.umd.js +1 -1
- package/dist/CoreUtils.mjs +1 -2
- package/dist/CoreUtils.umd.js +423 -539
- package/dist/lib/DataStore.d.ts +9 -5
- package/dist/lib/DataStoreEngine.d.ts +5 -5
- package/dist/lib/DataStoreSerializer.d.ts +4 -4
- package/package.json +5 -2
package/dist/lib/DataStore.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
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
5
|
import type { DataStoreEngine } from "./DataStoreEngine.ts";
|
|
6
|
-
import type { LooseUnion, Prettify
|
|
6
|
+
import type { LooseUnion, Prettify } from "./types.ts";
|
|
7
7
|
/** Function that takes the data in the old format and returns the data in the new format. Also supports an asynchronous migration. */
|
|
8
8
|
type MigrationFunc = (oldData: any) => any | Promise<any>;
|
|
9
9
|
/** Dictionary of format version numbers and the function that migrates to them from the previous whole integer */
|
|
@@ -40,7 +40,7 @@ export type DataStoreOptions<TData extends DataStoreData> = Prettify<{
|
|
|
40
40
|
*
|
|
41
41
|
* - ⚠️ Don't reuse the same engine instance for multiple DataStores, unless it explicitly supports it!
|
|
42
42
|
*/
|
|
43
|
-
engine: (() => DataStoreEngine
|
|
43
|
+
engine: (() => DataStoreEngine) | DataStoreEngine;
|
|
44
44
|
/**
|
|
45
45
|
* A dictionary of functions that can be used to migrate data from older versions to newer ones.
|
|
46
46
|
* The keys of the dictionary should be the format version that the functions can migrate to, from the previous whole integer value.
|
|
@@ -94,8 +94,12 @@ export type DataStoreOptions<TData extends DataStoreData> = Prettify<{
|
|
|
94
94
|
decodeData: DecodeTuple;
|
|
95
95
|
compressionFormat?: never;
|
|
96
96
|
})>;
|
|
97
|
-
/**
|
|
98
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Generic type that represents the serializable data structure saved in a {@linkcode DataStore} instance.
|
|
99
|
+
* - ⚠️ Uses `object` instead of an index signature so that interfaces without an explicit index signature can be used as `TData`.
|
|
100
|
+
* Make sure to only use JSON-serializable types here, otherwise unexpected behavior may occur!
|
|
101
|
+
*/
|
|
102
|
+
export type DataStoreData = object;
|
|
99
103
|
/**
|
|
100
104
|
* 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.
|
|
101
105
|
* Supports migrating data from older format versions to newer ones and populating the cache with default data if no persistent data is found.
|
|
@@ -118,7 +122,7 @@ export declare class DataStore<TData extends DataStoreData> {
|
|
|
118
122
|
readonly decodeData: DataStoreOptions<TData>["decodeData"];
|
|
119
123
|
readonly compressionFormat: Exclude<DataStoreOptions<TData>["compressionFormat"], undefined>;
|
|
120
124
|
readonly memoryCache: boolean;
|
|
121
|
-
readonly engine: DataStoreEngine
|
|
125
|
+
readonly engine: DataStoreEngine;
|
|
122
126
|
options: DataStoreOptions<TData>;
|
|
123
127
|
/**
|
|
124
128
|
* Whether all first-init checks should be done.
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
import type { DataStoreData, DataStoreOptions } from "./DataStore.ts";
|
|
7
7
|
import type { Prettify, SerializableVal } from "./types.ts";
|
|
8
8
|
/** Contains the only properties of {@linkcode DataStoreOptions} that are relevant to the {@linkcode DataStoreEngine} class. */
|
|
9
|
-
export type DataStoreEngineDSOptions<TData extends DataStoreData> = Prettify<Pick<DataStoreOptions<TData>, "decodeData" | "encodeData" | "id">>;
|
|
10
|
-
export interface DataStoreEngine<TData extends DataStoreData> {
|
|
9
|
+
export type DataStoreEngineDSOptions<TData extends DataStoreData = DataStoreData> = Prettify<Pick<DataStoreOptions<TData>, "decodeData" | "encodeData" | "id">>;
|
|
10
|
+
export interface DataStoreEngine<TData extends DataStoreData = DataStoreData> {
|
|
11
11
|
/** Deletes all data in persistent storage, including the data container itself (e.g. a file or a database) */
|
|
12
12
|
deleteStorage?(): Promise<void>;
|
|
13
13
|
}
|
|
@@ -15,7 +15,7 @@ export interface DataStoreEngine<TData extends DataStoreData> {
|
|
|
15
15
|
* Base class for creating {@linkcode DataStore} storage engines.
|
|
16
16
|
* This acts as an interchangeable API for writing and reading persistent JSON-serializable data in various environments.
|
|
17
17
|
*/
|
|
18
|
-
export declare abstract class DataStoreEngine<TData extends DataStoreData> {
|
|
18
|
+
export declare abstract class DataStoreEngine<TData extends DataStoreData = DataStoreData> {
|
|
19
19
|
protected dataStoreOptions: DataStoreEngineDSOptions<TData>;
|
|
20
20
|
constructor(options?: DataStoreEngineDSOptions<TData>);
|
|
21
21
|
/** Called by DataStore on creation, to pass its options. Only call this if you are using this instance standalone! */
|
|
@@ -54,7 +54,7 @@ export type BrowserStorageEngineOptions = {
|
|
|
54
54
|
* - ⚠️ Requires a DOM environment
|
|
55
55
|
* - ⚠️ Don't reuse engine instances, always create a new one for each {@linkcode DataStore} instance
|
|
56
56
|
*/
|
|
57
|
-
export declare class BrowserStorageEngine<TData extends DataStoreData> extends DataStoreEngine<TData> {
|
|
57
|
+
export declare class BrowserStorageEngine<TData extends DataStoreData = DataStoreData> extends DataStoreEngine<TData> {
|
|
58
58
|
protected options: BrowserStorageEngineOptions & Required<Pick<BrowserStorageEngineOptions, "type">>;
|
|
59
59
|
/**
|
|
60
60
|
* Creates an instance of `BrowserStorageEngine`.
|
|
@@ -86,7 +86,7 @@ export type FileStorageEngineOptions = {
|
|
|
86
86
|
* - ⚠️ Requires Node.js or Deno with Node compatibility (v1.31+)
|
|
87
87
|
* - ⚠️ Don't reuse engine instances, always create a new one for each {@linkcode DataStore} instance
|
|
88
88
|
*/
|
|
89
|
-
export declare class FileStorageEngine<TData extends DataStoreData> extends DataStoreEngine<TData> {
|
|
89
|
+
export declare class FileStorageEngine<TData extends DataStoreData = DataStoreData> extends DataStoreEngine<TData> {
|
|
90
90
|
protected options: FileStorageEngineOptions & Required<Pick<FileStorageEngineOptions, "filePath">>;
|
|
91
91
|
private fileAccessQueue;
|
|
92
92
|
/**
|
|
@@ -42,10 +42,10 @@ export type StoreFilter = string[] | ((id: string) => boolean);
|
|
|
42
42
|
*
|
|
43
43
|
* - ⚠️ Needs to run in a secure context (HTTPS) due to the use of the SubtleCrypto API if checksumming is enabled.
|
|
44
44
|
*/
|
|
45
|
-
export declare class DataStoreSerializer
|
|
46
|
-
protected stores: DataStore<
|
|
45
|
+
export declare class DataStoreSerializer {
|
|
46
|
+
protected stores: DataStore<DataStoreData>[];
|
|
47
47
|
protected options: Required<DataStoreSerializerOptions>;
|
|
48
|
-
constructor(stores: DataStore<
|
|
48
|
+
constructor(stores: DataStore<DataStoreData>[], options?: DataStoreSerializerOptions);
|
|
49
49
|
/** Calculates the checksum of a string */
|
|
50
50
|
protected calcChecksum(input: string): Promise<string>;
|
|
51
51
|
/**
|
|
@@ -114,5 +114,5 @@ export declare class DataStoreSerializer<TData extends DataStoreData> {
|
|
|
114
114
|
/** Checks if a given value is a SerializedDataStore object */
|
|
115
115
|
static isSerializedDataStoreObj(obj: unknown): obj is SerializedDataStore;
|
|
116
116
|
/** Returns the DataStore instances whose IDs match the provided array or function */
|
|
117
|
-
protected getStoresFiltered(stores?: StoreFilter): DataStore<
|
|
117
|
+
protected getStoresFiltered(stores?: StoreFilter): DataStore<DataStoreData>[];
|
|
118
118
|
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sv443-network/coreutils",
|
|
3
3
|
"libName": "@sv443-network/coreutils",
|
|
4
|
-
"version": "3.0.
|
|
4
|
+
"version": "3.0.2",
|
|
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",
|
|
8
8
|
"types": "dist/lib/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"browser": "./dist/CoreUtils.
|
|
11
|
+
"browser": "./dist/CoreUtils.mjs",
|
|
12
12
|
"types": "./dist/lib/index.d.ts",
|
|
13
13
|
"require": "./dist/CoreUtils.cjs",
|
|
14
14
|
"import": "./dist/CoreUtils.mjs"
|
|
@@ -76,6 +76,9 @@
|
|
|
76
76
|
"/CHANGELOG.md",
|
|
77
77
|
"/LICENSE.txt"
|
|
78
78
|
],
|
|
79
|
+
"publishConfig": {
|
|
80
|
+
"provenance": true
|
|
81
|
+
},
|
|
79
82
|
"scripts": {
|
|
80
83
|
"lint": "eslint . && tsc --noEmit",
|
|
81
84
|
"format": "eslint . --fix",
|