diff --git a/.changeset/fair-ducks-grow.md b/.changeset/fair-ducks-grow.md new file mode 100644 index 0000000000..addacefcde --- /dev/null +++ b/.changeset/fair-ducks-grow.md @@ -0,0 +1,5 @@ +--- +"create-t3-app": patch +--- + +create docker-compose.yml for postgres/mysql database options diff --git a/cli/src/installers/dbContainer.ts b/cli/src/installers/dbContainer.ts index 0a13f198df..8feb321e68 100644 --- a/cli/src/installers/dbContainer.ts +++ b/cli/src/installers/dbContainer.ts @@ -17,12 +17,6 @@ export const dbContainerInstaller: Installer = ({ databaseProvider, projectName, }) => { - const scriptSrc = path.join( - PKG_ROOT, - `template/extras/start-database/${databaseProvider}.sh` - ); - const scriptText = fs.readFileSync(scriptSrc, "utf-8"); - const scriptDest = path.join(projectDir, "start-database.sh"); // for configuration with postgresql and mysql when project is created with '.' project name const [projectNameParsed] = projectName === "." ? parseNameAndPath(projectDir) : [projectName]; @@ -30,9 +24,21 @@ export const dbContainerInstaller: Installer = ({ // Sanitize the project name for Docker container usage const sanitizedProjectName = sanitizeName(projectNameParsed); - fs.writeFileSync( - scriptDest, - scriptText.replaceAll("project1", sanitizedProjectName) + const dockerFolder = path.join( + PKG_ROOT, + "template/extras/docker/", + databaseProvider ); - fs.chmodSync(scriptDest, "755"); + + const files = fs.readdirSync(dockerFolder); + for (const file of files) { + const scriptSrc = path.join(dockerFolder, file); + const scriptDest = path.join(projectDir, file); + const scriptText = fs.readFileSync(scriptSrc, "utf-8"); + fs.writeFileSync( + scriptDest, + scriptText.replaceAll("project1", sanitizedProjectName) + ); + fs.chmodSync(scriptDest, "755"); + } }; diff --git a/cli/template/extras/docker/mysql/docker-compose.yml b/cli/template/extras/docker/mysql/docker-compose.yml new file mode 100644 index 0000000000..599bc046e2 --- /dev/null +++ b/cli/template/extras/docker/mysql/docker-compose.yml @@ -0,0 +1,10 @@ +services: + db: + image: mysql:latest + container_name: ${DB_CONTAINER_NAME?} + restart: unless-stopped + ports: + - "${DB_PORT?}:3306" + environment: + MYSQL_ROOT_PASSWORD: ${DB_PASSWORD?} + MYSQL_DATABASE: ${DB_NAME?} \ No newline at end of file diff --git a/cli/template/extras/start-database/mysql.sh b/cli/template/extras/docker/mysql/start-database.sh similarity index 88% rename from cli/template/extras/start-database/mysql.sh rename to cli/template/extras/docker/mysql/start-database.sh index 65789a3c7b..fe692e0b54 100755 --- a/cli/template/extras/start-database/mysql.sh +++ b/cli/template/extras/docker/mysql/start-database.sh @@ -50,9 +50,10 @@ if [ "$DB_PASSWORD" == "password" ]; then sed -i -e "s#:password@#:$DB_PASSWORD@#" .env fi -docker run -d \ - --name $DB_CONTAINER_NAME \ - -e MYSQL_ROOT_PASSWORD="$DB_PASSWORD" \ - -e MYSQL_DATABASE="$DB_NAME" \ - -p "$DB_PORT":3306 \ - docker.io/mysql && echo "Database container '$DB_CONTAINER_NAME' was successfully created" +export DB_PASSWORD +export DB_PORT +export DB_NAME +export DB_CONTAINER_NAME + +docker compose up -d \ + && echo "Database container '$DB_CONTAINER_NAME' was successfully created" diff --git a/cli/template/extras/docker/postgres/docker-compose.yml b/cli/template/extras/docker/postgres/docker-compose.yml new file mode 100644 index 0000000000..fa1ee9bb43 --- /dev/null +++ b/cli/template/extras/docker/postgres/docker-compose.yml @@ -0,0 +1,11 @@ +services: + db: + image: postgres:latest + container_name: ${DB_CONTAINER_NAME?} + restart: unless-stopped + ports: + - "${DB_PORT?}:5432" + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: ${DB_PASSWORD?} + POSTGRES_DB: ${DB_NAME?} \ No newline at end of file diff --git a/cli/template/extras/start-database/postgres.sh b/cli/template/extras/docker/postgres/start-database.sh similarity index 87% rename from cli/template/extras/start-database/postgres.sh rename to cli/template/extras/docker/postgres/start-database.sh index b162abdfe3..350f325163 100755 --- a/cli/template/extras/start-database/postgres.sh +++ b/cli/template/extras/docker/postgres/start-database.sh @@ -51,10 +51,10 @@ if [ "$DB_PASSWORD" = "password" ]; then sed -i -e "s#:password@#:$DB_PASSWORD@#" .env fi -docker run -d \ - --name $DB_CONTAINER_NAME \ - -e POSTGRES_USER="postgres" \ - -e POSTGRES_PASSWORD="$DB_PASSWORD" \ - -e POSTGRES_DB="$DB_NAME" \ - -p "$DB_PORT":5432 \ - docker.io/postgres && echo "Database container '$DB_CONTAINER_NAME' was successfully created" +export DB_PASSWORD +export DB_PORT +export DB_NAME +export DB_CONTAINER_NAME + +docker compose up -d \ + && echo "Database container '$DB_CONTAINER_NAME' was successfully created" diff --git a/www/src/components/docs/folderStructureDiagramApp.astro b/www/src/components/docs/folderStructureDiagramApp.astro index 68e9690737..6c500f2ec4 100644 --- a/www/src/components/docs/folderStructureDiagramApp.astro +++ b/www/src/components/docs/folderStructureDiagramApp.astro @@ -45,6 +45,7 @@ "prettier.config.js": ["tailwind"], "README.md": [], "start-database.sh (mysql or postgres only)": ["drizzle"], + "docker-compose.yml (mysql or postgres only)": ["drizzle"], "tailwind.config.ts": ["tailwind"], "tsconfig.json": [], }; diff --git a/www/src/components/docs/folderStructureDiagramPages.astro b/www/src/components/docs/folderStructureDiagramPages.astro index 8756fb6f4c..d4573dfa20 100644 --- a/www/src/components/docs/folderStructureDiagramPages.astro +++ b/www/src/components/docs/folderStructureDiagramPages.astro @@ -42,6 +42,7 @@ "prettier.config.js": ["tailwind"], "README.md": [], "start-database.sh (mysql or postgres only)": ["drizzle"], + "docker-compose.yml (mysql or postgres only)": ["drizzle"], "tailwind.config.ts": ["tailwind"], "tsconfig.json": [], }; diff --git a/www/src/pages/en/folder-structure-app.mdx b/www/src/pages/en/folder-structure-app.mdx index e0f3d77b85..b15f56fdd1 100644 --- a/www/src/pages/en/folder-structure-app.mdx +++ b/www/src/pages/en/folder-structure-app.mdx @@ -244,6 +244,13 @@ The `prettier.config.mjs` file is used to configure Prettier to include the pret The `start-database.sh` file is used to start the database. Please see the comments inside the file for information on how to start the database with your operating system. + +