@dyrected/db-mysql 2.5.27 → 2.5.28

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 CHANGED
@@ -36,6 +36,19 @@ __export(index_exports, {
36
36
  module.exports = __toCommonJS(index_exports);
37
37
  var import_core = require("@dyrected/core");
38
38
  var import_promise = __toESM(require("mysql2/promise"), 1);
39
+ function escapeMysqlIdentifier(identifier) {
40
+ return `\`${identifier.replace(/`/g, "``")}\``;
41
+ }
42
+ function normalizeMysqlSort(sort, existingCols) {
43
+ return (0, import_core.parseSort)(sort).map(({ field, direction }) => {
44
+ if (field === "createdAt" || field === "created_at") return `\`created_at\` ${direction}`;
45
+ if (field === "updatedAt" || field === "updated_at") return `\`updated_at\` ${direction}`;
46
+ if (existingCols.includes(field) && !["id", "data"].includes(field)) {
47
+ return `${escapeMysqlIdentifier(field)} ${direction}`;
48
+ }
49
+ return `JSON_UNQUOTE(JSON_EXTRACT(data, '$.${field}')) ${direction}`;
50
+ }).join(", ");
51
+ }
39
52
  var MysqlAdapter = class {
40
53
  pool;
41
54
  config;
@@ -196,8 +209,7 @@ FIX INSTRUCTIONS:
196
209
  whereSql = `WHERE ${result.sql}`;
197
210
  whereParams = result.params;
198
211
  }
199
- const rawSort = args.sort ?? "created_at DESC";
200
- const sort = rawSort.replace(/\bcreatedAt\b/g, "created_at").replace(/\bupdatedAt\b/g, "updated_at");
212
+ const sort = normalizeMysqlSort(args.sort, existingCols);
201
213
  const [countRows] = await this.query(
202
214
  `SELECT COUNT(*) AS total FROM \`${tableName}\` ${whereSql}`,
203
215
  whereParams
package/dist/index.js CHANGED
@@ -1,6 +1,19 @@
1
1
  // src/index.ts
2
- import { parseSqlWhere } from "@dyrected/core";
2
+ import { parseSort, parseSqlWhere } from "@dyrected/core";
3
3
  import mysql from "mysql2/promise";
4
+ function escapeMysqlIdentifier(identifier) {
5
+ return `\`${identifier.replace(/`/g, "``")}\``;
6
+ }
7
+ function normalizeMysqlSort(sort, existingCols) {
8
+ return parseSort(sort).map(({ field, direction }) => {
9
+ if (field === "createdAt" || field === "created_at") return `\`created_at\` ${direction}`;
10
+ if (field === "updatedAt" || field === "updated_at") return `\`updated_at\` ${direction}`;
11
+ if (existingCols.includes(field) && !["id", "data"].includes(field)) {
12
+ return `${escapeMysqlIdentifier(field)} ${direction}`;
13
+ }
14
+ return `JSON_UNQUOTE(JSON_EXTRACT(data, '$.${field}')) ${direction}`;
15
+ }).join(", ");
16
+ }
4
17
  var MysqlAdapter = class {
5
18
  pool;
6
19
  config;
@@ -161,8 +174,7 @@ FIX INSTRUCTIONS:
161
174
  whereSql = `WHERE ${result.sql}`;
162
175
  whereParams = result.params;
163
176
  }
164
- const rawSort = args.sort ?? "created_at DESC";
165
- const sort = rawSort.replace(/\bcreatedAt\b/g, "created_at").replace(/\bupdatedAt\b/g, "updated_at");
177
+ const sort = normalizeMysqlSort(args.sort, existingCols);
166
178
  const [countRows] = await this.query(
167
179
  `SELECT COUNT(*) AS total FROM \`${tableName}\` ${whereSql}`,
168
180
  whereParams
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dyrected/db-mysql",
3
- "version": "2.5.27",
3
+ "version": "2.5.28",
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.5.27"
21
+ "@dyrected/core": "^2.5.28"
22
22
  },
23
23
  "devDependencies": {
24
24
  "tsup": "^8.0.0",