diff --git a/backend/test/helpers.ts b/backend/test/helpers.ts index f440adc02..ca84bab86 100644 --- a/backend/test/helpers.ts +++ b/backend/test/helpers.ts @@ -6,7 +6,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ -import { initialize } from '@dbTools/helpers' import { entities } from '@entity/index' import { createTestClient } from 'apollo-server-testing' @@ -40,7 +39,6 @@ export const testEnvironment = async (testLogger: any = logger, testI18n: any = const testClient = createTestClient(server.apollo) const mutate = testClient.mutate const query = testClient.query - await initialize() return { mutate, query, con } } diff --git a/database/.env.dist b/database/.env.dist index 689e4f509..0b2c342ca 100644 --- a/database/.env.dist +++ b/database/.env.dist @@ -4,5 +4,3 @@ DB_USER=root DB_PASSWORD= DB_DATABASE=gradido_community MIGRATIONS_TABLE=migrations - -TYPEORM_SEEDING_FACTORIES=src/factories/**/*{.ts,.js} diff --git a/database/.env.template b/database/.env.template index f2517a397..e21d4548c 100644 --- a/database/.env.template +++ b/database/.env.template @@ -6,5 +6,3 @@ DB_USER=$DB_USER DB_PASSWORD=$DB_PASSWORD DB_DATABASE=gradido_community MIGRATIONS_TABLE=migrations - -TYPEORM_SEEDING_FACTORIES=src/factories/**/*{.ts,.js} diff --git a/database/ormconfig.js b/database/ormconfig.js deleted file mode 100644 index 71e444061..000000000 --- a/database/ormconfig.js +++ /dev/null @@ -1,15 +0,0 @@ -/* eslint-disable @typescript-eslint/no-var-requires */ - -const CONFIG = require('./src/config') - -module.export = { - name: 'default', - type: 'mysql', - host: CONFIG.DB_HOST, - port: CONFIG.DB_PORT, - username: CONFIG.DB_USER, - password: CONFIG.DB_PASSWORD, - database: CONFIG.DB_DATABASE, - seeds: ['src/seeds/**/*{.ts,.js}'], - factories: ['src/factories/**/*{.ts,.js}'], -} diff --git a/database/src/helpers.ts b/database/src/helpers.ts deleted file mode 100644 index 710094548..000000000 --- a/database/src/helpers.ts +++ /dev/null @@ -1,34 +0,0 @@ -import CONFIG from './config' -import { createPool, PoolConfig } from 'mysql' -import { Migration } from 'ts-mysql-migrate' -import path from 'path' - -const poolConfig: PoolConfig = { - host: CONFIG.DB_HOST, - port: CONFIG.DB_PORT, - user: CONFIG.DB_USER, - password: CONFIG.DB_PASSWORD, - database: CONFIG.DB_DATABASE, -} - -// Pool? -const pool = createPool(poolConfig) - -// Create & Initialize Migrations -const migration = new Migration({ - conn: pool, - tableName: CONFIG.MIGRATIONS_TABLE, - silent: true, - dir: path.join(__dirname, '..', 'migrations'), -}) - -const initialize = async (): Promise => { - await migration.initialize() -} - -const resetDB = async (closePool = false): Promise => { - await migration.reset() // use for resetting database - if (closePool) pool.end() -} - -export { resetDB, pool, migration, initialize } diff --git a/database/src/index.ts b/database/src/index.ts index a9504063e..48056ab55 100644 --- a/database/src/index.ts +++ b/database/src/index.ts @@ -1,18 +1,29 @@ import 'reflect-metadata' -import prepare from './prepare' -import connection from './typeorm/connection' -import { resetDB, pool, migration } from './helpers' +import { createDatabase } from './prepare' +import CONFIG from './config' + +import { createPool } from 'mysql' +import { Migration } from 'ts-mysql-migrate' +import path from 'path' const run = async (command: string) => { // Database actions not supported by our migration library - await prepare() - - // Database connection for TypeORM - const con = await connection() - if (!con || !con.isConnected) { - throw new Error(`Couldn't open connection to database`) - } + await createDatabase() + // Initialize Migrations + const pool = createPool({ + host: CONFIG.DB_HOST, + port: CONFIG.DB_PORT, + user: CONFIG.DB_USER, + password: CONFIG.DB_PASSWORD, + database: CONFIG.DB_DATABASE, + }) + const migration = new Migration({ + conn: pool, + tableName: CONFIG.MIGRATIONS_TABLE, + silent: true, + dir: path.join(__dirname, '..', 'migrations'), + }) await migration.initialize() // Execute command @@ -25,14 +36,13 @@ const run = async (command: string) => { break case 'reset': // TODO protect from production - await resetDB() // use for resetting database + await migration.reset() break default: throw new Error(`Unsupported command ${command}`) } // Terminate connections gracefully - await con.close() pool.end() } diff --git a/database/src/interface/TransactionContext.ts b/database/src/interface/TransactionContext.ts deleted file mode 100644 index 8eeb579a0..000000000 --- a/database/src/interface/TransactionContext.ts +++ /dev/null @@ -1,12 +0,0 @@ -import Decimal from 'decimal.js-light' - -export interface TransactionContext { - typeId: number - userId: number - balance: Decimal - balanceDate: Date - amount: Decimal - memo: string - creationDate?: Date - sendReceiverUserId?: number -} diff --git a/database/src/interface/UserContext.ts b/database/src/interface/UserContext.ts deleted file mode 100644 index f3ccaecf4..000000000 --- a/database/src/interface/UserContext.ts +++ /dev/null @@ -1,26 +0,0 @@ -export interface UserContext { - pubKey?: Buffer - email?: string - firstName?: string - lastName?: string - deletedAt?: Date - password?: BigInt - privKey?: Buffer - emailHash?: Buffer - createdAt?: Date - emailChecked?: boolean - language?: string - publisherId?: number - passphrase?: string -} - -export interface ServerUserContext { - username?: string - password?: string - email?: string - role?: string - activated?: number - lastLogin?: Date - created?: Date - modified?: Date -} diff --git a/database/src/interface/UserInterface.ts b/database/src/interface/UserInterface.ts deleted file mode 100644 index ca328c092..000000000 --- a/database/src/interface/UserInterface.ts +++ /dev/null @@ -1,32 +0,0 @@ -import Decimal from 'decimal.js-light' - -export interface UserInterface { - // from user - email?: string - firstName?: string - lastName?: string - password?: BigInt - pubKey?: Buffer - privKey?: Buffer - emailHash?: Buffer - createdAt?: Date - emailChecked?: boolean - language?: string - deletedAt?: Date - publisherId?: number - passphrase?: string - // from server user - serverUserPassword?: string - role?: string - activated?: number - lastLogin?: Date - modified?: Date - // flag for admin - isAdmin?: boolean - // flag for balance (creation of 1000 GDD) - addBalance?: boolean - // balance - recordDate?: Date - creationDate?: Date - amount?: Decimal -} diff --git a/database/src/prepare.ts b/database/src/prepare.ts index 440289aea..3c64b1c5e 100644 --- a/database/src/prepare.ts +++ b/database/src/prepare.ts @@ -1,15 +1,8 @@ -/* PREPARE SCRIPT - * - * This file ensures operations our migration library - * can not take care of are done. - * This applies to all Database Operations like - * creating, deleting, renaming Databases. - */ +import { createConnection } from 'mysql2/promise' -import { createConnection, RowDataPacket } from 'mysql2/promise' import CONFIG from './config' -export default async (): Promise => { +export const createDatabase = async (): Promise => { const con = await createConnection({ host: CONFIG.DB_HOST, port: CONFIG.DB_PORT, @@ -25,6 +18,8 @@ export default async (): Promise => { DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci;`) + /* LEGACY CODE + import { RowDataPacket } from 'mysql2/promise' // Check if old migration table is present, delete if needed const [rows] = await con.query(`SHOW TABLES FROM \`${CONFIG.DB_DATABASE}\` LIKE 'migrations';`) if ((rows).length > 0) { @@ -37,6 +32,7 @@ export default async (): Promise => { console.log('Found and dropped old migrations table') } } + */ await con.end() } diff --git a/database/src/typeorm/connection.ts b/database/src/typeorm/connection.ts deleted file mode 100644 index e3434c3aa..000000000 --- a/database/src/typeorm/connection.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { createConnection, Connection } from 'typeorm' -import CONFIG from '../config' -import { entities } from '../../entity/index' - -const connection = async (): Promise => { - let con = null - try { - con = await createConnection({ - name: 'default', - type: 'mysql', - host: CONFIG.DB_HOST, - port: CONFIG.DB_PORT, - username: CONFIG.DB_USER, - password: CONFIG.DB_PASSWORD, - database: CONFIG.DB_DATABASE, - entities, - synchronize: false, - }) - } catch (error) { - // eslint-disable-next-line no-console - console.log(error) - } - - return con -} - -export default connection diff --git a/dht-node/test/helpers.ts b/dht-node/test/helpers.ts index f298bed1c..aa7f94964 100644 --- a/dht-node/test/helpers.ts +++ b/dht-node/test/helpers.ts @@ -4,7 +4,6 @@ import CONFIG from '@/config' import connection from '@/typeorm/connection' import { checkDBVersion } from '@/typeorm/DBVersion' -import { initialize } from '@dbTools/helpers' import { entities } from '@entity/index' import { logger } from './testSetup' @@ -42,7 +41,6 @@ export const testEnvironment = async () => { logger.fatal('Fatal: Database Version incorrect') throw new Error('Fatal: Database Version incorrect') } - await initialize() return { con } }