wip
This commit is contained in:
166
src/db.ts
Normal file
166
src/db.ts
Normal file
@@ -0,0 +1,166 @@
|
||||
// interface MyJsonObject {
|
||||
// id: string;
|
||||
// name: string;
|
||||
// email: string;
|
||||
// }
|
||||
|
||||
const dbName: string = "ddln";
|
||||
const storeNameBase: string = "posts";
|
||||
let keyBase = "dandelion_posts_v1_"
|
||||
let key = "";
|
||||
|
||||
|
||||
interface IDBRequestEvent<T = any> extends Event {
|
||||
target: IDBRequest<T>;
|
||||
}
|
||||
|
||||
// IndexedDB uses DOMException, so let's use it for error typing
|
||||
type DBError = Event & {
|
||||
target: { errorCode: DOMException };
|
||||
};
|
||||
|
||||
export function openDatabase(userID:string): Promise<IDBDatabase> {
|
||||
const storeName = `${storeNameBase}_${userID}`;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const request: IDBOpenDBRequest = indexedDB.open(dbName, 1);
|
||||
|
||||
request.onerror = (event: Event) => {
|
||||
// Use a type assertion to access the specific properties of IDBRequest error event
|
||||
const errorEvent = event as IDBRequestEvent;
|
||||
reject(`Database error: ${errorEvent.target.error?.message}`);
|
||||
};
|
||||
|
||||
request.onupgradeneeded = (event: IDBVersionChangeEvent) => {
|
||||
const db: IDBDatabase = (event.target as IDBOpenDBRequest).result;
|
||||
if (!db.objectStoreNames.contains(storeName)) {
|
||||
db.createObjectStore(storeName, { keyPath: "id", autoIncrement: true });
|
||||
}
|
||||
};
|
||||
|
||||
request.onsuccess = (event: Event) => {
|
||||
const db: IDBDatabase = (event.target as IDBOpenDBRequest).result;
|
||||
resolve(db);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
export async function addData(userID: string, data: any): Promise<void> {
|
||||
try {
|
||||
const storeName = `${storeNameBase}_${userID}`;
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(storeName, "readwrite");
|
||||
const store = transaction.objectStore(storeName);
|
||||
|
||||
const addRequest = store.add(data);
|
||||
|
||||
addRequest.onsuccess = (e: Event) => {
|
||||
// console.log('Data has been added:', (e.target as IDBRequest).result);
|
||||
};
|
||||
|
||||
addRequest.onerror = (event: Event) => {
|
||||
// Use a type assertion to access the specific properties of IDBRequest error event
|
||||
const errorEvent = event as IDBRequestEvent;
|
||||
console.error('Error in adding data:', errorEvent.target.error?.message);
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error in opening database:', error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function addDataArray(userID: string, array: any[]): Promise<void> {
|
||||
try {
|
||||
const storeName = `${storeNameBase}_${userID}`;
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(storeName, "readwrite");
|
||||
const store = transaction.objectStore(storeName);
|
||||
|
||||
let count = 0;
|
||||
for (let data of array) {
|
||||
const addRequest = store.add(data);
|
||||
addRequest.onsuccess = (e: Event) => {
|
||||
// console.log('Data has been added:', (e.target as IDBRequest).result);
|
||||
};
|
||||
|
||||
addRequest.onerror = (event: Event) => {
|
||||
// Use a type assertion to access the specific properties of IDBRequest error event
|
||||
const errorEvent = event as IDBRequestEvent;
|
||||
console.error('Error in adding data:', errorEvent.target.error?.message);
|
||||
};
|
||||
|
||||
count++;
|
||||
|
||||
if (count % 100 === 0) {
|
||||
console.log(`Added ${count} posts...`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error in opening database:', error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getData(userID:string, lowerID:number, upperID:number): Promise<any | undefined> {
|
||||
const storeName = `${storeNameBase}_${userID}`;
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(storeName, "readonly");
|
||||
const store = transaction.objectStore(storeName);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const keyRangeValue = IDBKeyRange.bound(lowerID, upperID);
|
||||
|
||||
const records: any[] = [];
|
||||
|
||||
const cursorRequest = store.openCursor(keyRangeValue);
|
||||
|
||||
cursorRequest.onsuccess = (event: Event) => {
|
||||
const cursor = (event.target as IDBRequest).result as IDBCursorWithValue;
|
||||
if (cursor) {
|
||||
records.push(cursor.value); // Collect the record
|
||||
cursor.continue(); // Move to the next item in the range
|
||||
} else {
|
||||
// No more entries in the range
|
||||
resolve(records);
|
||||
}
|
||||
};
|
||||
|
||||
cursorRequest.onerror = (event: Event) => {
|
||||
// Use a type assertion to access the specific properties of IDBRequest error event
|
||||
const errorEvent = event as IDBRequestEvent;
|
||||
console.error('Transaction failed:', errorEvent.target.error?.message);
|
||||
reject(errorEvent.target.error); // Reject the promise if there's an error
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAllData(userID:string): Promise<any | undefined> {
|
||||
const storeName = `${storeNameBase}_${userID}`;
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(storeName, "readonly");
|
||||
const store = transaction.objectStore(storeName);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const getRequest = store.getAll();
|
||||
|
||||
getRequest.onsuccess = () => {
|
||||
if (getRequest.result) {
|
||||
// console.log('Retrieved data:', getRequest.result.jsonData);
|
||||
// resolve(getRequest.result.jsonData as any);
|
||||
resolve(getRequest.result);
|
||||
} else {
|
||||
console.log('No data record found for key', key);
|
||||
resolve(undefined); // explicitly resolve with undefined when no data is found
|
||||
}
|
||||
};
|
||||
|
||||
getRequest.onerror = (event: Event) => {
|
||||
// Use a type assertion to access the specific properties of IDBRequest error event
|
||||
const errorEvent = event as IDBRequestEvent;
|
||||
console.error('Transaction failed:', errorEvent.target.error?.message);
|
||||
reject(errorEvent.target.error); // reject the promise if there's an error
|
||||
};
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user