diff --git a/docs/api/classes/common-table-expression.ts.md b/docs/api/classes/common-table-expression.ts.md index 193a115..1869ba5 100644 --- a/docs/api/classes/common-table-expression.ts.md +++ b/docs/api/classes/common-table-expression.ts.md @@ -46,7 +46,7 @@ Added in v1.0.0 with_: ( alias: Alias2, select: (acc: { - [K in Aliases]: Table, K>; + [K in Aliases]: Table, K>; }) => SelectStatement ) => CommonTableExpressionFactory< @@ -66,7 +66,7 @@ withR: ( alias: Alias2, columns: readonly Selection2[], select: (acc: { - [K in Aliases]: Table, K>; + [K in Aliases]: Table, K>; }) => SelectStatement ) => CommonTableExpressionFactory< diff --git a/docs/api/classes/compound.ts.md b/docs/api/classes/compound.ts.md index e449182..31a5675 100644 --- a/docs/api/classes/compound.ts.md +++ b/docs/api/classes/compound.ts.md @@ -109,12 +109,13 @@ Added in v0.0.0 ```ts joinTable: < Alias1 extends string, + Scope2 extends string, Selection2 extends string, Alias2 extends string >( thisCompoundAlias: Alias1, operator: string, - table: Table + table: Table ) => JoinedFactory< Selection, @@ -137,11 +138,12 @@ Added in v0.0.0 ```ts commaJoinTable: < Alias1 extends string, + Scope2 extends string, Selection2 extends string, Alias2 extends string >( thisSelectAlias: Alias1, - table: Table + table: Table ) => Joined< Selection, diff --git a/docs/api/classes/joined.ts.md b/docs/api/classes/joined.ts.md index 3e52f69..5d92b15 100644 --- a/docs/api/classes/joined.ts.md +++ b/docs/api/classes/joined.ts.md @@ -95,8 +95,12 @@ Added in v0.0.0 **Signature** ```ts -commaJoinTable: ( - table: Table +commaJoinTable: < + Scope2 extends string, + Selection2 extends string, + Alias2 extends string +>( + table: Table ) => Joined< Selection, @@ -117,9 +121,13 @@ Added in v0.0.0 **Signature** ```ts -joinTable: ( +joinTable: < + Scope2 extends string, + Selection2 extends string, + Alias2 extends string +>( operator: string, - table: Table + table: Table ) => JoinedFactory< Selection, diff --git a/docs/api/classes/select-statement.ts.md b/docs/api/classes/select-statement.ts.md index 2e309bd..4170e74 100644 --- a/docs/api/classes/select-statement.ts.md +++ b/docs/api/classes/select-statement.ts.md @@ -225,11 +225,12 @@ Added in v0.0.0 ```ts commaJoinTable: < Alias1 extends string, + Scope2 extends string, Selection2 extends string, Alias2 extends string >( thisQueryAlias: Alias1, - table: Table + table: Table ) => Joined< Selection, @@ -250,12 +251,13 @@ Added in v0.0.0 ```ts joinTable: < Alias1 extends string, + Scope2 extends string, Selection2 extends string, Alias2 extends string >( thisQueryAlias: Alias1, operator: string, - table: Table + table: Table ) => JoinedFactory< Selection, diff --git a/docs/api/classes/stringified-select-statement.ts.md b/docs/api/classes/stringified-select-statement.ts.md index 4a99245..20b5acf 100644 --- a/docs/api/classes/stringified-select-statement.ts.md +++ b/docs/api/classes/stringified-select-statement.ts.md @@ -78,11 +78,12 @@ Added in v0.0.3 ```ts commaJoinTable: < Alias1 extends string, + Scope2 extends string, Selection2 extends string, Alias2 extends string >( thisQueryAlias: Alias1, - table: Table + table: Table ) => Joined< Selection, @@ -103,12 +104,13 @@ Added in v0.0.3 ```ts joinTable: < Alias1 extends string, + Scope2 extends string, Selection2 extends string, Alias2 extends string >( thisQueryAlias: Alias1, operator: string, - table: Table + table: Table ) => JoinedFactory< Selection, diff --git a/docs/api/classes/table.ts.md b/docs/api/classes/table.ts.md index 87ce7bd..884916a 100644 --- a/docs/api/classes/table.ts.md +++ b/docs/api/classes/table.ts.md @@ -36,7 +36,7 @@ This class is not meant to be used directly, but rather through the `table` func **Signature** ```ts -export declare class Table { +export declare class Table { private constructor( /* @internal */ public __props: { @@ -57,7 +57,7 @@ Added in v0.0.0 ```ts clickhouse: { - final: () => Table; + final: () => Table; } ``` @@ -103,8 +103,12 @@ Added in v0.0.0 **Signature** ```ts -commaJoinTable: ( - table: Table +commaJoinTable: < + Scope2 extends string, + Selection2 extends string, + Alias2 extends string +>( + table: Table ) => Joined< Selection, @@ -124,9 +128,13 @@ Added in v0.0.0 **Signature** ```ts -joinTable: ( +joinTable: < + Scope2 extends string, + Selection2 extends string, + Alias2 extends string +>( operator: string, - table: Table + table: Table ) => JoinedFactory< Selection, diff --git a/docs/api/index.ts.md b/docs/api/index.ts.md index f78a720..55a4c9b 100644 --- a/docs/api/index.ts.md +++ b/docs/api/index.ts.md @@ -221,10 +221,11 @@ export declare const select: < f: | readonly SubSelection[] | (( - f: Record & NoSelectFieldsCompileError + f: Record & + NoSelectFieldsCompileError ) => Record), from: TableOrSubquery -) => SelectStatement; +) => SelectStatement; ``` Added in v1.0.0 @@ -259,7 +260,7 @@ export declare const table: ( columns: readonly Selection[], alias: Alias, name?: string -) => Table; +) => Table<`${Alias}.${Selection}`, Selection, Alias>; ``` **Example** diff --git a/src/classes/common-table-expression.ts b/src/classes/common-table-expression.ts index ee776c0..d4b5457 100644 --- a/src/classes/common-table-expression.ts +++ b/src/classes/common-table-expression.ts @@ -63,7 +63,7 @@ export class CommonTableExpressionFactory< public with_ = ( alias: Alias2, select: (acc: { - [K in Aliases]: Table, K>; + [K in Aliases]: Table, K>; }) => SelectStatement ): CommonTableExpressionFactory< `${Alias2}.${Selection2}` | Scope, @@ -86,7 +86,7 @@ export class CommonTableExpressionFactory< alias: Alias2, columns: ReadonlyArray, select: (acc: { - [K in Aliases]: Table, K>; + [K in Aliases]: Table, K>; }) => SelectStatement ): CommonTableExpressionFactory< `${Alias2}.${Selection2}` | Scope, diff --git a/src/classes/compound.ts b/src/classes/compound.ts index bcce190..cd07c78 100644 --- a/src/classes/compound.ts +++ b/src/classes/compound.ts @@ -188,12 +188,13 @@ export class Compound { */ public joinTable = < Alias1 extends string, + Scope2 extends string, Selection2 extends string, Alias2 extends string >( thisCompoundAlias: Alias1, operator: string, - table: Table + table: Table ): JoinedFactory< Selection, | Exclude @@ -223,11 +224,12 @@ export class Compound { */ public commaJoinTable = < Alias1 extends string, + Scope2 extends string, Selection2 extends string, Alias2 extends string >( thisSelectAlias: Alias1, - table: Table + table: Table ): Joined< Selection, | Exclude diff --git a/src/classes/joined.ts b/src/classes/joined.ts index 8e9d759..94896e9 100644 --- a/src/classes/joined.ts +++ b/src/classes/joined.ts @@ -188,8 +188,12 @@ export class Joined< /** * @since 0.0.0 */ - public commaJoinTable = ( - table: Table + public commaJoinTable = < + Scope2 extends string, + Selection2 extends string, + Alias2 extends string + >( + table: Table ): Joined< Selection, | Scope @@ -214,9 +218,13 @@ export class Joined< /** * @since 0.0.0 */ - public joinTable = ( + public joinTable = < + Scope2 extends string, + Selection2 extends string, + Alias2 extends string + >( operator: string, - table: Table + table: Table ): JoinedFactory< Selection, | Scope diff --git a/src/classes/select-statement.ts b/src/classes/select-statement.ts index 807919a..957fedb 100644 --- a/src/classes/select-statement.ts +++ b/src/classes/select-statement.ts @@ -369,11 +369,12 @@ export class SelectStatement { */ public commaJoinTable = < Alias1 extends string, + Scope2 extends string, Selection2 extends string, Alias2 extends string >( thisQueryAlias: Alias1, - table: Table + table: Table ): Joined< Selection, | Exclude @@ -398,12 +399,13 @@ export class SelectStatement { */ public joinTable = < Alias1 extends string, + Scope2 extends string, Selection2 extends string, Alias2 extends string >( thisQueryAlias: Alias1, operator: string, - table: Table + table: Table ): JoinedFactory< Selection, | Exclude diff --git a/src/classes/stringified-select-statement.ts b/src/classes/stringified-select-statement.ts index 585a8b7..3f7e856 100644 --- a/src/classes/stringified-select-statement.ts +++ b/src/classes/stringified-select-statement.ts @@ -66,11 +66,12 @@ export class StringifiedSelectStatement { */ public commaJoinTable = < Alias1 extends string, + Scope2 extends string, Selection2 extends string, Alias2 extends string >( thisQueryAlias: Alias1, - table: Table + table: Table ): Joined< Selection, | Exclude @@ -95,12 +96,13 @@ export class StringifiedSelectStatement { */ public joinTable = < Alias1 extends string, + Scope2 extends string, Selection2 extends string, Alias2 extends string >( thisQueryAlias: Alias1, operator: string, - table: Table + table: Table ): JoinedFactory< Selection, | Exclude diff --git a/src/classes/table.ts b/src/classes/table.ts index dcae909..bf19337 100644 --- a/src/classes/table.ts +++ b/src/classes/table.ts @@ -25,7 +25,11 @@ import { StringifiedSelectStatement } from "./stringified-select-statement"; * * @since 0.0.0 */ -export class Table { +export class Table< + Scope extends string, + Selection extends string, + Alias extends string +> { /* @internal */ private constructor( /* @internal */ @@ -42,10 +46,10 @@ export class Table { columns: ReadonlyArray, alias: Alias, name: string = alias - ): Table => + ): Table<`${Alias}.${Selection}`, Selection, Alias> => new Table({ columns, alias, name, final: false }); - private copy = (): Table => + private copy = (): Table => new Table({ ...this.__props }); private setFinal = (final: boolean): this => { @@ -60,7 +64,7 @@ export class Table { /** * @since 0.0.0 */ - final: (): Table => this.copy().setFinal(true), + final: (): Table => this.copy().setFinal(true), }; /** @@ -95,8 +99,12 @@ export class Table { /** * @since 0.0.0 */ - public commaJoinTable = ( - table: Table + public commaJoinTable = < + Scope2 extends string, + Selection2 extends string, + Alias2 extends string + >( + table: Table ): Joined< Selection, | Exclude @@ -120,9 +128,13 @@ export class Table { /** * @since 0.0.0 */ - public joinTable = ( + public joinTable = < + Scope2 extends string, + Selection2 extends string, + Alias2 extends string + >( operator: string, - table: Table + table: Table ): JoinedFactory< Selection, | Exclude diff --git a/src/index.ts b/src/index.ts index e1c836d..665bb4d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -298,10 +298,11 @@ export const select = < f: | ReadonlyArray | (( - f: Record & NoSelectFieldsCompileError + f: Record & + NoSelectFieldsCompileError ) => Record), from: TableOrSubquery -): SelectStatement => +): SelectStatement => //@ts-expect-error from.select(f); diff --git a/src/print.ts b/src/print.ts index ea1d840..3619475 100644 --- a/src/print.ts +++ b/src/print.ts @@ -60,8 +60,12 @@ const printStringifiedSelectInternal = ( it: StringifiedSelectStatement ): PrintInternalRet => `(${it.__props.content.content})`; -const printTableInternal = ( - table: Table +const printTableInternal = < + Scope extends string, + Selection extends string, + Alias extends string +>( + table: Table ): PrintInternalRet => { const final = table.__props.final ? ` FINAL` : ""; if (table.__props.name === table.__props.alias) { diff --git a/src/types.ts b/src/types.ts index b0f172b..a1ee43c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -18,7 +18,7 @@ export type TableOrSubquery< > = | SelectStatement | StringifiedSelectStatement - | Table + | Table | Joined | Compound; diff --git a/tests/unit/select.test.ts b/tests/unit/select.test.ts new file mode 100644 index 0000000..6fd6788 --- /dev/null +++ b/tests/unit/select.test.ts @@ -0,0 +1,9 @@ +import { select, table } from "../../src"; +const cols = ["a", "b"] as const; +it("alias from table", () => { + const t = table(cols, "t"); + + t.select((f) => ({ abc: f["t.a"] })); + select((f) => ({ abc: f["t.a"] }), t); + expect(1).toBe(1); +});