Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

openDatabase.md

68 lines1.4 KB

1---
2id: openDatabase
3title: openDatabase
4---
5​
6```ts
7function openDatabase(
8 name,
9 version,
10 onUpgrade?,
11 idbFactory?,
12onBlocked?): Promise<IDBDatabase>;
13```
14​
15Defined in: [packages/db/src/indexed-db-wrapper.ts:94](https://github.com/TanStack/db/blob/main/packages/db/src/indexed-db-wrapper.ts#L94)
16​
17Opens an IndexedDB database with the specified name and version.
18A blocked request stays pending until native success or error. The caller
19owns the returned connection and must close it when no longer needed.
20​
21## Parameters
22​
23### name
24​
25`string`
26​
27The name of the database to open
28​
29### version
30​
31`number`
32​
33The version number of the database schema
34​
35### onUpgrade?
36​
37(`db`, `oldVersion`, `newVersion`, `transaction`) => `void`
38​
39Optional callback that runs during the onupgradeneeded event.
40 Use this to create object stores and indexes.
41​
42### idbFactory?
43​
44`IDBFactory`
45​
46Optional IDBFactory for testing/mocking (defaults to window.indexedDB or globalThis.indexedDB)
47​
48### onBlocked?
49​
50(`event`) => `void`
51​
52Optional diagnostic callback for native blocked events. The request stays pending.
53​
54## Returns
55​
56`Promise`\<`IDBDatabase`\>
57​
58A promise that resolves to the IDBDatabase instance
59​
60## Example
61​
62```typescript
63const db = await openDatabase('myApp', 1, (db, oldVersion, newVersion, transaction) => {
64 if (oldVersion < 1) {
65 db.createObjectStore('todos', { keyPath: 'id' })
66 }
67})
68```