From cf8e640d8a65a28e0d18ea16161f8663af4f2d82 Mon Sep 17 00:00:00 2001 From: Amirehsan Davoodi <33749038+AmirDavoodi@users.noreply.github.com> Date: Fri, 13 Dec 2024 20:59:00 +0330 Subject: [PATCH 1/3] docs: remove the synchronize database option for the command 'db:drop' (#1226) Co-authored-by: Ehsan Davoodi --- README.MD | 2 +- docs/guide/cli.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.MD b/README.MD index e0a2a1a1..f0a03184 100644 --- a/README.MD +++ b/README.MD @@ -87,7 +87,7 @@ for the seeder- & factory-location. |-------------------------|----------------------------------------------------|-----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `--root` or `-r` | `db:create`, `db:drop`, `seed:create` & `seed:run` | `process.cwd()` | Root directory of the project. | | `--dataSource` or `-d` | `db:create`, `db:drop` & `seed:run` | `data-source` | Name (or relative path incl. name) of the data-source file. | -| `--synchronize` or `-s` | `db:create` & `db:drop` | `yes` | Synchronize the database schema after database creation. Options: `yes` or `no`. | +| `--synchronize` or `-s` | `db:create` | `yes` | Synchronize the database schema after database creation. Options: `yes` or `no`. | | `--initialDatabase` | `db:create` | `undefined` | Specify the initial database to connect to. This option is only relevant for the `postgres` driver, which must always to connect to a database. If no database is provided, the database name will be equal to the connection user name. | | `--name` | `seed:create` & `seed:run` | `undefined` | Name (or relative path incl. name) of the seeder. | | `--preserveFilePaths` | `db:create`, `db:drop`, `seed:create` & `seed:run` | `false` | This option indicates if file paths should be preserved and treated as if the just-in-time compilation environment is detected. | diff --git a/docs/guide/cli.md b/docs/guide/cli.md index 84815c1c..5fff4973 100644 --- a/docs/guide/cli.md +++ b/docs/guide/cli.md @@ -30,7 +30,7 @@ for the seeder- & factory-location. |-------------------------|----------------------------------------------------|-----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `--root` or `-r` | `db:create`, `db:drop`, `seed:create` & `seed:run` | `process.cwd()` | Root directory of the project. | | `--dataSource` or `-d` | `db:create`, `db:drop` & `seed:run` | `data-source` | Name (or relative path incl. name) of the data-source file. | -| `--synchronize` or `-s` | `db:create` & `db:drop` | `yes` | Synchronize the database schema after database creation. Options: `yes` or `no`. | +| `--synchronize` or `-s` | `db:create` | `yes` | Synchronize the database schema after database creation. Options: `yes` or `no`. | | `--initialDatabase` | `db:create` | `undefined` | Specify the initial database to connect to. This option is only relevant for the `postgres` driver, which must always to connect to a database. If no database is provided, the database name will be equal to the connection user name. | | `--name` | `seed:create` & `seed:run` | `undefined` | Name (or relative path incl. name) of the seeder. | | `--preserveFilePaths` | `db:create`, `db:drop`, `seed:create` & `seed:run` | `false` | This option indicates if file paths should be preserved and treated as if the just-in-time compilation environment is detected. | From 36695b53a375a2a0ca479fcde56cd908445c895a Mon Sep 17 00:00:00 2001 From: Evgenii Strigo Date: Mon, 13 Jan 2025 01:21:19 +0100 Subject: [PATCH 2/3] feat: add create database template support for postgres --- src/database/driver/postgres.ts | 4 +++- src/database/driver/type.ts | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/database/driver/postgres.ts b/src/database/driver/postgres.ts index afdfe5c6..a957a3b1 100644 --- a/src/database/driver/postgres.ts +++ b/src/database/driver/postgres.ts @@ -87,7 +87,9 @@ export async function createPostgresDatabase( * @link https://github.com/typeorm/typeorm/blob/master/src/driver/postgres/PostgresQueryRunner.ts#L326 */ let query = `CREATE DATABASE "${options.database}"`; - if (typeof options.characterSet === 'string') { + if (typeof options.template === 'string') { + query += ` TEMPLATE "${options.template}"`; + } else if (typeof options.characterSet === 'string') { query += ` WITH ENCODING '${options.characterSet}'`; } diff --git a/src/database/driver/type.ts b/src/database/driver/type.ts index 8eecded7..d3a9bfee 100644 --- a/src/database/driver/type.ts +++ b/src/database/driver/type.ts @@ -18,6 +18,9 @@ export type DriverOptions = { charset?: string, characterSet?: string, + // only for postgres 13+, see https://www.postgresql.org/docs/current/manage-ag-templatedbs.html + template?: string, + extra?: { [key: string]: any } From 0794713e19cac30199c1ab37841f20bccc94c6d6 Mon Sep 17 00:00:00 2001 From: Evgenii Strigo Date: Mon, 17 Feb 2025 22:50:08 +0100 Subject: [PATCH 3/3] feat: pass template option to driver --- src/database/driver/utils/build.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/database/driver/utils/build.ts b/src/database/driver/utils/build.ts index 15098623..fba2a45a 100644 --- a/src/database/driver/utils/build.ts +++ b/src/database/driver/utils/build.ts @@ -39,6 +39,7 @@ export function buildDriverOptions(options: DataSourceOptions): DriverOptions { ...(driverOptions.connectString ? { connectString: driverOptions.connectString } : {}), ...(driverOptions.sid ? { sid: driverOptions.sid } : {}), ...(driverOptions.serviceName ? { serviceName: driverOptions.serviceName } : {}), + ...(driverOptions.template ? { template: driverOptions.template } : {}), ...(options.extra ? { extra: options.extra } : {}), ...(driverOptions.domain ? { domain: driverOptions.domain } : {}), };