@aria-framework/db-worker 0.1.0 → 0.2.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 (3) hide show
  1. package/README.md +10 -1
  2. package/client.js +14 -2
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -74,7 +74,12 @@ once the worker has opened the DB and run migrations.
74
74
  ## API
75
75
 
76
76
  ### `DbClient` (main thread)
77
- - `new DbClient({ workerPath, logger?, callTimeoutMs? = 30000 })`
77
+ - `new DbClient({ workerPath, logger?, callTimeoutMs? = 30000, slowMs? = 500 })`
78
+ - `slowMs`: any invoke whose round trip takes at least this long is
79
+ warn-logged — `Slow DB call (612ms): Ticket.queue` — so slow queries name
80
+ themselves in every app before users complain. The measurement includes
81
+ worker queue wait, so one slow query backing up others is visible as a
82
+ pile-up. Pass `0` to disable.
78
83
  - `init(workerData)` → Promise; rejects on worker boot failure (`init-error`)
79
84
  - `invoke(model, method, ...args)` → Promise. Args/results must be
80
85
  structured-clone serializable. Per-call timeout; on worker crash/exit ALL
@@ -100,6 +105,10 @@ Model methods may be sync or async (thenables are awaited). BigInts in results
100
105
 
101
106
  ## Changelog
102
107
 
108
+ - **0.2.0** — slow-call detection: `DbClient` warn-logs any invoke at or over
109
+ `slowMs` (default 500ms, 0 disables) with duration + `Model.method`. Turns
110
+ performance work measurement-driven — add the index when a real query names
111
+ itself, not speculatively.
103
112
  - **0.1.0** — first release. Extracted from Support101/Acc101
104
113
  `lib/{database,db-client,db-worker}.js`. Changes vs the app originals: model
105
114
  registry + boot moved to the app's `init()` callback; migrations dir is an
package/client.js CHANGED
@@ -20,12 +20,18 @@ class DbClient {
20
20
  * @param {string} opts.workerPath - Absolute path to the app's worker entry file
21
21
  * @param {Object} [opts.logger=console] - Logger with .info/.warn/.error
22
22
  * @param {number} [opts.callTimeoutMs=30000] - Per-invoke timeout
23
+ * @param {number} [opts.slowMs=500] - Warn-log any invoke whose round trip
24
+ * takes at least this long ("Slow DB call (612ms): Ticket.queue"), so slow
25
+ * queries name themselves before users complain. Includes worker queue
26
+ * wait — if one slow query backs up others, the pile-up is visible too.
27
+ * Pass 0 to disable.
23
28
  */
24
- constructor({ workerPath, logger, callTimeoutMs } = {}) {
29
+ constructor({ workerPath, logger, callTimeoutMs, slowMs } = {}) {
25
30
  if (!workerPath) throw new Error('DbClient: workerPath is required');
26
31
  this._workerPath = workerPath;
27
32
  this._logger = logger || console;
28
33
  this._callTimeoutMs = callTimeoutMs || 30 * 1000;
34
+ this._slowMs = slowMs === undefined ? 500 : slowMs;
29
35
  this._worker = null;
30
36
  this._pending = new Map();
31
37
  this._nextId = 0;
@@ -64,6 +70,12 @@ class DbClient {
64
70
  if (!pending) return;
65
71
  this._pending.delete(msg.id);
66
72
  if (pending.timer) clearTimeout(pending.timer);
73
+ if (this._slowMs) {
74
+ const elapsed = Date.now() - pending.started;
75
+ if (elapsed >= this._slowMs) {
76
+ this._logger.warn(`Slow DB call (${elapsed}ms): ${pending.label}`);
77
+ }
78
+ }
67
79
  if (msg.error) {
68
80
  const err = new Error(msg.error.message);
69
81
  err.stack = msg.error.stack;
@@ -132,7 +144,7 @@ class DbClient {
132
144
  reject(new Error(`DB call timed out after ${this._callTimeoutMs}ms: ${model}.${method}`));
133
145
  }
134
146
  }, this._callTimeoutMs);
135
- this._pending.set(id, { resolve, reject, timer });
147
+ this._pending.set(id, { resolve, reject, timer, started: Date.now(), label: `${model}.${method}` });
136
148
  this._worker.postMessage({ id, model, method, args });
137
149
  });
138
150
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aria-framework/db-worker",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Aria App Framework — db-worker module. Async RPC bridge over synchronous better-sqlite3: a worker-thread dispatcher (runWorker), a main-thread client with per-call timeouts and crash handling (DbClient), and a SQLite layer with WAL pragmas + a numbered-migration runner (database).",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,