DB Docs
API Reference
- Core API Reference
- Collection
- createCollection
- liveQueryCollectionOptions
- createLiveQueryCollection
- createOptimisticAction
- createTransaction
- Electric DB Collection
- electricCollectionOptions
- Query DB Collection
- queryCollectionOptions
- RxDB DB Collection
- rxdbCollectionOptions
- PowerSync Collection
- powerSyncCollectionOptions
- createIndexedDB
- indexedDBCollectionOptions
- React Hooks
- useLiveQuery
AI/LLM: This documentation page is available in plain markdown format at/db/latest/docs/reference/functions/put.md
put
ts
function put<T>(
objectStore,
value,
key?): Promise<IDBValidKey>;Defined in: packages/db/src/indexed-db-wrapper.ts:470
Writes an item to an object store using upsert semantics.
If an item with the same key exists, it will be replaced. If no item with the key exists, a new one will be created.
Type Parameters#
T#
T
The type of the item
Parameters#
objectStore#
IDBObjectStore
The IDBObjectStore to write to
value#
T
The item to write
key?#
IDBValidKey
Optional key for the item. Required if the object store doesn't have a keyPath.
Returns#
Promise<IDBValidKey>
A promise that resolves to the key of the written item
Example#
ts
// With keyPath (key extracted from value)
await executeTransaction(db, 'todos', 'readwrite', async (tx, stores) => {
const key = await put(stores.todos, { id: 1, text: 'Buy milk' })
console.log('Wrote item with key:', key)
})
// Without keyPath (explicit key)
await executeTransaction(db, 'items', 'readwrite', async (tx, stores) => {
const key = await put(stores.items, { text: 'Some data' }, 'myKey')
console.log('Wrote item with key:', key)
})