From 3b92895fa28305d608ee1a7676b4cb1db28d4be6 Mon Sep 17 00:00:00 2001 From: David Ludwig Date: Fri, 7 May 2021 18:30:39 +0000 Subject: [PATCH] Fix database package bug not properly registering to the connection manager with custom database service instances --- packages/database/src/DatabaseService.ts | 11 +++++++++-- services/torrent/src/database/index.ts | 18 ------------------ services/torrent/src/services/Database.ts | 8 +++++++- services/torrent/src/services/TorrentClient.ts | 4 ++-- 4 files changed, 18 insertions(+), 23 deletions(-) delete mode 100644 services/torrent/src/database/index.ts diff --git a/packages/database/src/DatabaseService.ts b/packages/database/src/DatabaseService.ts index 28cfb79..06e1d18 100644 --- a/packages/database/src/DatabaseService.ts +++ b/packages/database/src/DatabaseService.ts @@ -22,7 +22,14 @@ export class DatabaseService extends Inte /** * The active database connection */ - protected connection!: Connection; + public connection!: Connection; + + /** + * The function used to create the connection + * This is important because inheriting this service to provide custom entity types will not get + * the connection stored in the correct connection manager without overriding this explicitly. + */ + protected createConnection = createConnection; /** * The database entities @@ -61,7 +68,7 @@ export class DatabaseService extends Inte entities: EntitySchemaTypes[]) { // Create the database connection - return await createConnection({ + return await this.createConnection({ type : type, host : host, port : port, diff --git a/services/torrent/src/database/index.ts b/services/torrent/src/database/index.ts deleted file mode 100644 index 3b3b3db..0000000 --- a/services/torrent/src/database/index.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { createConnection } from "typeorm"; -import entities from "./entities"; - -export default async function connectToDatabase(host: string, port: number, username: string, - password: string, database: string) -{ - return createConnection({ - type: "mysql", - host, - port, - username, - password, - database, - synchronize: true, - entities, - migrations: ["src/migrations/*.ts"] - }); -} diff --git a/services/torrent/src/services/Database.ts b/services/torrent/src/services/Database.ts index 6543f17..817c680 100644 --- a/services/torrent/src/services/Database.ts +++ b/services/torrent/src/services/Database.ts @@ -1,4 +1,5 @@ import { DatabaseService, EntitySchemaTypes } from "@autoplex/database"; +import { createConnection } from "typeorm"; import * as entities from "../database/entities"; export default class Database extends DatabaseService @@ -6,5 +7,10 @@ export default class Database extends DatabaseService /** * The entities to use */ - public entities: EntitySchemaTypes[] = Object.values(entities); + protected entities: EntitySchemaTypes[] = Object.values(entities); + + /** + * Override the createConnection function to correctly register with the connection manager + */ + protected createConnection = createConnection; } diff --git a/services/torrent/src/services/TorrentClient.ts b/services/torrent/src/services/TorrentClient.ts index 133eaad..6b97491 100644 --- a/services/torrent/src/services/TorrentClient.ts +++ b/services/torrent/src/services/TorrentClient.ts @@ -4,10 +4,9 @@ import MagnetUri from "magnet-uri"; import WebTorrent from "webtorrent-hybrid"; import parseTorrent from "parse-torrent"; import { extname, join, sep } from "path"; -import Torrent from "../database/entities/Torrent"; import rimraf from "rimraf"; +import Torrent from "../database/entities/Torrent"; import { ISerializedTorrent, TorrentState } from "../common"; -import { Database } from "."; interface IAddOptions { downloadPath?: string; @@ -69,6 +68,7 @@ export class TorrentClient extends InternalService * Start the torrent client */ public start() { + console.log("Loading torrents..."); this.loadTorrents(); }