@prisma-next/cli 0.3.0-dev.17 → 0.3.0-dev.19

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 (58) hide show
  1. package/dist/{chunk-ZG5T6OB5.js → chunk-AGOTG4L3.js} +43 -1
  2. package/dist/chunk-AGOTG4L3.js.map +1 -0
  3. package/dist/chunk-HLLI4YL7.js +180 -0
  4. package/dist/chunk-HLLI4YL7.js.map +1 -0
  5. package/dist/chunk-VG2R7DGF.js +735 -0
  6. package/dist/chunk-VG2R7DGF.js.map +1 -0
  7. package/dist/cli.js +1621 -1382
  8. package/dist/cli.js.map +1 -1
  9. package/dist/commands/contract-emit.d.ts.map +1 -1
  10. package/dist/commands/contract-emit.js +3 -4
  11. package/dist/commands/db-init.js +4 -49
  12. package/dist/commands/db-init.js.map +1 -1
  13. package/dist/commands/db-introspect.d.ts.map +1 -1
  14. package/dist/commands/db-introspect.js +106 -136
  15. package/dist/commands/db-introspect.js.map +1 -1
  16. package/dist/commands/db-schema-verify.d.ts.map +1 -1
  17. package/dist/commands/db-schema-verify.js +118 -110
  18. package/dist/commands/db-schema-verify.js.map +1 -1
  19. package/dist/commands/db-sign.d.ts.map +1 -1
  20. package/dist/commands/db-sign.js +150 -153
  21. package/dist/commands/db-sign.js.map +1 -1
  22. package/dist/commands/db-verify.d.ts.map +1 -1
  23. package/dist/commands/db-verify.js +140 -119
  24. package/dist/commands/db-verify.js.map +1 -1
  25. package/dist/control-api/client.d.ts.map +1 -1
  26. package/dist/control-api/types.d.ts +132 -1
  27. package/dist/control-api/types.d.ts.map +1 -1
  28. package/dist/exports/control-api.d.ts +1 -1
  29. package/dist/exports/control-api.d.ts.map +1 -1
  30. package/dist/exports/control-api.js +1 -3
  31. package/dist/exports/index.js +3 -4
  32. package/dist/exports/index.js.map +1 -1
  33. package/package.json +10 -10
  34. package/src/commands/contract-emit.ts +179 -102
  35. package/src/commands/db-introspect.ts +151 -178
  36. package/src/commands/db-schema-verify.ts +150 -143
  37. package/src/commands/db-sign.ts +202 -196
  38. package/src/commands/db-verify.ts +179 -149
  39. package/src/control-api/client.ts +352 -22
  40. package/src/control-api/types.ts +149 -1
  41. package/src/exports/control-api.ts +9 -0
  42. package/dist/chunk-5MPKZYVI.js +0 -47
  43. package/dist/chunk-5MPKZYVI.js.map +0 -1
  44. package/dist/chunk-6EPKRATC.js +0 -91
  45. package/dist/chunk-6EPKRATC.js.map +0 -1
  46. package/dist/chunk-74IELXRA.js +0 -371
  47. package/dist/chunk-74IELXRA.js.map +0 -1
  48. package/dist/chunk-U6QI3AZ3.js +0 -133
  49. package/dist/chunk-U6QI3AZ3.js.map +0 -1
  50. package/dist/chunk-VI2YETW7.js +0 -38
  51. package/dist/chunk-VI2YETW7.js.map +0 -1
  52. package/dist/chunk-ZG5T6OB5.js.map +0 -1
  53. package/dist/utils/action.d.ts +0 -16
  54. package/dist/utils/action.d.ts.map +0 -1
  55. package/dist/utils/spinner.d.ts +0 -29
  56. package/dist/utils/spinner.d.ts.map +0 -1
  57. package/src/utils/action.ts +0 -43
  58. package/src/utils/spinner.ts +0 -67
@@ -1,32 +1,29 @@
1
1
  import { readFile } from 'node:fs/promises';
2
2
  import { relative, resolve } from 'node:path';
3
- import type { ContractIR } from '@prisma-next/contract/ir';
3
+ import type { VerifyDatabaseSchemaResult } from '@prisma-next/core-control-plane/types';
4
+ import { notOk, ok, type Result } from '@prisma-next/utils/result';
5
+ import { Command } from 'commander';
6
+ import { loadConfig } from '../config-loader';
7
+ import { createControlClient } from '../control-api/client';
4
8
  import {
9
+ CliStructuredError,
10
+ errorContractValidationFailed,
5
11
  errorDatabaseConnectionRequired,
6
12
  errorDriverRequired,
7
13
  errorFileNotFound,
8
- errorRuntime,
14
+ errorJsonFormatNotSupported,
9
15
  errorUnexpected,
10
- } from '@prisma-next/core-control-plane/errors';
11
- import type { VerifyDatabaseSchemaResult } from '@prisma-next/core-control-plane/types';
12
- import { createControlPlaneStack } from '@prisma-next/core-control-plane/types';
13
- import { Command } from 'commander';
14
- import { loadConfig } from '../config-loader';
15
- import { performAction } from '../utils/action';
16
+ } from '../utils/cli-errors';
16
17
  import { setCommandDescriptions } from '../utils/command-helpers';
17
- import {
18
- assertContractRequirementsSatisfied,
19
- assertFrameworkComponentsCompatible,
20
- } from '../utils/framework-components';
21
- import { parseGlobalFlags } from '../utils/global-flags';
18
+ import { type GlobalFlags, parseGlobalFlags } from '../utils/global-flags';
22
19
  import {
23
20
  formatCommandHelp,
24
21
  formatSchemaVerifyJson,
25
22
  formatSchemaVerifyOutput,
26
23
  formatStyledHeader,
27
24
  } from '../utils/output';
25
+ import { createProgressAdapter } from '../utils/progress-adapter';
28
26
  import { handleResult } from '../utils/result-handler';
29
- import { withSpinner } from '../utils/spinner';
30
27
 
31
28
  interface DbSchemaVerifyOptions {
32
29
  readonly db?: string;
@@ -44,6 +41,132 @@ interface DbSchemaVerifyOptions {
44
41
  readonly 'no-color'?: boolean;
45
42
  }
46
43
 
44
+ /**
45
+ * Executes the db schema-verify command and returns a structured Result.
46
+ */
47
+ async function executeDbSchemaVerifyCommand(
48
+ options: DbSchemaVerifyOptions,
49
+ flags: GlobalFlags,
50
+ ): Promise<Result<VerifyDatabaseSchemaResult, CliStructuredError>> {
51
+ // Load config
52
+ const config = await loadConfig(options.config);
53
+ const configPath = options.config
54
+ ? relative(process.cwd(), resolve(options.config))
55
+ : 'prisma-next.config.ts';
56
+ const contractPathAbsolute = config.contract?.output
57
+ ? resolve(config.contract.output)
58
+ : resolve('src/prisma/contract.json');
59
+ const contractPath = relative(process.cwd(), contractPathAbsolute);
60
+
61
+ // Output header
62
+ if (flags.json !== 'object' && !flags.quiet) {
63
+ const details: Array<{ label: string; value: string }> = [
64
+ { label: 'config', value: configPath },
65
+ { label: 'contract', value: contractPath },
66
+ ];
67
+ if (options.db) {
68
+ details.push({ label: 'database', value: options.db });
69
+ }
70
+ const header = formatStyledHeader({
71
+ command: 'db schema-verify',
72
+ description: 'Check whether the database schema satisfies your contract',
73
+ url: 'https://pris.ly/db-schema-verify',
74
+ details,
75
+ flags,
76
+ });
77
+ console.log(header);
78
+ }
79
+
80
+ // Load contract file
81
+ let contractJsonContent: string;
82
+ try {
83
+ contractJsonContent = await readFile(contractPathAbsolute, 'utf-8');
84
+ } catch (error) {
85
+ if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {
86
+ return notOk(
87
+ errorFileNotFound(contractPathAbsolute, {
88
+ why: `Contract file not found at ${contractPathAbsolute}`,
89
+ fix: `Run \`prisma-next contract emit\` to generate ${contractPath}, or update \`config.contract.output\` in ${configPath}`,
90
+ }),
91
+ );
92
+ }
93
+ return notOk(
94
+ errorUnexpected(error instanceof Error ? error.message : String(error), {
95
+ why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`,
96
+ }),
97
+ );
98
+ }
99
+
100
+ let contractJson: Record<string, unknown>;
101
+ try {
102
+ contractJson = JSON.parse(contractJsonContent) as Record<string, unknown>;
103
+ } catch (error) {
104
+ return notOk(
105
+ errorContractValidationFailed(
106
+ `Contract JSON is invalid: ${error instanceof Error ? error.message : String(error)}`,
107
+ { where: { path: contractPathAbsolute } },
108
+ ),
109
+ );
110
+ }
111
+
112
+ // Resolve database connection (--db flag or config.db.connection)
113
+ const dbConnection = options.db ?? config.db?.connection;
114
+ if (!dbConnection) {
115
+ return notOk(
116
+ errorDatabaseConnectionRequired({
117
+ why: `Database connection is required for db schema-verify (set db.connection in ${configPath}, or pass --db <url>)`,
118
+ }),
119
+ );
120
+ }
121
+
122
+ // Check for driver
123
+ if (!config.driver) {
124
+ return notOk(errorDriverRequired({ why: 'Config.driver is required for db schema-verify' }));
125
+ }
126
+
127
+ // Create control client
128
+ const client = createControlClient({
129
+ family: config.family,
130
+ target: config.target,
131
+ adapter: config.adapter,
132
+ driver: config.driver,
133
+ extensionPacks: config.extensionPacks ?? [],
134
+ });
135
+
136
+ // Create progress adapter
137
+ const onProgress = createProgressAdapter({ flags });
138
+
139
+ try {
140
+ const schemaVerifyResult = await client.schemaVerify({
141
+ contractIR: contractJson,
142
+ strict: options.strict ?? false,
143
+ connection: dbConnection,
144
+ onProgress,
145
+ });
146
+
147
+ // Add blank line after all async operations if spinners were shown
148
+ if (!flags.quiet && flags.json !== 'object' && process.stdout.isTTY) {
149
+ console.log('');
150
+ }
151
+
152
+ return ok(schemaVerifyResult);
153
+ } catch (error) {
154
+ // Driver already throws CliStructuredError for connection failures
155
+ if (error instanceof CliStructuredError) {
156
+ return notOk(error);
157
+ }
158
+
159
+ // Wrap unexpected errors
160
+ return notOk(
161
+ errorUnexpected(error instanceof Error ? error.message : String(error), {
162
+ why: `Unexpected error during db schema-verify: ${error instanceof Error ? error.message : String(error)}`,
163
+ }),
164
+ );
165
+ } finally {
166
+ await client.close();
167
+ }
168
+ }
169
+
47
170
  export function createDbSchemaVerifyCommand(): Command {
48
171
  const command = new Command('schema-verify');
49
172
  setCommandDescriptions(
@@ -62,7 +185,7 @@ export function createDbSchemaVerifyCommand(): Command {
62
185
  })
63
186
  .option('--db <url>', 'Database connection string')
64
187
  .option('--config <path>', 'Path to prisma-next.config.ts')
65
- .option('--json [format]', 'Output as JSON (object or ndjson)', false)
188
+ .option('--json [format]', 'Output as JSON (object)', false)
66
189
  .option('--strict', 'Strict mode: extra schema elements cause failures', false)
67
190
  .option('-q, --quiet', 'Quiet mode: errors only')
68
191
  .option('-v, --verbose', 'Verbose output: debug info, timings')
@@ -73,142 +196,26 @@ export function createDbSchemaVerifyCommand(): Command {
73
196
  .action(async (options: DbSchemaVerifyOptions) => {
74
197
  const flags = parseGlobalFlags(options);
75
198
 
76
- const result = await performAction(async () => {
77
- // Load config (file I/O)
78
- const config = await loadConfig(options.config);
79
- // Normalize config path for display (match contract path format - no ./ prefix)
80
- const configPath = options.config
81
- ? relative(process.cwd(), resolve(options.config))
82
- : 'prisma-next.config.ts';
83
- const contractPathAbsolute = config.contract?.output
84
- ? resolve(config.contract.output)
85
- : resolve('src/prisma/contract.json');
86
- // Convert to relative path for display
87
- const contractPath = relative(process.cwd(), contractPathAbsolute);
88
-
89
- // Output header (only for human-readable output)
90
- if (flags.json !== 'object' && !flags.quiet) {
91
- const details: Array<{ label: string; value: string }> = [
92
- { label: 'config', value: configPath },
93
- { label: 'contract', value: contractPath },
94
- ];
95
- if (options.db) {
96
- details.push({ label: 'database', value: options.db });
97
- }
98
- const header = formatStyledHeader({
199
+ // Validate JSON format option
200
+ if (flags.json === 'ndjson') {
201
+ const result = notOk(
202
+ errorJsonFormatNotSupported({
99
203
  command: 'db schema-verify',
100
- description: 'Check whether the database schema satisfies your contract',
101
- url: 'https://pris.ly/db-schema-verify',
102
- details,
103
- flags,
104
- });
105
- console.log(header);
106
- }
107
-
108
- // Load contract file (file I/O)
109
- let contractJsonContent: string;
110
- try {
111
- contractJsonContent = await readFile(contractPathAbsolute, 'utf-8');
112
- } catch (error) {
113
- if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {
114
- throw errorFileNotFound(contractPathAbsolute, {
115
- why: `Contract file not found at ${contractPathAbsolute}`,
116
- });
117
- }
118
- throw errorUnexpected(error instanceof Error ? error.message : String(error), {
119
- why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`,
120
- });
121
- }
122
- const contractJson = JSON.parse(contractJsonContent) as Record<string, unknown>;
123
-
124
- // Check for driver (needed for family instance creation)
125
- if (!config.driver) {
126
- throw errorDriverRequired();
127
- }
128
-
129
- // Store driver descriptor after null check
130
- const driverDescriptor = config.driver;
131
-
132
- // Create family instance (needed for contract validation)
133
- const stack = createControlPlaneStack({
134
- target: config.target,
135
- adapter: config.adapter,
136
- driver: driverDescriptor,
137
- extensionPacks: config.extensionPacks,
138
- });
139
- const familyInstance = config.family.create(stack);
140
-
141
- // Validate contract using instance validator
142
- const contractIR = familyInstance.validateContractIR(contractJson) as ContractIR;
143
-
144
- // Validate contract requirements fail-fast before connecting to database
145
- assertContractRequirementsSatisfied({ contract: contractIR, stack });
146
-
147
- // Resolve database connection (--db flag or config.db.connection)
148
- const dbConnection = options.db ?? config.db?.connection;
149
- if (!dbConnection) {
150
- throw errorDatabaseConnectionRequired();
151
- }
152
-
153
- const driver = await withSpinner(() => driverDescriptor.create(dbConnection), {
154
- message: 'Connecting to database...',
155
- flags,
156
- });
157
-
158
- try {
159
- const rawComponents = [config.target, config.adapter, ...(config.extensionPacks ?? [])];
160
- const frameworkComponents = assertFrameworkComponentsCompatible(
161
- config.family.familyId,
162
- config.target.targetId,
163
- rawComponents,
164
- );
165
-
166
- // Call family instance schemaVerify method
167
- let schemaVerifyResult: VerifyDatabaseSchemaResult;
168
- try {
169
- schemaVerifyResult = await withSpinner(
170
- () =>
171
- familyInstance.schemaVerify({
172
- driver,
173
- contractIR,
174
- strict: options.strict ?? false,
175
- contractPath: contractPathAbsolute,
176
- configPath,
177
- frameworkComponents,
178
- }),
179
- {
180
- message: 'Verifying database schema...',
181
- flags,
182
- },
183
- );
184
- } catch (error) {
185
- // Wrap errors from schemaVerify() in structured error
186
- throw errorRuntime(error instanceof Error ? error.message : String(error), {
187
- why: `Failed to verify database schema: ${error instanceof Error ? error.message : String(error)}`,
188
- });
189
- }
190
-
191
- // Add blank line after all async operations if spinners were shown
192
- if (!flags.quiet && flags.json !== 'object' && process.stdout.isTTY) {
193
- console.log('');
194
- }
204
+ format: 'ndjson',
205
+ supportedFormats: ['object'],
206
+ }),
207
+ );
208
+ const exitCode = handleResult(result, flags);
209
+ process.exit(exitCode);
210
+ }
195
211
 
196
- // Return result (don't throw for logical mismatches - handle exit code separately)
197
- return schemaVerifyResult;
198
- } finally {
199
- // Ensure driver connection is closed
200
- await driver.close();
201
- }
202
- });
212
+ const result = await executeDbSchemaVerifyCommand(options, flags);
203
213
 
204
214
  // Handle result - formats output and returns exit code
205
215
  const exitCode = handleResult(result, flags, (schemaVerifyResult) => {
206
- // Output based on flags
207
216
  if (flags.json === 'object') {
208
- // JSON output to stdout
209
217
  console.log(formatSchemaVerifyJson(schemaVerifyResult));
210
218
  } else {
211
- // Human-readable output to stdout
212
219
  const output = formatSchemaVerifyOutput(schemaVerifyResult, flags);
213
220
  if (output) {
214
221
  console.log(output);