@prisma-next/cli 0.3.0-dev.16 → 0.3.0-dev.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-BO73VO4I.js +45 -0
- package/dist/chunk-BO73VO4I.js.map +1 -0
- package/dist/{chunk-VI2YETW7.js → chunk-MPSJAVF6.js} +4 -2
- package/dist/{chunk-U6QI3AZ3.js → chunk-RIONCN4I.js} +45 -6
- package/dist/chunk-RIONCN4I.js.map +1 -0
- package/dist/{chunk-74IELXRA.js → chunk-RPYY5SM7.js} +273 -19
- package/dist/chunk-RPYY5SM7.js.map +1 -0
- package/dist/cli.js +708 -551
- package/dist/cli.js.map +1 -1
- package/dist/commands/contract-emit.js +2 -3
- package/dist/commands/db-init.js +5 -46
- package/dist/commands/db-init.js.map +1 -1
- package/dist/commands/db-introspect.d.ts.map +1 -1
- package/dist/commands/db-introspect.js +107 -133
- package/dist/commands/db-introspect.js.map +1 -1
- package/dist/commands/db-schema-verify.d.ts.map +1 -1
- package/dist/commands/db-schema-verify.js +119 -107
- package/dist/commands/db-schema-verify.js.map +1 -1
- package/dist/commands/db-sign.d.ts.map +1 -1
- package/dist/commands/db-sign.js +151 -150
- package/dist/commands/db-sign.js.map +1 -1
- package/dist/commands/db-verify.d.ts.map +1 -1
- package/dist/commands/db-verify.js +141 -116
- package/dist/commands/db-verify.js.map +1 -1
- package/dist/control-api/client.d.ts.map +1 -1
- package/dist/control-api/types.d.ts +41 -0
- package/dist/control-api/types.d.ts.map +1 -1
- package/dist/exports/control-api.js +2 -3
- package/dist/exports/index.js +2 -3
- package/dist/exports/index.js.map +1 -1
- package/package.json +10 -10
- package/src/commands/contract-emit.ts +1 -1
- package/src/commands/db-introspect.ts +151 -178
- package/src/commands/db-schema-verify.ts +150 -143
- package/src/commands/db-sign.ts +202 -196
- package/src/commands/db-verify.ts +179 -149
- package/src/control-api/client.ts +256 -22
- package/src/control-api/types.ts +42 -0
- package/dist/chunk-5MPKZYVI.js +0 -47
- package/dist/chunk-5MPKZYVI.js.map +0 -1
- package/dist/chunk-6EPKRATC.js +0 -91
- package/dist/chunk-6EPKRATC.js.map +0 -1
- package/dist/chunk-74IELXRA.js.map +0 -1
- package/dist/chunk-U6QI3AZ3.js.map +0 -1
- /package/dist/{chunk-VI2YETW7.js.map → chunk-MPSJAVF6.js.map} +0 -0
|
@@ -1,32 +1,29 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
2
|
import { relative, resolve } from 'node:path';
|
|
3
|
-
import type {
|
|
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
|
-
|
|
14
|
+
errorJsonFormatNotSupported,
|
|
9
15
|
errorUnexpected,
|
|
10
|
-
} from '
|
|
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
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
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);
|