masterrecord 1.5.16 → 1.5.17

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/CHANGES.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # MasterRecord Changelog
2
2
 
3
+ ## v1.5.17 — connections stay warm across close/reopen (ADO.NET-style pooling; no reconnect churn)
4
+
5
+ `close()` physically closed a pooled connection the moment its refcount reached zero. So a request-scoped context (or a background job that builds a scope per run and disposes it) reconnected on every cycle whenever it was the sole holder — the exact churn that scoping was supposed to avoid.
6
+
7
+ EF/ADO.NET doesn't do this: disposing a `DbContext` returns its connection to the pool **kept physically open and idle**, and the next context reuses it warm. MasterRecord's `_pools` now behaves the same:
8
+
9
+ - At refcount zero the connection is **kept open and idle** (not closed). The next context that needs the same database reuses the warm connection with no reconnect. Verified: `close()` then `new Ctx()` yields the *same* engine instance.
10
+ - A background **reaper** physically closes connections idle longer than `MR_POOL_IDLE_MS` (default `60000`), so idle connections are reclaimed, not leaked. The reaper timer is `unref()`-ed and never keeps the process alive; `closeAll()` stops it.
11
+ - **`MR_POOL_IDLE_MS=0`** opts out — the old behavior (close immediately at refcount zero).
12
+
13
+ This makes per-request contexts and per-run background scopes genuinely cheap: `new AppContext()` allocates a tracking map and points at the already-open connection; `close()` returns it to the pool warm.
14
+
15
+ New tests: `test/pool-idle-retention.test.js` — a closed connection is reused warm (no reconnect); the reaper closes a connection idle past the timeout; `MR_POOL_IDLE_MS=0` closes immediately. Full suite green (0 fail, 315 pass, 20 gated skipped); 0 lint errors.
16
+
3
17
  ## v1.5.16 — context pooling (EF's AddDbContextPool) + reset(); change-tracking/lifetime docs
4
18
 
5
19
  Completes the EF-faithful context-lifetime story so request-scoped contexts are affordable even with many contexts registered.
package/context.js CHANGED
@@ -59,6 +59,37 @@ function findAppRoot(){
59
59
 
60
60
  const _pools = global.__MR_POOLS__ || (global.__MR_POOLS__ = new Map());
61
61
 
62
+ // ADO.NET-style connection pooling (what EF sits on top of): when a pooled
63
+ // connection's refCount reaches zero it is NOT physically closed — it is kept
64
+ // OPEN and IDLE so the next context that needs it reuses a warm connection
65
+ // (this is why EF does not reconnect on every scope/DbContext dispose). A
66
+ // background reaper physically closes connections that have been idle longer
67
+ // than the timeout, so idle connections are eventually reclaimed. Set
68
+ // MR_POOL_IDLE_MS=0 to disable retention (close immediately at refCount 0).
69
+ const _POOL_IDLE_MS = process.env.MR_POOL_IDLE_MS !== undefined && Number(process.env.MR_POOL_IDLE_MS) >= 0
70
+ ? Number(process.env.MR_POOL_IDLE_MS)
71
+ : 60_000;
72
+ let _poolReaper = global.__MR_POOL_REAPER__ || null;
73
+ function _ensurePoolReaper() {
74
+ if (_poolReaper || _POOL_IDLE_MS <= 0) return;
75
+ _poolReaper = setInterval(() => {
76
+ const now = Date.now();
77
+ for (const [key, entry] of _pools) {
78
+ if (entry && !entry.promise && entry.refCount <= 0
79
+ && entry.idleSince != null && (now - entry.idleSince) >= _POOL_IDLE_MS) {
80
+ _pools.delete(key);
81
+ try {
82
+ if (entry.engine && typeof entry.engine.close === 'function') {
83
+ Promise.resolve(entry.engine.close()).catch(() => {});
84
+ }
85
+ } catch (_) { /* best-effort */ }
86
+ }
87
+ }
88
+ }, Math.min(Math.max(_POOL_IDLE_MS, 1000), 30_000));
89
+ if (typeof _poolReaper.unref === 'function') _poolReaper.unref(); // never keep the process alive
90
+ global.__MR_POOL_REAPER__ = _poolReaper;
91
+ }
92
+
62
93
  // Registry of live context instances so saveChanges()/close() can detect entities
63
94
  // with unsaved changes that are tracked by a DIFFERENT context instance — the
64
95
  // classic "saveChanges() succeeded but nothing was written" bug: you mutate an
@@ -2660,9 +2691,21 @@ class context {
2660
2691
  if (entry.engine === this._SQLEngine) {
2661
2692
  entry.refCount--;
2662
2693
  if (entry.refCount <= 0) {
2663
- _pools.delete(key);
2664
- if (this._SQLEngine && typeof this._SQLEngine.close === 'function') {
2665
- await this._SQLEngine.close();
2694
+ // ADO.NET-style: do NOT close the physical connection now.
2695
+ // Keep it OPEN and idle so the next context reuses a warm
2696
+ // connection (as EF does — a scoped DbContext dispose returns
2697
+ // its connection to the pool, it is not disconnected). The
2698
+ // reaper closes it only after it has been idle past the
2699
+ // timeout. (MR_POOL_IDLE_MS=0 disables retention -> close now.)
2700
+ entry.refCount = 0;
2701
+ if (_POOL_IDLE_MS <= 0) {
2702
+ _pools.delete(key);
2703
+ if (this._SQLEngine && typeof this._SQLEngine.close === 'function') {
2704
+ await this._SQLEngine.close();
2705
+ }
2706
+ } else {
2707
+ entry.idleSince = Date.now();
2708
+ _ensurePoolReaper();
2666
2709
  }
2667
2710
  }
2668
2711
  this._SQLEngine = null;
@@ -2713,6 +2756,12 @@ class context {
2713
2756
  }
2714
2757
  }
2715
2758
  _pools.clear();
2759
+ // Stop the idle-connection reaper (nothing left to reap).
2760
+ if (_poolReaper) {
2761
+ clearInterval(_poolReaper);
2762
+ _poolReaper = null;
2763
+ global.__MR_POOL_REAPER__ = null;
2764
+ }
2716
2765
  }
2717
2766
 
2718
2767
  /**
@@ -61,7 +61,9 @@ MasterRecord holds tracked entities with **strong references** for the life of t
61
61
 
62
62
  ### 1. Request-scoped context (recommended)
63
63
 
64
- Create a context per request and `close()` it when the request ends. Connections are pooled underneath, and `close()` releases the whole change tracker. Every `load → mutate → saveChanges` handler works unchanged, with no tracking-behavior tweaks and no way to introduce a lost write.
64
+ Create a context per request and `close()` it when the request ends. Every `load → mutate → saveChanges` handler works unchanged, with no tracking-behavior tweaks and no way to introduce a lost write.
65
+
66
+ `new AppContext()` is cheap — it does **not** open a new connection. Connections are pooled per database (like ADO.NET under EF): `close()` releases the change tracker and returns the connection to the pool **kept open and idle**, so the next context reuses a warm connection with no reconnect. Idle connections are reclaimed by a background reaper after `MR_POOL_IDLE_MS` (default 60000ms; set `0` to close immediately at refcount zero). So per-request contexts — and background jobs that build a scope per run — do not churn connections.
65
67
 
66
68
  ```javascript
67
69
  async function handler(req, res) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "masterrecord",
3
- "version": "1.5.16",
3
+ "version": "1.5.17",
4
4
  "description": "An Object-relational mapping for the Master framework. Master Record connects classes to relational database tables to establish a database with almost zero-configuration ",
5
5
  "type": "module",
6
6
  "main": "./MasterRecord.js",