Skip to content

Commit

Permalink
feat(postgres): allow schema creation for postgres (#1247)
Browse files Browse the repository at this point in the history
* feat(postgres): allow schema creation during createDatabase

* feat(postgres): add schema option to createPostgresDatabase and buildDriverOptions
  • Loading branch information
oroce authored and tada5hi committed Jan 24, 2025
1 parent ca465a6 commit 986ff58
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/database/driver/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export async function createSimplePostgresConnection(
user: options.user,
password: options.password,
ssl: options.ssl,
schema: options.schema,
...(options.extra ? options.extra : {}),
};

Expand Down Expand Up @@ -90,9 +91,17 @@ export async function createPostgresDatabase(
if (typeof options.characterSet === 'string') {
query += ` WITH ENCODING '${options.characterSet}'`;
}

const result = await executeSimplePostgresQuery(connection, query);

if (typeof options.schema === 'string' && options.schema !== 'public') {
const schemaConnection = await createSimplePostgresConnection(driver, options, {
...context,
initialDatabase: options.database,
});
const schemaQuery = `CREATE SCHEMA IF NOT EXISTS "${options.schema}"`;
await executeSimplePostgresQuery(schemaConnection, schemaQuery);
}

if (context.synchronize) {
await synchronizeDatabaseSchema(context.options);
}
Expand Down
3 changes: 3 additions & 0 deletions src/database/driver/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export type DriverOptions = {
charset?: string,
characterSet?: string,

// postgres specific
schema?: string

extra?: {
[key: string]: any
}
Expand Down
1 change: 1 addition & 0 deletions src/database/driver/utils/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ export function buildDriverOptions(options: DataSourceOptions): DriverOptions {
...(driverOptions.serviceName ? { serviceName: driverOptions.serviceName } : {}),
...(options.extra ? { extra: options.extra } : {}),
...(driverOptions.domain ? { domain: driverOptions.domain } : {}),
...(driverOptions.schema ? { schema: driverOptions.schema } : {}),
};
}

0 comments on commit 986ff58

Please sign in to comment.