lotsa stuff

This commit is contained in:
bobbydigitales
2024-10-30 20:51:44 -07:00
parent 05c4800461
commit 6ce693cefc
13 changed files with 1421 additions and 373 deletions

View File

@@ -5,9 +5,13 @@
// }
// Efficiently storing data in indexdb: https://stackoverflow.com/a/62975917
const postStoreName: string = "posts";
const tombStoneStoreName: string = "tombstones"
const followingStoreName: string = "following"
let keyBase = "dandelion_posts_v1_"
let key = "";
let version = 1;
@@ -22,12 +26,22 @@ 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 });
function upgrade_0to1(db: IDBDatabase) {
let postsStore = db.createObjectStore(postStoreName, { keyPath: "id", autoIncrement: true });
postsStore.createIndex("datetimeIndex", "post_timestamp", { unique: false });
postsStore.createIndex("postIDIndex", "data.post_id", { unique: true });
}
function upgrade_1to2(db: IDBDatabase) {
let followingStore = db.createObjectStore(followingStoreName, { keyPath: "id", autoIncrement: true });
}
let upgrades = new Map([
[0, upgrade_0to1],
[1, upgrade_1to2]
]);
export function openDatabase(userID: string): Promise<IDBDatabase> {
const dbName = `user_${userID}`
@@ -43,8 +57,11 @@ export function openDatabase(userID: string): Promise<IDBDatabase> {
request.onupgradeneeded = (event: IDBVersionChangeEvent) => {
const db: IDBDatabase = (event.target as IDBOpenDBRequest).result;
upgrade_0to1(db);
let upgradeFunction = upgrades.get(event.oldVersion);
if (!upgradeFunction) {
throw new Error(`db: Don't have an upgrade function to go from version ${event.oldVersion} to version ${event.newVersion}`);
}
upgradeFunction(db);
};
request.onsuccess = (event: Event) => {
@@ -54,17 +71,17 @@ export function openDatabase(userID: string): Promise<IDBDatabase> {
});
}
async function getDBTransactionStore(userID:string) {
async function getDBTransactionStore(userID: string, mode: IDBTransactionMode = "readonly") {
const db = await openDatabase(userID);
const transaction = db.transaction(postStoreName, "readwrite");
const transaction = db.transaction(postStoreName, mode);
const store = transaction.objectStore(postStoreName);
return {db, transaction, store}
return { db, transaction, store }
}
export async function addData(userID: string, data: any): Promise<void> {
try {
const {db, transaction, store} = await getDBTransactionStore(userID);
const { db, transaction, store } = await getDBTransactionStore(userID, "readwrite");
const addRequest = store.add({ post_timestamp: data.post_timestamp, data: data });
addRequest.onsuccess = (e: Event) => {
@@ -83,7 +100,7 @@ export async function addData(userID: string, data: any): Promise<void> {
export async function deleteData(userID: string, postID: string) {
try {
const {db, transaction, store} = await getDBTransactionStore(userID);
const { db, transaction, store } = await getDBTransactionStore(userID, "readwrite");
const index = store.index("postIDIndex");
const getRequest = index.getKey(postID);
@@ -108,7 +125,7 @@ export async function deleteData(userID: string, postID: string) {
export async function clearData(userID: string) {
try {
const {db, transaction, store} = await getDBTransactionStore(userID);
const { db, transaction, store } = await getDBTransactionStore(userID, "readwrite");
const clearRequest = store.clear();
clearRequest.onsuccess = (e: Event) => {
@@ -128,25 +145,30 @@ export async function clearData(userID: string) {
export async function addDataArray(userID: string, array: any[]): Promise<void> {
try {
const {db, transaction, store} = await getDBTransactionStore(userID);
const { db, transaction, store } = await getDBTransactionStore(userID, "readwrite");
let count = 0;
transaction.onerror = (event: Event) => {
console.error('Error in adding data:', event);
}
// let count = 0;
array.reverse();
for (let data of array) {
const addRequest = store.add({ post_timestamp: data.post_timestamp, data: data });
addRequest.onsuccess = (e: Event) => {
// console.log('Data has been added:', (e.target as IDBRequest).result);
};
// 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);
};
// 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++;
// count++;
// if (count % 100 === 0) {
// console.log(`Added ${count} posts...`);
@@ -161,7 +183,7 @@ export async function addDataArray(userID: string, array: any[]): Promise<void>
export async function checkPostIds(userID: string, post_ids: string[]) {
try {
const {db, transaction, store} = await getDBTransactionStore(userID);
const { db, transaction, store } = await getDBTransactionStore(userID);
const index = store.index("postIDIndex");
@@ -213,7 +235,7 @@ export async function checkPostIds(userID: string, post_ids: string[]) {
export async function mergeDataArray(userID: string, array: any[]): Promise<void> {
try {
const {db, transaction, store} = await getDBTransactionStore(userID);
const { db, transaction, store } = await getDBTransactionStore(userID, "readwrite");
const index = store.index("postIDIndex");
@@ -265,7 +287,7 @@ export async function mergeDataArray(userID: string, array: any[]): Promise<void
}
export async function getPostForUser(userID: string, postID: string): Promise<any | undefined> {
}
@@ -292,7 +314,7 @@ export async function getData(userID: string, lowerID: Date, upperID: Date): Pro
export async function getAllData(userID: string): Promise<any | undefined> {
const {store} = await getDBTransactionStore(userID);
const { store } = await getDBTransactionStore(userID);
return new Promise((resolve, reject) => {
const getRequest = store.getAll();
@@ -318,7 +340,7 @@ export async function getAllData(userID: string): Promise<any | undefined> {
}
export async function getAllIds(userID: string): Promise<any | undefined> {
const {store} = await getDBTransactionStore(userID);
const { store } = await getDBTransactionStore(userID);
const index = store.index("postIDIndex");
@@ -326,11 +348,11 @@ export async function getAllIds(userID: string): Promise<any | undefined> {
return new Promise((resolve, reject) => {
let request = index.openKeyCursor();
request.onsuccess = (event:any) => {
request.onsuccess = (event: any) => {
let cursor = event.target.result;
if (cursor) {
keys.push(cursor.key);
cursor.continue();
keys.push(cursor.key);
cursor.continue();
} else {
resolve(keys);
}
@@ -342,8 +364,8 @@ export async function getAllIds(userID: string): Promise<any | undefined> {
});
}
export async function getPostsByIds(userID:string, postIDs:string[]) {
const {store} = await getDBTransactionStore(userID);
export async function getPostsByIds(userID: string, postIDs: string[]) {
const { store } = await getDBTransactionStore(userID);
const index = store.index("postIDIndex");
let posts = [];
@@ -351,7 +373,7 @@ export async function getPostsByIds(userID:string, postIDs:string[]) {
const post = await new Promise((resolve, reject) => {
let request = index.get(postID);
request.onsuccess = (event:any) => {
request.onsuccess = (event: any) => {
resolve(event.target.result); // Resolve with the post
};

File diff suppressed because it is too large Load Diff

View File

@@ -8,6 +8,7 @@ const contentToCache = [
'/static/main.js',
'/static/lib/marked.min.js',
'/static/lib/qrcode.min.js',
'/static/lib/d3.js',
'/static/db.js',
'/static/favicon.ico'
];