chore: Add database create & drop commands
This commit is contained in:
@@ -3,3 +3,8 @@ PROTOCOL=http
|
|||||||
PORT=3000
|
PORT=3000
|
||||||
CORS_PORT=3001
|
CORS_PORT=3001
|
||||||
APP_ENV=development
|
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",
|
"dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/app.ts",
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"start": "node dist/index.js",
|
"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": {
|
"dependencies": {
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
@@ -16,6 +18,7 @@
|
|||||||
"express-graphql": "^0.12.0",
|
"express-graphql": "^0.12.0",
|
||||||
"http-errors": "~1.6.3",
|
"http-errors": "~1.6.3",
|
||||||
"morgan": "^1.10.0",
|
"morgan": "^1.10.0",
|
||||||
|
"pg": "^8.7.1",
|
||||||
"winston": "^3.3.3"
|
"winston": "^3.3.3"
|
||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
@@ -46,6 +49,7 @@
|
|||||||
"@types/http-errors": "^1.8.1",
|
"@types/http-errors": "^1.8.1",
|
||||||
"@types/morgan": "^1.9.3",
|
"@types/morgan": "^1.9.3",
|
||||||
"@types/node": "^16.10.2",
|
"@types/node": "^16.10.2",
|
||||||
|
"@types/pg": "^8.6.1",
|
||||||
"nodemon": "^2.0.13",
|
"nodemon": "^2.0.13",
|
||||||
"ts-node": "^10.2.1"
|
"ts-node": "^10.2.1"
|
||||||
}
|
}
|
||||||
|
@@ -7,6 +7,11 @@ type AppConfig = {
|
|||||||
port: string,
|
port: string,
|
||||||
corsPort: string,
|
corsPort: string,
|
||||||
appEnv: string,
|
appEnv: string,
|
||||||
|
postgresDatabase: string,
|
||||||
|
postgresPort: number,
|
||||||
|
postgresHost: string,
|
||||||
|
postgresUsername: string,
|
||||||
|
postgresPassword: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const appConfig: AppConfig = {
|
const appConfig: AppConfig = {
|
||||||
@@ -15,6 +20,11 @@ const appConfig: AppConfig = {
|
|||||||
port: process.env.PORT || '3000',
|
port: process.env.PORT || '3000',
|
||||||
corsPort: process.env.CORS_PORT || '3001',
|
corsPort: process.env.CORS_PORT || '3001',
|
||||||
appEnv: process.env.APP_ENV || 'development',
|
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;
|
export default appConfig;
|
||||||
|
94
yarn.lock
94
yarn.lock
@@ -3116,6 +3116,15 @@
|
|||||||
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
|
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
|
||||||
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==
|
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==
|
||||||
|
|
||||||
|
"@types/pg@^8.6.1":
|
||||||
|
version "8.6.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/pg/-/pg-8.6.1.tgz#099450b8dc977e8197a44f5229cedef95c8747f9"
|
||||||
|
integrity sha512-1Kc4oAGzAl7uqUStZCDvaLFqZrW9qWSjXOmBfdgyBP5La7Us6Mg4GBvRlSoaZMhQF/zSj1C8CtKMBkoiT8eL8w==
|
||||||
|
dependencies:
|
||||||
|
"@types/node" "*"
|
||||||
|
pg-protocol "*"
|
||||||
|
pg-types "^2.2.0"
|
||||||
|
|
||||||
"@types/prettier@^2.0.0":
|
"@types/prettier@^2.0.0":
|
||||||
version "2.4.1"
|
version "2.4.1"
|
||||||
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.4.1.tgz#e1303048d5389563e130f5bdd89d37a99acb75eb"
|
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.4.1.tgz#e1303048d5389563e130f5bdd89d37a99acb75eb"
|
||||||
@@ -4534,6 +4543,11 @@ buffer-indexof@^1.0.0:
|
|||||||
resolved "https://registry.yarnpkg.com/buffer-indexof/-/buffer-indexof-1.1.1.tgz#52fabcc6a606d1a00302802648ef68f639da268c"
|
resolved "https://registry.yarnpkg.com/buffer-indexof/-/buffer-indexof-1.1.1.tgz#52fabcc6a606d1a00302802648ef68f639da268c"
|
||||||
integrity sha512-4/rOEg86jivtPTeOUUT61jJO1Ya1TrR/OkqCSZDyq84WJh3LuuiphBYJN+fm5xufIk4XAFcEwte/8WzC8If/1g==
|
integrity sha512-4/rOEg86jivtPTeOUUT61jJO1Ya1TrR/OkqCSZDyq84WJh3LuuiphBYJN+fm5xufIk4XAFcEwte/8WzC8If/1g==
|
||||||
|
|
||||||
|
buffer-writer@2.0.0:
|
||||||
|
version "2.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/buffer-writer/-/buffer-writer-2.0.0.tgz#ce7eb81a38f7829db09c873f2fbb792c0c98ec04"
|
||||||
|
integrity sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==
|
||||||
|
|
||||||
buffer-xor@^1.0.3:
|
buffer-xor@^1.0.3:
|
||||||
version "1.0.3"
|
version "1.0.3"
|
||||||
resolved "https://registry.yarnpkg.com/buffer-xor/-/buffer-xor-1.0.3.tgz#26e61ed1422fb70dd42e6e36729ed51d855fe8d9"
|
resolved "https://registry.yarnpkg.com/buffer-xor/-/buffer-xor-1.0.3.tgz#26e61ed1422fb70dd42e6e36729ed51d855fe8d9"
|
||||||
@@ -10983,6 +10997,11 @@ package-json@^6.3.0:
|
|||||||
registry-url "^5.0.0"
|
registry-url "^5.0.0"
|
||||||
semver "^6.2.0"
|
semver "^6.2.0"
|
||||||
|
|
||||||
|
packet-reader@1.0.0:
|
||||||
|
version "1.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/packet-reader/-/packet-reader-1.0.0.tgz#9238e5480dedabacfe1fe3f2771063f164157d74"
|
||||||
|
integrity sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==
|
||||||
|
|
||||||
pacote@^11.2.6:
|
pacote@^11.2.6:
|
||||||
version "11.3.5"
|
version "11.3.5"
|
||||||
resolved "https://registry.yarnpkg.com/pacote/-/pacote-11.3.5.tgz#73cf1fc3772b533f575e39efa96c50be8c3dc9d2"
|
resolved "https://registry.yarnpkg.com/pacote/-/pacote-11.3.5.tgz#73cf1fc3772b533f575e39efa96c50be8c3dc9d2"
|
||||||
@@ -11194,6 +11213,57 @@ performance-now@^2.1.0:
|
|||||||
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
|
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
|
||||||
integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=
|
integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=
|
||||||
|
|
||||||
|
pg-connection-string@^2.5.0:
|
||||||
|
version "2.5.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/pg-connection-string/-/pg-connection-string-2.5.0.tgz#538cadd0f7e603fc09a12590f3b8a452c2c0cf34"
|
||||||
|
integrity sha512-r5o/V/ORTA6TmUnyWZR9nCj1klXCO2CEKNRlVuJptZe85QuhFayC7WeMic7ndayT5IRIR0S0xFxFi2ousartlQ==
|
||||||
|
|
||||||
|
pg-int8@1.0.1:
|
||||||
|
version "1.0.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/pg-int8/-/pg-int8-1.0.1.tgz#943bd463bf5b71b4170115f80f8efc9a0c0eb78c"
|
||||||
|
integrity sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==
|
||||||
|
|
||||||
|
pg-pool@^3.4.1:
|
||||||
|
version "3.4.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/pg-pool/-/pg-pool-3.4.1.tgz#0e71ce2c67b442a5e862a9c182172c37eda71e9c"
|
||||||
|
integrity sha512-TVHxR/gf3MeJRvchgNHxsYsTCHQ+4wm3VIHSS19z8NC0+gioEhq1okDY1sm/TYbfoP6JLFx01s0ShvZ3puP/iQ==
|
||||||
|
|
||||||
|
pg-protocol@*, pg-protocol@^1.5.0:
|
||||||
|
version "1.5.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/pg-protocol/-/pg-protocol-1.5.0.tgz#b5dd452257314565e2d54ab3c132adc46565a6a0"
|
||||||
|
integrity sha512-muRttij7H8TqRNu/DxrAJQITO4Ac7RmX3Klyr/9mJEOBeIpgnF8f9jAfRz5d3XwQZl5qBjF9gLsUtMPJE0vezQ==
|
||||||
|
|
||||||
|
pg-types@^2.1.0, pg-types@^2.2.0:
|
||||||
|
version "2.2.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/pg-types/-/pg-types-2.2.0.tgz#2d0250d636454f7cfa3b6ae0382fdfa8063254a3"
|
||||||
|
integrity sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==
|
||||||
|
dependencies:
|
||||||
|
pg-int8 "1.0.1"
|
||||||
|
postgres-array "~2.0.0"
|
||||||
|
postgres-bytea "~1.0.0"
|
||||||
|
postgres-date "~1.0.4"
|
||||||
|
postgres-interval "^1.1.0"
|
||||||
|
|
||||||
|
pg@^8.7.1:
|
||||||
|
version "8.7.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/pg/-/pg-8.7.1.tgz#9ea9d1ec225980c36f94e181d009ab9f4ce4c471"
|
||||||
|
integrity sha512-7bdYcv7V6U3KAtWjpQJJBww0UEsWuh4yQ/EjNf2HeO/NnvKjpvhEIe/A/TleP6wtmSKnUnghs5A9jUoK6iDdkA==
|
||||||
|
dependencies:
|
||||||
|
buffer-writer "2.0.0"
|
||||||
|
packet-reader "1.0.0"
|
||||||
|
pg-connection-string "^2.5.0"
|
||||||
|
pg-pool "^3.4.1"
|
||||||
|
pg-protocol "^1.5.0"
|
||||||
|
pg-types "^2.1.0"
|
||||||
|
pgpass "1.x"
|
||||||
|
|
||||||
|
pgpass@1.x:
|
||||||
|
version "1.0.4"
|
||||||
|
resolved "https://registry.yarnpkg.com/pgpass/-/pgpass-1.0.4.tgz#85eb93a83800b20f8057a2b029bf05abaf94ea9c"
|
||||||
|
integrity sha512-YmuA56alyBq7M59vxVBfPJrGSozru8QAdoNlWuW3cz8l+UX3cWge0vTvjKhsSHSJpo3Bom8/Mm6hf0TR5GY0+w==
|
||||||
|
dependencies:
|
||||||
|
split2 "^3.1.1"
|
||||||
|
|
||||||
picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.2, picomatch@^2.2.3:
|
picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.2, picomatch@^2.2.3:
|
||||||
version "2.3.0"
|
version "2.3.0"
|
||||||
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.0.tgz#f1f061de8f6a4bf022892e2d128234fb98302972"
|
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.0.tgz#f1f061de8f6a4bf022892e2d128234fb98302972"
|
||||||
@@ -11960,6 +12030,28 @@ postcss@^8.1.0:
|
|||||||
nanoid "^3.1.25"
|
nanoid "^3.1.25"
|
||||||
source-map-js "^0.6.2"
|
source-map-js "^0.6.2"
|
||||||
|
|
||||||
|
postgres-array@~2.0.0:
|
||||||
|
version "2.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/postgres-array/-/postgres-array-2.0.0.tgz#48f8fce054fbc69671999329b8834b772652d82e"
|
||||||
|
integrity sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==
|
||||||
|
|
||||||
|
postgres-bytea@~1.0.0:
|
||||||
|
version "1.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/postgres-bytea/-/postgres-bytea-1.0.0.tgz#027b533c0aa890e26d172d47cf9ccecc521acd35"
|
||||||
|
integrity sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=
|
||||||
|
|
||||||
|
postgres-date@~1.0.4:
|
||||||
|
version "1.0.7"
|
||||||
|
resolved "https://registry.yarnpkg.com/postgres-date/-/postgres-date-1.0.7.tgz#51bc086006005e5061c591cee727f2531bf641a8"
|
||||||
|
integrity sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==
|
||||||
|
|
||||||
|
postgres-interval@^1.1.0:
|
||||||
|
version "1.2.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/postgres-interval/-/postgres-interval-1.2.0.tgz#b460c82cb1587507788819a06aa0fffdb3544695"
|
||||||
|
integrity sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==
|
||||||
|
dependencies:
|
||||||
|
xtend "^4.0.0"
|
||||||
|
|
||||||
prelude-ls@^1.2.1:
|
prelude-ls@^1.2.1:
|
||||||
version "1.2.1"
|
version "1.2.1"
|
||||||
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
|
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
|
||||||
@@ -13648,7 +13740,7 @@ split-string@^3.0.1, split-string@^3.0.2:
|
|||||||
dependencies:
|
dependencies:
|
||||||
extend-shallow "^3.0.0"
|
extend-shallow "^3.0.0"
|
||||||
|
|
||||||
split2@^3.0.0:
|
split2@^3.0.0, split2@^3.1.1:
|
||||||
version "3.2.2"
|
version "3.2.2"
|
||||||
resolved "https://registry.yarnpkg.com/split2/-/split2-3.2.2.tgz#bf2cf2a37d838312c249c89206fd7a17dd12365f"
|
resolved "https://registry.yarnpkg.com/split2/-/split2-3.2.2.tgz#bf2cf2a37d838312c249c89206fd7a17dd12365f"
|
||||||
integrity sha512-9NThjpgZnifTkJpzTZ7Eue85S49QwpNhZTq6GRJwObb6jnLFNGB7Qm73V5HewTROPyxD0C29xqmaI68bQtV+hg==
|
integrity sha512-9NThjpgZnifTkJpzTZ7Eue85S49QwpNhZTq6GRJwObb6jnLFNGB7Qm73V5HewTROPyxD0C29xqmaI68bQtV+hg==
|
||||||
|
Reference in New Issue
Block a user