1 |
|
2 |
|
3 | import { EventEmitter } from "events";
|
4 |
|
5 | export = Nedb;
|
6 | export as namespace Nedb;
|
7 |
|
8 | declare class Nedb<G = any> extends EventEmitter {
|
9 | constructor(pathOrOptions?: string | Nedb.DataStoreOptions);
|
10 |
|
11 | persistence: Nedb.Persistence;
|
12 |
|
13 | /**
|
14 | * Load the database from the datafile, and trigger the execution of buffered commands if any
|
15 | */
|
16 | loadDatabase(cb?: (err: Error | null) => void): void;
|
17 |
|
18 | /**
|
19 | * Get an array of all the data in the database
|
20 | */
|
21 | getAllData(): any[];
|
22 |
|
23 | /**
|
24 | * Reset all currently defined indexes
|
25 | */
|
26 | resetIndexes(newData: any): void;
|
27 |
|
28 | /**
|
29 | * Ensure an index is kept for this field. Same parameters as lib/indexes
|
30 | * For now this function is synchronous, we need to test how much time it takes
|
31 | * We use an async API for consistency with the rest of the code
|
32 | * @param cb Optional callback, signature: err
|
33 | */
|
34 | ensureIndex(options: Nedb.EnsureIndexOptions, cb?: (err: Error | null) => void): void;
|
35 |
|
36 | /**
|
37 | * Remove an index
|
38 | * @param cb Optional callback, signature: err
|
39 | */
|
40 | removeIndex(fieldName: string, cb?: (err: Error | null) => void): void;
|
41 |
|
42 | /**
|
43 | * Add one or several document(s) to all indexes
|
44 | */
|
45 | addToIndexes<T extends G>(doc: T | T[]): void;
|
46 |
|
47 | /**
|
48 | * Remove one or several document(s) from all indexes
|
49 | */
|
50 | removeFromIndexes<T extends G>(doc: T | T[]): void;
|
51 |
|
52 | /**
|
53 | * Update one or several documents in all indexes
|
54 | * To update multiple documents, oldDoc must be an array of { oldDoc, newDoc } pairs
|
55 | * If one update violates a constraint, all changes are rolled back
|
56 | */
|
57 | updateIndexes<T extends G>(oldDoc: T, newDoc: T): void;
|
58 | updateIndexes<T extends G>(updates: Array<{ oldDoc: T; newDoc: T }>): void;
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | getCandidates(query: any): void;
|
70 |
|
71 | |
72 |
|
73 |
|
74 |
|
75 | insert<T extends G>(newDoc: T, cb?: (err: Error | null, document: T) => void): void;
|
76 | insert<T extends G>(newDocs: T[], cb?: (err: Error | null, documents: T[]) => void): void;
|
77 |
|
78 | /**
|
79 | * Count all documents matching the query
|
80 | * @param query MongoDB-style query
|
81 | */
|
82 | count(query: any, callback: (err: Error | null, n: number) => void): void;
|
83 | count(query: any): Nedb.CursorCount;
|
84 |
|
85 | /**
|
86 | * Find all documents matching the query
|
87 | * If no callback is passed, we return the cursor so that user can limit, skip and finally exec
|
88 | * @param query MongoDB-style query
|
89 | * @param projection MongoDB-style projection
|
90 | */
|
91 | find<T extends G>(query: any, projection: any, callback: (err: Error | null, documents: T[]) => void): void;
|
92 | find<T extends G>(query: any, projection?: any): Nedb.Cursor<T>;
|
93 |
|
94 | /**
|
95 | * Find all documents matching the query
|
96 | * If no callback is passed, we return the cursor so that user can limit, skip and finally exec
|
97 | * * @param query MongoDB-style query
|
98 | */
|
99 | find<T extends G>(query: any, callback: (err: Error | null, documents: T[]) => void): void;
|
100 |
|
101 | /**
|
102 | * Find one document matching the query
|
103 | * @param query MongoDB-style query
|
104 | * @param projection MongoDB-style projection
|
105 | */
|
106 | findOne<T extends G>(query: any, projection: any, callback: (err: Error | null, document: T) => void): void;
|
107 |
|
108 | /**
|
109 | * Find one document matching the query
|
110 | * @param query MongoDB-style query
|
111 | */
|
112 | findOne<T extends G>(query: any, callback: (err: Error | null, document: T) => void): void;
|
113 |
|
114 | /**
|
115 | * Update all docs matching query v1.7.4 and prior signature.
|
116 | * For now, very naive implementation (recalculating the whole database)
|
117 | * @param options Optional options
|
118 | * options.multi If true, can update multiple documents (defaults to false)
|
119 | * options.upsert If true, document is inserted if the query doesn't match anything
|
120 | * @param cb Optional callback, signature: err,
|
121 | * numReplaced,
|
122 | * upsert (set to true if the update was in fact an upsert)
|
123 | *
|
124 | * @api private Use Datastore.update which has the same signature
|
125 | */
|
126 | update(
|
127 | query: any,
|
128 | updateQuery: any,
|
129 | options?: Nedb.UpdateOptions,
|
130 | cb?: (err: Error | null, numberOfUpdated: number, upsert: boolean) => void,
|
131 | ): void;
|
132 |
|
133 | /**
|
134 | * Update all docs matching query v1.8 signature.
|
135 | * For now, very naive implementation (recalculating the whole database)
|
136 | * @param options Optional options
|
137 | * options.multi If true, can update multiple documents (defaults to false)
|
138 | * options.upsert If true, document is inserted if the query doesn't match anything
|
139 | * @param cb Optional callback, signature: err,
|
140 | * numAffected,
|
141 | * affectedDocuments (when returnUpdatedDocs is set to true), obj or array
|
142 | * upsert (set to true if the update was in fact an upsert)
|
143 | *
|
144 | * @api private Use Datastore.update which has the same signature
|
145 | */
|
146 | update<T extends G>(
|
147 | query: any,
|
148 | updateQuery: any,
|
149 | options?: Nedb.UpdateOptions,
|
150 | cb?: (err: Error | null, numberOfUpdated: number, affectedDocuments: any, upsert: boolean) => void,
|
151 | ): void;
|
152 |
|
153 | /**
|
154 | * Remove all docs matching the query
|
155 | * For now very naive implementation (similar to update)
|
156 | * @param options Optional options
|
157 | * options.multi If true, can update multiple documents (defaults to false)
|
158 | * @param cb Optional callback, signature: err, numRemoved
|
159 | *
|
160 | * @api private Use Datastore.remove which has the same signature
|
161 | */
|
162 | remove(query: any, options: Nedb.RemoveOptions, cb?: (err: Error | null, n: number) => void): void;
|
163 | remove(query: any, cb?: (err: Error | null, n: number) => void): void;
|
164 |
|
165 | addListener(event: "compaction.done", listener: () => void): this;
|
166 | on(event: "compaction.done", listener: () => void): this;
|
167 | once(event: "compaction.done", listener: () => void): this;
|
168 | prependListener(event: "compaction.done", listener: () => void): this;
|
169 | prependOnceListener(event: "compaction.done", listener: () => void): this;
|
170 | removeListener(event: "compaction.done", listener: () => void): this;
|
171 | off(event: "compaction.done", listener: () => void): this;
|
172 | listeners(event: "compaction.done"): Array<() => void>;
|
173 | rawListeners(event: "compaction.done"): Array<() => void>;
|
174 | listenerCount(type: "compaction.done"): number;
|
175 | }
|
176 |
|
177 | declare namespace Nedb {
|
178 | interface Cursor<T> {
|
179 | sort(query: any): Cursor<T>;
|
180 | skip(n: number): Cursor<T>;
|
181 | limit(n: number): Cursor<T>;
|
182 | projection(query: any): Cursor<T>;
|
183 | exec(callback: (err: Error | null, documents: T[]) => void): void;
|
184 | }
|
185 |
|
186 | interface CursorCount {
|
187 | exec(callback: (err: Error | null, count: number) => void): void;
|
188 | }
|
189 |
|
190 | interface DataStoreOptions {
|
191 | filename?: string | undefined;
|
192 | inMemoryOnly?: boolean | undefined;
|
193 | nodeWebkitAppName?: boolean | undefined;
|
194 | autoload?: boolean | undefined;
|
195 |
|
196 | onload?(error: Error | null): any;
|
197 |
|
198 |
|
199 |
|
200 | afterSerialization?(line: string): string;
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | beforeDeserialization?(line: string): string;
|
209 |
|
210 |
|
211 | corruptAlertThreshold?: number | undefined;
|
212 |
|
213 |
|
214 | timestampData?: boolean | undefined;
|
215 | }
|
216 |
|
217 | |
218 |
|
219 |
|
220 |
|
221 | interface UpdateOptions {
|
222 | multi?: boolean | undefined;
|
223 | upsert?: boolean | undefined;
|
224 | returnUpdatedDocs?: boolean | undefined;
|
225 | }
|
226 |
|
227 | |
228 |
|
229 |
|
230 | interface RemoveOptions {
|
231 | multi?: boolean | undefined;
|
232 | }
|
233 |
|
234 | interface EnsureIndexOptions {
|
235 | fieldName: string;
|
236 | unique?: boolean | undefined;
|
237 | sparse?: boolean | undefined;
|
238 | expireAfterSeconds?: number | undefined;
|
239 | }
|
240 |
|
241 | interface Persistence {
|
242 | compactDatafile(): void;
|
243 | setAutocompactionInterval(interval: number): void;
|
244 | stopAutocompaction(): void;
|
245 | }
|
246 | }
|