Browse Source

Fix database package bug not properly registering to the connection manager with custom database service instances

dev
David Ludwig 4 years ago
parent
commit
3b92895fa2
4 changed files with 18 additions and 23 deletions
  1. +9
    -2
      packages/database/src/DatabaseService.ts
  2. +0
    -18
      services/torrent/src/database/index.ts
  3. +7
    -1
      services/torrent/src/services/Database.ts
  4. +2
    -2
      services/torrent/src/services/TorrentClient.ts

+ 9
- 2
packages/database/src/DatabaseService.ts View File

@ -22,7 +22,14 @@ export class DatabaseService<M extends Microservice = Microservice> 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<M extends Microservice = Microservice> extends Inte
entities: EntitySchemaTypes[])
{
// Create the database connection
return await createConnection({
return await this.createConnection({
type : type,
host : host,
port : port,


+ 0
- 18
services/torrent/src/database/index.ts View File

@ -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"]
});
}

+ 7
- 1
services/torrent/src/services/Database.ts View File

@ -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;
}

+ 2
- 2
services/torrent/src/services/TorrentClient.ts View File

@ -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();
}


Loading…
Cancel
Save