#
Configuration
Outdated
Backend applications (orcherstrator, service) are configured within wrangler.toml file. Find out more information about configuration here. Each change must be followed by pnpm cf:typegen command to generate the types.
#
Package
The shared @types directory facilitates type checking across all services.
#
Orchestrator
Orchestrator contains bindings to the selected services which allows the communication via RPC.
name = "internal-docs-orchestrator"
compatibility_date = "2024-12-30"
main = "./.output/server/index.mjs"
compatibility_flags = [ "nodejs_compat" ]
[site]
bucket = ".output/public"
[observability]
enabled = true
[[services]]
binding = "LOREM_SERVICE"
service = "lorem-service"
[[services]]
binding = "IPSUM_SERVICE"
service = "ipsum-service"
[[services]]
binding = "DOLOR_SERVICE"
service = "dolor-service"
#
Service
Service can contain bindings to the following:
- D1 database
- KV storage
- Workers Queue
Configuration can also contain variables.
name = "lorem-service"
compatibility_date = "2024-12-30"
compatibility_flags = [ "nodejs_compat" ]
main = "src/index.ts"
[observability]
enabled = true
[vars]
I_USE = "ARCH BTW"
[[d1_databases]]
binding = "LOREM_DB"
database_name = "lorem_d1"
database_id = "0bc400e7-ca64-4fcf-a7b3-ac2f75006c6f"
migrations_dir = "./src/database/migrations"
[[kv_namespaces]]
binding = "LOREM_KV"
id = "aa76a96dc11f82d705de478e832fc6e7"
[[queues.producers]]
binding = 'LOREM_QUEUE'
queue = 'lorem_queue'
delivery_delay = 60
If service uses D1 database it has to be operated through Drizzle ORM. Inside src directory, create database with the following structure:
services/lorem
├── src/index.ts
│ ├── /database
│ ├── /migrations // SQL migrations, generated automatically by Drizzle
│ ├── /schema // Database schema (one file for each table)
│ ├── /seed // Seeding scripts
│ ├── drizzle.ts // Drizzle configuration
├── worker-configuration.d.ts
├── wrangler.toml
When it comes to testing, everything is handled automatically, as defined in wrangler.toml. However, if the service is using database, it's needed to define where should Miniflare take the migrations to apply.
import { join } from 'path'
import { defineWorkersProject, readD1Migrations } from '@cloudflare/vitest-pool-workers/config'
export default defineWorkersProject(async () => {
return {
test: {
// ...
poolOptions: {
workers: {
// ...
miniflare: {
bindings: { MIGRATIONS: await readD1Migrations(join(__dirname, 'src/database/migrations')) },
},
},
},
},
}
})
Each binding and variable must be mirrored to worker-configuration.d.ts types file located at the root of the service.
// Generated by Wrangler by running `wrangler types --env-interface LoremEnv`
interface LoremEnv {
I_USE: 'ARCH BTW'
LOREM_DB: D1Database
LOREM_KV: KVNamespace
LOREM_QUEUE: Queue
}
Same thing need to be done for env.d.ts file at the root of test directory.
declare module 'cloudflare:test' {
interface ProvidedEnv extends Env {
I_USE: 'ARCH BTW'
LOREM_DB: D1Database
LOREM_KV: KVNamespace
LOREM_QUEUE: Queue
}
export const SELF: Service<import('../src/index').default>
}
Last, but not least. TypeScript itself must be configured that services know types of other services. Configure tsconfig.json at the root of the repository. Services expose types at the repository scope, which are stored within the @types directory.
{
"compilerOptions": {
// ...
"types": [
"@services/lorem/worker-configuration.d.ts",
"@services/ipsum/worker-configuration.d.ts",
"@services/dolor/worker-configuration.d.ts",
// ...
],
},
}