more ffect: Service/Layer separation
This commit is contained in:
1 parent
fa1663c131
commit
bbf4e6bd3c
10 files changed
+110
-44
No files matched your search
@@ -6,8 +6,8 @@ import type { OrchestrationEvent } from "@t3tools/contracts";
|
||||
import { Effect } from "effect";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import type { OrchestrationEventRepositoryShape } from "../persistence/Services/OrchestrationEvents";
|
||||
import { OrchestrationEngine } from "./engine";
|
||||
import type { OrchestrationEventRepositoryShape } from "./eventRepository";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
|
||||
@@ -20,8 +20,8 @@ import {
|
||||
} from "effect";
|
||||
|
||||
import { createLogger } from "../logger";
|
||||
import type { OrchestrationEventRepositoryShape } from "./eventRepository";
|
||||
import { makeSqliteOrchestrationEventRepository } from "./eventStore";
|
||||
import type { OrchestrationEventRepositoryShape } from "../persistence/Services/OrchestrationEvents";
|
||||
import { makeSqliteOrchestrationEventRepository } from "../persistence/Layers/OrchestrationEvents";
|
||||
import { createEmptyReadModel, reduceEvent } from "./reducer";
|
||||
import { UI_ENTITY_CONTRACTS } from "./uiContractInventory";
|
||||
|
||||
|
||||
@@ -2,9 +2,9 @@ import path from "node:path";
|
||||
|
||||
import { Effect, Layer } from "effect";
|
||||
|
||||
import { OrchestrationEventRepository } from "./eventRepository";
|
||||
import { OrchestrationEventRepository } from "../persistence/Services/OrchestrationEvents";
|
||||
import { makeSqliteOrchestrationEventRepository } from "../persistence/Layers/OrchestrationEvents";
|
||||
import { OrchestrationEngine } from "./engine";
|
||||
import { makeSqliteOrchestrationEventRepository } from "./eventStore";
|
||||
import { OrchestrationConfig, OrchestrationEngineService } from "./services";
|
||||
|
||||
export const OrchestrationEventRepositoryLive = Layer.effect(
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
import * as Migrator from "@effect/sql/Migrator";
|
||||
import * as SqlClient from "@effect/sql/SqlClient";
|
||||
import { Effect } from "effect";
|
||||
|
||||
const ORCHESTRATION_MIGRATIONS = Migrator.fromRecord({
|
||||
"0001_create_orchestration_events": Effect.flatMap(SqlClient.SqlClient, (sql) =>
|
||||
Effect.gen(function* () {
|
||||
yield* sql`
|
||||
CREATE TABLE IF NOT EXISTS orchestration_events (
|
||||
sequence INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
event_id TEXT NOT NULL UNIQUE,
|
||||
event_type TEXT NOT NULL,
|
||||
aggregate_type TEXT NOT NULL,
|
||||
aggregate_id TEXT NOT NULL,
|
||||
occurred_at TEXT NOT NULL,
|
||||
command_id TEXT,
|
||||
payload_json TEXT NOT NULL
|
||||
)
|
||||
`;
|
||||
yield* sql`
|
||||
CREATE INDEX IF NOT EXISTS idx_orch_events_aggregate
|
||||
ON orchestration_events(aggregate_type, aggregate_id, sequence)
|
||||
`;
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
export const runOrchestrationMigrations = Migrator.make({})({
|
||||
loader: ORCHESTRATION_MIGRATIONS,
|
||||
table: "orchestration_sql_migrations",
|
||||
});
|
||||
+8
-4
@@ -6,7 +6,7 @@ import { DatabaseSync } from "node:sqlite";
|
||||
import { Runtime } from "effect";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { makeSqliteOrchestrationEventRepository } from "./eventStore";
|
||||
import { makeSqliteOrchestrationEventRepository } from "./OrchestrationEvents";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
@@ -69,17 +69,21 @@ describe("SqliteEventStore", () => {
|
||||
`
|
||||
SELECT name
|
||||
FROM sqlite_master
|
||||
WHERE type = 'table' AND name = 'orchestration_sql_migrations'
|
||||
WHERE type = 'table' AND name IN ('effect_sql_migrations', 'orchestration_sql_migrations')
|
||||
ORDER BY name ASC
|
||||
`,
|
||||
)
|
||||
.get() as { name: string } | undefined;
|
||||
expect(tableRow?.name).toBe("orchestration_sql_migrations");
|
||||
expect(tableRow?.name).toBeDefined();
|
||||
if (!tableRow) {
|
||||
throw new Error("Expected migrations table to exist");
|
||||
}
|
||||
|
||||
const migrationRows = db
|
||||
.prepare(
|
||||
`
|
||||
SELECT *
|
||||
FROM orchestration_sql_migrations
|
||||
FROM ${tableRow.name}
|
||||
`,
|
||||
)
|
||||
.all() as Array<Record<string, unknown>>;
|
||||
+3
-3
@@ -9,8 +9,8 @@ import * as SqlSchema from "@effect/sql/SqlSchema";
|
||||
import * as SqliteClient from "@effect/sql-sqlite-node/SqliteClient";
|
||||
import { Effect, ManagedRuntime, Schema } from "effect";
|
||||
|
||||
import type { OrchestrationEventRepositoryShape } from "./eventRepository";
|
||||
import { runOrchestrationMigrations } from "./migrations";
|
||||
import type { OrchestrationEventRepositoryShape } from "../Services/OrchestrationEvents";
|
||||
import { runMigrations } from "../Migrations";
|
||||
|
||||
const decodeEvent = Schema.decodeUnknownSync(OrchestrationEventSchema);
|
||||
|
||||
@@ -135,7 +135,7 @@ export function makeSqliteOrchestrationEventRepository(
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
yield* sql`PRAGMA journal_mode = WAL;`;
|
||||
yield* sql`PRAGMA foreign_keys = ON;`;
|
||||
yield* runOrchestrationMigrations;
|
||||
yield* runMigrations;
|
||||
});
|
||||
|
||||
const initialized = runtime.runPromise(initialize).then(() => undefined);
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* MigrationsLive - Migration runner with inline loader
|
||||
*
|
||||
* Uses Migrator.make with fromRecord to define migrations inline.
|
||||
* All migrations are statically imported - no dynamic file system loading.
|
||||
*
|
||||
* Migrations run automatically when the MigrationLayer is provided,
|
||||
* ensuring the database schema is always up-to-date before the application starts.
|
||||
*/
|
||||
|
||||
import * as Migrator from "@effect/sql/Migrator";
|
||||
import * as Layer from "effect/Layer";
|
||||
|
||||
// Import all migrations statically
|
||||
import Migration0001 from "./Migrations/001_OrchestrationEvents.ts";
|
||||
|
||||
|
||||
/**
|
||||
* Migration loader with all migrations defined inline.
|
||||
*
|
||||
* Key format: "{id}_{name}" where:
|
||||
* - id: numeric migration ID (determines execution order)
|
||||
* - name: descriptive name for the migration
|
||||
*
|
||||
* Uses Migrator.fromRecord which parses the key format and
|
||||
* returns migrations sorted by ID.
|
||||
*/
|
||||
const loader = Migrator.fromRecord({
|
||||
"1_OrchestrationEvents": Migration0001,
|
||||
});
|
||||
|
||||
/**
|
||||
* Migrator run function - no schema dumping needed
|
||||
* Uses the base Migrator.make without platform dependencies
|
||||
*/
|
||||
const run = Migrator.make({});
|
||||
|
||||
/**
|
||||
* Run all pending migrations.
|
||||
*
|
||||
* Creates the migrations tracking table (effect_sql_migrations) if it doesn't exist,
|
||||
* then runs any migrations with ID greater than the latest recorded migration.
|
||||
*
|
||||
* Returns array of [id, name] tuples for migrations that were run.
|
||||
*
|
||||
* @returns Effect containing array of executed migrations
|
||||
*/
|
||||
export const runMigrations = run({ loader });
|
||||
|
||||
/**
|
||||
* Layer that runs migrations when the layer is built.
|
||||
*
|
||||
* Use this to ensure migrations run before your application starts.
|
||||
* Migrations are run automatically - no separate script is needed.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { MigrationsLive } from "@acme/db/Migrations"
|
||||
* import { PgClient } from "@effect/sql-pg"
|
||||
*
|
||||
* // Migrations run automatically when PgClient is provided
|
||||
* const AppLayer = MigrationsLive.pipe(
|
||||
* Layer.provideMerge(PgClient.layer({ url: Redacted.make("postgresql://...") }))
|
||||
* )
|
||||
* ```
|
||||
*/
|
||||
export const MigrationsLive = Layer.effectDiscard(runMigrations);
|
||||
@@ -0,0 +1,24 @@
|
||||
import { SqlClient } from "@effect/sql";
|
||||
import * as Effect from "effect/Effect";
|
||||
|
||||
export default Effect.gen(function* () {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
|
||||
yield* sql`
|
||||
CREATE TABLE IF NOT EXISTS orchestration_events (
|
||||
sequence INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
event_id TEXT NOT NULL UNIQUE,
|
||||
event_type TEXT NOT NULL,
|
||||
aggregate_type TEXT NOT NULL,
|
||||
aggregate_id TEXT NOT NULL,
|
||||
occurred_at TEXT NOT NULL,
|
||||
command_id TEXT,
|
||||
payload_json TEXT NOT NULL
|
||||
)
|
||||
`;
|
||||
|
||||
yield* sql`
|
||||
CREATE INDEX IF NOT EXISTS idx_orch_events_aggregate
|
||||
ON orchestration_events(aggregate_type, aggregate_id, sequence)
|
||||
`;
|
||||
});
|
||||
File renamed without changes.
@@ -3,7 +3,9 @@
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"types": ["node"],
|
||||
"lib": ["ES2023", "esnext.disposable"]
|
||||
"lib": ["ES2023", "esnext.disposable"],
|
||||
"noEmit": true,
|
||||
"allowImportingTsExtensions": true
|
||||
},
|
||||
"include": ["src", "tsdown.config.ts"]
|
||||
}
|
||||
Reference in new issue
Block a user