IndexedDB insert means writing a structured clone of a JavaScript value into an object store. Use IDBObjectStore.add() for insert-only writes, and IDBObjectStore.put() to insert or replace by key.
Both methods need a readwrite transaction. Create the object store in onupgradeneeded when the database version increases. Then retrieve, update, or delete as needed.
Baseline · Widely available.
Not deprecated. add() and put() have been stable across browsers since about July 2015. Also available in Web Workers.
Quick rule.
Need a hard “must be new” insert? Use add() (duplicate key → ConstraintError). Saving a form that may already exist? Use put().
Syntax
Syntax
syntax.js
store.add(value, key?)
syntax.js
store.put(value, key?)
With an in-line keyPath, put the key on the object (for example { id: 1, name: "Alex" }). With out-of-line keys and no generator, pass the optional key argument.
Reference
Parameters
Both add() and put() take the same shape:
Parameter
Type
Description
value
any (cloneable)
The record to store. Must be cloneable by the structured clone algorithm (no functions, DOM nodes, etc.).
key
Key (optional)
Out-of-line primary key when the store has no in-line keyPath / generator. Do not pass it when the store uses in-line keys or autoIncrement.
Result
Return Value
Returns an IDBRequest. On success, request.result is the key of the stored record (including auto-generated keys). Prefer also listening to transaction.oncomplete—request success means the operation was queued, not that the whole transaction finished.
Exception
When it is thrown
ConstraintError
add() when the primary key already exists
ReadOnlyError
Transaction mode is readonly
DataError
Invalid or mismatched key / keyPath rules
DataCloneError
Value cannot be structured-cloned
TransactionInactiveError
The transaction is no longer active
Flow
How It Works
A typical insert follows these steps:
Open the database with indexedDB.open(name, version).
Create the store in onupgradeneeded if it does not exist yet.
Start a write tx: db.transaction(['store'], 'readwrite').
Call add(value) or put(value) on the object store.
Handle success or error; wait for oncomplete when you need a committed write.
Create the store in onupgradeneeded on first open.
Enable the button after onsuccess.
On click, run put() so repeat clicks update safely.
Show req.result in the status text for learners.
Edge Cases & Pitfalls
Most insert bugs come from wrong keys, readonly mode, or mixing up add and put.
mode
Need readwrite
add() and put() throw ReadOnlyError in a readonly transaction.
keys
add vs put
Duplicate keys fail with add() (ConstraintError) but succeed with put().
schema
Create stores on upgrade only
createObjectStore() works only inside onupgradeneeded with a higher version.
clone
Values must be cloneable
Functions, DOM nodes, and some exotic objects throw DataCloneError. Prefer plain objects and arrays.
commit
Success ≠ committed yet
Request onsuccess means the write was accepted into the transaction. Use transaction.oncomplete when you need confirmation it committed.
Compatibility
Browser Support
IndexedDB add() and put() are standard Web Storage features in modern browsers—not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
IndexedDB add() / put()
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support insert and upsert into object stores.
96%Global support (approx.)
Google Chrome24+
Full support
Mozilla Firefox16+
Full support
Apple Safari10+
Full support
Microsoft Edge12+
Full support
Opera15+
Full support
Samsung Internet1.5+
Full support
Internet Explorer10+
Partial / older
IndexedDB add/putWidely supported
Bottom line: Safe to use IndexedDB inserts in production web apps. Prefer feature detection with window.indexedDB for very old browsers.
Remember
Key Takeaways
Insert only:store.add(value) fails if the key already exists.
Upsert:store.put(value) inserts or replaces by primary key.
Mode: always use a readwrite transaction for writes.
Schema: create object stores only inside onupgradeneeded.
One line: insert with add(), upsert with put(), always inside readwrite.
Frequently Asked Questions
Open the database with indexedDB.open(), create an object store in onupgradeneeded if needed, start a readwrite transaction, then call store.add(object) for new records or store.put(object) to insert or replace.
add() is insert-only: it fails with ConstraintError if the key already exists. put() inserts or replaces (upsert). Use add() when duplicates must fail; use put() for save-or-update.
IndexedDB requires readwrite mode for add() and put(). A readonly transaction throws ReadOnlyError. If the transaction aborts, none of its writes are committed.
Only inside onupgradeneeded when you open the database with a higher version. Schema changes cannot run during a normal readwrite transaction on an already-open database.
An IDBRequest whose result is the key of the new record (including auto-generated keys). Also listen for transaction.oncomplete, because the request success only means the request was queued successfully.
Yes. add() and put() are Baseline Widely available since about July 2015 in Chrome, Firefox, Safari, Edge, and current mobile browsers. They also work in Web Workers.