0Pricing
JavaScript Academy · Lesson

Transactions and CRUD

Add, read, update, and delete records.

Transactions and CRUD is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Everything Is a Transaction

In IndexedDB, all reads and writes happen inside a transaction. A transaction groups operations so they either all succeed or all roll back, keeping data consistent.

Starting a Transaction

Create one with db.transaction(storeNames, mode). The mode is 'readonly' (default) or 'readwrite' for modifications.

const tx = db.transaction('notes', 'readwrite');
const store = tx.objectStore('notes');

Getting the Object Store

From the transaction, get the object store you want to work with via tx.objectStore(name). All CRUD methods live on this store.

const tx = db.transaction('notes', 'readwrite');
const store = tx.objectStore('notes');
// store.add / get / put / delete

Adding Records

store.add(value) inserts a new record. It fails if a record with the same key already exists, which protects against accidental overwrites.

const store = tx.objectStore('notes');
const req = store.add({ id: 1, text: 'Buy milk' });
req.onsuccess = () => console.log('added with key', req.result);

Reading a Record

store.get(key) retrieves one record by its key. The result arrives on the request's onsuccess as request.result (undefined if not found).

const req = store.get(1);
req.onsuccess = () => {
  console.log('record:', req.result);
};

Updating with put

store.put(value) inserts or replaces a record. Unlike add, it overwrites an existing record with the same key, making it ideal for updates.

store.put({ id: 1, text: 'Buy milk and eggs' });
// Replaces the existing record with id 1.

Deleting a Record

store.delete(key) removes the record with the given key. It succeeds even if no such record exists.

const req = store.delete(1);
req.onsuccess = () => console.log('deleted');

Clearing a Store

store.clear() removes every record in the object store at once. Use it to reset cached data.

store.clear();
// All records in 'notes' are removed.

Transaction Completion

The transaction itself fires oncomplete when all its operations have committed, and onerror or onabort if something fails. Use oncomplete to know writes are durable.

tx.oncomplete = () => console.log('all changes saved');
tx.onerror = () => console.log('transaction failed');

Auto-Commit Behavior

Transactions auto-commit when control returns to the event loop with no pending requests. You cannot keep one open across await of unrelated async work, do all store operations together.

// Queue operations synchronously within the same tx:
store.add({ id: 2, text: 'A' });
store.add({ id: 3, text: 'B' });
// Both commit together when tx completes.

CRUD Summary

The four core operations map cleanly:

  • Create: add
  • Read: get
  • Update: put
  • Delete: delete

All inside a transaction that guarantees consistency.

Quick Check

Test your understanding of transactions and CRUD.

Recap

You learned transactions and CRUD:

  • Start with db.transaction(store, mode).
  • Get the store with tx.objectStore(name).
  • add inserts, get reads, put upserts, delete removes.
  • Results arrive on each request's onsuccess.
  • tx.oncomplete confirms changes are saved.

Next, indexes and queries.

Frequently asked questions

Is the “Transactions and CRUD” lesson free?

Yes — the full text of “Transactions and CRUD” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Transactions and CRUD”?

Add, read, update, and delete records. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Transactions and CRUD” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Opening a Database
  2. Transactions and CRUD
  3. Indexes and Queries
  4. Versioning and Upgrades
← Back to JavaScript Academy