feat(create-medusa-app): set database name to project name (#8727)

* add check and prompt for db name

* change var format

* remove log

* remove log
This commit is contained in:
Shahed Nasser
2024-08-23 14:17:37 +03:00
committed by GitHub
parent aa1ef20172
commit 408ed2ee45
3 changed files with 38 additions and 2 deletions

View File

@@ -16,6 +16,12 @@ export default async function createDb({ client, db }: CreateDbOptions) {
await client.query(`CREATE DATABASE "${db}"`)
}
async function doesDbExist (client: pg.Client, dbName: string): Promise<boolean> {
const result = await client.query(`SELECT datname FROM pg_catalog.pg_database WHERE datname='${dbName}';`)
return !!result.rowCount
}
export async function runCreateDb({
client,
dbName,
@@ -61,6 +67,7 @@ async function getForDbName({
}): Promise<{
client: pg.Client
dbConnectionString: string
dbName: string
}> {
let client!: pg.Client
let postgresUsername = "postgres"
@@ -132,6 +139,23 @@ async function getForDbName({
}
}
// check if database exists
if (await doesDbExist(client, dbName)) {
const { newDbName } = await inquirer.prompt([
{
type: "input",
name: "newDbName",
message: `A database already exists with the name ${dbName}, please enter a name for the database:`,
default: dbName,
validate: (input) => {
return typeof input === "string" && input.length > 0 && input !== dbName
},
},
])
dbName = newDbName
}
// format connection string
const dbConnectionString = formatConnectionString({
user: postgresUsername,
@@ -143,6 +167,7 @@ async function getForDbName({
return {
client,
dbConnectionString,
dbName
}
}