@heyhru/app-dms-server 0.7.0 → 0.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.
Files changed (2) hide show
  1. package/dist/index.js +25 -7
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -252,20 +252,28 @@ function postSqlParse(req, reply) {
252
252
  return reply.send({ category: sqlParse(sql) });
253
253
  }
254
254
  async function postSqlExecute(req, reply) {
255
- const { dataSourceId, database, sql } = req.body ?? {};
255
+ const body = req.body ?? {};
256
+ const { dataSourceId, database, sql } = body;
256
257
  if (!dataSourceId || !sql) {
257
258
  return reply.code(400).send({ error: "\u6570\u636E\u6E90 ID \u548C SQL \u4E0D\u80FD\u4E3A\u7A7A" });
258
259
  }
260
+ const page = typeof body.page === "number" ? body.page : 1;
261
+ const pageSize = typeof body.pageSize === "number" ? body.pageSize : 50;
259
262
  const ip = req.headers["x-forwarded-for"] ?? req.headers["x-real-ip"] ?? "unknown";
260
263
  try {
261
- const rows = await sqlExecute({ dataSourceId, database, sql, userId: req.user.id, ip });
262
- return reply.send({ rows });
264
+ const result = await sqlExecute({ dataSourceId, database, sql, userId: req.user.id, ip, page, pageSize });
265
+ return reply.send(result);
263
266
  } catch (err) {
264
267
  req.log.error(err, "SQL execute failed (user=%s, ds=%s)", req.user.id, dataSourceId);
265
268
  return reply.code(400).send({ error: err instanceof Error ? err.message : String(err) });
266
269
  }
267
270
  }
268
- async function sqlExecute({ dataSourceId, database, sql, userId, ip }) {
271
+ async function fetchCountQuery(pool, sql) {
272
+ const countSql = `SELECT COUNT(*) AS total FROM (${sql}) AS _count_t`;
273
+ const rows = await pool.execute(countSql);
274
+ return Number(rows[0]?.total ?? 0);
275
+ }
276
+ async function sqlExecute({ dataSourceId, database, sql, userId, ip, page, pageSize }) {
269
277
  const category = sqlParse(sql);
270
278
  if (category !== "select") {
271
279
  throw new Error("\u4EC5\u5141\u8BB8\u76F4\u63A5\u6267\u884C SELECT \u8BED\u53E5");
@@ -275,10 +283,20 @@ async function sqlExecute({ dataSourceId, database, sql, userId, ip }) {
275
283
  return ds ? getPool(ds) : null;
276
284
  })();
277
285
  if (!pool) throw new Error("\u6570\u636E\u6E90\u672A\u627E\u5230");
286
+ const hasLimit = /\bLIMIT\b/i.test(sql);
287
+ const dataSql = hasLimit ? sql : `${sql} LIMIT ${pageSize} OFFSET ${(page - 1) * pageSize}`;
278
288
  const end = sqlDuration.startTimer({ data_source_id: dataSourceId, sql_type: category });
279
289
  let rows;
290
+ let total;
280
291
  try {
281
- rows = await pool.execute(sql);
292
+ if (hasLimit) {
293
+ rows = await pool.execute(dataSql);
294
+ total = rows.length;
295
+ } else {
296
+ const [rawRows, count] = await Promise.all([pool.execute(dataSql), fetchCountQuery(pool, sql)]);
297
+ rows = rawRows;
298
+ total = count;
299
+ }
282
300
  end({ status: "success" });
283
301
  } catch (err) {
284
302
  end({ status: "failed" });
@@ -289,10 +307,10 @@ async function sqlExecute({ dataSourceId, database, sql, userId, ip }) {
289
307
  dataSourceId,
290
308
  action: "SELECT",
291
309
  sqlText: sql,
292
- resultSummary: `${rows.length} rows`,
310
+ resultSummary: `${total} total rows`,
293
311
  ipAddress: ip
294
312
  });
295
- return rows;
313
+ return { rows, total, page, pageSize };
296
314
  }
297
315
  async function postSqlDatabases(req, reply) {
298
316
  const { dataSourceId } = req.body ?? {};
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.7.0",
6
+ "version": "0.8.0",
7
7
  "description": "DMS backend API server built on Fastify",
8
8
  "type": "module",
9
9
  "main": "./dist/index.mjs",
@@ -19,9 +19,9 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "@fastify/cors": "^11.2.0",
22
- "@heyhru/business-dms-approval": "0.6.1",
22
+ "@heyhru/business-dms-approval": "0.6.2",
23
23
  "@heyhru/business-dms-audit": "0.6.1",
24
- "@heyhru/business-dms-datasource": "0.7.0",
24
+ "@heyhru/business-dms-datasource": "0.7.1",
25
25
  "@heyhru/business-dms-saved-sql": "0.6.1",
26
26
  "@heyhru/business-dms-user": "0.6.1",
27
27
  "@heyhru/common-util-logger": "0.6.0",
@@ -42,5 +42,5 @@
42
42
  "typescript": "^6.0.2",
43
43
  "vitest": "^4.1.4"
44
44
  },
45
- "gitHead": "b9097b9ad29ac4c0c3610a39cb0db83f74a11fb8"
45
+ "gitHead": "b20e6c3e571bd4939fd32ece8f5bc6d8302a4c83"
46
46
  }