@@ -16,6 +16,8 @@ import {
1616 ConnectorData ,
1717 getConnectorWithDatabase ,
1818 IntermediateConnectorData ,
19+ populateMongoDatabase ,
20+ sanitizeMongoUri ,
1921} from './util'
2022
2123export default class IntrospectCommand extends Command {
@@ -26,6 +28,16 @@ export default class IntrospectCommand extends Command {
2628 char : 'i' ,
2729 description : 'Interactive mode' ,
2830 } ) ,
31+
32+ [ 'env-file' ] : flags . string ( {
33+ description : 'Path to .env file to inject env vars' ,
34+ char : 'e' ,
35+ } ) ,
36+ [ 'project' ] : flags . string ( {
37+ description : 'Path to Prisma definition file' ,
38+ char : 'p' ,
39+ } ) ,
40+
2941 /**
3042 * Postgres Params
3143 */
@@ -164,7 +176,7 @@ ${chalk.bold(
164176 const introspection = await connector . introspect ( databaseName )
165177 const sdl = existingDatamodel
166178 ? await introspection . getNormalizedDatamodel ( existingDatamodel )
167- : await introspection . getDatamodel ( )
179+ : await introspection . getNormalizedDatamodel ( )
168180
169181 const renderer = DefaultRenderer . create (
170182 introspection . databaseType ,
@@ -244,7 +256,12 @@ ${chalk.bold(
244256
245257 async getConnector ( ) : Promise < IntermediateConnectorData > {
246258 const hasExecuteRaw = await this . hasExecuteRaw ( )
247- const credentials = await this . getCredentials ( hasExecuteRaw )
259+ let credentials = this . getCredentialsByFlags ( )
260+ let interactive = false
261+ if ( ! credentials ) {
262+ credentials = await this . getCredentialsInteractively ( hasExecuteRaw )
263+ interactive = true
264+ }
248265 if ( credentials ) {
249266 const {
250267 connector,
@@ -255,6 +272,7 @@ ${chalk.bold(
255272 disconnect,
256273 databaseType : credentials . type ,
257274 databaseName : credentials . schema ,
275+ interactive,
258276 }
259277 }
260278
@@ -282,12 +300,11 @@ ${chalk.bold(
282300 disconnect,
283301 databaseType : client . databaseType ,
284302 databaseName,
303+ interactive : false ,
285304 }
286305 }
287306
288- async getCredentials (
289- hasExecuteRaw : boolean ,
290- ) : Promise < DatabaseCredentials | null > {
307+ getCredentialsByFlags ( ) : DatabaseCredentials | null {
291308 const requiredPostgresFlags = [ 'pg-host' , 'pg-user' , 'pg-password' , 'pg-db' ]
292309 const requiredMysqlFlags = [ 'mysql-host' , 'mysql-user' , 'mysql-password' ]
293310
@@ -321,7 +338,7 @@ ${chalk.bold(
321338
322339 if ( mysqlFlags . length >= requiredMysqlFlags . length ) {
323340 return {
324- host : flags [ 'myqsl -host' ] ,
341+ host : flags [ 'mysql -host' ] ,
325342 port : parseInt ( flags [ 'mysql-port' ] , 10 ) ,
326343 user : flags [ 'mysql-user' ] ,
327344 password : flags [ 'mysql-password' ] ,
@@ -335,22 +352,31 @@ ${chalk.bold(
335352 host : flags [ 'pg-host' ] ,
336353 user : flags [ 'pg-user' ] ,
337354 password : flags [ 'pg-password' ] ,
338- database : flags [ 'pg-database ' ] ,
355+ database : flags [ 'pg-db ' ] ,
339356 port : parseInt ( flags [ 'pg-port' ] , 10 ) ,
340357 schema : flags [ 'pg-schema' ] , // this is optional and can be undefined
341358 type : DatabaseType . postgres ,
342359 }
343360 }
344361
345362 if ( flags [ 'mongo-uri' ] ) {
363+ const uri = flags [ 'mongo-uri' ]
364+ const database = flags [ 'mongo-db' ] // this is optional and can be undefined
365+ const credentials = populateMongoDatabase ( { uri, database } )
346366 return {
347- uri : flags [ 'mongo- uri' ] ,
348- schema : flags [ 'mongo-db' ] , // this is optional and can be undefined
367+ uri : sanitizeMongoUri ( credentials . uri ) ,
368+ schema : credentials . database ,
349369 type : DatabaseType . mongo ,
350370 }
351371 }
352372
353- if ( flags . interactive || ! hasExecuteRaw ) {
373+ return null
374+ }
375+
376+ async getCredentialsInteractively (
377+ hasExecuteRaw : boolean ,
378+ ) : Promise < DatabaseCredentials | null > {
379+ if ( this . flags . interactive || ! hasExecuteRaw ) {
354380 const endpointDialog = new EndpointDialog ( {
355381 out : this . out ,
356382 client : this . client ,
@@ -359,11 +385,12 @@ ${chalk.bold(
359385 definition : this . definition ,
360386 shouldAskForGenerator : false ,
361387 } )
362- return await endpointDialog . getDatabase ( true )
388+ return endpointDialog . getDatabase ( true )
363389 }
364390
365391 return null
366392 }
393+
367394 handleMissingArgs (
368395 requiredArgs : string [ ] ,
369396 providedArgs : string [ ] ,
0 commit comments