managarten/apps/mana/apps/web/src/lib/data/scope/index.ts
Till JS 6d8637b837 feat(spaces): migrate todo/notes/contacts to scoped-db + add scopedGet
Three more modules now use the scope wrapper. Pattern matches the
calendar pilot:

  db.table<T>('X').toArray()            → scopedForModule<T>('mod','X').toArray()
  db.table<T>('X').orderBy('k').toArray → scopedForModule<T>(...).sortBy('k')
  db.table<T>('X').get(id)              → scopedGet<T>('X', id)

Added scopedGet() to the scope barrel — a primary-key fetch with a
post-read scope check so URL-manipulated deep links can't peek at
records from another space. Dexie's fast-path index read still happens;
the scope check is one field comparison on the single row.

Modules migrated:
- todo/queries.ts: useAllTasks, useAllBoardViews, useAllReminders,
  useAllProjects (4 queries; sortBy replaces orderBy-via-index)
- notes/queries.ts: useAllNotes (list), useNote (by id via scopedGet)
- contacts/queries.ts: useAllContacts

goals module lives in companion/goals with a different layout (not a
standard modules/*/queries.ts) — skipped this pass, will migrate in a
targeted follow-up.

Scope + visibility filters run BEFORE decrypt where possible so the
vault-locked UI path stays cheap: plaintext spaceId + visibility + deletedAt
metadata filters the decrypt workload before crypto gets invoked.

Performance note: sortBy() is an in-memory O(n) sort. Fine for a user's
task list, but if a hot path surfaces (e.g. a thousands-of-tasks view),
we add a [spaceId+order] compound index in a follow-up Dexie version.

Plan: docs/plans/spaces-foundation.md

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-20 18:26:01 +02:00

34 lines
814 B
TypeScript

/**
* Spaces scope layer — barrel.
*
* Module code should import everything it needs from here rather than
* reaching into the individual files, so future refactors of the
* internals stay invisible to consumers.
*
* See docs/plans/spaces-foundation.md for the full RFC.
*/
export {
getActiveSpace,
getActiveSpaceId,
getActiveSpaceStatus,
getActiveSpaceError,
setActiveSpace,
loadActiveSpace,
type ActiveSpace,
type ActiveSpaceStatus,
} from './active-space.svelte';
export { reconcileSentinels, personalSpaceSentinel } from './bootstrap';
export {
scopedTable,
scopedForModule,
scopedGet,
assertModuleAllowed,
getInScopeSpaceIds,
ScopeNotReadyError,
ModuleNotInSpaceError,
} from './scoped-db';
export { applyVisibility, isVisibleToCurrentUser, type Visibility } from './visibility';