@rebasepro/server-postgres 0.19.1 → 0.19.2-canary.g09316f6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{BranchService-ByLfM_Tt.js → BranchService-W3DMfcZZ.js} +2 -2
- package/dist/{BranchService-ByLfM_Tt.js.map → BranchService-W3DMfcZZ.js.map} +1 -1
- package/dist/PostgresBackendDriver.d.ts +81 -7
- package/dist/PostgresBootstrapper.d.ts +10 -0
- package/dist/{backup-cli-oM9WQVlV.js → backup-cli-DSpQyqcG.js} +2 -2
- package/dist/{backup-cli-oM9WQVlV.js.map → backup-cli-DSpQyqcG.js.map} +1 -1
- package/dist/{backup-service-Br3tqwsy.js → backup-service-DA7a6SUV.js} +2 -2
- package/dist/{backup-service-Br3tqwsy.js.map → backup-service-DA7a6SUV.js.map} +1 -1
- package/dist/{cli-errors-DxN4yzdH.js → cli-errors-C3g_kHBw.js} +137 -18
- package/dist/cli-errors-C3g_kHBw.js.map +1 -0
- package/dist/cli-errors.d.ts +19 -0
- package/dist/cli-helpers.d.ts +47 -0
- package/dist/cli.js +233 -19
- package/dist/cli.js.map +1 -1
- package/dist/{collection-index-BHgT_TKd.js → collection-index-DdnsxiJ_.js} +14 -4
- package/dist/{collection-index-BHgT_TKd.js.map → collection-index-DdnsxiJ_.js.map} +1 -1
- package/dist/collections/validate-relations.d.ts +7 -0
- package/dist/column-plan-helpers-CpILzHJS.js +145 -0
- package/dist/column-plan-helpers-CpILzHJS.js.map +1 -0
- package/dist/{doctor-D7HWmGzs.js → doctor-D5SrGJ5P.js} +175 -61
- package/dist/doctor-D5SrGJ5P.js.map +1 -0
- package/dist/{ensure-collection-policies-2c2NV8zX.js → ensure-collection-policies-CHO0moXQ.js} +5 -5
- package/dist/{ensure-collection-policies-2c2NV8zX.js.map → ensure-collection-policies-CHO0moXQ.js.map} +1 -1
- package/dist/{ensure-collection-tables-D4wJzM4y.js → ensure-collection-tables-Bkvehxdm.js} +318 -312
- package/dist/ensure-collection-tables-Bkvehxdm.js.map +1 -0
- package/dist/{ensure-tables-CIojK-Rx.js → ensure-tables-CYMtuuOd.js} +2 -2
- package/dist/{ensure-tables-CIojK-Rx.js.map → ensure-tables-CYMtuuOd.js.map} +1 -1
- package/dist/generate-drizzle-schema-logic-Dfyf_MRu.js +228 -0
- package/dist/generate-drizzle-schema-logic-Dfyf_MRu.js.map +1 -0
- package/dist/{generate-drizzle-schema-CwEgVyjO.js → generate-drizzle-schema-vSK-VdYT.js} +3 -3
- package/dist/generate-drizzle-schema-vSK-VdYT.js.map +1 -0
- package/dist/generate-postgres-ddl-logic-CR2xcS7e.js +475 -0
- package/dist/generate-postgres-ddl-logic-CR2xcS7e.js.map +1 -0
- package/dist/{generated-schema-staleness-DQdfJl2A.js → generated-schema-staleness-DRQe2BpC.js} +81 -3
- package/dist/{generated-schema-staleness-DQdfJl2A.js.map → generated-schema-staleness-DRQe2BpC.js.map} +1 -1
- package/dist/history/HistoryService.d.ts +23 -4
- package/dist/index.es.js +1991 -580
- package/dist/index.es.js.map +1 -1
- package/dist/introspect-db-logic-C6LQdTxj.js.map +1 -1
- package/dist/plan-schema-Hgl62S-w.js +1531 -0
- package/dist/plan-schema-Hgl62S-w.js.map +1 -0
- package/dist/{policy-drift-kNJ6X2Uj.js → policy-drift-B0GDRh7_.js} +2 -2
- package/dist/{policy-drift-kNJ6X2Uj.js.map → policy-drift-B0GDRh7_.js.map} +1 -1
- package/dist/{rls-bootstrap-sql-DLDnUjtx.js → rls-bootstrap-sql-BlzsOUtz.js} +111 -8
- package/dist/{rls-bootstrap-sql-DLDnUjtx.js.map → rls-bootstrap-sql-BlzsOUtz.js.map} +1 -1
- package/dist/{rls-enforcement-DeYEjYLt.js → rls-enforcement-BV12vBwQ.js} +2 -1
- package/dist/rls-enforcement-BV12vBwQ.js.map +1 -0
- package/dist/schema/catalogue-schema.d.ts +97 -0
- package/dist/schema/collection-index.d.ts +9 -0
- package/dist/schema/column-plan-helpers.d.ts +125 -0
- package/dist/schema/config-relations.d.ts +32 -0
- package/dist/schema/doctor-cli.js +4 -4
- package/dist/schema/doctor.d.ts +9 -0
- package/dist/schema/dynamic-tables.d.ts +27 -6
- package/dist/schema/ensure-collection-tables.d.ts +31 -268
- package/dist/schema/generate-drizzle-schema-logic.d.ts +26 -8
- package/dist/schema/generate-drizzle-schema.js +1 -1
- package/dist/schema/generate-postgres-ddl-logic.d.ts +84 -168
- package/dist/schema/generate-postgres-ddl.js +8 -2
- package/dist/schema/generate-postgres-ddl.js.map +1 -1
- package/dist/schema/generated-column-conflicts.d.ts +84 -0
- package/dist/schema/generated-schema-diff.d.ts +56 -0
- package/dist/schema/generated-schema-staleness.d.ts +33 -0
- package/dist/schema/introspect-db-logic.d.ts +2 -0
- package/dist/schema/plan/diff-plan.d.ts +220 -0
- package/dist/schema/plan/plan-schema.d.ts +84 -0
- package/dist/schema/plan/render-ddl.d.ts +62 -0
- package/dist/schema/plan/render-drizzle.d.ts +31 -0
- package/dist/schema/plan/types.d.ts +410 -0
- package/dist/schema/plan/updated-at-trigger.d.ts +22 -0
- package/dist/schema/relation-names.d.ts +28 -0
- package/dist/schema/search-column.d.ts +9 -0
- package/dist/security/rls-enforcement.d.ts +17 -0
- package/dist/services/FetchService.d.ts +176 -36
- package/dist/services/PersistService.d.ts +17 -4
- package/dist/services/RelationService.d.ts +11 -3
- package/dist/services/collection-helpers.d.ts +16 -2
- package/dist/services/dataService.d.ts +29 -2
- package/dist/services/field-op-sql.d.ts +16 -0
- package/dist/services/field-viewer.d.ts +19 -0
- package/dist/services/realtimeService.d.ts +35 -1
- package/dist/services/row-pipeline.d.ts +29 -5
- package/dist/services/soft-delete.d.ts +73 -0
- package/dist/utils/pg-error-utils.d.ts +41 -1
- package/dist/{vector-index-DkWxm2de.js → vector-index-CMJKb3C9.js} +3 -14
- package/dist/{vector-index-DkWxm2de.js.map → vector-index-CMJKb3C9.js.map} +1 -1
- package/package.json +18 -10
- package/dist/cli-errors-DxN4yzdH.js.map +0 -1
- package/dist/doctor-D7HWmGzs.js.map +0 -1
- package/dist/ensure-collection-tables-D4wJzM4y.js.map +0 -1
- package/dist/generate-drizzle-schema-CwEgVyjO.js.map +0 -1
- package/dist/generate-drizzle-schema-logic-so2Z_lyC.js +0 -528
- package/dist/generate-drizzle-schema-logic-so2Z_lyC.js.map +0 -1
- package/dist/generate-postgres-ddl-logic-CtnpIBVv.js +0 -1312
- package/dist/generate-postgres-ddl-logic-CtnpIBVv.js.map +0 -1
- package/dist/rls-enforcement-DeYEjYLt.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"introspect-db-logic-C6LQdTxj.js","names":[],"sources":["../src/schema/introspect-db-naming.ts","../src/schema/introspect-db-inference.ts","../src/schema/introspect-db-types.ts","../src/schema/introspect-db-structure.ts","../src/schema/introspect-db-logic.ts"],"sourcesContent":["/**\n * Naming helpers shared by the introspection modules. These live apart from\n * `introspect-db-logic.ts` because the inference pass needs them too, and\n * importing them from there would close a cycle back through this module.\n */\n\n/**\n * Convert a snake_case name to a human-readable Title Case label.\n * e.g. \"created_at\" -> \"Created At\", \"customer_id\" -> \"Customer Id\"\n */\nexport function humanize(snakeName: string): string {\n return snakeName\n .replace(/_/g, \" \")\n .replace(/\\b\\w/g, (c) => c.toUpperCase());\n}\n","import { humanize } from \"./introspect-db-naming\";\n\nexport interface InferenceResult {\n propType?: string; // If the inference changes the base type\n extra?: string; // String to append to the property definition\n}\n\nconst ISO_8601_REGEX = /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?(?:Z|[+-]\\d{2}:\\d{2})$/;\nconst UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;\nconst CUID_REGEX = /^c[^\\s-]{7,}$/i; // Basic CUID check\nconst COLOR_HEX_REGEX = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;\n\nexport function inferPropertyFromData(\n columnName: string,\n pgDataType: string,\n currentPropType: string,\n sampleValues: unknown[],\n isPk: boolean,\n /**\n * False when generating for a project without `@rebasepro/cms-types`, where\n * `BaseProperty` declares no `admin` field and the block below would not\n * compile. The type-level inferences (`propType`, `url`, `storage`) are\n * unaffected — only the form-widget hints are dropped.\n */\n emitAdmin = true\n): InferenceResult {\n const result: InferenceResult = {};\n const extraLines: string[] = [];\n // Collected separately and emitted as one block at the end. Pushed inline,\n // two admin-owned options from different branches produced two `admin: {`\n // blocks in one property — a duplicate key, which is also a compile error.\n const adminOptions: string[] = [];\n\n // Filter out null/undefined for analysis\n const validValues = sampleValues.filter(v => v !== null && v !== undefined && v !== \"\");\n\n if (validValues.length === 0) {\n return result; // Not enough data\n }\n\n const colNameLower = columnName.toLowerCase();\n\n // ── Number Analysis ──────────────────────────────────────────────────\n if (currentPropType === \"number\") {\n // Boolean stored as int\n const allZeroOrOne = validValues.every(v => v === 0 || v === 1 || v === \"0\" || v === \"1\");\n if (allZeroOrOne) {\n result.propType = \"boolean\";\n return result; // Don't do min/max if boolean\n }\n\n // Min / Max constraints\n const numValues = validValues.map(v => Number(v)).filter(v => !isNaN(v));\n if (numValues.length === validValues.length) {\n const min = Math.min(...numValues);\n const max = Math.max(...numValues);\n // Example heuristic: percentages\n if (min >= 0 && max <= 100 && (colNameLower.includes(\"percent\") || colNameLower.includes(\"rate\") || colNameLower.includes(\"score\"))) {\n extraLines.push(\" validation: {\\n min: 0,\\n max: 100\\n }\");\n } else if (min >= 0 && (colNameLower.includes(\"count\") || colNameLower.includes(\"total\") || colNameLower.includes(\"amount\"))) {\n extraLines.push(\" validation: {\\n min: 0\\n }\");\n }\n }\n\n // No currency heuristic. `admin.currency` was never a declared option and\n // nothing reads it, so a column called `price` got a block of config that did\n // nothing — invisible because this generator emits *source text*, which no\n // typechecker ever saw. Reinstate it by adding `currency` to\n // AdminNumberOptions first, and something that renders it.\n }\n\n // ── JSON / JSONB Analysis ────────────────────────────────────────────\n if (currentPropType === \"map\" || pgDataType.includes(\"json\")) {\n // PostGres jsonb can be object or array\n let allArrays = true;\n let allObjects = true;\n\n for (const v of validValues) {\n let parsed = v;\n if (typeof v === \"string\") {\n try { parsed = JSON.parse(v); } catch (e) { allArrays = false; allObjects = false; break; }\n }\n if (Array.isArray(parsed)) {\n allObjects = false;\n } else if (typeof parsed === \"object\" && parsed !== null) {\n allArrays = false;\n } else {\n allArrays = false;\n allObjects = false;\n }\n }\n\n if (allArrays && !allObjects) {\n result.propType = \"array\";\n // Infer inner type\n let allNumbers = true;\n let allStrings = true;\n for (const v of validValues) {\n const parsed = typeof v === \"string\" ? JSON.parse(v) : v;\n for (const item of parsed) {\n if (typeof item !== \"number\") allNumbers = false;\n if (typeof item !== \"string\") allStrings = false;\n }\n }\n const innerType = allNumbers ? \"number\" : allStrings ? \"string\" : \"map\";\n extraLines.push(` of: { name: \"${humanize(columnName)} Item\", type: \"${innerType}\" }`);\n } else {\n result.propType = \"map\";\n\n // Infer inner schema\n if (allObjects && validValues.length > 0) {\n const schema: Record<string, string> = {};\n for (const v of validValues) {\n const parsed = typeof v === \"string\" ? JSON.parse(v) : v;\n for (const [k, val] of Object.entries(parsed)) {\n if (val === null || val === undefined) continue;\n const type = typeof val;\n if (type === \"string\" || type === \"number\" || type === \"boolean\") {\n if (!schema[k]) schema[k] = type;\n else if (schema[k] !== type) schema[k] = \"mixed\";\n } else if (Array.isArray(val)) {\n if (!schema[k]) schema[k] = \"array\";\n else if (schema[k] !== \"array\") schema[k] = \"mixed\";\n } else if (type === \"object\") {\n if (!schema[k]) schema[k] = \"map\";\n else if (schema[k] !== \"map\") schema[k] = \"mixed\";\n }\n }\n }\n\n const keys = Object.keys(schema).filter(k => schema[k] !== \"mixed\");\n if (keys.length > 0) {\n const props = keys.map(k => {\n return `\\n ${k}: { name: \"${humanize(k)}\", type: \"${schema[k]}\" }`;\n }).join(\",\");\n extraLines.push(` properties: {${props}\\n }`);\n } else {\n extraLines.push(\" keyValue: true\");\n }\n } else {\n extraLines.push(\" keyValue: true\");\n }\n }\n }\n\n // ── String Analysis ──────────────────────────────────────────────────\n if (currentPropType === \"string\") {\n // Date/Time Strings\n if (validValues.every(v => typeof v === \"string\" && ISO_8601_REGEX.test(v))) {\n result.propType = \"date\";\n return result;\n }\n\n // Implicit Enums\n const uniqueValues = new Set(validValues);\n // Determine the maximum length among unique values\n let maxEnumLength = 0;\n for (const v of uniqueValues) {\n if (typeof v === \"string\" && v.length > maxEnumLength) {\n maxEnumLength = v.length;\n }\n }\n\n // Ensure no empty string, max length makes sense, and fewer unique values than total values (unless small total)\n if (uniqueValues.size > 0 && uniqueValues.size <= 5 && maxEnumLength <= 50 && validValues.length > uniqueValues.size && !uniqueValues.has(\"\")) {\n const isLikelyId = isPk || colNameLower.endsWith(\"_id\");\n if (!isLikelyId) {\n const enumEntries = Array.from(uniqueValues).map(v => `{ id: ${JSON.stringify(v)}, label: ${JSON.stringify(humanize(v as string))} }`).join(\", \");\n extraLines.push(` enum: [${enumEntries}]`);\n result.extra = extraLines.length > 0 ? \"\\n\" + extraLines.join(\",\\n\") + \",\" : \"\";\n return result; // Skip other string checks if it's an enum\n }\n }\n\n // UUID / CUID Detection\n const allUuid = validValues.every(v => typeof v === \"string\" && UUID_REGEX.test(v));\n const allCuid = validValues.every(v => typeof v === \"string\" && CUID_REGEX.test(v));\n if (allUuid) {\n if (isPk) extraLines.push(\" isId: \\\"uuid\\\"\");\n } else if (allCuid) {\n if (isPk) extraLines.push(\" isId: \\\"cuid\\\"\");\n }\n\n // No colour heuristic. `admin.color` was never a declared option and nothing\n // reads it — the same dead branch `currency` was. This generator emits *source\n // text*, so no typechecker ever saw either. Reinstate by adding the option and\n // something that renders it first.\n\n // Text Lengths, Multiline & Markdown\n let maxLength = 0;\n let hasNewlines = false;\n let hasMarkdown = false;\n for (const v of validValues) {\n if (typeof v === \"string\") {\n if (v.length > maxLength) maxLength = v.length;\n if (v.includes(\"\\n\")) hasNewlines = true;\n if (/^#{1,6}\\s+.+/m.test(v) || /\\*\\*.+\\*\\*/.test(v) || /\\[.+\\]\\(.+\\)/.test(v) || /^\\s*[-*]\\s+.+/m.test(v)) {\n hasMarkdown = true;\n }\n }\n }\n\n // `multiline` and `markdown` are *admin* property options, so they have\n // to sit inside the `admin` block. Emitted at the top of the property —\n // where they were — `PostgresCollectionConfig` does not declare them and\n // the generated file does not compile.\n if (hasMarkdown) {\n adminOptions.push(\"multiline: true\", \"markdown: true\");\n } else if (hasNewlines || maxLength > 100) {\n adminOptions.push(\"multiline: true\");\n }\n\n if (maxLength > 0 && maxLength < 10000) { // arbitrary cap to avoid huge limits\n // Pad the max length slightly for the constraint\n const paddedMax = Math.ceil((maxLength * 1.5) / 10) * 10;\n // Only add length constraint if it seems like a bounded string, not a long text\n if (paddedMax < 255) {\n extraLines.push(` validation: {\\n max: ${paddedMax}\\n }`);\n }\n }\n\n // Storage & Media (Extracted from old logic)\n const isUrl = colNameLower.endsWith(\"_url\") || colNameLower.endsWith(\"_uri\") || colNameLower.endsWith(\"_link\");\n const isMedia = colNameLower.includes(\"image\") || colNameLower.includes(\"avatar\") || colNameLower.includes(\"photo\") || colNameLower.includes(\"logo\") || colNameLower.includes(\"cover\");\n\n const allAbsoluteUrls = validValues.every(v => typeof v === \"string\" && (v.startsWith(\"http://\") || v.startsWith(\"https://\")));\n if (allAbsoluteUrls) {\n const isImage = validValues.some(v => typeof v === \"string\" && v.match(/\\.(jpeg|jpg|gif|png|webp|svg)/i));\n if (isImage || isMedia) {\n extraLines.push(\" url: true\");\n adminOptions.push(\"urlPreview: \\\"image\\\"\");\n } else {\n extraLines.push(\" url: true\");\n }\n } else {\n const hasFileExtension = validValues.some(v => typeof v === \"string\" && v.match(/\\.[a-zA-Z0-9]+$/));\n if (hasFileExtension) {\n const firstVal = validValues[0] as string;\n const lastSlash = firstVal.lastIndexOf(\"/\");\n const inferredStoragePath = lastSlash > 0 ? firstVal.substring(0, lastSlash) : \"files\";\n extraLines.push(` storage: {\\n storagePath: \"${inferredStoragePath}\"\\n }`);\n } else if (isUrl) {\n extraLines.push(\" url: true\");\n if (isMedia) adminOptions.push(\"urlPreview: \\\"image\\\"\");\n }\n }\n }\n\n if (emitAdmin && adminOptions.length > 0) {\n extraLines.push(` admin: {\\n${adminOptions.map((o) => ` ${o}`).join(\",\\n\")}\\n }`);\n }\n\n result.extra = extraLines.length > 0 ? \"\\n\" + extraLines.join(\",\\n\") + \",\" : \"\";\n return result;\n}\n","/**\n * The PostgreSQL type → Rebase property type mapping.\n *\n * Split out of `introspect-db-logic` so that the structural analysis can use it\n * without importing the generator, which imports the analysis. Re-exported from\n * `introspect-db-logic` so existing callers keep their import path.\n */\n\n/**\n * Map a PostgreSQL data type to a Rebase property type.\n */\nexport function mapPgType(dataType: string): string {\n const dt = dataType.toLowerCase();\n\n // Interval MUST be checked before numeric (\"interval\" contains \"int\")\n if (dt === \"interval\") return \"string\";\n\n // Array types MUST be checked before numeric (\"_int4\" contains \"int\")\n if (dt === \"array\" || dt.startsWith(\"_\")) return \"array\";\n\n // Numeric types\n if (\n dt.includes(\"int\") || // integer, smallint, bigint\n dt.includes(\"numeric\") ||\n dt.includes(\"decimal\") ||\n dt.includes(\"serial\") || // serial, bigserial\n dt === \"real\" ||\n dt === \"float4\" ||\n dt === \"float8\" ||\n dt === \"double precision\" ||\n dt === \"money\"\n ) {\n return \"number\";\n }\n\n // Boolean\n if (dt.includes(\"bool\")) return \"boolean\";\n\n // Date / Time\n if (dt.includes(\"time\") || dt.includes(\"date\")) return \"date\";\n\n // JSON\n if (dt === \"json\" || dt === \"jsonb\") return \"map\";\n\n // Binary\n if (dt === \"bytea\") return \"binary\";\n\n // Network types\n if (dt === \"inet\" || dt === \"cidr\" || dt === \"macaddr\" || dt === \"macaddr8\") return \"string\";\n\n // UUID\n if (dt === \"uuid\") return \"string\";\n\n // Text/varchar/char — default to string\n return \"string\";\n}\n","/**\n * What a schema's *structure* says about the app on top of it.\n *\n * Introspection has always been a table mirror: one table in, one collection\n * out, one nav entry each, every column a form field. A schema of thirty tables\n * produces thirty sidebar entries, and a panel whose navigation is a list of\n * table names reads as a database browser however good the fields are — which\n * is the actual complaint about generated admin panels, and is structural, not\n * cosmetic.\n *\n * Most of what separates the eight nouns a user navigates by from the thirty\n * tables underneath them is written down in the schema already: which tables\n * only exist to join two others, which are small referenced code lists, which\n * rows cannot outlive a parent row. This module reads that.\n *\n * ## Structure only\n *\n * Nothing here looks at a column or table *name*. Name heuristics — `status`,\n * `*_url`, `image`, `created_at` — are wrong exactly when a schema is not in\n * English, or is domain-specific, or spells things differently, and they are\n * wrong silently. Every rule below is a fact the database enforces: key\n * composition, foreign-key direction and delete rule, uniqueness, nullability,\n * declared type and length, generated-ness, row count.\n *\n * That constraint has a cost, and it is worth stating: a schema that declares\n * nothing beyond `NOT NULL` gives this module very little to work with, and it\n * returns `entity` for everything rather than guessing. Under-classifying is\n * the intended failure mode. A table wrongly hidden from the navigation is a\n * table the user cannot find; a table wrongly left in it is merely the status\n * quo.\n *\n * Pure module: no I/O. Row counts come in on {@link SchemaMetadata.rowCounts},\n * which the caller fills from {@link ./introspect-db-queries.countRowsUpTo} for\n * the tables {@link lookupCandidates} names.\n */\nimport type {\n ForeignKeyRow,\n SchemaMetadata,\n TableColumn,\n TableMeta,\n UniqueConstraintRow\n} from \"./introspect-db-logic\";\nimport { mapPgType } from \"./introspect-db-types\";\nimport type { CheckFactsByTable } from \"./introspect-db-constraints\";\n\n// ── Thresholds ────────────────────────────────────────────────────────\n\n/**\n * The row count above which a referenced table is a real entity rather than a\n * code list. Deliberately low: `pagila.category` has 16 rows and `language` 6,\n * while `actor` has 200 and `country` 109 — the gap between \"a fixed set\n * somebody typed once\" and \"data the app accumulates\" is wide, and picking a\n * number in the middle of it costs nothing.\n */\nexport const LOOKUP_MAX_ROWS = 50;\n\n/**\n * The most payload columns a code list may carry. A code list is an id, a\n * label, and perhaps a sort key or a flag; past that it is a table with\n * attributes, which is an entity.\n */\nexport const LOOKUP_MAX_PAYLOAD_COLUMNS = 3;\n\n/**\n * The most enum values a board can usefully have as columns. A kanban with\n * thirty columns is a horizontally scrolling table.\n */\nexport const KANBAN_MAX_VALUES = 12;\n\n/** Below this, a \"board\" is one or two columns — a filter, not a board. */\nexport const KANBAN_MIN_VALUES = 2;\n\n/**\n * How many columns a generated list view shows before it stops being readable.\n * Only applied when a table has more properties than this; a six-column table\n * gets no `listProperties` at all rather than a restatement of its own columns.\n */\nexport const LIST_PROPERTIES_CAP = 6;\n\n// ── Roles ─────────────────────────────────────────────────────────────\n\n/**\n * What a table *is*, structurally.\n *\n * - `entity` — a thing the app is about. Gets a collection and a nav entry.\n * - `junction` — exists only to relate two other tables. Gets no collection at\n * all; it becomes a many-to-many relation on both sides.\n * - `lookup` — a small, referenced, self-contained code list. Gets a collection,\n * grouped away from the entities rather than listed beside them.\n * - `owned-child` — rows that belong to exactly one parent row and are reached\n * through it. Gets a collection (it is a real table with real rows, and the\n * API still serves it) but no nav entry: it already renders as a tab on its\n * parent.\n */\nexport type TableRole = \"entity\" | \"junction\" | \"lookup\" | \"owned-child\";\n\n/**\n * Why a table was called someone's child, weakest last.\n *\n * Carried into the generated file as a comment. A reader who disagrees with the\n * classification needs to see what it was based on to know which line to change.\n */\nexport type OwnershipEvidence =\n /** The only foreign key declared `ON DELETE CASCADE`. */\n | \"cascade-delete\"\n /** The only foreign key that is part of the table's primary key. */\n | \"identifying-key\"\n /** The only foreign key that is `NOT NULL`. */\n | \"sole-required-key\"\n /** First column of a composite primary key made entirely of foreign keys. */\n | \"leading-key-column\";\n\nexport interface JunctionShape {\n sourceTable: string;\n sourceColumn: string;\n targetTable: string;\n targetColumn: string;\n}\n\nexport interface TableClassification {\n table: string;\n role: TableRole;\n /** One line, in prose, for the generated file. */\n reason: string;\n /** Set when `role === \"owned-child\"`. */\n owner?: { table: string; column: string; evidence: OwnershipEvidence };\n /** Set when `role === \"junction\"`. */\n junction?: JunctionShape;\n}\n\n// ── Column-level structural predicates ────────────────────────────────\n\n/**\n * A timestamp the database maintains: a temporal column defaulting to the\n * transaction clock.\n *\n * This is the structural stand-in for the `created_at`/`updated_at` name check.\n * It is strictly better than the name: it catches `fecha_creacion` and\n * `last_update` (pagila's spelling, which the name list misses), and it does not\n * fire on a user-editable `created_at date` column that has no default and which\n * the name check would wrongly make read-only.\n */\nexport function isAutoTimestamp(column: TableColumn): boolean {\n if (mapPgType(column.data_type) !== \"date\") return false;\n const columnDefault = (column.column_default ?? \"\").toLowerCase();\n if (!columnDefault) return false;\n return /\\b(now\\(\\)|current_timestamp|current_date|current_time|localtimestamp|localtime|transaction_timestamp\\(\\)|statement_timestamp\\(\\)|clock_timestamp\\(\\))/.test(columnDefault);\n}\n\n/** A key the database fills in: identity, serial, or a uuid-generating default. */\nexport function isGeneratedKey(column: TableColumn): boolean {\n if (column.is_identity === \"YES\") return true;\n const columnDefault = (column.column_default ?? \"\").toLowerCase();\n if (!columnDefault) return false;\n return columnDefault.includes(\"nextval(\") ||\n columnDefault.includes(\"gen_random_uuid\") ||\n columnDefault.includes(\"uuid_generate\");\n}\n\n/** A column Postgres computes; writing to it is an error. */\nexport function isGeneratedColumn(column: TableColumn): boolean {\n return column.is_generated === \"ALWAYS\";\n}\n\n/**\n * Types that exist to be searched or indexed, never to be typed into.\n *\n * A `tsvector` column is a derived search index — maintained by a trigger, a\n * generated expression, or an application job — and its contents are lexeme\n * positions, not text. Pagila's `film.fulltext` is one, and introspection used\n * to emit it as an ordinary required string: a mandatory form field whose\n * correct value no user can produce, on the sixth column of the list view.\n */\nexport function isDerivedIndexColumn(column: TableColumn): boolean {\n return column.udt_name === \"tsvector\" || column.udt_name === \"tsquery\";\n}\n\n/** Anything the user cannot meaningfully edit, whatever the reason. */\nexport function isReadOnlyColumn(column: TableColumn): boolean {\n return isGeneratedColumn(column) || isDerivedIndexColumn(column);\n}\n\n/**\n * A string column with a declared maximum length.\n *\n * `varchar(50)` and `text` are the same type to an application but not to the\n * author: choosing a bound is a statement that the value is short and\n * label-like, which is what makes this usable for picking a display column.\n */\nexport function isBoundedString(column: TableColumn): boolean {\n return mapPgType(column.data_type) === \"string\" &&\n typeof column.character_maximum_length === \"number\" &&\n column.character_maximum_length > 0;\n}\n\n/**\n * A column carrying data rather than structure: not a key, not a foreign key,\n * not a database-maintained timestamp, not computed.\n *\n * The count of these is what tells a pure join table from an association that\n * carries its own attributes — `northwind.order_details` has the key shape of a\n * junction and three payload columns, so it is not one.\n */\nexport function isPayloadColumn(column: TableColumn, pks: string[], fkColumns: Set<string>): boolean {\n if (pks.includes(column.column_name)) return false;\n if (fkColumns.has(column.column_name)) return false;\n if (isAutoTimestamp(column)) return false;\n if (isGeneratedColumn(column)) return false;\n return true;\n}\n\n// ── Foreign-key topology ──────────────────────────────────────────────\n\n/** One foreign key, with its columns grouped back together. */\nexport interface ForeignKeyConstraint {\n name: string;\n table: string;\n columns: string[];\n foreignTable: string;\n foreignColumns: string[];\n deleteRule?: string;\n}\n\n/**\n * Groups per-column foreign key rows back into constraints.\n *\n * Rows arrive one per referencing column. A composite key looks exactly like two\n * separate keys until they are grouped by constraint name, and the difference\n * matters: two single-column keys to two tables can be a junction, one\n * two-column key never is.\n */\nexport function groupForeignKeys(fks: ForeignKeyRow[]): ForeignKeyConstraint[] {\n const byName = new Map<string, ForeignKeyConstraint>();\n for (const fk of fks) {\n // Rows from older callers carry no constraint name. Falling back to a\n // per-column key treats each as its own single-column constraint, which\n // is what the generator assumed before names were available.\n const key = `${fk.table_name}::${fk.constraint_name ?? `${fk.column_name}->${fk.foreign_table_name}`}`;\n const existing = byName.get(key);\n if (existing) {\n existing.columns.push(fk.column_name);\n existing.foreignColumns.push(fk.foreign_column_name);\n continue;\n }\n byName.set(key, {\n name: fk.constraint_name ?? `${fk.table_name}_${fk.column_name}_fkey`,\n table: fk.table_name,\n columns: [fk.column_name],\n foreignTable: fk.foreign_table_name,\n foreignColumns: [fk.foreign_column_name],\n deleteRule: fk.delete_rule\n });\n }\n return Array.from(byName.values());\n}\n\n/** Column sets that are unique in a table: its primary key and every unique index. */\nfunction uniqueColumnSets(table: string, pks: string[], uniques: UniqueConstraintRow[]): string[][] {\n const sets: string[][] = [];\n if (pks.length > 0) sets.push(pks);\n for (const unique of uniques) {\n if (unique.table_name === table) sets.push(unique.column_names);\n }\n return sets;\n}\n\nfunction sameColumnSet(a: string[], b: string[]): boolean {\n if (a.length !== b.length) return false;\n const left = [...a].sort();\n const right = [...b].sort();\n return left.every((value, index) => value === right[index]);\n}\n\n// ── Classification ────────────────────────────────────────────────────\n\n/** Everything classification needs, assembled once per run. */\ninterface AnalysisContext {\n tables: Map<string, TableMeta>;\n constraintsByTable: Map<string, ForeignKeyConstraint[]>;\n inboundByTable: Map<string, ForeignKeyConstraint[]>;\n uniques: UniqueConstraintRow[];\n rowCounts: Record<string, number>;\n}\n\nfunction buildContext(metadata: SchemaMetadata, tables: Map<string, TableMeta>): AnalysisContext {\n const allConstraints = groupForeignKeys(metadata.fks);\n const constraintsByTable = new Map<string, ForeignKeyConstraint[]>();\n const inboundByTable = new Map<string, ForeignKeyConstraint[]>();\n\n for (const constraint of allConstraints) {\n const outbound = constraintsByTable.get(constraint.table) ?? [];\n outbound.push(constraint);\n constraintsByTable.set(constraint.table, outbound);\n\n const inbound = inboundByTable.get(constraint.foreignTable) ?? [];\n inbound.push(constraint);\n inboundByTable.set(constraint.foreignTable, inbound);\n }\n\n return {\n tables,\n constraintsByTable,\n inboundByTable,\n uniques: metadata.uniques,\n rowCounts: metadata.rowCounts\n };\n}\n\n/**\n * A table that exists only to relate two others.\n *\n * Requires all of:\n * - exactly two single-column foreign keys, to two distinct columns;\n * - that pair is unique — it is the primary key, or a unique index covers it.\n * Without this the table can hold the same pair twice, which is a list of\n * events between two things, not a set membership;\n * - no payload columns. A join row that carries a quantity is an association\n * with attributes, and collapsing it into a many-to-many silently drops those\n * attributes from the UI entirely;\n * - nothing references it. A junction with its own dependents is something rows\n * point *at*, so it needs an identity of its own.\n */\nfunction classifyJunction(table: string, context: AnalysisContext): TableClassification | null {\n const meta = context.tables.get(table);\n if (!meta) return null;\n\n const constraints = context.constraintsByTable.get(table) ?? [];\n if (constraints.length !== 2) return null;\n if (constraints.some((c) => c.columns.length !== 1)) return null;\n\n const [first, second] = constraints;\n if (first.columns[0] === second.columns[0]) return null;\n\n if ((context.inboundByTable.get(table) ?? []).length > 0) return null;\n\n const pair = [first.columns[0], second.columns[0]];\n const uniqueSets = uniqueColumnSets(table, meta.pks, context.uniques);\n if (!uniqueSets.some((set) => sameColumnSet(set, pair))) return null;\n\n const fkColumns = new Set(pair);\n const payload = meta.columns.filter((column) => isPayloadColumn(column, meta.pks, fkColumns));\n if (payload.length > 0) return null;\n\n return {\n table,\n role: \"junction\",\n reason: `only relates ${first.foreignTable} to ${second.foreignTable}: two foreign keys, unique together, no other data`,\n junction: {\n sourceTable: first.foreignTable,\n sourceColumn: first.columns[0],\n targetTable: second.foreignTable,\n targetColumn: second.columns[0]\n }\n };\n}\n\n/**\n * A small, referenced, self-contained code list.\n *\n * Requires: something references it, it references nothing, it carries few and\n * simple payload columns, and it is small — where \"small\" is a real count, not\n * an estimate. `reltuples` is -1 on any table that has never been analyzed,\n * which is every table in a freshly restored dump, so a count that is merely\n * absent must not read as \"small\".\n */\nfunction classifyLookup(table: string, context: AnalysisContext): TableClassification | null {\n if (!isLookupCandidate(table, context)) return null;\n\n const rowCount = context.rowCounts[table];\n if (typeof rowCount !== \"number\") return null;\n // An empty table is not a code list — there is nothing to look up, and a\n // schema-only restore would otherwise classify half the database this way.\n if (rowCount < 1 || rowCount > LOOKUP_MAX_ROWS) return null;\n\n const referencedBy = (context.inboundByTable.get(table) ?? []).length;\n return {\n table,\n role: \"lookup\",\n reason: `a ${rowCount}-row code list: referenced by ${referencedBy} table(s), references none, ${LOOKUP_MAX_PAYLOAD_COLUMNS} or fewer simple columns`\n };\n}\n\n/**\n * The structural half of the lookup test — everything except the row count.\n *\n * Separate because the count costs a query per table, and the caller only wants\n * to pay it for tables that could possibly qualify.\n */\nfunction isLookupCandidate(table: string, context: AnalysisContext): boolean {\n const meta = context.tables.get(table);\n if (!meta) return false;\n\n if ((context.constraintsByTable.get(table) ?? []).length > 0) return false;\n if ((context.inboundByTable.get(table) ?? []).length === 0) return false;\n\n const payload = meta.columns.filter((column) => isPayloadColumn(column, meta.pks, new Set()));\n if (payload.length > LOOKUP_MAX_PAYLOAD_COLUMNS) return false;\n\n // A code list holds labels and flags. Documents, arrays, files and dates are\n // content, and content means the rows are worth browsing in their own right.\n //\n // Strings are allowed whether or not they declare a length. Requiring\n // `varchar(n)` looked like a way to say \"label, not prose\", and it is —\n // except that a schema written any time recently uses `text` for everything,\n // which is the advice Postgres itself gives. Modern pagila is all `text`, so\n // that rule classified nothing at all on it.\n return payload.every((column) => {\n const type = mapPgType(column.data_type);\n return type === \"boolean\" || type === \"number\" || type === \"string\";\n });\n}\n\n/**\n * Names the tables whose classification depends on a row count.\n *\n * The caller counts these — and only these — before calling\n * {@link classifyTables}. On a schema of any size this is a handful of tables,\n * and the count itself is capped (see `countRowsUpTo`), so the whole extra cost\n * is bounded regardless of how much data the database holds.\n */\nexport function lookupCandidates(metadata: SchemaMetadata, tables: Map<string, TableMeta>): string[] {\n const context = buildContext(metadata, tables);\n return Array.from(tables.keys()).filter((table) => isLookupCandidate(table, context));\n}\n\n/**\n * Rows that belong to one parent row and are reached through it.\n *\n * Requires nothing to reference the table — a table others point at has an\n * identity of its own and belongs in the navigation — and an *unambiguous*\n * owner among its foreign keys. The evidence ladder runs strongest first; each\n * rung is only taken when exactly one key satisfies it, so a table with two\n * equally plausible parents stays an entity rather than being filed under a\n * coin-flip.\n */\nfunction classifyOwnedChild(table: string, context: AnalysisContext): TableClassification | null {\n const meta = context.tables.get(table);\n if (!meta) return null;\n\n const constraints = context.constraintsByTable.get(table) ?? [];\n if (constraints.length === 0) return null;\n if ((context.inboundByTable.get(table) ?? []).length > 0) return null;\n\n const owner = pickOwner(meta, constraints);\n if (!owner) return null;\n\n return {\n table,\n role: \"owned-child\",\n reason: `belongs to ${owner.constraint.foreignTable} (${describeEvidence(owner.evidence)}) and nothing else references it`,\n owner: {\n table: owner.constraint.foreignTable,\n column: owner.constraint.columns[0],\n evidence: owner.evidence\n }\n };\n}\n\nfunction describeEvidence(evidence: OwnershipEvidence): string {\n switch (evidence) {\n case \"cascade-delete\": return \"its only ON DELETE CASCADE foreign key\";\n case \"identifying-key\": return \"its only foreign key inside the primary key\";\n case \"sole-required-key\": return \"its only NOT NULL foreign key\";\n case \"leading-key-column\": return \"the leading column of a primary key made only of foreign keys\";\n }\n}\n\nfunction pickOwner(\n meta: TableMeta,\n constraints: ForeignKeyConstraint[]\n): { constraint: ForeignKeyConstraint; evidence: OwnershipEvidence } | null {\n const singleColumn = constraints.filter((c) => c.columns.length === 1);\n if (singleColumn.length === 0) return null;\n\n const nullability = new Map(meta.columns.map((c) => [c.column_name, c.is_nullable === \"NO\"]));\n\n const cascading = singleColumn.filter((c) => c.deleteRule === \"CASCADE\");\n if (cascading.length === 1) return { constraint: cascading[0], evidence: \"cascade-delete\" };\n\n const identifying = singleColumn.filter((c) => meta.pks.includes(c.columns[0]));\n if (identifying.length === 1) return { constraint: identifying[0], evidence: \"identifying-key\" };\n\n const required = singleColumn.filter((c) => nullability.get(c.columns[0]) === true);\n if (required.length === 1) return { constraint: required[0], evidence: \"sole-required-key\" };\n\n // Last resort, and the only rung that leans on an ordering convention rather\n // than a guarantee: in a table whose whole primary key is foreign keys, the\n // first key column is the parent the rows are filed under.\n // `northwind.order_details` is keyed `(order_id, product_id)` — the rows are\n // lines of an order, which is exactly what that ordering says.\n if (identifying.length === meta.pks.length && meta.pks.length > 1) {\n const leading = identifying.find((c) => c.columns[0] === meta.pks[0]);\n if (leading) return { constraint: leading, evidence: \"leading-key-column\" };\n }\n\n return null;\n}\n\n/**\n * Classifies every table in the schema.\n *\n * Order matters: junction is the most specific and most consequential (the\n * table disappears), so it is tested first; then lookup, which needs no\n * ownership reasoning; then ownership. Anything unmatched is an entity, which\n * is also what every rule falls back to when its evidence is ambiguous.\n */\nexport function classifyTables(\n metadata: SchemaMetadata,\n tables: Map<string, TableMeta>\n): Map<string, TableClassification> {\n const context = buildContext(metadata, tables);\n const result = new Map<string, TableClassification>();\n\n for (const table of tables.keys()) {\n const classification =\n classifyJunction(table, context) ??\n classifyLookup(table, context) ??\n classifyOwnedChild(table, context) ?? {\n table,\n role: \"entity\" as const,\n reason: \"no structural evidence that it is a join table, a code list, or owned by another table\"\n };\n result.set(table, classification);\n }\n\n return result;\n}\n\n// ── Presentation derived from structure ───────────────────────────────\n\n/**\n * The columns a property-level derivation needs, resolved once.\n */\nexport interface ColumnFacts {\n column: TableColumn;\n isPk: boolean;\n isFk: boolean;\n /** Covered by a single-column unique constraint or unique index. */\n isUniqueAlone: boolean;\n isAutoTimestamp: boolean;\n isGenerated: boolean;\n /** Allowed values, from a Postgres enum type or a readable CHECK. */\n enumValues?: string[];\n propType: string;\n}\n\nexport function buildColumnFacts(\n meta: TableMeta,\n metadata: SchemaMetadata,\n enumMap: Map<string, string[]>,\n checkFacts: CheckFactsByTable\n): Map<string, ColumnFacts> {\n const fkColumns = new Set(meta.fks.map((fk) => fk.column_name));\n const singleColumnUniques = new Set(\n metadata.uniques\n .filter((u) => u.table_name === meta.name && u.column_names.length === 1)\n .map((u) => u.column_names[0])\n );\n const tableChecks = checkFacts.get(meta.name);\n\n const facts = new Map<string, ColumnFacts>();\n for (const column of meta.columns) {\n const pgEnum = column.data_type === \"USER-DEFINED\" ? enumMap.get(column.udt_name) : undefined;\n const checkEnum = tableChecks?.get(column.column_name)?.enumValues;\n facts.set(column.column_name, {\n column,\n isPk: meta.pks.includes(column.column_name),\n isFk: fkColumns.has(column.column_name),\n isUniqueAlone: singleColumnUniques.has(column.column_name),\n isAutoTimestamp: isAutoTimestamp(column),\n isGenerated: isGeneratedColumn(column),\n enumValues: pgEnum ?? checkEnum,\n propType: pgEnum ? \"string\" : mapPgType(column.data_type)\n });\n }\n return facts;\n}\n\n/**\n * The column that identifies a row to a human.\n *\n * Structural, in three rungs, strongest first:\n *\n * 1. A single-column unique constraint on a required string. This is as close\n * as a schema comes to declaring \"this is what a row is called\": it is the\n * column a person looks a row up by, and the database guarantees it picks\n * out one row.\n * 2. The first required string that declares a length, when the table also has\n * strings that do not. Choosing `varchar(n)` for one column and `text` for\n * another is the author distinguishing a label from prose.\n * 3. The first required string in declaration order. Weak, but it is the same\n * rung the panel's own fallback stands on, and column order carries real\n * information — the identifying column of a table is written near the top of\n * it, in every schema, in every language.\n *\n * Deliberately not: a column called `name`, or `title`. That works on English\n * schemas written by someone who read the same tutorial. This picks\n * `film.title`, `actor.first_name` and `category.name` out of pagila without\n * knowing what any of those words mean.\n */\nexport function deriveTitleProperty(facts: Map<string, ColumnFacts>): string | undefined {\n const candidates = Array.from(facts.values()).filter((f) =>\n !f.isPk && !f.isFk && !f.isGenerated &&\n f.propType === \"string\" &&\n !f.enumValues &&\n f.column.is_nullable === \"NO\"\n );\n if (candidates.length === 0) return undefined;\n\n const unique = candidates.find((f) => f.isUniqueAlone);\n if (unique) return unique.column.column_name;\n\n const bounded = candidates.filter((f) => isBoundedString(f.column));\n if (bounded.length > 0 && bounded.length < candidates.length) {\n return bounded[0].column.column_name;\n }\n\n return candidates[0].column.column_name;\n}\n\n/**\n * The enum column a board should have as its columns.\n *\n * A board needs a small, closed, always-present set of states. `NOT NULL` is\n * required because a null has no column to sit in; the bounds keep out\n * two-state flags (a filter, not a board) and long code lists (a scrolling\n * table). The first qualifying column in declaration order wins, so the output\n * is stable across runs.\n */\nexport function deriveKanbanProperty(facts: Map<string, ColumnFacts>): string | undefined {\n for (const fact of facts.values()) {\n if (fact.isPk || fact.isFk || fact.isGenerated) continue;\n if (!fact.enumValues) continue;\n if (fact.column.is_nullable !== \"NO\") continue;\n if (fact.enumValues.length < KANBAN_MIN_VALUES || fact.enumValues.length > KANBAN_MAX_VALUES) continue;\n return fact.column.column_name;\n }\n return undefined;\n}\n\n/**\n * The column a list should be sorted by, newest first.\n *\n * Only when the table has exactly one database-maintained timestamp. With two —\n * a created and an updated stamp — the two orderings differ and the schema does\n * not say which the user means, so neither is chosen.\n */\nexport function deriveSort(facts: Map<string, ColumnFacts>): [string, \"desc\"] | undefined {\n const stamps = Array.from(facts.values()).filter((f) => f.isAutoTimestamp);\n if (stamps.length !== 1) return undefined;\n return [stamps[0].column.column_name, \"desc\"];\n}\n\n/**\n * The first `LIST_PROPERTIES_CAP` visible properties, or nothing.\n *\n * Returning nothing when the table is already narrow matters: `listProperties`\n * that restates every column is config the reader has to check against the\n * property list to discover it does nothing, and it silently stops new columns\n * from appearing in the list view when someone adds one later.\n *\n * `hidden` names the properties already marked `hideFromCollection` — spending\n * one of six columns on a value the list does not render is worse than not\n * capping at all.\n */\nexport function deriveListProperties(propertiesOrder: string[], hidden: ReadonlySet<string> = new Set()): string[] | undefined {\n const visible = propertiesOrder.filter((key) => !hidden.has(key));\n if (visible.length <= LIST_PROPERTIES_CAP) return undefined;\n return visible.slice(0, LIST_PROPERTIES_CAP);\n}\n","/**\n * Introspection logic — pure functions and the pipeline that transforms\n * raw PostgreSQL metadata into Rebase collection definition files.\n *\n * This module contains NO side-effects: no fs writes, no pg.Client creation,\n * no process.exit. It is imported by introspect-db.ts (the CLI entry-point)\n * and consumed directly by tests.\n */\nimport { firstFreeKey, toWireKey } from \"@rebasepro/utils\";\nimport { inferPropertyFromData } from \"./introspect-db-inference\";\nimport { humanize } from \"./introspect-db-naming\";\nimport { mapPgType } from \"./introspect-db-types\";\nimport type { CheckFactsByTable } from \"./introspect-db-constraints\";\nimport type { TableClassification } from \"./introspect-db-structure\";\nimport {\n buildColumnFacts,\n deriveKanbanProperty,\n deriveListProperties,\n deriveSort,\n deriveTitleProperty,\n isDerivedIndexColumn,\n isReadOnlyColumn\n} from \"./introspect-db-structure\";\n\n// ── Typed interfaces for SQL query results ────────────────────────────\n\nexport interface TableRow {\n table_name: string;\n /** True for the parent of a partitioned table (`relkind = 'p'`). */\n is_partitioned?: boolean;\n}\n\nexport interface TableColumn {\n table_name: string;\n column_name: string;\n data_type: string;\n udt_name: string;\n is_nullable: string;\n column_default: string | null;\n atttypmod: number | null;\n /** 1-based position in the table, as declared. */\n ordinal_position?: number;\n /** `\"ALWAYS\"` for a generated column, `\"NEVER\"` otherwise. */\n is_generated?: string;\n /** `\"YES\"` for an identity column. */\n is_identity?: string;\n /** `\"ALWAYS\"` or `\"BY DEFAULT\"` on an identity column. */\n identity_generation?: string | null;\n /** The declared `varchar(n)` / `char(n)` bound, if any. */\n character_maximum_length?: number | null;\n numeric_precision?: number | null;\n numeric_scale?: number | null;\n}\n\nexport interface EnumValue {\n enum_name: string;\n enum_value: string;\n sort_order: number;\n}\n\nexport interface PrimaryKeyRow {\n table_name: string;\n column_name: string;\n}\n\nexport interface ForeignKeyRow {\n table_name: string;\n column_name: string;\n foreign_table_name: string;\n foreign_column_name: string;\n /** Name of the FK constraint — the only way to tell composite keys apart. */\n constraint_name?: string;\n /** 1-based position of this column within its constraint. */\n ordinal?: number;\n /** `\"CASCADE\"`, `\"RESTRICT\"`, `\"SET NULL\"`, `\"SET DEFAULT\"`, `\"NO ACTION\"`. */\n delete_rule?: string;\n}\n\n/** A unique constraint or unique index, as an ordered column list. */\nexport interface UniqueConstraintRow {\n table_name: string;\n constraint_name: string;\n column_names: string[];\n}\n\n/** A CHECK constraint, as `pg_get_constraintdef` renders it. */\nexport interface CheckConstraintRow {\n table_name: string;\n constraint_name: string;\n definition: string;\n}\n\n/** A `COMMENT ON TABLE` (null `column_name`) or `COMMENT ON COLUMN`. */\nexport interface CommentRow {\n table_name: string;\n column_name: string | null;\n comment: string;\n}\n\n/**\n * Everything one introspection run reads from the database.\n *\n * Passed around as one value so a new signal means a new field here rather than\n * a new parameter on every function between the query and the generator — the\n * shape `generateCollectionFile` had grown to seven positional arguments by.\n */\nexport interface SchemaMetadata {\n schema: string;\n tables: TableRow[];\n columns: TableColumn[];\n enumValues: EnumValue[];\n pks: PrimaryKeyRow[];\n fks: ForeignKeyRow[];\n uniques: UniqueConstraintRow[];\n checks: CheckConstraintRow[];\n comments: CommentRow[];\n /**\n * Row counts for the tables that needed one, capped — see `countRowsUpTo`.\n * Absent for every table introspection never had a reason to count.\n */\n rowCounts: Record<string, number>;\n}\n\nexport interface TableMeta {\n name: string;\n columns: TableColumn[];\n pks: string[];\n fks: ForeignKeyRow[];\n}\n\n// ── Irregular plurals that naive rules can't handle ───────────────────\n\nconst IRREGULAR_SINGULARS: Record<string, string> = {\n people: \"person\",\n children: \"child\",\n men: \"man\",\n women: \"woman\",\n mice: \"mouse\",\n geese: \"goose\",\n teeth: \"tooth\",\n feet: \"foot\",\n data: \"datum\",\n media: \"medium\",\n criteria: \"criterion\",\n phenomena: \"phenomenon\"\n};\n\n/**\n * Plurals in \"-ves\" whose singular really ends in f/fe, and which of the two.\n *\n * A blanket \"-ves\" -> \"-f\" rule gets `knives` -> `knif`, and mangles every\n * ordinary \"-ive\" noun that happens to be plural along with it: `archives` ->\n * `archif`, `objectives` -> `objectif`. The set of English words that genuinely\n * swap f/fe for ves is small and closed, so it is listed rather than guessed —\n * anything else ending in \"ves\" drops the trailing 's' like any other plural.\n *\n * Matched on the whole word, not on the suffix: `olives` ends in `lives`.\n */\nconst VES_SINGULAR_ENDINGS: Record<string, \"f\" | \"fe\"> = {\n calves: \"f\", dwarves: \"f\", elves: \"f\", halves: \"f\", hooves: \"f\",\n leaves: \"f\", loaves: \"f\", scarves: \"f\", selves: \"f\", sheaves: \"f\",\n shelves: \"f\", thieves: \"f\", wharves: \"f\", wolves: \"f\",\n knives: \"fe\", lives: \"fe\", wives: \"fe\"\n};\n\n/** Words ending in 's' that are already singular. */\nconst UNCOUNTABLE = new Set([\n \"status\", \"campus\", \"virus\", \"bus\", \"plus\", \"census\",\n \"diagnosis\", \"analysis\", \"basis\", \"crisis\", \"thesis\",\n \"synopsis\", \"parenthesis\", \"hypothesis\", \"emphasis\",\n \"news\", \"series\", \"species\", \"means\", \"athletics\",\n \"economics\", \"electronics\", \"mathematics\", \"physics\",\n \"politics\", \"statistics\"\n]);\n\nexport function singularize(word: string): string {\n const lower = word.toLowerCase();\n\n // Check irregular forms\n if (IRREGULAR_SINGULARS[lower]) {\n // Preserve the original casing of the first character\n const singular = IRREGULAR_SINGULARS[lower];\n return word[0] === word[0].toUpperCase()\n ? singular.charAt(0).toUpperCase() + singular.slice(1)\n : singular;\n }\n\n // Check uncountable\n if (UNCOUNTABLE.has(lower)) return word;\n\n // Latin/Greek -es endings (diagnosis -> diagnosis is uncountable, but \"addresses\" -> \"address\")\n if (lower.endsWith(\"ices\") && lower.length > 5) {\n // e.g. \"indices\" -> \"index\", \"vertices\" -> \"vertex\"\n return word.slice(0, -4) + \"ex\";\n }\n if (lower.endsWith(\"ies\") && lower.length > 3) {\n return word.slice(0, -3) + \"y\";\n }\n if (VES_SINGULAR_ENDINGS[lower]) {\n // e.g. \"wolves\" -> \"wolf\", \"knives\" -> \"knife\"\n return word.slice(0, -3) + VES_SINGULAR_ENDINGS[lower];\n }\n if (lower.endsWith(\"ches\") || lower.endsWith(\"shes\") || lower.endsWith(\"sses\") || lower.endsWith(\"xes\") || lower.endsWith(\"zes\")) {\n return word.slice(0, -2);\n }\n if (lower.endsWith(\"ses\") && !lower.endsWith(\"sses\")) {\n // e.g. \"responses\" -> \"response\", \"databases\" -> \"database\"\n return word.slice(0, -1);\n }\n if (lower.endsWith(\"s\") && !lower.endsWith(\"ss\") && !lower.endsWith(\"us\") && !lower.endsWith(\"is\")) {\n return word.slice(0, -1);\n }\n\n return word;\n}\n\n/**\n * Convert a snake_case table name to a camelCase + \"Collection\" variable name.\n * e.g. \"company_token\" -> \"companyTokenCollection\"\n */\nexport function toCollectionVarName(tableName: string): string {\n const camel = tableName.replace(/_([a-z])/g, (_g, letter: string) => letter.toUpperCase()) + \"Collection\";\n // Only reshapes names that are not identifiers already, so every table that\n // generated a working file keeps the exact variable name it had. A table\n // called `2024 archive` used to emit `const 2024 archiveCollection`, which\n // is three syntax errors rather than a declaration.\n if (JS_IDENTIFIER.test(camel)) return camel;\n const sanitized = camel.replace(/[^A-Za-z0-9_$]/g, \"_\");\n return /^[0-9]/.test(sanitized) ? `_${sanitized}` : sanitized;\n}\n\nexport function getIconForTable(tableName: string): string {\n const table = tableName.toLowerCase();\n if (table.includes(\"user\") || table.includes(\"account\") || table.includes(\"member\") || table.includes(\"customer\") || table.includes(\"client\") || table.includes(\"patient\")) return \"Users\";\n if (table.includes(\"post\") || table.includes(\"article\") || table.includes(\"blog\") || table.includes(\"page\")) return \"FileText\";\n if (table.includes(\"product\") || table.includes(\"item\")) return \"Package\";\n if (table.includes(\"order\") || table.includes(\"cart\") || table.includes(\"purchase\") || table.includes(\"invoice\")) return \"ShoppingCart\";\n if (table.includes(\"setting\") || table.includes(\"config\")) return \"Settings\";\n if (table.includes(\"tag\") || table.includes(\"categor\")) return \"Tag\";\n if (table.includes(\"image\") || table.includes(\"photo\") || table.includes(\"media\") || table.includes(\"asset\")) return \"Image\";\n if (table.includes(\"notification\") || table.includes(\"message\") || table.includes(\"email\")) return \"Mail\";\n if (table.includes(\"log\") || table.includes(\"audit\") || table.includes(\"event\")) return \"Activity\";\n if (table.includes(\"subscription\") || table.includes(\"plan\") || table.includes(\"billing\")) return \"CreditCard\";\n if (table.includes(\"comment\") || table.includes(\"review\") || table.includes(\"feedback\")) return \"MessageCircle\";\n return \"Database\";\n}\n\nexport { mapPgType };\n\n// ── Build the enum map from query results ─────────────────────────────\n\nexport function buildEnumMap(enumValues: EnumValue[]): Map<string, string[]> {\n const enumMap = new Map<string, string[]>();\n for (const ev of enumValues) {\n const existing = enumMap.get(ev.enum_name);\n if (existing) {\n existing.push(ev.enum_value);\n } else {\n enumMap.set(ev.enum_name, [ev.enum_value]);\n }\n }\n return enumMap;\n}\n\n// ── Build the tables map from raw query results ───────────────────────\n\nexport function buildTablesMap(\n tables: TableRow[],\n columns: TableColumn[],\n pks: PrimaryKeyRow[],\n fks: ForeignKeyRow[]\n): Map<string, TableMeta> {\n const tablesMap = new Map<string, TableMeta>();\n for (const t of tables) {\n tablesMap.set(t.table_name, {\n name: t.table_name,\n columns: columns.filter((c) => c.table_name === t.table_name),\n pks: pks.filter((pk) => pk.table_name === t.table_name).map((pk) => pk.column_name),\n fks: fks.filter((fk) => fk.table_name === t.table_name)\n });\n }\n return tablesMap;\n}\n\n// ── Identify join tables ──────────────────────────────────────────────\n\n/**\n * Join tables, identified by column name.\n *\n * Superseded for the CLI by `classifyTables` in `./introspect-db-structure`,\n * which asks the database instead: two single-column keys, unique together, no\n * payload column, nothing referencing the table. This rule folds away\n * `northwind.order_details` — which has the key shape and carries unit price,\n * quantity and discount — because it recognises `id`, `created_at` and\n * `updated_at` by name and calls everything else a foreign key.\n *\n * Still used by `./introspect-runtime`, which builds collections in memory from\n * a narrower set of catalog queries and has no unique-constraint or row-count\n * data to reason with.\n */\nexport function identifyJoinTables(tablesMap: Map<string, TableMeta>): Set<string> {\n const joinTables = new Set<string>();\n for (const [tableName, meta] of tablesMap.entries()) {\n if (meta.fks.length === 2) {\n const isLikelyJoinTable = meta.columns.every((c) =>\n meta.fks.some((fk) => fk.column_name === c.column_name) ||\n c.column_name === \"id\" ||\n c.column_name === \"created_at\" ||\n c.column_name === \"updated_at\"\n );\n\n if (isLikelyJoinTable) {\n joinTables.add(tableName);\n }\n }\n }\n return joinTables;\n}\n\n// ── Property ordering heuristics ──────────────────────────────────────\n\n/**\n * Property metadata used to compute display priority.\n * Keeps computePropertyPriority free of any TableMeta coupling.\n */\nexport interface PropertyOrderingContext {\n /** The resolved Rebase property type (e.g. \"string\", \"number\", \"date\", \"relation\"). */\n propType: string;\n /** Whether this column is a primary key. */\n isPk: boolean;\n /** Whether this column is an enum (USER-DEFINED with matching values). */\n isEnum: boolean;\n /** Whether this is a storage/file-upload field (detected from column name). */\n isStorage: boolean;\n /** The PostgreSQL data_type (e.g. \"text\", \"character varying\", \"jsonb\"). */\n pgDataType: string;\n /** The original column index in PostgreSQL (for stable tiebreaking). */\n originalIndex: number;\n}\n\n// — Tier 0: Identity (0–9) ————————————————————————————————————————————\nconst IDENTITY_EXACT: Record<string, number> = {\n id: 0,\n uuid: 1,\n _id: 2\n};\n\n// — Tier 1: Title / Name — the \"display column\" (10–19) ———————————————\nconst TITLE_EXACT: Record<string, number> = {\n name: 10,\n title: 11,\n label: 12,\n display_name: 13,\n displayname: 13,\n headline: 14,\n subject: 15,\n heading: 16\n};\n\n// — Tier 2: Human identity fields (20–29) —————————————————————————————\nconst HUMAN_IDENTITY_EXACT: Record<string, number> = {\n first_name: 20,\n firstname: 20,\n last_name: 21,\n lastname: 21,\n full_name: 22,\n fullname: 22,\n given_name: 22,\n family_name: 23,\n middle_name: 24,\n username: 25,\n user_name: 25,\n email: 26,\n email_address: 26,\n phone: 27,\n phone_number: 27,\n mobile: 27\n};\n\n// — Tier 3: Core descriptors (30–39) ——————————————————————————————————\nconst DESCRIPTOR_EXACT: Record<string, number> = {\n slug: 30,\n code: 31,\n sku: 32,\n reference: 33,\n ref: 33,\n type: 34,\n kind: 34,\n status: 35,\n state: 35,\n role: 36,\n category: 37,\n group: 38,\n priority: 39,\n order: 39,\n sort_order: 39,\n position: 39\n};\n\n// — Tier 12: System timestamps (120–129) ——————————————————————————————\nconst SYSTEM_TIMESTAMP_EXACT: Record<string, number> = {\n created_at: 120,\n createdat: 120,\n creation_date: 120,\n inserted_at: 121,\n updated_at: 122,\n updatedat: 122,\n modified_at: 122,\n last_modified: 122,\n deleted_at: 123,\n deletedat: 123,\n archived_at: 124\n};\n\n// — Pattern-based rules for partial matches ———————————————————————————\nconst TITLE_PATTERNS = [\"name\", \"title\", \"label\"];\nconst LONG_TEXT_NAMES = new Set([\"description\", \"summary\", \"excerpt\", \"abstract\", \"overview\", \"bio\", \"biography\", \"about\"]);\nconst RICH_CONTENT_NAMES = new Set([\"content\", \"body\", \"html\", \"markup\", \"text\", \"article_body\", \"post_body\"]);\nconst MEDIA_PATTERNS = [\"image\", \"avatar\", \"photo\", \"logo\", \"cover\", \"thumbnail\", \"banner\", \"icon\", \"picture\", \"poster\"];\nconst JSON_MAP_NAMES = new Set([\"metadata\", \"meta\", \"config\", \"configuration\", \"settings\", \"options\", \"preferences\", \"data\", \"payload\", \"attributes\", \"extra\", \"additional_info\"]);\n\n/**\n * Compute a numeric priority score for a property.\n * Lower scores appear first in the generated `propertiesOrder` array.\n *\n * The system uses 14 tiers (0–139), with the original column index\n * added as a fractional tiebreaker (originalIndex / 10000) to\n * guarantee stable ordering within the same tier.\n *\n * Pure function — no side effects.\n */\nexport function computePropertyPriority(\n columnName: string,\n ctx: PropertyOrderingContext\n): number {\n // Normalize camelCase/PascalCase to snake_case, then lowercase\n const col = columnName.replace(/([a-z0-9])([A-Z])/g, \"$1_$2\").toLowerCase();\n const tiebreaker = ctx.originalIndex / 10000;\n\n // ── Tier 0: Primary key identity fields\n if (ctx.isPk) {\n const exactScore = IDENTITY_EXACT[col];\n return (exactScore ?? 5) + tiebreaker;\n }\n\n // ── Tier 12: System timestamps (check early to prevent false matches)\n const systemTs = SYSTEM_TIMESTAMP_EXACT[col];\n if (systemTs !== undefined) {\n return systemTs + tiebreaker;\n }\n\n // ── Tier 1: Title / Name exact matches\n const titleExact = TITLE_EXACT[col];\n if (titleExact !== undefined) {\n return titleExact + tiebreaker;\n }\n\n // ── Tier 2: Human identity exact matches\n const humanExact = HUMAN_IDENTITY_EXACT[col];\n if (humanExact !== undefined) {\n return humanExact + tiebreaker;\n }\n\n // ── Tier 3: Core descriptor exact matches\n const descriptorExact = DESCRIPTOR_EXACT[col];\n if (descriptorExact !== undefined) {\n return descriptorExact + tiebreaker;\n }\n\n // ── Tier 1b: Title-like partial matches (e.g. \"product_name\", \"page_title\")\n // Score 17–19 so they rank after exact matches but still in tier 1.\n for (const pattern of TITLE_PATTERNS) {\n if (col.includes(pattern) && col !== pattern) {\n return 17 + tiebreaker;\n }\n }\n\n // ── Tier 9: Media / file upload fields (check before general strings)\n if (ctx.isStorage) {\n return 90 + tiebreaker;\n }\n for (const pattern of MEDIA_PATTERNS) {\n if (col.includes(pattern)) {\n return 91 + tiebreaker;\n }\n }\n if (col.endsWith(\"_url\") || col.endsWith(\"_uri\") || col.endsWith(\"_link\")) {\n return 92 + tiebreaker;\n }\n\n // ── Tier 7: Long text fields\n if (LONG_TEXT_NAMES.has(col)) {\n return 70 + tiebreaker;\n }\n\n // ── Tier 8: Rich content fields\n if (RICH_CONTENT_NAMES.has(col)) {\n return 80 + tiebreaker;\n }\n\n // ── Tier 10: JSON / Map types\n if (ctx.propType === \"map\") {\n return JSON_MAP_NAMES.has(col) ? 100 + tiebreaker : 105 + tiebreaker;\n }\n\n // ── Tier 11: Array types\n if (ctx.propType === \"array\") {\n return 110 + tiebreaker;\n }\n\n // ── Tier 6: Owning relations\n if (ctx.propType === \"relation\") {\n return 60 + tiebreaker;\n }\n\n // ── Tier 4: Short text, enums, booleans — \"quick glance\" fields\n if (ctx.isEnum) {\n return 40 + tiebreaker;\n }\n if (ctx.propType === \"boolean\") {\n return 45 + tiebreaker;\n }\n if (ctx.propType === \"string\" && ctx.pgDataType !== \"text\") {\n // Short string (varchar, char, uuid that's not a PK)\n return 42 + tiebreaker;\n }\n\n // ── Tier 5: Numbers & user-facing dates\n if (ctx.propType === \"number\") {\n return 50 + tiebreaker;\n }\n if (ctx.propType === \"date\") {\n // A date that isn't a system timestamp (already handled above)\n return 55 + tiebreaker;\n }\n\n // ── Tier 7b: text data_type that didn't match long-text names\n if (ctx.propType === \"string\" && ctx.pgDataType === \"text\") {\n return 75 + tiebreaker;\n }\n\n // ── Tier 13: Fallback / unknown\n return 130 + tiebreaker;\n}\n\n/**\n * Sort a `propertiesOrder` array using the priority heuristic.\n * Returns a new sorted array; does not mutate the input.\n *\n * @param entries - Array of { key, columnName, propType, ... } objects\n * carrying the information needed to compute priority.\n */\nexport interface PropertyOrderEntry {\n /** The property key in the generated collection (may differ from columnName for relations). */\n key: string;\n /** The ordering context for this property. */\n ctx: PropertyOrderingContext;\n}\n\nexport function sortPropertiesOrder(entries: PropertyOrderEntry[]): string[] {\n return [...entries]\n .sort((a, b) => computePropertyPriority(a.key, a.ctx) - computePropertyPriority(b.key, b.ctx))\n .map((e) => e.key);\n}\n\n// ── Generate collection file content ──────────────────────────────────\n\nexport interface GeneratedFile {\n tableName: string;\n fileName: string;\n content: string;\n}\n\n/**\n * The structural analysis a run can hand the generator.\n *\n * Optional in full, and the generator degrades to exactly its previous output\n * without it. That is not politeness towards old callers: three existing test\n * suites and the `rebase init` scaffold path build a `TableMeta` by hand and\n * have no database to read constraints or row counts from, and they must keep\n * producing a valid collection.\n */\n/**\n * Which `defineCollection` — if any — the project being generated into can import.\n *\n * A bare `const x: PostgresCollectionConfig = { … }` annotation widens `properties`\n * to `Record<string, …>`, and every key-shaped field in the admin block —\n * `titleProperty`, `sort`, `propertiesOrder`, `listProperties`, `fixedFilter` — is\n * derived from those keys. Annotated, they accept any string: introspection was\n * emitting a `propertiesOrder` array that nothing checked, so renaming a column and\n * re-introspecting left a stale key that compiled silently. `defineCollection` is\n * the identity function whose `const P` type parameter keeps the keys literal, which\n * is what turns that checking on.\n *\n * There are two of them and they are not interchangeable:\n *\n * - `admin-types` — `@rebasepro/cms-types`. Its index side-effect-imports\n * `augment.ts`, so importing it is also what *declares* the `admin` block. Only a\n * project that depends on the package can resolve it.\n * - `common` — `@rebasepro/common`. Same key inference, no admin surface, no React\n * anywhere in its graph (`scripts/headless-guard` lists it as core). This is the\n * headless flavour.\n * - `annotation` — neither package is declared, so neither import would resolve and\n * the old annotation is the only honest thing to emit. Projects scaffolded before\n * `@rebasepro/common` joined the headless config package land here.\n *\n * The last two emit **no admin block, on the collection or on any property**. That is\n * not a downgrade: `@rebasepro/types` declares no `admin` field at all, so the block\n * introspection used to emit was a type error in every headless project it was\n * written into. See `packages/cms-types/src/augment.ts`.\n */\nexport type CollectionBuilder = \"admin-types\" | \"common\" | \"annotation\";\n\n/**\n * The package specifiers the generated files name, spelled once.\n *\n * Written as constants rather than inline in the import templates below because\n * `scripts/headless-guard/check-types.mjs` scans core sources for `from\n * \"@rebasepro/cms-types\"` and cannot tell a real import from one this module\n * *writes*. It is right to be that blunt — the guard's whole value is that it\n * cannot be reasoned around — so the string simply never appears in that shape\n * here. Inlining them back into the templates re-breaks `check:types-headless`.\n */\nexport const ADMIN_TYPES_PACKAGE = \"@rebasepro/cms-types\";\nexport const COMMON_PACKAGE = \"@rebasepro/common\";\nexport const TYPES_PACKAGE = \"@rebasepro/types\";\n\nexport interface GenerationContext {\n metadata?: SchemaMetadata;\n classifications?: Map<string, TableClassification>;\n checkFacts?: CheckFactsByTable;\n /**\n * Defaults to `admin-types`, which is what the generator has always emitted.\n * The CLI never relies on the default — `introspect-db.ts` detects the flavour\n * from the target project and passes it. See `detectCollectionBuilder`.\n */\n builder?: CollectionBuilder;\n}\n\n/** Adds entries to a property's `validation` block, creating it if absent. */\nfunction withValidation(extra: string, entries: string[]): string {\n if (entries.length === 0) return extra;\n const block = entries.map((e) => ` ${e}`).join(\",\\n\");\n if (extra.includes(\"validation: {\")) {\n return extra.replace(\"validation: {\", `validation: {\\n${block},`);\n }\n return `${extra}\\n validation: {\\n${block}\\n },`;\n}\n\n/**\n * Whether a property's generated text already sets `key:` as an object key.\n *\n * A bare `extra.includes(\"min:\")` looks like it answers this and does not:\n * `admin:` ends in `min:`, so every property with an admin block claimed to\n * have a minimum already and silently lost the one the database declared. The\n * leading-delimiter requirement is the whole point — a key is preceded by a\n * newline, a brace or a comma, never by another identifier character.\n */\nfunction hasGeneratedKey(extra: string, key: string): boolean {\n return new RegExp(`(^|[\\\\s{,])${key}\\\\s*:`).test(extra);\n}\n\n/**\n * Adds entries to a property's `admin` block, creating it if absent.\n *\n * `emitAdmin` is false for the headless flavours, where `BaseProperty` has no\n * `admin` field to put them in — see {@link CollectionBuilder}. The options are\n * dropped rather than relocated: every one of them (`readOnly`, `multiline`,\n * `hideFromCollection`, `urlPreview`) describes a form widget, and there is no\n * form.\n */\nfunction withAdminOptions(extra: string, entries: string[], emitAdmin = true): string {\n if (!emitAdmin) return extra;\n if (entries.length === 0) return extra;\n const block = entries.map((e) => ` ${e}`).join(\",\\n\");\n if (extra.includes(\"admin: {\")) {\n return extra.replace(\"admin: {\", `admin: {\\n${block},`);\n }\n return `${extra}\\n admin: {\\n${block}\\n },`;\n}\n\n/** A TypeScript string literal, escaped. */\nfunction quote(value: string): string {\n return JSON.stringify(value);\n}\n\nconst JS_IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;\n\n/**\n * An object key for the generated file: verbatim when the name is a JavaScript\n * identifier, quoted otherwise.\n *\n * Postgres constrains an identifier only by quoting, so `order`, `full name`\n * and `2fa_enabled` are all ordinary column names — and all three produced a\n * file that did not parse when written as a bare key.\n *\n * Still needed now that keys are camel-cased rather than copied from the\n * column: `toWireKey` splits on separators and joins, which fixes `full name`\n * but cannot fix a name that is not an identifier for some other reason —\n * `2fa_enabled` becomes `2faEnabled`, still leading with a digit, and `order`\n * was never a separator problem at all.\n */\nfunction propKey(name: string): string {\n return JS_IDENTIFIER.test(name) ? name : quote(name);\n}\n\n/**\n * Text safe to put after `//`.\n *\n * A table comment or a classification reason is free text out of the database.\n * A newline in one ended the comment and let the rest of the value continue as\n * code.\n */\nfunction commentText(value: string): string {\n return value.replace(/\\s*[\\r\\n]+\\s*/g, \" \");\n}\n\n/** A property-key array, one key per line, indented for the admin block. */\nfunction formatKeyList(keys: string[]): string {\n if (keys.length === 0) return \"[]\";\n return `[\\n${keys.map((k) => ` ${quote(k)}`).join(\",\\n\")}\\n ]`;\n}\n\n/**\n * Generate the full TypeScript file content for a single collection.\n * Pure function — no I/O.\n */\nexport function generateCollectionFile(\n tableName: string,\n meta: TableMeta,\n allFks: ForeignKeyRow[],\n joinTables: Set<string>,\n tablesMap: Map<string, TableMeta>,\n enumMap: Map<string, string[]>,\n sampleData?: Record<string, unknown>[],\n context: GenerationContext = {}\n): string {\n const collectionName = humanize(tableName);\n const singular = singularize(collectionName);\n const icon = getIconForTable(tableName);\n\n const classification = context.classifications?.get(tableName);\n const checkFacts: CheckFactsByTable = context.checkFacts ?? new Map();\n const tableChecks = checkFacts.get(tableName);\n const columnComments = new Map<string, string>();\n let tableComment: string | undefined;\n for (const comment of context.metadata?.comments ?? []) {\n if (comment.table_name !== tableName) continue;\n if (comment.column_name === null) tableComment = comment.comment;\n else columnComments.set(comment.column_name, comment.comment);\n }\n const singleColumnUniques = new Set(\n (context.metadata?.uniques ?? [])\n .filter((u) => u.table_name === tableName && u.column_names.length === 1)\n .map((u) => u.column_names[0])\n );\n\n const builder: CollectionBuilder = context.builder ?? \"admin-types\";\n /** Whether the target project has an `admin` field to write into at all. */\n const emitAdmin = builder === \"admin-types\";\n\n const BUILDER_IMPORT: Record<CollectionBuilder, string> = {\n \"admin-types\": `import { defineCollection } from ${quote(ADMIN_TYPES_PACKAGE)};`,\n common: `import { defineCollection } from ${quote(COMMON_PACKAGE)};`,\n annotation: `import { PostgresCollectionConfig } from ${quote(TYPES_PACKAGE)};`\n };\n const imports = new Set<string>([BUILDER_IMPORT[builder]]);\n\n /**\n * Imports the collection a relation points at — unless it is this one.\n *\n * A self-referencing key (`employees.reports_to -> employees`, which both\n * northwind and chinook have) otherwise made the file import its own default\n * export under the name it declares three lines later: `TS2440: Import\n * declaration conflicts with local declaration`. The relation target is a\n * thunk, so referring to the local const directly is fine — it is only\n * dereferenced after the module has finished evaluating.\n */\n const importCollection = (otherTable: string): string => {\n const varName = toCollectionVarName(otherTable);\n if (otherTable !== tableName) imports.add(`import ${varName} from ${quote(`./${otherTable}`)};`);\n return varName;\n };\n\n /**\n * A relation's `target` thunk, with its return type spelled out.\n *\n * The annotation is what makes `defineCollection` survive a relational schema.\n * Without an explicit type on the const, the collection's type is *inferred*,\n * and a relation cycle — `posts` belongs to `authors`, `authors` has many\n * `posts`; or `employees.reports_to -> employees`, which northwind, chinook and\n * musicbrainz all have — makes that inference circular: `TS7022: implicitly has\n * type 'any' because it is referenced directly or indirectly in its own\n * initializer`, plus `TS7023` on the thunk and `TS2303` on the import alias.\n * Naming the return type lets the checker type the thunk without resolving the\n * collection it points at, which breaks the cycle. Nothing else is given up:\n * the inference that matters runs over `properties`, not over `relations`.\n *\n * The annotated flavour has no cycle to break — its const is already typed — so\n * it keeps the plainer thunk it has always emitted.\n */\n const relationTarget = (targetVarName: string): string => {\n if (builder === \"annotation\") return `() => ${targetVarName}`;\n imports.add(`import type { AnyCollectionConfig } from ${quote(TYPES_PACKAGE)};`);\n return `(): AnyCollectionConfig => ${targetVarName}`;\n };\n\n let propsOutput = \"\";\n let relationsOutput = \"\";\n const orderEntries: PropertyOrderEntry[] = [];\n const propertyBlocks = new Map<string, string>();\n /**\n * Column → the property key it was generated under.\n *\n * Needed because the structural helpers below (`deriveTitleProperty`,\n * `deriveKanbanProperty`, `deriveSort`) answer in *columns* — they read\n * `pg_attribute` — while `display.title`, `kanban.columnProperty` and\n * `sort` name **properties**. The two used to be the same string, so\n * nothing carried the translation; now `full_name` is generated as\n * `fullName` and a title pointing at `full_name` points at nothing.\n */\n const keyByColumn = new Map<string, string>();\n /** Properties the list view will not render, so `listProperties` skips them. */\n const hiddenFromCollection = new Set<string>();\n let columnIndex = 0;\n\n // Detect composite primary keys\n const isCompositePk = meta.pks.length > 1;\n\n // Map columns\n for (const col of meta.columns) {\n // Skip foreign keys since we handle them as relations\n // Exception: Do not skip if it's part of the primary key!\n if (meta.fks.some((fk) => fk.column_name === col.column_name) && !meta.pks.includes(col.column_name)) continue;\n\n const currentIndex = columnIndex++;\n\n // The key this column is generated under — its *wire* name, which is\n // not its column name. `columnName` below carries the column, so the\n // two never have to agree and the API stops carrying `user_id` next to\n // `displayName`.\n //\n // Camel-casing makes collisions possible where none existed: `user_id`\n // and `userId` are two columns and one key, which is a duplicate key in\n // an object literal — a TypeScript error that stops the whole generated\n // collection compiling. Resolved the same way the foreign-key loop\n // below resolves its own: first free candidate, then a numbered tail,\n // so no column is ever dropped. The raw column name is the second\n // candidate, so the loser of a collision still gets a name that means\n // something. Deterministic for a given database: the columns arrive in\n // ordinal order, so the same schema always yields the same keys.\n const propertyKey = firstFreeKey([toWireKey(col.column_name), col.column_name], propertyBlocks);\n keyByColumn.set(col.column_name, propertyKey);\n\n // Check if this column uses a PostgreSQL enum type\n const colEnumValues = enumMap.get(col.udt_name);\n const isEnumColumn = col.data_type === \"USER-DEFINED\" && colEnumValues !== undefined;\n const isVectorColumn = col.udt_name === \"vector\";\n\n const propType = isEnumColumn ? \"string\" : (isVectorColumn ? \"vector\" : mapPgType(col.data_type));\n let extra = \"\";\n\n const colNameLower = col.column_name.toLowerCase();\n\n // ── Data Inference Engine ────────────────────────────────────────────\n let finalPropType = propType;\n let inferenceExtra = \"\";\n\n if (!isEnumColumn && sampleData && sampleData.length > 0) {\n const values = sampleData.map(r => r[col.column_name]);\n const inferred = inferPropertyFromData(col.column_name, col.data_type, propType, values, meta.pks.includes(col.column_name), emitAdmin);\n if (inferred.propType) finalPropType = inferred.propType;\n if (inferred.extra) inferenceExtra = inferred.extra;\n }\n\n const columnChecks = tableChecks?.get(col.column_name);\n\n // Enum values — generate real enum from the PG enum\n if (isEnumColumn && colEnumValues) {\n const enumEntries = colEnumValues\n .map((v) => `{ id: ${quote(v)}, label: ${quote(humanize(v))} }`)\n .join(\", \");\n extra += `\\n enum: [${enumEntries}],`;\n } else if (columnChecks?.enumValues && !inferenceExtra.includes(\"enum:\") && propType === \"string\") {\n // `CHECK (col IN (…))` is the other way a schema declares a closed\n // set. It is the same statement as a Postgres enum type, made by an\n // author who did not want a type — and until now the form offered a\n // free-text box for it and let the database reject the write.\n const enumEntries = columnChecks.enumValues\n .map((v) => `{ id: ${quote(v)}, label: ${quote(humanize(v))} }`)\n .join(\", \");\n extra += `\\n enum: [${enumEntries}],`;\n }\n\n // Date auto-value heuristics\n if (finalPropType === \"date\") {\n if (colNameLower === \"created_at\" || colNameLower === \"createdat\") {\n extra += \"\\n autoValue: \\\"on_create\\\",\";\n extra = withAdminOptions(extra, [\"readOnly: true\", \"hideFromCollection: true\"], emitAdmin);\n hiddenFromCollection.add(propertyKey);\n } else if (colNameLower === \"updated_at\" || colNameLower === \"updatedat\") {\n extra += \"\\n autoValue: \\\"on_update\\\",\";\n extra = withAdminOptions(extra, [\"readOnly: true\", \"hideFromCollection: true\"], emitAdmin);\n hiddenFromCollection.add(propertyKey);\n } else if (col.column_default && (col.column_default.includes(\"now()\") || col.column_default.includes(\"CURRENT_TIMESTAMP\"))) {\n extra += \"\\n autoValue: \\\"on_create\\\",\";\n extra = withAdminOptions(extra, [\"readOnly: true\"], emitAdmin);\n }\n }\n\n // Array/Map heuristics (Fallback if not inferred)\n if (finalPropType === \"array\" && !inferenceExtra.includes(\"of: {\")) {\n let innerType = \"string\";\n let colType = \"\";\n if (col.udt_name.startsWith(\"_\")) {\n const baseType = col.udt_name.substring(1);\n innerType = mapPgType(baseType);\n if (innerType === \"string\") colType = \"text[]\";\n else if (innerType === \"number\") colType = col.udt_name === \"_numeric\" ? \"numeric[]\" : \"integer[]\";\n else if (innerType === \"boolean\") colType = \"boolean[]\";\n }\n if (colType) {\n extra += `\\n columnType: ${quote(colType)},`;\n }\n extra += `\\n of: { name: ${quote(`${humanize(col.column_name)} Item`)}, type: ${quote(innerType)} },`;\n } else if (finalPropType === \"map\" && !inferenceExtra.includes(\"keyValue: true\") && !inferenceExtra.includes(\"properties: {\")) {\n extra += \"\\n keyValue: true,\";\n }\n\n // String sub-type heuristics (Fallback if not handled by inference or enum)\n if (finalPropType === \"string\" && !isEnumColumn && !inferenceExtra) {\n const isUrl = colNameLower.endsWith(\"_url\") || colNameLower.endsWith(\"_uri\") || colNameLower.endsWith(\"_link\");\n const isMedia = colNameLower.includes(\"image\") || colNameLower.includes(\"avatar\") || colNameLower.includes(\"photo\") || colNameLower.includes(\"logo\") || colNameLower.includes(\"cover\");\n\n if (isMedia) {\n extra += `\\n storage: {\\n storagePath: ${quote(`${tableName}/${col.column_name}`)}\\n },`;\n } else if (isUrl) {\n extra += \"\\n url: true,\";\n } else if (colNameLower === \"description\" || colNameLower === \"summary\" || colNameLower === \"excerpt\") {\n extra = withAdminOptions(extra, [\"multiline: true\"], emitAdmin);\n } else if (colNameLower === \"content\" || colNameLower === \"body\") {\n // Inside `admin`, because that is where both options live. At the\n // top of the property — where these were — the generated file\n // does not compile: `StringProperty` declares neither. Six of\n // OpenStreetMap's tables have a `body` column, which is how this\n // surfaced.\n extra = withAdminOptions(extra, [\"multiline: true\", \"markdown: true\"], emitAdmin);\n } else if (col.data_type === \"text\") {\n extra = withAdminOptions(extra, [\"multiline: true\"], emitAdmin);\n }\n }\n\n // Append inference results\n if (inferenceExtra) {\n extra += inferenceExtra;\n if (!extra.endsWith(\",\")) extra += \",\";\n }\n\n // ── Rules the database already enforces ──────────────────────────────\n // Everything below is read from the catalog, not guessed from the data\n // or the column's name. Each one is a constraint a write would hit\n // anyway; surfacing it means the form says no before the database does.\n const declaredValidation: string[] = [];\n\n // `varchar(n)` — a bound the author wrote down and nothing has read.\n if (finalPropType === \"string\" &&\n typeof col.character_maximum_length === \"number\" &&\n col.character_maximum_length > 0 &&\n !hasGeneratedKey(extra, \"max\")) {\n declaredValidation.push(`max: ${col.character_maximum_length}`);\n }\n\n if (columnChecks) {\n if (finalPropType === \"number\") {\n if (columnChecks.min !== undefined && !hasGeneratedKey(extra, \"min\")) declaredValidation.push(`min: ${columnChecks.min}`);\n if (columnChecks.max !== undefined && !hasGeneratedKey(extra, \"max\")) declaredValidation.push(`max: ${columnChecks.max}`);\n if (columnChecks.moreThan !== undefined) declaredValidation.push(`moreThan: ${columnChecks.moreThan}`);\n if (columnChecks.lessThan !== undefined) declaredValidation.push(`lessThan: ${columnChecks.lessThan}`);\n }\n if (finalPropType === \"string\") {\n if (columnChecks.lengthMin !== undefined && !hasGeneratedKey(extra, \"min\")) declaredValidation.push(`min: ${columnChecks.lengthMin}`);\n if (columnChecks.lengthMax !== undefined && !declaredValidation.some((v) => v.startsWith(\"max:\")) && !hasGeneratedKey(extra, \"max\")) {\n declaredValidation.push(`max: ${columnChecks.lengthMax}`);\n }\n }\n }\n\n // A single-column unique index is the same promise `validation.unique`\n // makes. Composite uniqueness is not: it constrains the combination, and\n // marking either column unique on its own would reject valid rows.\n if (singleColumnUniques.has(col.column_name) && !meta.pks.includes(col.column_name)) {\n declaredValidation.push(\"unique: true\");\n }\n\n extra = withValidation(extra, declaredValidation);\n\n // A generated column rejects every write, and a tsvector holds lexeme\n // positions rather than text, so an editable field for either is a field\n // that can only ever produce an error.\n if (isReadOnlyColumn(col) && !hasGeneratedKey(extra, \"readOnly\")) {\n const options = [\"readOnly: true\"];\n if (isDerivedIndexColumn(col) && !hasGeneratedKey(extra, \"hideFromCollection\")) {\n options.push(\"hideFromCollection: true\");\n hiddenFromCollection.add(propertyKey);\n }\n extra = withAdminOptions(extra, options, emitAdmin);\n }\n\n // `COMMENT ON COLUMN` — documentation the author already wrote, which\n // introspection has never carried across.\n const columnComment = columnComments.get(col.column_name);\n if (columnComment) {\n extra = `\\n description: ${quote(columnComment)},${extra}`;\n }\n\n // Identify IDs (unless already inferred as UUID/CUID by inferenceEngine)\n if (meta.pks.includes(col.column_name)) {\n if (isCompositePk) {\n extra += `\\n // Part of composite primary key (${commentText(meta.pks.join(\", \"))})`;\n } else if (finalPropType === \"number\" && !inferenceExtra.includes(\"isId:\")) {\n extra += \"\\n isId: \\\"increment\\\",\";\n } else if (col.data_type.toLowerCase() === \"uuid\" && !inferenceExtra.includes(\"isId:\")) {\n extra += \"\\n isId: \\\"uuid\\\",\";\n } else if (!inferenceExtra.includes(\"isId:\")) {\n extra += \"\\n isId: \\\"uuid\\\", // Verify if this is a UUID or CUID\";\n }\n }\n\n if (finalPropType === \"vector\") {\n const dims = col.atttypmod && col.atttypmod > 0 ? col.atttypmod : 1536;\n extra += `\\n dimensions: ${dims},`;\n }\n\n // `required` on a column the user cannot write is a form that cannot be\n // submitted: pagila's `film.fulltext` is NOT NULL and maintained by a\n // trigger, so demanding it of the user blocks every create.\n if (col.is_nullable === \"NO\" && !meta.pks.includes(col.column_name) && !col.column_default && !isReadOnlyColumn(col)) {\n if (extra.includes(\"validation: {\")) {\n extra = extra.replace(\"validation: {\", \"validation: {\\n required: true,\");\n } else {\n extra += \"\\n validation: {\\n required: true\\n },\";\n }\n }\n\n const humanName = humanize(col.column_name);\n\n orderEntries.push({\n key: propertyKey,\n ctx: {\n propType: finalPropType,\n isPk: meta.pks.includes(col.column_name),\n isEnum: isEnumColumn,\n isStorage: extra.includes(\"storage: {\") || inferenceExtra.includes(\"storage: {\"),\n pgDataType: col.data_type,\n originalIndex: currentIndex\n }\n });\n\n propertyBlocks.set(propertyKey, `\n ${propKey(propertyKey)}: {\n name: ${quote(humanName)},\n columnName: ${quote(col.column_name)},\n type: ${quote(finalPropType)},${extra}\n },`);\n }\n\n // Map Owning Relations (from this table's FKs to other tables)\n for (const fk of meta.fks) {\n const targetTableName = fk.foreign_table_name;\n if (!joinTables.has(targetTableName)) {\n // The relation gets its own property key, and it must not be one this\n // file has already used — a duplicate key in an object literal is a\n // TypeScript error, so the whole collection stops compiling.\n //\n // The collision needs three things at once and is invisible without\n // all three: a foreign key column that does *not* end in `_id`, that\n // column also being part of the primary key (which is what keeps it\n // as a property of its own rather than folding it into the relation),\n // and the stripped name matching the target table. MusicBrainz names\n // every foreign key after the table it points at — `area_tag (area,\n // tag)` — so 67 of its 339 collections came out with a property\n // declared twice.\n //\n // Camel-cased for the same reason the columns above are: this is a\n // property key, it sits in the same object literal, and a\n // `blog_author` beside a `publishedAt` is the two-conventions\n // defect reproduced inside a single collection.\n const stripped = toWireKey(fk.column_name.replace(/_id$/, \"\"));\n const relName = firstFreeKey(\n [\n stripped,\n toWireKey(targetTableName),\n `${stripped}Relation`\n ],\n propertyBlocks\n );\n // Push the relation property key, not the FK column name\n orderEntries.push({\n key: relName,\n ctx: {\n propType: \"relation\",\n isPk: false,\n isEnum: false,\n isStorage: false,\n pgDataType: \"\",\n originalIndex: columnIndex++\n }\n });\n\n const targetCollectionCamel = importCollection(targetTableName);\n\n const relHumanName = humanize(relName);\n\n propertyBlocks.set(relName, `\n ${propKey(relName)}: {\n name: ${quote(relHumanName)},\n type: \"relation\",\n // mapped from foreign key: ${commentText(fk.column_name)} -> ${commentText(targetTableName)}(${commentText(fk.foreign_column_name)})\n relation: {\n kind: \"belongsTo\",\n target: ${relationTarget(targetCollectionCamel)},\n localKey: ${quote(fk.column_name)}\n }\n },`);\n }\n }\n\n // Map Inverse Relations (1-to-many where OTHER table points to THIS table)\n // These go into the `relations` array so they render as subcollection tabs.\n const inverseFks = allFks.filter((fk) => fk.foreign_table_name === tableName && !joinTables.has(fk.table_name));\n for (const fk of inverseFks) {\n const sourceTableName = fk.table_name;\n\n const targetCollectionCamel = importCollection(sourceTableName);\n\n relationsOutput += `\n {\n kind: \"hasMany\",\n relationName: ${quote(sourceTableName)},\n target: ${relationTarget(targetCollectionCamel)},\n // the ${commentText(sourceTableName)}.${commentText(fk.column_name)} FK points back here\n foreignKeyOnTarget: ${quote(fk.column_name)}\n },`;\n }\n\n // Map Many-to-Many Relations (Join Tables)\n // These also go into the `relations` array so they render as subcollection tabs.\n const relatedJoinTables = Array.from(joinTables).filter((jt) => {\n const jtMeta = tablesMap.get(jt);\n return jtMeta ? jtMeta.fks.some((fk) => fk.foreign_table_name === tableName) : false;\n });\n\n for (const jt of relatedJoinTables) {\n const jtMeta = tablesMap.get(jt);\n if (!jtMeta) continue;\n\n const joinFks = jtMeta.fks;\n\n // Handle self-referencing M2M: both FKs point to the same table\n const selfRefFks = joinFks.filter((fk) => fk.foreign_table_name === tableName);\n if (selfRefFks.length === 2) {\n // Self-referencing M2M — generate a single owning relation\n const thisFk = selfRefFks[0];\n const otherFk = selfRefFks[1];\n\n const relPropName = `${tableName}_via_${otherFk.column_name.replace(/_id$/, \"\")}`;\n\n relationsOutput += `\n {\n kind: \"manyToMany\",\n relationName: ${quote(relPropName)},\n target: ${relationTarget(toCollectionVarName(tableName))},\n through: {\n table: ${quote(jt)},\n sourceColumn: ${quote(thisFk.column_name)},\n targetColumn: ${quote(otherFk.column_name)}\n }\n },`;\n continue;\n }\n\n const otherFk = joinFks.find((fk) => fk.foreign_table_name !== tableName);\n\n if (otherFk) {\n const targetTableName = otherFk.foreign_table_name;\n\n const targetCollectionCamel = importCollection(targetTableName);\n\n // Both sides of a many-to-many are `manyToMany`. There is no owning\n // and inverse side to pick between any more, so this no longer\n // guesses one from table-name ordering and no longer emits a\n // half-configured relation on the losing side with a comment asking\n // the reader to finish it by hand. Introspection already knows both\n // junction columns; each side just names them from its own end.\n const thisFk = joinFks.find((fk) => fk.foreign_table_name === tableName);\n\n const throughCode = thisFk\n ? `\\n through: {\\n table: ${quote(jt)},\\n sourceColumn: ${quote(thisFk.column_name)},\\n targetColumn: ${quote(otherFk.column_name)}\\n }`\n : \"\";\n\n relationsOutput += `\n {\n kind: \"manyToMany\",\n relationName: ${quote(targetTableName)},\n target: ${relationTarget(targetCollectionCamel)},${throughCode}\n },`;\n }\n }\n\n const relationsBlock = relationsOutput\n ? `\\n relations: [${relationsOutput}\\n ],`\n : \"\";\n\n const sortedPropertiesOrder = sortPropertiesOrder(orderEntries);\n for (const key of sortedPropertiesOrder) {\n propsOutput += propertyBlocks.get(key) || \"\";\n }\n\n // ── The admin block ──────────────────────────────────────────────────\n // `icon` and `propertiesOrder` used to be emitted at the *top level* of the\n // config, where they have not belonged since the admin block was split out:\n // `PostgresCollectionConfig` does not declare them, so every generated file\n // was a type error, and the panel — which reads the block — never saw them.\n const adminEntries: string[] = [`icon: ${quote(icon)}`];\n\n if (classification) {\n const derivedFacts = context.metadata\n ? buildColumnFacts(meta, context.metadata, enumMap, checkFacts)\n : undefined;\n\n if (classification.role === \"owned-child\") {\n // The rows are already reachable: every inbound foreign key renders\n // as a tab on the parent. A second, top-level entry for them is what\n // turns a navigation of eight nouns into a list of thirty tables.\n adminEntries.push(\"hideFromNavigation: true\");\n } else if (classification.role === \"lookup\") {\n adminEntries.push('group: \"Reference\"');\n }\n\n if (derivedFacts) {\n // Each of these comes back as a column and is emitted as a property.\n const asProperty = (column: string): string => keyByColumn.get(column) ?? toWireKey(column);\n\n const titleProperty = deriveTitleProperty(derivedFacts);\n if (titleProperty) adminEntries.push(`display: { title: ${quote(asProperty(titleProperty))} }`);\n\n const kanbanProperty = deriveKanbanProperty(derivedFacts);\n if (kanbanProperty) adminEntries.push(`kanban: {\\n columnProperty: ${quote(asProperty(kanbanProperty))}\\n }`);\n\n const sort = deriveSort(derivedFacts);\n if (sort) adminEntries.push(`sort: [${quote(asProperty(sort[0]))}, \"desc\"]`);\n }\n\n const listProperties = deriveListProperties(sortedPropertiesOrder, hiddenFromCollection);\n if (listProperties) {\n adminEntries.push(`listProperties: ${formatKeyList(listProperties)}`);\n }\n }\n\n adminEntries.push(`propertiesOrder: ${formatKeyList(sortedPropertiesOrder)}`);\n\n // Every entry above names a property key, and with `defineCollection` those\n // keys are now checked against `properties` — a stale `propertiesOrder` entry\n // left behind by a renamed column is a compile error rather than a silent\n // no-op. Which is also why the block cannot be emitted where the field is not\n // declared: see {@link CollectionBuilder}.\n const adminBlock = emitAdmin\n ? `\\n admin: {\\n ${adminEntries.join(\",\\n \")}\\n }`\n : \"\";\n\n const descriptionBlock = tableComment\n ? `\\n description: ${quote(tableComment)},`\n : \"\";\n\n // The classification is stated in the file because it is a *decision*, and a\n // decision the reader may disagree with. Naming the evidence tells them\n // which line to delete when they do.\n const classificationNote = classification && classification.role !== \"entity\"\n ? `\\n// Introspected as a ${commentText(classification.role)}: ${commentText(classification.reason)}.\\n`\n : \"\";\n\n const collectionVarName = toCollectionVarName(tableName);\n // `const x = defineCollection({ … })` is also the shape the ts-morph schema\n // editor in `@rebasepro/server` expects — `COLLECTION_FACTORIES` — so an\n // introspected collection is now editable from the panel the way a scaffolded\n // one is.\n const [open, close] = builder === \"annotation\"\n ? [`const ${collectionVarName}: PostgresCollectionConfig = {`, \"};\"]\n : [`const ${collectionVarName} = defineCollection({`, \"});\"];\n // Package imports first, then siblings. `AnyCollectionConfig` is added the\n // moment the first relation needs it — which is after the sibling collections\n // it points at have already been added — and only when a relation needs it, so\n // a project with `noUnusedLocals` never sees an import it does not use.\n const importLines = Array.from(imports);\n const orderedImports = [\n ...importLines.filter((line) => !line.includes('from \"./')),\n ...importLines.filter((line) => line.includes('from \"./'))\n ];\n const fileContent = `${orderedImports.join(\"\\n\")}\n${classificationNote}\n${open}\n name: ${quote(collectionName)},\n singularName: ${quote(singular)},\n slug: ${quote(tableName)},\n table: ${quote(tableName)},${descriptionBlock}\n properties: {${propsOutput}\n },${relationsBlock}${adminBlock}\n${close}\n\nexport default ${collectionVarName};\n`;\n\n return fileContent;\n}\n\n/**\n * Generate the content for an index.ts file that re-exports all collections.\n */\nexport function generateIndexContent(fileNames: string[]): string {\n const sorted = [...fileNames].sort();\n let imports = \"\";\n let arrayElements = \"\";\n for (const f of sorted) {\n const varName = toCollectionVarName(f);\n imports += `import ${varName} from ${quote(`./${f}`)};\\n`;\n arrayElements += ` ${varName},\\n`;\n }\n return `${imports}\\nexport const collections = [\\n${arrayElements}];\\n`;\n}\n\n/**\n * Merge new exports into existing index.ts content.\n * Returns the merged content string.\n */\nexport function mergeIndexContent(existingContent: string, newFileNames: string[]): string {\n const existingImports = new Set(\n [...existingContent.matchAll(/import\\s+([a-zA-Z0-9_]+)\\s+from\\s+\"\\.\\/([^\"]+)\"/g)].map((m) => m[2])\n );\n const sorted = [...newFileNames].sort();\n\n let newImports = \"\";\n let newElements = \"\";\n\n for (const f of sorted) {\n if (!existingImports.has(f)) {\n const varName = toCollectionVarName(f);\n newImports += `import ${varName} from ${quote(`./${f}`)};\\n`;\n newElements += ` ${varName},\\n`;\n }\n }\n\n if (!newImports) return existingContent;\n\n // Simple injection logic:\n // Add new imports below the last import or at the top\n const importRegex = /import\\s+.*?;/g;\n let lastImportMatch;\n let match;\n while ((match = importRegex.exec(existingContent)) !== null) {\n lastImportMatch = match;\n }\n\n let contentWithImports = existingContent;\n if (lastImportMatch) {\n const pos = lastImportMatch.index + lastImportMatch[0].length;\n contentWithImports = existingContent.slice(0, pos) + \"\\n\" + newImports.trimEnd() + existingContent.slice(pos);\n } else {\n contentWithImports = newImports + \"\\n\" + existingContent;\n }\n\n // Inject into the `collections = [...]` array\n const arrayRegex = /export\\s+const\\s+collections\\s*=\\s*\\[([\\s\\S]*?)\\];/;\n return contentWithImports.replace(arrayRegex, (fullMatch, arrayContent) => {\n let mergedArray = arrayContent.trimEnd();\n if (mergedArray && !mergedArray.endsWith(\",\")) mergedArray += \",\";\n if (mergedArray) mergedArray += \"\\n\";\n mergedArray += newElements.trimEnd();\n return `export const collections = [\\n ${mergedArray.trim()}\\n];`;\n });\n}\n\n/**\n * Safely extract the host portion of a database URL for logging.\n */\nexport function safeHostFromUrl(url: string): string {\n return url.includes(\"@\") ? url.split(\"@\")[1] : \"(local connection)\";\n}\n"],"mappings":";;;;;;;;;;;;;AAUA,SAAgB,SAAS,WAA2B;CAChD,OAAO,UACF,QAAQ,MAAM,GAAG,CAAC,CAClB,QAAQ,UAAU,MAAM,EAAE,YAAY,CAAC;AAChD;;;ACPA,IAAM,iBAAiB;AACvB,IAAM,aAAa;AACnB,IAAM,aAAa;AAGnB,SAAgB,sBACZ,YACA,YACA,iBACA,cACA,MAOA,YAAY,MACG;CACf,MAAM,SAA0B,CAAC;CACjC,MAAM,aAAuB,CAAC;CAI9B,MAAM,eAAyB,CAAC;CAGhC,MAAM,cAAc,aAAa,QAAO,MAAK,MAAM,QAAQ,MAAM,KAAA,KAAa,MAAM,EAAE;CAEtF,IAAI,YAAY,WAAW,GACvB,OAAO;CAGX,MAAM,eAAe,WAAW,YAAY;CAG5C,IAAI,oBAAoB,UAAU;EAG9B,IADqB,YAAY,OAAM,MAAK,MAAM,KAAK,MAAM,KAAK,MAAM,OAAO,MAAM,GACjF,GAAc;GACd,OAAO,WAAW;GAClB,OAAO;EACX;EAG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,CAAC;AACL;;;;AAKA,SAAgB,gBAAgB,KAAqB;CACjD,OAAO,IAAI,SAAS,GAAG,IAAI,IAAI,MAAM,GAAG,CAAC,CAAC,KAAK;AACnD"}
|
|
1
|
+
{"version":3,"file":"introspect-db-logic-C6LQdTxj.js","names":[],"sources":["../src/schema/introspect-db-naming.ts","../src/schema/introspect-db-inference.ts","../src/schema/introspect-db-types.ts","../src/schema/introspect-db-structure.ts","../src/schema/introspect-db-logic.ts"],"sourcesContent":["/**\n * Naming helpers shared by the introspection modules. These live apart from\n * `introspect-db-logic.ts` because the inference pass needs them too, and\n * importing them from there would close a cycle back through this module.\n */\n\n/**\n * Convert a snake_case name to a human-readable Title Case label.\n * e.g. \"created_at\" -> \"Created At\", \"customer_id\" -> \"Customer Id\"\n */\nexport function humanize(snakeName: string): string {\n return snakeName\n .replace(/_/g, \" \")\n .replace(/\\b\\w/g, (c) => c.toUpperCase());\n}\n","import { humanize } from \"./introspect-db-naming\";\n\nexport interface InferenceResult {\n propType?: string; // If the inference changes the base type\n extra?: string; // String to append to the property definition\n}\n\nconst ISO_8601_REGEX = /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?(?:Z|[+-]\\d{2}:\\d{2})$/;\nconst UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;\nconst CUID_REGEX = /^c[^\\s-]{7,}$/i; // Basic CUID check\nconst COLOR_HEX_REGEX = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;\n\nexport function inferPropertyFromData(\n columnName: string,\n pgDataType: string,\n currentPropType: string,\n sampleValues: unknown[],\n isPk: boolean,\n /**\n * False when generating for a project without `@rebasepro/cms-types`, where\n * `BaseProperty` declares no `admin` field and the block below would not\n * compile. The type-level inferences (`propType`, `url`, `storage`) are\n * unaffected — only the form-widget hints are dropped.\n */\n emitAdmin = true\n): InferenceResult {\n const result: InferenceResult = {};\n const extraLines: string[] = [];\n // Collected separately and emitted as one block at the end. Pushed inline,\n // two admin-owned options from different branches produced two `admin: {`\n // blocks in one property — a duplicate key, which is also a compile error.\n const adminOptions: string[] = [];\n\n // Filter out null/undefined for analysis\n const validValues = sampleValues.filter(v => v !== null && v !== undefined && v !== \"\");\n\n if (validValues.length === 0) {\n return result; // Not enough data\n }\n\n const colNameLower = columnName.toLowerCase();\n\n // ── Number Analysis ──────────────────────────────────────────────────\n if (currentPropType === \"number\") {\n // Boolean stored as int\n const allZeroOrOne = validValues.every(v => v === 0 || v === 1 || v === \"0\" || v === \"1\");\n if (allZeroOrOne) {\n result.propType = \"boolean\";\n return result; // Don't do min/max if boolean\n }\n\n // Min / Max constraints\n const numValues = validValues.map(v => Number(v)).filter(v => !isNaN(v));\n if (numValues.length === validValues.length) {\n const min = Math.min(...numValues);\n const max = Math.max(...numValues);\n // Example heuristic: percentages\n if (min >= 0 && max <= 100 && (colNameLower.includes(\"percent\") || colNameLower.includes(\"rate\") || colNameLower.includes(\"score\"))) {\n extraLines.push(\" validation: {\\n min: 0,\\n max: 100\\n }\");\n } else if (min >= 0 && (colNameLower.includes(\"count\") || colNameLower.includes(\"total\") || colNameLower.includes(\"amount\"))) {\n extraLines.push(\" validation: {\\n min: 0\\n }\");\n }\n }\n\n // No currency heuristic. `admin.currency` was never a declared option and\n // nothing reads it, so a column called `price` got a block of config that did\n // nothing — invisible because this generator emits *source text*, which no\n // typechecker ever saw. Reinstate it by adding `currency` to\n // AdminNumberOptions first, and something that renders it.\n }\n\n // ── JSON / JSONB Analysis ────────────────────────────────────────────\n if (currentPropType === \"map\" || pgDataType.includes(\"json\")) {\n // PostGres jsonb can be object or array\n let allArrays = true;\n let allObjects = true;\n\n for (const v of validValues) {\n let parsed = v;\n if (typeof v === \"string\") {\n try { parsed = JSON.parse(v); } catch (e) { allArrays = false; allObjects = false; break; }\n }\n if (Array.isArray(parsed)) {\n allObjects = false;\n } else if (typeof parsed === \"object\" && parsed !== null) {\n allArrays = false;\n } else {\n allArrays = false;\n allObjects = false;\n }\n }\n\n if (allArrays && !allObjects) {\n result.propType = \"array\";\n // Infer inner type\n let allNumbers = true;\n let allStrings = true;\n for (const v of validValues) {\n const parsed = typeof v === \"string\" ? JSON.parse(v) : v;\n for (const item of parsed) {\n if (typeof item !== \"number\") allNumbers = false;\n if (typeof item !== \"string\") allStrings = false;\n }\n }\n const innerType = allNumbers ? \"number\" : allStrings ? \"string\" : \"map\";\n extraLines.push(` of: { name: \"${humanize(columnName)} Item\", type: \"${innerType}\" }`);\n } else {\n result.propType = \"map\";\n\n // Infer inner schema\n if (allObjects && validValues.length > 0) {\n const schema: Record<string, string> = {};\n for (const v of validValues) {\n const parsed = typeof v === \"string\" ? JSON.parse(v) : v;\n for (const [k, val] of Object.entries(parsed)) {\n if (val === null || val === undefined) continue;\n const type = typeof val;\n if (type === \"string\" || type === \"number\" || type === \"boolean\") {\n if (!schema[k]) schema[k] = type;\n else if (schema[k] !== type) schema[k] = \"mixed\";\n } else if (Array.isArray(val)) {\n if (!schema[k]) schema[k] = \"array\";\n else if (schema[k] !== \"array\") schema[k] = \"mixed\";\n } else if (type === \"object\") {\n if (!schema[k]) schema[k] = \"map\";\n else if (schema[k] !== \"map\") schema[k] = \"mixed\";\n }\n }\n }\n\n const keys = Object.keys(schema).filter(k => schema[k] !== \"mixed\");\n if (keys.length > 0) {\n const props = keys.map(k => {\n return `\\n ${k}: { name: \"${humanize(k)}\", type: \"${schema[k]}\" }`;\n }).join(\",\");\n extraLines.push(` properties: {${props}\\n }`);\n } else {\n extraLines.push(\" keyValue: true\");\n }\n } else {\n extraLines.push(\" keyValue: true\");\n }\n }\n }\n\n // ── String Analysis ──────────────────────────────────────────────────\n if (currentPropType === \"string\") {\n // Date/Time Strings\n if (validValues.every(v => typeof v === \"string\" && ISO_8601_REGEX.test(v))) {\n result.propType = \"date\";\n return result;\n }\n\n // Implicit Enums\n const uniqueValues = new Set(validValues);\n // Determine the maximum length among unique values\n let maxEnumLength = 0;\n for (const v of uniqueValues) {\n if (typeof v === \"string\" && v.length > maxEnumLength) {\n maxEnumLength = v.length;\n }\n }\n\n // Ensure no empty string, max length makes sense, and fewer unique values than total values (unless small total)\n if (uniqueValues.size > 0 && uniqueValues.size <= 5 && maxEnumLength <= 50 && validValues.length > uniqueValues.size && !uniqueValues.has(\"\")) {\n const isLikelyId = isPk || colNameLower.endsWith(\"_id\");\n if (!isLikelyId) {\n const enumEntries = Array.from(uniqueValues).map(v => `{ id: ${JSON.stringify(v)}, label: ${JSON.stringify(humanize(v as string))} }`).join(\", \");\n extraLines.push(` enum: [${enumEntries}]`);\n result.extra = extraLines.length > 0 ? \"\\n\" + extraLines.join(\",\\n\") + \",\" : \"\";\n return result; // Skip other string checks if it's an enum\n }\n }\n\n // UUID / CUID Detection\n const allUuid = validValues.every(v => typeof v === \"string\" && UUID_REGEX.test(v));\n const allCuid = validValues.every(v => typeof v === \"string\" && CUID_REGEX.test(v));\n if (allUuid) {\n if (isPk) extraLines.push(\" isId: \\\"uuid\\\"\");\n } else if (allCuid) {\n if (isPk) extraLines.push(\" isId: \\\"cuid\\\"\");\n }\n\n // No colour heuristic. `admin.color` was never a declared option and nothing\n // reads it — the same dead branch `currency` was. This generator emits *source\n // text*, so no typechecker ever saw either. Reinstate by adding the option and\n // something that renders it first.\n\n // Text Lengths, Multiline & Markdown\n let maxLength = 0;\n let hasNewlines = false;\n let hasMarkdown = false;\n for (const v of validValues) {\n if (typeof v === \"string\") {\n if (v.length > maxLength) maxLength = v.length;\n if (v.includes(\"\\n\")) hasNewlines = true;\n if (/^#{1,6}\\s+.+/m.test(v) || /\\*\\*.+\\*\\*/.test(v) || /\\[.+\\]\\(.+\\)/.test(v) || /^\\s*[-*]\\s+.+/m.test(v)) {\n hasMarkdown = true;\n }\n }\n }\n\n // `multiline` and `markdown` are *admin* property options, so they have\n // to sit inside the `admin` block. Emitted at the top of the property —\n // where they were — `PostgresCollectionConfig` does not declare them and\n // the generated file does not compile.\n if (hasMarkdown) {\n adminOptions.push(\"multiline: true\", \"markdown: true\");\n } else if (hasNewlines || maxLength > 100) {\n adminOptions.push(\"multiline: true\");\n }\n\n if (maxLength > 0 && maxLength < 10000) { // arbitrary cap to avoid huge limits\n // Pad the max length slightly for the constraint\n const paddedMax = Math.ceil((maxLength * 1.5) / 10) * 10;\n // Only add length constraint if it seems like a bounded string, not a long text\n if (paddedMax < 255) {\n extraLines.push(` validation: {\\n max: ${paddedMax}\\n }`);\n }\n }\n\n // Storage & Media (Extracted from old logic)\n const isUrl = colNameLower.endsWith(\"_url\") || colNameLower.endsWith(\"_uri\") || colNameLower.endsWith(\"_link\");\n const isMedia = colNameLower.includes(\"image\") || colNameLower.includes(\"avatar\") || colNameLower.includes(\"photo\") || colNameLower.includes(\"logo\") || colNameLower.includes(\"cover\");\n\n const allAbsoluteUrls = validValues.every(v => typeof v === \"string\" && (v.startsWith(\"http://\") || v.startsWith(\"https://\")));\n if (allAbsoluteUrls) {\n const isImage = validValues.some(v => typeof v === \"string\" && v.match(/\\.(jpeg|jpg|gif|png|webp|svg)/i));\n if (isImage || isMedia) {\n extraLines.push(\" url: true\");\n adminOptions.push(\"urlPreview: \\\"image\\\"\");\n } else {\n extraLines.push(\" url: true\");\n }\n } else {\n const hasFileExtension = validValues.some(v => typeof v === \"string\" && v.match(/\\.[a-zA-Z0-9]+$/));\n if (hasFileExtension) {\n const firstVal = validValues[0] as string;\n const lastSlash = firstVal.lastIndexOf(\"/\");\n const inferredStoragePath = lastSlash > 0 ? firstVal.substring(0, lastSlash) : \"files\";\n extraLines.push(` storage: {\\n storagePath: \"${inferredStoragePath}\"\\n }`);\n } else if (isUrl) {\n extraLines.push(\" url: true\");\n if (isMedia) adminOptions.push(\"urlPreview: \\\"image\\\"\");\n }\n }\n }\n\n if (emitAdmin && adminOptions.length > 0) {\n extraLines.push(` admin: {\\n${adminOptions.map((o) => ` ${o}`).join(\",\\n\")}\\n }`);\n }\n\n result.extra = extraLines.length > 0 ? \"\\n\" + extraLines.join(\",\\n\") + \",\" : \"\";\n return result;\n}\n","/**\n * The PostgreSQL type → Rebase property type mapping.\n *\n * Split out of `introspect-db-logic` so that the structural analysis can use it\n * without importing the generator, which imports the analysis. Re-exported from\n * `introspect-db-logic` so existing callers keep their import path.\n */\n\n/**\n * Map a PostgreSQL data type to a Rebase property type.\n */\nexport function mapPgType(dataType: string): string {\n const dt = dataType.toLowerCase();\n\n // Interval MUST be checked before numeric (\"interval\" contains \"int\")\n if (dt === \"interval\") return \"string\";\n\n // Array types MUST be checked before numeric (\"_int4\" contains \"int\")\n if (dt === \"array\" || dt.startsWith(\"_\")) return \"array\";\n\n // Numeric types\n if (\n dt.includes(\"int\") || // integer, smallint, bigint\n dt.includes(\"numeric\") ||\n dt.includes(\"decimal\") ||\n dt.includes(\"serial\") || // serial, bigserial\n dt === \"real\" ||\n dt === \"float4\" ||\n dt === \"float8\" ||\n dt === \"double precision\" ||\n dt === \"money\"\n ) {\n return \"number\";\n }\n\n // Boolean\n if (dt.includes(\"bool\")) return \"boolean\";\n\n // Date / Time\n if (dt.includes(\"time\") || dt.includes(\"date\")) return \"date\";\n\n // JSON\n if (dt === \"json\" || dt === \"jsonb\") return \"map\";\n\n // Binary\n if (dt === \"bytea\") return \"binary\";\n\n // Network types\n if (dt === \"inet\" || dt === \"cidr\" || dt === \"macaddr\" || dt === \"macaddr8\") return \"string\";\n\n // UUID\n if (dt === \"uuid\") return \"string\";\n\n // Text/varchar/char — default to string\n return \"string\";\n}\n","/**\n * What a schema's *structure* says about the app on top of it.\n *\n * Introspection has always been a table mirror: one table in, one collection\n * out, one nav entry each, every column a form field. A schema of thirty tables\n * produces thirty sidebar entries, and a panel whose navigation is a list of\n * table names reads as a database browser however good the fields are — which\n * is the actual complaint about generated admin panels, and is structural, not\n * cosmetic.\n *\n * Most of what separates the eight nouns a user navigates by from the thirty\n * tables underneath them is written down in the schema already: which tables\n * only exist to join two others, which are small referenced code lists, which\n * rows cannot outlive a parent row. This module reads that.\n *\n * ## Structure only\n *\n * Nothing here looks at a column or table *name*. Name heuristics — `status`,\n * `*_url`, `image`, `created_at` — are wrong exactly when a schema is not in\n * English, or is domain-specific, or spells things differently, and they are\n * wrong silently. Every rule below is a fact the database enforces: key\n * composition, foreign-key direction and delete rule, uniqueness, nullability,\n * declared type and length, generated-ness, row count.\n *\n * That constraint has a cost, and it is worth stating: a schema that declares\n * nothing beyond `NOT NULL` gives this module very little to work with, and it\n * returns `entity` for everything rather than guessing. Under-classifying is\n * the intended failure mode. A table wrongly hidden from the navigation is a\n * table the user cannot find; a table wrongly left in it is merely the status\n * quo.\n *\n * Pure module: no I/O. Row counts come in on {@link SchemaMetadata.rowCounts},\n * which the caller fills from {@link ./introspect-db-queries.countRowsUpTo} for\n * the tables {@link lookupCandidates} names.\n */\nimport type {\n ForeignKeyRow,\n SchemaMetadata,\n TableColumn,\n TableMeta,\n UniqueConstraintRow\n} from \"./introspect-db-logic\";\nimport { mapPgType } from \"./introspect-db-types\";\nimport type { CheckFactsByTable } from \"./introspect-db-constraints\";\n\n// ── Thresholds ────────────────────────────────────────────────────────\n\n/**\n * The row count above which a referenced table is a real entity rather than a\n * code list. Deliberately low: `pagila.category` has 16 rows and `language` 6,\n * while `actor` has 200 and `country` 109 — the gap between \"a fixed set\n * somebody typed once\" and \"data the app accumulates\" is wide, and picking a\n * number in the middle of it costs nothing.\n */\nexport const LOOKUP_MAX_ROWS = 50;\n\n/**\n * The most payload columns a code list may carry. A code list is an id, a\n * label, and perhaps a sort key or a flag; past that it is a table with\n * attributes, which is an entity.\n */\nexport const LOOKUP_MAX_PAYLOAD_COLUMNS = 3;\n\n/**\n * The most enum values a board can usefully have as columns. A kanban with\n * thirty columns is a horizontally scrolling table.\n */\nexport const KANBAN_MAX_VALUES = 12;\n\n/** Below this, a \"board\" is one or two columns — a filter, not a board. */\nexport const KANBAN_MIN_VALUES = 2;\n\n/**\n * How many columns a generated list view shows before it stops being readable.\n * Only applied when a table has more properties than this; a six-column table\n * gets no `listProperties` at all rather than a restatement of its own columns.\n */\nexport const LIST_PROPERTIES_CAP = 6;\n\n// ── Roles ─────────────────────────────────────────────────────────────\n\n/**\n * What a table *is*, structurally.\n *\n * - `entity` — a thing the app is about. Gets a collection and a nav entry.\n * - `junction` — exists only to relate two other tables. Gets no collection at\n * all; it becomes a many-to-many relation on both sides.\n * - `lookup` — a small, referenced, self-contained code list. Gets a collection,\n * grouped away from the entities rather than listed beside them.\n * - `owned-child` — rows that belong to exactly one parent row and are reached\n * through it. Gets a collection (it is a real table with real rows, and the\n * API still serves it) but no nav entry: it already renders as a tab on its\n * parent.\n */\nexport type TableRole = \"entity\" | \"junction\" | \"lookup\" | \"owned-child\";\n\n/**\n * Why a table was called someone's child, weakest last.\n *\n * Carried into the generated file as a comment. A reader who disagrees with the\n * classification needs to see what it was based on to know which line to change.\n */\nexport type OwnershipEvidence =\n /** The only foreign key declared `ON DELETE CASCADE`. */\n | \"cascade-delete\"\n /** The only foreign key that is part of the table's primary key. */\n | \"identifying-key\"\n /** The only foreign key that is `NOT NULL`. */\n | \"sole-required-key\"\n /** First column of a composite primary key made entirely of foreign keys. */\n | \"leading-key-column\";\n\nexport interface JunctionShape {\n sourceTable: string;\n sourceColumn: string;\n targetTable: string;\n targetColumn: string;\n}\n\nexport interface TableClassification {\n table: string;\n role: TableRole;\n /** One line, in prose, for the generated file. */\n reason: string;\n /** Set when `role === \"owned-child\"`. */\n owner?: { table: string; column: string; evidence: OwnershipEvidence };\n /** Set when `role === \"junction\"`. */\n junction?: JunctionShape;\n}\n\n// ── Column-level structural predicates ────────────────────────────────\n\n/**\n * A timestamp the database maintains: a temporal column defaulting to the\n * transaction clock.\n *\n * This is the structural stand-in for the `created_at`/`updated_at` name check.\n * It is strictly better than the name: it catches `fecha_creacion` and\n * `last_update` (pagila's spelling, which the name list misses), and it does not\n * fire on a user-editable `created_at date` column that has no default and which\n * the name check would wrongly make read-only.\n */\nexport function isAutoTimestamp(column: TableColumn): boolean {\n if (mapPgType(column.data_type) !== \"date\") return false;\n const columnDefault = (column.column_default ?? \"\").toLowerCase();\n if (!columnDefault) return false;\n return /\\b(now\\(\\)|current_timestamp|current_date|current_time|localtimestamp|localtime|transaction_timestamp\\(\\)|statement_timestamp\\(\\)|clock_timestamp\\(\\))/.test(columnDefault);\n}\n\n/** A key the database fills in: identity, serial, or a uuid-generating default. */\nexport function isGeneratedKey(column: TableColumn): boolean {\n if (column.is_identity === \"YES\") return true;\n const columnDefault = (column.column_default ?? \"\").toLowerCase();\n if (!columnDefault) return false;\n return columnDefault.includes(\"nextval(\") ||\n columnDefault.includes(\"gen_random_uuid\") ||\n columnDefault.includes(\"uuid_generate\");\n}\n\n/** A column Postgres computes; writing to it is an error. */\nexport function isGeneratedColumn(column: TableColumn): boolean {\n return column.is_generated === \"ALWAYS\";\n}\n\n/**\n * Types that exist to be searched or indexed, never to be typed into.\n *\n * A `tsvector` column is a derived search index — maintained by a trigger, a\n * generated expression, or an application job — and its contents are lexeme\n * positions, not text. Pagila's `film.fulltext` is one, and introspection used\n * to emit it as an ordinary required string: a mandatory form field whose\n * correct value no user can produce, on the sixth column of the list view.\n */\nexport function isDerivedIndexColumn(column: TableColumn): boolean {\n return column.udt_name === \"tsvector\" || column.udt_name === \"tsquery\";\n}\n\n/** Anything the user cannot meaningfully edit, whatever the reason. */\nexport function isReadOnlyColumn(column: TableColumn): boolean {\n return isGeneratedColumn(column) || isDerivedIndexColumn(column);\n}\n\n/**\n * A string column with a declared maximum length.\n *\n * `varchar(50)` and `text` are the same type to an application but not to the\n * author: choosing a bound is a statement that the value is short and\n * label-like, which is what makes this usable for picking a display column.\n */\nexport function isBoundedString(column: TableColumn): boolean {\n return mapPgType(column.data_type) === \"string\" &&\n typeof column.character_maximum_length === \"number\" &&\n column.character_maximum_length > 0;\n}\n\n/**\n * A column carrying data rather than structure: not a key, not a foreign key,\n * not a database-maintained timestamp, not computed.\n *\n * The count of these is what tells a pure join table from an association that\n * carries its own attributes — `northwind.order_details` has the key shape of a\n * junction and three payload columns, so it is not one.\n */\nexport function isPayloadColumn(column: TableColumn, pks: string[], fkColumns: Set<string>): boolean {\n if (pks.includes(column.column_name)) return false;\n if (fkColumns.has(column.column_name)) return false;\n if (isAutoTimestamp(column)) return false;\n if (isGeneratedColumn(column)) return false;\n return true;\n}\n\n// ── Foreign-key topology ──────────────────────────────────────────────\n\n/** One foreign key, with its columns grouped back together. */\nexport interface ForeignKeyConstraint {\n name: string;\n table: string;\n columns: string[];\n foreignTable: string;\n foreignColumns: string[];\n deleteRule?: string;\n}\n\n/**\n * Groups per-column foreign key rows back into constraints.\n *\n * Rows arrive one per referencing column. A composite key looks exactly like two\n * separate keys until they are grouped by constraint name, and the difference\n * matters: two single-column keys to two tables can be a junction, one\n * two-column key never is.\n */\nexport function groupForeignKeys(fks: ForeignKeyRow[]): ForeignKeyConstraint[] {\n const byName = new Map<string, ForeignKeyConstraint>();\n for (const fk of fks) {\n // Rows from older callers carry no constraint name. Falling back to a\n // per-column key treats each as its own single-column constraint, which\n // is what the generator assumed before names were available.\n const key = `${fk.table_name}::${fk.constraint_name ?? `${fk.column_name}->${fk.foreign_table_name}`}`;\n const existing = byName.get(key);\n if (existing) {\n existing.columns.push(fk.column_name);\n existing.foreignColumns.push(fk.foreign_column_name);\n continue;\n }\n byName.set(key, {\n name: fk.constraint_name ?? `${fk.table_name}_${fk.column_name}_fkey`,\n table: fk.table_name,\n columns: [fk.column_name],\n foreignTable: fk.foreign_table_name,\n foreignColumns: [fk.foreign_column_name],\n deleteRule: fk.delete_rule\n });\n }\n return Array.from(byName.values());\n}\n\n/** Column sets that are unique in a table: its primary key and every unique index. */\nfunction uniqueColumnSets(table: string, pks: string[], uniques: UniqueConstraintRow[]): string[][] {\n const sets: string[][] = [];\n if (pks.length > 0) sets.push(pks);\n for (const unique of uniques) {\n if (unique.table_name === table) sets.push(unique.column_names);\n }\n return sets;\n}\n\nfunction sameColumnSet(a: string[], b: string[]): boolean {\n if (a.length !== b.length) return false;\n const left = [...a].sort();\n const right = [...b].sort();\n return left.every((value, index) => value === right[index]);\n}\n\n// ── Classification ────────────────────────────────────────────────────\n\n/** Everything classification needs, assembled once per run. */\ninterface AnalysisContext {\n tables: Map<string, TableMeta>;\n constraintsByTable: Map<string, ForeignKeyConstraint[]>;\n inboundByTable: Map<string, ForeignKeyConstraint[]>;\n uniques: UniqueConstraintRow[];\n rowCounts: Record<string, number>;\n}\n\nfunction buildContext(metadata: SchemaMetadata, tables: Map<string, TableMeta>): AnalysisContext {\n const allConstraints = groupForeignKeys(metadata.fks);\n const constraintsByTable = new Map<string, ForeignKeyConstraint[]>();\n const inboundByTable = new Map<string, ForeignKeyConstraint[]>();\n\n for (const constraint of allConstraints) {\n const outbound = constraintsByTable.get(constraint.table) ?? [];\n outbound.push(constraint);\n constraintsByTable.set(constraint.table, outbound);\n\n const inbound = inboundByTable.get(constraint.foreignTable) ?? [];\n inbound.push(constraint);\n inboundByTable.set(constraint.foreignTable, inbound);\n }\n\n return {\n tables,\n constraintsByTable,\n inboundByTable,\n uniques: metadata.uniques,\n rowCounts: metadata.rowCounts\n };\n}\n\n/**\n * A table that exists only to relate two others.\n *\n * Requires all of:\n * - exactly two single-column foreign keys, to two distinct columns;\n * - that pair is unique — it is the primary key, or a unique index covers it.\n * Without this the table can hold the same pair twice, which is a list of\n * events between two things, not a set membership;\n * - no payload columns. A join row that carries a quantity is an association\n * with attributes, and collapsing it into a many-to-many silently drops those\n * attributes from the UI entirely;\n * - nothing references it. A junction with its own dependents is something rows\n * point *at*, so it needs an identity of its own.\n */\nfunction classifyJunction(table: string, context: AnalysisContext): TableClassification | null {\n const meta = context.tables.get(table);\n if (!meta) return null;\n\n const constraints = context.constraintsByTable.get(table) ?? [];\n if (constraints.length !== 2) return null;\n if (constraints.some((c) => c.columns.length !== 1)) return null;\n\n const [first, second] = constraints;\n if (first.columns[0] === second.columns[0]) return null;\n\n if ((context.inboundByTable.get(table) ?? []).length > 0) return null;\n\n const pair = [first.columns[0], second.columns[0]];\n const uniqueSets = uniqueColumnSets(table, meta.pks, context.uniques);\n if (!uniqueSets.some((set) => sameColumnSet(set, pair))) return null;\n\n const fkColumns = new Set(pair);\n const payload = meta.columns.filter((column) => isPayloadColumn(column, meta.pks, fkColumns));\n if (payload.length > 0) return null;\n\n return {\n table,\n role: \"junction\",\n reason: `only relates ${first.foreignTable} to ${second.foreignTable}: two foreign keys, unique together, no other data`,\n junction: {\n sourceTable: first.foreignTable,\n sourceColumn: first.columns[0],\n targetTable: second.foreignTable,\n targetColumn: second.columns[0]\n }\n };\n}\n\n/**\n * A small, referenced, self-contained code list.\n *\n * Requires: something references it, it references nothing, it carries few and\n * simple payload columns, and it is small — where \"small\" is a real count, not\n * an estimate. `reltuples` is -1 on any table that has never been analyzed,\n * which is every table in a freshly restored dump, so a count that is merely\n * absent must not read as \"small\".\n */\nfunction classifyLookup(table: string, context: AnalysisContext): TableClassification | null {\n if (!isLookupCandidate(table, context)) return null;\n\n const rowCount = context.rowCounts[table];\n if (typeof rowCount !== \"number\") return null;\n // An empty table is not a code list — there is nothing to look up, and a\n // schema-only restore would otherwise classify half the database this way.\n if (rowCount < 1 || rowCount > LOOKUP_MAX_ROWS) return null;\n\n const referencedBy = (context.inboundByTable.get(table) ?? []).length;\n return {\n table,\n role: \"lookup\",\n reason: `a ${rowCount}-row code list: referenced by ${referencedBy} table(s), references none, ${LOOKUP_MAX_PAYLOAD_COLUMNS} or fewer simple columns`\n };\n}\n\n/**\n * The structural half of the lookup test — everything except the row count.\n *\n * Separate because the count costs a query per table, and the caller only wants\n * to pay it for tables that could possibly qualify.\n */\nfunction isLookupCandidate(table: string, context: AnalysisContext): boolean {\n const meta = context.tables.get(table);\n if (!meta) return false;\n\n if ((context.constraintsByTable.get(table) ?? []).length > 0) return false;\n if ((context.inboundByTable.get(table) ?? []).length === 0) return false;\n\n const payload = meta.columns.filter((column) => isPayloadColumn(column, meta.pks, new Set()));\n if (payload.length > LOOKUP_MAX_PAYLOAD_COLUMNS) return false;\n\n // A code list holds labels and flags. Documents, arrays, files and dates are\n // content, and content means the rows are worth browsing in their own right.\n //\n // Strings are allowed whether or not they declare a length. Requiring\n // `varchar(n)` looked like a way to say \"label, not prose\", and it is —\n // except that a schema written any time recently uses `text` for everything,\n // which is the advice Postgres itself gives. Modern pagila is all `text`, so\n // that rule classified nothing at all on it.\n return payload.every((column) => {\n const type = mapPgType(column.data_type);\n return type === \"boolean\" || type === \"number\" || type === \"string\";\n });\n}\n\n/**\n * Names the tables whose classification depends on a row count.\n *\n * The caller counts these — and only these — before calling\n * {@link classifyTables}. On a schema of any size this is a handful of tables,\n * and the count itself is capped (see `countRowsUpTo`), so the whole extra cost\n * is bounded regardless of how much data the database holds.\n */\nexport function lookupCandidates(metadata: SchemaMetadata, tables: Map<string, TableMeta>): string[] {\n const context = buildContext(metadata, tables);\n return Array.from(tables.keys()).filter((table) => isLookupCandidate(table, context));\n}\n\n/**\n * Rows that belong to one parent row and are reached through it.\n *\n * Requires nothing to reference the table — a table others point at has an\n * identity of its own and belongs in the navigation — and an *unambiguous*\n * owner among its foreign keys. The evidence ladder runs strongest first; each\n * rung is only taken when exactly one key satisfies it, so a table with two\n * equally plausible parents stays an entity rather than being filed under a\n * coin-flip.\n */\nfunction classifyOwnedChild(table: string, context: AnalysisContext): TableClassification | null {\n const meta = context.tables.get(table);\n if (!meta) return null;\n\n const constraints = context.constraintsByTable.get(table) ?? [];\n if (constraints.length === 0) return null;\n if ((context.inboundByTable.get(table) ?? []).length > 0) return null;\n\n const owner = pickOwner(meta, constraints);\n if (!owner) return null;\n\n return {\n table,\n role: \"owned-child\",\n reason: `belongs to ${owner.constraint.foreignTable} (${describeEvidence(owner.evidence)}) and nothing else references it`,\n owner: {\n table: owner.constraint.foreignTable,\n column: owner.constraint.columns[0],\n evidence: owner.evidence\n }\n };\n}\n\nfunction describeEvidence(evidence: OwnershipEvidence): string {\n switch (evidence) {\n case \"cascade-delete\": return \"its only ON DELETE CASCADE foreign key\";\n case \"identifying-key\": return \"its only foreign key inside the primary key\";\n case \"sole-required-key\": return \"its only NOT NULL foreign key\";\n case \"leading-key-column\": return \"the leading column of a primary key made only of foreign keys\";\n }\n}\n\nfunction pickOwner(\n meta: TableMeta,\n constraints: ForeignKeyConstraint[]\n): { constraint: ForeignKeyConstraint; evidence: OwnershipEvidence } | null {\n const singleColumn = constraints.filter((c) => c.columns.length === 1);\n if (singleColumn.length === 0) return null;\n\n const nullability = new Map(meta.columns.map((c) => [c.column_name, c.is_nullable === \"NO\"]));\n\n const cascading = singleColumn.filter((c) => c.deleteRule === \"CASCADE\");\n if (cascading.length === 1) return { constraint: cascading[0], evidence: \"cascade-delete\" };\n\n const identifying = singleColumn.filter((c) => meta.pks.includes(c.columns[0]));\n if (identifying.length === 1) return { constraint: identifying[0], evidence: \"identifying-key\" };\n\n const required = singleColumn.filter((c) => nullability.get(c.columns[0]) === true);\n if (required.length === 1) return { constraint: required[0], evidence: \"sole-required-key\" };\n\n // Last resort, and the only rung that leans on an ordering convention rather\n // than a guarantee: in a table whose whole primary key is foreign keys, the\n // first key column is the parent the rows are filed under.\n // `northwind.order_details` is keyed `(order_id, product_id)` — the rows are\n // lines of an order, which is exactly what that ordering says.\n if (identifying.length === meta.pks.length && meta.pks.length > 1) {\n const leading = identifying.find((c) => c.columns[0] === meta.pks[0]);\n if (leading) return { constraint: leading, evidence: \"leading-key-column\" };\n }\n\n return null;\n}\n\n/**\n * Classifies every table in the schema.\n *\n * Order matters: junction is the most specific and most consequential (the\n * table disappears), so it is tested first; then lookup, which needs no\n * ownership reasoning; then ownership. Anything unmatched is an entity, which\n * is also what every rule falls back to when its evidence is ambiguous.\n */\nexport function classifyTables(\n metadata: SchemaMetadata,\n tables: Map<string, TableMeta>\n): Map<string, TableClassification> {\n const context = buildContext(metadata, tables);\n const result = new Map<string, TableClassification>();\n\n for (const table of tables.keys()) {\n const classification =\n classifyJunction(table, context) ??\n classifyLookup(table, context) ??\n classifyOwnedChild(table, context) ?? {\n table,\n role: \"entity\" as const,\n reason: \"no structural evidence that it is a join table, a code list, or owned by another table\"\n };\n result.set(table, classification);\n }\n\n return result;\n}\n\n// ── Presentation derived from structure ───────────────────────────────\n\n/**\n * The columns a property-level derivation needs, resolved once.\n */\nexport interface ColumnFacts {\n column: TableColumn;\n isPk: boolean;\n isFk: boolean;\n /** Covered by a single-column unique constraint or unique index. */\n isUniqueAlone: boolean;\n isAutoTimestamp: boolean;\n isGenerated: boolean;\n /** Allowed values, from a Postgres enum type or a readable CHECK. */\n enumValues?: string[];\n propType: string;\n}\n\nexport function buildColumnFacts(\n meta: TableMeta,\n metadata: SchemaMetadata,\n enumMap: Map<string, string[]>,\n checkFacts: CheckFactsByTable\n): Map<string, ColumnFacts> {\n const fkColumns = new Set(meta.fks.map((fk) => fk.column_name));\n const singleColumnUniques = new Set(\n metadata.uniques\n .filter((u) => u.table_name === meta.name && u.column_names.length === 1)\n .map((u) => u.column_names[0])\n );\n const tableChecks = checkFacts.get(meta.name);\n\n const facts = new Map<string, ColumnFacts>();\n for (const column of meta.columns) {\n const pgEnum = column.data_type === \"USER-DEFINED\" ? enumMap.get(column.udt_name) : undefined;\n const checkEnum = tableChecks?.get(column.column_name)?.enumValues;\n facts.set(column.column_name, {\n column,\n isPk: meta.pks.includes(column.column_name),\n isFk: fkColumns.has(column.column_name),\n isUniqueAlone: singleColumnUniques.has(column.column_name),\n isAutoTimestamp: isAutoTimestamp(column),\n isGenerated: isGeneratedColumn(column),\n enumValues: pgEnum ?? checkEnum,\n propType: pgEnum ? \"string\" : mapPgType(column.data_type)\n });\n }\n return facts;\n}\n\n/**\n * The column that identifies a row to a human.\n *\n * Structural, in three rungs, strongest first:\n *\n * 1. A single-column unique constraint on a required string. This is as close\n * as a schema comes to declaring \"this is what a row is called\": it is the\n * column a person looks a row up by, and the database guarantees it picks\n * out one row.\n * 2. The first required string that declares a length, when the table also has\n * strings that do not. Choosing `varchar(n)` for one column and `text` for\n * another is the author distinguishing a label from prose.\n * 3. The first required string in declaration order. Weak, but it is the same\n * rung the panel's own fallback stands on, and column order carries real\n * information — the identifying column of a table is written near the top of\n * it, in every schema, in every language.\n *\n * Deliberately not: a column called `name`, or `title`. That works on English\n * schemas written by someone who read the same tutorial. This picks\n * `film.title`, `actor.first_name` and `category.name` out of pagila without\n * knowing what any of those words mean.\n */\nexport function deriveTitleProperty(facts: Map<string, ColumnFacts>): string | undefined {\n const candidates = Array.from(facts.values()).filter((f) =>\n !f.isPk && !f.isFk && !f.isGenerated &&\n f.propType === \"string\" &&\n !f.enumValues &&\n f.column.is_nullable === \"NO\"\n );\n if (candidates.length === 0) return undefined;\n\n const unique = candidates.find((f) => f.isUniqueAlone);\n if (unique) return unique.column.column_name;\n\n const bounded = candidates.filter((f) => isBoundedString(f.column));\n if (bounded.length > 0 && bounded.length < candidates.length) {\n return bounded[0].column.column_name;\n }\n\n return candidates[0].column.column_name;\n}\n\n/**\n * The enum column a board should have as its columns.\n *\n * A board needs a small, closed, always-present set of states. `NOT NULL` is\n * required because a null has no column to sit in; the bounds keep out\n * two-state flags (a filter, not a board) and long code lists (a scrolling\n * table). The first qualifying column in declaration order wins, so the output\n * is stable across runs.\n */\nexport function deriveKanbanProperty(facts: Map<string, ColumnFacts>): string | undefined {\n for (const fact of facts.values()) {\n if (fact.isPk || fact.isFk || fact.isGenerated) continue;\n if (!fact.enumValues) continue;\n if (fact.column.is_nullable !== \"NO\") continue;\n if (fact.enumValues.length < KANBAN_MIN_VALUES || fact.enumValues.length > KANBAN_MAX_VALUES) continue;\n return fact.column.column_name;\n }\n return undefined;\n}\n\n/**\n * The column a list should be sorted by, newest first.\n *\n * Only when the table has exactly one database-maintained timestamp. With two —\n * a created and an updated stamp — the two orderings differ and the schema does\n * not say which the user means, so neither is chosen.\n */\nexport function deriveSort(facts: Map<string, ColumnFacts>): [string, \"desc\"] | undefined {\n const stamps = Array.from(facts.values()).filter((f) => f.isAutoTimestamp);\n if (stamps.length !== 1) return undefined;\n return [stamps[0].column.column_name, \"desc\"];\n}\n\n/**\n * The first `LIST_PROPERTIES_CAP` visible properties, or nothing.\n *\n * Returning nothing when the table is already narrow matters: `listProperties`\n * that restates every column is config the reader has to check against the\n * property list to discover it does nothing, and it silently stops new columns\n * from appearing in the list view when someone adds one later.\n *\n * `hidden` names the properties already marked `hideFromCollection` — spending\n * one of six columns on a value the list does not render is worse than not\n * capping at all.\n */\nexport function deriveListProperties(propertiesOrder: string[], hidden: ReadonlySet<string> = new Set()): string[] | undefined {\n const visible = propertiesOrder.filter((key) => !hidden.has(key));\n if (visible.length <= LIST_PROPERTIES_CAP) return undefined;\n return visible.slice(0, LIST_PROPERTIES_CAP);\n}\n","/**\n * Introspection logic — pure functions and the pipeline that transforms\n * raw PostgreSQL metadata into Rebase collection definition files.\n *\n * This module contains NO side-effects: no fs writes, no pg.Client creation,\n * no process.exit. It is imported by introspect-db.ts (the CLI entry-point)\n * and consumed directly by tests.\n */\nimport { firstFreeKey, toWireKey } from \"@rebasepro/utils\";\nimport { inferPropertyFromData } from \"./introspect-db-inference\";\nimport { humanize } from \"./introspect-db-naming\";\nimport { mapPgType } from \"./introspect-db-types\";\nimport type { CheckFactsByTable } from \"./introspect-db-constraints\";\nimport type { TableClassification } from \"./introspect-db-structure\";\nimport {\n buildColumnFacts,\n deriveKanbanProperty,\n deriveListProperties,\n deriveSort,\n deriveTitleProperty,\n isDerivedIndexColumn,\n isReadOnlyColumn\n} from \"./introspect-db-structure\";\n\n// ── Typed interfaces for SQL query results ────────────────────────────\n\nexport interface TableRow {\n table_name: string;\n /** True for the parent of a partitioned table (`relkind = 'p'`). */\n is_partitioned?: boolean;\n}\n\nexport interface TableColumn {\n table_name: string;\n column_name: string;\n data_type: string;\n udt_name: string;\n is_nullable: string;\n column_default: string | null;\n atttypmod: number | null;\n /** 1-based position in the table, as declared. */\n ordinal_position?: number;\n /** `\"ALWAYS\"` for a generated column, `\"NEVER\"` otherwise. */\n is_generated?: string;\n /** The `GENERATED ALWAYS AS (…)` expression, when `is_generated` says so. */\n generation_expression?: string | null;\n /** `\"YES\"` for an identity column. */\n is_identity?: string;\n /** `\"ALWAYS\"` or `\"BY DEFAULT\"` on an identity column. */\n identity_generation?: string | null;\n /** The declared `varchar(n)` / `char(n)` bound, if any. */\n character_maximum_length?: number | null;\n numeric_precision?: number | null;\n numeric_scale?: number | null;\n}\n\nexport interface EnumValue {\n enum_name: string;\n enum_value: string;\n sort_order: number;\n}\n\nexport interface PrimaryKeyRow {\n table_name: string;\n column_name: string;\n}\n\nexport interface ForeignKeyRow {\n table_name: string;\n column_name: string;\n foreign_table_name: string;\n foreign_column_name: string;\n /** Name of the FK constraint — the only way to tell composite keys apart. */\n constraint_name?: string;\n /** 1-based position of this column within its constraint. */\n ordinal?: number;\n /** `\"CASCADE\"`, `\"RESTRICT\"`, `\"SET NULL\"`, `\"SET DEFAULT\"`, `\"NO ACTION\"`. */\n delete_rule?: string;\n}\n\n/** A unique constraint or unique index, as an ordered column list. */\nexport interface UniqueConstraintRow {\n table_name: string;\n constraint_name: string;\n column_names: string[];\n}\n\n/** A CHECK constraint, as `pg_get_constraintdef` renders it. */\nexport interface CheckConstraintRow {\n table_name: string;\n constraint_name: string;\n definition: string;\n}\n\n/** A `COMMENT ON TABLE` (null `column_name`) or `COMMENT ON COLUMN`. */\nexport interface CommentRow {\n table_name: string;\n column_name: string | null;\n comment: string;\n}\n\n/**\n * Everything one introspection run reads from the database.\n *\n * Passed around as one value so a new signal means a new field here rather than\n * a new parameter on every function between the query and the generator — the\n * shape `generateCollectionFile` had grown to seven positional arguments by.\n */\nexport interface SchemaMetadata {\n schema: string;\n tables: TableRow[];\n columns: TableColumn[];\n enumValues: EnumValue[];\n pks: PrimaryKeyRow[];\n fks: ForeignKeyRow[];\n uniques: UniqueConstraintRow[];\n checks: CheckConstraintRow[];\n comments: CommentRow[];\n /**\n * Row counts for the tables that needed one, capped — see `countRowsUpTo`.\n * Absent for every table introspection never had a reason to count.\n */\n rowCounts: Record<string, number>;\n}\n\nexport interface TableMeta {\n name: string;\n columns: TableColumn[];\n pks: string[];\n fks: ForeignKeyRow[];\n}\n\n// ── Irregular plurals that naive rules can't handle ───────────────────\n\nconst IRREGULAR_SINGULARS: Record<string, string> = {\n people: \"person\",\n children: \"child\",\n men: \"man\",\n women: \"woman\",\n mice: \"mouse\",\n geese: \"goose\",\n teeth: \"tooth\",\n feet: \"foot\",\n data: \"datum\",\n media: \"medium\",\n criteria: \"criterion\",\n phenomena: \"phenomenon\"\n};\n\n/**\n * Plurals in \"-ves\" whose singular really ends in f/fe, and which of the two.\n *\n * A blanket \"-ves\" -> \"-f\" rule gets `knives` -> `knif`, and mangles every\n * ordinary \"-ive\" noun that happens to be plural along with it: `archives` ->\n * `archif`, `objectives` -> `objectif`. The set of English words that genuinely\n * swap f/fe for ves is small and closed, so it is listed rather than guessed —\n * anything else ending in \"ves\" drops the trailing 's' like any other plural.\n *\n * Matched on the whole word, not on the suffix: `olives` ends in `lives`.\n */\nconst VES_SINGULAR_ENDINGS: Record<string, \"f\" | \"fe\"> = {\n calves: \"f\", dwarves: \"f\", elves: \"f\", halves: \"f\", hooves: \"f\",\n leaves: \"f\", loaves: \"f\", scarves: \"f\", selves: \"f\", sheaves: \"f\",\n shelves: \"f\", thieves: \"f\", wharves: \"f\", wolves: \"f\",\n knives: \"fe\", lives: \"fe\", wives: \"fe\"\n};\n\n/** Words ending in 's' that are already singular. */\nconst UNCOUNTABLE = new Set([\n \"status\", \"campus\", \"virus\", \"bus\", \"plus\", \"census\",\n \"diagnosis\", \"analysis\", \"basis\", \"crisis\", \"thesis\",\n \"synopsis\", \"parenthesis\", \"hypothesis\", \"emphasis\",\n \"news\", \"series\", \"species\", \"means\", \"athletics\",\n \"economics\", \"electronics\", \"mathematics\", \"physics\",\n \"politics\", \"statistics\"\n]);\n\nexport function singularize(word: string): string {\n const lower = word.toLowerCase();\n\n // Check irregular forms\n if (IRREGULAR_SINGULARS[lower]) {\n // Preserve the original casing of the first character\n const singular = IRREGULAR_SINGULARS[lower];\n return word[0] === word[0].toUpperCase()\n ? singular.charAt(0).toUpperCase() + singular.slice(1)\n : singular;\n }\n\n // Check uncountable\n if (UNCOUNTABLE.has(lower)) return word;\n\n // Latin/Greek -es endings (diagnosis -> diagnosis is uncountable, but \"addresses\" -> \"address\")\n if (lower.endsWith(\"ices\") && lower.length > 5) {\n // e.g. \"indices\" -> \"index\", \"vertices\" -> \"vertex\"\n return word.slice(0, -4) + \"ex\";\n }\n if (lower.endsWith(\"ies\") && lower.length > 3) {\n return word.slice(0, -3) + \"y\";\n }\n if (VES_SINGULAR_ENDINGS[lower]) {\n // e.g. \"wolves\" -> \"wolf\", \"knives\" -> \"knife\"\n return word.slice(0, -3) + VES_SINGULAR_ENDINGS[lower];\n }\n if (lower.endsWith(\"ches\") || lower.endsWith(\"shes\") || lower.endsWith(\"sses\") || lower.endsWith(\"xes\") || lower.endsWith(\"zes\")) {\n return word.slice(0, -2);\n }\n if (lower.endsWith(\"ses\") && !lower.endsWith(\"sses\")) {\n // e.g. \"responses\" -> \"response\", \"databases\" -> \"database\"\n return word.slice(0, -1);\n }\n if (lower.endsWith(\"s\") && !lower.endsWith(\"ss\") && !lower.endsWith(\"us\") && !lower.endsWith(\"is\")) {\n return word.slice(0, -1);\n }\n\n return word;\n}\n\n/**\n * Convert a snake_case table name to a camelCase + \"Collection\" variable name.\n * e.g. \"company_token\" -> \"companyTokenCollection\"\n */\nexport function toCollectionVarName(tableName: string): string {\n const camel = tableName.replace(/_([a-z])/g, (_g, letter: string) => letter.toUpperCase()) + \"Collection\";\n // Only reshapes names that are not identifiers already, so every table that\n // generated a working file keeps the exact variable name it had. A table\n // called `2024 archive` used to emit `const 2024 archiveCollection`, which\n // is three syntax errors rather than a declaration.\n if (JS_IDENTIFIER.test(camel)) return camel;\n const sanitized = camel.replace(/[^A-Za-z0-9_$]/g, \"_\");\n return /^[0-9]/.test(sanitized) ? `_${sanitized}` : sanitized;\n}\n\nexport function getIconForTable(tableName: string): string {\n const table = tableName.toLowerCase();\n if (table.includes(\"user\") || table.includes(\"account\") || table.includes(\"member\") || table.includes(\"customer\") || table.includes(\"client\") || table.includes(\"patient\")) return \"Users\";\n if (table.includes(\"post\") || table.includes(\"article\") || table.includes(\"blog\") || table.includes(\"page\")) return \"FileText\";\n if (table.includes(\"product\") || table.includes(\"item\")) return \"Package\";\n if (table.includes(\"order\") || table.includes(\"cart\") || table.includes(\"purchase\") || table.includes(\"invoice\")) return \"ShoppingCart\";\n if (table.includes(\"setting\") || table.includes(\"config\")) return \"Settings\";\n if (table.includes(\"tag\") || table.includes(\"categor\")) return \"Tag\";\n if (table.includes(\"image\") || table.includes(\"photo\") || table.includes(\"media\") || table.includes(\"asset\")) return \"Image\";\n if (table.includes(\"notification\") || table.includes(\"message\") || table.includes(\"email\")) return \"Mail\";\n if (table.includes(\"log\") || table.includes(\"audit\") || table.includes(\"event\")) return \"Activity\";\n if (table.includes(\"subscription\") || table.includes(\"plan\") || table.includes(\"billing\")) return \"CreditCard\";\n if (table.includes(\"comment\") || table.includes(\"review\") || table.includes(\"feedback\")) return \"MessageCircle\";\n return \"Database\";\n}\n\nexport { mapPgType };\n\n// ── Build the enum map from query results ─────────────────────────────\n\nexport function buildEnumMap(enumValues: EnumValue[]): Map<string, string[]> {\n const enumMap = new Map<string, string[]>();\n for (const ev of enumValues) {\n const existing = enumMap.get(ev.enum_name);\n if (existing) {\n existing.push(ev.enum_value);\n } else {\n enumMap.set(ev.enum_name, [ev.enum_value]);\n }\n }\n return enumMap;\n}\n\n// ── Build the tables map from raw query results ───────────────────────\n\nexport function buildTablesMap(\n tables: TableRow[],\n columns: TableColumn[],\n pks: PrimaryKeyRow[],\n fks: ForeignKeyRow[]\n): Map<string, TableMeta> {\n const tablesMap = new Map<string, TableMeta>();\n for (const t of tables) {\n tablesMap.set(t.table_name, {\n name: t.table_name,\n columns: columns.filter((c) => c.table_name === t.table_name),\n pks: pks.filter((pk) => pk.table_name === t.table_name).map((pk) => pk.column_name),\n fks: fks.filter((fk) => fk.table_name === t.table_name)\n });\n }\n return tablesMap;\n}\n\n// ── Identify join tables ──────────────────────────────────────────────\n\n/**\n * Join tables, identified by column name.\n *\n * Superseded for the CLI by `classifyTables` in `./introspect-db-structure`,\n * which asks the database instead: two single-column keys, unique together, no\n * payload column, nothing referencing the table. This rule folds away\n * `northwind.order_details` — which has the key shape and carries unit price,\n * quantity and discount — because it recognises `id`, `created_at` and\n * `updated_at` by name and calls everything else a foreign key.\n *\n * Still used by `./introspect-runtime`, which builds collections in memory from\n * a narrower set of catalog queries and has no unique-constraint or row-count\n * data to reason with.\n */\nexport function identifyJoinTables(tablesMap: Map<string, TableMeta>): Set<string> {\n const joinTables = new Set<string>();\n for (const [tableName, meta] of tablesMap.entries()) {\n if (meta.fks.length === 2) {\n const isLikelyJoinTable = meta.columns.every((c) =>\n meta.fks.some((fk) => fk.column_name === c.column_name) ||\n c.column_name === \"id\" ||\n c.column_name === \"created_at\" ||\n c.column_name === \"updated_at\"\n );\n\n if (isLikelyJoinTable) {\n joinTables.add(tableName);\n }\n }\n }\n return joinTables;\n}\n\n// ── Property ordering heuristics ──────────────────────────────────────\n\n/**\n * Property metadata used to compute display priority.\n * Keeps computePropertyPriority free of any TableMeta coupling.\n */\nexport interface PropertyOrderingContext {\n /** The resolved Rebase property type (e.g. \"string\", \"number\", \"date\", \"relation\"). */\n propType: string;\n /** Whether this column is a primary key. */\n isPk: boolean;\n /** Whether this column is an enum (USER-DEFINED with matching values). */\n isEnum: boolean;\n /** Whether this is a storage/file-upload field (detected from column name). */\n isStorage: boolean;\n /** The PostgreSQL data_type (e.g. \"text\", \"character varying\", \"jsonb\"). */\n pgDataType: string;\n /** The original column index in PostgreSQL (for stable tiebreaking). */\n originalIndex: number;\n}\n\n// — Tier 0: Identity (0–9) ————————————————————————————————————————————\nconst IDENTITY_EXACT: Record<string, number> = {\n id: 0,\n uuid: 1,\n _id: 2\n};\n\n// — Tier 1: Title / Name — the \"display column\" (10–19) ———————————————\nconst TITLE_EXACT: Record<string, number> = {\n name: 10,\n title: 11,\n label: 12,\n display_name: 13,\n displayname: 13,\n headline: 14,\n subject: 15,\n heading: 16\n};\n\n// — Tier 2: Human identity fields (20–29) —————————————————————————————\nconst HUMAN_IDENTITY_EXACT: Record<string, number> = {\n first_name: 20,\n firstname: 20,\n last_name: 21,\n lastname: 21,\n full_name: 22,\n fullname: 22,\n given_name: 22,\n family_name: 23,\n middle_name: 24,\n username: 25,\n user_name: 25,\n email: 26,\n email_address: 26,\n phone: 27,\n phone_number: 27,\n mobile: 27\n};\n\n// — Tier 3: Core descriptors (30–39) ——————————————————————————————————\nconst DESCRIPTOR_EXACT: Record<string, number> = {\n slug: 30,\n code: 31,\n sku: 32,\n reference: 33,\n ref: 33,\n type: 34,\n kind: 34,\n status: 35,\n state: 35,\n role: 36,\n category: 37,\n group: 38,\n priority: 39,\n order: 39,\n sort_order: 39,\n position: 39\n};\n\n// — Tier 12: System timestamps (120–129) ——————————————————————————————\nconst SYSTEM_TIMESTAMP_EXACT: Record<string, number> = {\n created_at: 120,\n createdat: 120,\n creation_date: 120,\n inserted_at: 121,\n updated_at: 122,\n updatedat: 122,\n modified_at: 122,\n last_modified: 122,\n deleted_at: 123,\n deletedat: 123,\n archived_at: 124\n};\n\n// — Pattern-based rules for partial matches ———————————————————————————\nconst TITLE_PATTERNS = [\"name\", \"title\", \"label\"];\nconst LONG_TEXT_NAMES = new Set([\"description\", \"summary\", \"excerpt\", \"abstract\", \"overview\", \"bio\", \"biography\", \"about\"]);\nconst RICH_CONTENT_NAMES = new Set([\"content\", \"body\", \"html\", \"markup\", \"text\", \"article_body\", \"post_body\"]);\nconst MEDIA_PATTERNS = [\"image\", \"avatar\", \"photo\", \"logo\", \"cover\", \"thumbnail\", \"banner\", \"icon\", \"picture\", \"poster\"];\nconst JSON_MAP_NAMES = new Set([\"metadata\", \"meta\", \"config\", \"configuration\", \"settings\", \"options\", \"preferences\", \"data\", \"payload\", \"attributes\", \"extra\", \"additional_info\"]);\n\n/**\n * Compute a numeric priority score for a property.\n * Lower scores appear first in the generated `propertiesOrder` array.\n *\n * The system uses 14 tiers (0–139), with the original column index\n * added as a fractional tiebreaker (originalIndex / 10000) to\n * guarantee stable ordering within the same tier.\n *\n * Pure function — no side effects.\n */\nexport function computePropertyPriority(\n columnName: string,\n ctx: PropertyOrderingContext\n): number {\n // Normalize camelCase/PascalCase to snake_case, then lowercase\n const col = columnName.replace(/([a-z0-9])([A-Z])/g, \"$1_$2\").toLowerCase();\n const tiebreaker = ctx.originalIndex / 10000;\n\n // ── Tier 0: Primary key identity fields\n if (ctx.isPk) {\n const exactScore = IDENTITY_EXACT[col];\n return (exactScore ?? 5) + tiebreaker;\n }\n\n // ── Tier 12: System timestamps (check early to prevent false matches)\n const systemTs = SYSTEM_TIMESTAMP_EXACT[col];\n if (systemTs !== undefined) {\n return systemTs + tiebreaker;\n }\n\n // ── Tier 1: Title / Name exact matches\n const titleExact = TITLE_EXACT[col];\n if (titleExact !== undefined) {\n return titleExact + tiebreaker;\n }\n\n // ── Tier 2: Human identity exact matches\n const humanExact = HUMAN_IDENTITY_EXACT[col];\n if (humanExact !== undefined) {\n return humanExact + tiebreaker;\n }\n\n // ── Tier 3: Core descriptor exact matches\n const descriptorExact = DESCRIPTOR_EXACT[col];\n if (descriptorExact !== undefined) {\n return descriptorExact + tiebreaker;\n }\n\n // ── Tier 1b: Title-like partial matches (e.g. \"product_name\", \"page_title\")\n // Score 17–19 so they rank after exact matches but still in tier 1.\n for (const pattern of TITLE_PATTERNS) {\n if (col.includes(pattern) && col !== pattern) {\n return 17 + tiebreaker;\n }\n }\n\n // ── Tier 9: Media / file upload fields (check before general strings)\n if (ctx.isStorage) {\n return 90 + tiebreaker;\n }\n for (const pattern of MEDIA_PATTERNS) {\n if (col.includes(pattern)) {\n return 91 + tiebreaker;\n }\n }\n if (col.endsWith(\"_url\") || col.endsWith(\"_uri\") || col.endsWith(\"_link\")) {\n return 92 + tiebreaker;\n }\n\n // ── Tier 7: Long text fields\n if (LONG_TEXT_NAMES.has(col)) {\n return 70 + tiebreaker;\n }\n\n // ── Tier 8: Rich content fields\n if (RICH_CONTENT_NAMES.has(col)) {\n return 80 + tiebreaker;\n }\n\n // ── Tier 10: JSON / Map types\n if (ctx.propType === \"map\") {\n return JSON_MAP_NAMES.has(col) ? 100 + tiebreaker : 105 + tiebreaker;\n }\n\n // ── Tier 11: Array types\n if (ctx.propType === \"array\") {\n return 110 + tiebreaker;\n }\n\n // ── Tier 6: Owning relations\n if (ctx.propType === \"relation\") {\n return 60 + tiebreaker;\n }\n\n // ── Tier 4: Short text, enums, booleans — \"quick glance\" fields\n if (ctx.isEnum) {\n return 40 + tiebreaker;\n }\n if (ctx.propType === \"boolean\") {\n return 45 + tiebreaker;\n }\n if (ctx.propType === \"string\" && ctx.pgDataType !== \"text\") {\n // Short string (varchar, char, uuid that's not a PK)\n return 42 + tiebreaker;\n }\n\n // ── Tier 5: Numbers & user-facing dates\n if (ctx.propType === \"number\") {\n return 50 + tiebreaker;\n }\n if (ctx.propType === \"date\") {\n // A date that isn't a system timestamp (already handled above)\n return 55 + tiebreaker;\n }\n\n // ── Tier 7b: text data_type that didn't match long-text names\n if (ctx.propType === \"string\" && ctx.pgDataType === \"text\") {\n return 75 + tiebreaker;\n }\n\n // ── Tier 13: Fallback / unknown\n return 130 + tiebreaker;\n}\n\n/**\n * Sort a `propertiesOrder` array using the priority heuristic.\n * Returns a new sorted array; does not mutate the input.\n *\n * @param entries - Array of { key, columnName, propType, ... } objects\n * carrying the information needed to compute priority.\n */\nexport interface PropertyOrderEntry {\n /** The property key in the generated collection (may differ from columnName for relations). */\n key: string;\n /** The ordering context for this property. */\n ctx: PropertyOrderingContext;\n}\n\nexport function sortPropertiesOrder(entries: PropertyOrderEntry[]): string[] {\n return [...entries]\n .sort((a, b) => computePropertyPriority(a.key, a.ctx) - computePropertyPriority(b.key, b.ctx))\n .map((e) => e.key);\n}\n\n// ── Generate collection file content ──────────────────────────────────\n\nexport interface GeneratedFile {\n tableName: string;\n fileName: string;\n content: string;\n}\n\n/**\n * The structural analysis a run can hand the generator.\n *\n * Optional in full, and the generator degrades to exactly its previous output\n * without it. That is not politeness towards old callers: three existing test\n * suites and the `rebase init` scaffold path build a `TableMeta` by hand and\n * have no database to read constraints or row counts from, and they must keep\n * producing a valid collection.\n */\n/**\n * Which `defineCollection` — if any — the project being generated into can import.\n *\n * A bare `const x: PostgresCollectionConfig = { … }` annotation widens `properties`\n * to `Record<string, …>`, and every key-shaped field in the admin block —\n * `titleProperty`, `sort`, `propertiesOrder`, `listProperties`, `fixedFilter` — is\n * derived from those keys. Annotated, they accept any string: introspection was\n * emitting a `propertiesOrder` array that nothing checked, so renaming a column and\n * re-introspecting left a stale key that compiled silently. `defineCollection` is\n * the identity function whose `const P` type parameter keeps the keys literal, which\n * is what turns that checking on.\n *\n * There are two of them and they are not interchangeable:\n *\n * - `admin-types` — `@rebasepro/cms-types`. Its index side-effect-imports\n * `augment.ts`, so importing it is also what *declares* the `admin` block. Only a\n * project that depends on the package can resolve it.\n * - `common` — `@rebasepro/common`. Same key inference, no admin surface, no React\n * anywhere in its graph (`scripts/headless-guard` lists it as core). This is the\n * headless flavour.\n * - `annotation` — neither package is declared, so neither import would resolve and\n * the old annotation is the only honest thing to emit. Projects scaffolded before\n * `@rebasepro/common` joined the headless config package land here.\n *\n * The last two emit **no admin block, on the collection or on any property**. That is\n * not a downgrade: `@rebasepro/types` declares no `admin` field at all, so the block\n * introspection used to emit was a type error in every headless project it was\n * written into. See `packages/cms-types/src/augment.ts`.\n */\nexport type CollectionBuilder = \"admin-types\" | \"common\" | \"annotation\";\n\n/**\n * The package specifiers the generated files name, spelled once.\n *\n * Written as constants rather than inline in the import templates below because\n * `scripts/headless-guard/check-types.mjs` scans core sources for `from\n * \"@rebasepro/cms-types\"` and cannot tell a real import from one this module\n * *writes*. It is right to be that blunt — the guard's whole value is that it\n * cannot be reasoned around — so the string simply never appears in that shape\n * here. Inlining them back into the templates re-breaks `check:types-headless`.\n */\nexport const ADMIN_TYPES_PACKAGE = \"@rebasepro/cms-types\";\nexport const COMMON_PACKAGE = \"@rebasepro/common\";\nexport const TYPES_PACKAGE = \"@rebasepro/types\";\n\nexport interface GenerationContext {\n metadata?: SchemaMetadata;\n classifications?: Map<string, TableClassification>;\n checkFacts?: CheckFactsByTable;\n /**\n * Defaults to `admin-types`, which is what the generator has always emitted.\n * The CLI never relies on the default — `introspect-db.ts` detects the flavour\n * from the target project and passes it. See `detectCollectionBuilder`.\n */\n builder?: CollectionBuilder;\n}\n\n/** Adds entries to a property's `validation` block, creating it if absent. */\nfunction withValidation(extra: string, entries: string[]): string {\n if (entries.length === 0) return extra;\n const block = entries.map((e) => ` ${e}`).join(\",\\n\");\n if (extra.includes(\"validation: {\")) {\n return extra.replace(\"validation: {\", `validation: {\\n${block},`);\n }\n return `${extra}\\n validation: {\\n${block}\\n },`;\n}\n\n/**\n * Whether a property's generated text already sets `key:` as an object key.\n *\n * A bare `extra.includes(\"min:\")` looks like it answers this and does not:\n * `admin:` ends in `min:`, so every property with an admin block claimed to\n * have a minimum already and silently lost the one the database declared. The\n * leading-delimiter requirement is the whole point — a key is preceded by a\n * newline, a brace or a comma, never by another identifier character.\n */\nfunction hasGeneratedKey(extra: string, key: string): boolean {\n return new RegExp(`(^|[\\\\s{,])${key}\\\\s*:`).test(extra);\n}\n\n/**\n * Adds entries to a property's `admin` block, creating it if absent.\n *\n * `emitAdmin` is false for the headless flavours, where `BaseProperty` has no\n * `admin` field to put them in — see {@link CollectionBuilder}. The options are\n * dropped rather than relocated: every one of them (`readOnly`, `multiline`,\n * `hideFromCollection`, `urlPreview`) describes a form widget, and there is no\n * form.\n */\nfunction withAdminOptions(extra: string, entries: string[], emitAdmin = true): string {\n if (!emitAdmin) return extra;\n if (entries.length === 0) return extra;\n const block = entries.map((e) => ` ${e}`).join(\",\\n\");\n if (extra.includes(\"admin: {\")) {\n return extra.replace(\"admin: {\", `admin: {\\n${block},`);\n }\n return `${extra}\\n admin: {\\n${block}\\n },`;\n}\n\n/** A TypeScript string literal, escaped. */\nfunction quote(value: string): string {\n return JSON.stringify(value);\n}\n\nconst JS_IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;\n\n/**\n * An object key for the generated file: verbatim when the name is a JavaScript\n * identifier, quoted otherwise.\n *\n * Postgres constrains an identifier only by quoting, so `order`, `full name`\n * and `2fa_enabled` are all ordinary column names — and all three produced a\n * file that did not parse when written as a bare key.\n *\n * Still needed now that keys are camel-cased rather than copied from the\n * column: `toWireKey` splits on separators and joins, which fixes `full name`\n * but cannot fix a name that is not an identifier for some other reason —\n * `2fa_enabled` becomes `2faEnabled`, still leading with a digit, and `order`\n * was never a separator problem at all.\n */\nfunction propKey(name: string): string {\n return JS_IDENTIFIER.test(name) ? name : quote(name);\n}\n\n/**\n * Text safe to put after `//`.\n *\n * A table comment or a classification reason is free text out of the database.\n * A newline in one ended the comment and let the rest of the value continue as\n * code.\n */\nfunction commentText(value: string): string {\n return value.replace(/\\s*[\\r\\n]+\\s*/g, \" \");\n}\n\n/** A property-key array, one key per line, indented for the admin block. */\nfunction formatKeyList(keys: string[]): string {\n if (keys.length === 0) return \"[]\";\n return `[\\n${keys.map((k) => ` ${quote(k)}`).join(\",\\n\")}\\n ]`;\n}\n\n/**\n * Generate the full TypeScript file content for a single collection.\n * Pure function — no I/O.\n */\nexport function generateCollectionFile(\n tableName: string,\n meta: TableMeta,\n allFks: ForeignKeyRow[],\n joinTables: Set<string>,\n tablesMap: Map<string, TableMeta>,\n enumMap: Map<string, string[]>,\n sampleData?: Record<string, unknown>[],\n context: GenerationContext = {}\n): string {\n const collectionName = humanize(tableName);\n const singular = singularize(collectionName);\n const icon = getIconForTable(tableName);\n\n const classification = context.classifications?.get(tableName);\n const checkFacts: CheckFactsByTable = context.checkFacts ?? new Map();\n const tableChecks = checkFacts.get(tableName);\n const columnComments = new Map<string, string>();\n let tableComment: string | undefined;\n for (const comment of context.metadata?.comments ?? []) {\n if (comment.table_name !== tableName) continue;\n if (comment.column_name === null) tableComment = comment.comment;\n else columnComments.set(comment.column_name, comment.comment);\n }\n const singleColumnUniques = new Set(\n (context.metadata?.uniques ?? [])\n .filter((u) => u.table_name === tableName && u.column_names.length === 1)\n .map((u) => u.column_names[0])\n );\n\n const builder: CollectionBuilder = context.builder ?? \"admin-types\";\n /** Whether the target project has an `admin` field to write into at all. */\n const emitAdmin = builder === \"admin-types\";\n\n const BUILDER_IMPORT: Record<CollectionBuilder, string> = {\n \"admin-types\": `import { defineCollection } from ${quote(ADMIN_TYPES_PACKAGE)};`,\n common: `import { defineCollection } from ${quote(COMMON_PACKAGE)};`,\n annotation: `import { PostgresCollectionConfig } from ${quote(TYPES_PACKAGE)};`\n };\n const imports = new Set<string>([BUILDER_IMPORT[builder]]);\n\n /**\n * Imports the collection a relation points at — unless it is this one.\n *\n * A self-referencing key (`employees.reports_to -> employees`, which both\n * northwind and chinook have) otherwise made the file import its own default\n * export under the name it declares three lines later: `TS2440: Import\n * declaration conflicts with local declaration`. The relation target is a\n * thunk, so referring to the local const directly is fine — it is only\n * dereferenced after the module has finished evaluating.\n */\n const importCollection = (otherTable: string): string => {\n const varName = toCollectionVarName(otherTable);\n if (otherTable !== tableName) imports.add(`import ${varName} from ${quote(`./${otherTable}`)};`);\n return varName;\n };\n\n /**\n * A relation's `target` thunk, with its return type spelled out.\n *\n * The annotation is what makes `defineCollection` survive a relational schema.\n * Without an explicit type on the const, the collection's type is *inferred*,\n * and a relation cycle — `posts` belongs to `authors`, `authors` has many\n * `posts`; or `employees.reports_to -> employees`, which northwind, chinook and\n * musicbrainz all have — makes that inference circular: `TS7022: implicitly has\n * type 'any' because it is referenced directly or indirectly in its own\n * initializer`, plus `TS7023` on the thunk and `TS2303` on the import alias.\n * Naming the return type lets the checker type the thunk without resolving the\n * collection it points at, which breaks the cycle. Nothing else is given up:\n * the inference that matters runs over `properties`, not over `relations`.\n *\n * The annotated flavour has no cycle to break — its const is already typed — so\n * it keeps the plainer thunk it has always emitted.\n */\n const relationTarget = (targetVarName: string): string => {\n if (builder === \"annotation\") return `() => ${targetVarName}`;\n imports.add(`import type { AnyCollectionConfig } from ${quote(TYPES_PACKAGE)};`);\n return `(): AnyCollectionConfig => ${targetVarName}`;\n };\n\n let propsOutput = \"\";\n let relationsOutput = \"\";\n const orderEntries: PropertyOrderEntry[] = [];\n const propertyBlocks = new Map<string, string>();\n /**\n * Column → the property key it was generated under.\n *\n * Needed because the structural helpers below (`deriveTitleProperty`,\n * `deriveKanbanProperty`, `deriveSort`) answer in *columns* — they read\n * `pg_attribute` — while `display.title`, `kanban.columnProperty` and\n * `sort` name **properties**. The two used to be the same string, so\n * nothing carried the translation; now `full_name` is generated as\n * `fullName` and a title pointing at `full_name` points at nothing.\n */\n const keyByColumn = new Map<string, string>();\n /** Properties the list view will not render, so `listProperties` skips them. */\n const hiddenFromCollection = new Set<string>();\n let columnIndex = 0;\n\n // Detect composite primary keys\n const isCompositePk = meta.pks.length > 1;\n\n // Map columns\n for (const col of meta.columns) {\n // Skip foreign keys since we handle them as relations\n // Exception: Do not skip if it's part of the primary key!\n if (meta.fks.some((fk) => fk.column_name === col.column_name) && !meta.pks.includes(col.column_name)) continue;\n\n const currentIndex = columnIndex++;\n\n // The key this column is generated under — its *wire* name, which is\n // not its column name. `columnName` below carries the column, so the\n // two never have to agree and the API stops carrying `user_id` next to\n // `displayName`.\n //\n // Camel-casing makes collisions possible where none existed: `user_id`\n // and `userId` are two columns and one key, which is a duplicate key in\n // an object literal — a TypeScript error that stops the whole generated\n // collection compiling. Resolved the same way the foreign-key loop\n // below resolves its own: first free candidate, then a numbered tail,\n // so no column is ever dropped. The raw column name is the second\n // candidate, so the loser of a collision still gets a name that means\n // something. Deterministic for a given database: the columns arrive in\n // ordinal order, so the same schema always yields the same keys.\n const propertyKey = firstFreeKey([toWireKey(col.column_name), col.column_name], propertyBlocks);\n keyByColumn.set(col.column_name, propertyKey);\n\n // Check if this column uses a PostgreSQL enum type\n const colEnumValues = enumMap.get(col.udt_name);\n const isEnumColumn = col.data_type === \"USER-DEFINED\" && colEnumValues !== undefined;\n const isVectorColumn = col.udt_name === \"vector\";\n\n const propType = isEnumColumn ? \"string\" : (isVectorColumn ? \"vector\" : mapPgType(col.data_type));\n let extra = \"\";\n\n const colNameLower = col.column_name.toLowerCase();\n\n // ── Data Inference Engine ────────────────────────────────────────────\n let finalPropType = propType;\n let inferenceExtra = \"\";\n\n if (!isEnumColumn && sampleData && sampleData.length > 0) {\n const values = sampleData.map(r => r[col.column_name]);\n const inferred = inferPropertyFromData(col.column_name, col.data_type, propType, values, meta.pks.includes(col.column_name), emitAdmin);\n if (inferred.propType) finalPropType = inferred.propType;\n if (inferred.extra) inferenceExtra = inferred.extra;\n }\n\n const columnChecks = tableChecks?.get(col.column_name);\n\n // Enum values — generate real enum from the PG enum\n if (isEnumColumn && colEnumValues) {\n const enumEntries = colEnumValues\n .map((v) => `{ id: ${quote(v)}, label: ${quote(humanize(v))} }`)\n .join(\", \");\n extra += `\\n enum: [${enumEntries}],`;\n } else if (columnChecks?.enumValues && !inferenceExtra.includes(\"enum:\") && propType === \"string\") {\n // `CHECK (col IN (…))` is the other way a schema declares a closed\n // set. It is the same statement as a Postgres enum type, made by an\n // author who did not want a type — and until now the form offered a\n // free-text box for it and let the database reject the write.\n const enumEntries = columnChecks.enumValues\n .map((v) => `{ id: ${quote(v)}, label: ${quote(humanize(v))} }`)\n .join(\", \");\n extra += `\\n enum: [${enumEntries}],`;\n }\n\n // Date auto-value heuristics\n if (finalPropType === \"date\") {\n if (colNameLower === \"created_at\" || colNameLower === \"createdat\") {\n extra += \"\\n autoValue: \\\"on_create\\\",\";\n extra = withAdminOptions(extra, [\"readOnly: true\", \"hideFromCollection: true\"], emitAdmin);\n hiddenFromCollection.add(propertyKey);\n } else if (colNameLower === \"updated_at\" || colNameLower === \"updatedat\") {\n extra += \"\\n autoValue: \\\"on_update\\\",\";\n extra = withAdminOptions(extra, [\"readOnly: true\", \"hideFromCollection: true\"], emitAdmin);\n hiddenFromCollection.add(propertyKey);\n } else if (col.column_default && (col.column_default.includes(\"now()\") || col.column_default.includes(\"CURRENT_TIMESTAMP\"))) {\n extra += \"\\n autoValue: \\\"on_create\\\",\";\n extra = withAdminOptions(extra, [\"readOnly: true\"], emitAdmin);\n }\n }\n\n // Array/Map heuristics (Fallback if not inferred)\n if (finalPropType === \"array\" && !inferenceExtra.includes(\"of: {\")) {\n let innerType = \"string\";\n let colType = \"\";\n if (col.udt_name.startsWith(\"_\")) {\n const baseType = col.udt_name.substring(1);\n innerType = mapPgType(baseType);\n if (innerType === \"string\") colType = \"text[]\";\n else if (innerType === \"number\") colType = col.udt_name === \"_numeric\" ? \"numeric[]\" : \"integer[]\";\n else if (innerType === \"boolean\") colType = \"boolean[]\";\n }\n if (colType) {\n extra += `\\n columnType: ${quote(colType)},`;\n }\n extra += `\\n of: { name: ${quote(`${humanize(col.column_name)} Item`)}, type: ${quote(innerType)} },`;\n } else if (finalPropType === \"map\" && !inferenceExtra.includes(\"keyValue: true\") && !inferenceExtra.includes(\"properties: {\")) {\n extra += \"\\n keyValue: true,\";\n }\n\n // String sub-type heuristics (Fallback if not handled by inference or enum)\n if (finalPropType === \"string\" && !isEnumColumn && !inferenceExtra) {\n const isUrl = colNameLower.endsWith(\"_url\") || colNameLower.endsWith(\"_uri\") || colNameLower.endsWith(\"_link\");\n const isMedia = colNameLower.includes(\"image\") || colNameLower.includes(\"avatar\") || colNameLower.includes(\"photo\") || colNameLower.includes(\"logo\") || colNameLower.includes(\"cover\");\n\n if (isMedia) {\n extra += `\\n storage: {\\n storagePath: ${quote(`${tableName}/${col.column_name}`)}\\n },`;\n } else if (isUrl) {\n extra += \"\\n url: true,\";\n } else if (colNameLower === \"description\" || colNameLower === \"summary\" || colNameLower === \"excerpt\") {\n extra = withAdminOptions(extra, [\"multiline: true\"], emitAdmin);\n } else if (colNameLower === \"content\" || colNameLower === \"body\") {\n // Inside `admin`, because that is where both options live. At the\n // top of the property — where these were — the generated file\n // does not compile: `StringProperty` declares neither. Six of\n // OpenStreetMap's tables have a `body` column, which is how this\n // surfaced.\n extra = withAdminOptions(extra, [\"multiline: true\", \"markdown: true\"], emitAdmin);\n } else if (col.data_type === \"text\") {\n extra = withAdminOptions(extra, [\"multiline: true\"], emitAdmin);\n }\n }\n\n // Append inference results\n if (inferenceExtra) {\n extra += inferenceExtra;\n if (!extra.endsWith(\",\")) extra += \",\";\n }\n\n // ── Rules the database already enforces ──────────────────────────────\n // Everything below is read from the catalog, not guessed from the data\n // or the column's name. Each one is a constraint a write would hit\n // anyway; surfacing it means the form says no before the database does.\n const declaredValidation: string[] = [];\n\n // `varchar(n)` — a bound the author wrote down and nothing has read.\n if (finalPropType === \"string\" &&\n typeof col.character_maximum_length === \"number\" &&\n col.character_maximum_length > 0 &&\n !hasGeneratedKey(extra, \"max\")) {\n declaredValidation.push(`max: ${col.character_maximum_length}`);\n }\n\n if (columnChecks) {\n if (finalPropType === \"number\") {\n if (columnChecks.min !== undefined && !hasGeneratedKey(extra, \"min\")) declaredValidation.push(`min: ${columnChecks.min}`);\n if (columnChecks.max !== undefined && !hasGeneratedKey(extra, \"max\")) declaredValidation.push(`max: ${columnChecks.max}`);\n if (columnChecks.moreThan !== undefined) declaredValidation.push(`moreThan: ${columnChecks.moreThan}`);\n if (columnChecks.lessThan !== undefined) declaredValidation.push(`lessThan: ${columnChecks.lessThan}`);\n }\n if (finalPropType === \"string\") {\n if (columnChecks.lengthMin !== undefined && !hasGeneratedKey(extra, \"min\")) declaredValidation.push(`min: ${columnChecks.lengthMin}`);\n if (columnChecks.lengthMax !== undefined && !declaredValidation.some((v) => v.startsWith(\"max:\")) && !hasGeneratedKey(extra, \"max\")) {\n declaredValidation.push(`max: ${columnChecks.lengthMax}`);\n }\n }\n }\n\n // A single-column unique index is the same promise `validation.unique`\n // makes. Composite uniqueness is not: it constrains the combination, and\n // marking either column unique on its own would reject valid rows.\n if (singleColumnUniques.has(col.column_name) && !meta.pks.includes(col.column_name)) {\n declaredValidation.push(\"unique: true\");\n }\n\n extra = withValidation(extra, declaredValidation);\n\n // A generated column rejects every write, and a tsvector holds lexeme\n // positions rather than text, so an editable field for either is a field\n // that can only ever produce an error.\n if (isReadOnlyColumn(col) && !hasGeneratedKey(extra, \"readOnly\")) {\n const options = [\"readOnly: true\"];\n if (isDerivedIndexColumn(col) && !hasGeneratedKey(extra, \"hideFromCollection\")) {\n options.push(\"hideFromCollection: true\");\n hiddenFromCollection.add(propertyKey);\n }\n extra = withAdminOptions(extra, options, emitAdmin);\n }\n\n // `COMMENT ON COLUMN` — documentation the author already wrote, which\n // introspection has never carried across.\n const columnComment = columnComments.get(col.column_name);\n if (columnComment) {\n extra = `\\n description: ${quote(columnComment)},${extra}`;\n }\n\n // Identify IDs (unless already inferred as UUID/CUID by inferenceEngine)\n if (meta.pks.includes(col.column_name)) {\n if (isCompositePk) {\n extra += `\\n // Part of composite primary key (${commentText(meta.pks.join(\", \"))})`;\n } else if (finalPropType === \"number\" && !inferenceExtra.includes(\"isId:\")) {\n extra += \"\\n isId: \\\"increment\\\",\";\n } else if (col.data_type.toLowerCase() === \"uuid\" && !inferenceExtra.includes(\"isId:\")) {\n extra += \"\\n isId: \\\"uuid\\\",\";\n } else if (!inferenceExtra.includes(\"isId:\")) {\n extra += \"\\n isId: \\\"uuid\\\", // Verify if this is a UUID or CUID\";\n }\n }\n\n if (finalPropType === \"vector\") {\n const dims = col.atttypmod && col.atttypmod > 0 ? col.atttypmod : 1536;\n extra += `\\n dimensions: ${dims},`;\n }\n\n // `required` on a column the user cannot write is a form that cannot be\n // submitted: pagila's `film.fulltext` is NOT NULL and maintained by a\n // trigger, so demanding it of the user blocks every create.\n if (col.is_nullable === \"NO\" && !meta.pks.includes(col.column_name) && !col.column_default && !isReadOnlyColumn(col)) {\n if (extra.includes(\"validation: {\")) {\n extra = extra.replace(\"validation: {\", \"validation: {\\n required: true,\");\n } else {\n extra += \"\\n validation: {\\n required: true\\n },\";\n }\n }\n\n const humanName = humanize(col.column_name);\n\n orderEntries.push({\n key: propertyKey,\n ctx: {\n propType: finalPropType,\n isPk: meta.pks.includes(col.column_name),\n isEnum: isEnumColumn,\n isStorage: extra.includes(\"storage: {\") || inferenceExtra.includes(\"storage: {\"),\n pgDataType: col.data_type,\n originalIndex: currentIndex\n }\n });\n\n propertyBlocks.set(propertyKey, `\n ${propKey(propertyKey)}: {\n name: ${quote(humanName)},\n columnName: ${quote(col.column_name)},\n type: ${quote(finalPropType)},${extra}\n },`);\n }\n\n // Map Owning Relations (from this table's FKs to other tables)\n for (const fk of meta.fks) {\n const targetTableName = fk.foreign_table_name;\n if (!joinTables.has(targetTableName)) {\n // The relation gets its own property key, and it must not be one this\n // file has already used — a duplicate key in an object literal is a\n // TypeScript error, so the whole collection stops compiling.\n //\n // The collision needs three things at once and is invisible without\n // all three: a foreign key column that does *not* end in `_id`, that\n // column also being part of the primary key (which is what keeps it\n // as a property of its own rather than folding it into the relation),\n // and the stripped name matching the target table. MusicBrainz names\n // every foreign key after the table it points at — `area_tag (area,\n // tag)` — so 67 of its 339 collections came out with a property\n // declared twice.\n //\n // Camel-cased for the same reason the columns above are: this is a\n // property key, it sits in the same object literal, and a\n // `blog_author` beside a `publishedAt` is the two-conventions\n // defect reproduced inside a single collection.\n const stripped = toWireKey(fk.column_name.replace(/_id$/, \"\"));\n const relName = firstFreeKey(\n [\n stripped,\n toWireKey(targetTableName),\n `${stripped}Relation`\n ],\n propertyBlocks\n );\n // Push the relation property key, not the FK column name\n orderEntries.push({\n key: relName,\n ctx: {\n propType: \"relation\",\n isPk: false,\n isEnum: false,\n isStorage: false,\n pgDataType: \"\",\n originalIndex: columnIndex++\n }\n });\n\n const targetCollectionCamel = importCollection(targetTableName);\n\n const relHumanName = humanize(relName);\n\n propertyBlocks.set(relName, `\n ${propKey(relName)}: {\n name: ${quote(relHumanName)},\n type: \"relation\",\n // mapped from foreign key: ${commentText(fk.column_name)} -> ${commentText(targetTableName)}(${commentText(fk.foreign_column_name)})\n relation: {\n kind: \"belongsTo\",\n target: ${relationTarget(targetCollectionCamel)},\n localKey: ${quote(fk.column_name)}\n }\n },`);\n }\n }\n\n // Map Inverse Relations (1-to-many where OTHER table points to THIS table)\n // These go into the `relations` array so they render as subcollection tabs.\n const inverseFks = allFks.filter((fk) => fk.foreign_table_name === tableName && !joinTables.has(fk.table_name));\n for (const fk of inverseFks) {\n const sourceTableName = fk.table_name;\n\n const targetCollectionCamel = importCollection(sourceTableName);\n\n relationsOutput += `\n {\n kind: \"hasMany\",\n relationName: ${quote(sourceTableName)},\n target: ${relationTarget(targetCollectionCamel)},\n // the ${commentText(sourceTableName)}.${commentText(fk.column_name)} FK points back here\n foreignKeyOnTarget: ${quote(fk.column_name)}\n },`;\n }\n\n // Map Many-to-Many Relations (Join Tables)\n // These also go into the `relations` array so they render as subcollection tabs.\n const relatedJoinTables = Array.from(joinTables).filter((jt) => {\n const jtMeta = tablesMap.get(jt);\n return jtMeta ? jtMeta.fks.some((fk) => fk.foreign_table_name === tableName) : false;\n });\n\n for (const jt of relatedJoinTables) {\n const jtMeta = tablesMap.get(jt);\n if (!jtMeta) continue;\n\n const joinFks = jtMeta.fks;\n\n // Handle self-referencing M2M: both FKs point to the same table\n const selfRefFks = joinFks.filter((fk) => fk.foreign_table_name === tableName);\n if (selfRefFks.length === 2) {\n // Self-referencing M2M — generate a single owning relation\n const thisFk = selfRefFks[0];\n const otherFk = selfRefFks[1];\n\n const relPropName = `${tableName}_via_${otherFk.column_name.replace(/_id$/, \"\")}`;\n\n relationsOutput += `\n {\n kind: \"manyToMany\",\n relationName: ${quote(relPropName)},\n target: ${relationTarget(toCollectionVarName(tableName))},\n through: {\n table: ${quote(jt)},\n sourceColumn: ${quote(thisFk.column_name)},\n targetColumn: ${quote(otherFk.column_name)}\n }\n },`;\n continue;\n }\n\n const otherFk = joinFks.find((fk) => fk.foreign_table_name !== tableName);\n\n if (otherFk) {\n const targetTableName = otherFk.foreign_table_name;\n\n const targetCollectionCamel = importCollection(targetTableName);\n\n // Both sides of a many-to-many are `manyToMany`. There is no owning\n // and inverse side to pick between any more, so this no longer\n // guesses one from table-name ordering and no longer emits a\n // half-configured relation on the losing side with a comment asking\n // the reader to finish it by hand. Introspection already knows both\n // junction columns; each side just names them from its own end.\n const thisFk = joinFks.find((fk) => fk.foreign_table_name === tableName);\n\n const throughCode = thisFk\n ? `\\n through: {\\n table: ${quote(jt)},\\n sourceColumn: ${quote(thisFk.column_name)},\\n targetColumn: ${quote(otherFk.column_name)}\\n }`\n : \"\";\n\n relationsOutput += `\n {\n kind: \"manyToMany\",\n relationName: ${quote(targetTableName)},\n target: ${relationTarget(targetCollectionCamel)},${throughCode}\n },`;\n }\n }\n\n const relationsBlock = relationsOutput\n ? `\\n relations: [${relationsOutput}\\n ],`\n : \"\";\n\n const sortedPropertiesOrder = sortPropertiesOrder(orderEntries);\n for (const key of sortedPropertiesOrder) {\n propsOutput += propertyBlocks.get(key) || \"\";\n }\n\n // ── The admin block ──────────────────────────────────────────────────\n // `icon` and `propertiesOrder` used to be emitted at the *top level* of the\n // config, where they have not belonged since the admin block was split out:\n // `PostgresCollectionConfig` does not declare them, so every generated file\n // was a type error, and the panel — which reads the block — never saw them.\n const adminEntries: string[] = [`icon: ${quote(icon)}`];\n\n if (classification) {\n const derivedFacts = context.metadata\n ? buildColumnFacts(meta, context.metadata, enumMap, checkFacts)\n : undefined;\n\n if (classification.role === \"owned-child\") {\n // The rows are already reachable: every inbound foreign key renders\n // as a tab on the parent. A second, top-level entry for them is what\n // turns a navigation of eight nouns into a list of thirty tables.\n adminEntries.push(\"hideFromNavigation: true\");\n } else if (classification.role === \"lookup\") {\n adminEntries.push('group: \"Reference\"');\n }\n\n if (derivedFacts) {\n // Each of these comes back as a column and is emitted as a property.\n const asProperty = (column: string): string => keyByColumn.get(column) ?? toWireKey(column);\n\n const titleProperty = deriveTitleProperty(derivedFacts);\n if (titleProperty) adminEntries.push(`display: { title: ${quote(asProperty(titleProperty))} }`);\n\n const kanbanProperty = deriveKanbanProperty(derivedFacts);\n if (kanbanProperty) adminEntries.push(`kanban: {\\n columnProperty: ${quote(asProperty(kanbanProperty))}\\n }`);\n\n const sort = deriveSort(derivedFacts);\n if (sort) adminEntries.push(`sort: [${quote(asProperty(sort[0]))}, \"desc\"]`);\n }\n\n const listProperties = deriveListProperties(sortedPropertiesOrder, hiddenFromCollection);\n if (listProperties) {\n adminEntries.push(`listProperties: ${formatKeyList(listProperties)}`);\n }\n }\n\n adminEntries.push(`propertiesOrder: ${formatKeyList(sortedPropertiesOrder)}`);\n\n // Every entry above names a property key, and with `defineCollection` those\n // keys are now checked against `properties` — a stale `propertiesOrder` entry\n // left behind by a renamed column is a compile error rather than a silent\n // no-op. Which is also why the block cannot be emitted where the field is not\n // declared: see {@link CollectionBuilder}.\n const adminBlock = emitAdmin\n ? `\\n admin: {\\n ${adminEntries.join(\",\\n \")}\\n }`\n : \"\";\n\n const descriptionBlock = tableComment\n ? `\\n description: ${quote(tableComment)},`\n : \"\";\n\n // The classification is stated in the file because it is a *decision*, and a\n // decision the reader may disagree with. Naming the evidence tells them\n // which line to delete when they do.\n const classificationNote = classification && classification.role !== \"entity\"\n ? `\\n// Introspected as a ${commentText(classification.role)}: ${commentText(classification.reason)}.\\n`\n : \"\";\n\n const collectionVarName = toCollectionVarName(tableName);\n // `const x = defineCollection({ … })` is also the shape the ts-morph schema\n // editor in `@rebasepro/server` expects — `COLLECTION_FACTORIES` — so an\n // introspected collection is now editable from the panel the way a scaffolded\n // one is.\n const [open, close] = builder === \"annotation\"\n ? [`const ${collectionVarName}: PostgresCollectionConfig = {`, \"};\"]\n : [`const ${collectionVarName} = defineCollection({`, \"});\"];\n // Package imports first, then siblings. `AnyCollectionConfig` is added the\n // moment the first relation needs it — which is after the sibling collections\n // it points at have already been added — and only when a relation needs it, so\n // a project with `noUnusedLocals` never sees an import it does not use.\n const importLines = Array.from(imports);\n const orderedImports = [\n ...importLines.filter((line) => !line.includes('from \"./')),\n ...importLines.filter((line) => line.includes('from \"./'))\n ];\n const fileContent = `${orderedImports.join(\"\\n\")}\n${classificationNote}\n${open}\n name: ${quote(collectionName)},\n singularName: ${quote(singular)},\n slug: ${quote(tableName)},\n table: ${quote(tableName)},${descriptionBlock}\n properties: {${propsOutput}\n },${relationsBlock}${adminBlock}\n${close}\n\nexport default ${collectionVarName};\n`;\n\n return fileContent;\n}\n\n/**\n * Generate the content for an index.ts file that re-exports all collections.\n */\nexport function generateIndexContent(fileNames: string[]): string {\n const sorted = [...fileNames].sort();\n let imports = \"\";\n let arrayElements = \"\";\n for (const f of sorted) {\n const varName = toCollectionVarName(f);\n imports += `import ${varName} from ${quote(`./${f}`)};\\n`;\n arrayElements += ` ${varName},\\n`;\n }\n return `${imports}\\nexport const collections = [\\n${arrayElements}];\\n`;\n}\n\n/**\n * Merge new exports into existing index.ts content.\n * Returns the merged content string.\n */\nexport function mergeIndexContent(existingContent: string, newFileNames: string[]): string {\n const existingImports = new Set(\n [...existingContent.matchAll(/import\\s+([a-zA-Z0-9_]+)\\s+from\\s+\"\\.\\/([^\"]+)\"/g)].map((m) => m[2])\n );\n const sorted = [...newFileNames].sort();\n\n let newImports = \"\";\n let newElements = \"\";\n\n for (const f of sorted) {\n if (!existingImports.has(f)) {\n const varName = toCollectionVarName(f);\n newImports += `import ${varName} from ${quote(`./${f}`)};\\n`;\n newElements += ` ${varName},\\n`;\n }\n }\n\n if (!newImports) return existingContent;\n\n // Simple injection logic:\n // Add new imports below the last import or at the top\n const importRegex = /import\\s+.*?;/g;\n let lastImportMatch;\n let match;\n while ((match = importRegex.exec(existingContent)) !== null) {\n lastImportMatch = match;\n }\n\n let contentWithImports = existingContent;\n if (lastImportMatch) {\n const pos = lastImportMatch.index + lastImportMatch[0].length;\n contentWithImports = existingContent.slice(0, pos) + \"\\n\" + newImports.trimEnd() + existingContent.slice(pos);\n } else {\n contentWithImports = newImports + \"\\n\" + existingContent;\n }\n\n // Inject into the `collections = [...]` array\n const arrayRegex = /export\\s+const\\s+collections\\s*=\\s*\\[([\\s\\S]*?)\\];/;\n return contentWithImports.replace(arrayRegex, (fullMatch, arrayContent) => {\n let mergedArray = arrayContent.trimEnd();\n if (mergedArray && !mergedArray.endsWith(\",\")) mergedArray += \",\";\n if (mergedArray) mergedArray += \"\\n\";\n mergedArray += newElements.trimEnd();\n return `export const collections = [\\n ${mergedArray.trim()}\\n];`;\n });\n}\n\n/**\n * Safely extract the host portion of a database URL for logging.\n */\nexport function safeHostFromUrl(url: string): string {\n return url.includes(\"@\") ? url.split(\"@\")[1] : \"(local connection)\";\n}\n"],"mappings":";;;;;;;;;;;;;AAUA,SAAgB,SAAS,WAA2B;CAChD,OAAO,UACF,QAAQ,MAAM,GAAG,CAAC,CAClB,QAAQ,UAAU,MAAM,EAAE,YAAY,CAAC;AAChD;;;ACPA,IAAM,iBAAiB;AACvB,IAAM,aAAa;AACnB,IAAM,aAAa;AAGnB,SAAgB,sBACZ,YACA,YACA,iBACA,cACA,MAOA,YAAY,MACG;CACf,MAAM,SAA0B,CAAC;CACjC,MAAM,aAAuB,CAAC;CAI9B,MAAM,eAAyB,CAAC;CAGhC,MAAM,cAAc,aAAa,QAAO,MAAK,MAAM,QAAQ,MAAM,KAAA,KAAa,MAAM,EAAE;CAEtF,IAAI,YAAY,WAAW,GACvB,OAAO;CAGX,MAAM,eAAe,WAAW,YAAY;CAG5C,IAAI,oBAAoB,UAAU;EAG9B,IADqB,YAAY,OAAM,MAAK,MAAM,KAAK,MAAM,KAAK,MAAM,OAAO,MAAM,GACjF,GAAc;GACd,OAAO,WAAW;GAClB,OAAO;EACX;EAGA,MAAM,YAAY,YAAY,KAAI,MAAK,OAAO,CAAC,CAAC,CAAC,CAAC,QAAO,MAAK,CAAC,MAAM,CAAC,CAAC;EACvE,IAAI,UAAU,WAAW,YAAY,QAAQ;GACzC,MAAM,MAAM,KAAK,IAAI,GAAG,SAAS;GACjC,MAAM,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