feat: add module schema discovery and validation
This commit is contained in:
@@ -24,10 +24,14 @@
|
||||
"devDependencies": {
|
||||
"@biomejs/biome": "2.5.3",
|
||||
"@types/node": "24.13.3",
|
||||
"drizzle-orm": "0.45.2",
|
||||
"typescript": "7.0.2",
|
||||
"vitest": "4.1.10"
|
||||
},
|
||||
"ekklesia": {
|
||||
"layer": "module-sdk"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"drizzle-orm": "0.45.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import {
|
||||
type ModuleId,
|
||||
type ModuleSchemaDefinition,
|
||||
moduleTableName,
|
||||
moduleTablePrefix,
|
||||
parseModuleSchemaDefinition,
|
||||
} from "@ekklesia/contracts";
|
||||
import { pgTableCreator } from "drizzle-orm/pg-core";
|
||||
|
||||
export function createModuleTable(moduleId: ModuleId) {
|
||||
moduleTablePrefix(moduleId);
|
||||
return pgTableCreator((localName) => moduleTableName(moduleId, localName));
|
||||
}
|
||||
|
||||
export function defineModuleSchema<const Definition extends ModuleSchemaDefinition>(
|
||||
definition: Definition,
|
||||
): Readonly<Definition> {
|
||||
return parseModuleSchemaDefinition(definition) as Readonly<Definition>;
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
export * from "@ekklesia/contracts";
|
||||
export * from "./define-module-runtime.js";
|
||||
export * from "./define-module-schema.js";
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { getTableConfig, integer } from "drizzle-orm/pg-core";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createModuleTable, defineModuleSchema } from "../src/index.js";
|
||||
|
||||
describe("module schema authoring", () => {
|
||||
it("creates namespaced PostgreSQL tables and a typed frozen definition", () => {
|
||||
const moduleTable = createModuleTable("care-team");
|
||||
const members = moduleTable("members", { id: integer().primaryKey() });
|
||||
const definition = defineModuleSchema({
|
||||
moduleId: "care-team",
|
||||
tables: { members },
|
||||
});
|
||||
|
||||
expect(getTableConfig(members).name).toBe("care_team_members");
|
||||
expect(definition.tables.members).toBe(members);
|
||||
expect(Object.isFrozen(definition)).toBe(true);
|
||||
expect(Object.isFrozen(definition.tables)).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects invalid module and logical table names", () => {
|
||||
expect(() => createModuleTable("Not Valid")).toThrow();
|
||||
const moduleTable = createModuleTable("example");
|
||||
expect(() => moduleTable("Not Valid", { id: integer() })).toThrow();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user