Skip to content

Commit

Permalink
Fix ORM Config generation (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinejaussoin authored Aug 10, 2021
1 parent 930d41f commit 950bb86
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
17 changes: 9 additions & 8 deletions backend/src/build-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ async function buildOrmConfigTs() {
}

function getConfig(extension: 'js' | 'ts') {
const config = getOrmConfig();
const rootPath = path.resolve(__dirname);
const entities = path.resolve(rootPath, 'db', 'entities');
const migrations = path.resolve(rootPath, 'db', 'migrations');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(config as any).entities = [`${entities}/*.${extension}`];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(config as any).migrations = [`${migrations}/*.${extension}`];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(config as any).cli.migrationsDir = migrations;
const migrations =
extension === 'ts' ? 'src/db/migrations' : 'dist/src/db/migrations';

const config = getOrmConfig({
entities: [`${entities}/*.${extension}`],
migrations: [`${migrations}/*.${extension}`],
migrationDir: migrations,
});

return config;
}

Expand Down
51 changes: 34 additions & 17 deletions backend/src/db/orm-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,50 @@ import SessionOptionsEntity from './entities/SessionOptions';

const migrationsDirectory = 'src/db/migrations';

export default function (): ConnectionOptions {
export type ConnectionOptionsCustomisation = {
entities: string[];
migrations: string[];
migrationDir: string;
};

export default function (
customisation?: Partial<ConnectionOptionsCustomisation>
): ConnectionOptions {
return {
type: 'postgres',
host: config.DB_HOST,
port: config.DB_PORT,
username: config.DB_USER,
password: config.DB_PASSWORD,
database: config.DB_NAME,
entities: [
PostEntity,
PostGroupEntity,
SessionEntity,
UserEntity,
UserView,
ColumnDefinitionEntity,
VoteEntity,
SessionTemplateEntity,
TemplateColumnDefinitionEntity,
SubscriptionEntity,
LicenceEntity,
SessionOptionsEntity,
],
entities:
customisation && customisation.entities
? customisation.entities
: [
PostEntity,
PostGroupEntity,
SessionEntity,
UserEntity,
UserView,
ColumnDefinitionEntity,
VoteEntity,
SessionTemplateEntity,
TemplateColumnDefinitionEntity,
SubscriptionEntity,
LicenceEntity,
SessionOptionsEntity,
],
synchronize: false,
logging: config.SQL_LOG ? 'all' : undefined,
migrations: [`${migrationsDirectory}/*.ts`],
migrations:
customisation && customisation.migrations
? customisation.migrations
: [`${migrationsDirectory}/*.ts`],
cli: {
migrationsDir: migrationsDirectory,
migrationsDir:
customisation && customisation.migrationDir
? customisation.migrationDir
: migrationsDirectory,
},
};
}

0 comments on commit 950bb86

Please sign in to comment.