-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: experimental read env data-source options and merge them with f…
…ile based options
- Loading branch information
Showing
13 changed files
with
305 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { merge } from 'smob'; | ||
import type { DataSourceOptions } from 'typeorm'; | ||
import type { BaseDataSourceOptions } from 'typeorm/data-source/BaseDataSourceOptions'; | ||
import type { BetterSqlite3ConnectionOptions } from 'typeorm/driver/better-sqlite3/BetterSqlite3ConnectionOptions'; | ||
import type { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions'; | ||
import type { SqliteConnectionOptions } from 'typeorm/driver/sqlite/SqliteConnectionOptions'; | ||
import type { SqlServerConnectionOptions } from 'typeorm/driver/sqlserver/SqlServerConnectionOptions'; | ||
import type { DatabaseType } from 'typeorm/driver/types/DatabaseType'; | ||
import type { LoggerOptions } from 'typeorm/logger/LoggerOptions'; | ||
import { useEnv } from '../../../env'; | ||
|
||
export function hasEnvDataSourceOptions() : boolean { | ||
return !!useEnv('type'); | ||
} | ||
|
||
export function readDataSourceOptionsFromEnv() : DataSourceOptions | undefined { | ||
if (!hasEnvDataSourceOptions()) { | ||
return undefined; | ||
} | ||
|
||
// todo: include seeder options | ||
const base : BaseDataSourceOptions = { | ||
type: useEnv('type') as DatabaseType, | ||
entities: useEnv('entities'), | ||
subscribers: useEnv('subscribers'), | ||
migrations: useEnv('migrations'), | ||
migrationsTableName: useEnv('migrationsTableName'), | ||
// migrationsTransactionMode: useEnv('migra') | ||
metadataTableName: useEnv('metadataTableName'), | ||
logging: useEnv('logging') as LoggerOptions, | ||
logger: useEnv('logger') as BaseDataSourceOptions['logger'], | ||
maxQueryExecutionTime: useEnv('maxQueryExecutionTime'), | ||
synchronize: useEnv('synchronize'), | ||
migrationsRun: useEnv('migrationsRun'), | ||
dropSchema: useEnv('schemaDrop'), | ||
entityPrefix: useEnv('entityPrefix'), | ||
extra: useEnv('extra'), | ||
cache: useEnv('cache'), | ||
}; | ||
|
||
const credentialOptions = { | ||
url: useEnv('url'), | ||
host: useEnv('host'), | ||
port: useEnv('port'), | ||
username: useEnv('username'), | ||
password: useEnv('password'), | ||
database: useEnv('database'), | ||
}; | ||
|
||
if (base.type === 'mysql' || base.type === 'mariadb') { | ||
return { | ||
...base, | ||
...credentialOptions, | ||
type: base.type, | ||
}; | ||
} | ||
|
||
if (base.type === 'postgres') { | ||
return { | ||
...base, | ||
...credentialOptions, | ||
type: base.type, | ||
schema: useEnv('schema'), | ||
uuidExtension: useEnv('uuidExtension') as PostgresConnectionOptions['uuidExtension'], | ||
}; | ||
} | ||
|
||
if (base.type === 'cockroachdb') { | ||
return { | ||
...base, | ||
...credentialOptions, | ||
type: base.type, | ||
schema: useEnv('schema'), | ||
timeTravelQueries: true, | ||
}; | ||
} | ||
|
||
if (base.type === 'sqlite') { | ||
return { | ||
...base, | ||
type: base.type, | ||
database: useEnv('database'), | ||
} as SqliteConnectionOptions; | ||
} | ||
|
||
if (base.type === 'better-sqlite3') { | ||
return { | ||
...base, | ||
type: base.type, | ||
database: useEnv('database'), | ||
} as BetterSqlite3ConnectionOptions; | ||
} | ||
|
||
if (base.type === 'mssql') { | ||
return { | ||
...base, | ||
...credentialOptions, | ||
type: base.type, | ||
schema: useEnv('schema'), | ||
} as SqlServerConnectionOptions; | ||
} | ||
|
||
if (base.type === 'oracle') { | ||
return { | ||
...base, | ||
...credentialOptions, | ||
type: base.type, | ||
sid: useEnv('sid'), | ||
}; | ||
} | ||
|
||
return { | ||
...base, | ||
...credentialOptions, | ||
} as DataSourceOptions; | ||
} | ||
|
||
export function mergeDataSourceOptionsWithEnv(options: DataSourceOptions) { | ||
const env = readDataSourceOptionsFromEnv(); | ||
if (!env) { | ||
return options; | ||
} | ||
|
||
if (env.type !== options.type) { | ||
return options; | ||
} | ||
|
||
return merge({}, env, options); | ||
} |
6 changes: 3 additions & 3 deletions
6
src/data-source/options/utils.ts → src/data-source/options/utils/file-path.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './env'; | ||
export * from './file-path'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.