Right now i'm building my own function for Joining Tables on a JDBC Related Database.
join-table.ts
export class JoinTable<
Class extends new (...args: any[]) => any,
Instance extends InstanceType<Class>,
Key extends keyof Instance
> {
private table: Class;
private selectedAttributes?: Key[] | null
private groupBys?: Key[]
private joinCondition: (keyof Class | keyof any) | (keyof Class | keyof any)[] | ({colA: keyof any, colB: keyof any})[] | {colA: keyof any, colB: keyof any} | undefined;
private joinedTable: JoinTable<any,any,any> | undefined;
constructor(
table: Class,
options?: {
attributes?: Key[],
groubBys?: Key[]
}
) {
this.table = table;
if (options) {
this.selectedAttributes = (options.attributes) ? options.attributes : null
this.groupBys = (options.groubBys) ? options.groubBys : []
}
}
public join<
JoinClass extends new (...args: any[]) => any,
JoinInstance extends InstanceType<JoinClass>,
JoinKey extends keyof JoinInstance
>(
table: JoinClass,
joinCondition: (Key & JoinKey) | ( Key & JoinKey)[] | ({colA: Key, colB: JoinKey})[] | {colA: Key, colB: JoinKey},
options?: {
attributes?: JoinKey[],
groubBys?: JoinKey[]
}
): JoinTable<JoinClass, JoinInstance, JoinKey> {
this.joinedTable = new JoinTable<JoinClass, JoinInstance, JoinKey>(table, options)
this.joinedTable.joinCondition = joinCondition
return this.joinedTable
}
}
lets use it like this:
export class Table1 {
a: number;
b: string;
x: boolean;
}
export class Table2 {
a: number;
b: string;
y: Object
}
export class Table3 {
a: number;
b: string;
z: Object
}
let j1 = new JoinTable(Table1).join(Table2, ['a', 'b']) // No Problem 'a' 'b' will be proposed for autocompletion like expected (type intersection)
let j2 = new JoinTable(Table1).join(Table2, ['a', 'b']).join(Table3, ['a', 'b', 'c']) // 'c' will not be proposed.
It seems like in the second call of .join() still refers to the Types of Table1.
Is this intended?