-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba8c1b1
commit 94ddbd9
Showing
6 changed files
with
208 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
cli/introspection/src/prompt/screens/Step2CreateOrSelectDB.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import React, { useContext, useState } from 'react' | ||
import { Box, Color } from 'ink' | ||
import BorderBox from '../components/BorderBox' | ||
import chalk from 'chalk' | ||
import { Link } from '../components/Link' | ||
import { useInitState } from '../components/InitState' | ||
import { DatabaseType } from 'prisma-datamodel' | ||
import { useConnector } from '../components/useConnector' | ||
import { prettyDb } from '../utils/print' | ||
import { RouterContext } from '../components/Router' | ||
import { createDatabase } from '@prisma/lift' | ||
import { ErrorBox, FixBox } from '../components/ErrorBox' | ||
import DummySelectable from '../components/DummySelectable' | ||
import Spinner from 'ink-spinner' | ||
const AnySpinner: any = Spinner | ||
|
||
const Step2CreateOrSelectDB: React.FC = () => { | ||
const [state] = useInitState() | ||
const [error, setError] = useState('') | ||
const [creatingDb, setCreatingDb] = useState(false) | ||
|
||
const router = useContext(RouterContext) | ||
const { schemas, disconnect } = useConnector() | ||
if (!state.dbCredentials) { | ||
throw new Error('Missing credentials in choose db view') | ||
} | ||
const { dbCredentials } = state | ||
const db = prettyDb(dbCredentials.type) | ||
const schemaWord = dbCredentials.type === DatabaseType.postgres ? 'schema' : 'database' | ||
const dbName = dbCredentials[schemaWord]! | ||
|
||
const schemaCount = state.useStarterKit ? schemas!.filter(s => s.countOfTables === 0).length : schemas!.length | ||
const href = dbCredentials.type === DatabaseType.postgres ? 'postgres-credentials' : 'mysql-credentials' | ||
|
||
const goBack = async () => { | ||
await disconnect() | ||
router.setRoute(href) | ||
} | ||
|
||
const onCreate = async () => { | ||
try { | ||
setCreatingDb(true) | ||
await createDatabase(dbCredentials.uri!) | ||
setCreatingDb(false) | ||
router.setRoute(state.useStarterKit ? 'download-example' : 'language-selection') | ||
} catch (e) { | ||
setCreatingDb(false) | ||
setError(e.message || JSON.stringify(e)) | ||
} | ||
} | ||
|
||
return ( | ||
<Box flexDirection="column"> | ||
<Box flexDirection="column" marginLeft={2}> | ||
<Color bold> | ||
A {schemaWord} named `{dbName}` doesn't exist on this {db} server. | ||
</Color> | ||
<Color bold> | ||
Create {db} database `{dbName}` or select an existing one. | ||
</Color> | ||
</Box> | ||
<BorderBox flexDirection="column" title={chalk.bold('Database options')} marginTop={1} marginBottom={1}> | ||
{creatingDb ? ( | ||
<DummySelectable tabIndex={0}> | ||
<Color cyan> | ||
<AnySpinner /> Creating {schemaWord} `{dbName}` | ||
</Color> | ||
</DummySelectable> | ||
) : ( | ||
!error && ( | ||
<Link | ||
label={`Create ${db} ${schemaWord} \`${dbName}\``} | ||
description="Start from scratch" | ||
tabIndex={0} | ||
padding={40} | ||
onSelect={onCreate} | ||
/> | ||
) | ||
)} | ||
{schemaCount > 0 && ( | ||
<Link | ||
label={`Use existing ${db} ${schemaWord}`} | ||
href="select-database" | ||
description={`Found ${schemaCount} ${schemaWord}${schemaCount === 1 ? '' : 's'}`} | ||
tabIndex={1} | ||
padding={40} | ||
/> | ||
)} | ||
</BorderBox> | ||
{error && error.length > 0 && ( | ||
<Box flexDirection="column" marginBottom={1}> | ||
<ErrorBox>{error}</ErrorBox> | ||
<FixBox>Make sure you have the correct rights to create the database.</FixBox> | ||
</Box> | ||
)} | ||
<Link onSelect={goBack} label="Back" description="(Database credentials)" tabIndex={3} kind="back" /> | ||
</Box> | ||
) | ||
} | ||
|
||
export default Step2CreateOrSelectDB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.