user4092086
user4092086

Reputation: 1226

Creating generic repository using TypeORM

I'm using typeorm and I want to create a generic repository:

import "reflect-metadata";
import { DBManager } from './db-manager';
import { Photo } from './entities/Photo';
import { createConnection, Connection } from "typeorm";

class GenericRepository<T> {
private connection: DBManager;
constructor(connection: DBManager) {
    this.connection = connection;
}

public list(): T[] {
    let result: T[] = [];
    this.connection.connect().then(async  connection => {
        result = await <Promise<T[]>>(connection.entityManager.find(T));
    });
    return result;
 }
}

let genericReposity = new GenericRepository<Photo>(new DBManager());
genericReposity.list();

This code of course doesn't woork and complains on find method that can not find name T

T should be my entity but I don't know how to achieve this

Upvotes: 5

Views: 11387

Answers (1)

pleerock
pleerock

Reputation: 18846

Generics in TypeScript are only in compile-type and all information about your generics types are removed in output javascript code. Thats why its not possible to do what you want. Its only possible if you explicitly specify a type to your generic repository, for example via constructor:

import "reflect-metadata";
import { DBManager } from './db-manager';
import { Photo } from './entities/Photo';
import { createConnection, Connection } from "typeorm";

export type ObjectType<T> = { new (): T } | Function;

class GenericRepository<T> {
    private connection: DBManager;
    private type: ObjectType<T>;
    constructor(type: ObjectType<T>, connection: DBManager) {
        this.type = type;        
        this.connection = connection;
}

public list(): T[] {
    let result: T[] = [];
    this.connection.connect().then(async  connection => {
        result = await <Promise<T[]>>(connection.entityManager.find(this.type));
    });
    return result;
 }
}

let genericReposity = new GenericRepository(Photo, new DBManager());
genericReposity.list();

Also side note, you probably don't want to connect to the database each time you request list of entities. So I recommend to redesign your class.

Upvotes: 8

Related Questions