@stamhoofd/sql 2.119.0 → 2.120.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 (272) hide show
  1. package/dist/{src/ModelCache.d.ts → ModelCache.d.ts} +1 -1
  2. package/dist/ModelCache.d.ts.map +1 -0
  3. package/{src/ModelCache.ts → dist/ModelCache.js} +8 -16
  4. package/dist/ModelCache.js.map +1 -0
  5. package/dist/{src/QueryableModel.d.ts → QueryableModel.d.ts} +7 -6
  6. package/dist/QueryableModel.d.ts.map +1 -0
  7. package/dist/{src/QueryableModel.js → QueryableModel.js} +9 -13
  8. package/dist/QueryableModel.js.map +1 -0
  9. package/dist/{src/SQL.d.ts → SQL.d.ts} +7 -5
  10. package/dist/SQL.d.ts.map +1 -0
  11. package/dist/SQL.js +100 -0
  12. package/dist/SQL.js.map +1 -0
  13. package/dist/{src/SQLDelete.d.ts → SQLDelete.d.ts} +2 -2
  14. package/dist/SQLDelete.d.ts.map +1 -0
  15. package/dist/{src/SQLDelete.js → SQLDelete.js} +8 -12
  16. package/dist/SQLDelete.js.map +1 -0
  17. package/dist/SQLExpression.d.ts.map +1 -0
  18. package/dist/{src/SQLExpression.js → SQLExpression.js} +3 -8
  19. package/dist/SQLExpression.js.map +1 -0
  20. package/dist/{src/SQLExpressions.d.ts → SQLExpressions.d.ts} +2 -2
  21. package/dist/SQLExpressions.d.ts.map +1 -0
  22. package/dist/{src/SQLExpressions.js → SQLExpressions.js} +78 -115
  23. package/dist/SQLExpressions.js.map +1 -0
  24. package/dist/{src/SQLInsert.d.ts → SQLInsert.d.ts} +3 -2
  25. package/dist/SQLInsert.d.ts.map +1 -0
  26. package/dist/{src/SQLInsert.js → SQLInsert.js} +17 -21
  27. package/dist/SQLInsert.js.map +1 -0
  28. package/dist/{src/SQLJoin.d.ts → SQLJoin.d.ts} +1 -1
  29. package/dist/SQLJoin.d.ts.map +1 -0
  30. package/{src/SQLJoin.ts → dist/SQLJoin.js} +15 -18
  31. package/dist/SQLJoin.js.map +1 -0
  32. package/dist/{src/SQLJsonExpressions.d.ts → SQLJsonExpressions.d.ts} +2 -2
  33. package/dist/SQLJsonExpressions.d.ts.map +1 -0
  34. package/dist/{src/SQLJsonExpressions.js → SQLJsonExpressions.js} +31 -44
  35. package/dist/SQLJsonExpressions.js.map +1 -0
  36. package/dist/SQLLogger.d.ts.map +1 -0
  37. package/dist/{src/SQLLogger.js → SQLLogger.js} +5 -8
  38. package/dist/SQLLogger.js.map +1 -0
  39. package/dist/{src/SQLOrderBy.d.ts → SQLOrderBy.d.ts} +1 -1
  40. package/dist/SQLOrderBy.d.ts.map +1 -0
  41. package/dist/{src/SQLOrderBy.js → SQLOrderBy.js} +8 -13
  42. package/dist/SQLOrderBy.js.map +1 -0
  43. package/dist/{src/SQLSelect.d.ts → SQLSelect.d.ts} +3 -3
  44. package/dist/SQLSelect.d.ts.map +1 -0
  45. package/dist/{src/SQLSelect.js → SQLSelect.js} +24 -29
  46. package/dist/SQLSelect.js.map +1 -0
  47. package/dist/{src/SQLTranslatedStringHelper.d.ts → SQLTranslatedStringHelper.d.ts} +3 -2
  48. package/dist/SQLTranslatedStringHelper.d.ts.map +1 -0
  49. package/dist/SQLTranslatedStringHelper.js +33 -0
  50. package/dist/SQLTranslatedStringHelper.js.map +1 -0
  51. package/dist/{src/SQLUpdate.d.ts → SQLUpdate.d.ts} +3 -2
  52. package/dist/SQLUpdate.d.ts.map +1 -0
  53. package/{src/SQLUpdate.ts → dist/SQLUpdate.js} +18 -35
  54. package/dist/SQLUpdate.js.map +1 -0
  55. package/dist/{src/SQLWhere.d.ts → SQLWhere.d.ts} +3 -3
  56. package/dist/SQLWhere.d.ts.map +1 -0
  57. package/dist/{src/SQLWhere.js → SQLWhere.js} +42 -56
  58. package/dist/SQLWhere.js.map +1 -0
  59. package/dist/{src/filters → filters}/SQLFilter.d.ts +5 -5
  60. package/dist/filters/SQLFilter.d.ts.map +1 -0
  61. package/{src/filters/SQLFilter.ts → dist/filters/SQLFilter.js} +55 -121
  62. package/dist/filters/SQLFilter.js.map +1 -0
  63. package/dist/{src/filters → filters}/SQLSorter.d.ts +6 -6
  64. package/dist/filters/SQLSorter.d.ts.map +1 -0
  65. package/dist/{src/filters → filters}/SQLSorter.js +4 -7
  66. package/dist/filters/SQLSorter.js.map +1 -0
  67. package/dist/filters/compilers/contains.d.ts +4 -0
  68. package/dist/filters/compilers/contains.d.ts.map +1 -0
  69. package/{src/filters/compilers/contains.ts → dist/filters/compilers/contains.js} +7 -25
  70. package/dist/filters/compilers/contains.js.map +1 -0
  71. package/dist/filters/compilers/equals.d.ts +4 -0
  72. package/dist/filters/compilers/equals.d.ts.map +1 -0
  73. package/dist/filters/compilers/equals.js +44 -0
  74. package/dist/filters/compilers/equals.js.map +1 -0
  75. package/dist/filters/compilers/greater.d.ts +4 -0
  76. package/dist/filters/compilers/greater.d.ts.map +1 -0
  77. package/dist/filters/compilers/greater.js +15 -0
  78. package/dist/filters/compilers/greater.js.map +1 -0
  79. package/dist/filters/compilers/in.d.ts +4 -0
  80. package/dist/filters/compilers/in.d.ts.map +1 -0
  81. package/{src/filters/compilers/in.ts → dist/filters/compilers/in.js} +7 -21
  82. package/dist/filters/compilers/in.js.map +1 -0
  83. package/dist/filters/compilers/index.d.ts.map +1 -0
  84. package/{src/filters/compilers/index.ts → dist/filters/compilers/index.js} +1 -0
  85. package/dist/filters/compilers/index.js.map +1 -0
  86. package/dist/filters/compilers/less.d.ts +4 -0
  87. package/dist/filters/compilers/less.d.ts.map +1 -0
  88. package/dist/filters/compilers/less.js +15 -0
  89. package/dist/filters/compilers/less.js.map +1 -0
  90. package/dist/filters/helpers/isJSONColumn.d.ts +5 -0
  91. package/dist/filters/helpers/isJSONColumn.d.ts.map +1 -0
  92. package/{src/filters/helpers/isJSONColumn.ts → dist/filters/helpers/isJSONColumn.js} +4 -5
  93. package/dist/filters/helpers/isJSONColumn.js.map +1 -0
  94. package/dist/{src/filters → filters}/helpers/normalizeCompareValue.d.ts +1 -1
  95. package/dist/filters/helpers/normalizeCompareValue.d.ts.map +1 -0
  96. package/{src/filters/helpers/normalizeCompareValue.ts → dist/filters/helpers/normalizeCompareValue.js} +2 -20
  97. package/dist/filters/helpers/normalizeCompareValue.js.map +1 -0
  98. package/dist/index.d.ts +14 -14
  99. package/dist/index.d.ts.map +1 -1
  100. package/dist/index.js +14 -25
  101. package/dist/index.js.map +1 -1
  102. package/package.json +6 -6
  103. package/dist/src/ModelCache.d.ts.map +0 -1
  104. package/dist/src/ModelCache.js +0 -57
  105. package/dist/src/ModelCache.js.map +0 -1
  106. package/dist/src/QueryableModel.d.ts.map +0 -1
  107. package/dist/src/QueryableModel.js.map +0 -1
  108. package/dist/src/SQL.d.ts.map +0 -1
  109. package/dist/src/SQL.js +0 -103
  110. package/dist/src/SQL.js.map +0 -1
  111. package/dist/src/SQLDelete.d.ts.map +0 -1
  112. package/dist/src/SQLDelete.js.map +0 -1
  113. package/dist/src/SQLExpression.d.ts.map +0 -1
  114. package/dist/src/SQLExpression.js.map +0 -1
  115. package/dist/src/SQLExpressions.d.ts.map +0 -1
  116. package/dist/src/SQLExpressions.js.map +0 -1
  117. package/dist/src/SQLInsert.d.ts.map +0 -1
  118. package/dist/src/SQLInsert.js.map +0 -1
  119. package/dist/src/SQLJoin.d.ts.map +0 -1
  120. package/dist/src/SQLJoin.js +0 -61
  121. package/dist/src/SQLJoin.js.map +0 -1
  122. package/dist/src/SQLJsonExpressions.d.ts.map +0 -1
  123. package/dist/src/SQLJsonExpressions.js.map +0 -1
  124. package/dist/src/SQLLogger.d.ts.map +0 -1
  125. package/dist/src/SQLLogger.js.map +0 -1
  126. package/dist/src/SQLOrderBy.d.ts.map +0 -1
  127. package/dist/src/SQLOrderBy.js.map +0 -1
  128. package/dist/src/SQLSelect.d.ts.map +0 -1
  129. package/dist/src/SQLSelect.js.map +0 -1
  130. package/dist/src/SQLTranslatedStringHelper.d.ts.map +0 -1
  131. package/dist/src/SQLTranslatedStringHelper.js +0 -37
  132. package/dist/src/SQLTranslatedStringHelper.js.map +0 -1
  133. package/dist/src/SQLUpdate.d.ts.map +0 -1
  134. package/dist/src/SQLUpdate.js +0 -60
  135. package/dist/src/SQLUpdate.js.map +0 -1
  136. package/dist/src/SQLWhere.d.ts.map +0 -1
  137. package/dist/src/SQLWhere.js.map +0 -1
  138. package/dist/src/filters/SQLFilter.d.ts.map +0 -1
  139. package/dist/src/filters/SQLFilter.js +0 -220
  140. package/dist/src/filters/SQLFilter.js.map +0 -1
  141. package/dist/src/filters/SQLSorter.d.ts.map +0 -1
  142. package/dist/src/filters/SQLSorter.js.map +0 -1
  143. package/dist/src/filters/compilers/contains.d.ts +0 -4
  144. package/dist/src/filters/compilers/contains.d.ts.map +0 -1
  145. package/dist/src/filters/compilers/contains.js +0 -29
  146. package/dist/src/filters/compilers/contains.js.map +0 -1
  147. package/dist/src/filters/compilers/equals.d.ts +0 -4
  148. package/dist/src/filters/compilers/equals.d.ts.map +0 -1
  149. package/dist/src/filters/compilers/equals.js +0 -47
  150. package/dist/src/filters/compilers/equals.js.map +0 -1
  151. package/dist/src/filters/compilers/greater.d.ts +0 -4
  152. package/dist/src/filters/compilers/greater.d.ts.map +0 -1
  153. package/dist/src/filters/compilers/greater.js +0 -18
  154. package/dist/src/filters/compilers/greater.js.map +0 -1
  155. package/dist/src/filters/compilers/in.d.ts +0 -4
  156. package/dist/src/filters/compilers/in.d.ts.map +0 -1
  157. package/dist/src/filters/compilers/in.js +0 -51
  158. package/dist/src/filters/compilers/in.js.map +0 -1
  159. package/dist/src/filters/compilers/index.d.ts.map +0 -1
  160. package/dist/src/filters/compilers/index.js +0 -8
  161. package/dist/src/filters/compilers/index.js.map +0 -1
  162. package/dist/src/filters/compilers/less.d.ts +0 -4
  163. package/dist/src/filters/compilers/less.d.ts.map +0 -1
  164. package/dist/src/filters/compilers/less.js +0 -18
  165. package/dist/src/filters/compilers/less.js.map +0 -1
  166. package/dist/src/filters/helpers/isJSONColumn.d.ts +0 -4
  167. package/dist/src/filters/helpers/isJSONColumn.d.ts.map +0 -1
  168. package/dist/src/filters/helpers/isJSONColumn.js +0 -17
  169. package/dist/src/filters/helpers/isJSONColumn.js.map +0 -1
  170. package/dist/src/filters/helpers/normalizeCompareValue.d.ts.map +0 -1
  171. package/dist/src/filters/helpers/normalizeCompareValue.js +0 -87
  172. package/dist/src/filters/helpers/normalizeCompareValue.js.map +0 -1
  173. package/dist/tests/filters/$and.test.d.ts +0 -2
  174. package/dist/tests/filters/$and.test.d.ts.map +0 -1
  175. package/dist/tests/filters/$and.test.js +0 -216
  176. package/dist/tests/filters/$and.test.js.map +0 -1
  177. package/dist/tests/filters/$contains.test.d.ts +0 -2
  178. package/dist/tests/filters/$contains.test.d.ts.map +0 -1
  179. package/dist/tests/filters/$contains.test.js +0 -917
  180. package/dist/tests/filters/$contains.test.js.map +0 -1
  181. package/dist/tests/filters/$eq.test.d.ts +0 -2
  182. package/dist/tests/filters/$eq.test.d.ts.map +0 -1
  183. package/dist/tests/filters/$eq.test.js +0 -1464
  184. package/dist/tests/filters/$eq.test.js.map +0 -1
  185. package/dist/tests/filters/$gt.test.d.ts +0 -2
  186. package/dist/tests/filters/$gt.test.d.ts.map +0 -1
  187. package/dist/tests/filters/$gt.test.js +0 -641
  188. package/dist/tests/filters/$gt.test.js.map +0 -1
  189. package/dist/tests/filters/$gte.test.d.ts +0 -2
  190. package/dist/tests/filters/$gte.test.d.ts.map +0 -1
  191. package/dist/tests/filters/$gte.test.js +0 -433
  192. package/dist/tests/filters/$gte.test.js.map +0 -1
  193. package/dist/tests/filters/$in.test.d.ts +0 -2
  194. package/dist/tests/filters/$in.test.d.ts.map +0 -1
  195. package/dist/tests/filters/$in.test.js +0 -773
  196. package/dist/tests/filters/$in.test.js.map +0 -1
  197. package/dist/tests/filters/$lt.test.d.ts +0 -2
  198. package/dist/tests/filters/$lt.test.d.ts.map +0 -1
  199. package/dist/tests/filters/$lt.test.js +0 -433
  200. package/dist/tests/filters/$lt.test.js.map +0 -1
  201. package/dist/tests/filters/$lte.test.d.ts +0 -2
  202. package/dist/tests/filters/$lte.test.d.ts.map +0 -1
  203. package/dist/tests/filters/$lte.test.js +0 -472
  204. package/dist/tests/filters/$lte.test.js.map +0 -1
  205. package/dist/tests/filters/$neq.test.d.ts +0 -2
  206. package/dist/tests/filters/$neq.test.d.ts.map +0 -1
  207. package/dist/tests/filters/$neq.test.js +0 -32
  208. package/dist/tests/filters/$neq.test.js.map +0 -1
  209. package/dist/tests/filters/$not.test.d.ts +0 -2
  210. package/dist/tests/filters/$not.test.d.ts.map +0 -1
  211. package/dist/tests/filters/$not.test.js +0 -50
  212. package/dist/tests/filters/$not.test.js.map +0 -1
  213. package/dist/tests/filters/$or.test.d.ts +0 -2
  214. package/dist/tests/filters/$or.test.d.ts.map +0 -1
  215. package/dist/tests/filters/$or.test.js +0 -185
  216. package/dist/tests/filters/$or.test.js.map +0 -1
  217. package/dist/tests/filters/SQLTranslatedStringHelper.test.d.ts +0 -2
  218. package/dist/tests/filters/SQLTranslatedStringHelper.test.d.ts.map +0 -1
  219. package/dist/tests/filters/SQLTranslatedStringHelper.test.js +0 -491
  220. package/dist/tests/filters/SQLTranslatedStringHelper.test.js.map +0 -1
  221. package/dist/tests/filters/dot-syntax.test.d.ts +0 -2
  222. package/dist/tests/filters/dot-syntax.test.d.ts.map +0 -1
  223. package/dist/tests/filters/dot-syntax.test.js +0 -210
  224. package/dist/tests/filters/dot-syntax.test.js.map +0 -1
  225. package/dist/tests/filters/exists.test.d.ts +0 -2
  226. package/dist/tests/filters/exists.test.d.ts.map +0 -1
  227. package/dist/tests/filters/exists.test.js +0 -96
  228. package/dist/tests/filters/exists.test.js.map +0 -1
  229. package/dist/tests/filters/joined-relations.test.d.ts +0 -2
  230. package/dist/tests/filters/joined-relations.test.d.ts.map +0 -1
  231. package/dist/tests/filters/joined-relations.test.js +0 -167
  232. package/dist/tests/filters/joined-relations.test.js.map +0 -1
  233. package/dist/tests/filters/special-cases.test.d.ts +0 -2
  234. package/dist/tests/filters/special-cases.test.d.ts.map +0 -1
  235. package/dist/tests/filters/special-cases.test.js +0 -114
  236. package/dist/tests/filters/special-cases.test.js.map +0 -1
  237. package/dist/tests/filters/wildcard.test.d.ts +0 -2
  238. package/dist/tests/filters/wildcard.test.d.ts.map +0 -1
  239. package/dist/tests/filters/wildcard.test.js +0 -67
  240. package/dist/tests/filters/wildcard.test.js.map +0 -1
  241. package/dist/tests/jest.global.setup.d.ts +0 -3
  242. package/dist/tests/jest.global.setup.d.ts.map +0 -1
  243. package/dist/tests/jest.global.setup.js +0 -7
  244. package/dist/tests/jest.global.setup.js.map +0 -1
  245. package/dist/tests/jest.setup.d.ts +0 -2
  246. package/dist/tests/jest.setup.d.ts.map +0 -1
  247. package/dist/tests/jest.setup.js +0 -5
  248. package/dist/tests/jest.setup.js.map +0 -1
  249. package/dist/tests/utils/index.d.ts +0 -57
  250. package/dist/tests/utils/index.d.ts.map +0 -1
  251. package/dist/tests/utils/index.js +0 -206
  252. package/dist/tests/utils/index.js.map +0 -1
  253. package/dist/tsconfig.tsbuildinfo +0 -1
  254. package/src/QueryableModel.ts +0 -134
  255. package/src/SQL.ts +0 -128
  256. package/src/SQLDelete.ts +0 -73
  257. package/src/SQLExpression.ts +0 -34
  258. package/src/SQLExpressions.ts +0 -587
  259. package/src/SQLInsert.ts +0 -113
  260. package/src/SQLJsonExpressions.ts +0 -283
  261. package/src/SQLLogger.ts +0 -82
  262. package/src/SQLOrderBy.ts +0 -69
  263. package/src/SQLSelect.ts +0 -520
  264. package/src/SQLTranslatedStringHelper.ts +0 -40
  265. package/src/SQLWhere.ts +0 -744
  266. package/src/filters/SQLSorter.ts +0 -60
  267. package/src/filters/compilers/equals.ts +0 -73
  268. package/src/filters/compilers/greater.ts +0 -21
  269. package/src/filters/compilers/less.ts +0 -20
  270. /package/dist/{src/SQLExpression.d.ts → SQLExpression.d.ts} +0 -0
  271. /package/dist/{src/SQLLogger.d.ts → SQLLogger.d.ts} +0 -0
  272. /package/dist/{src/filters → filters}/compilers/index.d.ts +0 -0
@@ -1,134 +0,0 @@
1
- import { Model, SQLResultNamespacedRow } from '@simonbackx/simple-database';
2
- import { SQLSelect } from './SQLSelect.js';
3
- import { SQL } from './SQL.js';
4
- import { SQLDelete } from './SQLDelete.js';
5
- import { SQLUpdate } from './SQLUpdate.js';
6
- import { SQLInsert } from './SQLInsert.js';
7
- import { SQLExpression } from './SQLExpression.js';
8
- import { ModelCache } from './ModelCache.js';
9
-
10
- export class QueryableModel extends Model {
11
- rawSelectedRow: SQLResultNamespacedRow | null = null;
12
- static cache: ModelCache<QueryableModel> | null = null;
13
-
14
- static select<T extends typeof Model & typeof QueryableModel>(this: T, ...columns: (SQLExpression | string)[]): SQLSelect<InstanceType<T>> {
15
- const transformer = (row: SQLResultNamespacedRow): InstanceType<T> => {
16
- const d = (this as T).fromRow(row[this.table]) as InstanceType<T> | undefined;
17
-
18
- if (this.cache && d) {
19
- this.cache.store(d);
20
- }
21
-
22
- // If there are any other namespaces, include it as metadata
23
-
24
- if (!d) {
25
- console.error('Could not transform row', row, 'into model', this.table, 'check if the primary key is returned in the query');
26
- throw new Error('Missing data for model ' + this.table);
27
- }
28
- if (d instanceof QueryableModel) {
29
- d.rawSelectedRow = row;
30
- }
31
-
32
- return d;
33
- };
34
-
35
- const select = new SQLSelect(transformer, ...(columns.length === 0 ? [SQL.wildcard()] : columns));
36
- return select.from(SQL.table(this.table));
37
- }
38
-
39
- static delete(): SQLDelete {
40
- return SQL.delete().from(SQL.table(this.table));
41
- }
42
-
43
- static update(): SQLUpdate {
44
- return SQL.update(SQL.table(this.table));
45
- }
46
-
47
- static insert(): SQLInsert {
48
- return SQL.insert(SQL.table(this.table));
49
- }
50
-
51
- async refresh() {
52
- const { fields, skipUpdate } = this.getChangedDatabaseProperties();
53
-
54
- if (Object.keys(fields).length > skipUpdate) {
55
- throw new Error('Cannot refresh a model that has unsaved changes');
56
- }
57
-
58
- const me = await (this.static as typeof QueryableModel).select().where(this.static.primary.name, this.getPrimaryKey()).first(true);
59
- this.copyFrom(me);
60
- }
61
-
62
- static async refreshAll<T extends typeof QueryableModel>(this: T, list: InstanceType<T>[]) {
63
- // Cut up in batches of max 100
64
- const batchSize = 100;
65
- for (let i = 0; i < list.length; i += batchSize) {
66
- const batch = list.slice(i, i + batchSize);
67
- const ids = list.map(item => item.getPrimaryKey()).filter(id => id !== null);
68
- if (ids.length === 0) {
69
- continue;
70
- }
71
-
72
- const refreshed = await this.getByIDs(...ids);
73
-
74
- for (const item of batch) {
75
- const refreshedItem = refreshed.find(r => r.getPrimaryKey() === item.getPrimaryKey());
76
- if (refreshedItem) {
77
- for (const column of this.columns.values()) {
78
- item[column.name] = refreshedItem[column.name];
79
- }
80
- }
81
- }
82
- }
83
- }
84
-
85
- /**
86
- * Get a model by its primary key
87
- * @param id primary key
88
- */
89
- static override async getByID<T extends typeof Model>(this: T & typeof QueryableModel, id: number | string, required: true): Promise<InstanceType<T>>;
90
- static override async getByID<T extends typeof Model>(this: T & typeof QueryableModel, id: number | string): Promise<InstanceType<T> | undefined>;
91
- static override async getByID<T extends typeof Model>(this: T & typeof QueryableModel, id: number | string, required?: boolean): Promise<InstanceType<T> | undefined> {
92
- if (this.cache) {
93
- const hit = this.cache.getById(id);
94
- if (hit) {
95
- console.log('Cache hit, get by id', this.name, id);
96
- return hit as InstanceType<T>;
97
- }
98
- }
99
- return ((await (this as any as typeof QueryableModel).select().where(this.primary.name, id).first(required ?? false)) ?? undefined) as any as InstanceType<T> | undefined;
100
- }
101
-
102
- /**
103
- * Get multiple models by their ID
104
- * @param ids primary key of the models you want to fetch
105
- */
106
- static override async getByIDs<T extends typeof Model>(this: T & typeof QueryableModel, ...ids: (number | string)[]): Promise<InstanceType<T>[]> {
107
- if (ids.length === 0) {
108
- return [];
109
- }
110
-
111
- if (this.cache) {
112
- const hits: InstanceType<T>[] = [];
113
- const remaining: (number | string)[] = [];
114
- for (const id of ids) {
115
- const hit = this.cache.getById(id);
116
- if (hit) {
117
- console.log('Cache hit, get by id', this.name, id);
118
- hits.push(hit as InstanceType<T>);
119
- }
120
- else {
121
- remaining.push(id);
122
- }
123
- }
124
- ids = remaining;
125
- if (remaining.length === 0) {
126
- return hits;
127
- }
128
- const fetched = await (this as any as typeof QueryableModel).select().where(this.primary.name, ids).limit(ids.length).fetch() as any as InstanceType<T>[];
129
- return [...fetched, ...hits];
130
- }
131
-
132
- return (this as any as typeof QueryableModel).select().where(this.primary.name, ids).limit(ids.length).fetch() as any as InstanceType<T>[];
133
- }
134
- }
package/src/SQL.ts DELETED
@@ -1,128 +0,0 @@
1
- import { SQLResultNamespacedRow } from '@simonbackx/simple-database';
2
- import { SQLDelete } from './SQLDelete.js';
3
- import { isSQLExpression, SQLExpression } from './SQLExpression.js';
4
- import { SQLAssignment, SQLCoalesce, SQLColumnExpression, SQLColumnExpressionParams, SQLDistinct, SQLIf, SQLIsNull, SQLJSONTableExpression, SQLParentNamespace, SQLSafeValue, SQLScalar, SQLScalarValue, SQLTableExpression, SQLWildcardSelectExpression } from './SQLExpressions.js';
5
- import { SQLInsert } from './SQLInsert.js';
6
- import { SQLJoin, SQLJoinType } from './SQLJoin.js';
7
- import { SQLJsonExtract, SQLJsonKeys, SQLJsonLength, SQLJsonType, SQLJsonUnquote, SQLJsonValue, SQLJsonValueType, SQLLpad } from './SQLJsonExpressions.js';
8
- import { parseTable, SQLSelect } from './SQLSelect.js';
9
- import { SQLUpdate } from './SQLUpdate.js';
10
- import { ParseWhereArguments, SQLEmptyWhere, SQLWhere } from './SQLWhere.js';
11
-
12
- class StaticSQL {
13
- wildcard(namespace?: string) {
14
- return new SQLWildcardSelectExpression(namespace);
15
- }
16
-
17
- column(...args: SQLColumnExpressionParams): SQLColumnExpression {
18
- return new SQLColumnExpression(...args);
19
- }
20
-
21
- parentColumn(column: string): SQLColumnExpression {
22
- return new SQLColumnExpression(SQLParentNamespace, column);
23
- }
24
-
25
- jsonExtract(column: SQLExpression, path: string, asScalar = false): SQLJsonExtract {
26
- return new SQLJsonExtract(column, asScalar ? new SQLScalar(path) : new SQLSafeValue(path));
27
- }
28
-
29
- jsonValue(column: SQLExpression, path: string, type?: SQLJsonValueType, asScalar = false): SQLJsonValue {
30
- return new SQLJsonValue(column, type, asScalar ? new SQLScalar(path) : new SQLSafeValue(path));
31
- }
32
-
33
- jsonKeys(column: SQLExpression): SQLJsonKeys {
34
- return new SQLJsonKeys(column);
35
- }
36
-
37
- jsonType(column: SQLExpression): SQLJsonType {
38
- return new SQLJsonType(column);
39
- }
40
-
41
- lpad(column: SQLExpression, length: number, value: string): SQLLpad {
42
- return new SQLLpad(column, new SQLSafeValue(length), new SQLSafeValue(value));
43
- }
44
-
45
- jsonUnquotedValue(column: SQLExpression, path: string): SQLJsonUnquote {
46
- return new SQLJsonUnquote(new SQLJsonExtract(column, new SQLSafeValue(path)));
47
- }
48
-
49
- jsonLength(column: SQLExpression, path?: string): SQLJsonLength {
50
- return new SQLJsonLength(column, path ? new SQLSafeValue(path) : undefined);
51
- }
52
-
53
- table(table: string, asNamespace?: string): SQLTableExpression {
54
- return new SQLTableExpression(table, asNamespace);
55
- }
56
-
57
- jsonTable(expression: SQLExpression, asNamespace: string): SQLJSONTableExpression {
58
- return new SQLJSONTableExpression(expression, asNamespace);
59
- }
60
-
61
- select(...columns: (SQLExpression | string)[]): InstanceType<typeof SQLSelect<SQLResultNamespacedRow>> {
62
- if (columns.length === 0) {
63
- return new SQLSelect(this.wildcard());
64
- }
65
- return new SQLSelect(...columns);
66
- }
67
-
68
- insert(tableName: SQLTableExpression | string): InstanceType<typeof SQLInsert> {
69
- return new SQLInsert(tableName);
70
- }
71
-
72
- update(tableName: SQLTableExpression | string): InstanceType<typeof SQLUpdate> {
73
- return new SQLUpdate(tableName);
74
- }
75
-
76
- assignment(key: SQLExpression | string, value: SQLExpression | SQLScalarValue): SQLAssignment {
77
- return new SQLAssignment(typeof key === 'string' ? new SQLColumnExpression(key) : key, isSQLExpression(value) ? value : new SQLScalar(value));
78
- }
79
-
80
- where(...args: ParseWhereArguments): SQLWhere {
81
- return new SQLEmptyWhere().and(...args);
82
- }
83
-
84
- whereNot(...args: ParseWhereArguments): SQLWhere {
85
- return new SQLEmptyWhere().andNot(...args);
86
- }
87
-
88
- delete(): InstanceType<typeof SQLDelete> {
89
- return new SQLDelete();
90
- }
91
-
92
- leftJoin(...args: Parameters<typeof parseTable>): SQLJoin {
93
- return new SQLJoin(SQLJoinType.Left, parseTable(...args));
94
- }
95
-
96
- rightJoin(...args: Parameters<typeof parseTable>): SQLJoin {
97
- return new SQLJoin(SQLJoinType.Right, parseTable(...args));
98
- }
99
-
100
- innerJoin(...args: Parameters<typeof parseTable>): SQLJoin {
101
- return new SQLJoin(SQLJoinType.Inner, parseTable(...args));
102
- }
103
-
104
- join(...args: Parameters<typeof parseTable>): SQLJoin {
105
- return new SQLJoin(SQLJoinType.Inner, parseTable(...args));
106
- }
107
-
108
- if(...args: ConstructorParameters<typeof SQLIf>): SQLIf {
109
- return new SQLIf(...args);
110
- }
111
-
112
- coalesce(...args: ConstructorParameters<typeof SQLCoalesce>): SQLCoalesce {
113
- return new SQLCoalesce(...args);
114
- }
115
-
116
- distinct(column?: SQLExpression): InstanceType<typeof SQLDistinct> {
117
- if (!column) {
118
- return new SQLDistinct(this.wildcard());
119
- }
120
- return new SQLDistinct(column);
121
- }
122
-
123
- isNull(column: SQLExpression): SQLIsNull {
124
- return new SQLIsNull(column);
125
- }
126
- }
127
-
128
- export const SQL = new StaticSQL();
package/src/SQLDelete.ts DELETED
@@ -1,73 +0,0 @@
1
- import { Database } from '@simonbackx/simple-database';
2
- import { SQLExpression, SQLExpressionOptions, SQLQuery, joinSQLQuery, normalizeSQLQuery } from './SQLExpression.js';
3
- import { SQLJoin } from './SQLJoin.js';
4
- import { SQLLogger } from './SQLLogger.js';
5
- import { Whereable } from './SQLWhere.js';
6
-
7
- class EmptyClass {}
8
- export class SQLDelete extends Whereable(EmptyClass) implements SQLExpression {
9
- _from: SQLExpression;
10
- _joins: (InstanceType<typeof SQLJoin>)[] = [];
11
-
12
- clone(): this {
13
- const c = new SQLDelete();
14
- Object.assign(c, this);
15
- return c as any;
16
- }
17
-
18
- from(table: SQLExpression): this {
19
- this._from = table;
20
- return this;
21
- }
22
-
23
- join(join: InstanceType<typeof SQLJoin>): this {
24
- this._joins.push(join);
25
- return this;
26
- }
27
-
28
- getSQL(options?: SQLExpressionOptions): SQLQuery {
29
- const query: SQLQuery[] = [
30
- 'DELETE',
31
- ];
32
-
33
- options = options ?? {};
34
- options.defaultNamespace = (this._from as any).namespace ?? (this._from as any).table ?? undefined;
35
-
36
- query.push(
37
- 'FROM',
38
- );
39
-
40
- query.push(this._from.getSQL(options));
41
-
42
- query.push(...this._joins.map(j => j.getSQL(options)));
43
-
44
- // Where
45
- if (this._where) {
46
- const always = this._where.isAlways;
47
- if (always === false) {
48
- throw new Error('Cannot use SQLDelete with a where that is not always true');
49
- }
50
- else if (always === null) {
51
- query.push('WHERE');
52
- query.push(this._where.getSQL(options));
53
- }
54
- }
55
-
56
- return joinSQLQuery(query, ' ');
57
- }
58
-
59
- async delete(): Promise<{ affectedRows: number }> {
60
- if (this._where && this._where.isAlways === false) {
61
- return { affectedRows: 0 };
62
- }
63
-
64
- const { query, params } = normalizeSQLQuery(this.getSQL());
65
-
66
- const [rows] = await SQLLogger.log(Database.delete(query, params), query, params);
67
- return rows;
68
- }
69
-
70
- async then(onFulfilled: (value: { affectedRows: number }) => any, onRejected: (reason: any) => any): Promise<any> {
71
- return this.delete().then(onFulfilled, onRejected);
72
- }
73
- }
@@ -1,34 +0,0 @@
1
- export type SQLExpressionOptions = {
2
- defaultNamespace?: string;
3
- parentNamespace?: string;
4
- };
5
-
6
- export type NormalizedSQLQuery = { query: string; params: any[] };
7
- export type SQLQuery = NormalizedSQLQuery | string;
8
-
9
- export function joinSQLQuery(queries: (SQLQuery | undefined | null)[], seperator?: string): NormalizedSQLQuery {
10
- queries = queries.filter(q => q !== undefined && q !== null);
11
- return {
12
- query: queries.map(q => typeof q === 'string' ? q : q!.query).join(seperator ?? ''),
13
- params: queries.flatMap(q => typeof q === 'string' ? [] : q!.params),
14
- };
15
- }
16
-
17
- export function normalizeSQLQuery(q: SQLQuery): NormalizedSQLQuery {
18
- return {
19
- query: typeof q === 'string' ? q : q.query,
20
- params: typeof q === 'string' ? [] : q.params,
21
- };
22
- }
23
-
24
- export interface SQLExpression {
25
- getSQL(options?: SQLExpressionOptions): SQLQuery;
26
- }
27
-
28
- export interface SQLNamedExpression extends SQLExpression {
29
- getName(): string;
30
- }
31
-
32
- export function isSQLExpression(obj: unknown): obj is SQLExpression {
33
- return typeof obj === 'object' && obj !== null && !!(obj as any).getSQL && typeof (obj as any).getSQL === 'function';
34
- }