feat(cli): run migrations and app in start command (#284)
* feat: perform pending migrations in start command * feat: log error when DB connection is refused * feat: log error when Redis connection is refused * refactor: fix type errors * fix: correct server and copy graphql schema * fix: differentiate migrations by env * chore: remove dev executable * chore: fix typo in default postgresUsername * fix: copy json files into dist folder * chore(cli): add dev script * chore: pull non-dev logs to info level * feat(cli): run app in start command * fix(backend): remove default count in Connection * fix(cli): remove .eslintrc usage in lint script * refactor: remove disableMigrationsListValidation * refactor: make Step optional in ExecutionStep * refactor: make Flow optional in Step
This commit is contained in:
@@ -5,15 +5,16 @@ type AppConfig = {
|
||||
host: string;
|
||||
protocol: string;
|
||||
port: string;
|
||||
webAppUrl?: string;
|
||||
webAppUrl: string;
|
||||
appEnv: string;
|
||||
isDev: boolean;
|
||||
postgresDatabase: string;
|
||||
postgresPort: number;
|
||||
postgresHost: string;
|
||||
postgresUsername: string;
|
||||
postgresPassword: string;
|
||||
postgresPassword?: string;
|
||||
postgresEnableSsl: boolean;
|
||||
baseUrl?: string;
|
||||
baseUrl: string;
|
||||
encryptionKey: string;
|
||||
appSecretKey: string;
|
||||
serveWebAppSeparately: boolean;
|
||||
@@ -21,37 +22,44 @@ type AppConfig = {
|
||||
redisPort: number;
|
||||
};
|
||||
|
||||
const host = process.env.HOST || 'localhost';
|
||||
const protocol = process.env.PROTOCOL || 'http';
|
||||
const port = process.env.PORT || '3000';
|
||||
const serveWebAppSeparately = process.env.SERVE_WEB_APP_SEPARATELY === 'true' ? true : false;
|
||||
|
||||
let webAppUrl = `${protocol}://${host}:${port}`;
|
||||
if (serveWebAppSeparately) {
|
||||
webAppUrl = process.env.WEB_APP_URL || 'http://localhost:3001';
|
||||
}
|
||||
|
||||
const baseUrl = `${protocol}://${host}:${port}`;
|
||||
|
||||
const appEnv = process.env.APP_ENV || 'development';
|
||||
|
||||
const appConfig: AppConfig = {
|
||||
host: process.env.HOST || 'localhost',
|
||||
protocol: process.env.PROTOCOL || 'http',
|
||||
port: process.env.PORT || '3000',
|
||||
appEnv: process.env.APP_ENV || 'development',
|
||||
host,
|
||||
protocol,
|
||||
port,
|
||||
appEnv: appEnv,
|
||||
isDev: appEnv === 'development',
|
||||
postgresDatabase: process.env.POSTGRES_DATABASE || 'automatisch_development',
|
||||
postgresPort: parseInt(process.env.POSTGRES_PORT) || 5432,
|
||||
postgresPort: parseInt(process.env.POSTGRES_PORT|| '5432'),
|
||||
postgresHost: process.env.POSTGRES_HOST || 'localhost',
|
||||
postgresUsername:
|
||||
process.env.POSTGRES_USERNAME || 'automatish_development_user',
|
||||
process.env.POSTGRES_USERNAME || 'automatisch_development_user',
|
||||
postgresPassword: process.env.POSTGRES_PASSWORD,
|
||||
postgresEnableSsl: process.env.POSTGRES_ENABLE_SSL === 'true' ? true : false,
|
||||
encryptionKey: process.env.ENCRYPTION_KEY,
|
||||
appSecretKey: process.env.APP_SECRET_KEY,
|
||||
serveWebAppSeparately:
|
||||
process.env.SERVE_WEB_APP_SEPARATELY === 'true' ? true : false,
|
||||
encryptionKey: process.env.ENCRYPTION_KEY || '',
|
||||
appSecretKey: process.env.APP_SECRET_KEY || '',
|
||||
serveWebAppSeparately,
|
||||
redisHost: process.env.REDIS_HOST || '127.0.0.1',
|
||||
redisPort: parseInt(process.env.REDIS_PORT) || 6379,
|
||||
redisPort: parseInt(process.env.REDIS_PORT || '6379'),
|
||||
baseUrl,
|
||||
webAppUrl,
|
||||
};
|
||||
|
||||
if (appConfig.serveWebAppSeparately) {
|
||||
appConfig.webAppUrl = process.env.WEB_APP_URL || 'http://localhost:3001';
|
||||
} else {
|
||||
appConfig.webAppUrl = `${appConfig.protocol}://${appConfig.host}:${appConfig.port}`;
|
||||
}
|
||||
|
||||
if (!appConfig.encryptionKey) {
|
||||
throw new Error('ENCRYPTION_KEY environment variable needs to be set!');
|
||||
}
|
||||
|
||||
const baseUrl = `${appConfig.protocol}://${appConfig.host}:${appConfig.port}`;
|
||||
appConfig.baseUrl = baseUrl;
|
||||
|
||||
export default appConfig;
|
||||
|
@@ -1,6 +1,19 @@
|
||||
import { Model } from 'objection';
|
||||
import knexInstance from 'knex';
|
||||
import process from 'process';
|
||||
import knex from 'knex';
|
||||
import type { Knex } from 'knex';
|
||||
import knexConfig from '../../knexfile';
|
||||
import logger from '../helpers/logger';
|
||||
|
||||
const knex = knexInstance(knexConfig)
|
||||
Model.knex(knex)
|
||||
const knexInstance: Knex = knex(knexConfig);
|
||||
|
||||
const CONNECTION_REFUSED = 'ECONNREFUSED';
|
||||
|
||||
knexInstance.raw('SELECT 1')
|
||||
.catch((err) => {
|
||||
if (err.code === CONNECTION_REFUSED) {
|
||||
logger.error('Make sure you have installed PostgreSQL and it is running.', err);
|
||||
process.exit();
|
||||
}
|
||||
});
|
||||
|
||||
export default knexInstance;
|
||||
|
4
packages/backend/src/config/orm.ts
Normal file
4
packages/backend/src/config/orm.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { Model } from 'objection';
|
||||
import database from './database';
|
||||
|
||||
Model.knex(database)
|
@@ -3,6 +3,7 @@ import appConfig from './app';
|
||||
const redisConfig = {
|
||||
host: appConfig.redisHost,
|
||||
port: appConfig.redisPort,
|
||||
enableOfflineQueue: false,
|
||||
};
|
||||
|
||||
export default redisConfig;
|
||||
|
Reference in New Issue
Block a user