Mucho mucho update
This commit is contained in:
34
static/db.js
34
static/db.js
@@ -10,17 +10,40 @@ const followingStoreName = "following";
|
||||
let keyBase = "dandelion_posts_v1_";
|
||||
let key = "";
|
||||
let version = 1;
|
||||
function upgrade_0to1(db) {
|
||||
async function upgrade_0to1(db) {
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
async function upgrade_2to3(db) {
|
||||
let followingStore = db.createObjectStore(followingStoreName, { keyPath: "id", autoIncrement: true });
|
||||
}
|
||||
let upgrades = new Map([
|
||||
[0, upgrade_0to1],
|
||||
[1, upgrade_1to2]
|
||||
[1, upgrade_1to2],
|
||||
[2, upgrade_2to3]
|
||||
]);
|
||||
export function openDatabase(userID) {
|
||||
const dbName = `user_${userID}`;
|
||||
@@ -30,13 +53,14 @@ export function openDatabase(userID) {
|
||||
const errorEvent = event;
|
||||
reject(`Database error: ${errorEvent.target.error?.message}`);
|
||||
};
|
||||
request.onupgradeneeded = (event) => {
|
||||
request.onupgradeneeded = async (event) => {
|
||||
const db = event.target.result;
|
||||
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);
|
||||
debugger;
|
||||
await upgradeFunction(db);
|
||||
};
|
||||
request.onsuccess = (event) => {
|
||||
const db = event.target.result;
|
||||
|
||||
Reference in New Issue
Block a user