Lotsa changes :)
This commit is contained in:
110
src/db.ts
110
src/db.ts
@@ -10,6 +10,7 @@
|
||||
const postStoreName: string = "posts";
|
||||
let keyBase = "dandelion_posts_v1_"
|
||||
let key = "";
|
||||
let version = 1;
|
||||
|
||||
|
||||
interface IDBRequestEvent<T = any> extends Event {
|
||||
@@ -21,26 +22,29 @@ type DBError = Event & {
|
||||
target: { errorCode: DOMException };
|
||||
};
|
||||
|
||||
function upgrade_0to1(db:IDBDatabase) {
|
||||
let store = db.createObjectStore(postStoreName, { keyPath: "id", autoIncrement: true });
|
||||
store.createIndex("datetimeIndex", "post_timestamp", { unique: false });
|
||||
store.createIndex("postIDIndex", "data.post_id", { unique: true });
|
||||
}
|
||||
|
||||
|
||||
export function openDatabase(userID: string): Promise<IDBDatabase> {
|
||||
const dbName = `user_${userID}`
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const request: IDBOpenDBRequest = indexedDB.open(dbName, 1);
|
||||
const request: IDBOpenDBRequest = indexedDB.open(dbName, version);
|
||||
|
||||
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(postStoreName)) {
|
||||
let store = db.createObjectStore(postStoreName, { keyPath: "id", autoIncrement: true });
|
||||
store.createIndex("datetimeIndex", "post_timestamp", { unique: false });
|
||||
store.createIndex("postIDIndex", "data.post_id", { unique: true });
|
||||
|
||||
}
|
||||
upgrade_0to1(db);
|
||||
|
||||
};
|
||||
|
||||
request.onsuccess = (event: Event) => {
|
||||
@@ -50,14 +54,17 @@ export function openDatabase(userID: string): Promise<IDBDatabase> {
|
||||
});
|
||||
}
|
||||
|
||||
async function getDBTransactionStore(userID:string) {
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(postStoreName, "readwrite");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
return {db, transaction, store}
|
||||
}
|
||||
|
||||
|
||||
export async function addData(userID: string, data: any): Promise<void> {
|
||||
try {
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(postStoreName, "readwrite");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
|
||||
const {db, transaction, store} = await getDBTransactionStore(userID);
|
||||
const addRequest = store.add({ post_timestamp: data.post_timestamp, data: data });
|
||||
|
||||
addRequest.onsuccess = (e: Event) => {
|
||||
@@ -76,12 +83,9 @@ export async function addData(userID: string, data: any): Promise<void> {
|
||||
|
||||
export async function deleteData(userID: string, postID: string) {
|
||||
try {
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(postStoreName, "readwrite");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
const {db, transaction, store} = await getDBTransactionStore(userID);
|
||||
const index = store.index("postIDIndex");
|
||||
|
||||
|
||||
const getRequest = index.getKey(postID);
|
||||
|
||||
getRequest.onerror = e => console.log((e.target as IDBRequest).error)
|
||||
@@ -104,9 +108,7 @@ export async function deleteData(userID: string, postID: string) {
|
||||
|
||||
export async function clearData(userID: string) {
|
||||
try {
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(postStoreName, "readwrite");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
const {db, transaction, store} = await getDBTransactionStore(userID);
|
||||
|
||||
const clearRequest = store.clear();
|
||||
clearRequest.onsuccess = (e: Event) => {
|
||||
@@ -126,9 +128,7 @@ export async function clearData(userID: string) {
|
||||
|
||||
export async function addDataArray(userID: string, array: any[]): Promise<void> {
|
||||
try {
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(postStoreName, "readwrite");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
const {db, transaction, store} = await getDBTransactionStore(userID);
|
||||
|
||||
let count = 0;
|
||||
|
||||
@@ -161,13 +161,12 @@ export async function addDataArray(userID: string, array: any[]): Promise<void>
|
||||
|
||||
export async function checkPostIds(userID: string, post_ids: string[]) {
|
||||
try {
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(postStoreName, "readwrite");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
const {db, transaction, store} = await getDBTransactionStore(userID);
|
||||
|
||||
const index = store.index("postIDIndex");
|
||||
|
||||
transaction.oncomplete = () => {
|
||||
console.log("Transaction completed successfully");
|
||||
// console.log("Transaction completed successfully");
|
||||
db.close();
|
||||
};
|
||||
|
||||
@@ -214,13 +213,12 @@ export async function checkPostIds(userID: string, post_ids: string[]) {
|
||||
|
||||
export async function mergeDataArray(userID: string, array: any[]): Promise<void> {
|
||||
try {
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(postStoreName, "readwrite");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
const {db, transaction, store} = await getDBTransactionStore(userID);
|
||||
|
||||
const index = store.index("postIDIndex");
|
||||
|
||||
transaction.oncomplete = () => {
|
||||
console.log("Transaction completed successfully");
|
||||
// console.log("Transaction completed successfully");
|
||||
db.close();
|
||||
};
|
||||
|
||||
@@ -266,46 +264,35 @@ export async function mergeDataArray(userID: string, array: any[]): Promise<void
|
||||
}
|
||||
}
|
||||
|
||||
export async function getPostForUser(userID: string, postID: string): Promise<any | undefined> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
export async function getData(userID: string, lowerID: Date, upperID: Date): Promise<any | undefined> {
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(postStoreName, "readonly");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
const { store } = await getDBTransactionStore(userID);
|
||||
const keyRangeValue = IDBKeyRange.bound(lowerID, upperID);
|
||||
const index = store.index("datetimeIndex");
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const keyRangeValue = IDBKeyRange.bound(lowerID, upperID);
|
||||
const getAllRequest = index.getAll(keyRangeValue);
|
||||
|
||||
const records: any[] = [];
|
||||
|
||||
const index = store.index("datetimeIndex");
|
||||
|
||||
|
||||
const cursorRequest = index.openCursor(keyRangeValue);
|
||||
|
||||
cursorRequest.onsuccess = (event: Event) => {
|
||||
const cursor = (event.target as IDBRequest).result as IDBCursorWithValue;
|
||||
if (cursor) {
|
||||
records.push(cursor.value.data); // Collect the record
|
||||
cursor.continue(); // Move to the next item in the range
|
||||
} else {
|
||||
// No more entries in the range
|
||||
resolve(records);
|
||||
}
|
||||
getAllRequest.onsuccess = () => {
|
||||
const records = getAllRequest.result.map((item: any) => item.data);
|
||||
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
|
||||
getAllRequest.onerror = () => {
|
||||
console.error('Transaction failed:', getAllRequest.error?.message);
|
||||
reject(getAllRequest.error);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
export async function getAllData(userID: string): Promise<any | undefined> {
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(postStoreName, "readonly");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
const {store} = await getDBTransactionStore(userID);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const getRequest = store.getAll();
|
||||
@@ -331,9 +318,8 @@ export async function getAllData(userID: string): Promise<any | undefined> {
|
||||
}
|
||||
|
||||
export async function getAllIds(userID: string): Promise<any | undefined> {
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(postStoreName, "readonly");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
const {store} = await getDBTransactionStore(userID);
|
||||
|
||||
const index = store.index("postIDIndex");
|
||||
|
||||
let keys: string[] = [];
|
||||
@@ -357,9 +343,7 @@ export async function getAllIds(userID: string): Promise<any | undefined> {
|
||||
}
|
||||
|
||||
export async function getPostsByIds(userID:string, postIDs:string[]) {
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(postStoreName, "readonly");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
const {store} = await getDBTransactionStore(userID);
|
||||
const index = store.index("postIDIndex");
|
||||
let posts = [];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user