Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function getCredentialsFromExistingDatamodel(): Promise {
const schemaPath = await getSchemaPath()
if (schemaPath) {
const datamodel = readFileSync(schemaPath, 'utf-8')
const { datasources } = await getConfig({ datamodel })
// For now just take the first data source
if (datasources && datasources.length > 1) {
console.error(
`There are more than 1 datasources listed in the datamodel ${datasources.map(d => d.name).join(', ')}, taking ${
datasources[0].name
}`,
)
}
if (datasources && datasources.length > 0) {
const uri = datasources[0].url.value
return uriToCredentials(uri)
}
}
return undefined
}
export async function ensureDatabaseExists(action: LiftAction, killInk: boolean, forceCreate: boolean = false) {
const datamodel = await getSchema()
const config = await getConfig({ datamodel })
const activeDatasource =
config.datasources.length === 1
? config.datasources[0]
: config.datasources.find(d => d.config.enabled === 'true') || config.datasources[0]
if (!activeDatasource) {
throw new Error(`Couldn't find a datasource in the schema.prisma file`)
}
const canConnect = await canConnectToDatabase(activeDatasource.url.value)
if (canConnect === true) {
return
}
const { code, message } = canConnect
if (code !== 'P1003') {
transpile = true,
}: GenerateInFolderOptions): Promise {
const before = performance.now()
if (!projectDir) {
throw new Error(
`Project dir missing. Usage: ts-node examples/generate.ts examples/accounts`,
)
}
if (!fs.existsSync(projectDir)) {
throw new Error(`Path ${projectDir} does not exist`)
}
const schemaPath = getSchemaPath(projectDir)
const datamodel = fs.readFileSync(schemaPath, 'utf-8')
const dmmf = await getDMMF({ datamodel })
const config = await getConfig({ datamodel })
const outputDir = path.join(projectDir, 'node_modules/@prisma/photon')
await getPackedPackage('@prisma/photon', outputDir)
const platform = await getPlatform()
await generateClient({
binaryPaths: {
queryEngine: {
[platform]: path.join(
__dirname,
`../../query-engine-${platform}${
platform === 'windows' ? '.exe' : ''
}`,
),
},
return this.help()
}
let url: string | undefined = args['--url']
let schemaPath = await getSchemaPath()
let config: ConfigMetaFormat | undefined
if (!url) {
if (!schemaPath) {
throw new Error(
`Either provide ${chalk.greenBright(
'--url',
)} or make sure that you are in a folder with a ${chalk.greenBright('schema.prisma')} file.`,
)
}
config = await getConfig({
datamodelPath: schemaPath,
})
const datasource = config.datasources[0]
if (!datasource) {
throw new Error(
`Either provide ${chalk.greenBright('--url')} or add a ${chalk.greenBright.bold(
'datasource',
)} in the ${chalk.greenBright(path.relative(process.cwd(), schemaPath))} file.`,
)
}
url = datasource.url.value
}
const engine = new IntrospectionEngine({
cwd: schemaPath ? path.dirname(schemaPath) : undefined,