-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add multiple connections and also read .env config options
- Loading branch information
hirsch
committed
Apr 10, 2020
1 parent
990cc04
commit 69c9956
Showing
6 changed files
with
140 additions
and
59 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,9 +1,20 @@ | ||
const path = require('path') | ||
|
||
module.exports = { | ||
type: 'sqlite', | ||
database: './test.db', | ||
entities: ['sample/entities/**/*{.ts,.js}'], | ||
factories: ['sample/factories/**/*{.ts,.js}'], | ||
seeds: ['sample/seeds/**/*{.ts,.js}'], | ||
} | ||
module.exports = [ | ||
{ | ||
name: 'sample', | ||
type: 'sqlite', | ||
database: 'test.db', | ||
entities: ['sample/entities/**/*{.ts,.js}'], | ||
factories: ['sample/factories/**/*{.ts,.js}'], | ||
seeds: ['sample/seeds/**/*{.ts,.js}'], | ||
}, | ||
{ | ||
name: 'other', | ||
type: 'sqlite', | ||
database: 'test.db', | ||
entities: ['sample/entities/**/*{.ts,.js}'], | ||
factories: ['sample/factories/**/*{.ts,.js}'], | ||
seeds: ['sample/seeds/**/*{.ts,.js}'], | ||
} | ||
] |
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 |
---|---|---|
@@ -1,29 +1,54 @@ | ||
import * as path from 'path' | ||
import { | ||
Connection, | ||
ConnectionOptionsReader, | ||
createConnection as createTypeORMConnection, | ||
ConnectionOptions as TypeORMConnectionOptions, | ||
} from 'typeorm' | ||
import { printError } from './utils/log.util' | ||
|
||
interface SeedingOptions { | ||
readonly factories: string[] | ||
readonly seeds: string[] | ||
factories: string[] | ||
seeds: string[] | ||
} | ||
|
||
export interface ConnectionOptionArguments { | ||
root: string | ||
configName: string | ||
} | ||
|
||
export declare type ConnectionOptions = TypeORMConnectionOptions & SeedingOptions | ||
|
||
export const getConnectionOptions = async (configPath = 'ormconfig.js'): Promise<ConnectionOptions> => { | ||
return require(path.join(process.cwd(), configPath)) | ||
const attachSeedingOptions = (option: ConnectionOptions): ConnectionOptions => { | ||
if (!option.factories) { | ||
option.factories = [process.env.TYPEORM_SEEDING_FACTORIES as string] | ||
} | ||
if (!option.seeds) { | ||
option.seeds = [process.env.TYPEORM_SEEDING_SEEDS as string] | ||
} | ||
return option | ||
} | ||
|
||
export const createConnection = async (configPath: string): Promise<Connection> => { | ||
let options = await getConnectionOptions(configPath) | ||
if (Array.isArray(options)) { | ||
options.forEach((item) => { | ||
if (item.name === 'default') { | ||
options = item | ||
} | ||
}) | ||
export const getConnectionOption = async ( | ||
option: ConnectionOptionArguments, | ||
name: string, | ||
): Promise<ConnectionOptions> => { | ||
const reader = new ConnectionOptionsReader(option) | ||
const options = (await reader.all()) as any[] | ||
if (options.length === 1) { | ||
return attachSeedingOptions(options[0]) | ||
} | ||
return createTypeORMConnection(options as TypeORMConnectionOptions) | ||
if (name !== undefined && name !== '') { | ||
const filteredOptions = options.filter((o) => o.name === name) | ||
if (filteredOptions.length === 1) { | ||
return attachSeedingOptions(options[0]) | ||
} else { | ||
printError('Could not find any connection with the name=', name) | ||
} | ||
} | ||
printError('There are multiple connections please provide a connection name') | ||
} | ||
|
||
export const createConnection = async (option: ConnectionOptionArguments, name: string): Promise<Connection> => { | ||
const connectionOption = await getConnectionOption(option, name) | ||
return createTypeORMConnection(connectionOption) | ||
} |
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