mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-16 16:19:40 +02:00
- shared-branding/mana-apps: drop duplicate `mana` and obsolete `inventar` URL entries - web/app.d.ts: move __BUILD_HASH__/__BUILD_TIME__ ambient declarations into declare global so they survive module-scoping - web: remove dead supabase template (routes/api/example, lib/server/middleware) — locals.session no longer exists post auth migration - habits/queries: drop stale Record<string,string> cast on LocalHabit (legacy emoji field) - shared-stores/toggle-field: cast to Dexie UpdateSpec instead of Partial<T> for newer dexie types Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
/**
|
|
* Generic boolean field toggle for Dexie tables.
|
|
*
|
|
* Standardizes the common pattern of toggling a boolean field (isFavorite, isPinned, etc.)
|
|
* on a record in IndexedDB. Reads current value, flips it, updates with timestamp.
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* import { toggleField } from '@mana/shared-stores';
|
|
* import { db } from '$lib/data/database';
|
|
*
|
|
* // In your store:
|
|
* async function toggleFavorite(id: string) {
|
|
* return toggleField(db.table('contacts'), id, 'isFavorite');
|
|
* }
|
|
* ```
|
|
*/
|
|
|
|
import type { Table, UpdateSpec } from 'dexie';
|
|
|
|
/**
|
|
* Toggle a boolean field on a Dexie record.
|
|
* @returns The new value of the field after toggling.
|
|
*/
|
|
export async function toggleField<T>(
|
|
table: Table<T, string>,
|
|
id: string,
|
|
field: string
|
|
): Promise<boolean> {
|
|
const record = await table.get(id);
|
|
if (!record) throw new Error(`Record ${id} not found`);
|
|
|
|
const current = !!(record as Record<string, unknown>)[field];
|
|
const newValue = !current;
|
|
|
|
await table.update(id, {
|
|
[field]: newValue,
|
|
updatedAt: new Date().toISOString(),
|
|
} as unknown as UpdateSpec<T>);
|
|
|
|
return newValue;
|
|
}
|