JavaScript Academy · Lesson

Transactions and CRUD

Add, read, update, and delete records.

Lesson 2 of 413 steps

Transactions and CRUD is a free JavaScript Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.

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.

Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “Transactions and CRUD” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “Transactions and CRUD”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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