@stamhoofd/sql 2.64.0 → 2.65.1

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 (54) hide show
  1. package/dist/index.d.ts +11 -10
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +1 -0
  4. package/dist/index.js.map +1 -1
  5. package/dist/src/QueryableModel.d.ts +12 -0
  6. package/dist/src/QueryableModel.d.ts.map +1 -0
  7. package/dist/src/QueryableModel.js +30 -0
  8. package/dist/src/QueryableModel.js.map +1 -0
  9. package/dist/src/SQL.d.ts +3 -3
  10. package/dist/src/SQL.d.ts.map +1 -1
  11. package/dist/src/SQL.js +5 -5
  12. package/dist/src/SQL.js.map +1 -1
  13. package/dist/src/SQLExpression.d.ts +1 -0
  14. package/dist/src/SQLExpression.d.ts.map +1 -1
  15. package/dist/src/SQLExpression.js +2 -2
  16. package/dist/src/SQLExpression.js.map +1 -1
  17. package/dist/src/SQLExpressions.d.ts +28 -3
  18. package/dist/src/SQLExpressions.d.ts.map +1 -1
  19. package/dist/src/SQLExpressions.js +75 -6
  20. package/dist/src/SQLExpressions.js.map +1 -1
  21. package/dist/src/SQLInsert.d.ts.map +1 -1
  22. package/dist/src/SQLInsert.js +6 -1
  23. package/dist/src/SQLInsert.js.map +1 -1
  24. package/dist/src/SQLJoin.d.ts +3 -3
  25. package/dist/src/SQLJoin.d.ts.map +1 -1
  26. package/dist/src/SQLJoin.js +2 -2
  27. package/dist/src/SQLJoin.js.map +1 -1
  28. package/dist/src/SQLOrderBy.d.ts +3 -3
  29. package/dist/src/SQLOrderBy.d.ts.map +1 -1
  30. package/dist/src/SQLOrderBy.js +8 -4
  31. package/dist/src/SQLOrderBy.js.map +1 -1
  32. package/dist/src/SQLSelect.d.ts +19 -2
  33. package/dist/src/SQLSelect.d.ts.map +1 -1
  34. package/dist/src/SQLSelect.js +164 -2
  35. package/dist/src/SQLSelect.js.map +1 -1
  36. package/dist/src/SQLUpdate.d.ts.map +1 -1
  37. package/dist/src/SQLUpdate.js +6 -1
  38. package/dist/src/SQLUpdate.js.map +1 -1
  39. package/dist/src/SQLWhere.d.ts +18 -10
  40. package/dist/src/SQLWhere.d.ts.map +1 -1
  41. package/dist/src/SQLWhere.js +32 -17
  42. package/dist/src/SQLWhere.js.map +1 -1
  43. package/dist/tsconfig.tsbuildinfo +1 -1
  44. package/package.json +2 -2
  45. package/src/QueryableModel.ts +35 -0
  46. package/src/SQL.ts +7 -8
  47. package/src/SQLExpression.ts +12 -12
  48. package/src/SQLExpressions.ts +92 -8
  49. package/src/SQLInsert.ts +7 -1
  50. package/src/SQLJoin.ts +11 -11
  51. package/src/SQLOrderBy.ts +28 -24
  52. package/src/SQLSelect.ts +210 -4
  53. package/src/SQLUpdate.ts +7 -1
  54. package/src/SQLWhere.ts +54 -26
package/src/SQLWhere.ts CHANGED
@@ -3,14 +3,39 @@ import { SQLArray, SQLColumnExpression, SQLDynamicExpression, SQLNull, readDynam
3
3
 
4
4
  type Constructor<T = {}> = new (...args: any[]) => T;
5
5
 
6
+ export enum SQLWhereSign {
7
+ Equal = '=',
8
+ Greater = '>',
9
+ GreaterEqual = '>=',
10
+ Less = '<',
11
+ LessEqual = '<=',
12
+ NotEqual = '!=',
13
+ };
14
+
15
+ export const SQLWhereSignMap = {
16
+ '=': SQLWhereSign.Equal,
17
+ '>': SQLWhereSign.Greater,
18
+ '>=': SQLWhereSign.GreaterEqual,
19
+ '<': SQLWhereSign.Less,
20
+ '<=': SQLWhereSign.LessEqual,
21
+ '!=': SQLWhereSign.NotEqual,
22
+ } as const;
23
+
24
+ function parseWhereSign(sign: SQLWhereSign | keyof typeof SQLWhereSignMap): SQLWhereSign {
25
+ if (typeof sign === 'string' && Object.hasOwnProperty.call(SQLWhereSignMap, sign)) {
26
+ return SQLWhereSignMap[sign];
27
+ }
28
+ return sign as SQLWhereSign;
29
+ }
30
+
6
31
  export type ParseWhereArguments = [
7
32
  where: SQLWhere,
8
33
  ] | [
9
34
  whereOrColumn: SQLExpression | string,
10
- sign: SQLWhereSign,
11
35
  value: SQLDynamicExpression,
12
36
  ] | [
13
37
  whereOrColumn: SQLExpression | string,
38
+ sign: SQLWhereSign | keyof typeof SQLWhereSignMap,
14
39
  value: SQLDynamicExpression,
15
40
  ];
16
41
 
@@ -124,36 +149,27 @@ export class SQLEmptyWhere extends SQLWhere {
124
149
  }
125
150
  }
126
151
 
127
- export enum SQLWhereSign {
128
- Equal = '=',
129
- Greater = '>',
130
- GreaterEqual = '>=',
131
- Less = '<',
132
- LessEqual = '<=',
133
- NotEqual = '!=',
134
- }
135
-
136
152
  export class SQLWhereEqual extends SQLWhere {
137
153
  column: SQLExpression;
138
154
  sign = SQLWhereSign.Equal;
139
155
  value: SQLExpression;
140
156
 
141
- static parseWhere(...[whereOrColumn, signOrValue, value]: ParseWhereArguments): SQLWhere {
142
- if (signOrValue === undefined) {
143
- return whereOrColumn as SQLWhere;
157
+ static parseWhere(...parsed: ParseWhereArguments): SQLWhere {
158
+ if (parsed[1] === undefined) {
159
+ return parsed[0];
144
160
  }
145
161
 
146
- if (value !== undefined) {
162
+ if (parsed.length === 3) {
147
163
  return new SQLWhereEqual(
148
- typeof whereOrColumn === 'string' ? new SQLColumnExpression(whereOrColumn) : whereOrColumn,
149
- signOrValue as SQLWhereSign,
150
- readDynamicSQLExpression(value),
164
+ typeof parsed[0] === 'string' ? new SQLColumnExpression(parsed[0]) : parsed[0],
165
+ parseWhereSign(parsed[1]),
166
+ readDynamicSQLExpression(parsed[2]),
151
167
  );
152
168
  }
153
169
  return new SQLWhereEqual(
154
- typeof whereOrColumn === 'string' ? new SQLColumnExpression(whereOrColumn) : whereOrColumn,
170
+ typeof parsed[0] === 'string' ? new SQLColumnExpression(parsed[0]) : parsed[0],
155
171
  SQLWhereSign.Equal,
156
- readDynamicSQLExpression(signOrValue),
172
+ readDynamicSQLExpression(parsed[1]),
157
173
  );
158
174
  }
159
175
 
@@ -188,12 +204,24 @@ export class SQLWhereEqual extends SQLWhere {
188
204
 
189
205
  invert(): this {
190
206
  switch (this.sign) {
191
- case SQLWhereSign.Equal: this.sign = SQLWhereSign.NotEqual; break;
192
- case SQLWhereSign.NotEqual: this.sign = SQLWhereSign.Equal; break;
193
- case SQLWhereSign.Greater: this.sign = SQLWhereSign.LessEqual; break;
194
- case SQLWhereSign.Less: this.sign = SQLWhereSign.GreaterEqual; break;
195
- case SQLWhereSign.GreaterEqual: this.sign = SQLWhereSign.Less; break;
196
- case SQLWhereSign.LessEqual: this.sign = SQLWhereSign.Greater; break;
207
+ case SQLWhereSign.Equal:
208
+ this.sign = SQLWhereSign.NotEqual;
209
+ break;
210
+ case SQLWhereSign.NotEqual:
211
+ this.sign = SQLWhereSign.Equal;
212
+ break;
213
+ case SQLWhereSign.Greater:
214
+ this.sign = SQLWhereSign.LessEqual;
215
+ break;
216
+ case SQLWhereSign.Less:
217
+ this.sign = SQLWhereSign.GreaterEqual;
218
+ break;
219
+ case SQLWhereSign.GreaterEqual:
220
+ this.sign = SQLWhereSign.Less;
221
+ break;
222
+ case SQLWhereSign.LessEqual:
223
+ this.sign = SQLWhereSign.Greater;
224
+ break;
197
225
  }
198
226
  return this;
199
227
  }
@@ -305,7 +333,7 @@ export class SQLWhereExists extends SQLWhere {
305
333
  getSQL(options?: SQLExpressionOptions): SQLQuery {
306
334
  return joinSQLQuery([
307
335
  `${this.notExists ? 'NOT EXISTS' : 'EXISTS'} (`,
308
- this.subquery.getSQL({ ...options }),
336
+ this.subquery.getSQL(options),
309
337
  `)`,
310
338
  ]);
311
339
  }