DBWrapper.d.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import '../_version.js';
  2. declare type IDBObjectStoreMethods = 'get' | 'count' | 'getKey' | 'getAll' | 'getAllKeys' | 'add' | 'put' | 'clear' | 'delete';
  3. declare type Query = IDBValidKey | IDBKeyRange | null;
  4. interface DBWrapperOptions {
  5. onupgradeneeded?: (event: IDBVersionChangeEvent) => any;
  6. onversionchange?: (event: IDBVersionChangeEvent) => any;
  7. }
  8. interface GetAllMatchingOptions {
  9. index?: string;
  10. query?: Query;
  11. direction?: IDBCursorDirection;
  12. count?: number;
  13. includeKeys?: boolean;
  14. }
  15. /**
  16. * A class that wraps common IndexedDB functionality in a promise-based API.
  17. * It exposes all the underlying power and functionality of IndexedDB, but
  18. * wraps the most commonly used features in a way that's much simpler to use.
  19. *
  20. * @private
  21. */
  22. export declare class DBWrapper {
  23. private readonly _name;
  24. private readonly _version;
  25. private readonly _onupgradeneeded?;
  26. private readonly _onversionchange;
  27. private _db;
  28. add?: Function;
  29. clear?: Function;
  30. count?: Function;
  31. delete?: Function;
  32. get?: Function;
  33. put?: Function;
  34. OPEN_TIMEOUT?: number;
  35. /**
  36. * @param {string} name
  37. * @param {number} version
  38. * @param {Object=} [callback]
  39. * @param {!Function} [callbacks.onupgradeneeded]
  40. * @param {!Function} [callbacks.onversionchange] Defaults to
  41. * DBWrapper.prototype._onversionchange when not specified.
  42. * @private
  43. */
  44. constructor(name: string, version: number, { onupgradeneeded, onversionchange, }?: DBWrapperOptions);
  45. /**
  46. * Returns the IDBDatabase instance (not normally needed).
  47. * @return {IDBDatabase|undefined}
  48. *
  49. * @private
  50. */
  51. get db(): IDBDatabase | null;
  52. /**
  53. * Opens a connected to an IDBDatabase, invokes any onupgradedneeded
  54. * callback, and added an onversionchange callback to the database.
  55. *
  56. * @return {IDBDatabase}
  57. * @private
  58. */
  59. open(): Promise<this | undefined>;
  60. /**
  61. * Polyfills the native `getKey()` method. Note, this is overridden at
  62. * runtime if the browser supports the native method.
  63. *
  64. * @param {string} storeName
  65. * @param {*} query
  66. * @return {Array}
  67. * @private
  68. */
  69. getKey(storeName: string, query: Query): Promise<IDBValidKey>;
  70. /**
  71. * Polyfills the native `getAll()` method. Note, this is overridden at
  72. * runtime if the browser supports the native method.
  73. *
  74. * @param {string} storeName
  75. * @param {*} query
  76. * @param {number} count
  77. * @return {Array}
  78. * @private
  79. */
  80. getAll(storeName: string, query?: Query, count?: number): Promise<any[]>;
  81. /**
  82. * Polyfills the native `getAllKeys()` method. Note, this is overridden at
  83. * runtime if the browser supports the native method.
  84. *
  85. * @param {string} storeName
  86. * @param {*} query
  87. * @param {number} count
  88. * @return {Array}
  89. * @private
  90. */
  91. getAllKeys(storeName: string, query: Query, count: number): Promise<IDBValidKey[]>;
  92. /**
  93. * Supports flexible lookup in an object store by specifying an index,
  94. * query, direction, and count. This method returns an array of objects
  95. * with the signature .
  96. *
  97. * @param {string} storeName
  98. * @param {Object} [opts]
  99. * @param {string} [opts.index] The index to use (if specified).
  100. * @param {*} [opts.query]
  101. * @param {IDBCursorDirection} [opts.direction]
  102. * @param {number} [opts.count] The max number of results to return.
  103. * @param {boolean} [opts.includeKeys] When true, the structure of the
  104. * returned objects is changed from an array of values to an array of
  105. * objects in the form {key, primaryKey, value}.
  106. * @return {Array}
  107. * @private
  108. */
  109. getAllMatching(storeName: string, { index, query, // IE/Edge errors if query === `undefined`.
  110. direction, count, includeKeys, }?: GetAllMatchingOptions): Promise<Array<IDBCursor | any>>;
  111. /**
  112. * Accepts a list of stores, a transaction type, and a callback and
  113. * performs a transaction. A promise is returned that resolves to whatever
  114. * value the callback chooses. The callback holds all the transaction logic
  115. * and is invoked with two arguments:
  116. * 1. The IDBTransaction object
  117. * 2. A `done` function, that's used to resolve the promise when
  118. * when the transaction is done, if passed a value, the promise is
  119. * resolved to that value.
  120. *
  121. * @param {Array<string>} storeNames An array of object store names
  122. * involved in the transaction.
  123. * @param {string} type Can be `readonly` or `readwrite`.
  124. * @param {!Function} callback
  125. * @return {*} The result of the transaction ran by the callback.
  126. * @private
  127. */
  128. transaction(storeNames: string | string[], type: IDBTransactionMode, callback: (txn: IDBTransaction, done: Function) => void): Promise<any>;
  129. /**
  130. * Delegates async to a native IDBObjectStore method.
  131. *
  132. * @param {string} method The method name.
  133. * @param {string} storeName The object store name.
  134. * @param {string} type Can be `readonly` or `readwrite`.
  135. * @param {...*} args The list of args to pass to the native method.
  136. * @return {*} The result of the transaction.
  137. * @private
  138. */
  139. _call(method: IDBObjectStoreMethods, storeName: string, type: IDBTransactionMode, ...args: any[]): Promise<any>;
  140. /**
  141. * Closes the connection opened by `DBWrapper.open()`. Generally this method
  142. * doesn't need to be called since:
  143. * 1. It's usually better to keep a connection open since opening
  144. * a new connection is somewhat slow.
  145. * 2. Connections are automatically closed when the reference is
  146. * garbage collected.
  147. * The primary use case for needing to close a connection is when another
  148. * reference (typically in another tab) needs to upgrade it and would be
  149. * blocked by the current, open connection.
  150. *
  151. * @private
  152. */
  153. close(): void;
  154. }
  155. export {};