json-database-st 1.0.12 → 2.0.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/JSONDatabase.d.ts +200 -0
- package/JSONDatabase.js +536 -608
- package/README.md +42 -110
- package/package.json +4 -2
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// File: JSONDatabase.d.ts
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A lightweight schema representation accepted by the DB.
|
|
6
|
+
* Libraries such as Zod or Joi can be used at runtime; this type is intentionally permissive.
|
|
7
|
+
*/
|
|
8
|
+
export type SchemaLike = any;
|
|
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;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* When true, any change is flushed to disk immediately. When false,
|
|
28
|
+
* writes may be batched for performance.
|
|
29
|
+
* Defaults to true.
|
|
30
|
+
*/
|
|
31
|
+
writeOnChange?: boolean;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Optional validation schema (Zod/Joi/etc). If provided, writes
|
|
35
|
+
* that would violate the schema will be rejected.
|
|
36
|
+
*/
|
|
37
|
+
schema?: SchemaLike;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Optional index definitions for faster lookups via `findByIndex`.
|
|
41
|
+
*/
|
|
42
|
+
indices?: IndexDefinition[];
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* If true, database will periodically compact the file to reduce size.
|
|
46
|
+
*/
|
|
47
|
+
autoCompact?: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Defines a secondary index on objects stored under a path.
|
|
52
|
+
* - `path` is a dot-separated path inside the DB where the array/object lives.
|
|
53
|
+
* - `field` is the property name to index on each element.
|
|
54
|
+
*/
|
|
55
|
+
export interface IndexDefinition {
|
|
56
|
+
name: string;
|
|
57
|
+
path: string;
|
|
58
|
+
field: string;
|
|
59
|
+
unique?: boolean;
|
|
60
|
+
}
|
|
61
|
+
|
|
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
|
+
export default class JSONDatabase extends EventEmitter {
|
|
78
|
+
/**
|
|
79
|
+
* Open or create a database file at `filename`.
|
|
80
|
+
* @param filename Path to JSON file on disk.
|
|
81
|
+
* @param options Optional DBOptions to configure behavior.
|
|
82
|
+
*/
|
|
83
|
+
constructor(filename: string, options?: DBOptions);
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Read a value at `path`. If `path` is omitted, returns the entire DB.
|
|
87
|
+
* @param path Dot-separated path or undefined for root.
|
|
88
|
+
* @param defaultValue Value returned when the path is missing.
|
|
89
|
+
*/
|
|
90
|
+
get<T = any>(path?: string, defaultValue?: T): Promise<T>;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Return true if the given path exists.
|
|
94
|
+
*/
|
|
95
|
+
has(path: string): Promise<boolean>;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Set the value at `path`. If `path` points to a nested location,
|
|
99
|
+
* intermediate objects/arrays will be created as needed.
|
|
100
|
+
*/
|
|
101
|
+
set(path: string, value: any): Promise<any>;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Delete the value at `path`. Returns true if something was deleted.
|
|
105
|
+
*/
|
|
106
|
+
delete(path: string): Promise<boolean>;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Append one or more items to an array at `path`. If the target is missing,
|
|
110
|
+
* an array will be created.
|
|
111
|
+
*/
|
|
112
|
+
push(path: string, ...items: any[]): Promise<void>;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Remove one or more items from an array at `path` (strict equality).
|
|
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;
|
|
192
|
+
}
|
|
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
|
+
*/
|