|
| 1 | +[dt-utils](../globals.md) / IndexedDB |
| 2 | + |
| 3 | +# Class: ~~IndexedDB~~ |
| 4 | + |
| 5 | +Defined in: [indexedDB/index.ts:37](https://github.com/jin-sir/dt-utils/blob/c80bde9fd6bdabc77e6c76035f655925caf5e8af/src/indexedDB/index.ts#L37) |
| 6 | + |
| 7 | +IndexedDB |
| 8 | + |
| 9 | +## Deprecated |
| 10 | + |
| 11 | +## Description |
| 12 | + |
| 13 | +这个类为 IndexedDB 提供了一个包装器,IndexedDB 是一个用于客户端存储大量结构化数据(包括文件/二进制对象)的 API。 |
| 14 | +它允许你以结构化格式存储和检索数据,并使用各种方法查询和操作这些数据。 |
| 15 | + |
| 16 | +## Example |
| 17 | + |
| 18 | +```typescript |
| 19 | +import { LocalIndexedDB } from 'dt-utils'; |
| 20 | + |
| 21 | +// 初始化数据库 |
| 22 | +const db = new LocalIndexedDB('userDB', 1, 'users'); |
| 23 | +await db.open(); |
| 24 | + |
| 25 | +// 添加数据 |
| 26 | +await db.add('user1', { name: 'John', age: 30 }); |
| 27 | + |
| 28 | +// 获取数据 |
| 29 | +const user = await db.get('user1'); |
| 30 | + |
| 31 | +// 更新数据 |
| 32 | +await db.set('user1', { name: 'John', age: 31 }); |
| 33 | + |
| 34 | +// 删除数据 |
| 35 | +await db.delete('user1'); |
| 36 | + |
| 37 | +// 清空所有数据 |
| 38 | +await db.clear(); |
| 39 | +``` |
| 40 | + |
| 41 | +## See |
| 42 | + |
| 43 | + - [IndexedDB 使用指南](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB|MDN) |
| 44 | + - [https://caniuse.com/#feat=indexeddb\|浏览器兼容性](https://caniuse.com/#feat=indexeddb|浏览器兼容性) |
| 45 | + |
| 46 | +## Constructors |
| 47 | + |
| 48 | +### Constructor |
| 49 | + |
| 50 | +> **new IndexedDB**(`database`, `version`, `storeName`, `openLog`): `IndexedDB` |
| 51 | +
|
| 52 | +Defined in: [indexedDB/index.ts:51](https://github.com/jin-sir/dt-utils/blob/c80bde9fd6bdabc77e6c76035f655925caf5e8af/src/indexedDB/index.ts#L51) |
| 53 | + |
| 54 | +Constructor for a new indexedDB object |
| 55 | + |
| 56 | +#### Parameters |
| 57 | + |
| 58 | +##### database |
| 59 | + |
| 60 | +`string` |
| 61 | + |
| 62 | +Database name |
| 63 | + |
| 64 | +##### version |
| 65 | + |
| 66 | +`number` |
| 67 | + |
| 68 | +Database version |
| 69 | + |
| 70 | +##### storeName |
| 71 | + |
| 72 | +`string` |
| 73 | + |
| 74 | +Store object name |
| 75 | + |
| 76 | +##### openLog |
| 77 | + |
| 78 | +`boolean` = `false` |
| 79 | + |
| 80 | +Whether to log indexedDB changes |
| 81 | + |
| 82 | +#### Returns |
| 83 | + |
| 84 | +`IndexedDB` |
| 85 | + |
| 86 | +## Methods |
| 87 | + |
| 88 | +### ~~add()~~ |
| 89 | + |
| 90 | +> **add**\<`T`\>(`key`, `value`): `Promise`\<`IDBRequest`\<`any`\>\> |
| 91 | +
|
| 92 | +Defined in: [indexedDB/index.ts:149](https://github.com/jin-sir/dt-utils/blob/c80bde9fd6bdabc77e6c76035f655925caf5e8af/src/indexedDB/index.ts#L149) |
| 93 | + |
| 94 | +#### Type Parameters |
| 95 | + |
| 96 | +##### T |
| 97 | + |
| 98 | +`T` |
| 99 | + |
| 100 | +#### Parameters |
| 101 | + |
| 102 | +##### key |
| 103 | + |
| 104 | +`string` |
| 105 | + |
| 106 | +##### value |
| 107 | + |
| 108 | +`T` |
| 109 | + |
| 110 | +#### Returns |
| 111 | + |
| 112 | +`Promise`\<`IDBRequest`\<`any`\>\> |
| 113 | + |
| 114 | +*** |
| 115 | + |
| 116 | +### ~~clear()~~ |
| 117 | + |
| 118 | +> **clear**(): `Promise`\<`IDBRequest`\<`any`\>\> |
| 119 | +
|
| 120 | +Defined in: [indexedDB/index.ts:171](https://github.com/jin-sir/dt-utils/blob/c80bde9fd6bdabc77e6c76035f655925caf5e8af/src/indexedDB/index.ts#L171) |
| 121 | + |
| 122 | +#### Returns |
| 123 | + |
| 124 | +`Promise`\<`IDBRequest`\<`any`\>\> |
| 125 | + |
| 126 | +*** |
| 127 | + |
| 128 | +### ~~delete()~~ |
| 129 | + |
| 130 | +> **delete**(`key`): `Promise`\<`IDBRequest`\<`any`\>\> |
| 131 | +
|
| 132 | +Defined in: [indexedDB/index.ts:166](https://github.com/jin-sir/dt-utils/blob/c80bde9fd6bdabc77e6c76035f655925caf5e8af/src/indexedDB/index.ts#L166) |
| 133 | + |
| 134 | +#### Parameters |
| 135 | + |
| 136 | +##### key |
| 137 | + |
| 138 | +`string` |
| 139 | + |
| 140 | +#### Returns |
| 141 | + |
| 142 | +`Promise`\<`IDBRequest`\<`any`\>\> |
| 143 | + |
| 144 | +*** |
| 145 | + |
| 146 | +### ~~get()~~ |
| 147 | + |
| 148 | +> **get**\<`T`\>(`key`): `Promise`\<`T`\> |
| 149 | +
|
| 150 | +Defined in: [indexedDB/index.ts:160](https://github.com/jin-sir/dt-utils/blob/c80bde9fd6bdabc77e6c76035f655925caf5e8af/src/indexedDB/index.ts#L160) |
| 151 | + |
| 152 | +#### Type Parameters |
| 153 | + |
| 154 | +##### T |
| 155 | + |
| 156 | +`T` |
| 157 | + |
| 158 | +#### Parameters |
| 159 | + |
| 160 | +##### key |
| 161 | + |
| 162 | +`string` |
| 163 | + |
| 164 | +#### Returns |
| 165 | + |
| 166 | +`Promise`\<`T`\> |
| 167 | + |
| 168 | +*** |
| 169 | + |
| 170 | +### ~~open()~~ |
| 171 | + |
| 172 | +> **open**(): `Promise`\<`IDBDatabase`\> |
| 173 | +
|
| 174 | +Defined in: [indexedDB/index.ts:70](https://github.com/jin-sir/dt-utils/blob/c80bde9fd6bdabc77e6c76035f655925caf5e8af/src/indexedDB/index.ts#L70) |
| 175 | + |
| 176 | +打开在构造函数中指定的数据库。 |
| 177 | +此方法返回一个 Promise,解析为数据库实例。 |
| 178 | + |
| 179 | +#### Returns |
| 180 | + |
| 181 | +`Promise`\<`IDBDatabase`\> |
| 182 | + |
| 183 | +*** |
| 184 | + |
| 185 | +### ~~set()~~ |
| 186 | + |
| 187 | +> **set**\<`T`\>(`key`, `value`): `Promise`\<`IDBRequest`\<`any`\>\> |
| 188 | +
|
| 189 | +Defined in: [indexedDB/index.ts:154](https://github.com/jin-sir/dt-utils/blob/c80bde9fd6bdabc77e6c76035f655925caf5e8af/src/indexedDB/index.ts#L154) |
| 190 | + |
| 191 | +#### Type Parameters |
| 192 | + |
| 193 | +##### T |
| 194 | + |
| 195 | +`T` |
| 196 | + |
| 197 | +#### Parameters |
| 198 | + |
| 199 | +##### key |
| 200 | + |
| 201 | +`string` |
| 202 | + |
| 203 | +##### value |
| 204 | + |
| 205 | +`T` |
| 206 | + |
| 207 | +#### Returns |
| 208 | + |
| 209 | +`Promise`\<`IDBRequest`\<`any`\>\> |
0 commit comments