Skip to content

Commit

Permalink
0.0.848
Browse files Browse the repository at this point in the history
  • Loading branch information
bahrus committed Oct 28, 2024
1 parent 7ef9935 commit ec5edab
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 112 deletions.
97 changes: 42 additions & 55 deletions env/IndexedDBObject.js
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
// })();
105 changes: 48 additions & 57 deletions env/IndexedDBObject.ts
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
// })();

0 comments on commit ec5edab

Please sign in to comment.