@prisma-next/cli 0.3.0-dev.7 → 0.3.0-dev.71

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 (180) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +314 -80
  3. package/dist/cli-errors-JlPTsazx.mjs +3 -0
  4. package/dist/cli.d.mts +1 -0
  5. package/dist/cli.js +1 -2376
  6. package/dist/cli.mjs +198 -0
  7. package/dist/cli.mjs.map +1 -0
  8. package/dist/client-PimzSD1f.mjs +981 -0
  9. package/dist/client-PimzSD1f.mjs.map +1 -0
  10. package/dist/commands/contract-emit.d.mts +7 -0
  11. package/dist/commands/contract-emit.d.mts.map +1 -0
  12. package/dist/commands/contract-emit.mjs +150 -0
  13. package/dist/commands/contract-emit.mjs.map +1 -0
  14. package/dist/commands/db-init.d.mts +7 -0
  15. package/dist/commands/db-init.d.mts.map +1 -0
  16. package/dist/commands/db-init.mjs +132 -0
  17. package/dist/commands/db-init.mjs.map +1 -0
  18. package/dist/commands/db-introspect.d.mts +7 -0
  19. package/dist/commands/db-introspect.d.mts.map +1 -0
  20. package/dist/commands/db-introspect.mjs +117 -0
  21. package/dist/commands/db-introspect.mjs.map +1 -0
  22. package/dist/commands/db-schema-verify.d.mts +7 -0
  23. package/dist/commands/db-schema-verify.d.mts.map +1 -0
  24. package/dist/commands/db-schema-verify.mjs +119 -0
  25. package/dist/commands/db-schema-verify.mjs.map +1 -0
  26. package/dist/commands/db-sign.d.mts +7 -0
  27. package/dist/commands/db-sign.d.mts.map +1 -0
  28. package/dist/commands/db-sign.mjs +141 -0
  29. package/dist/commands/db-sign.mjs.map +1 -0
  30. package/dist/commands/db-update.d.mts +7 -0
  31. package/dist/commands/db-update.d.mts.map +1 -0
  32. package/dist/commands/db-update.mjs +121 -0
  33. package/dist/commands/db-update.mjs.map +1 -0
  34. package/dist/commands/db-verify.d.mts +7 -0
  35. package/dist/commands/db-verify.d.mts.map +1 -0
  36. package/dist/commands/db-verify.mjs +132 -0
  37. package/dist/commands/db-verify.mjs.map +1 -0
  38. package/dist/commands/migration-apply.d.mts +23 -0
  39. package/dist/commands/migration-apply.d.mts.map +1 -0
  40. package/dist/commands/migration-apply.mjs +249 -0
  41. package/dist/commands/migration-apply.mjs.map +1 -0
  42. package/dist/commands/migration-plan.d.mts +25 -0
  43. package/dist/commands/migration-plan.d.mts.map +1 -0
  44. package/dist/commands/migration-plan.mjs +266 -0
  45. package/dist/commands/migration-plan.mjs.map +1 -0
  46. package/dist/commands/migration-show.d.mts +28 -0
  47. package/dist/commands/migration-show.d.mts.map +1 -0
  48. package/dist/commands/migration-show.mjs +138 -0
  49. package/dist/commands/migration-show.mjs.map +1 -0
  50. package/dist/commands/migration-status.d.mts +35 -0
  51. package/dist/commands/migration-status.d.mts.map +1 -0
  52. package/dist/commands/migration-status.mjs +259 -0
  53. package/dist/commands/migration-status.mjs.map +1 -0
  54. package/dist/commands/migration-verify.d.mts +16 -0
  55. package/dist/commands/migration-verify.d.mts.map +1 -0
  56. package/dist/commands/migration-verify.mjs +86 -0
  57. package/dist/commands/migration-verify.mjs.map +1 -0
  58. package/dist/config-loader-PPf4CtDj.mjs +43 -0
  59. package/dist/config-loader-PPf4CtDj.mjs.map +1 -0
  60. package/dist/{config-loader.d.ts → config-loader.d.mts} +8 -3
  61. package/dist/config-loader.d.mts.map +1 -0
  62. package/dist/config-loader.mjs +3 -0
  63. package/dist/exports/config-types.d.mts +2 -0
  64. package/dist/exports/config-types.mjs +3 -0
  65. package/dist/exports/control-api.d.mts +621 -0
  66. package/dist/exports/control-api.d.mts.map +1 -0
  67. package/dist/exports/control-api.mjs +97 -0
  68. package/dist/exports/control-api.mjs.map +1 -0
  69. package/dist/{load-ts-contract.d.ts → exports/index.d.mts} +10 -5
  70. package/dist/exports/index.d.mts.map +1 -0
  71. package/dist/exports/index.mjs +130 -0
  72. package/dist/exports/index.mjs.map +1 -0
  73. package/dist/extract-sql-ddl-BmlKvk4o.mjs +26 -0
  74. package/dist/extract-sql-ddl-BmlKvk4o.mjs.map +1 -0
  75. package/dist/framework-components-CjV_jD8f.mjs +59 -0
  76. package/dist/framework-components-CjV_jD8f.mjs.map +1 -0
  77. package/dist/migration-command-scaffold-DfY_F3ev.mjs +97 -0
  78. package/dist/migration-command-scaffold-DfY_F3ev.mjs.map +1 -0
  79. package/dist/progress-adapter-DENrzF6I.mjs +49 -0
  80. package/dist/progress-adapter-DENrzF6I.mjs.map +1 -0
  81. package/dist/result-handler-iA9JtUC7.mjs +1186 -0
  82. package/dist/result-handler-iA9JtUC7.mjs.map +1 -0
  83. package/package.json +75 -38
  84. package/src/cli.ts +43 -0
  85. package/src/commands/contract-emit.ts +221 -111
  86. package/src/commands/db-init.ts +217 -426
  87. package/src/commands/db-introspect.ts +148 -185
  88. package/src/commands/db-schema-verify.ts +162 -149
  89. package/src/commands/db-sign.ts +215 -202
  90. package/src/commands/db-update.ts +220 -0
  91. package/src/commands/db-verify.ts +193 -156
  92. package/src/commands/migration-apply.ts +431 -0
  93. package/src/commands/migration-plan.ts +446 -0
  94. package/src/commands/migration-show.ts +255 -0
  95. package/src/commands/migration-status.ts +436 -0
  96. package/src/commands/migration-verify.ts +151 -0
  97. package/src/config-loader.ts +13 -3
  98. package/src/control-api/client.ts +605 -0
  99. package/src/control-api/errors.ts +9 -0
  100. package/src/control-api/operations/contract-emit.ts +161 -0
  101. package/src/control-api/operations/db-init.ts +286 -0
  102. package/src/control-api/operations/db-update.ts +221 -0
  103. package/src/control-api/operations/extract-sql-ddl.ts +47 -0
  104. package/src/control-api/operations/migration-apply.ts +195 -0
  105. package/src/control-api/operations/migration-helpers.ts +49 -0
  106. package/src/control-api/types.ts +687 -0
  107. package/src/exports/config-types.ts +3 -3
  108. package/src/exports/control-api.ts +53 -0
  109. package/src/load-ts-contract.ts +16 -11
  110. package/src/utils/cli-errors.ts +3 -1
  111. package/src/utils/command-helpers.ts +92 -3
  112. package/src/utils/framework-components.ts +11 -30
  113. package/src/utils/migration-command-scaffold.ts +190 -0
  114. package/src/utils/output.ts +363 -25
  115. package/src/utils/progress-adapter.ts +86 -0
  116. package/dist/chunk-464LNZCE.js +0 -134
  117. package/dist/chunk-464LNZCE.js.map +0 -1
  118. package/dist/chunk-BZMBKEEQ.js +0 -997
  119. package/dist/chunk-BZMBKEEQ.js.map +0 -1
  120. package/dist/chunk-HWYQOCAJ.js +0 -47
  121. package/dist/chunk-HWYQOCAJ.js.map +0 -1
  122. package/dist/chunk-ZKYEJROM.js +0 -94
  123. package/dist/chunk-ZKYEJROM.js.map +0 -1
  124. package/dist/cli.d.ts +0 -2
  125. package/dist/cli.d.ts.map +0 -1
  126. package/dist/cli.js.map +0 -1
  127. package/dist/commands/contract-emit.d.ts +0 -3
  128. package/dist/commands/contract-emit.d.ts.map +0 -1
  129. package/dist/commands/contract-emit.js +0 -9
  130. package/dist/commands/contract-emit.js.map +0 -1
  131. package/dist/commands/db-init.d.ts +0 -3
  132. package/dist/commands/db-init.d.ts.map +0 -1
  133. package/dist/commands/db-init.js +0 -341
  134. package/dist/commands/db-init.js.map +0 -1
  135. package/dist/commands/db-introspect.d.ts +0 -3
  136. package/dist/commands/db-introspect.d.ts.map +0 -1
  137. package/dist/commands/db-introspect.js +0 -190
  138. package/dist/commands/db-introspect.js.map +0 -1
  139. package/dist/commands/db-schema-verify.d.ts +0 -3
  140. package/dist/commands/db-schema-verify.d.ts.map +0 -1
  141. package/dist/commands/db-schema-verify.js +0 -164
  142. package/dist/commands/db-schema-verify.js.map +0 -1
  143. package/dist/commands/db-sign.d.ts +0 -3
  144. package/dist/commands/db-sign.d.ts.map +0 -1
  145. package/dist/commands/db-sign.js +0 -199
  146. package/dist/commands/db-sign.js.map +0 -1
  147. package/dist/commands/db-verify.d.ts +0 -3
  148. package/dist/commands/db-verify.d.ts.map +0 -1
  149. package/dist/commands/db-verify.js +0 -173
  150. package/dist/commands/db-verify.js.map +0 -1
  151. package/dist/config-loader.d.ts.map +0 -1
  152. package/dist/config-loader.js +0 -7
  153. package/dist/config-loader.js.map +0 -1
  154. package/dist/exports/config-types.d.ts +0 -3
  155. package/dist/exports/config-types.d.ts.map +0 -1
  156. package/dist/exports/config-types.js +0 -6
  157. package/dist/exports/config-types.js.map +0 -1
  158. package/dist/exports/index.d.ts +0 -4
  159. package/dist/exports/index.d.ts.map +0 -1
  160. package/dist/exports/index.js +0 -175
  161. package/dist/exports/index.js.map +0 -1
  162. package/dist/load-ts-contract.d.ts.map +0 -1
  163. package/dist/utils/action.d.ts +0 -16
  164. package/dist/utils/action.d.ts.map +0 -1
  165. package/dist/utils/cli-errors.d.ts +0 -7
  166. package/dist/utils/cli-errors.d.ts.map +0 -1
  167. package/dist/utils/command-helpers.d.ts +0 -12
  168. package/dist/utils/command-helpers.d.ts.map +0 -1
  169. package/dist/utils/framework-components.d.ts +0 -81
  170. package/dist/utils/framework-components.d.ts.map +0 -1
  171. package/dist/utils/global-flags.d.ts +0 -25
  172. package/dist/utils/global-flags.d.ts.map +0 -1
  173. package/dist/utils/output.d.ts +0 -142
  174. package/dist/utils/output.d.ts.map +0 -1
  175. package/dist/utils/result-handler.d.ts +0 -15
  176. package/dist/utils/result-handler.d.ts.map +0 -1
  177. package/dist/utils/spinner.d.ts +0 -29
  178. package/dist/utils/spinner.d.ts.map +0 -1
  179. package/src/utils/action.ts +0 -43
  180. package/src/utils/spinner.ts +0 -67
@@ -0,0 +1,605 @@
1
+ import type { TargetBoundComponentDescriptor } from '@prisma-next/contract/framework-components';
2
+ import type { ContractIR } from '@prisma-next/contract/ir';
3
+ import type { ContractMarkerRecord } from '@prisma-next/contract/types';
4
+ import type { CoreSchemaView } from '@prisma-next/core-control-plane/schema-view';
5
+ import { createControlPlaneStack } from '@prisma-next/core-control-plane/stack';
6
+ import type {
7
+ ControlDriverInstance,
8
+ ControlFamilyInstance,
9
+ ControlPlaneStack,
10
+ SignDatabaseResult,
11
+ VerifyDatabaseResult,
12
+ VerifyDatabaseSchemaResult,
13
+ } from '@prisma-next/core-control-plane/types';
14
+ import { ifDefined } from '@prisma-next/utils/defined';
15
+ import { notOk, ok } from '@prisma-next/utils/result';
16
+ import { assertFrameworkComponentsCompatible } from '../utils/framework-components';
17
+ import { ContractValidationError } from './errors';
18
+ import { executeDbInit } from './operations/db-init';
19
+ import { executeDbUpdate } from './operations/db-update';
20
+ import { executeMigrationApply } from './operations/migration-apply';
21
+ import type {
22
+ ControlActionName,
23
+ ControlClient,
24
+ ControlClientOptions,
25
+ DbInitOptions,
26
+ DbInitResult,
27
+ DbUpdateOptions,
28
+ DbUpdateResult,
29
+ EmitOptions,
30
+ EmitResult,
31
+ IntrospectOptions,
32
+ MigrationApplyOptions,
33
+ MigrationApplyResult,
34
+ OnControlProgress,
35
+ SchemaVerifyOptions,
36
+ SignOptions,
37
+ VerifyOptions,
38
+ } from './types';
39
+
40
+ /**
41
+ * Creates a programmatic control client for Prisma Next operations.
42
+ *
43
+ * The client accepts framework component descriptors at creation time,
44
+ * manages driver lifecycle via connect()/close(), and exposes domain
45
+ * operations that delegate to the existing family instance methods.
46
+ *
47
+ * @see {@link ControlClient} for the client interface
48
+ * @see README.md "Programmatic Control API" section for usage examples
49
+ */
50
+ export function createControlClient(options: ControlClientOptions): ControlClient {
51
+ return new ControlClientImpl(options);
52
+ }
53
+
54
+ /**
55
+ * Implementation of ControlClient.
56
+ * Manages initialization and connection state, delegates operations to family instance.
57
+ */
58
+ class ControlClientImpl implements ControlClient {
59
+ private readonly options: ControlClientOptions;
60
+ private stack: ControlPlaneStack<string, string> | null = null;
61
+ private driver: ControlDriverInstance<string, string> | null = null;
62
+ private familyInstance: ControlFamilyInstance<string> | null = null;
63
+ private frameworkComponents: ReadonlyArray<
64
+ TargetBoundComponentDescriptor<string, string>
65
+ > | null = null;
66
+ private initialized = false;
67
+ private readonly defaultConnection: unknown;
68
+
69
+ constructor(options: ControlClientOptions) {
70
+ this.options = options;
71
+ this.defaultConnection = options.connection;
72
+ }
73
+
74
+ init(): void {
75
+ if (this.initialized) {
76
+ return; // Idempotent
77
+ }
78
+
79
+ // Create the control plane stack
80
+ this.stack = createControlPlaneStack({
81
+ target: this.options.target,
82
+ adapter: this.options.adapter,
83
+ driver: this.options.driver,
84
+ extensionPacks: this.options.extensionPacks,
85
+ });
86
+
87
+ // Create family instance using the stack
88
+ this.familyInstance = this.options.family.create(this.stack);
89
+
90
+ // Validate and type-narrow framework components
91
+ const rawComponents = [
92
+ this.options.target,
93
+ this.options.adapter,
94
+ ...(this.options.extensionPacks ?? []),
95
+ ];
96
+ this.frameworkComponents = assertFrameworkComponentsCompatible(
97
+ this.options.family.familyId,
98
+ this.options.target.targetId,
99
+ rawComponents,
100
+ );
101
+
102
+ this.initialized = true;
103
+ }
104
+
105
+ async connect(connection?: unknown): Promise<void> {
106
+ // Auto-init if needed
107
+ this.init();
108
+
109
+ if (this.driver) {
110
+ throw new Error('Already connected. Call close() before reconnecting.');
111
+ }
112
+
113
+ // Resolve connection: argument > default from options
114
+ const resolvedConnection = connection ?? this.defaultConnection;
115
+ if (resolvedConnection === undefined) {
116
+ throw new Error(
117
+ 'No connection provided. Pass a connection to connect() or provide a default connection when creating the client.',
118
+ );
119
+ }
120
+
121
+ // Check for driver descriptor
122
+ if (!this.stack?.driver) {
123
+ throw new Error(
124
+ 'Driver is not configured. Pass a driver descriptor when creating the control client to enable database operations.',
125
+ );
126
+ }
127
+
128
+ // Create driver instance
129
+ // Cast through any since connection type is driver-specific at runtime.
130
+ // The driver descriptor is typed with any for TConnection in ControlClientOptions,
131
+ // but createControlPlaneStack defaults it to string. We bridge this at runtime.
132
+ // biome-ignore lint/suspicious/noExplicitAny: required for runtime connection type flexibility
133
+ this.driver = await this.stack?.driver.create(resolvedConnection as any);
134
+ }
135
+
136
+ async close(): Promise<void> {
137
+ if (this.driver) {
138
+ await this.driver.close();
139
+ this.driver = null;
140
+ }
141
+ }
142
+
143
+ private async ensureConnected(): Promise<{
144
+ driver: ControlDriverInstance<string, string>;
145
+ familyInstance: ControlFamilyInstance<string>;
146
+ frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<string, string>>;
147
+ }> {
148
+ // Auto-init if needed
149
+ this.init();
150
+
151
+ // Auto-connect if not connected and default connection is available
152
+ if (!this.driver && this.defaultConnection !== undefined) {
153
+ await this.connect(this.defaultConnection);
154
+ }
155
+
156
+ if (!this.driver || !this.familyInstance || !this.frameworkComponents) {
157
+ throw new Error('Not connected. Call connect(connection) first.');
158
+ }
159
+ return {
160
+ driver: this.driver,
161
+ familyInstance: this.familyInstance,
162
+ frameworkComponents: this.frameworkComponents,
163
+ };
164
+ }
165
+
166
+ private async connectWithProgress(
167
+ connection: unknown | undefined,
168
+ action: ControlActionName,
169
+ onProgress?: OnControlProgress,
170
+ ): Promise<void> {
171
+ if (connection === undefined) return;
172
+ onProgress?.({
173
+ action,
174
+ kind: 'spanStart',
175
+ spanId: 'connect',
176
+ label: 'Connecting to database...',
177
+ });
178
+ try {
179
+ await this.connect(connection);
180
+ onProgress?.({ action, kind: 'spanEnd', spanId: 'connect', outcome: 'ok' });
181
+ } catch (error) {
182
+ onProgress?.({ action, kind: 'spanEnd', spanId: 'connect', outcome: 'error' });
183
+ throw error;
184
+ }
185
+ }
186
+
187
+ async verify(options: VerifyOptions): Promise<VerifyDatabaseResult> {
188
+ const { onProgress } = options;
189
+ await this.connectWithProgress(options.connection, 'verify', onProgress);
190
+ const { driver, familyInstance } = await this.ensureConnected();
191
+
192
+ // Validate contract using family instance
193
+ let contractIR: ContractIR;
194
+ try {
195
+ contractIR = familyInstance.validateContractIR(options.contractIR);
196
+ } catch (error) {
197
+ const message = error instanceof Error ? error.message : String(error);
198
+ throw new ContractValidationError(message, error);
199
+ }
200
+
201
+ // Emit verify span
202
+ onProgress?.({
203
+ action: 'verify',
204
+ kind: 'spanStart',
205
+ spanId: 'verify',
206
+ label: 'Verifying database signature...',
207
+ });
208
+
209
+ try {
210
+ // Delegate to family instance verify method
211
+ // Note: We pass empty strings for contractPath/configPath since the programmatic
212
+ // API doesn't deal with file paths. The family instance accepts these as optional
213
+ // metadata for error reporting.
214
+ const result = await familyInstance.verify({
215
+ driver,
216
+ contractIR,
217
+ expectedTargetId: this.options.target.targetId,
218
+ contractPath: '',
219
+ });
220
+
221
+ onProgress?.({
222
+ action: 'verify',
223
+ kind: 'spanEnd',
224
+ spanId: 'verify',
225
+ outcome: result.ok ? 'ok' : 'error',
226
+ });
227
+
228
+ return result;
229
+ } catch (error) {
230
+ onProgress?.({
231
+ action: 'verify',
232
+ kind: 'spanEnd',
233
+ spanId: 'verify',
234
+ outcome: 'error',
235
+ });
236
+ throw error;
237
+ }
238
+ }
239
+
240
+ async schemaVerify(options: SchemaVerifyOptions): Promise<VerifyDatabaseSchemaResult> {
241
+ const { onProgress } = options;
242
+ await this.connectWithProgress(options.connection, 'schemaVerify', onProgress);
243
+ const { driver, familyInstance, frameworkComponents } = await this.ensureConnected();
244
+
245
+ // Validate contract using family instance
246
+ let contractIR: ContractIR;
247
+ try {
248
+ contractIR = familyInstance.validateContractIR(options.contractIR);
249
+ } catch (error) {
250
+ const message = error instanceof Error ? error.message : String(error);
251
+ throw new ContractValidationError(message, error);
252
+ }
253
+
254
+ // Emit schemaVerify span
255
+ onProgress?.({
256
+ action: 'schemaVerify',
257
+ kind: 'spanStart',
258
+ spanId: 'schemaVerify',
259
+ label: 'Verifying database schema...',
260
+ });
261
+
262
+ try {
263
+ // Delegate to family instance schemaVerify method
264
+ const result = await familyInstance.schemaVerify({
265
+ driver,
266
+ contractIR,
267
+ strict: options.strict ?? false,
268
+ contractPath: '',
269
+ frameworkComponents,
270
+ });
271
+
272
+ onProgress?.({
273
+ action: 'schemaVerify',
274
+ kind: 'spanEnd',
275
+ spanId: 'schemaVerify',
276
+ outcome: result.ok ? 'ok' : 'error',
277
+ });
278
+
279
+ return result;
280
+ } catch (error) {
281
+ onProgress?.({
282
+ action: 'schemaVerify',
283
+ kind: 'spanEnd',
284
+ spanId: 'schemaVerify',
285
+ outcome: 'error',
286
+ });
287
+ throw error;
288
+ }
289
+ }
290
+
291
+ async sign(options: SignOptions): Promise<SignDatabaseResult> {
292
+ const { onProgress } = options;
293
+ await this.connectWithProgress(options.connection, 'sign', onProgress);
294
+ const { driver, familyInstance } = await this.ensureConnected();
295
+
296
+ // Validate contract using family instance
297
+ let contractIR: ContractIR;
298
+ try {
299
+ contractIR = familyInstance.validateContractIR(options.contractIR);
300
+ } catch (error) {
301
+ const message = error instanceof Error ? error.message : String(error);
302
+ throw new ContractValidationError(message, error);
303
+ }
304
+
305
+ // Emit sign span
306
+ onProgress?.({
307
+ action: 'sign',
308
+ kind: 'spanStart',
309
+ spanId: 'sign',
310
+ label: 'Signing database...',
311
+ });
312
+
313
+ try {
314
+ // Delegate to family instance sign method
315
+ const result = await familyInstance.sign({
316
+ driver,
317
+ contractIR,
318
+ contractPath: options.contractPath ?? '',
319
+ ...ifDefined('configPath', options.configPath),
320
+ });
321
+
322
+ onProgress?.({
323
+ action: 'sign',
324
+ kind: 'spanEnd',
325
+ spanId: 'sign',
326
+ outcome: 'ok',
327
+ });
328
+
329
+ return result;
330
+ } catch (error) {
331
+ onProgress?.({
332
+ action: 'sign',
333
+ kind: 'spanEnd',
334
+ spanId: 'sign',
335
+ outcome: 'error',
336
+ });
337
+ throw error;
338
+ }
339
+ }
340
+
341
+ async dbInit(options: DbInitOptions): Promise<DbInitResult> {
342
+ const { onProgress } = options;
343
+ await this.connectWithProgress(options.connection, 'dbInit', onProgress);
344
+ const { driver, familyInstance, frameworkComponents } = await this.ensureConnected();
345
+
346
+ // Check target supports migrations
347
+ if (!this.options.target.migrations) {
348
+ throw new Error(`Target "${this.options.target.targetId}" does not support migrations`);
349
+ }
350
+
351
+ // Validate contract using family instance
352
+ let contractIR: ContractIR;
353
+ try {
354
+ contractIR = familyInstance.validateContractIR(options.contractIR);
355
+ } catch (error) {
356
+ const message = error instanceof Error ? error.message : String(error);
357
+ throw new ContractValidationError(message, error);
358
+ }
359
+
360
+ // Delegate to extracted dbInit operation
361
+ return executeDbInit({
362
+ driver,
363
+ familyInstance,
364
+ contractIR,
365
+ mode: options.mode,
366
+ migrations: this.options.target.migrations,
367
+ frameworkComponents,
368
+ ...ifDefined('onProgress', onProgress),
369
+ });
370
+ }
371
+
372
+ async dbUpdate(options: DbUpdateOptions): Promise<DbUpdateResult> {
373
+ const { onProgress } = options;
374
+ await this.connectWithProgress(options.connection, 'dbUpdate', onProgress);
375
+ const { driver, familyInstance, frameworkComponents } = await this.ensureConnected();
376
+
377
+ if (!this.options.target.migrations) {
378
+ throw new Error(`Target "${this.options.target.targetId}" does not support migrations`);
379
+ }
380
+
381
+ let contractIR: ContractIR;
382
+ try {
383
+ contractIR = familyInstance.validateContractIR(options.contractIR);
384
+ } catch (error) {
385
+ const message = error instanceof Error ? error.message : String(error);
386
+ throw new ContractValidationError(message, error);
387
+ }
388
+
389
+ return executeDbUpdate({
390
+ driver,
391
+ familyInstance,
392
+ contractIR,
393
+ mode: options.mode,
394
+ migrations: this.options.target.migrations,
395
+ frameworkComponents,
396
+ ...ifDefined('acceptDataLoss', options.acceptDataLoss),
397
+ ...ifDefined('onProgress', onProgress),
398
+ });
399
+ }
400
+
401
+ async readMarker(): Promise<ContractMarkerRecord | null> {
402
+ const { driver, familyInstance } = await this.ensureConnected();
403
+ return familyInstance.readMarker({ driver });
404
+ }
405
+
406
+ async migrationApply(options: MigrationApplyOptions): Promise<MigrationApplyResult> {
407
+ const { onProgress } = options;
408
+ await this.connectWithProgress(options.connection, 'migrationApply', onProgress);
409
+ const { driver, familyInstance, frameworkComponents } = await this.ensureConnected();
410
+
411
+ if (!this.options.target.migrations) {
412
+ throw new Error(`Target "${this.options.target.targetId}" does not support migrations`);
413
+ }
414
+
415
+ return executeMigrationApply({
416
+ driver,
417
+ familyInstance,
418
+ originHash: options.originHash,
419
+ destinationHash: options.destinationHash,
420
+ pendingMigrations: options.pendingMigrations,
421
+ migrations: this.options.target.migrations,
422
+ frameworkComponents,
423
+ targetId: this.options.target.targetId,
424
+ ...(onProgress ? { onProgress } : {}),
425
+ });
426
+ }
427
+
428
+ async introspect(options?: IntrospectOptions): Promise<unknown> {
429
+ const onProgress = options?.onProgress;
430
+ await this.connectWithProgress(options?.connection, 'introspect', onProgress);
431
+ const { driver, familyInstance } = await this.ensureConnected();
432
+
433
+ // TODO: Pass schema option to familyInstance.introspect when schema filtering is implemented
434
+ const _schema = options?.schema;
435
+ void _schema;
436
+
437
+ // Emit introspect span
438
+ onProgress?.({
439
+ action: 'introspect',
440
+ kind: 'spanStart',
441
+ spanId: 'introspect',
442
+ label: 'Introspecting database schema...',
443
+ });
444
+
445
+ try {
446
+ const result = await familyInstance.introspect({ driver });
447
+
448
+ onProgress?.({
449
+ action: 'introspect',
450
+ kind: 'spanEnd',
451
+ spanId: 'introspect',
452
+ outcome: 'ok',
453
+ });
454
+
455
+ return result;
456
+ } catch (error) {
457
+ onProgress?.({
458
+ action: 'introspect',
459
+ kind: 'spanEnd',
460
+ spanId: 'introspect',
461
+ outcome: 'error',
462
+ });
463
+ throw error;
464
+ }
465
+ }
466
+
467
+ toSchemaView(schemaIR: unknown): CoreSchemaView | undefined {
468
+ this.init();
469
+ if (this.familyInstance?.toSchemaView) {
470
+ return this.familyInstance.toSchemaView(schemaIR);
471
+ }
472
+ return undefined;
473
+ }
474
+
475
+ async emit(options: EmitOptions): Promise<EmitResult> {
476
+ const { onProgress, contractConfig } = options;
477
+
478
+ // Ensure initialized (creates stack and family instance)
479
+ // emit() does NOT require a database connection
480
+ this.init();
481
+
482
+ if (!this.familyInstance) {
483
+ throw new Error('Family instance was not initialized. This is a bug.');
484
+ }
485
+
486
+ let contractRaw: unknown;
487
+ onProgress?.({
488
+ action: 'emit',
489
+ kind: 'spanStart',
490
+ spanId: 'resolveSource',
491
+ label: 'Resolving contract source...',
492
+ });
493
+
494
+ try {
495
+ const providerResult = await contractConfig.sourceProvider();
496
+ if (!providerResult.ok) {
497
+ onProgress?.({
498
+ action: 'emit',
499
+ kind: 'spanEnd',
500
+ spanId: 'resolveSource',
501
+ outcome: 'error',
502
+ });
503
+
504
+ return notOk({
505
+ code: 'CONTRACT_SOURCE_INVALID',
506
+ summary: providerResult.failure.summary,
507
+ why: providerResult.failure.summary,
508
+ meta: providerResult.failure.meta,
509
+ diagnostics: providerResult.failure,
510
+ });
511
+ }
512
+ contractRaw = providerResult.value;
513
+
514
+ onProgress?.({
515
+ action: 'emit',
516
+ kind: 'spanEnd',
517
+ spanId: 'resolveSource',
518
+ outcome: 'ok',
519
+ });
520
+ } catch (error) {
521
+ onProgress?.({
522
+ action: 'emit',
523
+ kind: 'spanEnd',
524
+ spanId: 'resolveSource',
525
+ outcome: 'error',
526
+ });
527
+
528
+ const message = error instanceof Error ? error.message : String(error);
529
+ return notOk({
530
+ code: 'CONTRACT_SOURCE_INVALID',
531
+ summary: 'Failed to resolve contract source',
532
+ why: message,
533
+ diagnostics: {
534
+ summary: 'Contract source provider threw an exception',
535
+ diagnostics: [
536
+ {
537
+ code: 'PROVIDER_THROW',
538
+ message,
539
+ },
540
+ ],
541
+ },
542
+ meta: undefined,
543
+ });
544
+ }
545
+
546
+ // Emit contract
547
+ onProgress?.({
548
+ action: 'emit',
549
+ kind: 'spanStart',
550
+ spanId: 'emit',
551
+ label: 'Emitting contract...',
552
+ });
553
+
554
+ try {
555
+ try {
556
+ this.familyInstance.validateContractIR(contractRaw);
557
+ } catch (error) {
558
+ onProgress?.({
559
+ action: 'emit',
560
+ kind: 'spanEnd',
561
+ spanId: 'emit',
562
+ outcome: 'error',
563
+ });
564
+ const message = error instanceof Error ? error.message : String(error);
565
+ return notOk({
566
+ code: 'CONTRACT_VALIDATION_FAILED',
567
+ summary: 'Contract validation failed',
568
+ why: message,
569
+ meta: undefined,
570
+ });
571
+ }
572
+
573
+ const emitResult = await this.familyInstance.emitContract({ contractIR: contractRaw });
574
+
575
+ onProgress?.({
576
+ action: 'emit',
577
+ kind: 'spanEnd',
578
+ spanId: 'emit',
579
+ outcome: 'ok',
580
+ });
581
+
582
+ return ok({
583
+ storageHash: emitResult.storageHash,
584
+ ...ifDefined('executionHash', emitResult.executionHash),
585
+ profileHash: emitResult.profileHash,
586
+ contractJson: emitResult.contractJson,
587
+ contractDts: emitResult.contractDts,
588
+ });
589
+ } catch (error) {
590
+ onProgress?.({
591
+ action: 'emit',
592
+ kind: 'spanEnd',
593
+ spanId: 'emit',
594
+ outcome: 'error',
595
+ });
596
+
597
+ return notOk({
598
+ code: 'EMIT_FAILED',
599
+ summary: 'Failed to emit contract',
600
+ why: error instanceof Error ? error.message : String(error),
601
+ meta: undefined,
602
+ });
603
+ }
604
+ }
605
+ }
@@ -0,0 +1,9 @@
1
+ export class ContractValidationError extends Error {
2
+ override readonly cause?: unknown;
3
+
4
+ constructor(message: string, cause?: unknown) {
5
+ super(message);
6
+ this.name = 'ContractValidationError';
7
+ this.cause = cause;
8
+ }
9
+ }