July 26, 2026 10 min read

Building Offline-First POS Systems with IndexedDB

Written by Asim Ali
Building Offline-First POS Systems with IndexedDB Illustration

For critical business applications like Point of Sale (POS) billing systems, internet connectivity drops should never halt transactions. Building an "offline-first" system ensures cashiers can continue printing invoices and logging sales locally. In this post, we will look at how to set up local database stores using JavaScript's IndexedDB API, cache static assets, and implement online data synchronization mechanisms.

1. What is IndexedDB?

Unlike localStorage, which is synchronous and limited to around 5MB of string data, IndexedDB is a fully transactional, asynchronous, object-oriented database running directly inside the user's browser. It has virtually no storage limits (based on disk space) and can store complex JavaScript objects, buffers, and arrays.

2. Initializing IndexedDB

To initialize IndexedDB, you request to open a database. Since the API is event-driven, you listen for lifecycle events like success, error, and database schema upgrades (onupgradeneeded):

const dbRequest = window.indexedDB.open('POS_Database', 1);

dbRequest.onupgradeneeded = function(e) {
    const db = e.target.result;
    
    // Create an object store for products with index key path
    if (!db.objectStoreNames.contains('products')) {
        db.createObjectStore('products', { keyPath: 'id' });
    }
    
    // Create an object store for offline transactions with auto-incrementing key
    if (!db.objectStoreNames.contains('sales_ledger')) {
        db.createObjectStore('sales_ledger', { keyPath: 'id', autoIncrement: true });
    }
};

3. Logging Sales Transactions Offline

When internet status drops, we intercept the checkout transaction and write it locally inside the sales_ledger store:

function saveSaleOffline(saleData) {
    const dbRequest = window.indexedDB.open('POS_Database', 1);
    
    dbRequest.onsuccess = function(e) {
        const db = e.target.result;
        const tx = db.transaction('sales_ledger', 'readwrite');
        const store = tx.objectStore('sales_ledger');
        
        const record = {
            products: saleData.items,
            total_amount: saleData.total,
            cashier_id: saleData.cashierId,
            sync_status: 'pending',
            created_at: new Date().toISOString()
        };
        
        store.add(record);
    };
}