Skip to main content

Data Layer

WEFA uses a three-tier data architecture: Dexie for persistence, bitECS for runtime entity management, and Zod for validation at all boundaries.

Dexie (IndexedDB)

The WEFADatabase is the local-first source of truth with these tables:

TablePurpose
playersPlayer profiles, element, biome, energy
plantsPlant inventory with photos, species, health
careEventsDaily care event log
creaturesCreature collection with stats, stage, traits
nurtureEventsCreature nurture event log
gameSessionsActive and completed game sessions
gameResultsMatch outcomes and rewards
badgesEarned achievement badges
attestationsEAS attestation records
eventLogGeneral event audit trail
creatureAssetsBlob cache for sprites and GLB models

Reactive Queries

Use useLiveQuery from Dexie for reactive local data, not TanStack Query:

// Correct: reactive to local changes
const plants = useLiveQuery(() => db.plants.toArray());

// TanStack Query is for network requests with Dexie persistence
const { data } = useQuery({
queryKey: ['api-data'],
networkMode: 'offlineFirst',
});

bitECS (Runtime)

bitECS manages fast entity queries during gameplay using typed arrays (numbers only, no strings or objects).

Components

// Components use typed arrays
const Element = defineComponent({ value: Types.ui8 });
const Health = defineComponent({ current: Types.ui16, max: Types.ui16 });
const Energy = defineComponent({ value: Types.ui32 });
const Stage = defineComponent({ value: Types.ui8 });
const Stats = defineComponent({
health: Types.ui16,
power: Types.ui16,
defense: Types.ui16,
speed: Types.ui16,
});

Sync with Dexie

Entities sync between Dexie (persistence) and bitECS (runtime) via utility functions:

  • loadFromDexie() - Hydrate bitECS world from Dexie on startup
  • syncToDexie() - Persist bitECS state back to Dexie
  • A Map<string, number> maps Dexie IDs to bitECS entity IDs

Zod (Validation)

All data boundaries use Zod schemas defined in src/types/:

  • player.ts - PlayerSchema
  • plant.ts - PlantSchema, CareEventSchema
  • creature.ts - CreatureSchema, NurtureEventSchema
  • game.ts - GameSessionSchema, GameResultSchema
  • common.ts - Element, Biome, Badge, CreatureStage enums

Each schema exports both the Zod schema and inferred TypeScript type.

Key Files

  • src/modules/db.ts - Dexie database definition
  • src/modules/ecs.ts - bitECS world, components, systems
  • src/types/ - All Zod schemas and TypeScript types