chore: Add database create & drop commands
This commit is contained in:
@@ -3,3 +3,8 @@ PROTOCOL=http
|
||||
PORT=3000
|
||||
CORS_PORT=3001
|
||||
APP_ENV=development
|
||||
POSTGRES_DATABASE=automatisch_development
|
||||
POSTGRES_PORT=5432
|
||||
POSTGRES_HOST=localhost
|
||||
POSTGRES_USERNAME=automatish_development_user
|
||||
POSTGRESS_PASSWORD=
|
||||
|
9
packages/backend/bin/database/client.ts
Normal file
9
packages/backend/bin/database/client.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Client } from 'pg';
|
||||
|
||||
const client = new Client({
|
||||
host: 'localhost',
|
||||
user: 'postgres',
|
||||
port: 5432,
|
||||
})
|
||||
|
||||
export default client;
|
42
packages/backend/bin/database/create.ts
Normal file
42
packages/backend/bin/database/create.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import appConfig from '../../src/config/app';
|
||||
import logger from '../../src/helpers/logger';
|
||||
import client from './client';
|
||||
|
||||
const createDatabaseAndUser = async () => {
|
||||
if(appConfig.appEnv !== 'development' && appConfig.appEnv !== 'test') {
|
||||
const errorMessage = 'Database creation can be used only with development or test environments!'
|
||||
logger.error(errorMessage)
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
await client.connect();
|
||||
await createDatabase();
|
||||
await createDatabaseUser();
|
||||
await grantPrivileges();
|
||||
|
||||
await client.end();
|
||||
}
|
||||
|
||||
const createDatabase = async () => {
|
||||
await client.query(`CREATE DATABASE ${appConfig.postgresDatabase}`);
|
||||
logger.info(`Database: ${appConfig.postgresDatabase} created!`);
|
||||
}
|
||||
|
||||
const createDatabaseUser = async () => {
|
||||
await client.query(`CREATE USER ${appConfig.postgresUsername}`);
|
||||
logger.info(`Database User: ${appConfig.postgresUsername} created!`);
|
||||
}
|
||||
|
||||
const grantPrivileges = async () => {
|
||||
await client.query(
|
||||
`GRANT ALL PRIVILEGES ON DATABASE ${appConfig.postgresDatabase} TO ${appConfig.postgresUsername};`
|
||||
);
|
||||
|
||||
logger.info(
|
||||
`${appConfig.postgresUsername} has granted all privileges on ${appConfig.postgresDatabase}!`
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
createDatabaseAndUser();
|
29
packages/backend/bin/database/drop.ts
Normal file
29
packages/backend/bin/database/drop.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import appConfig from '../../src/config/app';
|
||||
import logger from '../../src/helpers/logger';
|
||||
import client from './client';
|
||||
|
||||
const dropDatabase = async () => {
|
||||
if (appConfig.appEnv != 'development' && appConfig.appEnv != 'test') {
|
||||
const errorMessage = 'Drop database command can be used only with development or test environments!'
|
||||
|
||||
logger.error(errorMessage)
|
||||
return;
|
||||
}
|
||||
|
||||
await client.connect();
|
||||
await dropDatabaseAndUser();
|
||||
|
||||
await client.end();
|
||||
}
|
||||
|
||||
const dropDatabaseAndUser = async() => {
|
||||
await client.query(`DROP DATABASE IF EXISTS ${appConfig.postgresDatabase}`);
|
||||
logger.info(`Database: ${appConfig.postgresDatabase} removed!`);
|
||||
|
||||
await client.query(`DROP USER IF EXISTS ${appConfig.postgresUsername}`);
|
||||
logger.info(`Database User: ${appConfig.postgresUsername} removed!`);
|
||||
}
|
||||
|
||||
|
||||
|
||||
dropDatabase();
|
@@ -6,7 +6,9 @@
|
||||
"dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/app.ts",
|
||||
"build": "tsc",
|
||||
"start": "node dist/index.js",
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
"test": "echo \"Error: run tests from root\" && exit 1",
|
||||
"db:create": "ts-node ./bin/database/create.ts",
|
||||
"db:drop": "ts-node ./bin/database/drop.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
@@ -16,6 +18,7 @@
|
||||
"express-graphql": "^0.12.0",
|
||||
"http-errors": "~1.6.3",
|
||||
"morgan": "^1.10.0",
|
||||
"pg": "^8.7.1",
|
||||
"winston": "^3.3.3"
|
||||
},
|
||||
"contributors": [
|
||||
@@ -46,6 +49,7 @@
|
||||
"@types/http-errors": "^1.8.1",
|
||||
"@types/morgan": "^1.9.3",
|
||||
"@types/node": "^16.10.2",
|
||||
"@types/pg": "^8.6.1",
|
||||
"nodemon": "^2.0.13",
|
||||
"ts-node": "^10.2.1"
|
||||
}
|
||||
|
@@ -7,6 +7,11 @@ type AppConfig = {
|
||||
port: string,
|
||||
corsPort: string,
|
||||
appEnv: string,
|
||||
postgresDatabase: string,
|
||||
postgresPort: number,
|
||||
postgresHost: string,
|
||||
postgresUsername: string,
|
||||
postgresPassword: string
|
||||
}
|
||||
|
||||
const appConfig: AppConfig = {
|
||||
@@ -15,6 +20,11 @@ const appConfig: AppConfig = {
|
||||
port: process.env.PORT || '3000',
|
||||
corsPort: process.env.CORS_PORT || '3001',
|
||||
appEnv: process.env.APP_ENV || 'development',
|
||||
postgresDatabase: process.env.POSTGRES_DATABASE || 'automatisch_development',
|
||||
postgresPort: parseInt(process.env.POSTGRES_PORT) || 5432,
|
||||
postgresHost: process.env.POSTGRES_HOST || 'localhost',
|
||||
postgresUsername: process.env.POSTGRES_USERNAME || 'automatish_development_user',
|
||||
postgresPassword: process.env.POSTGRESS_PASSWORD,
|
||||
}
|
||||
|
||||
export default appConfig;
|
||||
|
Reference in New Issue
Block a user