A simple wrapper for IndexedDB with Expiry Time

technology

5 months ago

post image

A simple wrapper for IndexedDB with Expiry Time


  // Usage:

  // await idb.setItem('myKey', 'myValue', maxAge = 30 * 24 * 60 * 60 * 1000);

  // await idb.getItem('myKey');

  // await idb.removeItem('myKey');

  // await idb.clear();



// This is a simple wrapper for IndexedDB
// Note: This is a basic example and error handling should be implemented for production use.
const dbRequest = indexedDB.open('myDatabase', 1);

dbRequest.onupgradeneeded = event => {
    let db = event.target.result;
    db.createObjectStore('myObjectStore', { keyPath: 'id' });
};

const dbPromise = new Promise((resolve, reject) => {
    const dbRequest = indexedDB.open('myDatabase', 1);

    dbRequest.onupgradeneeded = event => {
        let db = event.target.result;
        db.createObjectStore('myObjectStore', { keyPath: 'id' });
    };

    dbRequest.onsuccess = () => resolve(dbRequest.result);
    dbRequest.onerror = () => reject(dbRequest.error);
});

const idb = {
    async getItem(key) {
        const db = await dbPromise;
        return new Promise((resolve, reject) => {
            const transaction = db.transaction('myObjectStore', 'readonly');
            const objectStore = transaction.objectStore('myObjectStore');
            const request = objectStore.get(key);

            request.onsuccess = () => {
                const result = request.result;
                if (result) {
                    if (result.expireTime <= Date.now()) {
                        idb.removeItem(key);
                        resolve(null);
                    } else {
                        resolve(result.data);
                    }
                } else {
                    resolve(null);
                }
            };

            request.onerror = () => {
                reject('Error getting item from IndexedDB');
            };
        });
    },

    async setItem(key, value, maxAge = 30 * 24 * 60 * 60 * 1000) {
        const db = await dbPromise;
        const transaction = db.transaction('myObjectStore', 'readwrite');
        const objectStore = transaction.objectStore('myObjectStore');
        const data = {
            id: key,
            data: value,
            expireTime: maxAge ? Date.now() + maxAge : undefined
        };
        objectStore.put(data);
    },

    async removeItem(key) {
        const transaction = dbRequest.result.transaction('myObjectStore', 'readwrite');
        const objectStore = transaction.objectStore('myObjectStore');
        objectStore.delete(key);
    },

    async clear() {
        const transaction = dbRequest.result.transaction('myObjectStore', 'readwrite');
        const objectStore = transaction.objectStore('myObjectStore');
        objectStore.clear();
    }

    // Usage:
    // await idb.setItem('myKey', 'myValue');
    // await idb.getItem('myKey');
    // await idb.removeItem('myKey');
    // await idb.clear();

};

You may also like:

Psum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate.

Technology is nothing. What's important is that you have a faith in people, that they're basically good and smart, and if you give them tools, they'll do wonderful things with them

Steven Jobs

post image
post image

You Can Buy For Less Than A College Degree

Dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut Aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum bore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea com

Leave a comment