MCPcopy
hub / github.com/prisma/prisma / createDatabase

Function createDatabase

packages/internals/src/schemaEngineCommands.ts:108–141  ·  view source on GitHub ↗
(connectionString: string, cwd = process.cwd(), schemaEnginePath?: string)

Source from the content-addressed store, hash-verified

106
107// could be refactored with engines using JSON RPC instead and just passing the schema
108export async function createDatabase(connectionString: string, cwd = process.cwd(), schemaEnginePath?: string) {
109 const dbExists = await canConnectToDatabase(connectionString, cwd, schemaEnginePath)
110
111 // If database is already created, stop here, don't create it
112 if (dbExists === true) {
113 return false
114 }
115
116 try {
117 await execaCommand({
118 connectionString,
119 cwd,
120 schemaEnginePath,
121 engineCommandName: 'create-database',
122 })
123
124 return true
125 } catch (_e) {
126 const e = _e as ExecaError
127
128 if (e.stderr) {
129 const logs = parseJsonFromStderr(e.stderr)
130 const error = logs.find((it) => it.level === 'ERROR' && it.target === 'schema_engine::logger')
131
132 if (error && error.fields.error_code && error.fields.message) {
133 throw new Error(`${error.fields.error_code}: ${error.fields.message}`)
134 } else {
135 throw new Error(`Schema engine error:\n${logs.map((log) => log.fields.message).join('\n')}`)
136 }
137 } else {
138 throw new Error(`Schema engine exited. ${_e}`)
139 }
140 }
141}
142
143export async function dropDatabase(connectionString: string, cwd = process.cwd(), schemaEnginePath?: string) {
144 try {

Callers 4

setupPostgresFunction · 0.90
setupMysqlFunction · 0.90
ensureDatabaseExistsFunction · 0.90

Calls 3

canConnectToDatabaseFunction · 0.85
execaCommandFunction · 0.85
parseJsonFromStderrFunction · 0.85

Tested by

no test coverage detected