Typescript – Builder Pattern and Inheritance

Using Typescript, I want to make a class of Builder of abstract class and follow a Builder Pattern and the child class will implement the details.

The parent abstract class:

export abstract class SQLQueryBuilder{

   // private tableName: string;
   private configs: ConfigsHelper;
   readonly TABLE_PREFIX_CONFIG_NAME: string = "tablePrefix";

    constructor(configs: HOption[]){
        this.configs = new ConfigsHelper(this._getCompulsoryFields());
        this.configs.loadFromHOption(configs);
    }

    protected getConfig(name: string): string{
        return this.configs.get(name);
    }

    protected _getCompulsoryFields(): string[]{
        return [this.TABLE_PREFIX_CONFIG_NAME];
    }

    public abstract build(tableName: string): SQLQueryBuilder;
  
}//class

The child class:

export class KnexSQLQueryBuilder extends SQLQueryBuilder{

    private knex = null;
    private query: Knex.QueryBuilder = null;

    constructor(configs: HOption[]){
        super(configs);

        const knexConfigs = {
            client: super.getConfig("client"),
            connection: {
              host : super.getConfig("host"),
              user : super.getConfig("user"),
              password : super.getConfig("password"),
              database : super.getConfig("database")
            }
        };

        this.knex = Knex(knexConfigs);
    }

    public build(tableName: string): KnexSQLQueryBuilder{
        this.query = this.knex(tableName);
        return this;
    }

}//class

If you notice, you need to change the return of build() function into child class name.

typescript builder pattern and inheritance - child class
typescript builder pattern and inheritance – child class