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

put.md

68 lines1.3 KB

1---
2id: put
3title: put
4---
5​
6```ts
7function put<T>(
8 objectStore,
9 value,
10key?): Promise<IDBValidKey>;
11```
12​
13Defined in: [packages/db/src/indexed-db-wrapper.ts:470](https://github.com/TanStack/db/blob/main/packages/db/src/indexed-db-wrapper.ts#L470)
14​
15Writes an item to an object store using upsert semantics.
16​
17If an item with the same key exists, it will be replaced.
18If no item with the key exists, a new one will be created.
19​
20## Type Parameters
21​
22### T
23​
24`T`
25​
26The type of the item
27​
28## Parameters
29​
30### objectStore
31​
32`IDBObjectStore`
33​
34The IDBObjectStore to write to
35​
36### value
37​
38`T`
39​
40The item to write
41​
42### key?
43​
44`IDBValidKey`
45​
46Optional key for the item. Required if the object store doesn't have a keyPath.
47​
48## Returns
49​
50`Promise`\<`IDBValidKey`\>
51​
52A promise that resolves to the key of the written item
53​
54## Example
55​
56```typescript
57// With keyPath (key extracted from value)
58await executeTransaction(db, 'todos', 'readwrite', async (tx, stores) => {
59 const key = await put(stores.todos, { id: 1, text: 'Buy milk' })
60 console.log('Wrote item with key:', key)
61})
62​
63// Without keyPath (explicit key)
64await executeTransaction(db, 'items', 'readwrite', async (tx, stores) => {
65 const key = await put(stores.items, { text: 'Some data' }, 'myKey')
66 console.log('Wrote item with key:', key)
67})
68```