1/************************************************************************************************** 2 * EJDB2 3 * 4 * MIT License 5 * 6 * Copyright (c) 2012-2021 Softmotions Ltd <info@softmotions.com> 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a copy 9 * of this software and associated documentation files (the "Software"), to deal 10 * in the Software without restriction, including without limitation the rights 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 * copies of the Software, and to permit persons to whom the Software is 13 * furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included in all 16 * copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 * SOFTWARE. 25 *************************************************************************************************/ 26 27/// <reference types="node"/> 28 29declare namespace ejdb2_node { 30 export type Placeholder = string | number; 31 32 /** 33 * EJDB2 Error helpers. 34 */ 35 export class JBE { 36 /** 37 * Returns `true` if given error [err] is `IWKV_ERROR_NOTFOUND` 38 * @param err 39 */ 40 static isNotFound(err: any): boolean; 41 42 /** 43 * Returns `true` if given errror [err] is `JQL_ERROR_QUERY_PARSE` 44 * @param {Error} err 45 * @return {boolean} 46 */ 47 static isInvalidQuery(err: any): boolean; 48 } 49 50 /** 51 * EJDB document. 52 */ 53 interface JBDOC { 54 /** 55 * Document identifier 56 */ 57 id: number; 58 59 /** 60 * Document JSON object 61 */ 62 json: any; 63 64 /** 65 * String represen 66 */ 67 toString(): string; 68 } 69 70 interface JBDOCStream { 71 [Symbol.asyncIterator](): AsyncIterableIterator<JBDOC>; 72 } 73 74 /** 75 * Query execution options. 76 */ 77 interface QueryOptions { 78 /** 79 * Overrides `limit` encoded in query text 80 */ 81 limit?: number; 82 83 /** 84 * Calback used to get query execution log. 85 */ 86 explainCallback?: (log: string) => void; 87 } 88 89 /** 90 * EJDB Query. 91 */ 92 class JQL { 93 /** 94 * Database to which query attached. 95 */ 96 readonly db: EJDB2; 97 98 /** 99 * Get `limit` value used by query. 100 */ 101 readonly limit: number; 102 103 /** 104 * Executes a query and returns a 105 * readable stream of matched documents. 106 * 107 */ 108 stream(opts?: QueryOptions): JBDOCStream; 109 110 /** 111 * Executes this query and waits its completion. 112 */ 113 completionPromise(opts?: QueryOptions): Promise<void>; 114 115 /** 116 * Returns a scalar integer value as result of query execution. 117 * Eg.: A count query: `/... | count` 118 */ 119 scalarInt(opts?: QueryOptions): Promise<number>; 120 121 /** 122 * Returns result set as a list. 123 * Use it with caution on large data sets. 124 */ 125 list(opts?: QueryOptions): Promise<Array<JBDOC>>; 126 127 /** 128 * Collects up to [n] documents from result set into array. 129 */ 130 firstN(n: number, opts?: QueryOptions): Promise<Array<JBDOC>>; 131 132 /** 133 * Returns a first record in result set. 134 * If record is not found promise with `undefined` will be returned. 135 */ 136 first(opts?: QueryOptions): Promise<JBDOC | undefined>; 137 138 /** 139 * Set [json] at the specified [placeholder]. 140 */ 141 setJSON(placeholder: Placeholder, val: object | string): JQL; 142 143 /** 144 * Set [regexp] string at the specified [placeholder]. 145 */ 146 setRegexp(placeholder: Placeholder, val: string): JQL; 147 148 /** 149 * Set number [val] at the specified [placeholder]. 150 */ 151 setNumber(placeholder: Placeholder, val: number): JQL; 152 153 /** 154 * Set boolean [val] at the specified [placeholder]. 155 */ 156 setBoolean(placeholder: Placeholder, val: boolean): JQL; 157 158 /** 159 * Set string [val] at the specified [placeholder]. 160 */ 161 setString(placeholder: Placeholder, val: string): JQL; 162 163 /** 164 * Set `null` at the specified [placeholder]. 165 */ 166 setNull(placeholder: Placeholder): JQL; 167 } 168 169 interface OpenOptions { 170 /** 171 * Open databas in read-only mode. 172 */ 173 readonly?: boolean; 174 175 /** 176 * Truncate database file on open. 177 */ 178 truncate?: boolean; 179 180 /** 181 * Enable WAL. Default: true. 182 */ 183 wal_enabled?: boolean; 184 185 /** 186 * Check CRC32 sum for every WAL checkpoint. 187 * Default: false. 188 */ 189 wal_check_crc_on_checkpoint?: boolean; 190 191 /** 192 * Size of checkpoint buffer in bytes. 193 * Default: 1Gb. Android: 64Mb 194 */ 195 wal_checkpoint_buffer_sz?: number; 196 197 /** 198 * WAL buffer size in bytes. 199 * Default: 8Mb. Android: 2Mb. 200 */ 201 wal_wal_buffer_sz?: number; 202 203 /** 204 * Checkpoint timeout in seconds. 205 * Default: 300. Android: 60. 206 */ 207 wal_checkpoint_timeout_sec?: number; 208 209 /** 210 * Savepoint timeout in secods. 211 * Default: 10. 212 */ 213 wal_savepoint_timeout_sec?: number; 214 215 /** 216 * Initial size of buffer in bytes used to process/store document on queries. 217 * Preferable average size of document. 218 * Default: 65536. Minimal: 16384. 219 */ 220 document_buffer_sz?: number; 221 222 /** 223 * Max sorting buffer size in bytes. 224 * If exceeded, an overflow temp file for data will be created. 225 * Default: 16777216. Minimal: 1048576 226 */ 227 sort_buffer_sz?: number; 228 229 /** 230 * Enable HTTP/Websocket endpoint. 231 */ 232 http_enabled?: boolean; 233 234 /** 235 * Server access token matched to 'X-Access-Token' HTTP header value. 236 */ 237 http_access_token?: string; 238 239 /** 240 * Server ip address to bind. 241 */ 242 http_bind?: string; 243 244 /** 245 * Max HTTP/WS API document body size. 246 * Default: 67108864. Minimal: 524288. 247 */ 248 http_max_body_size?: number; 249 250 /** 251 * HTTP port to listen. 252 */ 253 http_port?: number; 254 255 /** 256 * Allow anonymous read request. 257 */ 258 http_read_anon?: boolean; 259 } 260 261 /** 262 * Collection index descriptor. 263 */ 264 interface IndexDescriptor { 265 /** 266 * rfc6901 JSON pointer to indexed field. 267 */ 268 ptr: string; 269 270 /** 271 * Index mode as bit mask: 272 * 273 * - 0x01 EJDB_IDX_UNIQUE Index is unique 274 * - 0x04 EJDB_IDX_STR Index for JSON string field value type 275 * - 0x08 EJDB_IDX_I64 Index for 8 bytes width signed integer field values 276 * - 0x10 EJDB_IDX_F64 Index for 8 bytes width signed floating point field values. 277 */ 278 mode: number; 279 280 /** 281 * Index flags. See iwkv.h#iwdb_flags_t 282 */ 283 idbf: number; 284 285 /** 286 * Internal index database identifier. 287 */ 288 dbid: number; 289 290 /** 291 * Number of indexed records. 292 */ 293 rnum: number; 294 } 295 296 /** 297 * Collection descriptor. 298 */ 299 interface CollectionDescriptor { 300 /** 301 * Name of collection. 302 */ 303 name: string; 304 305 /** 306 * Internal database identifier. 307 */ 308 dbid: number; 309 310 /** 311 * Number of documents stored in collection. 312 */ 313 rnum: number; 314 315 /** 316 * List of collection indexes. 317 */ 318 indexes: Array<IndexDescriptor>; 319 } 320 321 interface EJDB2Info { 322 /** 323 * Database engine version string. 324 * Eg. "2.0.29" 325 */ 326 version: string; 327 328 /** 329 * Database file path. 330 */ 331 file: string; 332 333 /** 334 * Database file size in bytes. 335 */ 336 size: number; 337 338 /** 339 * List of database collections. 340 */ 341 collections: Array<CollectionDescriptor>; 342 } 343 344 /** 345 * EJDB2 Node.js wrapper. 346 */ 347 export class EJDB2 { 348 /** 349 * Open databse instance. 350 * @param path Database file path 351 * @param opts Options 352 */ 353 static open(path: String, opts?: OpenOptions): Promise<EJDB2>; 354 355 /** 356 * Closes this database instance. 357 */ 358 close(): Promise<void>; 359 360 /** 361 * Saves [json] document under specified [id] or create a document 362 * with new generated `id`. Returns promise holding actual document `id`. 363 */ 364 put(collection: String, json: object | string, id?: number): Promise<number>; 365 366 /** 367 * Apply rfc6902/rfc7386 JSON [patch] to the document identified by [id]. 368 */ 369 patch(collection: string, json: object | string, id: number): Promise<void>; 370 371 /** 372 * Apply JSON merge patch (rfc7396) to the document identified by `id` or 373 * insert new document under specified `id`. 374 */ 375 patchOrPut(collection: string, json: object | string, id: number): Promise<void>; 376 377 /** 378 * Get json body of document identified by [id] and stored in [collection]. 379 * 380 * If document with given `id` is not found then `Error` will be thrown. 381 * Not found error can be detected by {@link JBE.isNotFound} 382 */ 383 get(collection: string, id: number): Promise<object>; 384 385 /** 386 * Get json body of document identified by [id] and stored in [collection]. 387 * 388 * If document with given `id` is not found then `null` will be resoved. 389 */ 390 getOrNull(collection: string, id: number): Promise<object|null>; 391 392 /** 393 * Get json body with database metadata. 394 */ 395 info(): Promise<EJDB2Info>; 396 397 /** 398 * Removes document idenfied by [id] from [collection]. 399 * 400 * If document with given `id` is not found then `Error` will be thrown. 401 * Not found error can be detected by {@link JBE.isNotFound} 402 */ 403 del(collection: string, id: number): Promise<void>; 404 405 /** 406 * Renames collection 407 */ 408 renameCollection(oldCollectionName: string, newCollectionName: string): Promise<void>; 409 410 /** 411 * Ensures json document database index specified by [path] json pointer to string data type. 412 */ 413 ensureStringIndex(collection: string, path: string, unique?: boolean): Promise<void>; 414 415 /** 416 * Removes specified database index. 417 */ 418 removeStringIndex(collection: string, path: string, unique?: boolean): Promise<void>; 419 420 /** 421 * Ensures json document database index specified by [path] json pointer to integer data type. 422 */ 423 ensureIntIndex(collection: string, path: string, unique?: boolean): Promise<void>; 424 425 /** 426 * Removes specified database index. 427 */ 428 removeIntIndex(collection: string, path: string, unique?: boolean): Promise<void>; 429 430 /** 431 * Ensures json document database index specified by [path] json pointer to floating point data type. 432 */ 433 ensureFloatIndex(collection: string, path: string, unique?: boolean): Promise<void>; 434 435 /** 436 * Removes specified database index. 437 */ 438 removeFloatIndex(collection: string, path: string, unique?: boolean): Promise<void>; 439 440 /** 441 * Removes database [collection]. 442 */ 443 removeCollection(collection: string): Promise<void>; 444 445 /** 446 * Create instance of [query] specified for [collection]. 447 * If [collection] is not specified a [query] spec must contain collection name, 448 * eg: `@mycollection/[foo=bar]` 449 */ 450 createQuery(query: string, collection?: string): JQL; 451 452 /** 453 * Creates an online database backup image and copies it into the specified [fileName]. 454 * During online backup phase read/write database operations are allowed and not 455 * blocked for significant amount of time. Returns promise with backup 456 * finish time as number of milliseconds since epoch. 457 */ 458 onlineBackup(fileName: string): Promise<number>; 459 } 460} 461 462export = ejdb2_node; 463