Skip to content

Commit

Permalink
Fix FF not supporting autoincrementing keys in indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasr8 committed Nov 8, 2023
1 parent 77449df commit 5a758ef
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
30 changes: 30 additions & 0 deletions src/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,32 @@ export async function addParticipant(data: AddParticipant) {
return await db.participants.add({...data, deleted, notes});
}

export async function addParticipants(data: AddParticipant[]) {
const participants = data.map(p => ({
...p,
deleted: (p.deleted ? 1 : 0) as IDBBoolean,
notes: p.notes || '',
}));

// Firefox does not support compound indexes with auto-incrementing keys
// Essentially, when inserting an item with an auto-incrementing key, the
// index does not get updated so the item is not found when searching for it afterwards
// A workaround is to first insert the items and then use put() with the new key which
// forces the index to update.
// https://bugzilla.mozilla.org/show_bug.cgi?id=1404276
// There was a similar issue which has been fixed:
// https://bugs.chromium.org/p/chromium/issues/detail?id=701972
if (isFirefox()) {
return await db.transaction('readwrite', db.participants, async () => {
const ids = await db.participants.bulkAdd(participants, {allKeys: true});
const participantsWithIds = participants.map((p, i) => ({...p, id: ids[i]}));
await db.participants.bulkPut(participantsWithIds);
});
} else {
return await db.participants.bulkAdd(participants);
}
}

export async function deleteEvent(id: number) {
return db.transaction('readwrite', [db.events, db.regforms, db.participants], async () => {
const regforms = await db.regforms.where({eventId: id}).toArray();
Expand Down Expand Up @@ -272,3 +298,7 @@ export async function updateParticipant(id: number, data: IndicoParticipant) {
isPaid,
});
}

function isFirefox() {
return navigator.userAgent.toLowerCase().includes('firefox');
}
15 changes: 10 additions & 5 deletions src/pages/Events/sync.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import {ErrorModalFunction} from '../../context/ModalContextProvider';
import db, {Event, type IDBBoolean, Participant, Regform, updateParticipant} from '../../db/db';
import db, {
Event,
type IDBBoolean,
Participant,
Regform,
updateParticipant,
addParticipants,
} from '../../db/db';
import {
FailedResponse,
getEvent,
Expand Down Expand Up @@ -183,11 +190,9 @@ export async function syncParticipants(
// participants that we don't have locally, add them
const newData = onlyNew.map(p => ({
...p,
notes: '',
deleted: 0 as IDBBoolean,
regformId: regform.id as number,
regformId: regform.id,
}));
await db.participants.bulkAdd(newData);
await addParticipants(newData);
// participants that we have both locally and in Indico, just update them
const commonData = common.map(([{id}, data]) => ({key: id, changes: data}));
await db.participants.bulkUpdate(commonData);
Expand Down

0 comments on commit 5a758ef

Please sign in to comment.