@grest-ts/sql 0.0.51 → 0.0.55

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.
Files changed (46) hide show
  1. package/dist/src/MysqlTable.js +1 -1
  2. package/dist/src/MysqlTable.js.map +1 -1
  3. package/dist/src/MysqlTableStructureParser.d.ts +1 -1
  4. package/dist/src/MysqlTableStructureParser.d.ts.map +1 -1
  5. package/dist/src/MysqlTableStructureParser.js +6 -4
  6. package/dist/src/MysqlTableStructureParser.js.map +1 -1
  7. package/dist/src/SQL.js.map +1 -1
  8. package/dist/src/SqlExpression.d.ts +2 -2
  9. package/dist/src/SqlExpression.d.ts.map +1 -1
  10. package/dist/src/SqlExpression.js +2 -2
  11. package/dist/src/SqlExpression.js.map +1 -1
  12. package/dist/src/SqlFunctions.d.ts +1 -1
  13. package/dist/src/SqlFunctions.d.ts.map +1 -1
  14. package/dist/src/SqlFunctions.js.map +1 -1
  15. package/dist/src/_dedupCheck.js +1 -1
  16. package/dist/src/delete/DeleteBuilder.d.ts +4 -2
  17. package/dist/src/delete/DeleteBuilder.d.ts.map +1 -1
  18. package/dist/src/delete/DeleteBuilder.js +3 -3
  19. package/dist/src/delete/DeleteBuilder.js.map +1 -1
  20. package/dist/src/insert/InsertBuilder.d.ts +3 -1
  21. package/dist/src/insert/InsertBuilder.d.ts.map +1 -1
  22. package/dist/src/insert/InsertBuilder.js +5 -5
  23. package/dist/src/insert/InsertBuilder.js.map +1 -1
  24. package/dist/src/select/SelectBuilder.d.ts +1 -1
  25. package/dist/src/select/SelectBuilder.d.ts.map +1 -1
  26. package/dist/src/select/SelectBuilder.js +3 -3
  27. package/dist/src/select/SelectBuilder.js.map +1 -1
  28. package/dist/src/select/parts/S2Columns.d.ts +14 -10
  29. package/dist/src/select/parts/S2Columns.d.ts.map +1 -1
  30. package/dist/src/select/parts/S2Columns.js.map +1 -1
  31. package/dist/src/update/UpdateBuilder.d.ts.map +1 -1
  32. package/dist/src/update/UpdateBuilder.js +3 -3
  33. package/dist/src/update/UpdateBuilder.js.map +1 -1
  34. package/dist/tsconfig.publish.tsbuildinfo +1 -1
  35. package/package.json +2 -2
  36. package/src/MysqlTable.ts +1 -1
  37. package/src/MysqlTableStructureParser.ts +12 -10
  38. package/src/SQL.ts +3 -3
  39. package/src/SqlExpression.ts +9 -9
  40. package/src/SqlFunctions.ts +3 -3
  41. package/src/_dedupCheck.ts +1 -1
  42. package/src/delete/DeleteBuilder.ts +4 -4
  43. package/src/insert/InsertBuilder.ts +5 -5
  44. package/src/select/SelectBuilder.ts +3 -3
  45. package/src/select/parts/S2Columns.ts +17 -11
  46. package/src/update/UpdateBuilder.ts +3 -3
package/src/SQL.ts CHANGED
@@ -191,7 +191,7 @@ export class SQL {
191
191
  *
192
192
  */
193
193
  public static prepare<PrepareQueryArguments extends { [key: string]: any }, Result>(func: (args: PrepareQueryArguments) => ExecMethods<Result>): (args: PrepareQueryArguments) => SqlQuery<Result> {
194
- let sqlQuery: string = undefined;
194
+ let sqlQuery: string | undefined = undefined;
195
195
  const seenArguments: Map<string, string> = new Map();
196
196
  const getSqlString = () => {
197
197
  if (sqlQuery === undefined) {
@@ -209,9 +209,9 @@ export class SQL {
209
209
  }
210
210
  return sqlQuery;
211
211
  }
212
- return (args: PrepareQueryArguments) => {
212
+ return (args: PrepareQueryArguments): SqlQuery<Result> => {
213
213
  return {
214
- [SQL_ENTITY]: undefined,
214
+ [SQL_ENTITY]: undefined as Result,
215
215
  toSqlString: (): string => {
216
216
  let usedQuery = getSqlString();
217
217
  seenArguments.forEach((variable, key) => {
@@ -17,9 +17,9 @@ export class SqlExpression<TableRef, Name extends string | unknown | undefined,
17
17
 
18
18
  public readonly expression: string;
19
19
  public readonly nameAs: Name | undefined;
20
- public readonly transformer: Transformer<unknown, Type>
20
+ public readonly transformer: Transformer<any, Type> | undefined
21
21
 
22
- constructor(expression: string, nameAs?: Name, parser?: Transformer<unknown, Type>) {
22
+ constructor(expression: string, nameAs?: Name, parser?: Transformer<any, Type>) {
23
23
  this.expression = expression;
24
24
  this.nameAs = nameAs;
25
25
  this.transformer = parser;
@@ -132,8 +132,8 @@ export class SqlExpressionWithOver<TableRef, Name, Type extends string | number
132
132
  //public over<WindowName extends string>(namedWindow: WindowName): Expr<TableRef | `${WINDOW} as ${WindowName}`, unknown, Type>
133
133
  public over<WindowName extends string, TableRef1>(namedWindow: WindowName, func: (builder: Over<TableRef1>) => void): Expr<TableRef | TableRef1 | `(window) as ${WindowName}`, unknown, Type>
134
134
  public over(a: any, b?: any): any {
135
- let namedWindow: string = undefined;
136
- let func: (builder: Over<never>) => void = undefined;
135
+ let namedWindow: string | undefined = undefined;
136
+ let func: ((builder: Over<never>) => void) | undefined = undefined;
137
137
  if (typeof a === "function") {
138
138
  func = a
139
139
  } else if (typeof a === "string") {
@@ -143,7 +143,7 @@ export class SqlExpressionWithOver<TableRef, Name, Type extends string | number
143
143
  }
144
144
  }
145
145
  const builder = new Over<{}>();
146
- func(builder);
146
+ func!(builder);
147
147
  return SqlExpression.create(this.expression + " OVER (" + (namedWindow ? escapeId(namedWindow) + " " : "") + builder.toString() + ")");
148
148
  }
149
149
 
@@ -151,8 +151,8 @@ export class SqlExpressionWithOver<TableRef, Name, Type extends string | number
151
151
 
152
152
  export class Over<TableRef> {
153
153
 
154
- private _partitionBy: string[];
155
- private _orderBy: string[];
154
+ private _partitionBy: string[] | undefined;
155
+ private _orderBy: string[] | undefined;
156
156
 
157
157
  public orderBy<TableRef2>(...cols: OrderByStructure<Expr<TableRef2, string, any>>): Over<TableRef | TableRef2> {
158
158
  this._orderBy = orderByStructureToSqlString(cols)
@@ -165,8 +165,8 @@ export class Over<TableRef> {
165
165
  }
166
166
 
167
167
  public toString() {
168
- return (this._partitionBy?.length > 0 ? " PARTITION BY " + this._partitionBy.join(", ") : "") +
169
- (this._orderBy?.length > 0 ? " ORDER BY " + this._orderBy.join(", ") : "")
168
+ return (this._partitionBy && this._partitionBy.length > 0 ? " PARTITION BY " + this._partitionBy.join(", ") : "") +
169
+ (this._orderBy && this._orderBy.length > 0 ? " ORDER BY " + this._orderBy.join(", ") : "")
170
170
  }
171
171
 
172
172
  }
@@ -230,7 +230,7 @@ export function GROUP_CONCAT<TableRef, TableRef2 = never>(
230
230
  call?: (arg: GroupConcat<TableRef2>) => GroupConcat<TableRef2>
231
231
  ): Expr<TableRef | TableRef2, unknown, string> {
232
232
  const builder = new GroupConcat();
233
- call(builder);
233
+ call!(builder);
234
234
  return SqlExpression.create("GROUP_CONCAT(" + builder.toString() + ")");
235
235
  }
236
236
 
@@ -240,7 +240,7 @@ export class GroupConcat<TableRef = never> {
240
240
  private _columns: any[] = [];
241
241
  private _distinct: boolean = false;
242
242
  private _orderBy: string[] = [];
243
- private _separator: string = undefined;
243
+ private _separator: string | undefined = undefined;
244
244
 
245
245
  public all<TableRef2>(...args: (string | PrepareQueryArgument | Expr<TableRef2, any, any>)[]): GroupConcat<TableRef | TableRef2> {
246
246
  this._columns = args.map(toSql);
@@ -292,7 +292,7 @@ export function UNIX_TIMESTAMP<Name, TableRef>(field: Expr<TableRef, Name, tDate
292
292
  return SqlExpression.create("UNIX_TIMESTAMP(" + field.expression + ")")
293
293
  }
294
294
 
295
- export function DATE<Name, TableRef>(field: Expr<TableRef, Name, tDate | tDateTime>): Expr<TableRef, Name, tDate> {
295
+ export function DATE<Name, TableRef>(field: Expr<TableRef, Name, tDate | tDateTime | null>): Expr<TableRef, Name, tDate> {
296
296
  return SqlExpression.create("DATE(" + field.expression + ")")
297
297
  }
298
298
 
@@ -12,7 +12,7 @@
12
12
  const __KEY = Symbol.for("grest-ts:dedup:@grest-ts/sql");
13
13
  const __slot = globalThis as Record<symbol, unknown>;
14
14
  const __existing = __slot[__KEY] as { path: string; version: string } | undefined;
15
- const __here = { path: import.meta.url, version: "0.0.51" };
15
+ const __here = { path: import.meta.url, version: "0.0.55" };
16
16
  // Same path + same version means the same physical package is being
17
17
  // re-evaluated by a different ESM runner (e.g. vitest projects share one
18
18
  // process but each project has its own module cache). That isn't a true
@@ -12,14 +12,14 @@ export class DeleteBuilder<Tables, Entity> implements DeleteWhereMethods<Tables>
12
12
  GatewayDeleteWhereMethods<Entity>,
13
13
  GatewayDeleteOrderByMethods<Entity> {
14
14
 
15
- public readonly [SQL_ENTITY]: undefined; // never used
15
+ public readonly [SQL_ENTITY]!: { affectedRows: number }; // phantom, never populated
16
16
 
17
- private _from: string;
17
+ private _from: string = "";
18
18
  private readonly _where: string[] = [];
19
19
  private readonly _orderBy: string[] = [];
20
- private _limit: string;
20
+ private _limit: string = "";
21
21
 
22
- public from(table: string, alias: string): this {
22
+ public from(table: string, alias?: string): this {
23
23
  this._from = table + (alias ? " as " + escapeId(alias) : "");
24
24
  return this;
25
25
  }
@@ -4,17 +4,17 @@ import {SQL_ENTITY, SQL_EXPRESSION} from "../Symbols";
4
4
 
5
5
  export class InsertBuilder<Entity> implements ExecInsertMethods, InsertSetMethods<Entity> {
6
6
 
7
- public readonly [SQL_ENTITY]: undefined; // never used
7
+ public readonly [SQL_ENTITY]!: { insertId: number }; // phantom, never populated
8
8
 
9
9
  private _ignore: boolean = false;
10
- private _tableName: string;
10
+ private _tableName: string = "";
11
11
 
12
12
  private _set: string[] = []
13
13
 
14
- private _valueNames: Set<string>;
15
- private _valuesObjects: any[];
14
+ private _valueNames: Set<string> = new Set();
15
+ private _valuesObjects: any[] = [];
16
16
 
17
- private _select: string;
17
+ private _select: string = "";
18
18
 
19
19
 
20
20
  public to(tableName: string) {
@@ -36,10 +36,10 @@ export class SelectBuilder<Result, Aliases, AliasesFromWith, Tables> implements
36
36
  GatewaySelectOrderByMethods<Result, Tables>,
37
37
  GatewaySelectLimitMethods<Result> {
38
38
 
39
- public readonly [SQL_ENTITY]: undefined; // never used
39
+ public readonly [SQL_ENTITY]!: Result; // phantom, never populated
40
40
 
41
41
  private _withQueries: Map<string, string> = new Map()
42
- private _from: string;
42
+ private _from: string = "";
43
43
  private readonly _columns: string[] = [];
44
44
  private _columnStruct: DbTableDefinition<any> = {} as any;
45
45
 
@@ -49,7 +49,7 @@ export class SelectBuilder<Result, Aliases, AliasesFromWith, Tables> implements
49
49
  private readonly _having: string[] = []
50
50
  private readonly _groupBy: string[] = []
51
51
  private _orderBy: string[] = []
52
- private _limit: string = undefined
52
+ private _limit: string = ""
53
53
 
54
54
  private readonly unions: string[] = []
55
55
  private _unionColumnOrder: string[] = []
@@ -26,27 +26,33 @@ export interface ColumnsMethods<Result, Tables> {
26
26
  * Take array of Col-s and convert to Record<key, value> & ... object.
27
27
  */
28
28
  export type ExtractObj<Columns extends Expr<any, string, any>[]> = {
29
- [K in Columns[number]['nameAs']]: Extract<Columns[number], { nameAs: K }>['type']
29
+ [K in NonNullable<Columns[number]['nameAs']> & (string | number | symbol)]: _ColumnTypeByName<Columns[number], K>
30
30
  }
31
31
 
32
+ type _ColumnTypeByName<Column, K> = Column extends { nameAs: infer N, type: infer T }
33
+ ? K extends NonNullable<N> ? T : never
34
+ : never
35
+
32
36
 
33
37
  // --------------------------------------------------------------------
34
38
 
35
39
  type _checkIfExistsInOtherFields<Rest extends any[], Expr> =
36
40
  Rest extends []
37
41
  ? Expr
38
- : Expr extends { nameAs: string } ?
39
- Expr["nameAs"] extends (Rest extends { nameAs: infer A }[] ? A : never)
40
- ? `'${Expr["nameAs"]}' already exists in columns!`
41
- : Expr
42
- : Expr extends { nameAs: unknown } ?
43
- `Is missing column name. Add .as('name')`
44
- : Expr
42
+ : Expr extends { nameAs: infer N } ?
43
+ [NonNullable<N>] extends [string] ?
44
+ NonNullable<N> extends (Rest extends { nameAs: infer A }[] ? NonNullable<A> : never)
45
+ ? `'${NonNullable<N>}' already exists in columns!`
46
+ : Expr
47
+ : `Is missing column name. Add .as('name')`
48
+ : Expr
45
49
 
46
50
  type _checkIfExistsInResult<Result, Expr> =
47
- Expr extends { nameAs: string } ?
48
- Expr["nameAs"] extends keyof Result
49
- ? `'${Expr["nameAs"]}' already exists in result columns!`
51
+ Expr extends { nameAs: infer N } ?
52
+ [NonNullable<N>] extends [string] ?
53
+ NonNullable<N> extends keyof Result
54
+ ? `'${NonNullable<N> & string}' already exists in result columns!`
55
+ : Expr
50
56
  : Expr
51
57
  : Expr
52
58
 
@@ -13,14 +13,14 @@ export class UpdateBuilder<Entity, Tables> implements ExecUpdateMethods,
13
13
  UpdateOrderByMethods<Tables>,
14
14
  UpdateLimitMethods {
15
15
 
16
- public readonly [SQL_ENTITY]: { affectedRows: number; }; // never used
16
+ public readonly [SQL_ENTITY]!: { affectedRows: number; }; // phantom, never populated
17
17
 
18
- private _tableName: string;
18
+ private _tableName: string = "";
19
19
  private _joins: string[] = [];
20
20
  private _set: string[] = []
21
21
  private readonly _where: string[] = [];
22
22
  private readonly _orderBy: string[] = [];
23
- private _limit: string;
23
+ private _limit: string = "";
24
24
  private _ignore: boolean = false;
25
25
 
26
26
  public in(tableName: string, alias?: string) {