Created
July 19, 2020 12:34
-
-
Save ANUPAMCHAUDHARY1117/4a7abca0705ab212d39417da5427e26d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { IDBPDatabase, openDB } from 'idb'; | |
class IndexedDb { | |
private database: string; | |
private db: any; | |
constructor(database: string) { | |
this.database = database; | |
} | |
public async createObjectStore(tableNames: string[]) { | |
try { | |
this.db = await openDB(this.database, 1, { | |
upgrade(db: IDBPDatabase) { | |
for (const tableName of tableNames) { | |
if (db.objectStoreNames.contains(tableName)) { | |
continue; | |
} | |
db.createObjectStore(tableName, { autoIncrement: true, keyPath: 'id' }); | |
} | |
}, | |
}); | |
} catch (error) { | |
return false; | |
} | |
} | |
public async getValue(tableName: string, id: number) { | |
const tx = this.db.transaction(tableName, 'readonly'); | |
const store = tx.objectStore(tableName); | |
const result = await store.get(id); | |
console.log('Get Data ', JSON.stringify(result)); | |
return result; | |
} | |
public async getAllValue(tableName: string) { | |
const tx = this.db.transaction(tableName, 'readonly'); | |
const store = tx.objectStore(tableName); | |
const result = await store.getAll(); | |
console.log('Get All Data', JSON.stringify(result)); | |
return result; | |
} | |
public async putValue(tableName: string, value: object) { | |
const tx = this.db.transaction(tableName, 'readwrite'); | |
const store = tx.objectStore(tableName); | |
const result = await store.put(value); | |
console.log('Put Data ', JSON.stringify(result)); | |
return result; | |
} | |
public async putBulkValue(tableName: string, values: object[]) { | |
const tx = this.db.transaction(tableName, 'readwrite'); | |
const store = tx.objectStore(tableName); | |
for (const value of values) { | |
const result = await store.put(value); | |
console.log('Put Bulk Data ', JSON.stringify(result)); | |
} | |
return this.getAllValue(tableName); | |
} | |
public async deleteValue(tableName: string, id: number) { | |
const tx = this.db.transaction(tableName, 'readwrite'); | |
const store = tx.objectStore(tableName); | |
const result = await store.get(id); | |
if (!result) { | |
console.log('Id not found', id); | |
return result; | |
} | |
await store.delete(id); | |
console.log('Deleted Data', id); | |
return id; | |
} | |
} | |
export default IndexedDb; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment