Exchange ids vs posts. Support image paste.

This commit is contained in:
bobbydigitales
2024-09-22 21:12:51 -07:00
parent 998840ec2c
commit c449267f01
11 changed files with 711 additions and 202 deletions

113
src/db.ts
View File

@@ -159,7 +159,7 @@ export async function addDataArray(userID: string, array: any[]): Promise<void>
}
}
export async function mergeDataArray(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");
@@ -176,7 +176,60 @@ export async function mergeDataArray(userID: string, array:any[]): Promise<void>
db.close();
};
let postsToWrite:any = [];
let postIdsNeeded: any = [];
for (let id of post_ids) {
try {
let havePost = await new Promise<boolean>((resolve, reject) => {
const getRequest = index.getKey(id);
getRequest.onerror = (e) => {
console.log((e.target as IDBRequest).error);
reject(e);
};
getRequest.onsuccess = async (e) => {
const key = (e.target as IDBRequest).result;
resolve(key !== undefined)
};
});
// console.log(post.post_id, havePost);
if (!havePost) {
postIdsNeeded.push(id);
}
} catch (error) {
console.error("Error processing post:", error);
}
}
console.log(`checkPostIds need ${postIdsNeeded.length} posts`);
return postIdsNeeded;
} catch (error) {
console.error("Error in opening database:", error);
}
}
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 index = store.index("postIDIndex");
transaction.oncomplete = () => {
console.log("Transaction completed successfully");
db.close();
};
transaction.onerror = (event) => {
console.error("Transaction error:", (event.target as any).error);
db.close();
};
let postsToWrite: any = [];
for (let post of array) {
try {
@@ -196,7 +249,7 @@ export async function mergeDataArray(userID: string, array:any[]): Promise<void>
});
// console.log(post.post_id, havePost);
if (!havePost ) {
if (!havePost) {
postsToWrite.push(post);
}
@@ -276,3 +329,57 @@ 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 index = store.index("postIDIndex");
let keys: string[] = [];
return new Promise((resolve, reject) => {
let request = index.openKeyCursor();
request.onsuccess = (event:any) => {
let cursor = event.target.result;
if (cursor) {
keys.push(cursor.key);
cursor.continue();
} else {
resolve(keys);
}
};
request.onerror = (event: any) => {
reject(event);
};
});
}
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 index = store.index("postIDIndex");
let posts = [];
for (const postID of postIDs) {
const post = await new Promise((resolve, reject) => {
let request = index.get(postID);
request.onsuccess = (event:any) => {
resolve(event.target.result); // Resolve with the post
};
request.onerror = (event) => {
reject(event); // Reject if any error occurs
};
});
if (post) {
posts.push(post); // Add the post to the result array if found
}
}
return posts; // Return the array of posts
}