@prisma-next/target-postgres 0.4.0-dev.9 → 0.4.2

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.
Files changed (195) hide show
  1. package/dist/codec-ids-CojIXVf9.mjs +29 -0
  2. package/dist/codec-ids-CojIXVf9.mjs.map +1 -0
  3. package/dist/codec-ids.d.mts +28 -0
  4. package/dist/codec-ids.d.mts.map +1 -0
  5. package/dist/codec-ids.mjs +3 -0
  6. package/dist/codec-types.d.mts +42 -0
  7. package/dist/codec-types.d.mts.map +1 -0
  8. package/dist/codec-types.mjs +3 -0
  9. package/dist/codecs-BoahtY_Q.mjs +385 -0
  10. package/dist/codecs-BoahtY_Q.mjs.map +1 -0
  11. package/dist/codecs-D-F2KJqt.d.mts +299 -0
  12. package/dist/codecs-D-F2KJqt.d.mts.map +1 -0
  13. package/dist/codecs.d.mts +2 -0
  14. package/dist/codecs.mjs +3 -0
  15. package/dist/control.d.mts +1 -9
  16. package/dist/control.d.mts.map +1 -1
  17. package/dist/control.mjs +23 -5101
  18. package/dist/control.mjs.map +1 -1
  19. package/dist/data-transform-CxFRBIUp.d.mts +32 -0
  20. package/dist/data-transform-CxFRBIUp.d.mts.map +1 -0
  21. package/dist/data-transform-VfEGzXWt.mjs +39 -0
  22. package/dist/data-transform-VfEGzXWt.mjs.map +1 -0
  23. package/dist/data-transform.d.mts +2 -0
  24. package/dist/data-transform.mjs +3 -0
  25. package/dist/default-normalizer-DNOpRoOF.mjs +131 -0
  26. package/dist/default-normalizer-DNOpRoOF.mjs.map +1 -0
  27. package/dist/default-normalizer.d.mts +19 -0
  28. package/dist/default-normalizer.d.mts.map +1 -0
  29. package/dist/default-normalizer.mjs +3 -0
  30. package/dist/{descriptor-meta-DkvCmY98.mjs → descriptor-meta-BVoVtyp-.mjs} +1 -1
  31. package/dist/{descriptor-meta-DkvCmY98.mjs.map → descriptor-meta-BVoVtyp-.mjs.map} +1 -1
  32. package/dist/errors-AFvEPZ1R.mjs +34 -0
  33. package/dist/errors-AFvEPZ1R.mjs.map +1 -0
  34. package/dist/errors.d.mts +27 -0
  35. package/dist/errors.d.mts.map +1 -0
  36. package/dist/errors.mjs +3 -0
  37. package/dist/issue-planner-CFjB0_oO.mjs +879 -0
  38. package/dist/issue-planner-CFjB0_oO.mjs.map +1 -0
  39. package/dist/issue-planner.d.mts +85 -0
  40. package/dist/issue-planner.d.mts.map +1 -0
  41. package/dist/issue-planner.mjs +3 -0
  42. package/dist/migration.d.mts +90 -0
  43. package/dist/migration.d.mts.map +1 -0
  44. package/dist/migration.mjs +24 -0
  45. package/dist/migration.mjs.map +1 -0
  46. package/dist/native-type-normalizer-CInai_oY.mjs +38 -0
  47. package/dist/native-type-normalizer-CInai_oY.mjs.map +1 -0
  48. package/dist/native-type-normalizer.d.mts +18 -0
  49. package/dist/native-type-normalizer.d.mts.map +1 -0
  50. package/dist/native-type-normalizer.mjs +3 -0
  51. package/dist/op-factory-call-BKlruaiC.mjs +605 -0
  52. package/dist/op-factory-call-BKlruaiC.mjs.map +1 -0
  53. package/dist/op-factory-call-C3bWXKSP.d.mts +304 -0
  54. package/dist/op-factory-call-C3bWXKSP.d.mts.map +1 -0
  55. package/dist/op-factory-call.d.mts +3 -0
  56. package/dist/op-factory-call.mjs +3 -0
  57. package/dist/pack.d.mts +1 -1
  58. package/dist/pack.mjs +1 -1
  59. package/dist/planner-CLUvVhUN.mjs +98 -0
  60. package/dist/planner-CLUvVhUN.mjs.map +1 -0
  61. package/dist/planner-ddl-builders-Dxvw1LHw.mjs +132 -0
  62. package/dist/planner-ddl-builders-Dxvw1LHw.mjs.map +1 -0
  63. package/dist/planner-ddl-builders.d.mts +22 -0
  64. package/dist/planner-ddl-builders.d.mts.map +1 -0
  65. package/dist/planner-ddl-builders.mjs +3 -0
  66. package/dist/planner-identity-values-Dju-o5GF.mjs +91 -0
  67. package/dist/planner-identity-values-Dju-o5GF.mjs.map +1 -0
  68. package/dist/planner-identity-values.d.mts +20 -0
  69. package/dist/planner-identity-values.d.mts.map +1 -0
  70. package/dist/planner-identity-values.mjs +3 -0
  71. package/dist/planner-produced-postgres-migration-CRRTno6Z.d.mts +20 -0
  72. package/dist/planner-produced-postgres-migration-CRRTno6Z.d.mts.map +1 -0
  73. package/dist/planner-produced-postgres-migration-DSSPq8QS.mjs +33 -0
  74. package/dist/planner-produced-postgres-migration-DSSPq8QS.mjs.map +1 -0
  75. package/dist/planner-produced-postgres-migration.d.mts +5 -0
  76. package/dist/planner-produced-postgres-migration.mjs +3 -0
  77. package/dist/planner-schema-lookup-B7lkypwn.mjs +29 -0
  78. package/dist/planner-schema-lookup-B7lkypwn.mjs.map +1 -0
  79. package/dist/planner-schema-lookup.d.mts +22 -0
  80. package/dist/planner-schema-lookup.d.mts.map +1 -0
  81. package/dist/planner-schema-lookup.mjs +3 -0
  82. package/dist/planner-sql-checks-7jkgm9TX.mjs +241 -0
  83. package/dist/planner-sql-checks-7jkgm9TX.mjs.map +1 -0
  84. package/dist/planner-sql-checks.d.mts +55 -0
  85. package/dist/planner-sql-checks.d.mts.map +1 -0
  86. package/dist/planner-sql-checks.mjs +3 -0
  87. package/dist/planner-target-details-DH-azLu-.d.mts +11 -0
  88. package/dist/planner-target-details-DH-azLu-.d.mts.map +1 -0
  89. package/dist/planner-target-details.d.mts +2 -0
  90. package/dist/planner-target-details.mjs +1 -0
  91. package/dist/planner.d.mts +68 -0
  92. package/dist/planner.d.mts.map +1 -0
  93. package/dist/planner.mjs +4 -0
  94. package/dist/postgres-migration-BjA3Zmts.d.mts +50 -0
  95. package/dist/postgres-migration-BjA3Zmts.d.mts.map +1 -0
  96. package/dist/postgres-migration-qtmtbONe.mjs +52 -0
  97. package/dist/postgres-migration-qtmtbONe.mjs.map +1 -0
  98. package/dist/render-ops-D6_DHdOK.mjs +8 -0
  99. package/dist/render-ops-D6_DHdOK.mjs.map +1 -0
  100. package/dist/render-ops.d.mts +11 -0
  101. package/dist/render-ops.d.mts.map +1 -0
  102. package/dist/render-ops.mjs +3 -0
  103. package/dist/render-typescript-1rF_SB4g.mjs +85 -0
  104. package/dist/render-typescript-1rF_SB4g.mjs.map +1 -0
  105. package/dist/render-typescript.d.mts +15 -0
  106. package/dist/render-typescript.d.mts.map +1 -0
  107. package/dist/render-typescript.mjs +3 -0
  108. package/dist/runtime.d.mts +15 -3
  109. package/dist/runtime.d.mts.map +1 -1
  110. package/dist/runtime.mjs +10 -1
  111. package/dist/runtime.mjs.map +1 -1
  112. package/dist/shared-Bxkt8pNO.d.mts +41 -0
  113. package/dist/shared-Bxkt8pNO.d.mts.map +1 -0
  114. package/dist/sql-utils-r-Lw535w.mjs +76 -0
  115. package/dist/sql-utils-r-Lw535w.mjs.map +1 -0
  116. package/dist/sql-utils.d.mts +59 -0
  117. package/dist/sql-utils.d.mts.map +1 -0
  118. package/dist/sql-utils.mjs +3 -0
  119. package/dist/statement-builders-BPnmt6wx.mjs +116 -0
  120. package/dist/statement-builders-BPnmt6wx.mjs.map +1 -0
  121. package/dist/statement-builders.d.mts +23 -0
  122. package/dist/statement-builders.d.mts.map +1 -0
  123. package/dist/statement-builders.mjs +3 -0
  124. package/dist/tables-BmdW_FWO.mjs +477 -0
  125. package/dist/tables-BmdW_FWO.mjs.map +1 -0
  126. package/dist/types-ClK03Ojd.d.mts +10 -0
  127. package/dist/types-ClK03Ojd.d.mts.map +1 -0
  128. package/dist/types.d.mts +2 -0
  129. package/dist/types.mjs +1 -0
  130. package/package.json +40 -18
  131. package/src/core/codec-ids.ts +30 -0
  132. package/src/core/codecs.ts +645 -0
  133. package/src/core/default-normalizer.ts +131 -0
  134. package/src/core/descriptor-meta.ts +1 -1
  135. package/src/core/errors.ts +33 -0
  136. package/src/core/json-schema-type-expression.ts +131 -0
  137. package/src/core/migrations/issue-planner.ts +832 -0
  138. package/src/core/migrations/op-factory-call.ts +858 -0
  139. package/src/core/migrations/operations/columns.ts +285 -0
  140. package/src/core/migrations/operations/constraints.ts +191 -0
  141. package/src/core/migrations/operations/data-transform.ts +119 -0
  142. package/src/core/migrations/operations/dependencies.ts +36 -0
  143. package/src/core/migrations/operations/enums.ts +113 -0
  144. package/src/core/migrations/operations/indexes.ts +61 -0
  145. package/src/core/migrations/operations/raw.ts +15 -0
  146. package/src/core/migrations/operations/shared.ts +67 -0
  147. package/src/core/migrations/operations/tables.ts +63 -0
  148. package/src/core/migrations/planner-ddl-builders.ts +1 -1
  149. package/src/core/migrations/planner-produced-postgres-migration.ts +67 -0
  150. package/src/core/migrations/planner-recipes.ts +1 -1
  151. package/src/core/migrations/planner-sql-checks.ts +1 -1
  152. package/src/core/migrations/planner-strategies.ts +592 -151
  153. package/src/core/migrations/planner-target-details.ts +0 -6
  154. package/src/core/migrations/planner.ts +65 -785
  155. package/src/core/migrations/postgres-migration.ts +73 -0
  156. package/src/core/migrations/render-ops.ts +9 -0
  157. package/src/core/migrations/render-typescript.ts +105 -0
  158. package/src/core/migrations/runner.ts +2 -4
  159. package/src/core/native-type-normalizer.ts +49 -0
  160. package/src/core/sql-utils.ts +104 -0
  161. package/src/exports/codec-ids.ts +1 -0
  162. package/src/exports/codec-types.ts +51 -0
  163. package/src/exports/codecs.ts +2 -0
  164. package/src/exports/control.ts +9 -142
  165. package/src/exports/data-transform.ts +1 -0
  166. package/src/exports/default-normalizer.ts +1 -0
  167. package/src/exports/errors.ts +1 -0
  168. package/src/exports/issue-planner.ts +1 -0
  169. package/src/exports/migration.ts +46 -0
  170. package/src/exports/native-type-normalizer.ts +1 -0
  171. package/src/exports/op-factory-call.ts +25 -0
  172. package/src/exports/planner-ddl-builders.ts +8 -0
  173. package/src/exports/planner-identity-values.ts +1 -0
  174. package/src/exports/planner-produced-postgres-migration.ts +1 -0
  175. package/src/exports/planner-schema-lookup.ts +6 -0
  176. package/src/exports/planner-sql-checks.ts +11 -0
  177. package/src/exports/planner-target-details.ts +1 -0
  178. package/src/exports/planner.ts +1 -0
  179. package/src/exports/render-ops.ts +1 -0
  180. package/src/exports/render-typescript.ts +1 -0
  181. package/src/exports/runtime.ts +19 -4
  182. package/src/exports/sql-utils.ts +7 -0
  183. package/src/exports/statement-builders.ts +7 -0
  184. package/src/exports/types.ts +1 -0
  185. package/dist/migration-builders.d.mts +0 -88
  186. package/dist/migration-builders.d.mts.map +0 -1
  187. package/dist/migration-builders.mjs +0 -3
  188. package/dist/operation-descriptors-CxymFSgK.mjs +0 -52
  189. package/dist/operation-descriptors-CxymFSgK.mjs.map +0 -1
  190. package/src/core/migrations/descriptor-planner.ts +0 -464
  191. package/src/core/migrations/operation-descriptors.ts +0 -166
  192. package/src/core/migrations/operation-resolver.ts +0 -929
  193. package/src/core/migrations/planner-reconciliation.ts +0 -798
  194. package/src/core/migrations/scaffolding.ts +0 -140
  195. package/src/exports/migration-builders.ts +0 -56
@@ -0,0 +1,131 @@
1
+ import type { ColumnDefault } from '@prisma-next/contract/types';
2
+
3
+ /**
4
+ * Pre-compiled regex patterns for performance.
5
+ * These are compiled once at module load time rather than on each function call.
6
+ */
7
+ const NEXTVAL_PATTERN = /^nextval\s*\(/i;
8
+ const NOW_FUNCTION_PATTERN = /^(now\s*\(\s*\)|CURRENT_TIMESTAMP)$/i;
9
+ const CLOCK_TIMESTAMP_PATTERN = /^clock_timestamp\s*\(\s*\)$/i;
10
+ const TIMESTAMP_CAST_SUFFIX = /::timestamp(?:tz|\s+(?:with|without)\s+time\s+zone)?$/i;
11
+ const TEXT_CAST_SUFFIX = /::text$/i;
12
+ const NOW_LITERAL_PATTERN = /^'now'$/i;
13
+ const UUID_PATTERN = /^gen_random_uuid\s*\(\s*\)$/i;
14
+ const UUID_OSSP_PATTERN = /^uuid_generate_v4\s*\(\s*\)$/i;
15
+ const NULL_PATTERN = /^NULL(?:::.+)?$/i;
16
+ const TRUE_PATTERN = /^true$/i;
17
+ const FALSE_PATTERN = /^false$/i;
18
+ const NUMERIC_PATTERN = /^-?\d+(\.\d+)?$/;
19
+ const STRING_LITERAL_PATTERN = /^'((?:[^']|'')*)'(?:::(?:"[^"]+"|[\w\s]+)(?:\(\d+\))?)?$/;
20
+
21
+ /**
22
+ * Returns the canonical expression for a timestamp default function, or undefined
23
+ * if the expression is not a recognized timestamp default.
24
+ *
25
+ * Keeps now()/CURRENT_TIMESTAMP and clock_timestamp() distinct:
26
+ * - now(), CURRENT_TIMESTAMP, ('now'::text)::timestamp... → 'now()'
27
+ * - clock_timestamp(), clock_timestamp()::timestamptz → 'clock_timestamp()'
28
+ *
29
+ * These are semantically different in Postgres: now() returns the transaction
30
+ * start time (constant within a transaction), while clock_timestamp() returns
31
+ * the actual wall-clock time (can differ across rows in a single INSERT).
32
+ */
33
+ function canonicalizeTimestampDefault(expr: string): string | undefined {
34
+ if (NOW_FUNCTION_PATTERN.test(expr)) return 'now()';
35
+ if (CLOCK_TIMESTAMP_PATTERN.test(expr)) return 'clock_timestamp()';
36
+
37
+ if (!TIMESTAMP_CAST_SUFFIX.test(expr)) return undefined;
38
+
39
+ let inner = expr.replace(TIMESTAMP_CAST_SUFFIX, '').trim();
40
+
41
+ if (inner.startsWith('(') && inner.endsWith(')')) {
42
+ inner = inner.slice(1, -1).trim();
43
+ }
44
+
45
+ if (NOW_FUNCTION_PATTERN.test(inner)) return 'now()';
46
+ if (CLOCK_TIMESTAMP_PATTERN.test(inner)) return 'clock_timestamp()';
47
+
48
+ inner = inner.replace(TEXT_CAST_SUFFIX, '').trim();
49
+ if (NOW_LITERAL_PATTERN.test(inner)) return 'now()';
50
+
51
+ return undefined;
52
+ }
53
+
54
+ /**
55
+ * Parses a raw Postgres column default expression into a normalized ColumnDefault.
56
+ * This enables semantic comparison between contract defaults and introspected schema defaults.
57
+ *
58
+ * Used by the migration diff layer to normalize raw database defaults during comparison,
59
+ * keeping the introspection layer focused on faithful data capture.
60
+ *
61
+ * @param rawDefault - Raw default expression from information_schema.columns.column_default
62
+ * @param nativeType - Native column type, used for type-aware parsing (bigint tagging, JSON detection)
63
+ * @returns Normalized ColumnDefault or undefined if the expression cannot be parsed
64
+ */
65
+ export function parsePostgresDefault(
66
+ rawDefault: string,
67
+ nativeType?: string,
68
+ ): ColumnDefault | undefined {
69
+ const trimmed = rawDefault.trim();
70
+ const normalizedType = nativeType?.toLowerCase();
71
+ const isBigInt = normalizedType === 'bigint' || normalizedType === 'int8';
72
+
73
+ if (NEXTVAL_PATTERN.test(trimmed)) {
74
+ return { kind: 'function', expression: 'autoincrement()' };
75
+ }
76
+
77
+ const canonicalTimestamp = canonicalizeTimestampDefault(trimmed);
78
+ if (canonicalTimestamp) {
79
+ return { kind: 'function', expression: canonicalTimestamp };
80
+ }
81
+
82
+ if (UUID_PATTERN.test(trimmed)) {
83
+ return { kind: 'function', expression: 'gen_random_uuid()' };
84
+ }
85
+
86
+ if (UUID_OSSP_PATTERN.test(trimmed)) {
87
+ return { kind: 'function', expression: 'gen_random_uuid()' };
88
+ }
89
+
90
+ if (NULL_PATTERN.test(trimmed)) {
91
+ return { kind: 'literal', value: null };
92
+ }
93
+
94
+ if (TRUE_PATTERN.test(trimmed)) {
95
+ return { kind: 'literal', value: true };
96
+ }
97
+ if (FALSE_PATTERN.test(trimmed)) {
98
+ return { kind: 'literal', value: false };
99
+ }
100
+
101
+ if (NUMERIC_PATTERN.test(trimmed)) {
102
+ const num = Number(trimmed);
103
+ if (!Number.isFinite(num)) return undefined;
104
+ if (isBigInt && !Number.isSafeInteger(num)) {
105
+ return { kind: 'literal', value: trimmed };
106
+ }
107
+ return { kind: 'literal', value: num };
108
+ }
109
+
110
+ const stringMatch = trimmed.match(STRING_LITERAL_PATTERN);
111
+ if (stringMatch?.[1] !== undefined) {
112
+ const unescaped = stringMatch[1].replace(/''/g, "'");
113
+ if (normalizedType === 'json' || normalizedType === 'jsonb') {
114
+ try {
115
+ return { kind: 'literal', value: JSON.parse(unescaped) };
116
+ } catch {
117
+ // Keep legacy behavior for malformed/non-JSON string content.
118
+ }
119
+ }
120
+ if (isBigInt && NUMERIC_PATTERN.test(unescaped)) {
121
+ const num = Number(unescaped);
122
+ if (Number.isSafeInteger(num)) {
123
+ return { kind: 'literal', value: num };
124
+ }
125
+ return { kind: 'literal', value: unescaped };
126
+ }
127
+ return { kind: 'literal', value: unescaped };
128
+ }
129
+
130
+ return { kind: 'function', expression: trimmed };
131
+ }
@@ -1,5 +1,5 @@
1
- import type { CodecTypes } from '@prisma-next/adapter-postgres/codec-types';
2
1
  import { postgresAuthoringFieldPresets, postgresAuthoringTypes } from './authoring';
2
+ import type { CodecTypes } from './codecs';
3
3
 
4
4
  const postgresTargetDescriptorMetaBase = {
5
5
  kind: 'target',
@@ -0,0 +1,33 @@
1
+ import { CliStructuredError } from '@prisma-next/errors/control';
2
+
3
+ /**
4
+ * A `PostgresMigration` instance method that needs the materialized control
5
+ * adapter (currently only `this.dataTransform(...)`) was invoked, but the
6
+ * migration was constructed without a `ControlStack`. Concrete authoring
7
+ * usage always goes through the migration CLI entrypoint, which assembles
8
+ * a stack from the loaded `prisma-next.config.ts`; reaching this error
9
+ * means a test fixture or ad-hoc consumer instantiated `PostgresMigration`
10
+ * with the no-arg form (legal for `operations` / `describe` introspection
11
+ * only).
12
+ *
13
+ * Distinct from `PN-MIG-2001` (placeholder not filled) and `PN-MIG-2005`
14
+ * (data-transform query plan against wrong contract) because the missing
15
+ * input is the stack itself, not the per-operation contract.
16
+ *
17
+ * Lives in `@prisma-next/target-postgres/errors` rather than the shared
18
+ * framework migration errors module because the failure is target-specific:
19
+ * the contract it talks about (`PostgresMigration`, the Postgres control
20
+ * adapter, the Postgres-target stack) only exists in this package.
21
+ */
22
+ export function errorPostgresMigrationStackMissing(): CliStructuredError {
23
+ return new CliStructuredError(
24
+ '2007',
25
+ 'PostgresMigration.dataTransform requires a control adapter',
26
+ {
27
+ domain: 'MIG',
28
+ why: 'PostgresMigration.dataTransform was invoked on an instance constructed without a ControlStack. The stored controlAdapter is undefined, so dataTransform cannot lower its query plan.',
29
+ fix: 'Construct the migration via the migration CLI entrypoint (which assembles a ControlStack from the loaded prisma-next.config.ts), or pass a ControlStack containing a Postgres adapter to the migration constructor in test fixtures.',
30
+ meta: {},
31
+ },
32
+ );
33
+ }
@@ -0,0 +1,131 @@
1
+ type JsonSchemaRecord = Record<string, unknown>;
2
+
3
+ const MAX_DEPTH = 32;
4
+
5
+ function isRecord(value: unknown): value is JsonSchemaRecord {
6
+ return typeof value === 'object' && value !== null;
7
+ }
8
+
9
+ function escapeStringLiteral(str: string): string {
10
+ return str
11
+ .replace(/\\/g, '\\\\')
12
+ .replace(/'/g, "\\'")
13
+ .replace(/\n/g, '\\n')
14
+ .replace(/\r/g, '\\r');
15
+ }
16
+
17
+ function quotePropertyKey(key: string): string {
18
+ return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key : `'${escapeStringLiteral(key)}'`;
19
+ }
20
+
21
+ function renderLiteral(value: unknown): string {
22
+ if (typeof value === 'string') {
23
+ return `'${escapeStringLiteral(value)}'`;
24
+ }
25
+ if (typeof value === 'number' || typeof value === 'boolean') {
26
+ return String(value);
27
+ }
28
+ if (value === null) {
29
+ return 'null';
30
+ }
31
+ return 'unknown';
32
+ }
33
+
34
+ function renderUnion(items: readonly unknown[], depth: number): string {
35
+ const rendered = items.map((item) => render(item, depth));
36
+ return rendered.join(' | ');
37
+ }
38
+
39
+ function renderObjectType(schema: JsonSchemaRecord, depth: number): string {
40
+ const properties = isRecord(schema['properties']) ? schema['properties'] : {};
41
+ const required = Array.isArray(schema['required'])
42
+ ? new Set(schema['required'].filter((key): key is string => typeof key === 'string'))
43
+ : new Set<string>();
44
+ const keys = Object.keys(properties).sort((left, right) => left.localeCompare(right));
45
+
46
+ if (keys.length === 0) {
47
+ const additionalProperties = schema['additionalProperties'];
48
+ if (additionalProperties === true || additionalProperties === undefined) {
49
+ return 'Record<string, unknown>';
50
+ }
51
+ return `Record<string, ${render(additionalProperties, depth)}>`;
52
+ }
53
+
54
+ const renderedProperties = keys.map((key) => {
55
+ const valueSchema = (properties as JsonSchemaRecord)[key];
56
+ const optionalMarker = required.has(key) ? '' : '?';
57
+ return `${quotePropertyKey(key)}${optionalMarker}: ${render(valueSchema, depth)}`;
58
+ });
59
+
60
+ return `{ ${renderedProperties.join('; ')} }`;
61
+ }
62
+
63
+ function renderArrayType(schema: JsonSchemaRecord, depth: number): string {
64
+ if (Array.isArray(schema['items'])) {
65
+ return `readonly [${schema['items'].map((item) => render(item, depth)).join(', ')}]`;
66
+ }
67
+
68
+ if (schema['items'] !== undefined) {
69
+ const itemType = render(schema['items'], depth);
70
+ const needsParens = itemType.includes(' | ') || itemType.includes(' & ');
71
+ return needsParens ? `(${itemType})[]` : `${itemType}[]`;
72
+ }
73
+
74
+ return 'unknown[]';
75
+ }
76
+
77
+ function render(schema: unknown, depth: number): string {
78
+ if (depth > MAX_DEPTH || !isRecord(schema)) {
79
+ return 'JsonValue';
80
+ }
81
+
82
+ const nextDepth = depth + 1;
83
+
84
+ if ('const' in schema) {
85
+ return renderLiteral(schema['const']);
86
+ }
87
+
88
+ if (Array.isArray(schema['enum'])) {
89
+ return schema['enum'].map((value) => renderLiteral(value)).join(' | ');
90
+ }
91
+
92
+ if (Array.isArray(schema['oneOf'])) {
93
+ return renderUnion(schema['oneOf'], nextDepth);
94
+ }
95
+
96
+ if (Array.isArray(schema['anyOf'])) {
97
+ return renderUnion(schema['anyOf'], nextDepth);
98
+ }
99
+
100
+ if (Array.isArray(schema['allOf'])) {
101
+ return schema['allOf'].map((item) => render(item, nextDepth)).join(' & ');
102
+ }
103
+
104
+ if (Array.isArray(schema['type'])) {
105
+ return schema['type'].map((item) => render({ ...schema, type: item }, nextDepth)).join(' | ');
106
+ }
107
+
108
+ switch (schema['type']) {
109
+ case 'string':
110
+ return 'string';
111
+ case 'number':
112
+ case 'integer':
113
+ return 'number';
114
+ case 'boolean':
115
+ return 'boolean';
116
+ case 'null':
117
+ return 'null';
118
+ case 'array':
119
+ return renderArrayType(schema, nextDepth);
120
+ case 'object':
121
+ return renderObjectType(schema, nextDepth);
122
+ default:
123
+ break;
124
+ }
125
+
126
+ return 'JsonValue';
127
+ }
128
+
129
+ export function renderTypeScriptTypeFromJsonSchema(schema: unknown): string {
130
+ return render(schema, 0);
131
+ }