@dyrected/db-mysql 2.7.0 → 2.8.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/dist/index.cjs +65 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +65 -0
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -348,6 +348,71 @@ FIX INSTRUCTIONS:
|
|
|
348
348
|
await this.pool.end();
|
|
349
349
|
}
|
|
350
350
|
}
|
|
351
|
+
async aggregate(args) {
|
|
352
|
+
if (Object.keys(args.aggregates).length === 0) {
|
|
353
|
+
return {};
|
|
354
|
+
}
|
|
355
|
+
const tableName = this.getTableName(args.collection);
|
|
356
|
+
if (!this.inTransaction) await this.ensureTable(args.collection);
|
|
357
|
+
const [cols] = await this.query(`SHOW COLUMNS FROM \`${tableName}\``);
|
|
358
|
+
const existingCols = cols.map((c) => c.Field);
|
|
359
|
+
const toFieldExpr = (field) => {
|
|
360
|
+
if (field === "createdAt") return "`created_at`";
|
|
361
|
+
if (field === "updatedAt") return "`updated_at`";
|
|
362
|
+
if (existingCols.includes(field) && !["id", "data"].includes(field)) {
|
|
363
|
+
return `\`${field}\``;
|
|
364
|
+
}
|
|
365
|
+
return `JSON_UNQUOTE(JSON_EXTRACT(data, '$.${field}'))`;
|
|
366
|
+
};
|
|
367
|
+
const toCastExpr = (rawField, cast) => {
|
|
368
|
+
const base = toFieldExpr(rawField);
|
|
369
|
+
if (cast === "string") return base;
|
|
370
|
+
if (cast === "boolean") return `IF(${base} IS NOT NULL, IF(${base} IN ('true','1'), 1, 0), NULL)`;
|
|
371
|
+
if (cast === "date") return `CAST(${base} AS DATETIME)`;
|
|
372
|
+
const sqlType = cast === "integer" ? "SIGNED" : "DECIMAL(20,6)";
|
|
373
|
+
return `IF(${base} REGEXP '^-?[0-9]+(\\\\.[0-9]+)?([eE][+-]?[0-9]+)?$', CAST(${base} AS ${sqlType}), NULL)`;
|
|
374
|
+
};
|
|
375
|
+
const selectParts = [];
|
|
376
|
+
const allParams = [];
|
|
377
|
+
for (const [name, op] of Object.entries(args.aggregates)) {
|
|
378
|
+
let whereSql = null;
|
|
379
|
+
if (op.where && Object.keys(op.where).length > 0) {
|
|
380
|
+
const parsed = (0, import_core.parseSqlWhere)(op.where, toFieldExpr, "?");
|
|
381
|
+
whereSql = parsed.sql;
|
|
382
|
+
allParams.push(...parsed.params);
|
|
383
|
+
}
|
|
384
|
+
let aggExpr;
|
|
385
|
+
if ("count" in op) {
|
|
386
|
+
aggExpr = whereSql ? `COUNT(IF(${whereSql}, 1, NULL))` : `COUNT(*)`;
|
|
387
|
+
} else if (op.sum) {
|
|
388
|
+
const val = toCastExpr(op.sum, op.cast);
|
|
389
|
+
aggExpr = whereSql ? `SUM(IF(${whereSql}, ${val}, NULL))` : `SUM(${val})`;
|
|
390
|
+
} else if (op.avg) {
|
|
391
|
+
const val = toCastExpr(op.avg, op.cast);
|
|
392
|
+
aggExpr = whereSql ? `AVG(IF(${whereSql}, ${val}, NULL))` : `AVG(${val})`;
|
|
393
|
+
} else if (op.min) {
|
|
394
|
+
const val = toCastExpr(op.min, op.cast);
|
|
395
|
+
aggExpr = whereSql ? `MIN(IF(${whereSql}, ${val}, NULL))` : `MIN(${val})`;
|
|
396
|
+
} else if (op.max) {
|
|
397
|
+
const val = toCastExpr(op.max, op.cast);
|
|
398
|
+
aggExpr = whereSql ? `MAX(IF(${whereSql}, ${val}, NULL))` : `MAX(${val})`;
|
|
399
|
+
} else {
|
|
400
|
+
aggExpr = whereSql ? `COUNT(IF(${whereSql}, 1, NULL))` : `COUNT(*)`;
|
|
401
|
+
}
|
|
402
|
+
selectParts.push(
|
|
403
|
+
`${aggExpr} AS \`${name.replace(/`/g, "``")}\``
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
const query = `SELECT ${selectParts.join(", ")} FROM \`${tableName}\``;
|
|
407
|
+
const [rows] = await this.query(query, allParams);
|
|
408
|
+
const row = rows[0] ?? {};
|
|
409
|
+
const result = {};
|
|
410
|
+
for (const name of Object.keys(args.aggregates)) {
|
|
411
|
+
const raw = row[name];
|
|
412
|
+
result[name] = raw === null || raw === void 0 ? null : Number(raw);
|
|
413
|
+
}
|
|
414
|
+
return result;
|
|
415
|
+
}
|
|
351
416
|
};
|
|
352
417
|
var mysqlAdapter = (config) => new MysqlAdapter(config);
|
|
353
418
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/index.d.cts
CHANGED
|
@@ -58,6 +58,10 @@ declare class MysqlAdapter implements DatabaseAdapter {
|
|
|
58
58
|
transaction<T>(callback: (db: DatabaseAdapter) => Promise<T>): Promise<T>;
|
|
59
59
|
/** Gracefully close the connection pool. Call on process exit. */
|
|
60
60
|
close(): Promise<void>;
|
|
61
|
+
aggregate(args: {
|
|
62
|
+
collection: string;
|
|
63
|
+
aggregates: Record<string, any>;
|
|
64
|
+
}): Promise<Record<string, number | null>>;
|
|
61
65
|
}
|
|
62
66
|
declare const mysqlAdapter: (config: MysqlAdapterConfig) => MysqlAdapter;
|
|
63
67
|
|
package/dist/index.d.ts
CHANGED
|
@@ -58,6 +58,10 @@ declare class MysqlAdapter implements DatabaseAdapter {
|
|
|
58
58
|
transaction<T>(callback: (db: DatabaseAdapter) => Promise<T>): Promise<T>;
|
|
59
59
|
/** Gracefully close the connection pool. Call on process exit. */
|
|
60
60
|
close(): Promise<void>;
|
|
61
|
+
aggregate(args: {
|
|
62
|
+
collection: string;
|
|
63
|
+
aggregates: Record<string, any>;
|
|
64
|
+
}): Promise<Record<string, number | null>>;
|
|
61
65
|
}
|
|
62
66
|
declare const mysqlAdapter: (config: MysqlAdapterConfig) => MysqlAdapter;
|
|
63
67
|
|
package/dist/index.js
CHANGED
|
@@ -313,6 +313,71 @@ FIX INSTRUCTIONS:
|
|
|
313
313
|
await this.pool.end();
|
|
314
314
|
}
|
|
315
315
|
}
|
|
316
|
+
async aggregate(args) {
|
|
317
|
+
if (Object.keys(args.aggregates).length === 0) {
|
|
318
|
+
return {};
|
|
319
|
+
}
|
|
320
|
+
const tableName = this.getTableName(args.collection);
|
|
321
|
+
if (!this.inTransaction) await this.ensureTable(args.collection);
|
|
322
|
+
const [cols] = await this.query(`SHOW COLUMNS FROM \`${tableName}\``);
|
|
323
|
+
const existingCols = cols.map((c) => c.Field);
|
|
324
|
+
const toFieldExpr = (field) => {
|
|
325
|
+
if (field === "createdAt") return "`created_at`";
|
|
326
|
+
if (field === "updatedAt") return "`updated_at`";
|
|
327
|
+
if (existingCols.includes(field) && !["id", "data"].includes(field)) {
|
|
328
|
+
return `\`${field}\``;
|
|
329
|
+
}
|
|
330
|
+
return `JSON_UNQUOTE(JSON_EXTRACT(data, '$.${field}'))`;
|
|
331
|
+
};
|
|
332
|
+
const toCastExpr = (rawField, cast) => {
|
|
333
|
+
const base = toFieldExpr(rawField);
|
|
334
|
+
if (cast === "string") return base;
|
|
335
|
+
if (cast === "boolean") return `IF(${base} IS NOT NULL, IF(${base} IN ('true','1'), 1, 0), NULL)`;
|
|
336
|
+
if (cast === "date") return `CAST(${base} AS DATETIME)`;
|
|
337
|
+
const sqlType = cast === "integer" ? "SIGNED" : "DECIMAL(20,6)";
|
|
338
|
+
return `IF(${base} REGEXP '^-?[0-9]+(\\\\.[0-9]+)?([eE][+-]?[0-9]+)?$', CAST(${base} AS ${sqlType}), NULL)`;
|
|
339
|
+
};
|
|
340
|
+
const selectParts = [];
|
|
341
|
+
const allParams = [];
|
|
342
|
+
for (const [name, op] of Object.entries(args.aggregates)) {
|
|
343
|
+
let whereSql = null;
|
|
344
|
+
if (op.where && Object.keys(op.where).length > 0) {
|
|
345
|
+
const parsed = parseSqlWhere(op.where, toFieldExpr, "?");
|
|
346
|
+
whereSql = parsed.sql;
|
|
347
|
+
allParams.push(...parsed.params);
|
|
348
|
+
}
|
|
349
|
+
let aggExpr;
|
|
350
|
+
if ("count" in op) {
|
|
351
|
+
aggExpr = whereSql ? `COUNT(IF(${whereSql}, 1, NULL))` : `COUNT(*)`;
|
|
352
|
+
} else if (op.sum) {
|
|
353
|
+
const val = toCastExpr(op.sum, op.cast);
|
|
354
|
+
aggExpr = whereSql ? `SUM(IF(${whereSql}, ${val}, NULL))` : `SUM(${val})`;
|
|
355
|
+
} else if (op.avg) {
|
|
356
|
+
const val = toCastExpr(op.avg, op.cast);
|
|
357
|
+
aggExpr = whereSql ? `AVG(IF(${whereSql}, ${val}, NULL))` : `AVG(${val})`;
|
|
358
|
+
} else if (op.min) {
|
|
359
|
+
const val = toCastExpr(op.min, op.cast);
|
|
360
|
+
aggExpr = whereSql ? `MIN(IF(${whereSql}, ${val}, NULL))` : `MIN(${val})`;
|
|
361
|
+
} else if (op.max) {
|
|
362
|
+
const val = toCastExpr(op.max, op.cast);
|
|
363
|
+
aggExpr = whereSql ? `MAX(IF(${whereSql}, ${val}, NULL))` : `MAX(${val})`;
|
|
364
|
+
} else {
|
|
365
|
+
aggExpr = whereSql ? `COUNT(IF(${whereSql}, 1, NULL))` : `COUNT(*)`;
|
|
366
|
+
}
|
|
367
|
+
selectParts.push(
|
|
368
|
+
`${aggExpr} AS \`${name.replace(/`/g, "``")}\``
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
const query = `SELECT ${selectParts.join(", ")} FROM \`${tableName}\``;
|
|
372
|
+
const [rows] = await this.query(query, allParams);
|
|
373
|
+
const row = rows[0] ?? {};
|
|
374
|
+
const result = {};
|
|
375
|
+
for (const name of Object.keys(args.aggregates)) {
|
|
376
|
+
const raw = row[name];
|
|
377
|
+
result[name] = raw === null || raw === void 0 ? null : Number(raw);
|
|
378
|
+
}
|
|
379
|
+
return result;
|
|
380
|
+
}
|
|
316
381
|
};
|
|
317
382
|
var mysqlAdapter = (config) => new MysqlAdapter(config);
|
|
318
383
|
export {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dyrected/db-mysql",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.8.0",
|
|
4
4
|
"description": "MySQL adapter for Dyrected CMS",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"mysql2": "^3.22.3",
|
|
21
|
-
"@dyrected/core": "^2.
|
|
21
|
+
"@dyrected/core": "^2.8.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"tsup": "^8.0.0",
|