db: business-layer schema — ledger/device event split, tariffs, permits, sessions

Implements the wiki design in packages/db + packages/shared.

Event split: rename events -> ledger_events (signed business ledger) and add
device_events (unsigned telemetry). ledger_events gains a signed JSON payload
(amount/tariffVersionId/sessionRef/tender…) + keyId; canonicalize() includes
the payload via sorted-key serialization so business data is tamper-evident.
Raw Dingtian input now writes device_events, not a signed input_received.

New tables: tariffs + immutable tariff_versions (composable/versioned, currency
+ FX-ready), permits (+ permit_credentials, permit_plates; maxConcurrent default
1), blocklist, sessions (rebuildable projection cache — not a source of truth).

shared: split ParkingEvent/Type into LedgerEvent/LedgerEventType + DeviceEventKind;
add LedgerPayload, Tender, TariffStructure/TariffBlock.

Regenerated a single baseline migration (no production chain data existed).
Verified: chain appends + verifyChain ok; tampering a payment payload breaks
the signature. Full repo builds (5/5).
This commit is contained in:
2026-06-15 18:13:35 +02:00
parent 9a4c7ee27b
commit 8c2cf93067
11 changed files with 958 additions and 349 deletions
+113
View File
@@ -0,0 +1,113 @@
CREATE TABLE `blocklist` (
`id` text PRIMARY KEY NOT NULL,
`kind` text NOT NULL,
`value` text NOT NULL,
`reason` text,
`active` integer DEFAULT true NOT NULL,
`added_by` text,
`added_at` text DEFAULT (current_timestamp) NOT NULL
);
--> statement-breakpoint
CREATE TABLE `device_events` (
`id` text PRIMARY KEY NOT NULL,
`device_id` text,
`lane` integer,
`category` text,
`kind` text NOT NULL,
`detail` text,
`occurred_at` text DEFAULT (current_timestamp) NOT NULL
);
--> statement-breakpoint
CREATE TABLE `lane_devices` (
`id` text PRIMARY KEY NOT NULL,
`lane` integer NOT NULL,
`category` text NOT NULL,
`driver_id` text NOT NULL,
`config` text NOT NULL,
`enabled` integer DEFAULT true NOT NULL,
`created_at` text DEFAULT (current_timestamp) NOT NULL
);
--> statement-breakpoint
CREATE TABLE `ledger_events` (
`id` text PRIMARY KEY NOT NULL,
`index` integer NOT NULL,
`type` text NOT NULL,
`direction` text,
`lane` integer NOT NULL,
`source` text,
`identity` text,
`payload` text,
`occurred_at` text NOT NULL,
`prev_hash` text,
`signature` text NOT NULL,
`key_id` text NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `ledger_events_index_unique` ON `ledger_events` (`index`);--> statement-breakpoint
CREATE TABLE `permit_credentials` (
`id` text PRIMARY KEY NOT NULL,
`permit_id` text NOT NULL,
`kind` text NOT NULL,
`value` text NOT NULL
);
--> statement-breakpoint
CREATE TABLE `permit_plates` (
`id` text PRIMARY KEY NOT NULL,
`permit_id` text NOT NULL,
`plate` text NOT NULL
);
--> statement-breakpoint
CREATE TABLE `permits` (
`id` text PRIMARY KEY NOT NULL,
`holder_name` text,
`contact` text,
`max_concurrent` integer DEFAULT 1,
`valid_from` text,
`valid_to` text,
`status` text DEFAULT 'active' NOT NULL,
`created_at` text DEFAULT (current_timestamp) NOT NULL
);
--> statement-breakpoint
CREATE TABLE `sessions` (
`id` text PRIMARY KEY NOT NULL,
`lane` integer,
`identity` text,
`source` text,
`permit_id` text,
`entered_at` text NOT NULL,
`exited_at` text,
`state` text DEFAULT 'open' NOT NULL,
`last_event_index` integer
);
--> statement-breakpoint
CREATE TABLE `setup_state` (
`id` integer PRIMARY KEY NOT NULL,
`completed_at` text
);
--> statement-breakpoint
CREATE TABLE `tariff_versions` (
`id` text PRIMARY KEY NOT NULL,
`tariff_id` text NOT NULL,
`effective_from` text NOT NULL,
`currency` text NOT NULL,
`structure` text NOT NULL,
`created_by` text,
`created_at` text DEFAULT (current_timestamp) NOT NULL
);
--> statement-breakpoint
CREATE TABLE `tariffs` (
`id` text PRIMARY KEY NOT NULL,
`scope` text DEFAULT 'site' NOT NULL,
`name` text NOT NULL,
`created_at` text DEFAULT (current_timestamp) NOT NULL
);
--> statement-breakpoint
CREATE TABLE `users` (
`id` text PRIMARY KEY NOT NULL,
`username` text NOT NULL,
`password_hash` text NOT NULL,
`role` text NOT NULL,
`created_at` text DEFAULT (current_timestamp) NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `users_username_unique` ON `users` (`username`);