Exchange ids vs posts. Support image paste.
This commit is contained in:
89
db.js
89
db.js
@@ -118,6 +118,50 @@ export async function addDataArray(userID, array) {
|
||||
console.error('Error in opening database:', error);
|
||||
}
|
||||
}
|
||||
export async function checkPostIds(userID, post_ids) {
|
||||
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.error);
|
||||
db.close();
|
||||
};
|
||||
let postIdsNeeded = [];
|
||||
for (let id of post_ids) {
|
||||
try {
|
||||
let havePost = await new Promise((resolve, reject) => {
|
||||
const getRequest = index.getKey(id);
|
||||
getRequest.onerror = (e) => {
|
||||
console.log(e.target.error);
|
||||
reject(e);
|
||||
};
|
||||
getRequest.onsuccess = async (e) => {
|
||||
const key = e.target.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, array) {
|
||||
try {
|
||||
const db = await openDatabase(userID);
|
||||
@@ -215,4 +259,49 @@ export async function getAllData(userID) {
|
||||
};
|
||||
});
|
||||
}
|
||||
export async function getAllIds(userID) {
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(postStoreName, "readonly");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
const index = store.index("postIDIndex");
|
||||
let keys = [];
|
||||
return new Promise((resolve, reject) => {
|
||||
let request = index.openKeyCursor();
|
||||
request.onsuccess = (event) => {
|
||||
let cursor = event.target.result;
|
||||
if (cursor) {
|
||||
keys.push(cursor.key);
|
||||
cursor.continue();
|
||||
}
|
||||
else {
|
||||
resolve(keys);
|
||||
}
|
||||
};
|
||||
request.onerror = (event) => {
|
||||
reject(event);
|
||||
};
|
||||
});
|
||||
}
|
||||
export async function getPostsByIds(userID, postIDs) {
|
||||
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) => {
|
||||
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
|
||||
}
|
||||
//# sourceMappingURL=db.js.map
|
||||
Reference in New Issue
Block a user