Skip to content

Instantly share code, notes, and snippets.

@KhalilZaidoun
Created March 18, 2018 13:08
Show Gist options
  • Save KhalilZaidoun/124abdd8e50603eb8086c2395cc79986 to your computer and use it in GitHub Desktop.
Save KhalilZaidoun/124abdd8e50603eb8086c2395cc79986 to your computer and use it in GitHub Desktop.
indexed-db helpers methods
import indexedDB from './indexedDB'
if (!indexedDB) {
// console.log("Your browser does not support a stable version of IndexedDB. Some features will not be available.")
}
const DATABASE_NAME = 'db_name';
const READ_WRITE_PERMISSION = 'readwrite';
export const initDataBase = () => new Promise((resolve, reject) => {
const requestIndexedDB = indexedDB.open(DATABASE_NAME, 1);
requestIndexedDB.onerror = event => {
reject();
};
requestIndexedDB.onupgradeneeded = event => {
// console.log(' initDataBase onupgradeneeded');
const db = event.target.result;
// Create Notes Store [For Example]
const noteStore = db.createObjectStore("note", {keyPath: "id", autoIncrement: true});
noteStore.createIndex("fileName", "fileName", {unique: true});
noteStore.createIndex("fullPath", "fullPath", {unique: true});
noteStore.createIndex("type", "type", {unique: false});
// ...
};
requestIndexedDB.onsuccess = event => {
// console.log(' initDataBase onsuccess');
resolve();
}
});
// Get First Row From ObjectStore ( by default index ).
// RESTRICTION : type of value is Number
export const get = (objStoreName, value) => new Promise((resolve, reject) => {
// Open the database
const open = indexedDB.open(DATABASE_NAME);
open.onsuccess = () => {
let db = open.result;
let objectStore = db.transaction(objStoreName).objectStore(objStoreName);
let objectStoreRequest = objectStore.get(value);
objectStoreRequest.onsuccess = (event) => {
resolve(event.target.result);
};
objectStoreRequest.onerror = (event) => {
reject();
};
}
});
// Get First Row From ObjectStore by index
// RESTRICTION : type of value is Number if key is unique index in Store.
// RESTRICTION : type of value is String if key is not unique index in Store.
export const getRow = (objStoreName, key, value) => {
return new Promise(resolve => {
// Open the database
const open = indexedDB.open(DATABASE_NAME);
open.onsuccess = () => {
let db = open.result;
let objectStore = db.transaction(objStoreName).objectStore(objStoreName);
let index = objectStore.index(key);
index.get(value).onsuccess = (event) => {
resolve(event.target.result);
};
}
});
};
// Get Rows From ObjectStore by index
// RESTRICTION : type of value is Number if key is unique index in Store.
// RESTRICTION : type of value is String if key is not unique index in Store.
export const getRows = (objStoreName, key, value) => new Promise((resolve, reject) => {
// Open the database
const open = indexedDB.open(DATABASE_NAME);
open.onsuccess = () => {
let array = [];
let db = open.result;
let objectStore = db.transaction(objStoreName).objectStore(objStoreName);
let index = objectStore.index(key);
let singleKeyRange = IDBKeyRange.only(value);
index.openCursor(singleKeyRange).onsuccess = (event) => {
let cursor = event.target.result;
// if there is still another cursor to go, keep runing this code
if (cursor) {
array.push(cursor.value);
cursor.continue();
} else {
resolve(array);
}
};
}
});
// Get All Rows From ObjectStore
export const getAllRows = (objStoreName) => new Promise((resolve, reject) => {
// Open the database
const open = indexedDB.open(DATABASE_NAME);
open.onsuccess = () => {
let array = [];
let db = open.result;
let objectStore = db.transaction(objStoreName).objectStore(objStoreName);
objectStore.openCursor().onsuccess = (event) => {
let cursor = event.target.result;
// if there is still another cursor to go, keep runing this code
if (cursor) {
array.push(cursor.value);
cursor.continue();
} else {
resolve(array);
}
};
}
});
// Put the row to the object store specify in params
// objStoreName is String, row is object : {'key':value, ... }
export const putRow = (objStoreName, row) => new Promise((resolve, reject) => {
// Open the database
const open = indexedDB.open(DATABASE_NAME);
open.onsuccess = (e) => {
let db = open.result;
let tx = db.transaction(objStoreName, READ_WRITE_PERMISSION);
let _store = tx.objectStore(objStoreName);
let request = _store.put(row);
request.onsuccess = (event) => {
resolve(event.target.result);
};
request.onerror = (event) => {
reject(event.srcElement.error);
};
};
open.onerror = (e) => {
reject(e);
}
});
// Add the row to the object store specify in params
// objStoreName is String, row is object : {'key':value, ... }
export const addRow = (objStoreName, row) => new Promise((resolve, reject) => {
// Open the database
const open = indexedDB.open(DATABASE_NAME);
open.onsuccess = (e) => {
let db = open.result;
let tx = db.transaction(objStoreName, READ_WRITE_PERMISSION);
let _store = tx.objectStore(objStoreName);
let request = _store.add(row);
request.onsuccess = (event) => {
resolve(event.target.result);
};
request.onerror = (event) => {
reject();
};
};
open.onerror = (e) => {
reject();
}
});
// Delete the row in the object store specify by value of id
// objStoreName is String, key is the id of store
export const deleteRow = (objStoreName, k, value) => new Promise((resolve, reject) => {
// Open the database
const open = indexedDB.open(DATABASE_NAME);
open.onsuccess = (e) => {
let db = open.result;
let tx = db.transaction(objStoreName, READ_WRITE_PERMISSION);
let _store = tx.objectStore(objStoreName);
let request = _store.delete(value);
request.onsuccess = (event) => {
resolve(event.target.result);
};
request.onerror = (event) => {
reject();
};
};
open.onerror = (e) => {
reject();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment