index.d.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. declare namespace lmdb {
  2. export function open<V = any, K extends Key = Key>(path: string, options: RootDatabaseOptions): RootDatabase<V, K>
  3. export function open<V = any, K extends Key = Key>(options: RootDatabaseOptionsWithPath): RootDatabase<V, K>
  4. export function openAsClass<V = any, K extends Key = Key>(options: RootDatabaseOptionsWithPath): DatabaseClass<V, K>
  5. class Database<V = any, K extends Key = Key> {
  6. /**
  7. * Get the value stored by given id/key
  8. * @param id The key for the entry
  9. * @param options Additional options for the retrieval
  10. **/
  11. get(id: K, options?: GetOptions): V | undefined
  12. /**
  13. * Get the entry stored by given id/key, which includes both the value and the version number (if available)
  14. * @param id The key for the entry
  15. * @param options Additional options for the retrieval
  16. **/
  17. getEntry(id: K, options?: GetOptions): {
  18. value: V
  19. version?: number
  20. } | undefined
  21. /**
  22. * Get the value stored by given id/key in binary format, as a Buffer
  23. * @param id The key for the entry
  24. **/
  25. getBinary(id: K): Buffer | undefined
  26. /**
  27. * Get the value stored by given id/key in binary format, as a temporary Buffer.
  28. * This is faster, but the data is only valid until the next get operation (then it will be overwritten).
  29. * @param id The key for the entry
  30. **/
  31. getBinaryFast(id: K): Buffer | undefined
  32. /**
  33. * For random access structures and "fast" binary data, the underlying data is volatile,
  34. * and not safe to access after the next get. This function allows the data to
  35. * be copied as needed for safe access in the future. This can be used with buffers and random
  36. * access data structures.
  37. * @param data The data to be copied
  38. **/
  39. retain(data: any): any
  40. /**
  41. * Asynchronously fetch the values stored by the given ids and accesses all
  42. * pages to ensure that any hard page faults and disk I/O are performed
  43. * asynchronously in a separate thread. Once completed, synchronous
  44. * gets to the same entries will most likely be in memory and fast.
  45. * @param ids The keys for the entries to prefetch
  46. **/
  47. prefetch(ids: K[], callback?: Function): Promise<void>
  48. /**
  49. * Asynchronously get the values stored by the given ids and return the
  50. * values in array corresponding to the array of ids.
  51. * @param ids The keys for the entries to get
  52. **/
  53. getMany(ids: K[], callback?: (error: any, values: V[]) => any): Promise<(V | undefined)[]>
  54. /**
  55. * @experimental Asynchronously get a value by id.
  56. * @param ids
  57. * @param callback
  58. */
  59. getAsync(id: K, options?: GetOptions, callback?: (value: V) => any): Promise<(V | undefined)[]>
  60. /**
  61. * Store the provided value, using the provided id/key
  62. * @param id The key for the entry
  63. * @param value The value to store
  64. **/
  65. put(id: K, value: V): Promise<boolean>
  66. /**
  67. * Store the provided value, using the provided id/key and version number, and optionally the required
  68. * existing version
  69. * @param id The key for the entry
  70. * @param value The value to store
  71. * @param version The version number to assign to this entry
  72. * @param ifVersion If provided the put will only succeed if the previous version number matches this (atomically checked)
  73. **/
  74. put(id: K, value: V, version: number, ifVersion?: number): Promise<boolean>
  75. /**
  76. * Remove the entry with the provided id/key
  77. * @param id The key for the entry to remove
  78. **/
  79. remove(id: K): Promise<boolean>
  80. /**
  81. * Remove the entry with the provided id/key, conditionally based on the provided existing version number
  82. * @param id The key for the entry to remove
  83. * @param ifVersion If provided the remove will only succeed if the previous version number matches this (atomically checked)
  84. **/
  85. remove(id: K, ifVersion: number): Promise<boolean>
  86. /**
  87. * Remove the entry with the provided id/key and value (mainly used for dupsort databases) and optionally the required
  88. * existing version
  89. * @param id The key for the entry to remove
  90. * @param valueToRemove The value for the entry to remove
  91. **/
  92. remove(id: K, valueToRemove: V): Promise<boolean>
  93. /**
  94. * Synchronously store the provided value, using the provided id/key, will return after the data has been written.
  95. * @param id The key for the entry
  96. * @param value The value to store
  97. **/
  98. putSync(id: K, value: V): void
  99. /**
  100. * Synchronously store the provided value, using the provided id/key and version number
  101. * @param id The key for the entry
  102. * @param value The value to store
  103. * @param version The version number to assign to this entry
  104. **/
  105. putSync(id: K, value: V, version: number): void
  106. /**
  107. * Synchronously store the provided value, using the provided id/key and options
  108. * @param id The key for the entry
  109. * @param value The value to store
  110. * @param options The version number to assign to this entry
  111. **/
  112. putSync(id: K, value: V, options: PutOptions): void
  113. /**
  114. * Synchronously remove the entry with the provided id/key
  115. * existing version
  116. * @param id The key for the entry to remove
  117. **/
  118. removeSync(id: K): boolean
  119. /**
  120. * Synchronously remove the entry with the provided id/key and value (mainly used for dupsort databases)
  121. * existing version
  122. * @param id The key for the entry to remove
  123. * @param valueToRemove The value for the entry to remove
  124. **/
  125. removeSync(id: K, valueToRemove: V): boolean
  126. /**
  127. * Get all the values for the given key (for dupsort databases)
  128. * existing version
  129. * @param key The key for the entry to remove
  130. * @param options The options for the iterator
  131. **/
  132. getValues(key: K, options?: RangeOptions): RangeIterable<V>
  133. /**
  134. * Get the count of all the values for the given key (for dupsort databases)
  135. * existing version
  136. * @param options The options for the range/iterator
  137. **/
  138. getValuesCount(key: K, options?: RangeOptions): number
  139. /**
  140. * Get all the unique keys for the given range
  141. * existing version
  142. * @param options The options for the range/iterator
  143. **/
  144. getKeys(options?: RangeOptions): RangeIterable<K>
  145. /**
  146. * Get the count of all the unique keys for the given range
  147. * existing version
  148. * @param options The options for the range/iterator
  149. **/
  150. getKeysCount(options?: RangeOptions): number
  151. /**
  152. * Get all the entries for the given range
  153. * existing version
  154. * @param options The options for the range/iterator
  155. **/
  156. getRange(options?: RangeOptions): RangeIterable<{ key: K, value: V, version?: number }>
  157. /**
  158. * Get the count of all the entries for the given range
  159. * existing version
  160. * @param options The options for the range/iterator
  161. **/
  162. getCount(options?: RangeOptions): number
  163. /**
  164. * @deprecated since version 2.0, use transaction() instead
  165. */
  166. transactionAsync<T>(action: () => T): T
  167. /**
  168. * Execute a transaction asynchronously, running all the actions within the action callback in the transaction,
  169. * and committing the transaction after the action callback completes.
  170. * existing version
  171. * @param action The function to execute within the transaction
  172. **/
  173. transaction<T>(action: () => T): Promise<T>
  174. /**
  175. * Execute a transaction synchronously, running all the actions within the action callback in the transaction,
  176. * and committing the transaction after the action callback completes.
  177. * existing version
  178. * @param action The function to execute within the transaction
  179. * @params flags Additional flags specifying transaction behavior, this is optional and defaults to abortable, synchronous commits that are flushed to disk before returning
  180. **/
  181. transactionSync<T>(action: () => T, flags?: TransactionFlags): T
  182. /**
  183. * Execute a transaction asynchronously, running all the actions within the action callback in the transaction,
  184. * and committing the transaction after the action callback completes.
  185. * existing version
  186. * @param action The function to execute within the transaction
  187. **/
  188. childTransaction<T>(action: () => T): Promise<T>
  189. /**
  190. * Returns the transaction id of the currently executing transaction. This is an integer that increments with each
  191. * transaction. This is only available inside transaction callbacks (for transactionSync or asynchronous transaction),
  192. * and does not provide access transaction ids for asynchronous put/delete methods (the 'aftercommit' method can be
  193. * used for that).
  194. */
  195. getWriteTxnId(): number
  196. /**
  197. * Returns the current transaction and marks it as in use. This can then be explicitly used for read operations
  198. * @returns The transaction object
  199. **/
  200. useReadTransaction(): Transaction
  201. /**
  202. * Execute a set of write operations that will all be batched together in next queued asynchronous transaction.
  203. * @param action The function to execute with a set of write operations.
  204. **/
  205. batch<T>(action: () => any): Promise<boolean>
  206. /**
  207. * Execute writes actions that are all conditionally dependent on the entry with the provided key having the provided
  208. * version number (checked atomically).
  209. * @param id Key of the entry to check
  210. * @param ifVersion The require version number of the entry for all actions to succeed
  211. * @param action The function to execute with actions that will be dependent on this condition
  212. **/
  213. ifVersion(id: K, ifVersion: number, action: () => any): Promise<boolean>
  214. /**
  215. * Execute writes actions that are all conditionally dependent on the entry with the provided key
  216. * not existing (checked atomically).
  217. * @param id Key of the entry to check
  218. * @param action The function to execute with actions that will be dependent on this condition
  219. **/
  220. ifNoExists(id: K, action: () => any): Promise<boolean>
  221. /**
  222. * Check if an entry for the provided key exists
  223. * @param id Key of the entry to check
  224. */
  225. doesExist(key: K): boolean
  226. /**
  227. * Check if an entry for the provided key/value exists
  228. * @param id Key of the entry to check
  229. * @param value Value of the entry to check (can be undefined to check for existence of the entry)
  230. * @param options Options for existence check (can include transaction)
  231. */
  232. doesExist(key: K, value: V, options?: GetOptions): boolean
  233. /**
  234. * Check if an entry for the provided key exists with the expected version
  235. * @param id Key of the entry to check
  236. * @param version Expected version
  237. */
  238. doesExist(key: K, version: number): boolean
  239. /**
  240. * @deprecated since version 2.0, use drop() or dropSync() instead
  241. */
  242. deleteDB(): Promise<void>
  243. /**
  244. * Delete this database/store (asynchronously).
  245. **/
  246. drop(): Promise<void>
  247. /**
  248. * Synchronously delete this database/store.
  249. **/
  250. dropSync(): void
  251. /**
  252. * @deprecated since version 2.0, use clearAsync() or clearSync() instead
  253. */
  254. clear(): Promise<void>
  255. /**
  256. * Asynchronously clear all the entries from this database/store.
  257. **/
  258. clearAsync(): Promise<void>
  259. /**
  260. * Synchronously clear all the entries from this database/store.
  261. **/
  262. clearSync(): void
  263. /** A promise-like object that resolves when previous writes have been committed. */
  264. committed: Promise<boolean>
  265. /** A promise-like object that resolves when previous writes have been committed and fully flushed/synced to disk/storage. */
  266. flushed: Promise<boolean>
  267. /**
  268. * Check the reader locks and remove any stale reader locks. Returns the number of stale locks that were removed.
  269. **/
  270. readerCheck(): number
  271. /**
  272. * Returns a string that describes all the current reader locks, useful for debugging if reader locks aren't being removed.
  273. **/
  274. readerList(): string
  275. /**
  276. * Returns statistics about the current database
  277. **/
  278. getStats(): {}
  279. /**
  280. * Explicitly force the read transaction to reset to the latest snapshot/version of the database
  281. **/
  282. resetReadTxn(): void
  283. /**
  284. * Make a snapshot copy of the current database at the indicated path
  285. * @param path Path to store the backup
  286. * @param compact Apply compaction while making the backup (slower and smaller)
  287. **/
  288. backup(path: string, compact: boolean): Promise<void>
  289. /**
  290. * Close the current database.
  291. **/
  292. close(): Promise<void>
  293. /**
  294. * Add event listener
  295. */
  296. on(event: 'beforecommit' | 'aftercommit', callback: (event: any) => void): void
  297. }
  298. /* A constant that can be returned from a transaction to indicate that the transaction should be aborted */
  299. export const ABORT: {};
  300. /* A constant that can be returned in RangeIterable#map function to skip (filter out) the current value */
  301. export const SKIP: {};
  302. /* A constant that can be used as a conditional versions for put and ifVersion to indicate that the write should conditional on the key/entry existing */
  303. export const IF_EXISTS: number;
  304. class RootDatabase<V = any, K extends Key = Key> extends Database<V, K> {
  305. /**
  306. * Open a database store using the provided options.
  307. **/
  308. openDB<OV = V, OK extends Key = K>(options?: DatabaseOptions & { name: string }): Database<OV, OK>
  309. /**
  310. * Open a database store using the provided options.
  311. **/
  312. openDB<OV = V, OK extends Key = K>(dbName: string, dbOptions: DatabaseOptions): Database<OV, OK>
  313. }
  314. class DatabaseClass<V = any, K extends Key = Key> {
  315. new(name: string | null, options: DatabaseOptions): Database<V, K>
  316. }
  317. type Key = Key[] | string | symbol | number | boolean | Uint8Array;
  318. interface DatabaseOptions {
  319. name?: string
  320. cache?: boolean | {}
  321. compression?: boolean | CompressionOptions
  322. encoding?: 'msgpack' | 'json' | 'string' | 'binary' | 'ordered-binary'
  323. sharedStructuresKey?: Key
  324. useVersions?: boolean
  325. keyEncoding?: 'uint32' | 'binary' | 'ordered-binary'
  326. dupSort?: boolean
  327. strictAsyncOrder?: boolean
  328. }
  329. interface RootDatabaseOptions extends DatabaseOptions {
  330. /** The maximum number of databases to be able to open (there is some extra overhead if this is set very high).*/
  331. maxDbs?: number
  332. /** Set a longer delay (in milliseconds) to wait longer before committing writes to increase the number of writes per transaction (higher latency, but more efficient) **/
  333. commitDelay?: number
  334. /**
  335. * This can be used to specify the initial amount of how much virtual memory address space (in bytes) to allocate for mapping to the database files.
  336. * Setting a map size will typically disable remapChunks by default unless the size is larger than appropriate for the OS. Different OSes have different allocation limits.
  337. **/
  338. mapSize?: number
  339. /**
  340. * This defines the page size of the database. This defaults to the default page size of the OS (usually 4,096, except on MacOS with M-series, which is 16,384 bytes).
  341. * You may want to consider setting this to 8,192 for databases larger than available memory (and moreso if you have range queries) or 4,096 for databases that can mostly cache in memory.
  342. * Note that this only effects the page size of new databases (does not affect existing databases). */
  343. pageSize?: number
  344. /** This enables committing transactions where LMDB waits for a transaction to be fully flushed to disk after the transaction has been committed and defaults to being enabled on non-Windows OSes. This option is discussed in more detail below. */
  345. overlappingSync?: boolean
  346. /** Resolve asynchronous operations when commits are finished and visible and include a separate promise for when a commit is flushed to disk, as a flushed property on the commit promise. Note that you can alternately use the flushed property on the database. */
  347. separateFlushed?: boolean
  348. /**
  349. * This a flag to specify if dynamic memory mapping should be used. Enabling this generally makes read operations a little bit slower, but frees up more mapped memory, making it friendlier to other applications.
  350. * This is enabled by default on 32-bit operating systems (which require this to go beyond 4GB database size) if mapSize is not specified, otherwise it is disabled by default.
  351. **/
  352. remapChunks?: boolean
  353. /** This provides a small performance boost (when not using useWritemap) for writes, by skipping zero'ing out malloc'ed data, but can leave application data in unused portions of the database. This is recommended unless there are concerns of database files being accessible. */
  354. noMemInit?: boolean
  355. /** Use writemaps, discouraged at this. This improves performance by reducing malloc calls, but it is possible for a stray pointer to corrupt data. */
  356. useWritemap?: boolean
  357. /** Treat path as a filename instead of directory (this is the default if the path appears to end with an extension and has '.' in it) */
  358. noSubdir?: boolean
  359. /**
  360. * Does not explicitly flush data to disk at all. This can be useful for temporary databases where durability/integrity is not necessary, and can significantly improve write performance that is I/O bound.
  361. * However, we discourage this flag for data that needs integrity and durability in storage, since it can result in data loss/corruption if the computer crashes.
  362. **/
  363. noSync?: boolean
  364. /** This isn't as dangerous as `noSync`, but doesn't improve performance much either. */
  365. noMetaSync?: boolean
  366. /** Self-descriptive */
  367. readOnly?: boolean
  368. /** The maximum number of concurrent read transactions (readers) to be able to open ([more information](http://www.lmdb.tech/doc/group__mdb.html#gae687966c24b790630be2a41573fe40e2)). */
  369. maxReaders?: number
  370. /** This enables encryption, and the provided value is the key that is used for encryption. This may be a buffer or string, but must be 32 bytes/characters long. This uses the Chacha8 cipher for fast and secure on-disk encryption of data. */
  371. encryptionKey?: string | Buffer
  372. /**
  373. * This is enabled by default and will ensure that all asynchronous write operations performed in the same event turn will be batched together into the same transaction.
  374. * Disabling this allows lmdb-js to commit a transaction at any time, and asynchronous operations will only be guaranteed to be in the same transaction if explicitly batched together (with transaction, batch, ifVersion).
  375. * If this is disabled (set to false), you can control how many writes can occur before starting a transaction with txnStartThreshold (allow a transaction will still be started at the next event turn if the threshold is not met).
  376. * Disabling event turn batching (and using lower txnStartThreshold values) can facilitate a faster response time to write operations. txnStartThreshold defaults to 5.
  377. **/
  378. eventTurnBatching?: boolean
  379. /** This is the current encoder instance that is being used to serialize data */
  380. encoder?: any
  381. /** This is the current decoder instance that is being used to deserialize data */
  382. decoder?: any
  383. }
  384. interface RootDatabaseOptionsWithPath extends RootDatabaseOptions {
  385. path?: string
  386. }
  387. interface CompressionOptions {
  388. threshold?: number
  389. dictionary?: Buffer
  390. }
  391. interface GetOptions {
  392. transaction?: Transaction
  393. }
  394. interface RangeOptions {
  395. /** Starting key for a range **/
  396. start?: Key
  397. /** Ending key for a range **/
  398. end?: Key
  399. /** Iterate through the entries in reverse order **/
  400. reverse?: boolean
  401. /** Include version numbers in each entry returned **/
  402. versions?: boolean
  403. /** The maximum number of entries to return **/
  404. limit?: number
  405. /** The number of entries to skip **/
  406. offset?: number
  407. /** Use a snapshot of the database from when the iterator started **/
  408. snapshot?: boolean
  409. /** Use the provided transaction for this range query */
  410. transaction?: Transaction
  411. }
  412. interface PutOptions {
  413. /* Append to the database using MDB_APPEND, which can be faster */
  414. append?: boolean
  415. /* Append to a dupsort database using MDB_APPENDDUP, which can be faster */
  416. appendDup?: boolean
  417. /* Perform put with MDB_NOOVERWRITE which will fail if the entry for the key already exists */
  418. noOverwrite?: boolean
  419. /* Perform put with MDB_NODUPDATA which will fail if the entry for the key/value already exists */
  420. noDupData?: boolean
  421. /* The version of the entry to set */
  422. version?: number
  423. }
  424. export enum TransactionFlags {
  425. /* Indicates that the transaction needs to be abortable */
  426. ABORTABLE = 1,
  427. /* Indicates that the transaction needs to be committed before returning */
  428. SYNCHRONOUS_COMMIT = 2,
  429. /* Indicates that the function can return before the transaction has been flushed to disk */
  430. NO_SYNC_FLUSH = 0x10000
  431. }
  432. class RangeIterable<T> implements Iterable<T> {
  433. map<U>(callback: (entry: T) => U): RangeIterable<U>
  434. flatMap<U>(callback: (entry: T) => U[]): RangeIterable<U>
  435. slice(start: number, end: number): RangeIterable<T>
  436. filter(callback: (entry: T) => any): RangeIterable<T>
  437. [Symbol.iterator]() : Iterator<T>
  438. forEach(callback: (entry: T) => any): void
  439. mapError<U>(callback: (error: Error) => U): RangeIterable<U>
  440. onDone?: Function
  441. asArray: T[]
  442. }
  443. class Transaction {
  444. /**
  445. * When there is no more need for the transaction and it can be closed.
  446. */
  447. done(): void
  448. }
  449. export function getLastVersion(): number
  450. export function compareKeys(a: Key, b: Key): number
  451. class Binary {}
  452. /* Wrap a Buffer/Uint8Array for direct assignment as a value bypassing any encoding, for put (and doesExist) operations.
  453. */
  454. export function asBinary(buffer: Uint8Array): Binary
  455. /* Indicates if V8 accelerated functions are enabled. If this is false, some functions will be a little slower, and you may want to npm install --build-from-source to enable maximum performance */
  456. export let v8AccelerationEnabled: boolean
  457. /* Return database augmented with methods to better conform to levelup */
  458. export function levelup(database: Database): Database
  459. }
  460. export = lmdb