@aiquants/mssql 0.1.3 → 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.
- package/README.md +45 -4
- package/dist/drizzle.d.mts +5 -0
- package/dist/drizzle.d.ts +5 -0
- package/dist/drizzle.js +1 -0
- package/dist/drizzle.mjs +1 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/json-list-HgwvRzAE.d.mts +3 -0
- package/dist/json-list-HgwvRzAE.d.ts +3 -0
- package/package.json +21 -3
package/README.md
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
# @aiquants/mssql
|
|
2
2
|
|
|
3
|
-
What SQL Server refuses, and how to stay inside it. Domain-free
|
|
3
|
+
What SQL Server refuses, and how to stay inside it. Domain-free; the main entry is dependency-free.
|
|
4
4
|
|
|
5
|
-
SQL Server
|
|
5
|
+
SQL Server が**拒むもの**と、その内側に留まる方法。ドメインに縛られず、本体の入口は依存を持ちません。
|
|
6
6
|
|
|
7
7
|
| 何を答えるか | 入口 |
|
|
8
8
|
| :--- | :--- |
|
|
9
9
|
| 失敗した作業単位を、もう一度実行してよいか | `runWithTransactionRetry` / `isDeadlockVictim` / `isRolledBackByServer` |
|
|
10
10
|
| 失敗が、SQL Server のどの番号の失敗か (包み直しの奥まで) | `sqlErrorOf` (`SqlServerMessage`) |
|
|
11
11
|
| 1 文に何件まで載せられるか | `chunkByParameters` / `chunkSizeForParameters` / `chunk` |
|
|
12
|
+
| 長さの決まらない並びを、1 つのパラメータで `IN` に渡すには | `inJsonList` / `notInJsonList` (`@aiquants/mssql/drizzle`) / `jsonListText` |
|
|
12
13
|
| 返ってきた日時は、どの瞬間か | `toInstant` |
|
|
13
14
|
|
|
14
15
|
いずれも**ドライバが押し付けてくる事実**であり、呼び出し側のドメインには属しません。写しを各所に
|
|
@@ -92,8 +93,11 @@ await runWithTransactionRetry(
|
|
|
92
93
|
pnpm add @aiquants/mssql
|
|
93
94
|
```
|
|
94
95
|
|
|
95
|
-
|
|
96
|
-
|
|
96
|
+
本体の入口 (`@aiquants/mssql`) は依存を持ちません。`mssql` / `tedious` / ORM のいずれにも `import` を持たず、
|
|
97
|
+
ドライバのエラーが一様に載せる `code`・`number`・`message` と、原因の連なり (`cause`) だけを見ます (duck typing)。
|
|
98
|
+
|
|
99
|
+
`@aiquants/mssql/drizzle` だけが drizzle-orm を `import` します。drizzle-orm は任意の peer
|
|
100
|
+
(`>=1.0.0-beta.4 <2.0.0`) なので、この入口を使う時だけ入れてください。
|
|
97
101
|
|
|
98
102
|
## Contract
|
|
99
103
|
|
|
@@ -195,6 +199,43 @@ const size = Math.min(MAX_IN_LIST, chunkSizeForParameters(1, RESERVED_PARAMETERS
|
|
|
195
199
|
> 変わってもそこだけ追従しません。引き算は `chunkSizeForParameters` に任せ、文ごとの事情は
|
|
196
200
|
> `reservedParameters` として渡してください。
|
|
197
201
|
|
|
202
|
+
### 長さの決まらない `IN` の並び
|
|
203
|
+
|
|
204
|
+
`inArray(column, ids)` は値ごとにパラメータを 1 つ束ねます。並びの長さがデータと共に伸びる所では、
|
|
205
|
+
件数が上限を跨いだ日に**実行される前に**拒否されます。`IN` / `NOT IN` の並びは、長さを問わず JSON の
|
|
206
|
+
配列 1 つにして `OPENJSON` で読み返してください。パラメータは常に 1 つです。
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
import { inJsonList, notInJsonList } from "@aiquants/mssql/drizzle"
|
|
210
|
+
|
|
211
|
+
// [documents].[id] in (select [value] from openjson(@par0))
|
|
212
|
+
const rows = await db.select().from(documents).where(inJsonList(documents.id, ids))
|
|
213
|
+
|
|
214
|
+
// [documents].[status] not in (select [value] from openjson(@par0))
|
|
215
|
+
const open = await db.select().from(documents).where(notInJsonList(documents.status, closedStatuses))
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
| 入口 | 描く SQL・返すもの | 依存 |
|
|
219
|
+
| :--- | :--- | :--- |
|
|
220
|
+
| `inJsonList(column, values)` | `<column> in (select [value] from openjson(@p))` | drizzle-orm (任意の peer) |
|
|
221
|
+
| `notInJsonList(column, values)` | `<column> not in (select [value] from openjson(@p))` | drizzle-orm (任意の peer) |
|
|
222
|
+
| `jsonListText(values)` | `@p` に束ねる JSON の配列の文字 (ORM を通さない文で使う) | なし |
|
|
223
|
+
|
|
224
|
+
- 値は字・有限の数・真偽です。null・undefined・`NaN`・`Infinity`・配列の空き・ほかの型は `TypeError` で
|
|
225
|
+
拒みます。`JSON.stringify` はこれらを黙って null に書き、`NOT IN` の並びの null はどの行も選ばなく
|
|
226
|
+
するからです。
|
|
227
|
+
- 値は渡された順のまま書き、並べ替えも重なりの除去もしません。空の並びの `IN` は、どの行も選びません。
|
|
228
|
+
- 列が null の行は、並びが空でなければ `NOT IN` でも選ばれません (`NOT IN` の三値論理)。
|
|
229
|
+
- `[value]` は既定の照合順序の `nvarchar(max)` で、比べはデータ型の優先順位に従います。数・日付・
|
|
230
|
+
`uniqueidentifier` の列では `[value]` の側が列の型に変わり、索引の seek を保ちます。
|
|
231
|
+
- 値から索引で辿れない列 (索引の無い列・`OR` で繋いだ列) では、SQL Server は外側の行ごとに配列を読み
|
|
232
|
+
直します。費用は「並びの長さ × 外側の行数」で伸びるので、長い並びでは実行計画を確かめてください。
|
|
233
|
+
|
|
234
|
+
`chunk` / `chunkByParameters` は、次の 2 つに使い続けます。
|
|
235
|
+
|
|
236
|
+
- 1 行に複数の値を束ねる `VALUES` (`insert ... values`)。
|
|
237
|
+
- 1 文で触れる行の数を抑えたい書き込み (錠の昇格を避けるため)。
|
|
238
|
+
|
|
198
239
|
---
|
|
199
240
|
|
|
200
241
|
## 日時の読み取り
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { SQLWrapper, SQL } from "drizzle-orm";
|
|
2
|
+
import { J as JsonListValue } from "./json-list-HgwvRzAE.mjs";
|
|
3
|
+
declare function inJsonList(column: SQLWrapper, values: readonly JsonListValue[]): SQL;
|
|
4
|
+
declare function notInJsonList(column: SQLWrapper, values: readonly JsonListValue[]): SQL;
|
|
5
|
+
export { inJsonList, notInJsonList };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { SQLWrapper, SQL } from "drizzle-orm";
|
|
2
|
+
import { J as JsonListValue } from "./json-list-HgwvRzAE.js";
|
|
3
|
+
declare function inJsonList(column: SQLWrapper, values: readonly JsonListValue[]): SQL;
|
|
4
|
+
declare function notInJsonList(column: SQLWrapper, values: readonly JsonListValue[]): SQL;
|
|
5
|
+
export { inJsonList, notInJsonList };
|
package/dist/drizzle.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var i=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var a=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var l=(n,e)=>{for(var t in e)i(n,t,{get:e[t],enumerable:!0})},L=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of a(e))!f.call(n,o)&&o!==t&&i(n,o,{get:()=>e[o],enumerable:!(r=p(e,o))||r.enumerable});return n};var y=n=>L(i({},"__esModule",{value:!0}),n);var d={};l(d,{inJsonList:()=>b,notInJsonList:()=>c});module.exports=y(d);var u=require("drizzle-orm");function s(n){for(let[e,t]of n.entries())if(!(typeof t=="string"||typeof t=="boolean"||typeof t=="number"&&Number.isFinite(t)))throw new TypeError(`[@aiquants/mssql] jsonListText: values[${e}] must be a string, a finite number or a boolean, received ${m(t)}`);return JSON.stringify(n)}function m(n){return n===null?"null":typeof n=="number"?String(n):typeof n}function b(n,e){return u.sql`${n} in (select [value] from openjson(${s(e)}))`}function c(n,e){return u.sql`${n} not in (select [value] from openjson(${s(e)}))`}0&&(module.exports={inJsonList,notInJsonList});
|
package/dist/drizzle.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{sql as r}from"drizzle-orm";function o(n){for(let[e,t]of n.entries())if(!(typeof t=="string"||typeof t=="boolean"||typeof t=="number"&&Number.isFinite(t)))throw new TypeError(`[@aiquants/mssql] jsonListText: values[${e}] must be a string, a finite number or a boolean, received ${i(t)}`);return JSON.stringify(n)}function i(n){return n===null?"null":typeof n=="number"?String(n):typeof n}function f(n,e){return r`${n} in (select [value] from openjson(${o(e)}))`}function l(n,e){return r`${n} not in (select [value] from openjson(${o(e)}))`}export{f as inJsonList,l as notInJsonList};
|
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var c=Object.defineProperty;var
|
|
1
|
+
"use strict";var c=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var R=(e,n)=>{for(var t in n)c(e,t,{get:n[t],enumerable:!0})},w=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of b(n))!A.call(e,o)&&o!==t&&c(e,o,{get:()=>n[o],enumerable:!(r=E(n,o))||r.enumerable});return e};var S=e=>w(c({},"__esModule",{value:!0}),e);var L={};R(L,{DEFAULT_TRANSACTION_ATTEMPTS:()=>B,MAX_IN_LIST:()=>O,MAX_PARAMETERS:()=>q,RESERVED_PARAMETERS:()=>P,chunk:()=>l,chunkByParameters:()=>h,chunkSizeForParameters:()=>f,isDeadlockVictim:()=>m,isRolledBackByServer:()=>y,jsonListText:()=>g,runWithTransactionRetry:()=>D,sqlErrorOf:()=>p,toInstant:()=>C});module.exports=S(L);function g(e){for(let[n,t]of e.entries())if(!(typeof t=="string"||typeof t=="boolean"||typeof t=="number"&&Number.isFinite(t)))throw new TypeError(`[@aiquants/mssql] jsonListText: values[${n}] must be a string, a finite number or a boolean, received ${_(t)}`);return JSON.stringify(e)}function _(e){return e===null?"null":typeof e=="number"?String(e):typeof e}var q=2100,P=100,O=500;function l(e,n=500){if(!Number.isInteger(n)||n<=0)throw new TypeError(`[@aiquants/mssql] chunk size must be a positive integer, received ${String(n)}`);let t=[];for(let r=0;r<e.length;r+=n)t.push(e.slice(r,r+n));return t}function h(e,n,t=100){return l(e,f(n,t))}function f(e,n=100){if(!Number.isInteger(e)||e<=0)throw new TypeError(`[@aiquants/mssql] parametersPerItem must be a positive integer, received ${String(e)}`);if(!Number.isInteger(n)||n<0)throw new TypeError(`[@aiquants/mssql] reservedParameters must be a non-negative integer, received ${String(n)}`);let t=2100-n,r=Math.floor(t/e);if(r<1)throw new TypeError(`[@aiquants/mssql] a single item binds ${e} parameters, which exceeds the ${t} available in one statement`);return r}var B=3;var k="EABORT";function*d(e){let n=e;for(let t=0;t<5&&typeof n=="object"&&n!==null;t+=1){let r=n;yield r,n=r.cause}}function p(e,n){for(let t of d(e)){let{number:r,message:o}=t;if(typeof r=="number"&&Number.isInteger(r)&&r>0&&typeof o=="string"&&n.includes(r))return{number:r,message:o}}return null}function m(e){return p(e,[1205])!==null}function y(e){for(let n of d(e))if(n.code===k)return!0;return!1}var x=globalThis.setTimeout;function M(e){let t=50*2**(e-1)*(.75+Math.random()*.5);return new Promise(r=>x(r,Math.round(t)))}async function D(e,n){let t=n?.attempts??3,r=n?.delay??M;for(let o=1;;o+=1){let i,u=!1,T=async s=>{try{return await s()}catch(a){throw i=a,u=!0,a}};try{return await e(T)}catch(s){let a=u?i:void 0;if(o<t&&m(u?i:s)){n?.onRetry?.({attempt:o,attempts:t,error:s,cause:a}),await r(o);continue}throw u&&y(s)?i:s}}}function C(e){return e instanceof Date?e:new Date(/(?:Z|[+-]\d{2}:?\d{2})$/.test(e)?e:`${e.replace(" ","T")}Z`)}0&&(module.exports={DEFAULT_TRANSACTION_ATTEMPTS,MAX_IN_LIST,MAX_PARAMETERS,RESERVED_PARAMETERS,chunk,chunkByParameters,chunkSizeForParameters,isDeadlockVictim,isRolledBackByServer,jsonListText,runWithTransactionRetry,sqlErrorOf,toInstant});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
function R(e){for(let[n,t]of e.entries())if(!(typeof t=="string"||typeof t=="boolean"||typeof t=="number"&&Number.isFinite(t)))throw new TypeError(`[@aiquants/mssql] jsonListText: values[${n}] must be a string, a finite number or a boolean, received ${f(t)}`);return JSON.stringify(e)}function f(e){return e===null?"null":typeof e=="number"?String(e):typeof e}var S=2100,g=100,_=500;function d(e,n=500){if(!Number.isInteger(n)||n<=0)throw new TypeError(`[@aiquants/mssql] chunk size must be a positive integer, received ${String(n)}`);let t=[];for(let r=0;r<e.length;r+=n)t.push(e.slice(r,r+n));return t}function h(e,n,t=100){return d(e,p(n,t))}function p(e,n=100){if(!Number.isInteger(e)||e<=0)throw new TypeError(`[@aiquants/mssql] parametersPerItem must be a positive integer, received ${String(e)}`);if(!Number.isInteger(n)||n<0)throw new TypeError(`[@aiquants/mssql] reservedParameters must be a non-negative integer, received ${String(n)}`);let t=2100-n,r=Math.floor(t/e);if(r<1)throw new TypeError(`[@aiquants/mssql] a single item binds ${e} parameters, which exceeds the ${t} available in one statement`);return r}var x=3;var m="EABORT";function*c(e){let n=e;for(let t=0;t<5&&typeof n=="object"&&n!==null;t+=1){let r=n;yield r,n=r.cause}}function y(e,n){for(let t of c(e)){let{number:r,message:o}=t;if(typeof r=="number"&&Number.isInteger(r)&&r>0&&typeof o=="string"&&n.includes(r))return{number:r,message:o}}return null}function T(e){return y(e,[1205])!==null}function E(e){for(let n of c(e))if(n.code===m)return!0;return!1}var b=globalThis.setTimeout;function A(e){let t=50*2**(e-1)*(.75+Math.random()*.5);return new Promise(r=>b(r,Math.round(t)))}async function M(e,n){let t=n?.attempts??3,r=n?.delay??A;for(let o=1;;o+=1){let i,u=!1,l=async s=>{try{return await s()}catch(a){throw i=a,u=!0,a}};try{return await e(l)}catch(s){let a=u?i:void 0;if(o<t&&T(u?i:s)){n?.onRetry?.({attempt:o,attempts:t,error:s,cause:a}),await r(o);continue}throw u&&E(s)?i:s}}}function C(e){return e instanceof Date?e:new Date(/(?:Z|[+-]\d{2}:?\d{2})$/.test(e)?e:`${e.replace(" ","T")}Z`)}export{x as DEFAULT_TRANSACTION_ATTEMPTS,_ as MAX_IN_LIST,S as MAX_PARAMETERS,g as RESERVED_PARAMETERS,d as chunk,h as chunkByParameters,p as chunkSizeForParameters,T as isDeadlockVictim,E as isRolledBackByServer,R as jsonListText,M as runWithTransactionRetry,y as sqlErrorOf,C as toInstant};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiquants/mssql",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "What SQL Server refuses, and how to stay inside it: retry a transaction only when it was the deadlock victim (keeping the cause the ORM destroys), read the SQL Server error number through wrapped causes, split statements to fit the 2100-parameter limit, and read offset-less timestamps as UTC.
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "What SQL Server refuses, and how to stay inside it: retry a transaction only when it was the deadlock victim (keeping the cause the ORM destroys), read the SQL Server error number through wrapped causes, split statements to fit the 2100-parameter limit, bind an IN list of any length as one OPENJSON parameter, and read offset-less timestamps as UTC. The main entry has no dependencies; the ./drizzle entry takes drizzle-orm as an optional peer.",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.mjs",
|
|
@@ -11,6 +11,11 @@
|
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"import": "./dist/index.mjs",
|
|
13
13
|
"require": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./drizzle": {
|
|
16
|
+
"types": "./dist/drizzle.d.ts",
|
|
17
|
+
"import": "./dist/drizzle.mjs",
|
|
18
|
+
"require": "./dist/drizzle.js"
|
|
14
19
|
}
|
|
15
20
|
},
|
|
16
21
|
"files": [
|
|
@@ -24,6 +29,8 @@
|
|
|
24
29
|
"transaction",
|
|
25
30
|
"deadlock",
|
|
26
31
|
"retry",
|
|
32
|
+
"openjson",
|
|
33
|
+
"drizzle-orm",
|
|
27
34
|
"typescript"
|
|
28
35
|
],
|
|
29
36
|
"author": {
|
|
@@ -32,11 +39,20 @@
|
|
|
32
39
|
},
|
|
33
40
|
"license": "MIT",
|
|
34
41
|
"devDependencies": {
|
|
42
|
+
"drizzle-orm": "1.0.0-rc.4",
|
|
35
43
|
"rimraf": "^6.1.2",
|
|
36
44
|
"tsup": "^8.5.1",
|
|
37
45
|
"typescript": "^5.9.3",
|
|
38
46
|
"vitest": "^4.1.8"
|
|
39
47
|
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"drizzle-orm": ">=1.0.0-beta.4 <2.0.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependenciesMeta": {
|
|
52
|
+
"drizzle-orm": {
|
|
53
|
+
"optional": true
|
|
54
|
+
}
|
|
55
|
+
},
|
|
40
56
|
"engines": {
|
|
41
57
|
"node": ">=18.0.0",
|
|
42
58
|
"pnpm": ">=8.0.0"
|
|
@@ -45,7 +61,9 @@
|
|
|
45
61
|
"access": "public"
|
|
46
62
|
},
|
|
47
63
|
"scripts": {
|
|
48
|
-
"build": "
|
|
64
|
+
"build": "pnpm -w run workspace:build \"$PNPM_SCRIPT_SRC_DIR\"",
|
|
65
|
+
"prebuild:self": "pnpm -w run workspace:sync \"$PNPM_SCRIPT_SRC_DIR\"",
|
|
66
|
+
"build:self": "tsup && node ../../.config/scripts/strip-dts-comments.mjs dist",
|
|
49
67
|
"build:watch": "tsup --watch",
|
|
50
68
|
"dev": "tsup --watch",
|
|
51
69
|
"typecheck": "tsc --noEmit",
|