-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
90 additions
and
112 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,47 @@ | ||
"use strict"; | ||
class IndexedDBObject { | ||
dbName; | ||
objName; | ||
#db; | ||
constructor(dbName, objName) { | ||
this.dbName = dbName; | ||
this.objName = objName; | ||
import { BaseIndexedDB } from "./BaseIndexedDB.js"; | ||
class IndexedDBObject extends BaseIndexedDB { | ||
get dbOptions() { | ||
return { keyPath: 'key' }; | ||
} | ||
async init() { | ||
return new Promise((resolve, reject) => { | ||
const request = indexedDB.open(this.dbName, 1); | ||
request.onupgradeneeded = event => { | ||
this.db = event.target.result; | ||
this.db.createObjectStore(this.storeName, { keyPath: 'key' }); | ||
}; | ||
request.onsuccess = event => { | ||
this.db = event.target.result; | ||
resolve(); | ||
}; | ||
request.onerror = event => { | ||
reject(`Failed to open database: ${event.target.error}`); | ||
}; | ||
}); | ||
async assign(obj) { | ||
for (const key in obj) { | ||
await this.#setItem(key, obj[key]); | ||
} | ||
} | ||
async setItem(key, value) { | ||
return new Promise((resolve, reject) => { | ||
const transaction = this.db.transaction([this.storeName], 'readwrite'); | ||
const store = transaction.objectStore(this.storeName); | ||
const request = store.put({ key, value }); | ||
request.onsuccess = () => { | ||
resolve(); | ||
}; | ||
request.onerror = event => { | ||
reject(`Failed to set item: ${event.target.error}`); | ||
}; | ||
}); | ||
async #setItem(key, value) { | ||
return await this.idbAction('put', { key, value }); | ||
// return new Promise((resolve, reject) => { | ||
// const transaction = this.db.transaction([this.storeName], 'readwrite'); | ||
// const store = transaction.objectStore(this.storeName); | ||
// const request = store.put({ key, value }); | ||
// request.onsuccess = () => { | ||
// resolve(); | ||
// }; | ||
// request.onerror = event => { | ||
// reject(`Failed to set item: ${event.target.error}`); | ||
// }; | ||
// }); | ||
} | ||
async getItem(key) { | ||
return new Promise((resolve, reject) => { | ||
const transaction = this.db.transaction([this.storeName], 'readonly'); | ||
const store = transaction.objectStore(this.storeName); | ||
const request = store.get(key); | ||
request.onsuccess = event => { | ||
resolve(event.target.result ? event.target.result.value : null); | ||
}; | ||
request.onerror = event => { | ||
reject(`Failed to get item: ${event.target.error}`); | ||
}; | ||
}); | ||
async getProperty(key) { | ||
return await this.idbAction('get', key); | ||
// return new Promise((resolve, reject) => { | ||
// const transaction = this.db.transaction([this.storeName], 'readonly'); | ||
// const store = transaction.objectStore(this.storeName); | ||
// const request = store.get(key); | ||
// request.onsuccess = event => { | ||
// resolve(event.target.result ? event.target.result.value : null); | ||
// }; | ||
// request.onerror = event => { | ||
// reject(`Failed to get item: ${event.target.error}`); | ||
// }; | ||
// }); | ||
} | ||
} | ||
// Usage example | ||
(async () => { | ||
const db = new IndexedDBKeyValues('myDatabase', 'myStore'); | ||
await db.init(); | ||
await db.setItem('myKey', 'myValue'); | ||
const value = await db.getItem('myKey'); | ||
console.log('Stored value:', value); // Output: Stored value: myValue | ||
})(); | ||
// // Usage example | ||
// (async () => { | ||
// const db = new IndexedDBKeyValues('myDatabase', 'myStore'); | ||
// await db.init(); | ||
// await db.setItem('myKey', 'myValue'); | ||
// const value = await db.getItem('myKey'); | ||
// console.log('Stored value:', value); // Output: Stored value: myValue | ||
// })(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,57 @@ | ||
class IndexedDBObject<TObject> { | ||
#db: any | ||
constructor(public dbName: string, public objName: string) {} | ||
|
||
async init() { | ||
return new Promise((resolve, reject) => { | ||
const request = indexedDB.open(this.dbName, 1); | ||
|
||
request.onupgradeneeded = event => { | ||
this.db = event.target.result; | ||
this.db.createObjectStore(this.storeName, { keyPath: 'key' }); | ||
}; | ||
|
||
request.onsuccess = event => { | ||
this.db = event.target.result; | ||
resolve(); | ||
}; | ||
|
||
request.onerror = event => { | ||
reject(`Failed to open database: ${event.target.error}`); | ||
}; | ||
}); | ||
import { BaseIndexedDB } from "./BaseIndexedDB.js"; | ||
|
||
class IndexedDBObject<TObject> extends BaseIndexedDB { | ||
get dbOptions(){ | ||
return { keyPath: 'key'} | ||
} | ||
|
||
async setItem(key, value) { | ||
return new Promise((resolve, reject) => { | ||
const transaction = this.db.transaction([this.storeName], 'readwrite'); | ||
const store = transaction.objectStore(this.storeName); | ||
const request = store.put({ key, value }); | ||
|
||
request.onsuccess = () => { | ||
resolve(); | ||
}; | ||
|
||
request.onerror = event => { | ||
reject(`Failed to set item: ${event.target.error}`); | ||
}; | ||
}); | ||
async assign(obj: Partial<TObject>){ | ||
for(const key in obj){ | ||
await this.#setItem(key, obj[key]); | ||
} | ||
} | ||
|
||
async getItem(key) { | ||
return new Promise((resolve, reject) => { | ||
const transaction = this.db.transaction([this.storeName], 'readonly'); | ||
const store = transaction.objectStore(this.storeName); | ||
const request = store.get(key); | ||
|
||
request.onsuccess = event => { | ||
resolve(event.target.result ? event.target.result.value : null); | ||
}; | ||
async #setItem(key: keyof TObject, value: any) { | ||
return await this.idbAction('put', {key, value}); | ||
// return new Promise((resolve, reject) => { | ||
// const transaction = this.db.transaction([this.storeName], 'readwrite'); | ||
// const store = transaction.objectStore(this.storeName); | ||
// const request = store.put({ key, value }); | ||
|
||
// request.onsuccess = () => { | ||
// resolve(); | ||
// }; | ||
|
||
// request.onerror = event => { | ||
// reject(`Failed to set item: ${event.target.error}`); | ||
// }; | ||
// }); | ||
} | ||
|
||
request.onerror = event => { | ||
reject(`Failed to get item: ${event.target.error}`); | ||
}; | ||
}); | ||
async getProperty(key: keyof TObject) { | ||
return await this.idbAction('get', key); | ||
// return new Promise((resolve, reject) => { | ||
// const transaction = this.db.transaction([this.storeName], 'readonly'); | ||
// const store = transaction.objectStore(this.storeName); | ||
// const request = store.get(key); | ||
|
||
// request.onsuccess = event => { | ||
// resolve(event.target.result ? event.target.result.value : null); | ||
// }; | ||
|
||
// request.onerror = event => { | ||
// reject(`Failed to get item: ${event.target.error}`); | ||
// }; | ||
// }); | ||
} | ||
} | ||
|
||
// Usage example | ||
(async () => { | ||
const db = new IndexedDBKeyValues('myDatabase', 'myStore'); | ||
await db.init(); | ||
await db.setItem('myKey', 'myValue'); | ||
const value = await db.getItem('myKey'); | ||
console.log('Stored value:', value); // Output: Stored value: myValue | ||
})(); | ||
// // Usage example | ||
// (async () => { | ||
// const db = new IndexedDBKeyValues('myDatabase', 'myStore'); | ||
// await db.init(); | ||
// await db.setItem('myKey', 'myValue'); | ||
// const value = await db.getItem('myKey'); | ||
// console.log('Stored value:', value); // Output: Stored value: myValue | ||
// })(); | ||
|