@stamhoofd/sql 2.89.2 → 2.90.0

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 (125) hide show
  1. package/dist/index.d.ts +1 -2
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +9 -10
  4. package/dist/index.js.map +1 -1
  5. package/dist/src/SQLJsonExpressions.d.ts +2 -0
  6. package/dist/src/SQLJsonExpressions.d.ts.map +1 -1
  7. package/dist/src/SQLJsonExpressions.js +8 -0
  8. package/dist/src/SQLJsonExpressions.js.map +1 -1
  9. package/dist/src/SQLSelect.d.ts.map +1 -1
  10. package/dist/src/SQLSelect.js +7 -0
  11. package/dist/src/SQLSelect.js.map +1 -1
  12. package/dist/src/SQLWhere.d.ts +6 -0
  13. package/dist/src/SQLWhere.d.ts.map +1 -1
  14. package/dist/src/SQLWhere.js +39 -1
  15. package/dist/src/SQLWhere.js.map +1 -1
  16. package/dist/src/filters/SQLFilter.d.ts +61 -25
  17. package/dist/src/filters/SQLFilter.d.ts.map +1 -1
  18. package/dist/src/filters/SQLFilter.js +183 -380
  19. package/dist/src/filters/SQLFilter.js.map +1 -1
  20. package/dist/src/filters/{modern/compilers → compilers}/contains.d.ts +1 -1
  21. package/dist/src/filters/compilers/contains.d.ts.map +1 -0
  22. package/dist/src/filters/{modern/compilers → compilers}/contains.js +6 -6
  23. package/dist/src/filters/compilers/contains.js.map +1 -0
  24. package/dist/src/filters/{modern/compilers → compilers}/equals.d.ts +1 -1
  25. package/dist/src/filters/compilers/equals.d.ts.map +1 -0
  26. package/dist/src/filters/{modern/compilers → compilers}/equals.js +7 -7
  27. package/dist/src/filters/compilers/equals.js.map +1 -0
  28. package/dist/src/filters/{modern/compilers → compilers}/greater.d.ts +1 -1
  29. package/dist/src/filters/compilers/greater.d.ts.map +1 -0
  30. package/dist/src/filters/{modern/compilers → compilers}/greater.js +4 -4
  31. package/dist/src/filters/compilers/greater.js.map +1 -0
  32. package/dist/src/filters/{modern/compilers → compilers}/in.d.ts +1 -1
  33. package/dist/src/filters/compilers/in.d.ts.map +1 -0
  34. package/dist/src/filters/{modern/compilers → compilers}/in.js +7 -7
  35. package/dist/src/filters/compilers/in.js.map +1 -0
  36. package/dist/src/filters/compilers/index.d.ts.map +1 -0
  37. package/dist/src/filters/compilers/index.js.map +1 -0
  38. package/dist/src/filters/{modern/compilers → compilers}/less.d.ts +1 -1
  39. package/dist/src/filters/compilers/less.d.ts.map +1 -0
  40. package/dist/src/filters/{modern/compilers → compilers}/less.js +4 -4
  41. package/dist/src/filters/compilers/less.js.map +1 -0
  42. package/dist/src/filters/helpers/isJSONColumn.d.ts +4 -0
  43. package/dist/src/filters/helpers/isJSONColumn.d.ts.map +1 -0
  44. package/dist/src/filters/helpers/isJSONColumn.js +16 -0
  45. package/dist/src/filters/helpers/isJSONColumn.js.map +1 -0
  46. package/dist/src/filters/{modern/helpers → helpers}/normalizeCompareValue.d.ts +2 -2
  47. package/dist/src/filters/helpers/normalizeCompareValue.d.ts.map +1 -0
  48. package/dist/src/filters/{modern/helpers → helpers}/normalizeCompareValue.js +13 -13
  49. package/dist/src/filters/helpers/normalizeCompareValue.js.map +1 -0
  50. package/dist/tests/filters/$and.test.js +49 -18
  51. package/dist/tests/filters/$and.test.js.map +1 -1
  52. package/dist/tests/filters/$contains.test.js +20 -20
  53. package/dist/tests/filters/$contains.test.js.map +1 -1
  54. package/dist/tests/filters/$eq.test.js +59 -53
  55. package/dist/tests/filters/$eq.test.js.map +1 -1
  56. package/dist/tests/filters/$gt.test.js +18 -18
  57. package/dist/tests/filters/$gt.test.js.map +1 -1
  58. package/dist/tests/filters/$gte.test.js +14 -14
  59. package/dist/tests/filters/$gte.test.js.map +1 -1
  60. package/dist/tests/filters/$in.test.js +24 -24
  61. package/dist/tests/filters/$in.test.js.map +1 -1
  62. package/dist/tests/filters/$lt.test.js +14 -14
  63. package/dist/tests/filters/$lt.test.js.map +1 -1
  64. package/dist/tests/filters/$lte.test.js +14 -14
  65. package/dist/tests/filters/$lte.test.js.map +1 -1
  66. package/dist/tests/filters/$neq.test.js +3 -3
  67. package/dist/tests/filters/$neq.test.js.map +1 -1
  68. package/dist/tests/filters/$not.test.js +5 -5
  69. package/dist/tests/filters/$not.test.js.map +1 -1
  70. package/dist/tests/filters/$or.test.js +16 -16
  71. package/dist/tests/filters/$or.test.js.map +1 -1
  72. package/dist/tests/filters/dot-syntax.test.js +10 -10
  73. package/dist/tests/filters/dot-syntax.test.js.map +1 -1
  74. package/dist/tests/filters/exists.test.js +16 -16
  75. package/dist/tests/filters/exists.test.js.map +1 -1
  76. package/dist/tests/filters/joined-relations.test.js +31 -31
  77. package/dist/tests/filters/joined-relations.test.js.map +1 -1
  78. package/dist/tests/filters/special-cases.test.js +11 -11
  79. package/dist/tests/filters/special-cases.test.js.map +1 -1
  80. package/dist/tests/filters/wildcard.test.js +8 -8
  81. package/dist/tests/filters/wildcard.test.js.map +1 -1
  82. package/dist/tests/utils/index.d.ts +7 -7
  83. package/dist/tests/utils/index.d.ts.map +1 -1
  84. package/dist/tests/utils/index.js +6 -6
  85. package/dist/tests/utils/index.js.map +1 -1
  86. package/dist/tsconfig.tsbuildinfo +1 -1
  87. package/package.json +2 -2
  88. package/src/SQLJsonExpressions.ts +10 -0
  89. package/src/SQLSelect.ts +9 -0
  90. package/src/SQLWhere.ts +48 -1
  91. package/src/filters/SQLFilter.ts +203 -485
  92. package/src/filters/{modern/compilers → compilers}/contains.ts +5 -5
  93. package/src/filters/{modern/compilers → compilers}/equals.ts +6 -6
  94. package/src/filters/{modern/compilers → compilers}/greater.ts +3 -3
  95. package/src/filters/{modern/compilers → compilers}/in.ts +6 -6
  96. package/src/filters/{modern/compilers → compilers}/less.ts +3 -3
  97. package/src/filters/helpers/isJSONColumn.ts +13 -0
  98. package/src/filters/{modern/helpers → helpers}/normalizeCompareValue.ts +14 -14
  99. package/dist/src/filters/modern/SQLModernFilter.d.ts +0 -73
  100. package/dist/src/filters/modern/SQLModernFilter.d.ts.map +0 -1
  101. package/dist/src/filters/modern/SQLModernFilter.js +0 -200
  102. package/dist/src/filters/modern/SQLModernFilter.js.map +0 -1
  103. package/dist/src/filters/modern/compilers/contains.d.ts.map +0 -1
  104. package/dist/src/filters/modern/compilers/contains.js.map +0 -1
  105. package/dist/src/filters/modern/compilers/equals.d.ts.map +0 -1
  106. package/dist/src/filters/modern/compilers/equals.js.map +0 -1
  107. package/dist/src/filters/modern/compilers/greater.d.ts.map +0 -1
  108. package/dist/src/filters/modern/compilers/greater.js.map +0 -1
  109. package/dist/src/filters/modern/compilers/in.d.ts.map +0 -1
  110. package/dist/src/filters/modern/compilers/in.js.map +0 -1
  111. package/dist/src/filters/modern/compilers/index.d.ts.map +0 -1
  112. package/dist/src/filters/modern/compilers/index.js.map +0 -1
  113. package/dist/src/filters/modern/compilers/less.d.ts.map +0 -1
  114. package/dist/src/filters/modern/compilers/less.js.map +0 -1
  115. package/dist/src/filters/modern/helpers/isJSONColumn.d.ts +0 -4
  116. package/dist/src/filters/modern/helpers/isJSONColumn.d.ts.map +0 -1
  117. package/dist/src/filters/modern/helpers/isJSONColumn.js +0 -16
  118. package/dist/src/filters/modern/helpers/isJSONColumn.js.map +0 -1
  119. package/dist/src/filters/modern/helpers/normalizeCompareValue.d.ts.map +0 -1
  120. package/dist/src/filters/modern/helpers/normalizeCompareValue.js.map +0 -1
  121. package/src/filters/modern/SQLModernFilter.ts +0 -256
  122. package/src/filters/modern/helpers/isJSONColumn.ts +0 -13
  123. /package/dist/src/filters/{modern/compilers → compilers}/index.d.ts +0 -0
  124. /package/dist/src/filters/{modern/compilers → compilers}/index.js +0 -0
  125. /package/src/filters/{modern/compilers → compilers}/index.ts +0 -0
@@ -1,256 +0,0 @@
1
- import { SimpleError } from '@simonbackx/simple-errors';
2
- import { compileFilter, FilterCompiler, FilterDefinitions, filterDefinitionsToCompiler, RequiredFilterCompiler, StamhoofdFilter } from '@stamhoofd/structures';
3
- import { SQLExpression, SQLExpressionOptions, SQLQuery } from '../../SQLExpression';
4
- import { SQLJoin } from '../../SQLJoin';
5
- import { SQLJsonValue } from '../../SQLJsonExpressions';
6
- import { SQLSelect } from '../../SQLSelect';
7
- import { SQLWhere, SQLWhereAnd, SQLWhereExists, SQLWhereJoin, SQLWhereNot, SQLWhereOr } from '../../SQLWhere';
8
- import { $equalsSQLFilterCompiler, $greaterThanSQLFilterCompiler, $inSQLFilterCompiler, $lessThanSQLFilterCompiler } from './compilers';
9
- import { $containsSQLFilterCompiler } from './compilers/contains';
10
-
11
- export type SQLSyncFilterRunner = (column: SQLCurrentColumn) => SQLWhere;
12
- export type SQLFilterRunner = (column: SQLCurrentColumn) => Promise<SQLWhere> | SQLWhere;
13
- export type SQLFilterCompiler = FilterCompiler<SQLFilterRunner>;
14
- export type SQLRequiredFilterCompiler = RequiredFilterCompiler<SQLFilterRunner>;
15
- export type SQLModernFilterDefinitions = FilterDefinitions<SQLFilterRunner>;
16
-
17
- export enum SQLModernValueType {
18
- /** At the root of a select */
19
- Table = 'Table',
20
-
21
- /** Column with type string */
22
- String = 'String',
23
-
24
- /** MySQL Datetime */
25
- Datetime = 'Datetime',
26
-
27
- /** Column with type number */
28
- Number = 'Number',
29
-
30
- /** Column with type boolean, meaning 1 or 0 */
31
- Boolean = 'Boolean',
32
-
33
- /** True or false in JSON */
34
- JSONBoolean = 'JSONBoolean',
35
- JSONString = 'JSONString',
36
-
37
- JSONNumber = 'JSONNumber',
38
-
39
- /** [...] */
40
- JSONArray = 'JSONArray',
41
-
42
- /** {...} */
43
- JSONObject = 'JSONObject',
44
- }
45
-
46
- export type SQLCurrentColumn = {
47
- expression: SQLExpression;
48
-
49
- /**
50
- * MySQL nullable. Please fill this in correctly! If a value can be null, can not exist (=mysql null), or can be JSONNull, set this to true.
51
- *
52
- * Mainly > and < operators will make sure the behaviour is consistent with MySQL sorting (normally comparing with null will always return false in MySQL)
53
- */
54
- nullable?: boolean;
55
-
56
- /**
57
- * JSON nullable
58
- */
59
-
60
- /**
61
- * Type of this column, use to normalize values received from filters
62
- */
63
- type: SQLModernValueType;
64
- checkPermission?: () => Promise<void>;
65
- };
66
-
67
- export function createColumnFilter(column: SQLCurrentColumn, childDefinitions?: SQLModernFilterDefinitions): SQLFilterCompiler {
68
- return (filter: StamhoofdFilter) => {
69
- const compiler = childDefinitions ? filterDefinitionsToCompiler(childDefinitions) : filterDefinitionsToCompiler(baseModernSQLFilterCompilers);
70
- const runner = $andSQLFilterCompiler(filter, compiler);
71
-
72
- return (_: SQLCurrentColumn) => {
73
- return runner({
74
- nullable: false,
75
- ...column,
76
- });
77
- };
78
- };
79
- }
80
-
81
- export function createWildcardColumnFilter(getColumn: (key: string) => SQLCurrentColumn, childDefinitions?: (key: string) => SQLModernFilterDefinitions): SQLFilterCompiler {
82
- const wildcardCompiler = (filter: StamhoofdFilter, _, key: string) => {
83
- const compiler = childDefinitions ? filterDefinitionsToCompiler(childDefinitions(key)) : filterDefinitionsToCompiler(baseModernSQLFilterCompilers);
84
- const runner = $andSQLFilterCompiler(filter, compiler);
85
-
86
- return (_: SQLCurrentColumn) => {
87
- return runner({
88
- nullable: false,
89
- ...getColumn(key),
90
- });
91
- };
92
- };
93
-
94
- return (filter: StamhoofdFilter) => {
95
- return $andSQLFilterCompiler(filter, wildcardCompiler);
96
- };
97
- }
98
-
99
- /**
100
- * Filter with a subquery that should return at least one result.
101
- */
102
- export function createExistsFilter(baseSelect: InstanceType<typeof SQLSelect> & SQLExpression, definitions: SQLModernFilterDefinitions): SQLFilterCompiler {
103
- return (filter: StamhoofdFilter, _: SQLFilterCompiler) => {
104
- if (filter !== null && typeof filter === 'object' && '$elemMatch' in filter) {
105
- filter = filter['$elemMatch'] as StamhoofdFilter;
106
- }
107
-
108
- const runner = compileToSQLRunner(filter, definitions);
109
-
110
- return async (_: SQLCurrentColumn) => {
111
- const w = await runner({
112
- expression: SQLRootExpression,
113
- type: SQLModernValueType.Table,
114
- nullable: false,
115
- });
116
- const q = baseSelect.clone().andWhere(w);
117
- return new SQLWhereExists(q);
118
- };
119
- };
120
- }
121
-
122
- /**
123
- * WARNING: only use this on one-to-one relations. Using it on one-to-many relations will result in duplicate results.
124
- *
125
- * By default doesRelationAlwaysExist is set to true, this means we expect the relation to always exist. This helps optimize the query (dropping the join if the where clause in the join is always true)
126
- */
127
- export function createJoinedRelationFilter(join: SQLJoin, definitions: SQLModernFilterDefinitions, options: { doesRelationAlwaysExist: boolean } = { doesRelationAlwaysExist: true }): SQLFilterCompiler {
128
- return (filter: StamhoofdFilter, _: SQLFilterCompiler) => {
129
- if (filter !== null && typeof filter === 'object' && '$elemMatch' in filter) {
130
- filter = filter['$elemMatch'] as StamhoofdFilter;
131
- }
132
-
133
- return async (_: SQLCurrentColumn) => {
134
- const w = await compileToModernSQLFilter(filter, definitions);
135
- return new SQLWhereJoin(join, w, {
136
- doesRelationAlwaysExist: options.doesRelationAlwaysExist,
137
- });
138
- };
139
- };
140
- }
141
-
142
- export function $andSQLFilterCompiler(filter: StamhoofdFilter, filters: SQLFilterCompiler): SQLFilterRunner {
143
- const runners = compileSQLFilter(filter, filters);
144
-
145
- return async (column: SQLCurrentColumn) => {
146
- const wheres = (await Promise.all(
147
- runners.map(runner => (runner(column))),
148
- ));
149
-
150
- return new SQLWhereAnd(wheres);
151
- };
152
- }
153
-
154
- export function $orSQLFilterCompiler(filter: StamhoofdFilter, filters: SQLFilterCompiler): SQLFilterRunner {
155
- const runners = compileSQLFilter(filter, filters);
156
-
157
- return async (column: SQLCurrentColumn) => {
158
- const wheres = (await Promise.all(
159
- runners.map(runner => (runner(column))),
160
- ));
161
-
162
- return new SQLWhereOr(wheres);
163
- };
164
- }
165
-
166
- export function $notSQLFilterCompiler(filter: StamhoofdFilter, filters: SQLFilterCompiler): SQLFilterRunner {
167
- const andRunner = $andSQLFilterCompiler(filter, filters);
168
-
169
- return async (column: SQLCurrentColumn) => {
170
- return new SQLWhereNot(await andRunner(column));
171
- };
172
- }
173
-
174
- function invertFilterCompiler(compiler: SQLRequiredFilterCompiler): SQLRequiredFilterCompiler {
175
- return (filter: StamhoofdFilter, parentCompiler: SQLFilterCompiler) => {
176
- const runner = compiler(filter, parentCompiler);
177
- return async (column) => {
178
- return new SQLWhereNot(await runner(column));
179
- };
180
- };
181
- }
182
-
183
- export const baseModernSQLFilterCompilers: SQLModernFilterDefinitions = {
184
- $and: $andSQLFilterCompiler,
185
- $or: $orSQLFilterCompiler,
186
- $not: $notSQLFilterCompiler,
187
- $eq: $equalsSQLFilterCompiler,
188
- $neq: invertFilterCompiler($equalsSQLFilterCompiler),
189
-
190
- $lt: $lessThanSQLFilterCompiler,
191
- $gt: $greaterThanSQLFilterCompiler,
192
- $lte: invertFilterCompiler($greaterThanSQLFilterCompiler),
193
- $gte: invertFilterCompiler($lessThanSQLFilterCompiler),
194
-
195
- $in: $inSQLFilterCompiler,
196
-
197
- $contains: $containsSQLFilterCompiler,
198
- };
199
-
200
- const compileSQLFilter = compileFilter<SQLFilterRunner>;
201
-
202
- export const SQLRootExpression: SQLExpression = {
203
- getSQL(options?: SQLExpressionOptions): SQLQuery {
204
- throw new SimpleError({
205
- code: 'invalid_filter',
206
- message: 'Root level filters are not allowed to use $eq or $neq',
207
- });
208
- },
209
- };
210
-
211
- export function compileToSQLRunner(filter: StamhoofdFilter, definitions: SQLModernFilterDefinitions): SQLFilterRunner {
212
- if (filter === null) {
213
- return () => {
214
- return new SQLWhereAnd([]); // No filter, return empty where
215
- };
216
- }
217
- const compiler = filterDefinitionsToCompiler(definitions); // this compiler searches in the definition for the right compiler for the given key
218
- const runner = $andSQLFilterCompiler(filter, compiler);
219
- return runner;
220
- };
221
-
222
- export async function compileToModernSQLFilter(filter: StamhoofdFilter, filters: SQLModernFilterDefinitions): Promise<SQLWhere> {
223
- const runner = compileToSQLRunner(filter, filters);
224
- return await runner({
225
- expression: SQLRootExpression,
226
- type: SQLModernValueType.Table,
227
- nullable: false,
228
- });
229
- };
230
-
231
- /**
232
- * Casts json strings, numbers and booleans to native MySQL types. This includes json null to mysql null.
233
- */
234
- export function normalizeColumn(column: SQLCurrentColumn): SQLCurrentColumn {
235
- if (column.type === SQLModernValueType.JSONString) {
236
- return {
237
- expression: new SQLJsonValue(column.expression, 'CHAR'),
238
- type: SQLModernValueType.String,
239
- };
240
- }
241
-
242
- if (column.type === SQLModernValueType.JSONBoolean) {
243
- return {
244
- expression: new SQLJsonValue(column.expression, 'UNSIGNED'),
245
- type: SQLModernValueType.Boolean,
246
- };
247
- }
248
-
249
- if (column.type === SQLModernValueType.JSONNumber) {
250
- return {
251
- expression: new SQLJsonValue(column.expression, 'UNSIGNED'),
252
- type: SQLModernValueType.Number,
253
- };
254
- }
255
- return column;
256
- }
@@ -1,13 +0,0 @@
1
- import { SQLCurrentColumn, SQLModernValueType } from '../SQLModernFilter';
2
-
3
- export function isJSONColumn({ type }: SQLCurrentColumn): boolean {
4
- return isJSONType(type);
5
- }
6
-
7
- export function isJSONType(type: SQLModernValueType): boolean {
8
- return type === SQLModernValueType.JSONString
9
- || type === SQLModernValueType.JSONBoolean
10
- || type === SQLModernValueType.JSONNumber
11
- || type === SQLModernValueType.JSONArray
12
- || type === SQLModernValueType.JSONObject;
13
- }