import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField, type TailorAnyDBType } from "@tailor-platform/sdk"; const builtins = (params: { userType?: TailorAnyDBType }) => { // Cross-module FK: relation only when the foreign type is injected; plain uuid at codegen const userId = db .uuid() .description("Foreign key to the user-management User this Worker extends"); return { userId: params.userType ? userId.unique().relation({ type: "1-1", toward: { type: params.userType, as: "user" }, backward: "worker", }) : userId.unique(), // Stable, human-facing identifier independent of the User's login identity workerCode: db .string() .unique() .description("Stable worker code that survives employee-code reassignment"), }; }; type ReservedFields = keyof ReturnType | CommonReservedFields; export interface CreateWorkerTypeParams> { fields?: NoReservedFields; userType?: TailorAnyDBType; } export function createWorkerType>( params: CreateWorkerTypeParams, ) { return db .table("Worker", { ...builtins(params), ...((params.fields ?? {}) as F), ...db.fields.timestamps(), }) .permission(defaultPermission) .gqlPermission(defaultGqlPermission); } // Codegen instance with no cross-module types; real FK injected via module.ts export const worker = createWorkerType({});