json-database-st 2.0.3 → 3.1.4
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/JSONDatabase.d.ts +75 -188
- package/JSONDatabase.js +7339 -468
- package/JSONDatabase.legacy.js +567 -0
- package/LICENSE +21 -21
- package/README.md +62 -39
- package/index.d.ts +0 -0
- package/index.darwin-arm64.node +0 -0
- package/index.js +117 -0
- package/index.win32-x64-msvc.node +0 -0
- package/package.json +18 -5
package/JSONDatabase.d.ts
CHANGED
|
@@ -1,200 +1,87 @@
|
|
|
1
|
-
// File: JSONDatabase.d.ts
|
|
2
1
|
import { EventEmitter } from 'events';
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Options configuring behavior of the JSONDatabase.
|
|
12
|
-
*/
|
|
13
|
-
export interface DBOptions {
|
|
14
|
-
/**
|
|
15
|
-
* Optional key used to encrypt the file on disk. If provided,
|
|
16
|
-
* all reads/writes will transparently encrypt/decrypt data.
|
|
17
|
-
*/
|
|
18
|
-
encryptionKey?: string;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* When true the file will be written with 2-space indentation for readability.
|
|
22
|
-
* Defaults to false (compact).
|
|
23
|
-
*/
|
|
24
|
-
prettyPrint?: boolean;
|
|
3
|
+
export interface IndexConfig {
|
|
4
|
+
name: string;
|
|
5
|
+
path: string;
|
|
6
|
+
field: string;
|
|
7
|
+
unique?: boolean;
|
|
8
|
+
}
|
|
25
9
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
10
|
+
export interface DatabaseOptions {
|
|
11
|
+
encryptionKey?: string;
|
|
12
|
+
saveDelay?: number;
|
|
13
|
+
prettyPrint?: boolean;
|
|
14
|
+
silent?: boolean;
|
|
15
|
+
wal?: boolean;
|
|
16
|
+
schema?: any;
|
|
17
|
+
indices?: IndexConfig[];
|
|
18
|
+
}
|
|
32
19
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
20
|
+
export interface MiddlewareContext {
|
|
21
|
+
path: string;
|
|
22
|
+
value?: any;
|
|
23
|
+
finalData?: any;
|
|
24
|
+
[key: string]: any;
|
|
25
|
+
}
|
|
38
26
|
|
|
39
|
-
|
|
40
|
-
* Optional index definitions for faster lookups via `findByIndex`.
|
|
41
|
-
*/
|
|
42
|
-
indices?: IndexDefinition[];
|
|
27
|
+
export type MiddlewareFn = (ctx: MiddlewareContext) => MiddlewareContext;
|
|
43
28
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
29
|
+
export class DBError extends Error {}
|
|
30
|
+
export class TransactionError extends DBError {}
|
|
31
|
+
export class ValidationError extends DBError {
|
|
32
|
+
issues?: any[];
|
|
33
|
+
constructor(msg: string, issues?: any[]);
|
|
48
34
|
}
|
|
49
35
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
36
|
+
export class QueryCursor implements PromiseLike<any[]> {
|
|
37
|
+
limit(n: number): this;
|
|
38
|
+
skip(n: number): this;
|
|
39
|
+
sort(criteria: any): this;
|
|
40
|
+
select(fields: string[]): this;
|
|
41
|
+
exec(): Promise<any[]>;
|
|
42
|
+
then<TResult1 = any[], TResult2 = never>(
|
|
43
|
+
onfulfilled?: ((value: any[]) => TResult1 | PromiseLike<TResult1>) | null,
|
|
44
|
+
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
|
|
45
|
+
): PromiseLike<TResult1 | TResult2>;
|
|
60
46
|
}
|
|
61
47
|
|
|
62
|
-
/**
|
|
63
|
-
* Discriminated union describing a single batched operation.
|
|
64
|
-
*/
|
|
65
|
-
export type BatchOp =
|
|
66
|
-
| { type: 'set'; path: string; value: any }
|
|
67
|
-
| { type: 'delete'; path: string }
|
|
68
|
-
| { type: 'push'; path: string; values: any[] }
|
|
69
|
-
| { type: 'pull'; path: string; values: any[] };
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Main JSON file-backed database.
|
|
73
|
-
*
|
|
74
|
-
* The API is promise-based and supports paths using dot-notation
|
|
75
|
-
* to address nested objects/arrays (e.g. "users.0.name").
|
|
76
|
-
*/
|
|
77
48
|
export default class JSONDatabase extends EventEmitter {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
*/
|
|
117
|
-
pull(path: string, ...items: any[]): Promise<void>;
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Atomically add `amount` to a numeric value at `path`. If the value is missing,
|
|
121
|
-
* it will be treated as 0 before adding.
|
|
122
|
-
*/
|
|
123
|
-
add(path: string, amount: number): Promise<number>;
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Atomically subtract `amount` from a numeric value at `path`.
|
|
127
|
-
*/
|
|
128
|
-
subtract(path: string, amount: number): Promise<number>;
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Run a function with the current DB snapshot and atomically persist
|
|
132
|
-
* the result. The function may mutate the provided object (in-memory)
|
|
133
|
-
* and the final value will be written back.
|
|
134
|
-
*/
|
|
135
|
-
transaction<T = any>(fn: (data: any) => T | Promise<T>): Promise<T>;
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Execute a batch of operations. If `stopOnError` is true, processing
|
|
139
|
-
* will stop at the first failing operation and the returned promise will reject.
|
|
140
|
-
*/
|
|
141
|
-
batch(ops: BatchOp[], options?: { stopOnError?: boolean }): Promise<any>;
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Find the first item under `path` that matches `predicate`.
|
|
145
|
-
* `predicate` may be an object of key/value pairs or a predicate function.
|
|
146
|
-
*/
|
|
147
|
-
find<T = any>(path: string, predicate: Partial<T> | ((item: T) => boolean)): Promise<T | undefined>;
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Lookup a record using a previously defined index.
|
|
151
|
-
*/
|
|
152
|
-
findByIndex<T = any>(indexName: string, value: any): Promise<T | undefined>;
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Return a paginated slice of an array at `path`. `page` is 1-based.
|
|
156
|
-
*/
|
|
157
|
-
paginate<T = any>(
|
|
158
|
-
path: string,
|
|
159
|
-
page?: number,
|
|
160
|
-
limit?: number,
|
|
161
|
-
filterFn?: (item: T) => boolean
|
|
162
|
-
): Promise<{
|
|
163
|
-
data: T[];
|
|
164
|
-
meta: { total: number; page: number; limit: number; totalPages: number; hasNext: boolean };
|
|
165
|
-
}>;
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Create a snapshot file (or internal snapshot) and return the snapshot id/path.
|
|
169
|
-
*/
|
|
170
|
-
createSnapshot(label?: string): Promise<string>;
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Remove all data and return the previous root object.
|
|
174
|
-
*/
|
|
175
|
-
clear(): Promise<object>;
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Close any open file handles and stop background tasks.
|
|
179
|
-
*/
|
|
180
|
-
close(): Promise<void>;
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Register a handler to run before an operation. `op` is one of: 'get','set','delete','push','pull','batch'.
|
|
184
|
-
* The callback receives a context object with details about the operation and may throw to abort the operation.
|
|
185
|
-
*/
|
|
186
|
-
before(op: string, path: string, cb: (ctx: any) => any): void;
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Register a handler to run after an operation completes successfully.
|
|
190
|
-
*/
|
|
191
|
-
after(op: string, path: string, cb: (ctx: any) => any): void;
|
|
49
|
+
static DBError: typeof DBError;
|
|
50
|
+
static TransactionError: typeof TransactionError;
|
|
51
|
+
static ValidationError: typeof ValidationError;
|
|
52
|
+
static QueryCursor: typeof QueryCursor;
|
|
53
|
+
|
|
54
|
+
constructor(filename: string, options?: DatabaseOptions);
|
|
55
|
+
|
|
56
|
+
get<T = any>(path?: string, defaultValue?: T): Promise<T>;
|
|
57
|
+
has(path: string): Promise<boolean>;
|
|
58
|
+
set(path: string, value: any): Promise<boolean>;
|
|
59
|
+
delete(path: string): Promise<boolean>;
|
|
60
|
+
|
|
61
|
+
push(path: string, ...items: any[]): Promise<boolean | void>;
|
|
62
|
+
pull(path: string, ...items: any[]): Promise<boolean | void>;
|
|
63
|
+
|
|
64
|
+
add(path: string, amount: number): Promise<boolean>;
|
|
65
|
+
subtract(path: string, amount: number): Promise<boolean>;
|
|
66
|
+
|
|
67
|
+
find<T = any>(path: string, predicate: ((item: T) => boolean) | object): Promise<T | undefined>;
|
|
68
|
+
findByIndex<T = any>(indexName: string, value: any): Promise<T | undefined>;
|
|
69
|
+
|
|
70
|
+
query(path: string, query?: any): QueryCursor;
|
|
71
|
+
|
|
72
|
+
transaction<T = any>(fn: (data: any) => T | Promise<T>): Promise<boolean>;
|
|
73
|
+
batch(ops: Array<{ type: 'set' | 'delete' | 'push'; path: string; value?: any; values?: any[] }>): Promise<boolean>;
|
|
74
|
+
|
|
75
|
+
clear(): Promise<boolean>;
|
|
76
|
+
|
|
77
|
+
paginate<T = any>(path: string, page?: number, limit?: number): Promise<{
|
|
78
|
+
data: T[];
|
|
79
|
+
meta: { total: number; page: number; limit: number; totalPages: number; hasNext: boolean };
|
|
80
|
+
}>;
|
|
81
|
+
|
|
82
|
+
createSnapshot(label?: string): Promise<string>;
|
|
83
|
+
close(): Promise<void>;
|
|
84
|
+
|
|
85
|
+
before(op: 'set' | 'delete' | 'push' | 'pull', pattern: string, cb: MiddlewareFn): void;
|
|
86
|
+
after(op: 'set' | 'delete' | 'push' | 'pull', pattern: string, cb: MiddlewareFn): void;
|
|
192
87
|
}
|
|
193
|
-
|
|
194
|
-
/* Example usage:
|
|
195
|
-
import JSONDatabase from './JSONDatabase';
|
|
196
|
-
|
|
197
|
-
const db = new JSONDatabase('data.json', { prettyPrint: true });
|
|
198
|
-
await db.set('users.alice', { id: 'alice', age: 30 });
|
|
199
|
-
const alice = await db.get('users.alice');
|
|
200
|
-
*/
|