WIP
This commit is contained in:
71
db.js
71
db.js
@@ -3,12 +3,11 @@
|
||||
// name: string;
|
||||
// email: string;
|
||||
// }
|
||||
const dbName = "ddln";
|
||||
const storeNameBase = "posts";
|
||||
const postStoreName = "posts";
|
||||
let keyBase = "dandelion_posts_v1_";
|
||||
let key = "";
|
||||
export function openDatabase(userID) {
|
||||
const storeName = `${storeNameBase}_${userID}`;
|
||||
const dbName = `user_${userID}`;
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = indexedDB.open(dbName, 1);
|
||||
request.onerror = (event) => {
|
||||
@@ -18,8 +17,8 @@ export function openDatabase(userID) {
|
||||
};
|
||||
request.onupgradeneeded = (event) => {
|
||||
const db = event.target.result;
|
||||
if (!db.objectStoreNames.contains(storeName)) {
|
||||
let store = db.createObjectStore(storeName, { keyPath: "id", autoIncrement: true });
|
||||
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 });
|
||||
}
|
||||
@@ -32,10 +31,9 @@ export function openDatabase(userID) {
|
||||
}
|
||||
export async function addData(userID, data) {
|
||||
try {
|
||||
const storeName = `${storeNameBase}_${userID}`;
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(storeName, "readwrite");
|
||||
const store = transaction.objectStore(storeName);
|
||||
const transaction = db.transaction(postStoreName, "readwrite");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
const addRequest = store.add({ post_timestamp: data.post_timestamp, data: data });
|
||||
addRequest.onsuccess = (e) => {
|
||||
// console.log('Data has been added:', (e.target as IDBRequest).result);
|
||||
@@ -50,12 +48,53 @@ export async function addData(userID, data) {
|
||||
console.error('Error in opening database:', error);
|
||||
}
|
||||
}
|
||||
export async function deleteData(userID, postID) {
|
||||
try {
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(postStoreName, "readwrite");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
const index = store.index("postIDIndex");
|
||||
const getRequest = index.getKey(postID);
|
||||
getRequest.onerror = e => console.log(e.target.error);
|
||||
getRequest.onsuccess = e => {
|
||||
const key = e.target.result;
|
||||
if (key === undefined) {
|
||||
console.error("Post not found");
|
||||
return null;
|
||||
}
|
||||
const deleteRequest = store.delete(key);
|
||||
deleteRequest.onerror = e => { console.error(e.target.error); return false; };
|
||||
deleteRequest.onsuccess = () => true;
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error in opening database:', error);
|
||||
}
|
||||
}
|
||||
export async function clearData(userID) {
|
||||
try {
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(postStoreName, "readwrite");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
const clearRequest = store.clear();
|
||||
clearRequest.onsuccess = (e) => {
|
||||
// console.log('Data has been added:', (e.target as IDBRequest).result);
|
||||
};
|
||||
clearRequest.onerror = (event) => {
|
||||
// Use a type assertion to access the specific properties of IDBRequest error event
|
||||
const errorEvent = event;
|
||||
console.error('Error in clearing data:', errorEvent.target.error?.message);
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error in opening database:', error);
|
||||
}
|
||||
}
|
||||
export async function addDataArray(userID, array) {
|
||||
try {
|
||||
const storeName = `${storeNameBase}_${userID}`;
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(storeName, "readwrite");
|
||||
const store = transaction.objectStore(storeName);
|
||||
const transaction = db.transaction(postStoreName, "readwrite");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
let count = 0;
|
||||
array.reverse();
|
||||
for (let data of array) {
|
||||
@@ -79,10 +118,9 @@ export async function addDataArray(userID, array) {
|
||||
}
|
||||
}
|
||||
export async function getData(userID, lowerID, upperID) {
|
||||
const storeName = `${storeNameBase}_${userID}`;
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(storeName, "readonly");
|
||||
const store = transaction.objectStore(storeName);
|
||||
const transaction = db.transaction(postStoreName, "readonly");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
return new Promise((resolve, reject) => {
|
||||
const keyRangeValue = IDBKeyRange.bound(lowerID, upperID);
|
||||
const records = [];
|
||||
@@ -108,10 +146,9 @@ export async function getData(userID, lowerID, upperID) {
|
||||
});
|
||||
}
|
||||
export async function getAllData(userID) {
|
||||
const storeName = `${storeNameBase}_${userID}`;
|
||||
const db = await openDatabase(userID);
|
||||
const transaction = db.transaction(storeName, "readonly");
|
||||
const store = transaction.objectStore(storeName);
|
||||
const transaction = db.transaction(postStoreName, "readonly");
|
||||
const store = transaction.objectStore(postStoreName);
|
||||
return new Promise((resolve, reject) => {
|
||||
const getRequest = store.getAll();
|
||||
getRequest.onsuccess = () => {
|
||||
|
||||
Reference in New Issue
Block a user