@tailor-platform/sdk 2.34.0 → 2.35.0
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/CHANGELOG.md +26 -0
- package/dist/{application-CwpOIOTt.mjs → application-Bj2MtL-d.mjs} +1 -1
- package/dist/{application-Nce5z46R.mjs → application-DIsQSnj3.mjs} +25 -25
- package/dist/application-DIsQSnj3.mjs.map +1 -0
- package/dist/cli/commands/deploy/tailordb/migration-validation.d.mts +1 -2
- package/dist/cli/commands/tailordb/migrate/generate.d.mts +1 -1
- package/dist/cli/lib.mjs +1 -1
- package/dist/cli/lib.mjs.map +1 -1
- package/dist/cli/main.mjs +37 -37
- package/dist/cli/main.mjs.map +1 -1
- package/dist/cli/services/application.d.mts +1 -0
- package/dist/cli/services/function-bundle.d.mts +1 -0
- package/dist/cli/shared/sdk-builder-pure-plugin.d.mts +1 -0
- package/dist/completion/zsh-worker.zsh +4 -4
- package/dist/{kysely-type-DxyIWXc6.mjs → kysely-type-B0ri1-Xs.mjs} +9 -7
- package/dist/kysely-type-B0ri1-Xs.mjs.map +1 -0
- package/dist/plugin/builtin/kysely-type/index.mjs +1 -1
- package/dist/{register-ts-hook-CtWJL3EO.mjs → register-ts-hook-CQ7shKO5.mjs} +59 -58
- package/dist/register-ts-hook-CQ7shKO5.mjs.map +1 -0
- package/docs/cli/tailordb.md +8 -6
- package/docs/cli-reference.md +1 -1
- package/docs/configuration.md +2 -0
- package/docs/services/tailordb/index.md +4 -0
- package/docs/services/tailordb/migration.md +12 -7
- package/docs/testing.md +2 -2
- package/package.json +1 -1
- package/dist/application-Nce5z46R.mjs.map +0 -1
- package/dist/cli/commands/tailordb/migrate/remote-state.d.mts +0 -1
- package/dist/kysely-type-DxyIWXc6.mjs.map +0 -1
- package/dist/register-ts-hook-CtWJL3EO.mjs.map +0 -1
|
@@ -9,6 +9,7 @@ import "./auth/service.mjs";
|
|
|
9
9
|
import "./executor/service.mjs";
|
|
10
10
|
import "./http-adapter/service.mjs";
|
|
11
11
|
import "./resolver/service.mjs";
|
|
12
|
+
import "../shared/sdk-builder-pure-plugin.mjs";
|
|
12
13
|
import "../shared/start-context.mjs";
|
|
13
14
|
import "./workflow/bundler.mjs";
|
|
14
15
|
import "./workflow/service.mjs";
|
|
@@ -3,5 +3,6 @@ import "../shared/sourcemap-sources.mjs";
|
|
|
3
3
|
import "../cache/bundle-cache.mjs";
|
|
4
4
|
import "../shared/tsconfig-paths-plugin.mjs";
|
|
5
5
|
import "../shared/platform-bundle-plugin.mjs";
|
|
6
|
+
import "../shared/sdk-builder-pure-plugin.mjs";
|
|
6
7
|
import "../shared/start-context.mjs";
|
|
7
8
|
import "rolldown";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "rolldown";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# politty-completion-version: 1
|
|
2
|
-
# politty-bin-sig:
|
|
2
|
+
# politty-bin-sig: 1793059475
|
|
3
3
|
# politty-bin-path: /home/runner/work/sdk/sdk/node_modules/.bin/tailor
|
|
4
4
|
# program: tailor
|
|
5
5
|
# shell: zsh
|
|
@@ -3502,9 +3502,9 @@ __tailor_worker_complete_tailordb_migration_generate() {
|
|
|
3502
3502
|
__tailor_worker_not_used "--yes" "-y" "--y" && _opts+=("--yes:Skip confirmation prompts")
|
|
3503
3503
|
__tailor_worker_not_used "--config" "-c" "--c" && _opts+=("--config:Path to Tailor config file")
|
|
3504
3504
|
__tailor_worker_not_used "--name" "-n" "--n" && _opts+=("--name:Optional description for the migration")
|
|
3505
|
-
__tailor_worker_not_used "--init" && _opts+=("--init:Delete existing migrations and start fresh")
|
|
3505
|
+
__tailor_worker_not_used "--init" && _opts+=("--init:Delete existing migrations and start fresh (all namespaces with migrations configured, or only the one given by --namespace)")
|
|
3506
3506
|
__tailor_worker_not_used "--data-only" "--dataOnly" && _opts+=("--data-only:Create a migration with no schema changes whose migration script runs a standalone data transformation")
|
|
3507
|
-
__tailor_worker_not_used "--namespace" && _opts+=("--namespace:Target TailorDB namespace for --data-only (required if multiple namespaces exist)")
|
|
3507
|
+
__tailor_worker_not_used "--namespace" && _opts+=("--namespace:Target TailorDB namespace for --data-only (required if multiple namespaces exist), or the only namespace --init resets")
|
|
3508
3508
|
_opts+=("--rename:Record a field, table, or nested member rename instead of remove + add (format\: \"Table.oldField\:newField\", \"OldTable\:NewTable\", or \"Table.field.oldMember\:newMember\"; repeatable). Renames require a migration script that copies the data.")
|
|
3509
3509
|
_opts+=("--drop:Confirm that a removed field, table, or nested member is a genuine removal, not a rename (format\: \"Table.field\", \"Table\", or \"Table.field.member\"; repeatable). Required in non-interactive runs for a removal with rename candidates.")
|
|
3510
3510
|
_opts+=("--expand-contract:Convert a field type, or a single value into an array, through a temporary field (format\: \"Table.field\"; repeatable). Generates two migrations.")
|
|
@@ -3994,7 +3994,7 @@ __tailor_worker_complete_tailordb_migration() {
|
|
|
3994
3994
|
__tailor_worker_cdescribe 'options' _opts
|
|
3995
3995
|
return 0
|
|
3996
3996
|
fi
|
|
3997
|
-
local -a _subs=("generate:Generate migration files by detecting schema differences between current local tables and the previous migration snapshot." "rebaseline:Collapse the full migration history into a new 0000 baseline." "resolve:Settle a migration that a failed deploy left partially applied." "script:Add a migration script (migrate.ts) template to an existing migration directory, or record with --no-script that a migration intentionally has none." "set:Set migration checkpoint to a specific number." "status:Show the current migration status for TailorDB namespaces, including applied and pending migrations." "sync:Sync remote TailorDB schema to a specific migration snapshot (recovery from --no-schema-check drift)." "test:Test pending migrations with seed fixtures or cloned data in a temporary workspace." "validate:Validate the full migration history, unreviewed generated migration scripts, and schema drift (local tables vs. migration snapshot, remote schema vs. migration checkpoint) without deploying. This includes the migration and schema-drift checks used by 'deploy' and exits with a non-zero code when issues are found.")
|
|
3997
|
+
local -a _subs=("generate:Generate migration files by detecting schema differences between current local tables and the previous migration snapshot." "rebaseline:Collapse the full migration history into a new 0000 baseline." "resolve:Settle a migration that a failed deploy left partially applied, or whose run a deploy could not confirm." "script:Add a migration script (migrate.ts) template to an existing migration directory, or record with --no-script that a migration intentionally has none." "set:Set migration checkpoint to a specific number." "status:Show the current migration status for TailorDB namespaces, including applied and pending migrations." "sync:Sync remote TailorDB schema to a specific migration snapshot (recovery from --no-schema-check drift)." "test:Test pending migrations with seed fixtures or cloned data in a temporary workspace." "validate:Validate the full migration history, unreviewed generated migration scripts, and schema drift (local tables vs. migration snapshot, remote schema vs. migration checkpoint) without deploying. This includes the migration and schema-drift checks used by 'deploy' and exits with a non-zero code when issues are found.")
|
|
3998
3998
|
__tailor_worker_cdescribe 'subcommands' _subs
|
|
3999
3999
|
}
|
|
4000
4000
|
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import{t as e}from"./multiline-EyzjEwn9.mjs";import{resolve as t}from"pathe";const n=new TextEncoder;function mapFieldTypeToPostgresType(e){switch(e){case`uuid`:return`uuid`;case`string`:case`enum`:return`text`;case`boolean`:case`bool`:return`boolean`;case`integer`:return`integer`;case`float`:return`double precision`;case`decimal`:return`numeric`;case`date`:return`date`;case`datetime`:return`timestamptz`;case`time`:return`time`;case`nested`:return`jsonb`;default:throw Error(`Field type "${e}" has no PostgreSQL column type.`)}}function identifier(e){return`"${e.replaceAll(`"`,`""`)}"`}function fnv1aHex(e){let t=2166136261;for(let r of n.encode(e))t=Math.imul(t^r,16777619)>>>0;return t.toString(16).padStart(8,`0`)}function derivedName(e,t,r){let i=`_${fnv1aHex(JSON.stringify([e,t,r]))}_${r}`,a=`${e}_${t}`;for(;n.encode(a+i).length>63;)a=a.slice(0,-1);return a+i}function derivedIdentifier(e,t,n){return identifier(derivedName(e,t,n))}function dollarQuoted(e){let t=`$fk$`;for(;e.includes(t);)t=`${t.slice(0,-1)}_$`;return`${t}\n${e}\n${t}`}function stringLiteral(e){return`'${e.replaceAll(`'`,`''`)}'`}function columnType(e){let t=e.type===`decimal`?`numeric(1000, ${e.scale??6})`:mapFieldTypeToPostgresType(e.type);return e.array&&e.type!==`nested`?`${t}[]`:t}function scalarLiteral(e,t,n){let r=mapFieldTypeToPostgresType(t.type);if(e instanceof Date){if(Number.isNaN(e.getTime()))throw Error(`Default of field ${n} is an invalid Date.`);return`${stringLiteral(e.toISOString())}::${r}`}switch(typeof e){case`string`:return r===`text`?stringLiteral(e):`${stringLiteral(e)}::${r}`;case`number`:if(!Number.isFinite(e))throw Error(`Default of field ${n} is not a finite number.`);return String(e);case`boolean`:return e?`TRUE`:`FALSE`;default:throw Error(`Default of field ${n} cannot be rendered as a SQL literal.`)}}const r=/* @__PURE__ */ new Map([[`datetime`,`now()`],[`date`,`CURRENT_DATE`],[`time`,`LOCALTIME`]]);function defaultExpression(e,t){let n=e.default,i=r.get(e.type);if(n===`now`&&i!==void 0)return i;if(e.array){if(!Array.isArray(n))throw Error(`Default of array field ${t} must be an array.`);let r=columnType(e);return n.length===0?`'{}'::${r}`:`ARRAY[${n.map(n=>scalarLiteral(n,e,t)).join(`, `)}]::${r}`}return scalarLiteral(n,e,t)}function parseSerialFormat(e,t){let n=/^(?<prefix>[^%]*)%(?<zero>0?)(?<width>\d*)(?<conversion>[a-zA-Z])(?<suffix>[^%]*)$/.exec(e)?.groups,r=n?.conversion;if(!n||r!==`d`&&r!==`x`&&r!==`X`)throw Error(`Serial format "${e}" of field ${t} is not supported; use a single %d, %x, or %X specifier with an optional width (e.g. "INV-%05d").`);return{prefix:n.prefix??``,zeroPad:n.zero===`0`,width:n.width?Number(n.width):0,conversion:r,suffix:n.suffix??``}}function sequenceName(e,t){return derivedIdentifier(e,t,`seq`)}function sequenceRange(e){let t=[`START WITH ${e.start}`,`MINVALUE ${e.start}`];return e.maxValue!==void 0&&t.push(`MAXVALUE ${e.maxValue}`),t.join(` `)}const i={d:e=>e,x:e=>`to_hex(${e})`,X:e=>`upper(to_hex(${e}))`};function serialStringDefault(e,t,n){let r=`nextval(${stringLiteral(e)})`;if(t===void 0)return`(${r}::text)`;let{prefix:a,width:o,zeroPad:s,conversion:c,suffix:l}=parseSerialFormat(t,n),u=i[c](r),d=c===`d`?`${u}::text`:u;return o>0&&(d=`format('%${o}s', ${u})`,s&&(d=`translate(${d}, ' ', '0')`)),`(${[...a?[stringLiteral(a)]:[],d,...l?[stringLiteral(l)]:[]].join(` || `)})`}function columnDefinition(e,t,n){let r=identifier(t),i=`${identifier(e)}.${r}`,a=[r,columnType(n)];if(n.serial)return n.type===`integer`?(a.push(`GENERATED BY DEFAULT AS IDENTITY (${sequenceRange(n.serial)})`),n.unique&&a.push(`UNIQUE`),a.join(` `)):(n.required&&a.push(`NOT NULL`),n.unique&&a.push(`UNIQUE`),a.push(`DEFAULT ${serialStringDefault(sequenceName(e,t),n.serial.format,i)}`),a.join(` `));let o=n.default!==void 0,s=n.hooks?.create!==void 0||n.optionalOnCreate===!0;return n.required&&(o||!s)&&a.push(`NOT NULL`),n.unique&&a.push(`UNIQUE`),o&&a.push(`DEFAULT ${defaultExpression(n,i)}`),a.join(` `)}function jsonPathKey(e){return`"${e.replaceAll(`\\`,`\\\\`).replaceAll(`"`,`\\"`)}"`}function referencesWithin(e,t,n){let r=t.array?`${n}[*]`:n;return t.type===`nested`?Object.entries(t.fields??{}).flatMap(([t,n])=>referencesWithin(e,n,`${r}.${jsonPathKey(t)}`)):!t.foreignKey||t.foreignKeyType===void 0?[]:[{column:e,path:r,table:t.foreignKeyType,field:t.foreignKeyField??`id`,type:t.type}]}function tableReferences(e){return Object.entries(e.fields).filter(([e])=>e!==`id`).flatMap(([e,t])=>referencesWithin(e,t,`$`))}function isConstraintReference(e,t){if(e.path!==`$`)return!1;let n=e.field===`id`?`uuid`:t.get(e.table)?.fields[e.field]?.type;return n===void 0||mapFieldTypeToPostgresType(n)===mapFieldTypeToPostgresType(e.type)}function referenceClause(e){return`REFERENCES ${identifier(e.table)} (${identifier(e.field)})`}function declaredType(e){return e===`bool`?`boolean`:e}function checkReference(e,t){let n=t.get(e.table);if(!n)return{problem:`references unknown table "${e.table}".`};let r=e.field===`id`?{type:`uuid`,unique:!0}:n.fields[e.field];if(!r)return{problem:`references unknown field "${e.field}" of table "${e.table}".`};let i=e.field===`id`||declaredType(r.type)===declaredType(e.type);return!r.unique||!i?{problem:`references "${e.table}"."${e.field}", which must be unique and of the same type as the referencing field.`}:{pgType:mapFieldTypeToPostgresType(r.type)}}function referencedColumnType(e,t,n){let r=checkReference(e,t);if(`problem`in r)throw Error(`Field ${n} ${r.problem}`);return r.pgType}function withoutReference(e){let{foreignKey:t,foreignKeyType:n,foreignKeyField:r,...i}=e;return i}function withoutUnresolvableReference(e,t){return e.fields?{...e,fields:Object.fromEntries(Object.entries(e.fields).map(([e,n])=>[e,withoutUnresolvableReference(n,t)]))}:!e.foreignKey||e.foreignKeyType===void 0?e:`problem`in checkReference({column:``,path:`$`,table:e.foreignKeyType,field:e.foreignKeyField??`id`,type:e.type},t)?withoutReference(e):e}function dropUnresolvableReferences(e){let t=new Map(e.map(e=>[e.name,e]));return e.map(e=>({...e,fields:Object.fromEntries(Object.entries(e.fields).map(([e,n])=>[e,withoutUnresolvableReference(n,t)]))}))}function referenceCheck(e,t,n){let r=referencedColumnType(e,t,n),i=r===mapFieldTypeToPostgresType(e.type)?`parent.${identifier(e.field)} = (fk_check.v #>> '{}')::${r}`:`parent.${identifier(e.field)}::text = (fk_check.v #>> '{}')`,a=`violates foreign key constraints: % not found in ${`${identifier(e.table)}.${identifier(e.field)}`.replaceAll(`%`,`%%`)}`;return[` FOR v IN SELECT jsonb_path_query(to_jsonb(NEW.${identifier(e.column)}), ${stringLiteral(e.path)}) LOOP`,` IF jsonb_typeof(fk_check.v) <> 'null' AND NOT EXISTS (SELECT 1 FROM ${identifier(e.table)} AS parent WHERE ${i}) THEN`,` RAISE EXCEPTION ${stringLiteral(a)}, fk_check.v #>> '{}' USING ERRCODE = 'foreign_key_violation';`,` END IF;`,` END LOOP;`].join(`
|
|
1
|
+
import{t as e}from"./multiline-EyzjEwn9.mjs";import{resolve as t}from"pathe";const n=new TextEncoder;function mapFieldTypeToPostgresType(e){switch(e){case`uuid`:return`uuid`;case`string`:case`enum`:return`text`;case`boolean`:case`bool`:return`boolean`;case`integer`:return`integer`;case`float`:return`double precision`;case`decimal`:return`numeric`;case`date`:return`date`;case`datetime`:return`timestamptz`;case`time`:return`time`;case`nested`:return`jsonb`;default:throw Error(`Field type "${e}" has no PostgreSQL column type.`)}}function identifier(e){return`"${e.replaceAll(`"`,`""`)}"`}function fnv1aHex(e){let t=2166136261;for(let r of n.encode(e))t=Math.imul(t^r,16777619)>>>0;return t.toString(16).padStart(8,`0`)}function derivedName(e,t,r){let i=`_${fnv1aHex(JSON.stringify([e,t,r]))}_${r}`,a=`${e}_${t}`;for(;n.encode(a+i).length>63;)a=a.slice(0,-1);return a+i}function derivedIdentifier(e,t,n){return identifier(derivedName(e,t,n))}function dollarQuoted(e){let t=`$fk$`;for(;e.includes(t);)t=`${t.slice(0,-1)}_$`;return`${t}\n${e}\n${t}`}function stringLiteral(e){return`'${e.replaceAll(`'`,`''`)}'`}function columnType(e){let t=e.type===`decimal`?`numeric(1000, ${e.scale??6})`:mapFieldTypeToPostgresType(e.type);return e.array&&e.type!==`nested`?`${t}[]`:t}function scalarLiteral(e,t,n){let r=mapFieldTypeToPostgresType(t.type);if(e instanceof Date){if(Number.isNaN(e.getTime()))throw Error(`Default of field ${n} is an invalid Date.`);return`${stringLiteral(e.toISOString())}::${r}`}switch(typeof e){case`string`:return r===`text`?stringLiteral(e):`${stringLiteral(e)}::${r}`;case`number`:if(!Number.isFinite(e))throw Error(`Default of field ${n} is not a finite number.`);return String(e);case`boolean`:return e?`TRUE`:`FALSE`;default:throw Error(`Default of field ${n} cannot be rendered as a SQL literal.`)}}const r=/* @__PURE__ */ new Map([[`datetime`,`now()`],[`date`,`CURRENT_DATE`],[`time`,`LOCALTIME`]]);function defaultExpression(e,t){let n=e.default,i=r.get(e.type);if(n===`now`&&i!==void 0)return i;if(e.array){if(!Array.isArray(n))throw Error(`Default of array field ${t} must be an array.`);let r=columnType(e);return n.length===0?`'{}'::${r}`:`ARRAY[${n.map(n=>scalarLiteral(n,e,t)).join(`, `)}]::${r}`}return scalarLiteral(n,e,t)}function parseSerialFormat(e,t){let n=/^(?<prefix>[^%]*)%(?<zero>0?)(?<width>\d*)(?<conversion>[a-zA-Z])(?<suffix>[^%]*)$/.exec(e)?.groups,r=n?.conversion;if(!n||r!==`d`&&r!==`x`&&r!==`X`)throw Error(`Serial format "${e}" of field ${t} is not supported; use a single %d, %x, or %X specifier with an optional width (e.g. "INV-%05d").`);return{prefix:n.prefix??``,zeroPad:n.zero===`0`,width:n.width?Number(n.width):0,conversion:r,suffix:n.suffix??``}}function sequenceName(e,t){return derivedIdentifier(e,t,`seq`)}function sequenceRange(e){let t=[`START WITH ${e.start}`,`MINVALUE ${e.start}`];return e.maxValue!==void 0&&t.push(`MAXVALUE ${e.maxValue}`),t.join(` `)}const i={d:e=>e,x:e=>`to_hex(${e})`,X:e=>`upper(to_hex(${e}))`};function serialStringDefault(e,t,n){let r=`nextval(${stringLiteral(e)})`;if(t===void 0)return`(${r}::text)`;let{prefix:a,width:o,zeroPad:s,conversion:c,suffix:l}=parseSerialFormat(t,n),u=i[c](r),d=c===`d`?`${u}::text`:u;return o>0&&(d=`format('%${o}s', ${u})`,s&&(d=`translate(${d}, ' ', '0')`)),`(${[...a?[stringLiteral(a)]:[],d,...l?[stringLiteral(l)]:[]].join(` || `)})`}function columnDefinition(e,t,n){let r=identifier(t),i=`${identifier(e)}.${r}`,a=[r,columnType(n)];if(n.serial)return n.type===`integer`?(a.push(`GENERATED BY DEFAULT AS IDENTITY (${sequenceRange(n.serial)})`),n.unique&&a.push(`UNIQUE`),a.join(` `)):(n.required&&a.push(`NOT NULL`),n.unique&&a.push(`UNIQUE`),a.push(`DEFAULT ${serialStringDefault(sequenceName(e,t),n.serial.format,i)}`),a.join(` `));let o=n.default!==void 0,s=n.hooks?.create!==void 0||n.optionalOnCreate===!0;return n.required&&(o||!s)&&a.push(`NOT NULL`),n.unique&&a.push(`UNIQUE`),o&&a.push(`DEFAULT ${defaultExpression(n,i)}`),a.join(` `)}function jsonPathKey(e){return`"${e.replaceAll(`\\`,`\\\\`).replaceAll(`"`,`\\"`)}"`}function referencesWithin(e,t,n){let r=t.array?`${n}[*]`:n;return t.type===`nested`?Object.entries(t.fields??{}).flatMap(([t,n])=>referencesWithin(e,n,`${r}.${jsonPathKey(t)}`)):!t.foreignKey||t.foreignKeyType===void 0?[]:[{column:e,path:r,table:t.foreignKeyType,field:t.foreignKeyField??`id`,type:t.type}]}const a=/* @__PURE__ */ new Set([`integer`,`float`,`decimal`,`boolean`,`bool`,`uuid`]);function nestedUniqueMembers(e,t,n){return t.array?[]:t.type===`nested`?Object.entries(t.fields??{}).flatMap(([t,r])=>nestedUniqueMembers(e,r,[...n,t])):t.unique&&n.length>0?[{column:e,path:n,field:t}]:[]}function nestedUniqueIndex(e,t){let n=t.path.map(stringLiteral).join(`, `),r=`${identifier(t.column)} #>> ARRAY[${n}]`,i=a.has(t.field.type)?`CAST(${r} AS ${mapFieldTypeToPostgresType(t.field.type)})`:r;return`CREATE UNIQUE INDEX IF NOT EXISTS ${derivedIdentifier(e,`${t.column}.${t.path.join(`.`)}`,`nested_idx`)} ON ${identifier(e)} ((${i}))`}function tableReferences(e){return Object.entries(e.fields).filter(([e])=>e!==`id`).flatMap(([e,t])=>referencesWithin(e,t,`$`))}function isConstraintReference(e,t){if(e.path!==`$`)return!1;let n=e.field===`id`?`uuid`:t.get(e.table)?.fields[e.field]?.type;return n===void 0||mapFieldTypeToPostgresType(n)===mapFieldTypeToPostgresType(e.type)}function referenceClause(e){return`REFERENCES ${identifier(e.table)} (${identifier(e.field)})`}function declaredType(e){return e===`bool`?`boolean`:e}function checkReference(e,t){let n=t.get(e.table);if(!n)return{problem:`references unknown table "${e.table}".`};let r=e.field===`id`?{type:`uuid`,unique:!0}:n.fields[e.field];if(!r)return{problem:`references unknown field "${e.field}" of table "${e.table}".`};let i=e.field===`id`||declaredType(r.type)===declaredType(e.type);return!r.unique||!i?{problem:`references "${e.table}"."${e.field}", which must be unique and of the same type as the referencing field.`}:{pgType:mapFieldTypeToPostgresType(r.type)}}function referencedColumnType(e,t,n){let r=checkReference(e,t);if(`problem`in r)throw Error(`Field ${n} ${r.problem}`);return r.pgType}function withoutReference(e){let{foreignKey:t,foreignKeyType:n,foreignKeyField:r,...i}=e;return i}function withoutUnresolvableReference(e,t){return e.fields?{...e,fields:Object.fromEntries(Object.entries(e.fields).map(([e,n])=>[e,withoutUnresolvableReference(n,t)]))}:!e.foreignKey||e.foreignKeyType===void 0?e:`problem`in checkReference({column:``,path:`$`,table:e.foreignKeyType,field:e.foreignKeyField??`id`,type:e.type},t)?withoutReference(e):e}function dropUnresolvableReferences(e){let t=new Map(e.map(e=>[e.name,e]));return e.map(e=>({...e,fields:Object.fromEntries(Object.entries(e.fields).map(([e,n])=>[e,withoutUnresolvableReference(n,t)]))}))}function referenceCheck(e,t,n){let r=referencedColumnType(e,t,n),i=r===mapFieldTypeToPostgresType(e.type)?`parent.${identifier(e.field)} = (fk_check.v #>> '{}')::${r}`:`parent.${identifier(e.field)}::text = (fk_check.v #>> '{}')`,a=`violates foreign key constraints: % not found in ${`${identifier(e.table)}.${identifier(e.field)}`.replaceAll(`%`,`%%`)}`;return[` FOR v IN SELECT jsonb_path_query(to_jsonb(NEW.${identifier(e.column)}), ${stringLiteral(e.path)}) LOOP`,` IF jsonb_typeof(fk_check.v) <> 'null' AND NOT EXISTS (SELECT 1 FROM ${identifier(e.table)} AS parent WHERE ${i}) THEN`,` RAISE EXCEPTION ${stringLiteral(a)}, fk_check.v #>> '{}' USING ERRCODE = 'foreign_key_violation';`,` END IF;`,` END LOOP;`].join(`
|
|
2
2
|
`)}function referenceTrigger(e,t,n){if(t.length===0)return[];let r=derivedIdentifier(e.name,`references`,`fk`);return[`CREATE OR REPLACE FUNCTION ${r}() RETURNS trigger LANGUAGE plpgsql AS ${dollarQuoted([`<<fk_check>>`,`DECLARE v jsonb;`,`BEGIN`,...t.map(t=>referenceCheck(t,n,`${e.name}.${t.column}`)),` RETURN NULL;`,`END`].join(`
|
|
3
|
-
`))}`,`CREATE OR REPLACE TRIGGER ${r} AFTER INSERT OR UPDATE ON ${identifier(e.name)} FOR EACH ROW EXECUTE FUNCTION ${r}()`]}function
|
|
4
|
-
`)}
|
|
3
|
+
`))}`,`CREATE OR REPLACE TRIGGER ${r} AFTER INSERT OR UPDATE ON ${identifier(e.name)} FOR EACH ROW EXECUTE FUNCTION ${r}()`]}function incomingReferences(e,t){return[...t.values()].flatMap(n=>tableReferences(n).filter(n=>!isConstraintReference(n,t)&&n.table===e.name).flatMap(e=>{let r=checkReference(e,t);return`pgType`in r?[{...e,child:n.name,pgType:r.pgType}]:[]}))}function holdsParent(e){let t=`child.${identifier(e.column)}`,n=`OLD.${identifier(e.field)}`,r=e.pgType===mapFieldTypeToPostgresType(e.type);if(r&&e.path===`$[*]`)return`${n} = ANY(${t})`;let i=r?`(held.v #>> '{}')::${e.pgType} = ${n}`:`(held.v #>> '{}') = ${n}::text`,a=r?`WHEN NOT pg_input_is_valid(held.v #>> '{}', ${stringLiteral(e.pgType)}) THEN false `:``;return`EXISTS (SELECT 1 FROM jsonb_path_query(to_jsonb(${t}), ${stringLiteral(e.path)}) AS held(v) WHERE CASE WHEN jsonb_typeof(held.v) = 'null' THEN false ${a}ELSE ${i} END)`}const o=`old_rows`;function holds(e,t){return`EXISTS (SELECT 1 FROM ${t} AS child WHERE ${holdsParent(e)})`}function refuseWhen(e,t,n){let r=`update or delete on table \`${e.name}\` violates foreign key constraint: some record is still referenced from table \`${t.child}\``;return[` IF ${n} THEN`,` RAISE EXCEPTION ${stringLiteral(r.replaceAll(`%`,`%%`))} USING ERRCODE = 'foreign_key_violation';`,` END IF;`].join(`
|
|
4
|
+
`)}function referencedDeleteCheck(e,t){let n=holds(t,identifier(t.child));return refuseWhen(e,t,t.child===e.name?`${n} OR ${holds(t,o)}`:n)}function referencedUpdateCheck(e,t){let n=identifier(t.field);return refuseWhen(e,t,`NEW.${n} IS DISTINCT FROM OLD.${n} AND ${holds(t,identifier(t.child))}`)}function referencedTrigger(e,t){let n=incomingReferences(e,t);return n.length===0?[]:[[`referenced_delete`,`DELETE`,n.some(t=>t.child===e.name)?` REFERENCING OLD TABLE AS ${o}`:``,referencedDeleteCheck],[`referenced_update`,`UPDATE`,``,referencedUpdateCheck]].flatMap(([t,r,i,a])=>{let o=derivedIdentifier(e.name,t,`fk`);return[`CREATE OR REPLACE FUNCTION ${o}() RETURNS trigger LANGUAGE plpgsql AS ${dollarQuoted([`BEGIN`,...n.map(t=>a(e,t)),` RETURN NULL;`,`END`].join(`
|
|
5
|
+
`))}`,`CREATE OR REPLACE TRIGGER ${o} AFTER ${r} ON ${identifier(e.name)}${i} FOR EACH ROW EXECUTE FUNCTION ${o}()`]})}function generateTableDDL(e,t={}){let n=identifier(e.name),r=new Map((t.tables??[e]).map(e=>[e.name,e])),i=[],a=[],o=[` "id" uuid PRIMARY KEY DEFAULT gen_random_uuid()`],s=tableReferences(e),c=new Map(s.filter(e=>isConstraintReference(e,r)).map(e=>[e.column,e]));for(let[r,s]of Object.entries(e.fields)){if(r===`id`)continue;if(s.serial&&s.type!==`integer`){let t=sequenceName(e.name,r);i.push(`CREATE SEQUENCE IF NOT EXISTS ${t} ${sequenceRange(s.serial)}`),a.push(`ALTER SEQUENCE ${t} OWNED BY ${n}.${identifier(r)}`)}let l=c.get(r),u=l&&!t.deferredReferences?.has(r)?` ${referenceClause(l)}`:``;o.push(` ${columnDefinition(e.name,r,s)}${u}`)}let l=[...Object.entries(e.indexes??{}).filter(([,e])=>e.unique).map(([t,r])=>`CREATE UNIQUE INDEX IF NOT EXISTS ${derivedIdentifier(e.name,t,`idx`)} ON ${n} (${r.fields.map(identifier).join(`, `)})`),...Object.entries(e.fields).filter(([e])=>e!==`id`).flatMap(([e,t])=>nestedUniqueMembers(e,t,[])).map(t=>nestedUniqueIndex(e.name,t))];for(let t of c.values())referencedColumnType(t,r,`${e.name}.${t.column}`);return[...i,`CREATE TABLE IF NOT EXISTS ${n} (\n${o.join(`,
|
|
6
|
+
`)}\n)`,...a,...l,...referenceTrigger(e,s.filter(e=>!isConstraintReference(e,r)),r),...referencedTrigger(e,r)]}function addForeignKey(e,t){let n=derivedName(e.name,t.column,`fk`),r=identifier(e.name);return`DO ${dollarQuoted([`BEGIN`,`IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = ${stringLiteral(n)} AND conrelid = ${stringLiteral(r)}::regclass) THEN`,`ALTER TABLE ${r} ADD CONSTRAINT ${identifier(n)} FOREIGN KEY (${identifier(t.column)}) ${referenceClause(t)};`,`END IF;`,`END`].join(`
|
|
5
7
|
`))}`}function planCreation(e){let t=new Set(e.map(e=>e.name)),n=new Map(e.map(e=>[e.name,e])),r=/* @__PURE__ */ new Set,i=[...e],a=[],o=/* @__PURE__ */ new Map,pendingOf=e=>tableReferences(e).filter(e=>isConstraintReference(e,n)).filter(n=>t.has(n.table)&&n.table!==e.name&&!r.has(n.table));for(;i.length>0;){let e=i.findIndex(e=>pendingOf(e).length===0),[t]=i.splice(Math.max(e,0),1),n=pendingOf(t);n.length>0&&o.set(t,n),r.add(t.name),a.push(t)}return{ordered:a,deferred:o}}function generateSchemaDDL(e){let{ordered:t,deferred:n}=planCreation(e),r=t.map(t=>generateTableDDL(t,{tables:e,deferredReferences:new Set(n.get(t)?.map(e=>e.column))})),i=t.flatMap(e=>(n.get(e)??[]).map(t=>addForeignKey(e,t)));return[...r,i].filter(e=>e.length>0).map(e=>e.map(e=>`${e};`).join(`
|
|
6
8
|
`)).join(`
|
|
7
9
|
|
|
8
10
|
`)}function templateLiteral(e){return`\`${e.replaceAll(`\\`,`\\\\`).replaceAll("`","\\`").replaceAll("${","\\${").replaceAll(`\r`,`\\r`)}\``}function generatePgliteSchemaModule(e,t){let n=e.map(({namespace:e,tables:t})=>` ${JSON.stringify(e)}: ${templateLiteral(generateSchemaDDL(t))},`);return[`/**`,` * Auto-generated PGlite schema for TailorDB tables.`," * Create a namespace's tables in a test with `pglite.exec(pgliteSchema[namespace])`.",` *`,` * DO NOT EDIT - This file is auto-generated by ${t.generatedBy}.`,` */`,``,`export const pgliteSchema = {`,...n,`} as const;`,``].join(`
|
|
9
|
-
`)}function toDDLTables(e){return Object.values(e).map(e=>({name:e.name,fields:Object.fromEntries(Object.entries(e.fields).map(([e,t])=>[e,t.config])),indexes:e.indexes}))}function generatePGliteSchemaModule(e){return generatePgliteSchemaModule(e.map(({namespace:e,tables:t})=>({namespace:e,tables:toDDLTables(t)})),{generatedBy:`the kysely-type plugin`})}function mapFieldTypeToColumnType(e,t=!1){switch(e){case`uuid`:case`string`:case`decimal`:return`string`;case`integer`:case`float`:return`number`;case`date`:return t?`TemporalDate`:`Timestamp`;case`datetime`:return t?`TemporalInstant`:`Timestamp`;case`time`:return t?`TemporalTime`:`string`;case`bool`:case`boolean`:return`boolean`;case`enum`:case`nested`:throw Error(`Field type "${e}" has no scalar column type; resolve it before mapping.`);default:return`string`}}const
|
|
10
|
-
`)}${a.length>0?`;`:``}\n}`,s=Object.values(n).some(e=>e.required!==!0),c=Object.values(n).some(e=>e.hooks?.create||e.default!==void 0||e.optionalOnCreate===!0);return usesColumnTypeAlias(i)||s||c?{type:`ObjectColumnType<${o}>`,usedUtilityTypes:{...i,ObjectColumnType:!0}}:{type:o,usedUtilityTypes:i}}function getBaseType(e,t,n){let r=e.type,i=emptyUsedUtilityTypes();if(r===`enum`)return{type:getEnumType(e),usedUtilityTypes:i};if(r===`nested`)return getNestedType(e,t);let a=mapFieldTypeToColumnType(r,t&&!(n&&r===`time`));switch(a){case`Timestamp`:i.Timestamp=!0;break;case`TemporalDate`:i.TemporalDate=!0;break;case`TemporalInstant`:i.TemporalInstant=!0;break;case`TemporalTime`:i.TemporalTime=!0}return{type:a,usedUtilityTypes:i}}function generateFieldType(e,t,n=!1){let r=getBaseType(e,t,n),i={...r.usedUtilityTypes},
|
|
11
|
+
`)}function toDDLTables(e){return Object.values(e).map(e=>({name:e.name,fields:Object.fromEntries(Object.entries(e.fields).map(([e,t])=>[e,t.config])),indexes:e.indexes}))}function generatePGliteSchemaModule(e){return generatePgliteSchemaModule(e.map(({namespace:e,tables:t})=>({namespace:e,tables:toDDLTables(t)})),{generatedBy:`the kysely-type plugin`})}function mapFieldTypeToColumnType(e,t=!1){switch(e){case`uuid`:case`string`:case`decimal`:return`string`;case`integer`:case`float`:return`number`;case`date`:return t?`TemporalDate`:`Timestamp`;case`datetime`:return t?`TemporalInstant`:`Timestamp`;case`time`:return t?`TemporalTime`:`string`;case`bool`:case`boolean`:return`boolean`;case`enum`:case`nested`:throw Error(`Field type "${e}" has no scalar column type; resolve it before mapping.`);default:return`string`}}const s=/* @__PURE__ */ new Map([[`Timestamp`,{select:`Date`,write:`Date | string`}],[`TemporalDate`,{select:`Temporal.PlainDate`,write:`Temporal.PlainDate | string`}],[`TemporalInstant`,{select:`Temporal.Instant`,write:`Temporal.Instant | string`}],[`TemporalTime`,{select:`Temporal.PlainTime`,write:`Temporal.PlainTime | string`}]]);function emptyUsedUtilityTypes(){return{Timestamp:!1,TemporalDate:!1,TemporalInstant:!1,TemporalTime:!1,Serial:!1,ObjectColumnType:!1,ArrayColumnType:!1}}function mergeUsedUtilityTypes(e,t){return{Timestamp:e.Timestamp||t.Timestamp,TemporalDate:e.TemporalDate||t.TemporalDate,TemporalInstant:e.TemporalInstant||t.TemporalInstant,TemporalTime:e.TemporalTime||t.TemporalTime,Serial:e.Serial||t.Serial,ObjectColumnType:e.ObjectColumnType||t.ObjectColumnType,ArrayColumnType:e.ArrayColumnType||t.ArrayColumnType}}function usesColumnTypeAlias(e){return e.Timestamp||e.TemporalDate||e.TemporalInstant||e.TemporalTime}function getEnumType(e){let t=e.allowedValues;return t&&Array.isArray(t)?t.map(e=>`"${typeof e==`string`?e:e.value}"`).join(` | `):`string`}function getNestedType(e,t){let n=e.fields;if(!n||typeof n!=`object`)return{type:`string`,usedUtilityTypes:emptyUsedUtilityTypes()};let r=Object.entries(n).map(([e,n])=>{let r=generateFieldType(n,t,!0);return{fieldType:`${e}${n.required===!0?``:`?`}: ${r.type}`,usedUtilityTypes:r.usedUtilityTypes}}),i=r.reduce((e,t)=>mergeUsedUtilityTypes(e,t.usedUtilityTypes),emptyUsedUtilityTypes()),a=r.map(e=>e.fieldType),o=`{\n ${a.join(`;
|
|
12
|
+
`)}${a.length>0?`;`:``}\n}`,s=Object.values(n).some(e=>e.required!==!0),c=Object.values(n).some(e=>e.hooks?.create||e.default!==void 0||e.optionalOnCreate===!0);return usesColumnTypeAlias(i)||s||c?{type:`ObjectColumnType<${o}>`,usedUtilityTypes:{...i,ObjectColumnType:!0}}:{type:o,usedUtilityTypes:i}}function getBaseType(e,t,n){let r=e.type,i=emptyUsedUtilityTypes();if(r===`enum`)return{type:getEnumType(e),usedUtilityTypes:i};if(r===`nested`)return getNestedType(e,t);let a=mapFieldTypeToColumnType(r,t&&!(n&&r===`time`));switch(a){case`Timestamp`:i.Timestamp=!0;break;case`TemporalDate`:i.TemporalDate=!0;break;case`TemporalInstant`:i.TemporalInstant=!0;break;case`TemporalTime`:i.TemporalTime=!0}return{type:a,usedUtilityTypes:i}}function generateFieldType(e,t,n=!1){let r=getBaseType(e,t,n),i={...r.usedUtilityTypes},a=e.array===!0,o=e.required!==!0,c=s.has(r.type),l=r.type;return a&&(c||l.startsWith(`ObjectColumnType<`)?(l=`ArrayColumnType<${r.type}>`,i.ArrayColumnType=!0):l=e.type===`enum`?`(${r.type})[]`:`${r.type}[]`),o&&(l=`${l} | null`),e.serial&&(i.Serial=!0,l=`Serial<${l}>`),(e.hooks?.create||e.default!==void 0||e.optionalOnCreate===!0)&&(l=`Generated<${l}>`),{type:l,usedUtilityTypes:i}}function generateTableInterface(t,n,r){let i=Object.entries(n).filter(([e])=>e!==`id`).map(([e,t])=>({fieldName:e,...generateFieldType(t,r)})),a=[`id: Generated<string>;`,...i.map(e=>`${e.fieldName}: ${e.type};`)],o=i.reduce((e,t)=>mergeUsedUtilityTypes(e,t.usedUtilityTypes),emptyUsedUtilityTypes());return{typeDef:e`
|
|
11
13
|
${t}: {
|
|
12
14
|
${a.join(`
|
|
13
15
|
`)}
|
|
@@ -47,5 +49,5 @@ import{t as e}from"./multiline-EyzjEwn9.mjs";import{resolve as t}from"pathe";con
|
|
|
47
49
|
`].join(`
|
|
48
50
|
|
|
49
51
|
`)+`
|
|
50
|
-
`}const
|
|
51
|
-
//# sourceMappingURL=kysely-type-
|
|
52
|
+
`}const c=`@tailor-platform/kysely-type`,l=`./generated/tailordb.ts`;function kyselyTypePlugin(e){return{id:c,description:`Generates Kysely type definitions for TailorDB tables`,pluginConfig:e,async onTailorDBReady(e){let{distPath:n,pgliteSchemaPath:r,temporal:i}=e.pluginConfig;if(r&&t(n)===t(r))throw Error(`distPath and pgliteSchemaPath must resolve to different files.`);let a=[];for(let t of e.tailordb){let e=[];for(let n of Object.values(t.tables)){let t=await processKyselyType(n,i);e.push(t)}e.length!==0&&a.push({namespace:t.namespace,types:e})}let o=[];if(a.length>0){let t=generateUnifiedKyselyTypes(a,i);o.push({path:e.pluginConfig.distPath,content:t}),e.pluginConfig.pgliteSchemaPath&&o.push({path:e.pluginConfig.pgliteSchemaPath,content:generatePGliteSchemaModule(e.tailordb.filter(e=>Object.keys(e.tables).length>0))})}return{files:o}}}}export{processKyselyFields as a,dropUnresolvableReferences as c,generateUnifiedKyselyTypes as i,generatePgliteSchemaModule as l,c as n,s as o,kyselyTypePlugin as r,mapFieldTypeToColumnType as s,l as t,withoutReference as u};
|
|
53
|
+
//# sourceMappingURL=kysely-type-B0ri1-Xs.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kysely-type-B0ri1-Xs.mjs","names":["multiline","utf8","fieldType","name","hash","byte","value","suffix","tableName","memberName","kind","head","tag","body","base","field","pgType","label","CURRENT_TIME_EXPRESSIONS","currentTime","v","groups","format","conversion","fieldName","range","serial","SERIAL_CONVERSIONS","nextval","sequence","number","width","zeroPad","prefix","column","parts","hasDefault","filledOnCreate","key","here","path","child","CASTABLE_UNIQUE_TYPES","keys","member","indexed","table","reference","targetType","tables","type","target","targetField","sameType","check","_foreignKey","_type","_field","rest","byName","comparison","message","references","parentValue","notComparable","DELETED_ROWS","source","condition","remaining","event","transition","tableIdentifier","options","t","sequences","ownerships","columns","constraintReferences","r","constraint","inline","indexes","index","names","created","ordered","deferred","pending","statements","constraints","group","statement","entries","namespaces","namespace","temporal","COLUMN_TYPE_ALIASES","a","b","usedUtilityTypes","allowedValues","fieldConfig","fields","fieldResults","config","result","aggregatedUtilityTypes","acc","fieldTypes","obj","hasOptionalFields","hasGeneratedFields","nested","baseTypeResult","isArray","isNullable","isColumnTypeBase","finalType","fieldLines","parsedField","namespaceData","globalUsedUtilityTypes","ns","utilityTypeImports","types","line","KyselyGeneratorID","DEFAULT_KYSELY_TYPES_DIST_PATH","ctx","pgliteSchemaPath","resolve","distPath","allNamespaceData","typeMetadataList","metadata","files","content"],"sources":["../src/utils/tailordb-ddl.ts","../src/plugin/builtin/kysely-type/pglite-schema.ts","../src/utils/field-column-type.ts","../src/plugin/builtin/kysely-type/type-processor.ts","../src/plugin/builtin/kysely-type/index.ts"],"sourcesContent":["/**\n * PostgreSQL DDL derived from TailorDB table definitions, for creating the\n * tables a PGlite-backed test needs. The columns match the flat shape the\n * Kysely type generators emit, not TailorDB's storage layout.\n */\n\n/** The field shape the DDL generator reads; parsed and snapshot field configs are both assignable. */\nexport interface DDLFieldConfig {\n type: string;\n required?: boolean;\n array?: boolean;\n unique?: boolean;\n serial?: { start: number; maxValue?: number; format?: string };\n scale?: number;\n default?: unknown;\n optionalOnCreate?: boolean;\n foreignKey?: boolean;\n foreignKeyType?: string;\n foreignKeyField?: string;\n hooks?: { create?: unknown; update?: unknown };\n fields?: Record<string, DDLFieldConfig>;\n}\n\n/** A table to emit DDL for. */\nexport interface DDLTableConfig {\n name: string;\n fields: Record<string, DDLFieldConfig>;\n indexes?: Record<string, { fields: string[]; unique?: boolean }>;\n}\n\nconst MAX_IDENTIFIER_BYTES = 63;\n// The platform rounds decimals to their scale (6 unless configured); numeric\n// needs a precision to carry a scale, and the maximum leaves it unconstrained.\nconst DECIMAL_PRECISION = 1000;\nconst DEFAULT_DECIMAL_SCALE = 6;\nconst utf8 = new TextEncoder();\n\n/**\n * Map a TailorDB field type to the PostgreSQL column type PGlite tests use for it.\n * @param fieldType - TailorDB field type name\n * @returns The PostgreSQL type name\n * @throws If the type is not a TailorDB field type\n */\nexport function mapFieldTypeToPostgresType(fieldType: string): string {\n switch (fieldType) {\n case \"uuid\":\n return \"uuid\";\n case \"string\":\n case \"enum\":\n return \"text\";\n case \"boolean\":\n case \"bool\":\n return \"boolean\";\n case \"integer\":\n return \"integer\";\n case \"float\":\n return \"double precision\";\n case \"decimal\":\n return \"numeric\";\n case \"date\":\n return \"date\";\n case \"datetime\":\n return \"timestamptz\";\n case \"time\":\n return \"time\";\n case \"nested\":\n return \"jsonb\";\n default:\n throw new Error(`Field type \"${fieldType}\" has no PostgreSQL column type.`);\n }\n}\n\nfunction identifier(name: string): string {\n return `\"${name.replaceAll('\"', '\"\"')}\"`;\n}\n\nfunction fnv1aHex(value: string): string {\n let hash = 0x811c9dc5;\n for (const byte of utf8.encode(value)) {\n hash = Math.imul(hash ^ byte, 0x01000193) >>> 0;\n }\n return hash.toString(16).padStart(8, \"0\");\n}\n\ntype DerivedNameKind = \"seq\" | \"idx\" | \"nested_idx\" | \"fk\";\n\nfunction derivedName(tableName: string, memberName: string, kind: DerivedNameKind): string {\n const suffix = `_${fnv1aHex(JSON.stringify([tableName, memberName, kind]))}_${kind}`;\n let head = `${tableName}_${memberName}`;\n while (utf8.encode(head + suffix).length > MAX_IDENTIFIER_BYTES) head = head.slice(0, -1);\n return head + suffix;\n}\n\nfunction derivedIdentifier(tableName: string, memberName: string, kind: DerivedNameKind): string {\n return identifier(derivedName(tableName, memberName, kind));\n}\n\nfunction dollarQuoted(body: string): string {\n let tag = \"$fk$\";\n while (body.includes(tag)) tag = `${tag.slice(0, -1)}_$`;\n return `${tag}\\n${body}\\n${tag}`;\n}\n\nfunction stringLiteral(value: string): string {\n return `'${value.replaceAll(\"'\", \"''\")}'`;\n}\n\nfunction columnType(field: DDLFieldConfig): string {\n const base =\n field.type === \"decimal\"\n ? `numeric(${DECIMAL_PRECISION}, ${field.scale ?? DEFAULT_DECIMAL_SCALE})`\n : mapFieldTypeToPostgresType(field.type);\n return field.array && field.type !== \"nested\" ? `${base}[]` : base;\n}\n\nfunction scalarLiteral(value: unknown, field: DDLFieldConfig, label: string): string {\n const pgType = mapFieldTypeToPostgresType(field.type);\n if (value instanceof Date) {\n if (Number.isNaN(value.getTime())) {\n throw new Error(`Default of field ${label} is an invalid Date.`);\n }\n return `${stringLiteral(value.toISOString())}::${pgType}`;\n }\n switch (typeof value) {\n case \"string\":\n return pgType === \"text\" ? stringLiteral(value) : `${stringLiteral(value)}::${pgType}`;\n case \"number\":\n if (!Number.isFinite(value)) {\n throw new Error(`Default of field ${label} is not a finite number.`);\n }\n return String(value);\n case \"boolean\":\n return value ? \"TRUE\" : \"FALSE\";\n default:\n throw new Error(`Default of field ${label} cannot be rendered as a SQL literal.`);\n }\n}\n\nconst CURRENT_TIME_EXPRESSIONS = new Map([\n [\"datetime\", \"now()\"],\n [\"date\", \"CURRENT_DATE\"],\n [\"time\", \"LOCALTIME\"],\n]);\n\nfunction defaultExpression(field: DDLFieldConfig, label: string): string {\n const value = field.default;\n const currentTime = CURRENT_TIME_EXPRESSIONS.get(field.type);\n if (value === \"now\" && currentTime !== undefined) {\n return currentTime;\n }\n if (field.array) {\n if (!Array.isArray(value)) {\n throw new Error(`Default of array field ${label} must be an array.`);\n }\n const pgType = columnType(field);\n if (value.length === 0) return `'{}'::${pgType}`;\n return `ARRAY[${value.map((v) => scalarLiteral(v, field, label)).join(\", \")}]::${pgType}`;\n }\n return scalarLiteral(value, field, label);\n}\n\ninterface SerialFormat {\n prefix: string;\n width: number;\n zeroPad: boolean;\n conversion: \"d\" | \"x\" | \"X\";\n suffix: string;\n}\n\n// printf-style with exactly one conversion specifier; octal has no Postgres\n// formatting function to stand in for it.\nfunction parseSerialFormat(format: string, label: string): SerialFormat {\n const match =\n /^(?<prefix>[^%]*)%(?<zero>0?)(?<width>\\d*)(?<conversion>[a-zA-Z])(?<suffix>[^%]*)$/.exec(\n format,\n );\n const groups = match?.groups;\n const conversion = groups?.conversion;\n if (!groups || (conversion !== \"d\" && conversion !== \"x\" && conversion !== \"X\")) {\n throw new Error(\n `Serial format \"${format}\" of field ${label} is not supported; use a single %d, %x, or %X specifier with an optional width (e.g. \"INV-%05d\").`,\n );\n }\n return {\n prefix: groups.prefix ?? \"\",\n zeroPad: groups.zero === \"0\",\n width: groups.width ? Number(groups.width) : 0,\n conversion,\n suffix: groups.suffix ?? \"\",\n };\n}\n\nfunction sequenceName(tableName: string, fieldName: string): string {\n return derivedIdentifier(tableName, fieldName, \"seq\");\n}\n\n// A sequence's minimum defaults to 1, which rejects a start of 0.\nfunction sequenceRange(serial: NonNullable<DDLFieldConfig[\"serial\"]>): string {\n const range = [`START WITH ${serial.start}`, `MINVALUE ${serial.start}`];\n if (serial.maxValue !== undefined) range.push(`MAXVALUE ${serial.maxValue}`);\n return range.join(\" \");\n}\n\nconst SERIAL_CONVERSIONS: Record<SerialFormat[\"conversion\"], (nextval: string) => string> = {\n d: (nextval) => nextval,\n x: (nextval) => `to_hex(${nextval})`,\n X: (nextval) => `upper(to_hex(${nextval}))`,\n};\n\nfunction serialStringDefault(sequence: string, format: string | undefined, label: string): string {\n const nextval = `nextval(${stringLiteral(sequence)})`;\n if (format === undefined) return `(${nextval}::text)`;\n const { prefix, width, zeroPad, conversion, suffix } = parseSerialFormat(format, label);\n const number = SERIAL_CONVERSIONS[conversion](nextval);\n let value = conversion === \"d\" ? `${number}::text` : number;\n if (width > 0) {\n value = `format('%${width}s', ${number})`;\n if (zeroPad) value = `translate(${value}, ' ', '0')`;\n }\n const parts = [\n ...(prefix ? [stringLiteral(prefix)] : []),\n value,\n ...(suffix ? [stringLiteral(suffix)] : []),\n ];\n return `(${parts.join(\" || \")})`;\n}\n\nfunction columnDefinition(tableName: string, fieldName: string, field: DDLFieldConfig): string {\n const column = identifier(fieldName);\n const label = `${identifier(tableName)}.${column}`;\n const parts = [column, columnType(field)];\n\n if (field.serial) {\n if (field.type === \"integer\") {\n parts.push(`GENERATED BY DEFAULT AS IDENTITY (${sequenceRange(field.serial)})`);\n if (field.unique) parts.push(\"UNIQUE\");\n return parts.join(\" \");\n }\n if (field.required) parts.push(\"NOT NULL\");\n if (field.unique) parts.push(\"UNIQUE\");\n parts.push(\n `DEFAULT ${serialStringDefault(sequenceName(tableName, fieldName), field.serial.format, label)}`,\n );\n return parts.join(\" \");\n }\n\n const hasDefault = field.default !== undefined;\n const filledOnCreate = field.hooks?.create !== undefined || field.optionalOnCreate === true;\n if (field.required && (hasDefault || !filledOnCreate)) parts.push(\"NOT NULL\");\n if (field.unique) parts.push(\"UNIQUE\");\n if (hasDefault) parts.push(`DEFAULT ${defaultExpression(field, label)}`);\n return parts.join(\" \");\n}\n\ninterface Reference {\n /** Column holding the reference */\n column: string;\n /** JSONPath from the column value to each referencing value; `$` for a scalar column */\n path: string;\n table: string;\n field: string;\n /** TailorDB type of the referencing value */\n type: string;\n}\n\nfunction jsonPathKey(key: string): string {\n return `\"${key.replaceAll(\"\\\\\", \"\\\\\\\\\").replaceAll('\"', '\\\\\"')}\"`;\n}\n\nfunction referencesWithin(column: string, field: DDLFieldConfig, path: string): Reference[] {\n const here = field.array ? `${path}[*]` : path;\n if (field.type === \"nested\") {\n return Object.entries(field.fields ?? {}).flatMap(([key, child]) =>\n referencesWithin(column, child, `${here}.${jsonPathKey(key)}`),\n );\n }\n if (!field.foreignKey || field.foreignKeyType === undefined) return [];\n return [\n {\n column,\n path: here,\n table: field.foreignKeyType,\n field: field.foreignKeyField ?? \"id\",\n type: field.type,\n },\n ];\n}\n\n// The platform indexes a unique member of a nested object by the value at its\n// path, cast to the member's type. Members inside an array have no single value\n// to index, so they are left out.\nconst CASTABLE_UNIQUE_TYPES = new Set([\"integer\", \"float\", \"decimal\", \"boolean\", \"bool\", \"uuid\"]);\n\ninterface NestedUniqueMember {\n column: string;\n path: string[];\n field: DDLFieldConfig;\n}\n\nfunction nestedUniqueMembers(\n column: string,\n field: DDLFieldConfig,\n path: string[],\n): NestedUniqueMember[] {\n if (field.array) return [];\n if (field.type === \"nested\") {\n return Object.entries(field.fields ?? {}).flatMap(([key, child]) =>\n nestedUniqueMembers(column, child, [...path, key]),\n );\n }\n return field.unique && path.length > 0 ? [{ column, path, field }] : [];\n}\n\nfunction nestedUniqueIndex(tableName: string, member: NestedUniqueMember): string {\n const keys = member.path.map(stringLiteral).join(\", \");\n const value = `${identifier(member.column)} #>> ARRAY[${keys}]`;\n const indexed = CASTABLE_UNIQUE_TYPES.has(member.field.type)\n ? `CAST(${value} AS ${mapFieldTypeToPostgresType(member.field.type)})`\n : value;\n const name = derivedIdentifier(\n tableName,\n `${member.column}.${member.path.join(\".\")}`,\n \"nested_idx\",\n );\n return `CREATE UNIQUE INDEX IF NOT EXISTS ${name} ON ${identifier(tableName)} ((${indexed}))`;\n}\n\nfunction tableReferences(table: DDLTableConfig): Reference[] {\n return Object.entries(table.fields)\n .filter(([fieldName]) => fieldName !== \"id\")\n .flatMap(([fieldName, field]) => referencesWithin(fieldName, field, \"$\"));\n}\n\n// Postgres cannot reference from an array or jsonb column, nor from a column\n// whose type differs from the referenced one (a string field related to id),\n// so only a plain column of the same storage type becomes a FOREIGN KEY constraint.\nfunction isConstraintReference(\n reference: Reference,\n tables: ReadonlyMap<string, DDLTableConfig>,\n): boolean {\n if (reference.path !== \"$\") return false;\n const targetType =\n reference.field === \"id\" ? \"uuid\" : tables.get(reference.table)?.fields[reference.field]?.type;\n return (\n targetType === undefined ||\n mapFieldTypeToPostgresType(targetType) === mapFieldTypeToPostgresType(reference.type)\n );\n}\n\nfunction referenceClause(reference: Reference): string {\n return `REFERENCES ${identifier(reference.table)} (${identifier(reference.field)})`;\n}\n\nfunction declaredType(type: string): string {\n return type === \"bool\" ? \"boolean\" : type;\n}\n\ntype ReferenceCheck = { pgType: string } | { problem: string };\n\nfunction checkReference(\n reference: Reference,\n tables: ReadonlyMap<string, DDLTableConfig>,\n): ReferenceCheck {\n const target = tables.get(reference.table);\n if (!target) return { problem: `references unknown table \"${reference.table}\".` };\n const targetField =\n reference.field === \"id\" ? { type: \"uuid\", unique: true } : target.fields[reference.field];\n if (!targetField) {\n return {\n problem: `references unknown field \"${reference.field}\" of table \"${reference.table}\".`,\n };\n }\n const sameType =\n reference.field === \"id\" || declaredType(targetField.type) === declaredType(reference.type);\n if (!targetField.unique || !sameType) {\n return {\n problem: `references \"${reference.table}\".\"${reference.field}\", which must be unique and of the same type as the referencing field.`,\n };\n }\n return { pgType: mapFieldTypeToPostgresType(targetField.type) };\n}\n\nfunction referencedColumnType(\n reference: Reference,\n tables: ReadonlyMap<string, DDLTableConfig>,\n label: string,\n): string {\n const check = checkReference(reference, tables);\n if (\"problem\" in check) throw new Error(`Field ${label} ${check.problem}`);\n return check.pgType;\n}\n\n/**\n * The field without the relation it declares.\n * @param field - A field config\n * @returns The field with its foreign key settings removed\n */\nexport function withoutReference(field: DDLFieldConfig): DDLFieldConfig {\n const {\n foreignKey: _foreignKey,\n foreignKeyType: _type,\n foreignKeyField: _field,\n ...rest\n } = field;\n return rest;\n}\n\nfunction withoutUnresolvableReference(\n field: DDLFieldConfig,\n tables: ReadonlyMap<string, DDLTableConfig>,\n): DDLFieldConfig {\n if (field.fields) {\n return {\n ...field,\n fields: Object.fromEntries(\n Object.entries(field.fields).map(([name, child]) => [\n name,\n withoutUnresolvableReference(child, tables),\n ]),\n ),\n };\n }\n if (!field.foreignKey || field.foreignKeyType === undefined) return field;\n const reference = {\n column: \"\",\n path: \"$\",\n table: field.foreignKeyType,\n field: field.foreignKeyField ?? \"id\",\n type: field.type,\n };\n return \"problem\" in checkReference(reference, tables) ? withoutReference(field) : field;\n}\n\n/**\n * Remove the references the given tables cannot satisfy: a target table or\n * field that is missing, not unique, or of another type than the referencing\n * field. {@link generateSchemaDDL} rejects such references, so this lets a\n * schema that is only partly consistent still render, minus those references.\n * @param tables - Tables of one schema\n * @returns The tables, with unsatisfiable references removed from their fields\n */\nexport function dropUnresolvableReferences(tables: readonly DDLTableConfig[]): DDLTableConfig[] {\n const byName = new Map(tables.map((table) => [table.name, table]));\n return tables.map((table) => ({\n ...table,\n fields: Object.fromEntries(\n Object.entries(table.fields).map(([name, field]) => [\n name,\n withoutUnresolvableReference(field, byName),\n ]),\n ),\n }));\n}\n\nfunction referenceCheck(\n reference: Reference,\n tables: ReadonlyMap<string, DDLTableConfig>,\n label: string,\n): string {\n const pgType = referencedColumnType(reference, tables, label);\n const comparison =\n pgType === mapFieldTypeToPostgresType(reference.type)\n ? `parent.${identifier(reference.field)} = (fk_check.v #>> '{}')::${pgType}`\n : `parent.${identifier(reference.field)}::text = (fk_check.v #>> '{}')`;\n const target = `${identifier(reference.table)}.${identifier(reference.field)}`;\n const message = `violates foreign key constraints: % not found in ${target.replaceAll(\"%\", \"%%\")}`;\n return [\n ` FOR v IN SELECT jsonb_path_query(to_jsonb(NEW.${identifier(reference.column)}), ${stringLiteral(reference.path)}) LOOP`,\n ` IF jsonb_typeof(fk_check.v) <> 'null' AND NOT EXISTS (SELECT 1 FROM ${identifier(reference.table)} AS parent WHERE ${comparison}) THEN`,\n ` RAISE EXCEPTION ${stringLiteral(message)}, fk_check.v #>> '{}' USING ERRCODE = 'foreign_key_violation';`,\n \" END IF;\",\n \" END LOOP;\",\n ].join(\"\\n\");\n}\n\nfunction referenceTrigger(\n table: DDLTableConfig,\n references: readonly Reference[],\n tables: ReadonlyMap<string, DDLTableConfig>,\n): string[] {\n if (references.length === 0) return [];\n const name = derivedIdentifier(table.name, \"references\", \"fk\");\n const checks = references.map((reference) =>\n referenceCheck(reference, tables, `${table.name}.${reference.column}`),\n );\n const body = [\n \"<<fk_check>>\",\n \"DECLARE v jsonb;\",\n \"BEGIN\",\n ...checks,\n \" RETURN NULL;\",\n \"END\",\n ].join(\"\\n\");\n return [\n `CREATE OR REPLACE FUNCTION ${name}() RETURNS trigger LANGUAGE plpgsql AS ${dollarQuoted(body)}`,\n `CREATE OR REPLACE TRIGGER ${name} AFTER INSERT OR UPDATE ON ${identifier(table.name)} FOR EACH ROW EXECUTE FUNCTION ${name}()`,\n ];\n}\n\ninterface IncomingReference extends Reference {\n /** Table whose array or nested column holds the reference */\n child: string;\n pgType: string;\n}\n\nfunction incomingReferences(\n table: DDLTableConfig,\n tables: ReadonlyMap<string, DDLTableConfig>,\n): IncomingReference[] {\n return [...tables.values()].flatMap((child) =>\n tableReferences(child)\n .filter(\n (reference) => !isConstraintReference(reference, tables) && reference.table === table.name,\n )\n .flatMap((reference) => {\n const check = checkReference(reference, tables);\n return \"pgType\" in check ? [{ ...reference, child: child.name, pgType: check.pgType }] : [];\n }),\n );\n}\n\nfunction holdsParent(reference: IncomingReference): string {\n const column = `child.${identifier(reference.column)}`;\n const parentValue = `OLD.${identifier(reference.field)}`;\n const sameType = reference.pgType === mapFieldTypeToPostgresType(reference.type);\n if (sameType && reference.path === \"$[*]\") return `${parentValue} = ANY(${column})`;\n const comparison = sameType\n ? `(held.v #>> '{}')::${reference.pgType} = ${parentValue}`\n : `(held.v #>> '{}') = ${parentValue}::text`;\n const notComparable = sameType\n ? `WHEN NOT pg_input_is_valid(held.v #>> '{}', ${stringLiteral(reference.pgType)}) THEN false `\n : \"\";\n return `EXISTS (SELECT 1 FROM jsonb_path_query(to_jsonb(${column}), ${stringLiteral(reference.path)}) AS held(v) WHERE CASE WHEN jsonb_typeof(held.v) = 'null' THEN false ${notComparable}ELSE ${comparison} END)`;\n}\n\nconst DELETED_ROWS = \"old_rows\";\n\nfunction holds(reference: IncomingReference, source: string): string {\n return `EXISTS (SELECT 1 FROM ${source} AS child WHERE ${holdsParent(reference)})`;\n}\n\nfunction refuseWhen(\n table: DDLTableConfig,\n reference: IncomingReference,\n condition: string,\n): string {\n const message = `update or delete on table \\`${table.name}\\` violates foreign key constraint: some record is still referenced from table \\`${reference.child}\\``;\n return [\n ` IF ${condition} THEN`,\n ` RAISE EXCEPTION ${stringLiteral(message.replaceAll(\"%\", \"%%\"))} USING ERRCODE = 'foreign_key_violation';`,\n \" END IF;\",\n ].join(\"\\n\");\n}\n\nfunction referencedDeleteCheck(table: DDLTableConfig, reference: IncomingReference): string {\n const remaining = holds(reference, identifier(reference.child));\n const condition =\n reference.child === table.name\n ? `${remaining} OR ${holds(reference, DELETED_ROWS)}`\n : remaining;\n return refuseWhen(table, reference, condition);\n}\n\nfunction referencedUpdateCheck(table: DDLTableConfig, reference: IncomingReference): string {\n const field = identifier(reference.field);\n return refuseWhen(\n table,\n reference,\n `NEW.${field} IS DISTINCT FROM OLD.${field} AND ${holds(reference, identifier(reference.child))}`,\n );\n}\n\nfunction referencedTrigger(\n table: DDLTableConfig,\n tables: ReadonlyMap<string, DDLTableConfig>,\n): string[] {\n const references = incomingReferences(table, tables);\n if (references.length === 0) return [];\n const selfReferencing = references.some((reference) => reference.child === table.name);\n const deletedRows = selfReferencing ? ` REFERENCING OLD TABLE AS ${DELETED_ROWS}` : \"\";\n return (\n [\n [\"referenced_delete\", \"DELETE\", deletedRows, referencedDeleteCheck],\n [\"referenced_update\", \"UPDATE\", \"\", referencedUpdateCheck],\n ] as const\n ).flatMap(([member, event, transition, check]) => {\n const name = derivedIdentifier(table.name, member, \"fk\");\n const body = [\n \"BEGIN\",\n ...references.map((reference) => check(table, reference)),\n \" RETURN NULL;\",\n \"END\",\n ].join(\"\\n\");\n return [\n `CREATE OR REPLACE FUNCTION ${name}() RETURNS trigger LANGUAGE plpgsql AS ${dollarQuoted(body)}`,\n `CREATE OR REPLACE TRIGGER ${name} AFTER ${event} ON ${identifier(table.name)}${transition} FOR EACH ROW EXECUTE FUNCTION ${name}()`,\n ];\n });\n}\n\n/** What {@link generateTableDDL} needs to know about the other tables of the schema. */\nexport interface TableDDLOptions {\n /** Every table of the schema, to resolve the types referenced columns point at */\n tables?: readonly DDLTableConfig[];\n /** Columns whose FOREIGN KEY is added after all tables exist, because the target is created later */\n deferredReferences?: ReadonlySet<string>;\n}\n\n/**\n * DDL statements that create one table: any sequences its string serial\n * fields draw from, the table, the sequences' ownership of their columns,\n * its unique indexes, then the triggers that enforce the references Postgres\n * cannot express as a FOREIGN KEY (inside array and nested columns): one\n * checking the references the table holds, one refusing to delete a row, and\n * one refusing to change the key of a row, that another table's array or\n * nested value still points at. Every statement\n * is `IF NOT EXISTS` or otherwise idempotent, so re-applying the script on a\n * database that already has the table is a no-op. Owning a sequence makes\n * `DROP TABLE` drop it and `TRUNCATE ... RESTART IDENTITY` reset it to the\n * configured start. Index names end in `_idx` so they cannot take the\n * `<table>_<column>_key` name Postgres gives a UNIQUE column, which\n * `IF NOT EXISTS` would otherwise silently skip.\n * @param table - Table name, fields, and indexes\n * @param options - The schema's other tables, and references to leave to later statements\n * @returns Statements in execution order, without trailing semicolons\n * @throws If a field type, default, or serial format cannot be expressed\n */\nexport function generateTableDDL(table: DDLTableConfig, options: TableDDLOptions = {}): string[] {\n const tableIdentifier = identifier(table.name);\n const tables = new Map((options.tables ?? [table]).map((t) => [t.name, t]));\n const sequences: string[] = [];\n const ownerships: string[] = [];\n const columns = [` \"id\" uuid PRIMARY KEY DEFAULT gen_random_uuid()`];\n const references = tableReferences(table);\n const constraintReferences = new Map(\n references.filter((r) => isConstraintReference(r, tables)).map((r) => [r.column, r]),\n );\n\n for (const [fieldName, field] of Object.entries(table.fields)) {\n if (fieldName === \"id\") continue;\n if (field.serial && field.type !== \"integer\") {\n const sequence = sequenceName(table.name, fieldName);\n sequences.push(`CREATE SEQUENCE IF NOT EXISTS ${sequence} ${sequenceRange(field.serial)}`);\n ownerships.push(\n `ALTER SEQUENCE ${sequence} OWNED BY ${tableIdentifier}.${identifier(fieldName)}`,\n );\n }\n const constraint = constraintReferences.get(fieldName);\n const inline =\n constraint && !options.deferredReferences?.has(fieldName)\n ? ` ${referenceClause(constraint)}`\n : \"\";\n columns.push(` ${columnDefinition(table.name, fieldName, field)}${inline}`);\n }\n\n const indexes = [\n ...Object.entries(table.indexes ?? {})\n .filter(([, index]) => index.unique)\n .map(\n ([name, index]) =>\n `CREATE UNIQUE INDEX IF NOT EXISTS ${derivedIdentifier(table.name, name, \"idx\")} ON ${tableIdentifier} (${index.fields.map(identifier).join(\", \")})`,\n ),\n ...Object.entries(table.fields)\n .filter(([fieldName]) => fieldName !== \"id\")\n .flatMap(([fieldName, field]) => nestedUniqueMembers(fieldName, field, []))\n .map((member) => nestedUniqueIndex(table.name, member)),\n ];\n\n for (const reference of constraintReferences.values()) {\n referencedColumnType(reference, tables, `${table.name}.${reference.column}`);\n }\n\n return [\n ...sequences,\n `CREATE TABLE IF NOT EXISTS ${tableIdentifier} (\\n${columns.join(\",\\n\")}\\n)`,\n ...ownerships,\n ...indexes,\n ...referenceTrigger(\n table,\n references.filter((r) => !isConstraintReference(r, tables)),\n tables,\n ),\n ...referencedTrigger(table, tables),\n ];\n}\n\nfunction addForeignKey(table: DDLTableConfig, reference: Reference): string {\n const name = derivedName(table.name, reference.column, \"fk\");\n const tableIdentifier = identifier(table.name);\n const body = [\n \"BEGIN\",\n `IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = ${stringLiteral(name)} AND conrelid = ${stringLiteral(tableIdentifier)}::regclass) THEN`,\n `ALTER TABLE ${tableIdentifier} ADD CONSTRAINT ${identifier(name)} FOREIGN KEY (${identifier(reference.column)}) ${referenceClause(reference)};`,\n \"END IF;\",\n \"END\",\n ].join(\"\\n\");\n return `DO ${dollarQuoted(body)}`;\n}\n\n// Tables come after the tables they reference. Where references form a cycle,\n// the first table of the cycle is created without the constraint that points\n// at a table not yet created, and the constraint is added once all tables exist.\nfunction planCreation(tables: readonly DDLTableConfig[]): {\n ordered: DDLTableConfig[];\n deferred: Map<DDLTableConfig, Reference[]>;\n} {\n const names = new Set(tables.map((table) => table.name));\n const byName = new Map(tables.map((table) => [table.name, table]));\n const created = new Set<string>();\n const remaining = [...tables];\n const ordered: DDLTableConfig[] = [];\n const deferred = new Map<DDLTableConfig, Reference[]>();\n const pendingOf = (table: DDLTableConfig) =>\n tableReferences(table)\n .filter((r) => isConstraintReference(r, byName))\n .filter((r) => names.has(r.table) && r.table !== table.name && !created.has(r.table));\n while (remaining.length > 0) {\n const index = remaining.findIndex((table) => pendingOf(table).length === 0);\n const [table] = remaining.splice(Math.max(index, 0), 1) as [DDLTableConfig];\n const pending = pendingOf(table);\n if (pending.length > 0) deferred.set(table, pending);\n created.add(table.name);\n ordered.push(table);\n }\n return { ordered, deferred };\n}\n\n/**\n * A single SQL script creating every given table, for `pglite.exec()`. Tables\n * are created after the tables they reference.\n * @param tables - Tables to create\n * @returns The script, empty when there are no tables\n * @throws If a field references a table or field that is not among the tables\n */\nexport function generateSchemaDDL(tables: readonly DDLTableConfig[]): string {\n const { ordered, deferred } = planCreation(tables);\n const statements = ordered.map((table) =>\n generateTableDDL(table, {\n tables,\n deferredReferences: new Set(deferred.get(table)?.map((r) => r.column)),\n }),\n );\n const constraints = ordered.flatMap((table) =>\n (deferred.get(table) ?? []).map((reference) => addForeignKey(table, reference)),\n );\n return [...statements, constraints]\n .filter((group) => group.length > 0)\n .map((group) => group.map((statement) => `${statement};`).join(\"\\n\"))\n .join(\"\\n\\n\");\n}\n\n/** One namespace's tables, as the schema module groups them. */\nexport interface PgliteSchemaNamespace {\n namespace: string;\n tables: readonly DDLTableConfig[];\n}\n\n/** Options for {@link generatePgliteSchemaModule}. */\nexport interface PgliteSchemaModuleOptions {\n /** Named in the generated header as the producer users must not edit around. */\n generatedBy: string;\n}\n\n// A template literal keeps line feeds but folds a carriage return into one.\nfunction templateLiteral(value: string): string {\n const escaped = value\n .replaceAll(\"\\\\\", \"\\\\\\\\\")\n .replaceAll(\"`\", \"\\\\`\")\n .replaceAll(\"${\", \"\\\\${\")\n .replaceAll(\"\\r\", \"\\\\r\");\n return `\\`${escaped}\\``;\n}\n\n/**\n * Render the module that exports each namespace's `CREATE TABLE` script for\n * `pglite.exec()`.\n * @param namespaces - Namespaces with the tables to create\n * @param options - Header wording\n * @returns TypeScript source of the schema module\n */\nexport function generatePgliteSchemaModule(\n namespaces: readonly PgliteSchemaNamespace[],\n options: PgliteSchemaModuleOptions,\n): string {\n const entries = namespaces.map(\n ({ namespace, tables }) =>\n ` ${JSON.stringify(namespace)}: ${templateLiteral(generateSchemaDDL(tables))},`,\n );\n return [\n \"/**\",\n \" * Auto-generated PGlite schema for TailorDB tables.\",\n \" * Create a namespace's tables in a test with `pglite.exec(pgliteSchema[namespace])`.\",\n \" *\",\n ` * DO NOT EDIT - This file is auto-generated by ${options.generatedBy}.`,\n \" */\",\n \"\",\n \"export const pgliteSchema = {\",\n ...entries,\n \"} as const;\",\n \"\",\n ].join(\"\\n\");\n}\n","import { generatePgliteSchemaModule, type DDLTableConfig } from \"#/utils/tailordb-ddl\";\nimport type { TailorDBType } from \"#/parser/service/tailordb/types\";\n\n/** Tables of one namespace, as the schema module groups them. */\nexport interface PGliteSchemaNamespace {\n namespace: string;\n tables: Record<string, TailorDBType>;\n}\n\n/**\n * Reduce parsed tables to what the DDL generator reads.\n * @param tables - Parsed TailorDB tables keyed by name\n * @returns DDL table configs in the same order\n */\nexport function toDDLTables(tables: Record<string, TailorDBType>): DDLTableConfig[] {\n return Object.values(tables).map((type) => ({\n name: type.name,\n fields: Object.fromEntries(\n Object.entries(type.fields).map(([fieldName, field]) => [fieldName, field.config]),\n ),\n indexes: type.indexes,\n }));\n}\n\n/**\n * Render the module that exports each namespace's `CREATE TABLE` script for\n * `pglite.exec()`.\n * @param namespaces - Namespaces with their parsed tables\n * @returns TypeScript source of the schema module\n */\nexport function generatePGliteSchemaModule(namespaces: readonly PGliteSchemaNamespace[]): string {\n return generatePgliteSchemaModule(\n namespaces.map(({ namespace, tables }) => ({ namespace, tables: toDDLTables(tables) })),\n { generatedBy: \"the kysely-type plugin\" },\n );\n}\n","/** The Kysely column type a TailorDB field maps to, before array/null modifiers. */\nexport type FieldColumnType =\n | \"string\"\n | \"number\"\n | \"boolean\"\n | \"Timestamp\"\n | \"TemporalDate\"\n | \"TemporalInstant\"\n | \"TemporalTime\";\n\n/**\n * Map a scalar TailorDB field type to the column type generated code uses for it.\n *\n * With `temporal: false` (the default), `date`/`datetime` resolve to `Timestamp` and\n * `time` to `string`, because that's what the function runtime hands back for them by\n * default. With `temporal: true`, `date`/`datetime`/`time` resolve to their\n * `Temporal.PlainDate`/`Temporal.Instant`/`Temporal.PlainTime` counterparts instead,\n * matching a `getDB` created with `createGetDB({ temporal: true })`.\n *\n * `enum` and `nested` carry their own shape, so each generator resolves them\n * before reaching here; passing either is a caller bug rather than a `string`\n * column.\n * @param fieldType - TailorDB scalar field type name\n * @param temporal - Whether to map date/datetime/time fields to their Temporal column\n * types instead of their `Date`/`string` defaults\n * @returns The column type for the field, defaulting to `string`\n * @throws If given `enum` or `nested`\n */\nexport function mapFieldTypeToColumnType(fieldType: string, temporal = false): FieldColumnType {\n switch (fieldType) {\n case \"uuid\":\n case \"string\":\n case \"decimal\":\n return \"string\";\n case \"integer\":\n case \"float\":\n return \"number\";\n case \"date\":\n return temporal ? \"TemporalDate\" : \"Timestamp\";\n case \"datetime\":\n return temporal ? \"TemporalInstant\" : \"Timestamp\";\n case \"time\":\n return temporal ? \"TemporalTime\" : \"string\";\n case \"bool\":\n case \"boolean\":\n return \"boolean\";\n case \"enum\":\n case \"nested\":\n throw new Error(\n `Field type \"${fieldType}\" has no scalar column type; resolve it before mapping.`,\n );\n default:\n return \"string\";\n }\n}\n\n/** The select and write types a `ColumnType`-shaped alias expands to. */\nexport type ColumnTypeAliasExpansion = {\n /** Type the alias reads back as. */\n readonly select: string;\n /** Type the alias accepts on insert and update. */\n readonly write: string;\n};\n\n/**\n * Column types whose alias expands to a `ColumnType`, mapped to that expansion.\n *\n * Kysely only unwraps a `ColumnType` at the top level of a table property, so an\n * array of one of these stays wrapped in `ArrayColumnType<...>` rather than taking\n * a `[]` suffix, which would nest the `ColumnType` out of Kysely's reach. Where a\n * generator has to spell out the slots itself, it reads the expansion from here\n * rather than inlining the alias, which would nest just the same.\n *\n * Both type generators track alias usage per alias to decide which declarations to\n * emit, so adding an entry here also means teaching them to report the new alias.\n */\nexport const COLUMN_TYPE_ALIASES: ReadonlyMap<string, ColumnTypeAliasExpansion> = new Map<\n FieldColumnType,\n ColumnTypeAliasExpansion\n>([\n [\"Timestamp\", { select: \"Date\", write: \"Date | string\" }],\n [\"TemporalDate\", { select: \"Temporal.PlainDate\", write: \"Temporal.PlainDate | string\" }],\n [\"TemporalInstant\", { select: \"Temporal.Instant\", write: \"Temporal.Instant | string\" }],\n [\"TemporalTime\", { select: \"Temporal.PlainTime\", write: \"Temporal.PlainTime | string\" }],\n]);\n","import { COLUMN_TYPE_ALIASES, mapFieldTypeToColumnType } from \"#/utils/field-column-type\";\nimport multiline from \"#/utils/multiline\";\nimport {\n type KyselyFieldConfig,\n type KyselyNamespaceMetadata,\n type KyselyTypeMetadata,\n type UsedUtilityTypes,\n} from \"./types\";\nimport type { TailorDBType } from \"#/parser/service/tailordb/types\";\n\ntype FieldTypeResult = {\n type: string;\n usedUtilityTypes: UsedUtilityTypes;\n};\n\nfunction emptyUsedUtilityTypes(): UsedUtilityTypes {\n return {\n Timestamp: false,\n TemporalDate: false,\n TemporalInstant: false,\n TemporalTime: false,\n Serial: false,\n ObjectColumnType: false,\n ArrayColumnType: false,\n };\n}\n\nfunction mergeUsedUtilityTypes(a: UsedUtilityTypes, b: UsedUtilityTypes): UsedUtilityTypes {\n return {\n Timestamp: a.Timestamp || b.Timestamp,\n TemporalDate: a.TemporalDate || b.TemporalDate,\n TemporalInstant: a.TemporalInstant || b.TemporalInstant,\n TemporalTime: a.TemporalTime || b.TemporalTime,\n Serial: a.Serial || b.Serial,\n ObjectColumnType: a.ObjectColumnType || b.ObjectColumnType,\n ArrayColumnType: a.ArrayColumnType || b.ArrayColumnType,\n };\n}\n\n/**\n * Whether a utility-type usage record uses any `ColumnType`-shaped alias.\n * @param usedUtilityTypes - Utility-type usage record to check\n * @returns Whether any `ColumnType`-shaped alias is used\n */\nfunction usesColumnTypeAlias(usedUtilityTypes: UsedUtilityTypes): boolean {\n return (\n usedUtilityTypes.Timestamp ||\n usedUtilityTypes.TemporalDate ||\n usedUtilityTypes.TemporalInstant ||\n usedUtilityTypes.TemporalTime\n );\n}\n\n/**\n * Get the enum type definition.\n * @param fieldConfig - The field configuration\n * @returns The enum type as a string union\n */\nfunction getEnumType(fieldConfig: KyselyFieldConfig): string {\n const allowedValues = fieldConfig.allowedValues;\n\n if (allowedValues && Array.isArray(allowedValues)) {\n return allowedValues\n .map((v: string | { value: string }) => {\n const value = typeof v === \"string\" ? v : v.value;\n return `\"${value}\"`;\n })\n .join(\" | \");\n }\n return \"string\";\n}\n\n/**\n * Get the nested object type definition.\n * @param fieldConfig - The field configuration\n * @param temporal - Whether date/datetime/time fields resolve to their Temporal column\n * types instead of their `Date`/`string` defaults\n * @returns The nested type with used utility types\n */\nfunction getNestedType(fieldConfig: KyselyFieldConfig, temporal: boolean): FieldTypeResult {\n const fields = fieldConfig.fields;\n if (!fields || typeof fields !== \"object\") {\n return {\n type: \"string\",\n usedUtilityTypes: emptyUsedUtilityTypes(),\n };\n }\n\n const fieldResults = Object.entries(fields).map(([fieldName, config]) => {\n const result = generateFieldType(config, temporal, true);\n const optional = config.required !== true ? \"?\" : \"\";\n return {\n fieldType: `${fieldName}${optional}: ${result.type}`,\n usedUtilityTypes: result.usedUtilityTypes,\n };\n });\n\n const aggregatedUtilityTypes = fieldResults.reduce(\n (acc, result) => mergeUsedUtilityTypes(acc, result.usedUtilityTypes),\n emptyUsedUtilityTypes(),\n );\n\n const fieldTypes = fieldResults.map((r) => r.fieldType);\n const obj = `{\\n ${fieldTypes.join(\";\\n \")}${fieldTypes.length > 0 ? \";\" : \"\"}\\n}`;\n\n const hasOptionalFields = Object.values(fields).some((config) => config.required !== true);\n const hasGeneratedFields = Object.values(fields).some(\n (config) =>\n config.hooks?.create || config.default !== undefined || config.optionalOnCreate === true,\n );\n if (usesColumnTypeAlias(aggregatedUtilityTypes) || hasOptionalFields || hasGeneratedFields) {\n return {\n type: `ObjectColumnType<${obj}>`,\n usedUtilityTypes: { ...aggregatedUtilityTypes, ObjectColumnType: true },\n };\n }\n return { type: obj, usedUtilityTypes: aggregatedUtilityTypes };\n}\n\n/**\n * Get the base Kysely type for a field (without array/null modifiers).\n * @param fieldConfig - The field configuration\n * @param temporal - Whether date/datetime/time fields resolve to their Temporal column\n * types instead of their `Date`/`string` defaults\n * @param nested - Whether the field is inside an object\n * @returns The base type with used utility types\n */\nfunction getBaseType(\n fieldConfig: KyselyFieldConfig,\n temporal: boolean,\n nested: boolean,\n): FieldTypeResult {\n const fieldType = fieldConfig.type;\n const usedUtilityTypes = emptyUsedUtilityTypes();\n\n if (fieldType === \"enum\") {\n return { type: getEnumType(fieldConfig), usedUtilityTypes };\n }\n if (fieldType === \"nested\") {\n return getNestedType(fieldConfig, temporal);\n }\n\n const type = mapFieldTypeToColumnType(fieldType, temporal && !(nested && fieldType === \"time\"));\n switch (type) {\n case \"Timestamp\":\n usedUtilityTypes.Timestamp = true;\n break;\n case \"TemporalDate\":\n usedUtilityTypes.TemporalDate = true;\n break;\n case \"TemporalInstant\":\n usedUtilityTypes.TemporalInstant = true;\n break;\n case \"TemporalTime\":\n usedUtilityTypes.TemporalTime = true;\n break;\n case \"string\":\n case \"number\":\n case \"boolean\":\n break;\n }\n\n return { type, usedUtilityTypes };\n}\n\n/**\n * Generate the complete field type including array and null modifiers.\n * @param fieldConfig - The field configuration\n * @param temporal - Whether date/datetime/time fields resolve to their Temporal column\n * types instead of their `Date`/`string` defaults\n * @param nested - Whether the field is inside an object\n * @returns The complete field type with used utility types\n */\nfunction generateFieldType(\n fieldConfig: KyselyFieldConfig,\n temporal: boolean,\n nested = false,\n): FieldTypeResult {\n const baseTypeResult = getBaseType(fieldConfig, temporal, nested);\n const usedUtilityTypes = { ...baseTypeResult.usedUtilityTypes };\n\n const isArray = fieldConfig.array === true;\n const isNullable = fieldConfig.required !== true;\n\n // A ColumnType-shaped alias and ObjectColumnType cannot be wrapped with [] for\n // arrays, because Kysely only resolves ColumnType at the top-level table\n // property. Use ArrayColumnType to keep the ColumnType at the top level.\n const isColumnTypeBase = COLUMN_TYPE_ALIASES.has(baseTypeResult.type);\n\n let finalType = baseTypeResult.type;\n if (isArray) {\n if (isColumnTypeBase || finalType.startsWith(\"ObjectColumnType<\")) {\n finalType = `ArrayColumnType<${baseTypeResult.type}>`;\n usedUtilityTypes.ArrayColumnType = true;\n } else {\n const needsParens = fieldConfig.type === \"enum\";\n finalType = needsParens ? `(${baseTypeResult.type})[]` : `${baseTypeResult.type}[]`;\n }\n }\n if (isNullable) {\n finalType = `${finalType} | null`;\n }\n\n if (fieldConfig.serial) {\n usedUtilityTypes.Serial = true;\n finalType = `Serial<${finalType}>`;\n }\n if (\n fieldConfig.hooks?.create ||\n fieldConfig.default !== undefined ||\n fieldConfig.optionalOnCreate === true\n ) {\n finalType = `Generated<${finalType}>`;\n }\n\n return { type: finalType, usedUtilityTypes };\n}\n\n/**\n * Generate the table interface.\n * @param name - Table name\n * @param fields - Field configurations keyed by field name\n * @param temporal - Whether date/datetime/time fields resolve to their Temporal column\n * types instead of their `Date`/`string` defaults\n * @returns The type definition and used utility types\n */\nfunction generateTableInterface(\n name: string,\n fields: Record<string, KyselyFieldConfig>,\n temporal: boolean,\n): {\n typeDef: string;\n usedUtilityTypes: UsedUtilityTypes;\n} {\n const fieldEntries = Object.entries(fields).filter(([fieldName]) => fieldName !== \"id\");\n\n const fieldResults = fieldEntries.map(([fieldName, fieldConfig]) => ({\n fieldName,\n ...generateFieldType(fieldConfig, temporal),\n }));\n\n const fieldLines = [\n \"id: Generated<string>;\",\n ...fieldResults.map((result) => `${result.fieldName}: ${result.type};`),\n ];\n\n const aggregatedUtilityTypes = fieldResults.reduce(\n (acc, result) => mergeUsedUtilityTypes(acc, result.usedUtilityTypes),\n emptyUsedUtilityTypes(),\n );\n\n const typeDef = multiline /* ts */ `\n ${name}: {\n ${fieldLines.join(\"\\n\")}\n }\n `;\n\n return { typeDef, usedUtilityTypes: aggregatedUtilityTypes };\n}\n\n/**\n * Generate KyselyTypeMetadata from field configurations.\n * @param name - Table name\n * @param fields - Field configurations keyed by field name\n * @param temporal - Whether date/datetime/time fields resolve to their Temporal column\n * types instead of their `Date`/`string` defaults. Defaults to `false`.\n * @returns Generated Kysely type metadata\n */\nexport function processKyselyFields(\n name: string,\n fields: Record<string, KyselyFieldConfig>,\n temporal = false,\n): KyselyTypeMetadata {\n const result = generateTableInterface(name, fields, temporal);\n\n return {\n name,\n typeDef: result.typeDef,\n usedUtilityTypes: result.usedUtilityTypes,\n };\n}\n\n/**\n * Convert a TailorDBType into KyselyTypeMetadata.\n * @param type - Parsed TailorDB table\n * @param temporal - Whether date/datetime/time fields resolve to their Temporal column\n * types instead of their `Date`/`string` defaults. Defaults to `false`.\n * @returns Generated Kysely type metadata\n */\nexport async function processKyselyType(\n type: TailorDBType,\n temporal = false,\n): Promise<KyselyTypeMetadata> {\n return processKyselyFields(\n type.name,\n Object.fromEntries(\n Object.entries(type.fields).map(([fieldName, parsedField]) => [\n fieldName,\n parsedField.config,\n ]),\n ),\n temporal,\n );\n}\n\n/**\n * Generate unified types file from multiple namespaces.\n * @param namespaceData - Namespace metadata\n * @param temporal - Whether `kyselyTypePlugin` was configured with `{ temporal: true }`;\n * passed to `createGetDB` so the generated `getDB` always reads back the Temporal values\n * the tables above are typed with. Defaults to `false`.\n * @returns Generated types file contents\n */\nexport function generateUnifiedKyselyTypes(\n namespaceData: KyselyNamespaceMetadata[],\n temporal = false,\n): string {\n if (namespaceData.length === 0) {\n return \"\";\n }\n\n // Aggregate used utility types from all namespaces\n const globalUsedUtilityTypes = namespaceData\n .flatMap((ns) => ns.types)\n .reduce(\n (acc, type) => mergeUsedUtilityTypes(acc, type.usedUtilityTypes),\n emptyUsedUtilityTypes(),\n );\n\n const utilityTypeImports: string[] = [\"type Generated\"];\n if (globalUsedUtilityTypes.Timestamp) {\n utilityTypeImports.push(\"type Timestamp\");\n }\n if (globalUsedUtilityTypes.TemporalDate) {\n utilityTypeImports.push(\"type TemporalDate\");\n }\n if (globalUsedUtilityTypes.TemporalInstant) {\n utilityTypeImports.push(\"type TemporalInstant\");\n }\n if (globalUsedUtilityTypes.TemporalTime) {\n utilityTypeImports.push(\"type TemporalTime\");\n }\n if (globalUsedUtilityTypes.ObjectColumnType) {\n utilityTypeImports.push(\"type ObjectColumnType\");\n }\n if (globalUsedUtilityTypes.ArrayColumnType) {\n utilityTypeImports.push(\"type ArrayColumnType\");\n }\n if (globalUsedUtilityTypes.Serial) {\n utilityTypeImports.push(\"type Serial\");\n }\n\n const importsSection = multiline /* ts */ `\n import {\n createGetDB,\n ${utilityTypeImports.join(\",\\n\")},\n type NamespaceDB,\n type NamespaceInsertable,\n type NamespaceSelectable,\n type NamespaceTable,\n type NamespaceTableName,\n type NamespaceTransaction,\n type NamespaceUpdateable,\n } from \"@tailor-platform/sdk/kysely\";\n `;\n\n // Generate Namespace interface with multiple namespaces\n const namespaceInterfaces = namespaceData\n .map(({ namespace, types }) => {\n const typeDefsWithIndent = types\n .map((type) => {\n return type.typeDef\n .split(\"\\n\")\n .map((line) => (line.trim() ? ` ${line}` : \"\"))\n .join(\"\\n\");\n })\n .join(\"\\n\\n\");\n\n return ` \"${namespace}\": {\\n${typeDefsWithIndent}\\n }`;\n })\n .join(\",\\n\");\n\n const namespaceInterface = `export interface Namespace {\\n${namespaceInterfaces}\\n}`;\n\n const getDBFunction = multiline /* ts */ `\n export const getDB = createGetDB<Namespace>(${temporal ? \"{ temporal: true }\" : \"\"});\n\n export type DB<N extends keyof Namespace = keyof Namespace> = NamespaceDB<Namespace, N>;\n `;\n\n const utilityTypeExports = multiline /* ts */ `\n export type Transaction<K extends keyof Namespace | DB = keyof Namespace> =\n NamespaceTransaction<Namespace, K>;\n\n type TableName = NamespaceTableName<Namespace>;\n export type Table<T extends TableName> = NamespaceTable<Namespace, T>;\n\n export type Insertable<T extends TableName> = NamespaceInsertable<Namespace, T>;\n export type Selectable<T extends TableName> = NamespaceSelectable<Namespace, T>;\n export type Updateable<T extends TableName> = NamespaceUpdateable<Namespace, T>;\n `;\n\n return (\n [importsSection, namespaceInterface, getDBFunction, utilityTypeExports].join(\"\\n\\n\") + \"\\n\"\n );\n}\n","import { resolve } from \"pathe\";\nimport { generatePGliteSchemaModule } from \"./pglite-schema\";\nimport { processKyselyType, generateUnifiedKyselyTypes } from \"./type-processor\";\nimport type { Plugin, GeneratorResult, TailorDBReadyContext } from \"#/plugin/types\";\nimport type { KyselyTypeMetadata, KyselyNamespaceMetadata } from \"./types\";\n\n/** Unique identifier for the Kysely type generator plugin. */\nexport const KyselyGeneratorID = \"@tailor-platform/kysely-type\";\n\ntype KyselyTypePluginOptions = {\n distPath: string;\n pgliteSchemaPath?: string;\n /**\n * When `true`, generated tables map date/datetime fields and top-level time fields to\n * `Temporal.PlainDate`/`Temporal.Instant`/`Temporal.PlainTime` columns instead of\n * their `Date`/`string` defaults, and the generated `getDB` reads them back as those\n * Temporal values. Nested time fields remain strings. Defaults to `false`.\n */\n temporal?: boolean;\n};\n\n// Register this plugin's config type under its own id, via the package's\n// real public specifier, so callers can resolve it type-safely from a\n// `Plugin[]` array (see plugin/get-plugin-config.ts's resolvePluginConfig)\n// without importing KyselyTypePluginOptions, which stays unexported.\ndeclare module \"@tailor-platform/sdk/plugin\" {\n interface PluginConfigRegistry {\n \"@tailor-platform/kysely-type\": KyselyTypePluginOptions;\n }\n}\n\n/** Conventional output path used when `kyselyTypePlugin` has no `distPath` configured. */\nexport const DEFAULT_KYSELY_TYPES_DIST_PATH = \"./generated/tailordb.ts\";\n\n/**\n * Plugin that generates Kysely type definitions for TailorDB tables.\n * @param options - Plugin options\n * @param options.distPath - Output file path for generated types\n * @param options.pgliteSchemaPath - Output file path for the PGlite `CREATE TABLE` script module; omit to skip it\n * @param options.temporal - When `true`, generated tables map date/datetime fields and\n * top-level time fields to their `Temporal.PlainDate`/`Temporal.Instant`/\n * `Temporal.PlainTime` counterparts instead of `Date`/`string`. Nested time fields\n * remain strings. Defaults to `false`.\n * @returns Plugin instance with onTailorDBReady hook\n */\nexport function kyselyTypePlugin(\n options: KyselyTypePluginOptions,\n): Plugin<unknown, KyselyTypePluginOptions> {\n return {\n id: KyselyGeneratorID,\n description: \"Generates Kysely type definitions for TailorDB tables\",\n pluginConfig: options,\n\n async onTailorDBReady(\n ctx: TailorDBReadyContext<KyselyTypePluginOptions>,\n ): Promise<GeneratorResult> {\n const { distPath, pgliteSchemaPath, temporal } = ctx.pluginConfig;\n if (pgliteSchemaPath && resolve(distPath) === resolve(pgliteSchemaPath)) {\n throw new Error(\"distPath and pgliteSchemaPath must resolve to different files.\");\n }\n\n const allNamespaceData: KyselyNamespaceMetadata[] = [];\n\n for (const ns of ctx.tailordb) {\n const typeMetadataList: KyselyTypeMetadata[] = [];\n\n for (const type of Object.values(ns.tables)) {\n const metadata = await processKyselyType(type, temporal);\n typeMetadataList.push(metadata);\n }\n\n if (typeMetadataList.length === 0) continue;\n\n allNamespaceData.push({\n namespace: ns.namespace,\n types: typeMetadataList,\n });\n }\n\n const files: GeneratorResult[\"files\"] = [];\n if (allNamespaceData.length > 0) {\n const content = generateUnifiedKyselyTypes(allNamespaceData, temporal);\n files.push({\n path: ctx.pluginConfig.distPath,\n content,\n });\n if (ctx.pluginConfig.pgliteSchemaPath) {\n files.push({\n path: ctx.pluginConfig.pgliteSchemaPath,\n content: generatePGliteSchemaModule(\n ctx.tailordb.filter((ns) => Object.keys(ns.tables).length > 0),\n ),\n });\n }\n }\n\n return { files };\n },\n };\n}\n"],"mappings":"A,6EA8BA,MAKMC,EAAO,IAAI,YAQjB,SAAgB,2BAA2B,EAA2B,CACpE,OAAQC,E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MK,EAAyC,CAAC,EAEhD,IAAK,IAAMnF,KAAQ,OAAO,OAAOwE,EAAG,MAAM,EAAG,CAC3C,IAAMY,EAAW,MAAM,kBAAkBpF,EAAM6C,CAAQ,EACvDsC,EAAiB,KAAKC,CAAQ,CAChC,CAEID,EAAiB,SAAW,GAEhCD,EAAiB,KAAK,CACpB,UAAWV,EAAG,UACd,MAAOW,CACT,CAAC,CACH,CAEA,IAAME,EAAkC,CAAC,EACzC,GAAIH,EAAiB,OAAS,EAAG,CAC/B,IAAMI,EAAU,2BAA2BJ,EAAkBrC,CAAQ,EACrEwC,EAAM,KAAK,CACT,KAAMP,EAAI,aAAa,SACvB,SACF,CAAC,EACGA,EAAI,aAAa,kBACnBO,EAAM,KAAK,CACT,KAAMP,EAAI,aAAa,iBACvB,QAAS,2BACPA,EAAI,SAAS,OAAQ,GAAO,OAAO,KAAKN,EAAG,MAAM,CAAC,CAAC,OAAS,CAAC,CAC/D,CACF,CAAC,CAEL,CAEA,MAAO,CAAE,OAAM,CACjB,CACF,CACF,C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export{t as DEFAULT_KYSELY_TYPES_DIST_PATH,n as KyselyGeneratorID,r as kyselyTypePlugin}from"../../../kysely-type-
|
|
1
|
+
export{t as DEFAULT_KYSELY_TYPES_DIST_PATH,n as KyselyGeneratorID,r as kyselyTypePlugin}from"../../../kysely-type-B0ri1-Xs.mjs";
|