aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/client/db/repositories.ts
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-05-02 11:58:13 +0900
committernsfisis <nsfisis@gmail.com>2026-05-02 11:58:13 +0900
commitd47d1a014a71ae65cbbf1b384eed87c6fe078b07 (patch)
tree76c3af22e10f963104f84f3d680c5dddfc8b2be6 /src/client/db/repositories.ts
parent023d0fcfce575030ee503c5f60df8c28dba7ab07 (diff)
downloadkioku-d47d1a014a71ae65cbbf1b384eed87c6fe078b07.tar.gz
kioku-d47d1a014a71ae65cbbf1b384eed87c6fe078b07.tar.zst
kioku-d47d1a014a71ae65cbbf1b384eed87c6fe078b07.zip
feat(note-types): make note type CRUD work fully offline-firstHEADmain
CreateNoteTypeModal, DeleteNoteTypeModal, and the NoteTypeEditor (which covers field add/edit/delete/reorder) now write through the local IndexedDB repositories and fire-and-forget syncActionAtom, mirroring the deck-CRUD pattern. The dead EditNoteTypeModal — never imported — is removed. The local hasNotes / hasNoteFieldValues guards mirror the server's delete-time checks so a note type with attached notes, or a field with saved values, can't be silently soft-deleted offline. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'src/client/db/repositories.ts')
-rw-r--r--src/client/db/repositories.ts55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/client/db/repositories.ts b/src/client/db/repositories.ts
index 9f1d27c..085a23c 100644
--- a/src/client/db/repositories.ts
+++ b/src/client/db/repositories.ts
@@ -462,6 +462,19 @@ export const localNoteTypeRepository = {
},
/**
+ * Returns true if any non-deleted note still uses this note type. Used to
+ * block deletion (mirrors the server's hasNotes check).
+ */
+ async hasNotes(id: string): Promise<boolean> {
+ const note = await db.notes
+ .where("noteTypeId")
+ .equals(id)
+ .filter((n) => n.deletedAt === null)
+ .first();
+ return note !== undefined;
+ },
+
+ /**
* Get all unsynced note types
*/
async findUnsynced(): Promise<LocalNoteType[]> {
@@ -573,6 +586,48 @@ export const localNoteFieldTypeRepository = {
},
/**
+ * Returns true if any noteFieldValue references this field type. Used to
+ * block deletion (mirrors the server's hasNoteFieldValues check).
+ */
+ async hasNoteFieldValues(id: string): Promise<boolean> {
+ const value = await db.noteFieldValues
+ .where("noteFieldTypeId")
+ .equals(id)
+ .first();
+ return value !== undefined;
+ },
+
+ /**
+ * Reorder field types in a note type. The given fieldIds become the new
+ * ordering 0..n-1. Returns the reordered field types in the new order.
+ */
+ async reorder(
+ noteTypeId: string,
+ fieldIds: string[],
+ ): Promise<LocalNoteFieldType[]> {
+ const now = new Date();
+ const result = await db.transaction("rw", db.noteFieldTypes, async () => {
+ const updated: LocalNoteFieldType[] = [];
+ for (let i = 0; i < fieldIds.length; i++) {
+ const fieldId = fieldIds[i];
+ if (!fieldId) continue;
+ const field = await db.noteFieldTypes.get(fieldId);
+ if (!field || field.noteTypeId !== noteTypeId) continue;
+ const next: LocalNoteFieldType = {
+ ...field,
+ order: i,
+ updatedAt: now,
+ _synced: false,
+ };
+ await db.noteFieldTypes.put(next);
+ updated.push(next);
+ }
+ return updated;
+ });
+ return result.sort((a, b) => a.order - b.order);
+ },
+
+ /**
* Get all unsynced field types
*/
async findUnsynced(): Promise<LocalNoteFieldType[]> {