I have the following code for a query building I'm writing for fun:
export enum ColFlag {
PrimaryKey = 1,
NotNull,
Unique,
Binary,
Unsigned,
ZF,
AutoIncrement,
GeneratedColumn
}
export interface IColumn {
name: string;
alias?: string;
flags?: ColFlag[]
}
export class Column implements IColumn {
name: string = '';
alias?: string;
flags?: ColFlag[] = [];
constructor(init?: Partial<IColumn>) {
if (init) {
if (init.name) this.name = init.name;
if (init.alias) this.alias = init.alias;
if (init.flags) this.flags = init.flags;
}
}
}
export interface ITable {
name: string;
columns: IColumn[];
}
export class Table implements ITable {
name: string = '';
columns: Column[] = [];
constructor(init?: Partial<ITable>) {
if (init) {
if (init.name) this.name = init.name;
if (init.columns) this.columns = init.columns.map(c => new Column(c));
}
}
}
export class Query<T extends Table> {
table: T;
cols: Column[] = [];
constructor(table: T) {
this.table = table;
}
select(cols: (T['columns'][number]['name'])[]): this {
// do stuff
return this;
}
}
Which when used looks like:
const testTable: ITable = {
name: 'test_table',
columns: [
{
name: 'id',
flags: [ColFlag.AutoIncrement, ColFlag.NotNull, ColFlag.PrimaryKey]
},
{
name: 'value'
}
]
}
const query = new Query(testTable).select(['value']);
My current solution transpiles just fine, but it doesn't provide any meaningful intellisense. Incorrect values will also be accecpted by the transpilers. Is there any to dynamically determine the string values of the objects within the columns array to act as string literals and catch build-time errors?