From a240d76d15e4b86b3ac4a214b4214ef75e854352 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 10 Mar 2023 00:21:03 +0100 Subject: [PATCH 1/7] remove unused interfaces --- database/src/interface/TransactionContext.ts | 12 -------- database/src/interface/UserContext.ts | 26 ---------------- database/src/interface/UserInterface.ts | 32 -------------------- 3 files changed, 70 deletions(-) delete mode 100644 database/src/interface/TransactionContext.ts delete mode 100644 database/src/interface/UserContext.ts delete mode 100644 database/src/interface/UserInterface.ts 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 -} From 66c09d0615ece9104dd553cfa7e8004f68ef2779 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 10 Mar 2023 00:21:16 +0100 Subject: [PATCH 2/7] remove typeorm connection --- database/src/typeorm/connection.ts | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 database/src/typeorm/connection.ts 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 From 7d548968012e9a7ad6f0e855804c6042c07667f8 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 10 Mar 2023 00:21:27 +0100 Subject: [PATCH 3/7] refactor all main scripts --- database/src/helpers.ts | 34 ---------------------------------- database/src/index.ts | 33 +++++++++++++++++++++------------ database/src/prepare.ts | 16 +++++++--------- 3 files changed, 28 insertions(+), 55 deletions(-) delete mode 100644 database/src/helpers.ts 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..2d83bc52b 100644 --- a/database/src/index.ts +++ b/database/src/index.ts @@ -1,18 +1,28 @@ 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, Pool } 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() + 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 +35,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/prepare.ts b/database/src/prepare.ts index 440289aea..de47f7336 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 => { +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,9 @@ export default async (): Promise => { console.log('Found and dropped old migrations table') } } + */ await con.end() } + +export { createDatabase } From 6a7dac0cc643ef953664d19172c80affd90f3099 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 10 Mar 2023 00:21:37 +0100 Subject: [PATCH 4/7] cleaned test helpers --- backend/test/helpers.ts | 2 -- dht-node/test/helpers.ts | 2 -- 2 files changed, 4 deletions(-) diff --git a/backend/test/helpers.ts b/backend/test/helpers.ts index 1935b01a0..50bd3eb3f 100644 --- a/backend/test/helpers.ts +++ b/backend/test/helpers.ts @@ -3,7 +3,6 @@ import { createTestClient } from 'apollo-server-testing' import createServer from '../src/server/createServer' -import { initialize } from '@dbTools/helpers' import { entities } from '@entity/index' import { i18n, logger } from './testSetup' @@ -33,7 +32,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/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 } } From 331a4e12051dbe3be3c8c3303745129c8849751e Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 10 Mar 2023 00:24:00 +0100 Subject: [PATCH 5/7] lint fix --- database/src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/database/src/index.ts b/database/src/index.ts index 2d83bc52b..48056ab55 100644 --- a/database/src/index.ts +++ b/database/src/index.ts @@ -2,7 +2,7 @@ import 'reflect-metadata' import { createDatabase } from './prepare' import CONFIG from './config' -import { createPool, Pool } from 'mysql' +import { createPool } from 'mysql' import { Migration } from 'ts-mysql-migrate' import path from 'path' @@ -10,6 +10,7 @@ const run = async (command: string) => { // Database actions not supported by our migration library await createDatabase() + // Initialize Migrations const pool = createPool({ host: CONFIG.DB_HOST, port: CONFIG.DB_PORT, From a1bcf7515adf3b3543d0087cb7664f1708842be9 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 10 Mar 2023 10:54:41 +0100 Subject: [PATCH 6/7] removed more obsolete data --- database/.env.dist | 2 -- database/.env.template | 2 -- database/ormconfig.js | 15 --------------- 3 files changed, 19 deletions(-) delete mode 100644 database/ormconfig.js 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}'], -} From 03f1cc320684711d23e17a09578ba4ddaefddacd Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Mon, 3 Apr 2023 12:52:49 +0200 Subject: [PATCH 7/7] implement Hannes suggestion regarding export style --- database/src/prepare.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/database/src/prepare.ts b/database/src/prepare.ts index de47f7336..3c64b1c5e 100644 --- a/database/src/prepare.ts +++ b/database/src/prepare.ts @@ -2,7 +2,7 @@ import { createConnection } from 'mysql2/promise' import CONFIG from './config' -const createDatabase = async (): Promise => { +export const createDatabase = async (): Promise => { const con = await createConnection({ host: CONFIG.DB_HOST, port: CONFIG.DB_PORT, @@ -36,5 +36,3 @@ const createDatabase = async (): Promise => { await con.end() } - -export { createDatabase }