checkpoint

This commit is contained in:
bobbydigitales
2025-03-25 20:04:52 -07:00
parent 71b5284b0f
commit 0b5fee4aca
12 changed files with 240 additions and 138 deletions

View File

@@ -5,8 +5,9 @@
// }
// Efficiently storing data in indexdb: https://stackoverflow.com/a/62975917
const postStoreName = "posts";
const tombStoneStoreName = "tombstones";
const tombstoneStoreName = "tombstones";
const followingStoreName = "following";
const profileStoreName = "profiles";
let keyBase = "dandelion_posts_v1_";
let key = "";
let version = 1;
@@ -15,28 +16,45 @@ async function upgrade_0to1(db) {
postsStore.createIndex("datetimeIndex", "post_timestamp", { unique: false });
postsStore.createIndex("postIDIndex", "data.post_id", { unique: true });
}
// let following = ['b38b623c-c3fa-4351-9cab-50233c99fa4e'];
// let profiles = [
// {
// id: 'b38b623c-c3fa-4351-9cab-50233c99fa4e',
// name: 'Robert Anderberg',
// handle: 'bobbydigitales',
// avatar_image: new ArrayBuffer(1024),
// banner_image: new ArrayBuffer(1024),
// description: "A very nice person who never does anything wrong.",
// }];
// let tombstones = ['b38b623c-c3fa-4351-9cab-50233c99fa4e'];
async function upgrade_1to2(db) {
console.log("Upgrading database from 1 to 2");
console.log("Converting all image arraybuffers to Blobs");
// TODO convert all images from arraybuffers to blobs
let knownUsers = [...(await indexedDB.databases())].map((db) => db.name?.replace('user_', ''));
if (knownUsers.length === 0) {
return;
}
for (const userID of knownUsers) {
console.log(`Converting images for user ${userID}`);
let posts = await getAllData(userID);
for (const post of posts) {
let image_data = post.data.image_data;
if (image_data) {
let blob = new Blob([image_data]);
post.data.image_data = blob;
// TODO get the format of a new post right
addData(userID, post);
}
}
}
let followingStore = db.createObjectStore(followingStoreName, { keyPath: "id", autoIncrement: true });
let profileStore = db.createObjectStore(profileStoreName, { keyPath: "id", autoIncrement: true });
let tombstoneStore = db.createObjectStore(tombstoneStoreName, { keyPath: "id", autoIncrement: true });
tombstoneStore.createIndex("postIDIndex", "data.post_id", { unique: true });
}
// async function upgrade_1to2(db: IDBDatabase) {
// console.log("Upgrading database from 1 to 2");
// console.log("Converting all image arraybuffers to Blobs")
// // TODO convert all images from arraybuffers to blobs
// let knownUsers = [...(await indexedDB.databases())].map((db) => db.name?.replace('user_', ''));
// if (knownUsers.length === 0) {
// return;
// }
// for (const userID of knownUsers as string[]) {
// console.log(`Converting images for user ${userID}`)
// let posts = await getAllData(userID);
// for (const post of posts) {
// let image_data = post.data.image_data;
// if (image_data) {
// let blob = new Blob([image_data as ArrayBuffer]);
// post.data.image_data = blob;
// // TODO get the format of a new post right
// addData(userID, post);
// }
// }
// }
// }
async function upgrade_2to3(db) {
let followingStore = db.createObjectStore(followingStoreName, { keyPath: "id", autoIncrement: true });
}
@@ -59,7 +77,7 @@ export function openDatabase(userID) {
if (!upgradeFunction) {
throw new Error(`db: Don't have an upgrade function to go from version ${event.oldVersion} to version ${event.newVersion}`);
}
debugger;
// debugger;
await upgradeFunction(db);
};
request.onsuccess = (event) => {