@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.
- package/CHANGELOG.md +666 -0
- package/THIRD-PARTY-NOTICES.md +5 -33
- package/dist/binaryParameter.js +9 -0
- package/dist/cdcOffsetsTable.js +1 -0
- package/dist/changeTrackingCdc.js +111 -0
- package/dist/index.js +12 -820
- package/dist/numberParameter.js +19 -0
- package/dist/replicationAdapter.js +35 -0
- package/dist/retry.js +20 -0
- package/dist/rowResultProjection.js +16 -0
- package/dist/sqlLayer.js +78 -0
- package/dist/store.js +553 -0
- package/dist/writeOutput.js +18 -0
- package/package.json +3 -3
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { TYPES as e } from "tedious";
|
|
2
|
+
import { resolveParameter as t } from "tedious/lib/data-type";
|
|
3
|
+
//#region src/numberParameter.ts
|
|
4
|
+
var n = (t) => typeof t == "bigint" ? e.BigInt : typeof t == "number" && Number.isSafeInteger(t) ? t >= -2147483648 && t <= 2147483647 ? e.Int : e.BigInt : e.Float, r = {
|
|
5
|
+
...e.Int,
|
|
6
|
+
declaration: (e) => n(e.value).declaration(e),
|
|
7
|
+
resolve: (e, r, i) => t({
|
|
8
|
+
...e,
|
|
9
|
+
type: n(e.value)
|
|
10
|
+
}, r, i).data,
|
|
11
|
+
writeTypeInfo: (e, t, r) => n(t.value).writeTypeInfo(e, t, r),
|
|
12
|
+
writeValue: (e, t, r) => n(t.value).writeValue(e, t, r),
|
|
13
|
+
validate: (e, t, r) => {
|
|
14
|
+
if (typeof e == "number" && !Number.isFinite(e)) throw TypeError("SQL Server numeric parameters must be finite.");
|
|
15
|
+
return n(e).validate(e, t, r);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
//#endregion
|
|
19
|
+
export { r as numberParameter };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Effect as e } from "effect";
|
|
2
|
+
import { SqlClient as t } from "effect/sql/SqlClient";
|
|
3
|
+
//#region src/replicationAdapter.ts
|
|
4
|
+
var n = (e) => e.__mssqlReplicationFriend ?? null, r = (e, t) => e === t ? 0 : e < t ? -1 : 1, i = () => ({
|
|
5
|
+
async capturePrimaryPosition(r) {
|
|
6
|
+
let i = n(r);
|
|
7
|
+
if (i === null) throw Error("mssqlReplicationAdapter: primary is not an MssqlStore (missing __mssqlReplicationFriend).");
|
|
8
|
+
return i.runEffect(e.gen(function* () {
|
|
9
|
+
return (yield* (yield* t)`
|
|
10
|
+
SELECT CONVERT(VARCHAR(40), end_of_log_lsn) AS lsn
|
|
11
|
+
FROM sys.dm_hadr_database_replica_states
|
|
12
|
+
WHERE database_id = DB_ID()
|
|
13
|
+
AND is_local = 1
|
|
14
|
+
AND is_primary_replica = 1
|
|
15
|
+
`)[0]?.lsn ?? "00000000:00000000:0000";
|
|
16
|
+
}));
|
|
17
|
+
},
|
|
18
|
+
async probeReplicaPosition(r) {
|
|
19
|
+
let i = n(r);
|
|
20
|
+
if (i === null) throw Error("mssqlReplicationAdapter: replica is not an MssqlStore.");
|
|
21
|
+
return i.runEffect(e.gen(function* () {
|
|
22
|
+
return (yield* (yield* t)`
|
|
23
|
+
SELECT CONVERT(VARCHAR(40), last_hardened_lsn) AS lsn
|
|
24
|
+
FROM sys.dm_hadr_database_replica_states
|
|
25
|
+
WHERE database_id = DB_ID()
|
|
26
|
+
AND is_local = 1
|
|
27
|
+
`)[0]?.lsn ?? "00000000:00000000:0000";
|
|
28
|
+
}));
|
|
29
|
+
},
|
|
30
|
+
compare(e, t) {
|
|
31
|
+
return r(t, e) >= 0 ? "caught-up" : "behind";
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
//#endregion
|
|
35
|
+
export { i as mssqlReplicationAdapter };
|
package/dist/retry.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { MSSQL_TRANSIENT_NUMBERS as e, causeBranches as t } from "@voltro/database";
|
|
2
|
+
//#region src/retry.ts
|
|
3
|
+
var n = e, r = (e) => {
|
|
4
|
+
let t = e;
|
|
5
|
+
for (let e = 0; e < 5 && typeof t == "object" && t; e++) {
|
|
6
|
+
let e = t.number;
|
|
7
|
+
if (typeof e == "number") return String(e);
|
|
8
|
+
let n = t.code;
|
|
9
|
+
if (typeof n == "string") return n;
|
|
10
|
+
t = t.cause;
|
|
11
|
+
}
|
|
12
|
+
}, i = (e) => t(e).some((e) => {
|
|
13
|
+
let t = r(e);
|
|
14
|
+
return t !== void 0 && n.has(t);
|
|
15
|
+
}), a = /* @__PURE__ */ new Set(["2601", "2627"]), o = (e) => t(e).some((e) => {
|
|
16
|
+
let t = r(e);
|
|
17
|
+
return t !== void 0 && a.has(t);
|
|
18
|
+
}), s = (e) => i(e) ? "retry" : "noRetry";
|
|
19
|
+
//#endregion
|
|
20
|
+
export { o as isDuplicateKeyMssqlFailure, i as isRetryableMssqlFailure, s as mssqlRetryFilter };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Effect as e } from "effect";
|
|
2
|
+
import { sqlError as t } from "@voltro/database";
|
|
3
|
+
//#region src/rowResultProjection.ts
|
|
4
|
+
var n = (n, r) => e.gen(function* () {
|
|
5
|
+
let e = yield* n`
|
|
6
|
+
SELECT name, system_type_id FROM sys.columns
|
|
7
|
+
WHERE object_id = OBJECT_ID(${n`${n(r)}`.compile()[0]}, 'U')
|
|
8
|
+
ORDER BY column_id
|
|
9
|
+
`.withoutTransform;
|
|
10
|
+
return e.length === 0 ? yield* t(`Cannot inspect result columns for ${r}`) : e;
|
|
11
|
+
}), r = (e, t, n) => e.join(", ", !1)(t.map((t) => {
|
|
12
|
+
let r = e.literal(`[${t.name.replaceAll("]", "]]")}]`), i = n ? e`${e(n)}.${r}` : r;
|
|
13
|
+
return t.system_type_id === 106 || t.system_type_id === 108 ? e`CONVERT(varchar(80), ${i}) AS ${r}` : i;
|
|
14
|
+
}));
|
|
15
|
+
//#endregion
|
|
16
|
+
export { r as mssqlRowResultProjection, n as readMssqlPhysicalColumns };
|
package/dist/sqlLayer.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { numberParameter as e } from "./numberParameter.js";
|
|
2
|
+
import { binaryParameter as t } from "./binaryParameter.js";
|
|
3
|
+
import { MssqlClient as n } from "@effect/sql-mssql";
|
|
4
|
+
import { TYPES as r } from "tedious";
|
|
5
|
+
import { Config as i, Duration as a, Redacted as o } from "effect";
|
|
6
|
+
import { DEFAULT_ACQUIRE_TIMEOUT_MS as s, SHARED_TRANSACTION_SERVICE as c } from "@voltro/database";
|
|
7
|
+
//#region src/sqlLayer.ts
|
|
8
|
+
var l = {
|
|
9
|
+
...n.defaultParameterTypes,
|
|
10
|
+
null: r.NVarChar,
|
|
11
|
+
Date: r.DateTime2,
|
|
12
|
+
string: r.NVarChar,
|
|
13
|
+
number: e,
|
|
14
|
+
Uint8Array: t
|
|
15
|
+
}, u = (e) => {
|
|
16
|
+
let t = e.acquireTimeoutMs ?? s;
|
|
17
|
+
return t > 0 ? t : void 0;
|
|
18
|
+
}, d = (e) => {
|
|
19
|
+
let t = u(e);
|
|
20
|
+
return n.layerConfig({
|
|
21
|
+
transactionService: i.succeed(c),
|
|
22
|
+
server: i.succeed(e.server),
|
|
23
|
+
parameterTypes: i.succeed(l),
|
|
24
|
+
...t === void 0 ? {} : { connectTimeout: i.succeed(a.millis(t)) },
|
|
25
|
+
...e.port === void 0 ? {} : { port: i.succeed(e.port) },
|
|
26
|
+
...e.database === void 0 ? {} : { database: i.succeed(e.database) },
|
|
27
|
+
...e.username === void 0 ? {} : { username: i.succeed(e.username) },
|
|
28
|
+
...e.password === void 0 ? {} : { password: i.succeed(o.make(e.password)) },
|
|
29
|
+
encrypt: i.succeed(e.encrypt ?? !1),
|
|
30
|
+
...e.trustServer === void 0 ? {} : { trustServer: i.succeed(e.trustServer) },
|
|
31
|
+
...e.maxConnections === void 0 ? {} : { maxConnections: i.succeed(e.maxConnections) }
|
|
32
|
+
});
|
|
33
|
+
}, f = (e) => {
|
|
34
|
+
let t = e.get("sslmode");
|
|
35
|
+
if (t !== null) {
|
|
36
|
+
if (t === "require") return !0;
|
|
37
|
+
if (t === "disable") return !1;
|
|
38
|
+
throw Error(`DB_URL '?sslmode=${t}' is not supported by the mssql dialect — use 'require' (TLS without certificate verification) or 'disable' (plaintext).`);
|
|
39
|
+
}
|
|
40
|
+
for (let t of ["ssl", "encrypt"]) {
|
|
41
|
+
let n = e.get(t);
|
|
42
|
+
if (n !== null) {
|
|
43
|
+
if (n === "true" || n === "1") return !0;
|
|
44
|
+
if (n === "false" || n === "0") return !1;
|
|
45
|
+
throw Error(`DB_URL '?${t}=${n}' is not supported by the mssql dialect — use 'true'/'1' or 'false'/'0'.`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}, p = (e) => {
|
|
49
|
+
let t = e.acquireTimeoutMs === void 0 ? {} : { acquireTimeoutMs: e.acquireTimeoutMs }, n = (e) => e === void 0 ? {} : {
|
|
50
|
+
encrypt: e,
|
|
51
|
+
...e ? { trustServer: !0 } : {}
|
|
52
|
+
};
|
|
53
|
+
if (e.url) {
|
|
54
|
+
let r = new URL(e.url);
|
|
55
|
+
return {
|
|
56
|
+
server: r.hostname || "localhost",
|
|
57
|
+
port: r.port ? Number(r.port) : 1433,
|
|
58
|
+
username: decodeURIComponent(r.username || "sa"),
|
|
59
|
+
password: decodeURIComponent(r.password || ""),
|
|
60
|
+
database: r.pathname.replace(/^\//, "") || "master",
|
|
61
|
+
...n(e.ssl ?? f(r.searchParams)),
|
|
62
|
+
...e.maxConnections === void 0 ? {} : { maxConnections: e.maxConnections },
|
|
63
|
+
...t
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
server: e.host ?? "localhost",
|
|
68
|
+
port: e.port ?? 1433,
|
|
69
|
+
username: e.username ?? "sa",
|
|
70
|
+
password: e.password ?? "",
|
|
71
|
+
database: e.database ?? "master",
|
|
72
|
+
...n(e.ssl),
|
|
73
|
+
...e.maxConnections === void 0 ? {} : { maxConnections: e.maxConnections },
|
|
74
|
+
...t
|
|
75
|
+
};
|
|
76
|
+
}, m = (e) => d(p(e));
|
|
77
|
+
//#endregion
|
|
78
|
+
export { n as MssqlClient, p as connectionFromConfig, d as makeMssqlSqlLayer, m as makeMssqlSqlLayerFromConfig, u as resolveAcquireTimeoutMs, f as sslFromUrlQuery };
|