( connectionString: string, cwd = process.cwd(), schemaEnginePath?: string, )
| 66 | |
| 67 | // could be refactored with engines using JSON RPC instead and just passing the schema |
| 68 | export async function canConnectToDatabase( |
| 69 | connectionString: string, |
| 70 | cwd = process.cwd(), |
| 71 | schemaEnginePath?: string, |
| 72 | ): Promise<ConnectionResult> { |
| 73 | if (!connectionString) { |
| 74 | throw new Error('Connection url is empty. See https://pris.ly/d/config-url') |
| 75 | } |
| 76 | |
| 77 | try { |
| 78 | await execaCommand({ |
| 79 | connectionString, |
| 80 | cwd, |
| 81 | schemaEnginePath: schemaEnginePath, |
| 82 | engineCommandName: 'can-connect-to-database', |
| 83 | }) |
| 84 | } catch (_e) { |
| 85 | const e = _e as ExecaError |
| 86 | |
| 87 | if (e.stderr) { |
| 88 | const logs = parseJsonFromStderr(e.stderr) |
| 89 | const error = logs.find((it) => it.level === 'ERROR' && it.target === 'schema_engine::logger') |
| 90 | |
| 91 | if (error && error.fields.error_code && error.fields.message) { |
| 92 | return { |
| 93 | code: error.fields.error_code as DatabaseErrorCodes, |
| 94 | message: error.fields.message, |
| 95 | } |
| 96 | } else { |
| 97 | throw new Error(`Schema engine error:\n${logs.map((log) => log.fields.message).join('\n')}`) |
| 98 | } |
| 99 | } else { |
| 100 | throw new Error(`Schema engine exited. ${_e}`) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return true |
| 105 | } |
| 106 | |
| 107 | // could be refactored with engines using JSON RPC instead and just passing the schema |
| 108 | export async function createDatabase(connectionString: string, cwd = process.cwd(), schemaEnginePath?: string) { |
no test coverage detected