Resource pattern
The Worlds Platform follows a consistent directory structure for implementing resources and services within the core and world-specific database scopes.Directory structure
Each resource resides within the internal database service directories and contains:queries.sql: DDL and CRUD operations.queries.sql.ts: Generated string constants for SQL queries (rundeno task generate).schema.ts: Zod schemas for the resource.service.ts: Service class implementation.service.test.ts: Deno tests using in-memory database clients.
Implementation checklist
Follow this checklist when adding new resources, tables, or routes:New resource
New resource
- Create the database resource directory.
- Add
queries.sqlwith DDL and CRUD operations. - Run
deno task generateto update SQL embedder files. - Create
schema.tsandservice.ts. - Register the table in the database initialization service.
- Implement unit tests in
service.test.ts.
New world table
New world table
- Add DDL to the resource’s
queries.sqlfile. - [ ] Register the table in the world-scope portion ofinitializeDatabase. - [ ] VerifyDatabaseManagerimplementations execute this initialization.
New route
New route
- Create
routes/v1/.../route.ts. - Implement a default export returning a
Routerinstance. - Register the module path in the server configuration.
- Implement authentication, validation, and error handling.
- Add integration tests in
route.test.ts.
Routing and API
The platform uses a modular routing system, listing available modules in the server configuration.Route handler pattern
Every route handler follows a consistent execution pattern:- Path parameters: Read parameters from
ctx.params. - Authentication: Call
authorizeRequestto verify identity. - Validation: Parse and validate the request body or query via Zod.
- Business logic: Distribute work to services or resolve a world database client.
- Response: Return success via
Response.json(record)or anErrorResponse.
Testing framework
The platform utilizes a structured testing framework to ensure component reliability.TestContext
TheTestContext provided by the testing utilities module provides an isolated
environment:
- Isolated main database: Builds an in-memory main client for each session.
- Schema initialization: Automatically initializes the database schema.
- Memory database management: Uses
MemoryDatabaseManagerfor fresh in-memory world databases.
Testing patterns
- Service tests: Verify individual resource services by performing assertions on CRUD operations.
- Route tests: Verify API endpoints by executing requests using
app.fetch()with a mockRequest. - Identity mocking: Use
createTestOrganization(context)to insert a mock organization and obtain a valid API key.