@mlagie/sql-connector 3.2.0 → 3.3.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.
package/index.d.ts CHANGED
@@ -362,6 +362,19 @@ export type SelectAggregation = {
362
362
  */
363
363
  count?: string | string[] | Record<string, any>;
364
364
 
365
+ /**
366
+ * Calculates MAX(column).
367
+ *
368
+ * A string, list of strings or object may be supplied
369
+ * depending on the query builder expression.
370
+ *
371
+ * @example
372
+ * ```ts
373
+ * { max: "created_at", as: "latest" }
374
+ * ```
375
+ */
376
+ max?: string | string[] | Record<string, any>;
377
+
365
378
  /**
366
379
  * Alias assigned to the resulting expression.
367
380
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mlagie/sql-connector",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "description": "The sql-connector module allows you to manage connections to a MySQL database, define table schemas, and interact with data in a simple and efficient way.",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -171,37 +171,13 @@ class SchemaDiffer {
171
171
 
172
172
  /*
173
173
  * Second pass:
174
- * detect additions and modifications.
175
- */
176
- for (const [columnName, field] of Object.entries(desiredColumns)) {
177
- const currentField = getSafe(virtualCurrentColumns, columnName);
178
-
179
- if (!currentField) {
180
- operations.push({
181
- type: "addColumn",
182
- table: tableName,
183
- column: columnName,
184
- field
185
- });
186
-
187
- continue;
188
- }
189
-
190
- if (!equivalentField(field, currentField)) {
191
- operations.push({
192
- type: "alterColumn",
193
- table: tableName,
194
- column: columnName,
195
- field,
196
- previous: currentField
197
- });
198
- }
199
- }
200
-
201
- /*
202
- * Third pass:
203
174
  * detect removed columns.
204
175
  *
176
+ * Deletions are emitted BEFORE additions/alterations so that a
177
+ * dropped primary key column (e.g. `uuid`) releases its PRIMARY KEY
178
+ * before a new one (e.g. `id`) is added; otherwise PostgreSQL fails
179
+ * with "multiple primary keys for table".
180
+ *
205
181
  * Renamed columns are excluded because their old name
206
182
  * intentionally disappears from the desired schema.
207
183
  */
@@ -228,6 +204,35 @@ class SchemaDiffer {
228
204
  operations.push(operation);
229
205
  }
230
206
  }
207
+ /*
208
+ * Third pass:
209
+ * detect additions and modifications (after the deletions).
210
+ */
211
+ for (const [columnName, field] of Object.entries(desiredColumns)) {
212
+ const currentField = getSafe(virtualCurrentColumns, columnName);
213
+
214
+ if (!currentField) {
215
+ operations.push({
216
+ type: "addColumn",
217
+ table: tableName,
218
+ column: columnName,
219
+ field
220
+ });
221
+
222
+ continue;
223
+ }
224
+
225
+ if (!equivalentField(field, currentField)) {
226
+ operations.push({
227
+ type: "alterColumn",
228
+ table: tableName,
229
+ column: columnName,
230
+ field,
231
+ previous: currentField
232
+ });
233
+ }
234
+ }
235
+
231
236
  }
232
237
 
233
238
  /*
@@ -0,0 +1,11 @@
1
+ const { getDialect } = require("../../db/dialects");
2
+
3
+ module.exports = (aggregateType, field) => {
4
+ if (typeof field === 'string') return `${aggregateType}(${getDialect().escape(field)})`;
5
+ if (field instanceof Array && field !== null) return field.map(col => `${aggregateType}(${getDialect().escape(col)})`).join(' + ');
6
+ if (field instanceof Object) {
7
+ const [key, value] = Object.entries(field)[0];
8
+ return `${aggregateType}(CASE WHEN ${getDialect().escape(key)} = ${getDialect().escapeValue(value)} THEN 1 END)`;
9
+ }
10
+ throw new Error(`Invalid field type for ${aggregateType}. Must be a string, array, or object.`);
11
+ }
@@ -1,6 +1,6 @@
1
1
  const { getDialect } = require("../../db/dialects");
2
2
  const { escapeOrderDirection } = require("../sql");
3
- const count = require("./count");
3
+ const buildAggregationField = require("./buildAggregationField");
4
4
 
5
5
  function buildGroupByItem(group) {
6
6
  let sql;
@@ -43,7 +43,9 @@ function buildField(field) {
43
43
  else if (field.distinct)
44
44
  sql = `DISTINCT ${getDialect().escape(field.distinct)}`;
45
45
  else if (field.count)
46
- sql = count(field.count);
46
+ sql = buildAggregationField("COUNT", field.count);
47
+ else if (field.max)
48
+ sql = buildAggregationField("MAX", field.max);
47
49
  if (field.as)
48
50
  sql += ` AS ${getDialect().escape(field.as)}`;
49
51
  else if (field.sum)
@@ -1,11 +0,0 @@
1
- const { getDialect } = require("../../db/dialects");
2
-
3
- module.exports = (field) => {
4
- if (typeof field === 'string') return `COUNT(${getDialect().escape(field)})`;
5
- if (field instanceof Array && field !== null) return field.map(col => `COUNT(${getDialect().escape(col)})`).join(' + ');
6
- if (field instanceof Object) {
7
- const [key, value] = Object.entries(field)[0];
8
- return `COUNT(CASE WHEN ${getDialect().escape(key)} = ${getDialect().escapeValue(value)} THEN 1 END)`;
9
- }
10
- throw new Error("Invalid field type for COUNT. Must be a string, array, or object.");
11
- }