diff --git a/backend/src/database/connection.ts b/backend/src/database/connection.ts new file mode 100644 index 000000000..584b657d2 --- /dev/null +++ b/backend/src/database/connection.ts @@ -0,0 +1,18 @@ +import { createConnection, Connection } from 'mysql2/promise' +import CONFIG from '../config' + +const connection = async (): Promise => { + const con = await createConnection({ + host: CONFIG.DB_HOST, + port: CONFIG.DB_PORT, + user: CONFIG.DB_USER, + password: CONFIG.DB_PASSWORD, + database: CONFIG.DB_DATABASE, + }) + + await con.connect() + + return con +} + +export default connection diff --git a/backend/src/index.ts b/backend/src/index.ts index 523b32ef7..981731c67 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -2,7 +2,9 @@ import 'reflect-metadata' import express from 'express' import { buildSchema } from 'type-graphql' import { ApolloServer } from 'apollo-server-express' -// import { createConnection } from 'typeorm' +import { RowDataPacket } from 'mysql2/promise' + +import connection from './database/connection' import CONFIG from './config' // TODO move to extern @@ -15,7 +17,19 @@ import { TransactionResolver } from './graphql/resolvers/TransactionResolver' // TODO implement // import queryComplexity, { simpleEstimator, fieldConfigEstimator } from "graphql-query-complexity"; +const DB_VERSION = '0001-init_db' + async function main() { + // check for correct database version + const con = await connection() + const [rows] = await con.query(`SELECT fileName FROM migrations ORDER BY version DESC LIMIT 1;`) + if ( + (rows).length === 0 || + (rows)[0].fileName.indexOf(DB_VERSION) === -1 + ) { + throw new Error(`Wrong database version - the backend requires '${DB_VERSION}'`) + } + // const connection = await createConnection() const schema = await buildSchema({ resolvers: [UserResolver, BalanceResolver, TransactionResolver, GdtResolver], @@ -45,4 +59,8 @@ async function main() { }) } -main() +main().catch((e) => { + // eslint-disable-next-line no-console + console.error(e) + process.exit(1) +}) diff --git a/docker-compose.yml b/docker-compose.yml index 3734ba473..70d36521e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -61,6 +61,9 @@ services: - internal-net ports: - 4000:4000 + depends_on: + - mariadb + restart: always environment: # Envs used in Dockerfile # - DOCKER_WORKDIR="/app" @@ -69,6 +72,7 @@ services: - BUILD_VERSION - BUILD_COMMIT - NODE_ENV="production" + - DB_HOST=mariadb # Application only envs #env_file: # - ./frontend/.env