@an5/adapters 0.2.5 → 0.2.9
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 +283 -305
- package/dist/an5Adapter.d.ts +142 -7
- package/dist/an5Adapter.js +998 -68
- package/dist/base/metadata.d.ts +10 -0
- package/dist/base/metadata.js +107 -0
- package/dist/base/sql.d.ts +19 -1
- package/dist/base/sql.js +152 -24
- package/dist/base/types.d.ts +10 -1
- package/dist/browser.d.ts +4 -1
- package/dist/browser.js +9 -2
- package/dist/googlesheets/adapter.d.ts +1 -0
- package/dist/googlesheets/adapter.js +9 -2
- package/dist/googlesheets/config.d.ts +4 -4
- package/dist/googlesheets/helpers.js +2 -0
- package/dist/googlesheets/parseConnectionString.js +5 -2
- package/dist/googlesheets/sqlExecutor.js +29 -24
- package/dist/googlesheets/tableClient.js +14 -5
- package/dist/index.d.ts +5 -1
- package/dist/index.js +15 -1
- package/dist/mssql/engine.js +8 -4
- package/dist/mysql/engine.js +1 -1
- package/dist/nbase/client.d.ts +92 -0
- package/dist/nbase/client.js +147 -0
- package/dist/nbase/index.d.ts +47 -0
- package/dist/nbase/index.js +80 -0
- package/dist/postgres/engine.js +1 -1
- package/dist/sqlite/browserEngine.d.ts +27 -0
- package/dist/sqlite/browserEngine.js +161 -0
- package/dist/sqlite/engine.js +15 -3
- package/dist/sqlite/index.d.ts +1 -0
- package/dist/sqlite/index.js +1 -0
- package/dist/typed.d.ts +5 -0
- package/dist/typed.js +2 -0
- package/dotnet/An5Adapter.cs +1112 -275
- package/dotnet/Base/Core.cs +380 -82
- package/dotnet/Mssql/MssqlEngine.cs +145 -144
- package/dotnet/Postgres/PostgresEngine.cs +149 -121
- package/dotnet/Sqlite/SqliteEngine.cs +214 -0
- package/golang/an5_adapter.go +314 -0
- package/golang/base/base.go +100 -0
- package/golang/base/metadata.go +116 -0
- package/golang/base/where.go +311 -0
- package/golang/go.mod +3 -0
- package/golang/mssql/mssql.go +32 -0
- package/golang/postgres/postgres.go +37 -0
- package/golang/sqlite/sqlite.go +14 -0
- package/golang/sqlite_test.go +42 -0
- package/golang/table_client.go +1329 -0
- package/package.json +26 -9
- package/python/an5_adapter.py +184 -97
- package/python/base/__init__.py +3 -3
- package/python/base/dialects.py +11 -8
- package/python/base/metadata.py +59 -10
- package/python/base/sql.py +202 -121
- package/python/mssql/__init__.py +1 -1
- package/python/mssql/provider.py +62 -62
- package/python/postgres/__init__.py +1 -1
- package/python/postgres/provider.py +15 -15
- package/python/sqlite/__init__.py +3 -0
- package/python/sqlite/provider.py +83 -0
- package/python/table_client.py +636 -216
- package/rust/Cargo.toml +31 -0
- package/rust/README.md +71 -0
- package/rust/src/adapter.rs +1312 -0
- package/rust/src/base/dialect.rs +172 -0
- package/rust/src/base/metadata.rs +134 -0
- package/rust/src/base/mod.rs +17 -0
- package/rust/src/base/where_builder.rs +405 -0
- package/rust/src/lib.rs +75 -0
- package/typescript/src/an5Adapter.ts +1515 -505
- package/typescript/src/base/index.ts +3 -3
- package/typescript/src/base/metadata.ts +148 -28
- package/typescript/src/base/sql.ts +264 -98
- package/typescript/src/base/types.ts +33 -24
- package/typescript/src/browser.ts +10 -6
- package/typescript/src/config.ts +95 -95
- package/typescript/src/googlesheets/adapter.ts +269 -259
- package/typescript/src/googlesheets/an5SheetsAdapter.ts +3 -3
- package/typescript/src/googlesheets/config.ts +47 -47
- package/typescript/src/googlesheets/helpers.ts +144 -143
- package/typescript/src/googlesheets/index.ts +3 -3
- package/typescript/src/googlesheets/parseConnectionString.ts +48 -46
- package/typescript/src/googlesheets/retry.ts +126 -126
- package/typescript/src/googlesheets/sqlExecutor.ts +304 -301
- package/typescript/src/googlesheets/tableClient.ts +461 -455
- package/typescript/src/googlesheets/types.ts +6 -6
- package/typescript/src/index.ts +48 -27
- package/typescript/src/mssql/engine.ts +134 -132
- package/typescript/src/mssql/index.ts +1 -1
- package/typescript/src/mysql/engine.ts +80 -80
- package/typescript/src/mysql/index.ts +1 -1
- package/typescript/src/nbase/client.ts +214 -0
- package/typescript/src/nbase/index.ts +104 -0
- package/typescript/src/postgres/engine.ts +87 -87
- package/typescript/src/postgres/index.ts +1 -1
- package/typescript/src/sqlite/browserEngine.ts +192 -0
- package/typescript/src/sqlite/engine.ts +66 -55
- package/typescript/src/sqlite/index.ts +3 -1
- package/typescript/src/typed.ts +52 -0
package/README.md
CHANGED
|
@@ -1,305 +1,283 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
Standalone runtime
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
- **
|
|
10
|
-
- **
|
|
11
|
-
- **
|
|
12
|
-
- **
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
15
|
-
- **
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
//
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
//
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
|
252
|
-
|
|
253
|
-
|
|
|
254
|
-
|
|
|
255
|
-
| `$transaction(fn)` |
|
|
256
|
-
| `$
|
|
257
|
-
| `$
|
|
258
|
-
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
|
267
|
-
|
|
268
|
-
| `
|
|
269
|
-
| `
|
|
270
|
-
| `
|
|
271
|
-
| `
|
|
272
|
-
| `
|
|
273
|
-
| `
|
|
274
|
-
| `
|
|
275
|
-
| `
|
|
276
|
-
| `
|
|
277
|
-
| `
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
## Provider Layout
|
|
286
|
-
|
|
287
|
-
- TypeScript source providers live under `typescript/src/{base,mssql,postgres,mysql,sqlite,googlesheets}`; `npm run build` compiles them into `dist/` (also exposed via `@an5/adapters/base`, `@an5/adapters/mssql`, … subpaths).
|
|
288
|
-
- Python providers live under `python/{base,mssql,postgres}`, with `python/an5_adapter.py` kept as the public facade. `npm run build:python` (or `python -m build --outdir dist-py`) builds the package into `dist-py/`.
|
|
289
|
-
- .NET providers live under `dotnet/{Base,Mssql,Postgres}`, with `dotnet/an5Adapter.cs` kept as the public facade.
|
|
290
|
-
- Adapters do not depend on generated `an5-client` artifacts; generated clients may pass metadata in explicitly when they need model/table mapping.
|
|
291
|
-
|
|
292
|
-
## Testing
|
|
293
|
-
|
|
294
|
-
```bash
|
|
295
|
-
# TypeScript/Node
|
|
296
|
-
node test/unit.test.js
|
|
297
|
-
|
|
298
|
-
# Python
|
|
299
|
-
python -m compileall python
|
|
300
|
-
python test/smoke.py
|
|
301
|
-
```
|
|
302
|
-
|
|
303
|
-
## License
|
|
304
|
-
|
|
305
|
-
MIT
|
|
1
|
+
# @an5/adapters
|
|
2
|
+
|
|
3
|
+
Standalone runtime database adapter and query engine for AN5 ORM. Provides connection pooling, SQL dialect query building (`parseWhere`, `buildOrderBy`, `quote`), dynamic model proxy access, eager loading (`include`), nested writes, vector search, transactions, and typed table clients across TypeScript, Python, .NET (C#), Golang, and Google Sheets.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Dynamic Model Access** — Access models directly via `db.user.findMany()` or `db.User.create()` (proxy resolves casing/plurals via metadata); `db.table('User')` is the escape hatch for runtime-known model names
|
|
8
|
+
- **Typed Model Access** — Bind generated delegates for full type-safety: `createAn5Adapter<{ user: UserTableClient }>({...})`, plus the `TypedAn5Adapter` / `AdapterAPI` helper types
|
|
9
|
+
- **Relations & Eager Loading** — Query nested relations with `include`, relation-level `select`, `where`, `orderBy`, pagination, and `_count`
|
|
10
|
+
- **Nested Writes** — Create and update records with nested relation writes (`create`, `update`, `disconnect`)
|
|
11
|
+
- **Query Builder & Dialects** — Dialect-aware SQL formatting for MSSQL, PostgreSQL, MySQL, and SQLite
|
|
12
|
+
- **Aggregations & GroupBy** — Standard ORM aggregates (`_count`, `_sum`, `_avg`, `_min`, `_max`) and `groupBy`
|
|
13
|
+
- **Field Math Operators** — Atomic updates with `increment`, `decrement`, `multiply`, `divide`, `set`
|
|
14
|
+
- **Vector Search** — Similarity search using NBase, pgvector (PostgreSQL), `VECTOR_DISTANCE` (MSSQL), or in-memory cosine/euclidean/dot similarity
|
|
15
|
+
- **Real Transactions** — Interactive and callback transactions (`$transaction(async tx => ...)` and `$begin()/$commit()/$rollback()`)
|
|
16
|
+
- **Cross-Language** — Unified API in TypeScript, Python, .NET (C#), Golang, and Rust
|
|
17
|
+
- **Google Sheets Database** — Use spreadsheets as a live database with full CRUD and SQL syntax support
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
### TypeScript
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @an5/adapters
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Python
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install an5-adapters
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### .NET
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
dotnet add package An5Adapters
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Go
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
# Included in the npm package under golang/ or via go module
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Rust
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
cargo add an5-adapters
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The crate is driver-agnostic — it depends on `sqlx` with `any`, so the app enables the driver it needs. See [crates.io](https://crates.io/crates/an5-adapters) and [docs.rs](https://docs.rs/an5-adapters).
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## NBase — Neural Vector Database
|
|
58
|
+
|
|
59
|
+
[NBase](https://github.com/N2FlowJS/nbase) is a partitioned vector database with
|
|
60
|
+
HNSW, LSH and KNN indexing behind a REST API. an5 can run vector search there
|
|
61
|
+
instead of loading every row into memory, which matters once a table holds
|
|
62
|
+
millions of embeddings.
|
|
63
|
+
|
|
64
|
+
The rows stay in your relational table: only the vectors move to NBase, each one
|
|
65
|
+
tagged with its row id, and search results are hydrated from the table.
|
|
66
|
+
|
|
67
|
+
### Configure
|
|
68
|
+
|
|
69
|
+
NBase is configured through the connection string, like every other backend:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { createAn5Adapter } from '@an5/adapters';
|
|
73
|
+
|
|
74
|
+
const db = createAn5Adapter({ connectionString: 'nbase://localhost:1307' });
|
|
75
|
+
await db.$connect(); // probes NBase
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Accepted forms:
|
|
79
|
+
|
|
80
|
+
| Form | Result |
|
|
81
|
+
|------|--------|
|
|
82
|
+
| `nbase://localhost:1307` | `http://localhost:1307` |
|
|
83
|
+
| `nbase:localhost:1307` | `http://localhost:1307` |
|
|
84
|
+
| `nbase:http://localhost:1307` | `http://localhost:1307` |
|
|
85
|
+
| `nbase:https://vectors.example.com` | `https://vectors.example.com` |
|
|
86
|
+
| `nbase://localhost:1307?token=t&timeoutMs=500&method=hnsw` | options applied |
|
|
87
|
+
|
|
88
|
+
`?token`, `?timeoutMs`, `?method`, `?idField` and `?modelField` are read from the
|
|
89
|
+
query string, and a path is kept as a base path for deployments behind a
|
|
90
|
+
reverse proxy.
|
|
91
|
+
|
|
92
|
+
This adapter is **vector-only**: it has no relational database, so a search
|
|
93
|
+
returns the NBase hits — id, metadata and `distance` — instead of table rows.
|
|
94
|
+
|
|
95
|
+
### Search
|
|
96
|
+
|
|
97
|
+
With an `nbase://` connection string, search returns the hits themselves:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
const hits = await db.table('Article').vectorSearch({ vector: queryEmbedding, take: 5 });
|
|
101
|
+
// hits[0] = { id: 'Article:42', an5Id: '42', an5Model: 'Article', distance: 0.11 }
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Search rows from your own table
|
|
105
|
+
|
|
106
|
+
To get real rows back, keep your database connection string and point it at the
|
|
107
|
+
same NBase instance. NBase is still configured with a connection string, never a
|
|
108
|
+
params object:
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
const db = createAn5Adapter({
|
|
112
|
+
connectionString: 'sqlserver://localhost:1433;database=mydb',
|
|
113
|
+
nbase: 'nbase://localhost:1307',
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
await db.table('Article').indexVectorsInNBase({ vectorField: 'embedding' });
|
|
117
|
+
// → { indexed: 1200 }
|
|
118
|
+
|
|
119
|
+
const hits = await db.table('Article').vectorSearch({ vector: queryEmbedding, take: 5 });
|
|
120
|
+
// hits[0] = { id, title, ..., distance }
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Vectors are tagged with their row id, so a hit is hydrated from the table by
|
|
124
|
+
that id and the result keeps the `{ ...row, distance }` shape the other engines
|
|
125
|
+
return.
|
|
126
|
+
|
|
127
|
+
### Standalone client
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
import { createNBaseVectorClient } from '@an5/adapters/nbase';
|
|
131
|
+
|
|
132
|
+
const nbase = createNBaseVectorClient({ url: 'http://localhost:1307' });
|
|
133
|
+
await nbase.health();
|
|
134
|
+
await nbase.addVectors([{ id: 'a:1', vector: [0.1, 0.2], metadata: { an5Id: '1' } }]);
|
|
135
|
+
const { results } = await nbase.search([0.1, 0.2], { k: 5, distanceMetric: 'cosine' });
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
If NBase is unreachable, `vectorSearch` logs a warning and falls back to the
|
|
139
|
+
database's native vector support, then to in-memory similarity, so an outage
|
|
140
|
+
degrades performance rather than breaking queries.
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
## Usage
|
|
144
|
+
|
|
145
|
+
### TypeScript
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
import { createAn5Adapter } from '@an5/adapters';
|
|
149
|
+
|
|
150
|
+
const db = createAn5Adapter({
|
|
151
|
+
connectionString: process.env.DATABASE_URL!,
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// Dynamic model access
|
|
155
|
+
const users = await db.user.findMany({
|
|
156
|
+
where: { active: true },
|
|
157
|
+
include: {
|
|
158
|
+
orders: {
|
|
159
|
+
where: { status: 'paid' },
|
|
160
|
+
orderBy: { total: 'desc' },
|
|
161
|
+
take: 5,
|
|
162
|
+
},
|
|
163
|
+
_count: true,
|
|
164
|
+
},
|
|
165
|
+
take: 10,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
// Nested writes
|
|
169
|
+
const created = await db.user.create({
|
|
170
|
+
data: {
|
|
171
|
+
email: 'alpha@example.com',
|
|
172
|
+
name: 'Alpha',
|
|
173
|
+
orders: {
|
|
174
|
+
create: [
|
|
175
|
+
{ total: 100, status: 'open' },
|
|
176
|
+
{ total: 250, status: 'paid' },
|
|
177
|
+
],
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
include: { orders: true, _count: true },
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// Atomic updates
|
|
184
|
+
await db.user.update({
|
|
185
|
+
where: { id: created.id },
|
|
186
|
+
data: {
|
|
187
|
+
score: { increment: 10 },
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// Aggregations
|
|
192
|
+
const stats = await db.order.aggregate({
|
|
193
|
+
_count: true,
|
|
194
|
+
_sum: { total: true },
|
|
195
|
+
_avg: { total: true },
|
|
196
|
+
});
|
|
197
|
+
console.log(stats._count._all, stats._sum.total, stats._avg.total);
|
|
198
|
+
|
|
199
|
+
// Transactions
|
|
200
|
+
await db.$transaction(async (tx) => {
|
|
201
|
+
await tx.user.update({
|
|
202
|
+
where: { id: created.id },
|
|
203
|
+
data: { score: { increment: 5 } },
|
|
204
|
+
});
|
|
205
|
+
await tx.order.create({
|
|
206
|
+
data: { userId: created.id, total: 50, status: 'paid' },
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// Raw query
|
|
211
|
+
const rawRows = await db.$queryRawUnsafe('SELECT * FROM users WHERE active = @p_0', 1);
|
|
212
|
+
|
|
213
|
+
// Disconnect
|
|
214
|
+
await db.$disconnect();
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Python
|
|
218
|
+
|
|
219
|
+
```python
|
|
220
|
+
from an5_adapter import create_an5_adapter
|
|
221
|
+
|
|
222
|
+
db = create_an5_adapter("sqlserver://localhost:1433;database=mydb;user=sa;password=pass")
|
|
223
|
+
|
|
224
|
+
# Dynamic model access
|
|
225
|
+
users = db.user.find_many(where={"active": True}, take=10)
|
|
226
|
+
|
|
227
|
+
# Nested create
|
|
228
|
+
created = db.user.create({
|
|
229
|
+
"data": {
|
|
230
|
+
"email": "alpha@example.com",
|
|
231
|
+
"name": "Alpha",
|
|
232
|
+
}
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
# Transactions
|
|
236
|
+
def perform_transfer(tx):
|
|
237
|
+
tx.user.update({"where": {"id": "u1"}, "data": {"score": {"increment": 10}}})
|
|
238
|
+
|
|
239
|
+
db.transaction(perform_transfer)
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
---
|
|
243
|
+
|
|
244
|
+
## API Reference
|
|
245
|
+
|
|
246
|
+
### `An5Adapter`
|
|
247
|
+
|
|
248
|
+
| Method / Property | Description |
|
|
249
|
+
|-------------------|-------------|
|
|
250
|
+
| `db[modelName]` | Dynamic proxy returning `AdapterTableClient` for the model |
|
|
251
|
+
| `table<T>(name)` | Explicitly returns `AdapterTableClient<T>` |
|
|
252
|
+
| `exec(query, params)` | Execute raw parameterized query returning rows |
|
|
253
|
+
| `$queryRawUnsafe(sql, ...args)` | Positional raw query |
|
|
254
|
+
| `$executeRaw(sql, ...args)` | Raw DML execution returning affected count |
|
|
255
|
+
| `$transaction(fn)` | Callback transaction with automatic commit/rollback |
|
|
256
|
+
| `$begin()` | Interactive transaction returning `An5AdapterTx` |
|
|
257
|
+
| `$connect()` | Open connection / pool |
|
|
258
|
+
| `$disconnect()` | Close connections |
|
|
259
|
+
|
|
260
|
+
### `AdapterTableClient`
|
|
261
|
+
|
|
262
|
+
| Method | Description |
|
|
263
|
+
|--------|-------------|
|
|
264
|
+
| `findMany(args)` | Query multiple records with `where`, `orderBy`, `skip`, `take`, `select`, `include` |
|
|
265
|
+
| `findFirst(args)` | Query first matching record |
|
|
266
|
+
| `findUnique(args)` | Query unique record by unique filter |
|
|
267
|
+
| `create(args)` | Insert a record with support for nested relation writes |
|
|
268
|
+
| `createMany(args)` | Bulk insert records |
|
|
269
|
+
| `update(args)` | Update record with field math operations & nested relation writes |
|
|
270
|
+
| `updateMany(args)` | Update multiple records |
|
|
271
|
+
| `delete(args)` | Delete a single record |
|
|
272
|
+
| `deleteMany(args)` | Delete multiple records |
|
|
273
|
+
| `upsert(args)` | Insert or update a record |
|
|
274
|
+
| `count(args)` | Count matching records |
|
|
275
|
+
| `aggregate(args)` | Compute `_count`, `_sum`, `_avg`, `_min`, `_max` |
|
|
276
|
+
| `groupBy(args)` | Group by fields with aggregations and pagination |
|
|
277
|
+
| `vectorSearch(args)` | Semantic vector similarity search |
|
|
278
|
+
|
|
279
|
+
---
|
|
280
|
+
|
|
281
|
+
## License
|
|
282
|
+
|
|
283
|
+
MIT
|