@grest-ts/sql 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,24 +1,24 @@
1
- import {AliasedTable, isColumnOkToUse, Key, NotUsingWithPart, SQL_BOOL} from "../../Types";
2
- import {Expr} from "../../SqlExpression";
3
- import {OrderByMethods} from "./S5OrderBy";
4
- import {LimitMethods} from "./S6Limit";
5
- import {ExecMethods} from "./S7Exec";
6
-
7
- export interface HavingMethods<Result, Tables> extends OrderByMethods<Result, Tables>, LimitMethods<Result>, ExecMethods<Result> {
8
- having<
9
- TableRef,
10
- Columns extends (Expr<TableRef, string | unknown, SQL_BOOL>)[]
11
- >(
12
- ...col: isColumnOkToUse<Tables, Columns>
13
- ): OrderByMethods<Result, Tables>
14
-
15
- havingF<
16
- TableRef,
17
- Columns extends Expr<TableRef, string | unknown, SQL_BOOL>[]
18
- >(
19
- func: (columnsTable: AliasedTable<"(columns)", "(columns)", Result, object, NotUsingWithPart>) => isColumnOkToUse<Tables & Key<"(columns)">, Columns>
20
- ): OrderByMethods<Result, Tables> & LimitMethods<Result>
21
- }
22
-
23
-
24
-
1
+ import {AliasedTable, isColumnOkToUse, Key, NotUsingWithPart, SQL_BOOL} from "../../Types";
2
+ import {Expr} from "../../SqlExpression";
3
+ import {OrderByMethods} from "./S5OrderBy";
4
+ import {LimitMethods} from "./S6Limit";
5
+ import {ExecMethods} from "./S7Exec";
6
+
7
+ export interface HavingMethods<Result, Tables> extends OrderByMethods<Result, Tables>, LimitMethods<Result>, ExecMethods<Result> {
8
+ having<
9
+ TableRef,
10
+ Columns extends (Expr<TableRef, string | unknown, SQL_BOOL>)[]
11
+ >(
12
+ ...col: isColumnOkToUse<Tables, Columns>
13
+ ): OrderByMethods<Result, Tables>
14
+
15
+ havingF<
16
+ TableRef,
17
+ Columns extends Expr<TableRef, string | unknown, SQL_BOOL>[]
18
+ >(
19
+ func: (columnsTable: AliasedTable<"(columns)", "(columns)", Result, object, NotUsingWithPart>) => isColumnOkToUse<Tables & Key<"(columns)">, Columns>
20
+ ): OrderByMethods<Result, Tables> & LimitMethods<Result>
21
+ }
22
+
23
+
24
+
@@ -1,21 +1,21 @@
1
- import {AliasedTable, isColumnOkToUse, Key, NotUsingWithPart, OrderByStructure} from "../../Types";
2
- import {LimitMethods} from "./S6Limit";
3
- import {Expr} from "../../SqlExpression";
4
- import {ExecMethods} from "./S7Exec";
5
-
6
- export interface OrderByMethods<Result, Tables> extends LimitMethods<Result>, ExecMethods<Result> {
7
- orderBy<
8
- TableRef,
9
- Columns extends OrderByStructure<Expr<TableRef, string | unknown, any>>
10
- >(
11
- ...items: isColumnOkToUse<Tables, Columns>
12
- ): LimitMethods<Result>
13
-
14
- orderByF<
15
- TableRef,
16
- Columns extends OrderByStructure<Expr<TableRef, string | unknown, string | number>>
17
- >(
18
- func: (columnsTable: AliasedTable<"(columns)", "(columns)", Result, object, NotUsingWithPart>) => isColumnOkToUse<Tables & Key<"(columns)">, Columns>
19
- ): LimitMethods<Result>
20
-
21
- }
1
+ import {AliasedTable, isColumnOkToUse, Key, NotUsingWithPart, OrderByStructure} from "../../Types";
2
+ import {LimitMethods} from "./S6Limit";
3
+ import {Expr} from "../../SqlExpression";
4
+ import {ExecMethods} from "./S7Exec";
5
+
6
+ export interface OrderByMethods<Result, Tables> extends LimitMethods<Result>, ExecMethods<Result> {
7
+ orderBy<
8
+ TableRef,
9
+ Columns extends OrderByStructure<Expr<TableRef, string | unknown, any>>
10
+ >(
11
+ ...items: isColumnOkToUse<Tables, Columns>
12
+ ): LimitMethods<Result>
13
+
14
+ orderByF<
15
+ TableRef,
16
+ Columns extends OrderByStructure<Expr<TableRef, string | unknown, string | number>>
17
+ >(
18
+ func: (columnsTable: AliasedTable<"(columns)", "(columns)", Result, object, NotUsingWithPart>) => isColumnOkToUse<Tables & Key<"(columns)">, Columns>
19
+ ): LimitMethods<Result>
20
+
21
+ }
@@ -1,9 +1,9 @@
1
- import {ExecMethods} from "./S7Exec";
2
-
3
- export interface LimitMethods<Result> extends ExecMethods<Result> {
4
- noLimit(): ExecMethods<Result>
5
-
6
- limit(limit: number | [number, number]): ExecMethods<Result>
7
-
8
- limit1(): ExecMethods<Result>
9
- }
1
+ import {ExecMethods} from "./S7Exec";
2
+
3
+ export interface LimitMethods<Result> extends ExecMethods<Result> {
4
+ noLimit(): ExecMethods<Result>
5
+
6
+ limit(limit: number | [number, number]): ExecMethods<Result>
7
+
8
+ limit1(): ExecMethods<Result>
9
+ }
@@ -1,41 +1,41 @@
1
- import {AliasedTable, NotUsingWithPart, SelectExecutor, SqlSelectQuery} from "../../Types";
2
- import {Expr} from "../../SqlExpression";
3
- import {UnionMethods} from "./S0Union";
4
-
5
- export interface ExecMethods<Result> extends SqlSelectQuery<Result>, UnionMethods<Result> {
6
-
7
- forUpdate(): ExecMethods<Result>
8
-
9
- asScalar<Alias extends string>(
10
- alias: Alias & ScalarSubQueryAllowsOnlyOneColumn<Alias, Result> extends never ? "Scalar subquery allows only 1 column!" : Alias
11
- ): Expr<never, Alias, Result[keyof Result]>
12
-
13
- as<Alias extends string>(alias: Alias): AliasedTable<Alias, `(SUBQUERY) as ${Alias}`, Result, object, NotUsingWithPart>
14
-
15
- /**
16
- * Example of an executor method.
17
- * Notice that it can force existence of other arguments in the exec call. In this case forcing ctx to be added.
18
- * async <Result>(sql: string, ctx: Ctx): Promise<Result> => {
19
- * return someDb.query(ctx, sql);
20
- * }
21
- */
22
- exec<Args extends any[]>(executor: SelectExecutor<Result, Args>, ...args: Args): Promise<Result[]>
23
-
24
- /**
25
- * Same as exec, but returns one row
26
- */
27
- execOne<Args extends any[]>(executor: SelectExecutor<Result, Args>, ...args: Args): Promise<Result>
28
-
29
- }
30
-
31
- /**
32
- * Basically checks that Result would have only one column defined.
33
- */
34
- type ScalarSubQueryAllowsOnlyOneColumn<Result, T, Keys extends keyof any = keyof T> =
35
- Keys extends any
36
- ? T extends Record<Keys, any>
37
- ? Exclude<keyof T, Keys> extends never
38
- ? Result
39
- : never
40
- : never
41
- : never;
1
+ import {AliasedTable, NotUsingWithPart, SelectExecutor, SqlSelectQuery} from "../../Types";
2
+ import {Expr} from "../../SqlExpression";
3
+ import {UnionMethods} from "./S0Union";
4
+
5
+ export interface ExecMethods<Result> extends SqlSelectQuery<Result>, UnionMethods<Result> {
6
+
7
+ forUpdate(): ExecMethods<Result>
8
+
9
+ asScalar<Alias extends string>(
10
+ alias: Alias & ScalarSubQueryAllowsOnlyOneColumn<Alias, Result> extends never ? "Scalar subquery allows only 1 column!" : Alias
11
+ ): Expr<never, Alias, Result[keyof Result]>
12
+
13
+ as<Alias extends string>(alias: Alias): AliasedTable<Alias, `(SUBQUERY) as ${Alias}`, Result, object, NotUsingWithPart>
14
+
15
+ /**
16
+ * Example of an executor method.
17
+ * Notice that it can force existence of other arguments in the exec call. In this case forcing ctx to be added.
18
+ * async <Result>(sql: string, ctx: Ctx): Promise<Result> => {
19
+ * return someDb.query(ctx, sql);
20
+ * }
21
+ */
22
+ exec<Args extends any[]>(executor: SelectExecutor<Result, Args>, ...args: Args): Promise<Result[]>
23
+
24
+ /**
25
+ * Same as exec, but returns one row
26
+ */
27
+ execOne<Args extends any[]>(executor: SelectExecutor<Result, Args>, ...args: Args): Promise<Result>
28
+
29
+ }
30
+
31
+ /**
32
+ * Basically checks that Result would have only one column defined.
33
+ */
34
+ type ScalarSubQueryAllowsOnlyOneColumn<Result, T, Keys extends keyof any = keyof T> =
35
+ Keys extends any
36
+ ? T extends Record<Keys, any>
37
+ ? Exclude<keyof T, Keys> extends never
38
+ ? Result
39
+ : never
40
+ : never
41
+ : never;
@@ -1,96 +1,96 @@
1
- import {escape, escapeId} from "../escape";
2
- import {SQL_ENTITY, SQL_EXPRESSION} from "../Symbols";
3
- import {ExecUpdateMethods, GatewayUpdateOrderByMethods, GatewayUpdateWhereMethods, UpdateLimitMethods, UpdateOrderByMethods, UpdateSetMethods, UpdateWhereMethods} from "./UpdateInterfaces";
4
- import {AnyExpr, SqlExpression} from "../SqlExpression";
5
- import {OrderByStructure} from "../Types";
6
- import {orderByStructureToSqlString, toSql} from "../SqlFunctions";
7
-
8
- export class UpdateBuilder<Entity, Tables> implements ExecUpdateMethods,
9
- GatewayUpdateWhereMethods<Entity>,
10
- GatewayUpdateOrderByMethods<Entity>,
11
- UpdateSetMethods<Entity, Tables>,
12
- UpdateWhereMethods<Tables>,
13
- UpdateOrderByMethods<Tables>,
14
- UpdateLimitMethods {
15
-
16
- public readonly [SQL_ENTITY]: { affectedRows: number; }; // never used
17
-
18
- private _tableName: string;
19
- private _joins: string[] = [];
20
- private _set: string[] = []
21
- private readonly _where: string[] = [];
22
- private readonly _orderBy: string[] = [];
23
- private _limit: string;
24
- private _ignore: boolean = false;
25
-
26
- public in(tableName: string, alias?: string) {
27
- this._tableName = tableName + (alias ? " as " + escapeId(alias) : "");
28
- return this;
29
- }
30
-
31
- public ignore() {
32
- this._ignore = true;
33
- return this
34
- }
35
-
36
- public join(table: any): this {
37
- this._joins.push(table[SQL_EXPRESSION])
38
- return this;
39
- }
40
-
41
- public set(obj: any): this {
42
- for (const prop in obj) {
43
- this._set.push(escapeId(prop) + " = " + toSql(obj[prop]));
44
- }
45
- return this
46
- }
47
-
48
- public where(...cols: any | SqlExpression<any, any, any>): this {
49
- for (let i = 0; i < cols.length; i++) {
50
- const col = cols[i];
51
- if (col instanceof SqlExpression) {
52
- this._where.push(col.expression)
53
- } else {
54
- for (const k in col) {
55
- this._where.push(escapeId(k) + " = " + escape(col[k]))
56
- }
57
- }
58
- }
59
- return this
60
- }
61
-
62
- public noWhere() {
63
- return this;
64
- }
65
-
66
- public orderBy(...items: any | OrderByStructure<AnyExpr>): this {
67
- this._orderBy.push(...orderByStructureToSqlString(items))
68
- return this;
69
- }
70
-
71
- public noLimit(): this {
72
- return this;
73
- }
74
-
75
- public limit1(): this {
76
- return this.limit(1);
77
- }
78
-
79
- public limit(limit: number | [number, number]): this {
80
- if (Array.isArray(limit)) {
81
- this._limit = Number(limit[0]) + "," + Number(limit[1]);
82
- } else {
83
- this._limit = String(Number(limit));
84
- }
85
- return this;
86
- }
87
-
88
- public toSqlString() {
89
- return "UPDATE " + (this._ignore ? "IGNORE " : "") + this._tableName + " " +
90
- (this._joins.length > 0 ? ", \n" + this._joins.join(",\n") : "") +
91
- (this._set.length > 0 ? "SET \n\t" + this._set.join(",\n\t") + " \n" : "") +
92
- (this._where.length > 0 ? "WHERE " + this._where.join(" AND ") + " \n" : "") +
93
- (this._orderBy.length > 0 ? "ORDER BY " + this._orderBy.join(", ") + " \n" : "") +
94
- (this._limit ? "LIMIT " + this._limit : "")
95
- }
1
+ import {escape, escapeId} from "../escape";
2
+ import {SQL_ENTITY, SQL_EXPRESSION} from "../Symbols";
3
+ import {ExecUpdateMethods, GatewayUpdateOrderByMethods, GatewayUpdateWhereMethods, UpdateLimitMethods, UpdateOrderByMethods, UpdateSetMethods, UpdateWhereMethods} from "./UpdateInterfaces";
4
+ import {AnyExpr, SqlExpression} from "../SqlExpression";
5
+ import {OrderByStructure} from "../Types";
6
+ import {orderByStructureToSqlString, toSql} from "../SqlFunctions";
7
+
8
+ export class UpdateBuilder<Entity, Tables> implements ExecUpdateMethods,
9
+ GatewayUpdateWhereMethods<Entity>,
10
+ GatewayUpdateOrderByMethods<Entity>,
11
+ UpdateSetMethods<Entity, Tables>,
12
+ UpdateWhereMethods<Tables>,
13
+ UpdateOrderByMethods<Tables>,
14
+ UpdateLimitMethods {
15
+
16
+ public readonly [SQL_ENTITY]: { affectedRows: number; }; // never used
17
+
18
+ private _tableName: string;
19
+ private _joins: string[] = [];
20
+ private _set: string[] = []
21
+ private readonly _where: string[] = [];
22
+ private readonly _orderBy: string[] = [];
23
+ private _limit: string;
24
+ private _ignore: boolean = false;
25
+
26
+ public in(tableName: string, alias?: string) {
27
+ this._tableName = tableName + (alias ? " as " + escapeId(alias) : "");
28
+ return this;
29
+ }
30
+
31
+ public ignore() {
32
+ this._ignore = true;
33
+ return this
34
+ }
35
+
36
+ public join(table: any): this {
37
+ this._joins.push(table[SQL_EXPRESSION])
38
+ return this;
39
+ }
40
+
41
+ public set(obj: any): this {
42
+ for (const prop in obj) {
43
+ this._set.push(escapeId(prop) + " = " + toSql(obj[prop]));
44
+ }
45
+ return this
46
+ }
47
+
48
+ public where(...cols: any | SqlExpression<any, any, any>): this {
49
+ for (let i = 0; i < cols.length; i++) {
50
+ const col = cols[i];
51
+ if (col instanceof SqlExpression) {
52
+ this._where.push(col.expression)
53
+ } else {
54
+ for (const k in col) {
55
+ this._where.push(escapeId(k) + " = " + escape(col[k]))
56
+ }
57
+ }
58
+ }
59
+ return this
60
+ }
61
+
62
+ public noWhere() {
63
+ return this;
64
+ }
65
+
66
+ public orderBy(...items: any | OrderByStructure<AnyExpr>): this {
67
+ this._orderBy.push(...orderByStructureToSqlString(items))
68
+ return this;
69
+ }
70
+
71
+ public noLimit(): this {
72
+ return this;
73
+ }
74
+
75
+ public limit1(): this {
76
+ return this.limit(1);
77
+ }
78
+
79
+ public limit(limit: number | [number, number]): this {
80
+ if (Array.isArray(limit)) {
81
+ this._limit = Number(limit[0]) + "," + Number(limit[1]);
82
+ } else {
83
+ this._limit = String(Number(limit));
84
+ }
85
+ return this;
86
+ }
87
+
88
+ public toSqlString() {
89
+ return "UPDATE " + (this._ignore ? "IGNORE " : "") + this._tableName + " " +
90
+ (this._joins.length > 0 ? ", \n" + this._joins.join(",\n") : "") +
91
+ (this._set.length > 0 ? "SET \n\t" + this._set.join(",\n\t") + " \n" : "") +
92
+ (this._where.length > 0 ? "WHERE " + this._where.join(" AND ") + " \n" : "") +
93
+ (this._orderBy.length > 0 ? "ORDER BY " + this._orderBy.join(", ") + " \n" : "") +
94
+ (this._limit ? "LIMIT " + this._limit : "")
95
+ }
96
96
  }
@@ -1,87 +1,87 @@
1
- import {AliasedTable, isColumnOkToUse, isTableReferenced, Key, NotUsingWithPart, OrderByStructure, SQL_BOOL, SqlQuery} from "../Types";
2
- import {Expr} from "../SqlExpression";
3
-
4
- export interface GatewayUpdateWhereMethods<Entity> {
5
-
6
- where(obj: Partial<Entity>): GatewayUpdateOrderByMethods<Entity>
7
-
8
- }
9
-
10
- export interface GatewayUpdateOrderByMethods<Entity> extends UpdateLimitMethods {
11
-
12
- orderBy(fields: keyof Entity): UpdateLimitMethods
13
-
14
- }
15
-
16
- export interface UpdateSetMethods<Entity, Tables> {
17
-
18
- set(value: PartialAndCheckExpr<Entity, Tables>): UpdateWhereMethods<Tables>
19
-
20
- join<
21
- Alias extends string,
22
- TableRef extends `${string} as ${Alias}`,
23
- >(
24
- table: AliasedTable<Alias, TableRef, object, object, NotUsingWithPart>
25
- ): UpdateSetMethods<Entity, Tables & Key<TableRef>>;
26
-
27
- }
28
-
29
- type PartialAndCheckExpr<Entity, Tables> = {
30
- [P in keyof Entity]?: Entity[P] | Expr<keyof Tables, string, Entity[P]>
31
- };
32
-
33
- export interface UpdateWhereMethods<Tables> {
34
-
35
- noWhere(): UpdateOrderByMethods<Tables>
36
-
37
- where<
38
- T1 extends string,
39
- T2 extends string = never,
40
- T3 extends string = never,
41
- T4 extends string = never,
42
- T5 extends string = never,
43
- T6 extends string = never,
44
- T7 extends string = never,
45
- T8 extends string = never,
46
- T9 extends string = never,
47
- T10 extends string = never,
48
- T = Tables
49
- >(
50
- c1: C<T, T1>,
51
- c2?: C<T, T2>,
52
- c3?: C<T, T3>,
53
- c4?: C<T, T4>,
54
- c5?: C<T, T5>,
55
- c6?: C<T, T6>,
56
- c7?: C<T, T7>,
57
- c8?: C<T, T8>,
58
- c9?: C<T, T9>,
59
- c10?: C<T, T10>
60
- ): UpdateOrderByMethods<Tables>
61
-
62
- }
63
-
64
- type C<Tables, T extends string> = isTableReferenced<Tables, Key<T>, Expr<T, unknown, SQL_BOOL>>
65
-
66
- export interface UpdateOrderByMethods<Tables> extends UpdateLimitMethods {
67
-
68
- orderBy<
69
- TableRef,
70
- Columns extends OrderByStructure<Expr<TableRef, string | unknown, any>>
71
- >(
72
- ...items: isColumnOkToUse<Tables, Columns>
73
- ): UpdateLimitMethods
74
-
75
- }
76
-
77
- export interface UpdateLimitMethods {
78
- noLimit(): ExecUpdateMethods
79
-
80
- limit(limit: number | [number, number]): ExecUpdateMethods
81
-
82
- limit1(): ExecUpdateMethods
83
- }
84
-
85
- export interface ExecUpdateMethods extends SqlQuery<{ affectedRows: number }> {
86
-
87
- }
1
+ import {AliasedTable, isColumnOkToUse, isTableReferenced, Key, NotUsingWithPart, OrderByStructure, SQL_BOOL, SqlQuery} from "../Types";
2
+ import {Expr} from "../SqlExpression";
3
+
4
+ export interface GatewayUpdateWhereMethods<Entity> {
5
+
6
+ where(obj: Partial<Entity>): GatewayUpdateOrderByMethods<Entity>
7
+
8
+ }
9
+
10
+ export interface GatewayUpdateOrderByMethods<Entity> extends UpdateLimitMethods {
11
+
12
+ orderBy(fields: keyof Entity): UpdateLimitMethods
13
+
14
+ }
15
+
16
+ export interface UpdateSetMethods<Entity, Tables> {
17
+
18
+ set(value: PartialAndCheckExpr<Entity, Tables>): UpdateWhereMethods<Tables>
19
+
20
+ join<
21
+ Alias extends string,
22
+ TableRef extends `${string} as ${Alias}`,
23
+ >(
24
+ table: AliasedTable<Alias, TableRef, object, object, NotUsingWithPart>
25
+ ): UpdateSetMethods<Entity, Tables & Key<TableRef>>;
26
+
27
+ }
28
+
29
+ type PartialAndCheckExpr<Entity, Tables> = {
30
+ [P in keyof Entity]?: Entity[P] | Expr<keyof Tables, string, Entity[P]>
31
+ };
32
+
33
+ export interface UpdateWhereMethods<Tables> {
34
+
35
+ noWhere(): UpdateOrderByMethods<Tables>
36
+
37
+ where<
38
+ T1 extends string,
39
+ T2 extends string = never,
40
+ T3 extends string = never,
41
+ T4 extends string = never,
42
+ T5 extends string = never,
43
+ T6 extends string = never,
44
+ T7 extends string = never,
45
+ T8 extends string = never,
46
+ T9 extends string = never,
47
+ T10 extends string = never,
48
+ T = Tables
49
+ >(
50
+ c1: C<T, T1>,
51
+ c2?: C<T, T2>,
52
+ c3?: C<T, T3>,
53
+ c4?: C<T, T4>,
54
+ c5?: C<T, T5>,
55
+ c6?: C<T, T6>,
56
+ c7?: C<T, T7>,
57
+ c8?: C<T, T8>,
58
+ c9?: C<T, T9>,
59
+ c10?: C<T, T10>
60
+ ): UpdateOrderByMethods<Tables>
61
+
62
+ }
63
+
64
+ type C<Tables, T extends string> = isTableReferenced<Tables, Key<T>, Expr<T, unknown, SQL_BOOL>>
65
+
66
+ export interface UpdateOrderByMethods<Tables> extends UpdateLimitMethods {
67
+
68
+ orderBy<
69
+ TableRef,
70
+ Columns extends OrderByStructure<Expr<TableRef, string | unknown, any>>
71
+ >(
72
+ ...items: isColumnOkToUse<Tables, Columns>
73
+ ): UpdateLimitMethods
74
+
75
+ }
76
+
77
+ export interface UpdateLimitMethods {
78
+ noLimit(): ExecUpdateMethods
79
+
80
+ limit(limit: number | [number, number]): ExecUpdateMethods
81
+
82
+ limit1(): ExecUpdateMethods
83
+ }
84
+
85
+ export interface ExecUpdateMethods extends SqlQuery<{ affectedRows: number }> {
86
+
87
+ }