@voltro/sql-mssql 0.85.0 → 0.86.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.
@@ -1,11 +1,11 @@
1
1
  # Third-Party Notices — @voltro/sql-mssql
2
2
 
3
- This package depends on the open-source software listed below. Each is the
3
+ This package uses the open-source software listed below. Each is the
4
4
  property of its respective copyright holders and is used under the terms of
5
5
  its license. This file is provided for attribution; it grants no rights in
6
6
  @voltro/sql-mssql itself, which is proprietary (see LICENSE).
7
7
 
8
- Generated from the resolved runtime dependency closure (73 packages).
8
+ Generated from the resolved runtime dependency closure (72 packages).
9
9
 
10
10
  ---
11
11
 
@@ -1392,34 +1392,6 @@ FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TOR
1392
1392
  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1393
1393
  ```
1394
1394
 
1395
- ## layerr@3.0.0
1396
-
1397
- License: MIT
1398
-
1399
- ```
1400
- MIT License
1401
-
1402
- Copyright (c) 2020 Perry Mitchell
1403
-
1404
- Permission is hereby granted, free of charge, to any person obtaining a copy
1405
- of this software and associated documentation files (the "Software"), to deal
1406
- in the Software without restriction, including without limitation the rights
1407
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1408
- copies of the Software, and to permit persons to whom the Software is
1409
- furnished to do so, subject to the following conditions:
1410
-
1411
- The above copyright notice and this permission notice shall be included in all
1412
- copies or substantial portions of the Software.
1413
-
1414
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1415
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1416
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1417
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1418
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1419
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1420
- SOFTWARE.
1421
- ```
1422
-
1423
1395
  ## lodash.includes@4.3.0
1424
1396
 
1425
1397
  License: MIT
@@ -2312,14 +2284,14 @@ Apache License
2312
2284
  limitations under the License.
2313
2285
  ```
2314
2286
 
2315
- ## ulidx@2.4.1
2287
+ ## ulid@3.0.2
2316
2288
 
2317
2289
  License: MIT
2318
2290
 
2319
2291
  ```
2320
- MIT License
2292
+ The MIT License (MIT)
2321
2293
 
2322
- Copyright (c) 2021 Perry Mitchell
2294
+ Copyright (c) 2017 Alizain Feerasta
2323
2295
 
2324
2296
  Permission is hereby granted, free of charge, to any person obtaining a copy
2325
2297
  of this software and associated documentation files (the "Software"), to deal
@@ -0,0 +1,9 @@
1
+ import { TYPES as e } from "tedious";
2
+ //#region src/binaryParameter.ts
3
+ var t = {
4
+ ...e.VarBinary,
5
+ resolveLength: (t) => Math.max(1, e.VarBinary.resolveLength(t)),
6
+ validate: (t, n, r) => e.VarBinary.validate(t instanceof Uint8Array && !Buffer.isBuffer(t) ? Buffer.from(t.buffer, t.byteOffset, t.byteLength) : t, n, r)
7
+ };
8
+ //#endregion
9
+ export { t as binaryParameter };
@@ -0,0 +1 @@
1
+ export { CDC_OFFSETS_TABLE, _voltroMssqlCdcOffsetsTable } from "@voltro/database";
@@ -0,0 +1,111 @@
1
+ import { mssqlRowResultProjection as e, readMssqlPhysicalColumns as t } from "./rowResultProjection.js";
2
+ import { Effect as n } from "effect";
3
+ import { decodeRowsFromSchema as r } from "@voltro/database";
4
+ import { createLogger as i } from "@voltro/logger";
5
+ import { SqlClient as a } from "effect/sql/SqlClient";
6
+ //#region src/changeTrackingCdc.ts
7
+ var o = {
8
+ I: "insert",
9
+ U: "update",
10
+ D: "delete"
11
+ }, s = (e, t) => e(n.gen(function* () {
12
+ let e = yield* a;
13
+ if (((yield* e`
14
+ SELECT COUNT(*) AS ${e("on")}
15
+ FROM sys.change_tracking_databases
16
+ WHERE database_id = DB_ID()`)[0]?.on ?? 0) === 0) return yield* n.fail(/* @__PURE__ */ Error("mssql Change Tracking CDC requires CT enabled on the database. Run `ALTER DATABASE <db> SET CHANGE_TRACKING = ON (CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON)`, or set CDC=0 for inline emit."));
17
+ let r = [];
18
+ for (let n of t) ((yield* e`
19
+ SELECT CASE WHEN OBJECT_ID(${n}, 'U') IS NULL THEN 0 ELSE 1 END AS ${e("present")}`)[0]?.present ?? 0) === 0 ? r.push(n) : yield* e.unsafe(`IF NOT EXISTS (
20
+ SELECT 1 FROM sys.change_tracking_tables
21
+ WHERE object_id = OBJECT_ID(${c(n)})
22
+ )
23
+ ALTER TABLE ${l(n)} ENABLE CHANGE_TRACKING WITH (TRACK_COLUMNS_UPDATED = OFF)`);
24
+ return r;
25
+ })), c = (e) => `'${e.replace(/'/g, "''")}'`, l = (e) => `[${e.replace(/\]/g, "]]")}]`, u = async (u) => {
26
+ let d = i({ scope: "voltro:mssql:cdc" }), f = u.pollIntervalMs ?? 500, p = await s(u.run, u.tables), m = /* @__PURE__ */ new Set(), h = async () => (await u.run(n.gen(function* () {
27
+ return yield* (yield* a)`SELECT CONVERT(VARCHAR(40), CHANGE_TRACKING_CURRENT_VERSION()) AS v`;
28
+ })))[0]?.v ?? "0", g = u.startVersion ?? await h(), _ = !1, v = null, y = !1, b = (e, t) => BigInt(e) > BigInt(t), x = async (i, s) => {
29
+ let f = await u.run(n.gen(function* () {
30
+ let n = yield* a, r = n.unsafe(l(i)), o = (yield* n`
31
+ SELECT CONVERT(VARCHAR(40), CHANGE_TRACKING_MIN_VALID_VERSION(OBJECT_ID(${n.unsafe(c(i))}))) AS floor`)[0]?.floor, u = s;
32
+ if (o != null && BigInt(s) < BigInt(o)) {
33
+ if (!m.has(i)) return {
34
+ resync: !0,
35
+ rows: []
36
+ };
37
+ u = o;
38
+ }
39
+ let d = yield* t(n, i);
40
+ return {
41
+ resync: !1,
42
+ rows: yield* n`
43
+ SELECT ct.SYS_CHANGE_OPERATION AS __ct_op, ct.id AS __ct_id, ${e(n, d, "t")}
44
+ FROM CHANGETABLE(CHANGES ${r}, ${BigInt(u)}) AS ct
45
+ LEFT JOIN ${r} AS t ON t.id = ct.id
46
+ ORDER BY ct.SYS_CHANGE_VERSION`.unprepared
47
+ };
48
+ }));
49
+ if (f.resync) d.warn("cdc: CT retention floor passed our cursor — resyncing to current version", { table: i }), g = await h(), u.onResync?.(), u.onVersion?.(g);
50
+ else for (let e of f.rows) {
51
+ let t = o[e.__ct_op];
52
+ if (!t) continue;
53
+ let { __ct_op: n, __ct_id: a, ...s } = e;
54
+ if (t === "delete") u.onChange({
55
+ table: i,
56
+ op: "delete",
57
+ old: { id: a },
58
+ new: null,
59
+ netChange: !0
60
+ });
61
+ else {
62
+ let e = r([s], i, "mssql")[0];
63
+ u.onChange({
64
+ table: i,
65
+ op: t,
66
+ old: null,
67
+ new: e,
68
+ netChange: !0
69
+ });
70
+ }
71
+ }
72
+ }, S = async () => {
73
+ if (!(_ || y)) {
74
+ y = !0;
75
+ try {
76
+ if (p.length > 0) {
77
+ let e = p;
78
+ p = await s(u.run, e);
79
+ for (let t of e) p.includes(t) || m.add(t);
80
+ }
81
+ let e = await h();
82
+ if (b(e, g)) {
83
+ for (let e of u.tables) {
84
+ if (_) break;
85
+ p.includes(e) || (await x(e, g), m.delete(e));
86
+ }
87
+ g = b(e, g) ? e : g, u.onVersion?.(g);
88
+ }
89
+ } catch (e) {
90
+ _ || (d.warn("cdc: poll cycle failed — retrying next tick", {}, e), u.onError?.(e));
91
+ } finally {
92
+ y = !1, _ || (v = setTimeout(() => {
93
+ S();
94
+ }, f));
95
+ }
96
+ }
97
+ };
98
+ return v = setTimeout(() => {
99
+ S();
100
+ }, f), v.unref && v.unref(), d.info("cdc: Change Tracking reader attached", {
101
+ tables: u.tables.length,
102
+ from: u.startVersion ?? g
103
+ }), {
104
+ stop: async () => {
105
+ _ = !0, v &&= (clearTimeout(v), null);
106
+ },
107
+ currentVersion: () => g
108
+ };
109
+ };
110
+ //#endregion
111
+ export { s as ensureChangeTracking, u as startChangeTrackingCdc };