dsqlbase 0.2.1 → 0.3.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 +32 -0
- package/README.md +41 -60
- package/dist/client/clients/base.d.ts +20 -0
- package/dist/client/clients/base.d.ts.map +1 -0
- package/dist/client/clients/base.js +52 -0
- package/dist/client/clients/base.js.map +1 -0
- package/dist/client/clients/dsql.d.ts +24 -0
- package/dist/client/clients/dsql.d.ts.map +1 -0
- package/dist/client/clients/dsql.js +31 -0
- package/dist/client/clients/dsql.js.map +1 -0
- package/dist/client/clients/index.d.ts +6 -0
- package/dist/client/clients/index.d.ts.map +1 -0
- package/dist/client/clients/index.js +6 -0
- package/dist/client/clients/index.js.map +1 -0
- package/dist/client/clients/model.d.ts +169 -0
- package/dist/client/clients/model.d.ts.map +1 -0
- package/dist/client/{model/client.js → clients/model.js} +82 -53
- package/dist/client/clients/model.js.map +1 -0
- package/dist/client/clients/transaction.d.ts +9 -0
- package/dist/client/clients/transaction.d.ts.map +1 -0
- package/dist/client/{transaction/transaction-client.js → clients/transaction.js} +7 -7
- package/dist/client/clients/transaction.js.map +1 -0
- package/dist/client/clients/union.d.ts +39 -0
- package/dist/client/clients/union.d.ts.map +1 -0
- package/dist/client/clients/union.js +54 -0
- package/dist/client/clients/union.js.map +1 -0
- package/dist/client/create.d.ts +15 -18
- package/dist/client/create.d.ts.map +1 -1
- package/dist/client/create.js +10 -27
- package/dist/client/create.js.map +1 -1
- package/dist/client/index.d.ts +4 -8
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +3 -6
- package/dist/client/index.js.map +1 -1
- package/dist/client/normalizers/base.d.ts +467 -0
- package/dist/client/normalizers/base.d.ts.map +1 -0
- package/dist/client/normalizers/base.js.map +1 -0
- package/dist/client/normalizers/context.d.ts +14 -0
- package/dist/client/normalizers/context.d.ts.map +1 -0
- package/dist/client/normalizers/context.js +16 -0
- package/dist/client/normalizers/context.js.map +1 -0
- package/dist/client/normalizers/index.d.ts +8 -0
- package/dist/client/normalizers/index.d.ts.map +1 -0
- package/dist/client/normalizers/index.js +8 -0
- package/dist/client/normalizers/index.js.map +1 -0
- package/dist/client/normalizers/pagination.d.ts +87 -0
- package/dist/client/normalizers/pagination.d.ts.map +1 -0
- package/dist/client/normalizers/pagination.js +245 -0
- package/dist/client/normalizers/pagination.js.map +1 -0
- package/dist/client/normalizers/request.d.ts +41 -0
- package/dist/client/normalizers/request.d.ts.map +1 -0
- package/dist/client/normalizers/request.js +346 -0
- package/dist/client/normalizers/request.js.map +1 -0
- package/dist/client/normalizers/rules.d.ts +132 -0
- package/dist/client/normalizers/rules.d.ts.map +1 -0
- package/dist/client/normalizers/rules.js +82 -0
- package/dist/client/normalizers/rules.js.map +1 -0
- package/dist/client/normalizers/utils.d.ts +6 -0
- package/dist/client/normalizers/utils.d.ts.map +1 -0
- package/dist/client/normalizers/utils.js +18 -0
- package/dist/client/normalizers/utils.js.map +1 -0
- package/dist/client/normalizers/where.d.ts +39 -0
- package/dist/client/normalizers/where.d.ts.map +1 -0
- package/dist/client/normalizers/where.js +230 -0
- package/dist/client/normalizers/where.js.map +1 -0
- package/dist/client/utils/occ-retry.d.ts.map +1 -0
- package/dist/client/utils/occ-retry.js.map +1 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/schema/columns/guid.d.ts +21 -21
- package/dist/schema/columns/guid.d.ts.map +1 -1
- package/dist/schema/columns/guid.js +24 -25
- package/dist/schema/columns/guid.js.map +1 -1
- package/dist/schema/index.d.ts +0 -2
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema/index.js +0 -2
- package/dist/schema/index.js.map +1 -1
- package/dist/schema/relations.d.ts +22 -13
- package/dist/schema/relations.d.ts.map +1 -1
- package/dist/schema/relations.js +9 -1
- package/dist/schema/relations.js.map +1 -1
- package/dist/schema/table.d.ts +2 -2
- package/dist/schema/table.d.ts.map +1 -1
- package/dist/schema/table.js.map +1 -1
- package/dist/schema/union.d.ts +13 -8
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +14 -9
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/utils/global-id.d.ts +3 -3
- package/dist/schema/utils/global-id.js +3 -3
- package/dist/schema/utils/global-id.js.map +1 -1
- package/dist/schema/utils/guid-hooks.d.ts +49 -0
- package/dist/schema/utils/guid-hooks.d.ts.map +1 -0
- package/dist/schema/utils/guid-hooks.js +155 -0
- package/dist/schema/utils/guid-hooks.js.map +1 -0
- package/package.json +8 -6
- package/dist/client/database/base.d.ts +0 -99
- package/dist/client/database/base.d.ts.map +0 -1
- package/dist/client/database/base.js +0 -216
- package/dist/client/database/base.js.map +0 -1
- package/dist/client/database/client.d.ts +0 -26
- package/dist/client/database/client.d.ts.map +0 -1
- package/dist/client/database/client.js +0 -47
- package/dist/client/database/client.js.map +0 -1
- package/dist/client/database/index.d.ts +0 -65
- package/dist/client/database/index.d.ts.map +0 -1
- package/dist/client/database/index.js +0 -3
- package/dist/client/database/index.js.map +0 -1
- package/dist/client/model/base.d.ts +0 -747
- package/dist/client/model/base.d.ts.map +0 -1
- package/dist/client/model/base.js.map +0 -1
- package/dist/client/model/client.d.ts +0 -139
- package/dist/client/model/client.d.ts.map +0 -1
- package/dist/client/model/client.js.map +0 -1
- package/dist/client/model/filters.d.ts +0 -276
- package/dist/client/model/filters.d.ts.map +0 -1
- package/dist/client/model/filters.js +0 -470
- package/dist/client/model/filters.js.map +0 -1
- package/dist/client/model/normalizer.d.ts +0 -143
- package/dist/client/model/normalizer.d.ts.map +0 -1
- package/dist/client/model/normalizer.js +0 -641
- package/dist/client/model/normalizer.js.map +0 -1
- package/dist/client/nodes.d.ts +0 -62
- package/dist/client/nodes.d.ts.map +0 -1
- package/dist/client/nodes.js +0 -378
- package/dist/client/nodes.js.map +0 -1
- package/dist/client/pagination/cursor.d.ts +0 -40
- package/dist/client/pagination/cursor.d.ts.map +0 -1
- package/dist/client/pagination/cursor.js +0 -79
- package/dist/client/pagination/cursor.js.map +0 -1
- package/dist/client/pagination/page.d.ts +0 -23
- package/dist/client/pagination/page.d.ts.map +0 -1
- package/dist/client/pagination/page.js +0 -37
- package/dist/client/pagination/page.js.map +0 -1
- package/dist/client/transaction/index.d.ts +0 -2
- package/dist/client/transaction/index.d.ts.map +0 -1
- package/dist/client/transaction/index.js +0 -2
- package/dist/client/transaction/index.js.map +0 -1
- package/dist/client/transaction/occ-retry.d.ts.map +0 -1
- package/dist/client/transaction/occ-retry.js.map +0 -1
- package/dist/client/transaction/transaction-client.d.ts +0 -9
- package/dist/client/transaction/transaction-client.d.ts.map +0 -1
- package/dist/client/transaction/transaction-client.js.map +0 -1
- package/dist/client/union/client.d.ts +0 -55
- package/dist/client/union/client.d.ts.map +0 -1
- package/dist/client/union/client.js +0 -74
- package/dist/client/union/client.js.map +0 -1
- package/dist/schema/embedded.d.ts +0 -31
- package/dist/schema/embedded.d.ts.map +0 -1
- package/dist/schema/embedded.js +0 -33
- package/dist/schema/embedded.js.map +0 -1
- package/dist/schema/tenant.d.ts +0 -31
- package/dist/schema/tenant.d.ts.map +0 -1
- package/dist/schema/tenant.js +0 -33
- package/dist/schema/tenant.js.map +0 -1
- /package/dist/client/{model → normalizers}/base.js +0 -0
- /package/dist/client/{transaction → utils}/occ-retry.d.ts +0 -0
- /package/dist/client/{transaction → utils}/occ-retry.js +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,37 @@
|
|
|
1
1
|
# dsqlbase
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 69cd317: `dsqlbase`:
|
|
8
|
+
|
|
9
|
+
- Client rewritten with a cleaner structure (breaking): `ModelClient.get({ key })` for unique reads; `update()` / `delete()` take `key: {...}`; `client.paginated()` is now `client.paginatedQuery()` and returns `{ items, hasMore, nextCursor }`; `$listByGlobalId` removed.
|
|
10
|
+
- `update` / `delete` change exactly one row; `updateMany` / `deleteMany` change every match (breaking).
|
|
11
|
+
- Row policies replace tenancy: `table.policy()`, `PolicyViolationError`, `createClient({ policies: { bypass } })`, per-request `sql.claim()` / `$withClaims()` (breaking: `tenantScope`, `$identityClaims` and the client tenancy generics are removed).
|
|
12
|
+
- `$findByGlobalId(id)` takes the id alone and resolves by any unique key (breaking).
|
|
13
|
+
- `guid()` takes only a column name; what its ids name comes from relations (breaking).
|
|
14
|
+
- `union(name, { members, columns })` declares its shared columns, and `relations()` takes a union as its source (breaking).
|
|
15
|
+
- Embedded objects removed (breaking).
|
|
16
|
+
- `guid()` columns wrap ids through column hooks.
|
|
17
|
+
|
|
18
|
+
`@dsqlbase/core`:
|
|
19
|
+
|
|
20
|
+
- `ColumnHooks` `read` / `write` / `filter` / `writeDeps`, in the new `@dsqlbase/core/resolvers` entrypoint (breaking: `ColumnRowDecoder` removed).
|
|
21
|
+
- `Column.references` and `Column.dependsOn` record what a column points at (breaking).
|
|
22
|
+
- `SchemaRegistry` is built in phases and resolves relations once (breaking: `getRelationTarget` and related helpers removed).
|
|
23
|
+
- `UnionDefinition` takes a name and declared columns; `UnionColumnDefinition` removed (breaking).
|
|
24
|
+
- `Table.uniqueKeys` lists every unique key.
|
|
25
|
+
- Tenancy and column-group APIs removed (breaking).
|
|
26
|
+
- `$onUpdate` hook receives the column itself.
|
|
27
|
+
|
|
28
|
+
Docs: docs/decisions/0025-client-rewrite.md, docs/guide/pagination.md, docs/guide/querying.md, docs/guide/global-ids.md, docs/guide/polymorphic-relations.md, docs/guide/transactions.md, docs/guide/schema.md, docs/guide/policies.md, docs/internals/runtime-pipeline.md, docs/internals/architecture.md, docs/internals/codec-boundary.md, README.md, packages/dsqlbase/README.md
|
|
29
|
+
|
|
30
|
+
### Patch Changes
|
|
31
|
+
|
|
32
|
+
- Updated dependencies [69cd317]
|
|
33
|
+
- @dsqlbase/core@0.3.0
|
|
34
|
+
|
|
3
35
|
## 0.2.1
|
|
4
36
|
|
|
5
37
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -80,31 +80,37 @@ const withProjects = await dsql.teams.findMany({
|
|
|
80
80
|
});
|
|
81
81
|
```
|
|
82
82
|
|
|
83
|
-
##
|
|
83
|
+
## Row policies
|
|
84
84
|
|
|
85
|
-
|
|
86
|
-
|
|
85
|
+
Aurora DSQL has no row-level security, so the client enforces RLS-shaped policies itself, on
|
|
86
|
+
every statement it builds — joins, unions, counts and node lookups included.
|
|
87
87
|
|
|
88
88
|
```ts
|
|
89
|
-
import {
|
|
89
|
+
import { sql } from "dsqlbase";
|
|
90
|
+
import { table, text, uuid } from "dsqlbase/schema";
|
|
90
91
|
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
export const invoices = ws.table("invoices", {
|
|
92
|
+
export const invoices = table("invoices", {
|
|
94
93
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
94
|
+
workspaceId: uuid("workspace_id").notNull().readOnly().$onCreate(() => sql.claim("workspaceId")),
|
|
95
95
|
number: text("number").notNull(),
|
|
96
96
|
});
|
|
97
97
|
|
|
98
|
+
invoices.policy("workspace_isolation", {
|
|
99
|
+
using: (t) => sql`${t.workspaceId} = ${sql.claim("workspaceId")}`,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const dsql = createClient({ schema, session });
|
|
103
|
+
|
|
98
104
|
// Per request:
|
|
99
|
-
const db = dsql.$
|
|
105
|
+
const db = dsql.$withClaims({ workspaceId: claims.workspace_id });
|
|
100
106
|
|
|
101
107
|
await db.invoices.findMany({}); // … WHERE "workspace_id" = $1
|
|
102
|
-
await db.invoices.create({ data: { number: "INV-1" } }); // workspace_id filled from the
|
|
108
|
+
await db.invoices.create({ data: { number: "INV-1" } }); // workspace_id filled from the setting
|
|
103
109
|
```
|
|
104
110
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
111
|
+
A table with any policy denies by default, an unset setting renders `NULL` and so matches
|
|
112
|
+
nothing, and a write that fails a policy's check throws `PolicyViolationError` with nothing
|
|
113
|
+
written. See the [row policies guide](https://github.com/slsdotdev/dsqlbase/blob/main/docs/guide/policies.md).
|
|
108
114
|
|
|
109
115
|
## Global ids
|
|
110
116
|
|
|
@@ -122,18 +128,18 @@ export const authors = table("authors", {
|
|
|
122
128
|
|
|
123
129
|
export const articles = table("articles", {
|
|
124
130
|
id: guid("id").primaryKey().defaultRandom(),
|
|
125
|
-
authorId: guid("author_id"
|
|
131
|
+
authorId: guid("author_id").notNull(), // an authors id, through a relation to authors.id
|
|
126
132
|
});
|
|
127
133
|
```
|
|
128
134
|
|
|
129
135
|
```ts
|
|
130
|
-
const article = await dsql.articles.
|
|
136
|
+
const article = await dsql.articles.get({ key: { id }, join: { author: true } });
|
|
131
137
|
|
|
132
138
|
article?.id; // "guid:WyJhcnRpY2xlcyIs…"
|
|
133
139
|
article?.authorId === article?.author?.id; // true
|
|
134
140
|
|
|
135
|
-
const record = await dsql.$findByGlobalId(
|
|
136
|
-
if (record
|
|
141
|
+
const record = await dsql.$findByGlobalId(id);
|
|
142
|
+
if (record?.__type === "articles") record.title;
|
|
137
143
|
```
|
|
138
144
|
|
|
139
145
|
A raw uuid is still accepted on input; an id from another table throws rather than quietly
|
|
@@ -142,21 +148,20 @@ matching nothing. It serializes as a plain `uuid`, so adopting it produces no mi
|
|
|
142
148
|
|
|
143
149
|
## Pagination
|
|
144
150
|
|
|
145
|
-
`
|
|
151
|
+
`paginatedQuery` reads one page at a time from a cursor rather than an offset, ordered by your
|
|
146
152
|
`orderBy` and then the primary key, so no row is skipped or repeated between pages:
|
|
147
153
|
|
|
148
154
|
```ts
|
|
149
|
-
const page = await dsql.tasks.
|
|
155
|
+
const page = await dsql.tasks.paginatedQuery({
|
|
150
156
|
where: { status: "todo" },
|
|
151
157
|
orderBy: { createdAt: "desc" },
|
|
152
158
|
limit: 20,
|
|
153
|
-
after: previous?.
|
|
154
|
-
count: true, // adds totalCount
|
|
159
|
+
after: previous?.nextCursor,
|
|
155
160
|
});
|
|
156
161
|
|
|
157
|
-
page.items; // each with
|
|
158
|
-
page.
|
|
159
|
-
page.
|
|
162
|
+
page.items; // each with __cursor
|
|
163
|
+
page.hasMore;
|
|
164
|
+
page.nextCursor;
|
|
160
165
|
|
|
161
166
|
const open = await dsql.tasks.count({ where: { status: "todo" } });
|
|
162
167
|
```
|
|
@@ -209,46 +214,20 @@ await dsql.boards.findMany({ where: { limits: { hasKey: "cpu" } } });
|
|
|
209
214
|
See the
|
|
210
215
|
[schema guide](https://github.com/slsdotdev/dsqlbase/blob/main/docs/guide/schema.md#json-columns).
|
|
211
216
|
|
|
212
|
-
## Embedded objects
|
|
213
|
-
|
|
214
|
-
`embedded()` declares a reusable value object; `.column(name)` places it in a table as real
|
|
215
|
-
columns, `<name>_<member>`, read and written as one nested object:
|
|
216
|
-
|
|
217
|
-
```ts
|
|
218
|
-
import { embedded, numeric, table, text, uuid } from "dsqlbase/schema";
|
|
219
|
-
|
|
220
|
-
export const money = embedded({
|
|
221
|
-
amount: numeric("amount").notNull(),
|
|
222
|
-
currency: text("currency").notNull().default("EUR"),
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
export const invoices = table("invoices", {
|
|
226
|
-
id: uuid("id").primaryKey().defaultRandom(),
|
|
227
|
-
netValue: money.column("net_value"), // net_value_amount, net_value_currency
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
invoices.index("invoices_net_value_idx").columns((c) => [c.netValue.amount]);
|
|
231
|
-
|
|
232
|
-
await dsql.invoices.create({ data: { netValue: { amount: 120 } } });
|
|
233
|
-
await dsql.invoices.findMany({
|
|
234
|
-
where: { netValue: { where: { amount: { gt: 100 } } } },
|
|
235
|
-
orderBy: { netValue: { amount: "desc" } },
|
|
236
|
-
});
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
A group whose members are all nullable reads `null` when all of them are. See the
|
|
240
|
-
[embedded objects guide](https://github.com/slsdotdev/dsqlbase/blob/main/docs/guide/embeddable-objects.md).
|
|
241
|
-
|
|
242
217
|
## Polymorphic relations
|
|
243
218
|
|
|
244
219
|
`union()` groups tables that stand in for one another — a GraphQL union or interface — and
|
|
245
220
|
relations can point at it. The client reads it as one `UNION ALL`, ordered and limited across
|
|
246
|
-
members in SQL, and
|
|
221
|
+
members in SQL, and `__type` on every row says which member it came from:
|
|
247
222
|
|
|
248
223
|
```ts
|
|
249
|
-
import { union, relations, hasMany } from "dsqlbase/schema";
|
|
224
|
+
import { datetime, guid, union, relations, hasMany } from "dsqlbase/schema";
|
|
250
225
|
|
|
251
|
-
|
|
226
|
+
// `columns` declares the fields every member shares — the ones that work across it.
|
|
227
|
+
export const posts = union("posts", {
|
|
228
|
+
members: { photos, videos },
|
|
229
|
+
columns: { id: guid("id"), authorId: guid("author_id").notNull(), createdAt: datetime("created_at") },
|
|
230
|
+
});
|
|
252
231
|
|
|
253
232
|
export const authorRelations = relations(authors, {
|
|
254
233
|
posts: hasMany(posts, { from: [authors.columns.id], to: [posts.columns.authorId] }),
|
|
@@ -266,19 +245,21 @@ const author = await dsql.authors.findOne({
|
|
|
266
245
|
});
|
|
267
246
|
|
|
268
247
|
for (const post of author?.posts ?? []) {
|
|
269
|
-
if (post
|
|
248
|
+
if (post.__type === "photos") post.photoUrl;
|
|
270
249
|
}
|
|
271
250
|
|
|
272
|
-
const page = await dsql.posts.
|
|
251
|
+
const page = await dsql.posts.paginatedQuery({ where: { __type: { in: types } }, limit: 20 });
|
|
273
252
|
```
|
|
274
253
|
|
|
275
254
|
A `belongsTo(union, { ..., discriminator })` points one row at one of several tables. Its
|
|
276
|
-
`guid()` reads back keyed by whichever member the discriminator names
|
|
255
|
+
`guid()` reads back keyed by whichever member the discriminator names; without a discriminator,
|
|
256
|
+
by the first member holding the key. `relations()` also takes a union, giving every member the
|
|
257
|
+
relation. See the
|
|
277
258
|
[polymorphic relations guide](https://github.com/slsdotdev/dsqlbase/blob/main/docs/guide/polymorphic-relations.md).
|
|
278
259
|
|
|
279
260
|
## Links
|
|
280
261
|
|
|
281
|
-
- [Guide](https://github.com/slsdotdev/dsqlbase/blob/main/docs/guide/README.md) — schema, querying, pagination, polymorphic relations, sessions, transactions,
|
|
262
|
+
- [Guide](https://github.com/slsdotdev/dsqlbase/blob/main/docs/guide/README.md) — schema, querying, pagination, polymorphic relations, sessions, transactions, row policies, global ids, migrations, DSQL notes
|
|
282
263
|
- [Repository](https://github.com/slsdotdev/dsqlbase)
|
|
283
264
|
- [Issues](https://github.com/slsdotdev/dsqlbase/issues)
|
|
284
265
|
- [Contributing](https://github.com/slsdotdev/dsqlbase/blob/main/CONTRIBUTING.md)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AnyTable, AnyUnion, DefinitionSchema, ExecutableQuery, OperationResult, RuntimeTables, RuntimeUnions, Schema, SQLQuery, SQLStatement } from "@dsqlbase/core";
|
|
2
|
+
import { ModelClient } from "./model.js";
|
|
3
|
+
import { UnionClient } from "./union.js";
|
|
4
|
+
import { ClientContext } from "../normalizers/context.js";
|
|
5
|
+
import { GlobalIdResultOf } from "../normalizers/base.js";
|
|
6
|
+
export declare abstract class BaseClient<T extends DefinitionSchema> {
|
|
7
|
+
protected readonly _ctx: ClientContext<T>;
|
|
8
|
+
protected readonly _models: Map<string, ModelClient<AnyTable, T> | UnionClient<AnyUnion, T>>;
|
|
9
|
+
constructor(ctx: ClientContext<T>);
|
|
10
|
+
$findByGlobalId(guid: string): ExecutableQuery<OperationResult<"one", GlobalIdResultOf<Schema<T>>>>;
|
|
11
|
+
$query<T = unknown>(sql: SQLQuery): ExecutableQuery<T[]>;
|
|
12
|
+
$execute<T = unknown>(query: SQLStatement): Promise<T[]>;
|
|
13
|
+
}
|
|
14
|
+
export type Aliases<T extends DefinitionSchema> = keyof RuntimeTables<Schema<T>> | keyof RuntimeUnions<Schema<T>>;
|
|
15
|
+
export type Models<T extends DefinitionSchema> = {
|
|
16
|
+
readonly [K in Aliases<T>]: RuntimeTables<Schema<T>> extends Record<K, infer TTable> ? TTable extends AnyTable ? ModelClient<TTable, T> : never : RuntimeUnions<Schema<T>> extends Record<K, infer TUnion> ? TUnion extends AnyUnion ? UnionClient<TUnion, T> : never : never;
|
|
17
|
+
};
|
|
18
|
+
export type WithModels<TSchema extends DefinitionSchema, TClient extends BaseClient<TSchema>> = TClient & Models<TSchema>;
|
|
19
|
+
export declare function withModels<TSchema extends DefinitionSchema, TClient extends BaseClient<TSchema>>(client: TClient): WithModels<TSchema, TClient>;
|
|
20
|
+
//# sourceMappingURL=base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../src/client/clients/base.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,QAAQ,EACR,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,aAAa,EACb,aAAa,EACb,MAAM,EACN,QAAQ,EACR,YAAY,EACb,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D,8BAAsB,UAAU,CAAC,CAAC,SAAS,gBAAgB;IACzD,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC;IAE1C,SAAS,CAAC,QAAQ,CAAC,OAAO,mEAGtB;gBAEQ,GAAG,EAAE,aAAa,CAAC,CAAC,CAAC;IAc1B,eAAe,CACpB,IAAI,EAAE,MAAM,GACX,eAAe,CAAC,eAAe,CAAC,KAAK,EAAE,gBAAgB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAOvE,MAAM,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,QAAQ;IAc3B,QAAQ,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC;CAW/D;AAED,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,gBAAgB,IAC1C,MAAM,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAC9B,MAAM,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AAEnC,MAAM,MAAM,MAAM,CAAC,CAAC,SAAS,gBAAgB,IAAI;IAC/C,QAAQ,EAAE,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,CAAC,CAAC,EAAE,MAAM,MAAM,CAAC,GAChF,MAAM,SAAS,QAAQ,GACrB,WAAW,CAAC,MAAM,EAAE,CAAC,CAAC,GACtB,KAAK,GACP,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,CAAC,CAAC,EAAE,MAAM,MAAM,CAAC,GACtD,MAAM,SAAS,QAAQ,GACrB,WAAW,CAAC,MAAM,EAAE,CAAC,CAAC,GACtB,KAAK,GACP,KAAK;CACZ,CAAC;AAEF,MAAM,MAAM,UAAU,CACpB,OAAO,SAAS,gBAAgB,EAChC,OAAO,SAAS,UAAU,CAAC,OAAO,CAAC,IACjC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;AAE9B,wBAAgB,UAAU,CAAC,OAAO,SAAS,gBAAgB,EAAE,OAAO,SAAS,UAAU,CAAC,OAAO,CAAC,EAC9F,MAAM,EAAE,OAAO,GACd,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,CAU9B"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { ExecutableQuery, } from "@dsqlbase/core";
|
|
2
|
+
import { ModelClient } from "./model.js";
|
|
3
|
+
import { UnionClient } from "./union.js";
|
|
4
|
+
export class BaseClient {
|
|
5
|
+
_ctx;
|
|
6
|
+
_models = new Map();
|
|
7
|
+
constructor(ctx) {
|
|
8
|
+
this._ctx = ctx;
|
|
9
|
+
for (const [alias, table] of ctx.schema.getTableEntries()) {
|
|
10
|
+
const model = new ModelClient(ctx, table);
|
|
11
|
+
this._models.set(alias, model);
|
|
12
|
+
}
|
|
13
|
+
for (const [alias, union] of ctx.schema.getUnions()) {
|
|
14
|
+
const client = new UnionClient(ctx, union);
|
|
15
|
+
this._models.set(alias, client);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
$findByGlobalId(guid) {
|
|
19
|
+
const { table, request } = this._ctx.normalizer.resolveGlobalId(guid);
|
|
20
|
+
const operation = this._ctx.operations.createSelectOperation(table, request);
|
|
21
|
+
return new ExecutableQuery(operation, this._ctx.session);
|
|
22
|
+
}
|
|
23
|
+
$query(sql) {
|
|
24
|
+
return new ExecutableQuery({
|
|
25
|
+
mode: "many",
|
|
26
|
+
name: "anonymous_query",
|
|
27
|
+
type: "select",
|
|
28
|
+
args: {},
|
|
29
|
+
query: sql.toQuery(),
|
|
30
|
+
resolve: (result) => result,
|
|
31
|
+
}, this._ctx.session);
|
|
32
|
+
}
|
|
33
|
+
async $execute(query) {
|
|
34
|
+
// A `sql` template here is an object with no `text`: the session would run nothing.
|
|
35
|
+
if (typeof query?.text !== "string") {
|
|
36
|
+
throw new TypeError("$execute takes a rendered statement ({ text, params }); pass a sql`…` template to " +
|
|
37
|
+
"$query, or call .toQuery() on it.");
|
|
38
|
+
}
|
|
39
|
+
return this._ctx.session.execute(query);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export function withModels(client) {
|
|
43
|
+
return new Proxy(client, {
|
|
44
|
+
get(target, prop, receiver) {
|
|
45
|
+
if (target["_models"].has(prop)) {
|
|
46
|
+
return target["_models"].get(prop);
|
|
47
|
+
}
|
|
48
|
+
return Reflect.get(target, prop, receiver);
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=base.js.map
|
|
@@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C,OAAO,CAAC,CAAC;IAC3D,CAAC;IAED,MAAM,CAAc,GAAa;QAC/B,OAAO,IAAI,eAAe,CACxB;YACE,IAAI,EAAE,MAAM;YACZ,IAAI,EAAE,iBAAiB;YACvB,IAAI,EAAE,QAAQ;YACd,IAAI,EAAE,EAAE;YACR,KAAK,EAAE,GAAG,CAAC,OAAO,EAAE;YACpB,OAAO,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM;SAC5B,EACD,IAAI,CAAC,IAAI,CAAC,OAAO,CAClB,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,QAAQ,CAAc,KAAmB;QAC7C,oFAAoF;QACpF,IAAI,OAAQ,KAAsC,EAAE,IAAI,KAAK,QAAQ,EAAE,CAAC;YACtE,MAAM,IAAI,SAAS,CACjB,oFAAoF;gBAClF,mCAAmC,CACtC,CAAC;QACJ,CAAC;QAED,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAI,KAAK,CAAC,CAAC;IAC7C,CAAC;CACF;AAuBD,MAAM,UAAU,UAAU,CACxB,MAAe;IAEf,OAAO,IAAI,KAAK,CAAC,MAAM,EAAE;QACvB,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ;YACxB,IAAI,MAAM,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,IAAc,CAAC,EAAE,CAAC;gBAC1C,OAAO,MAAM,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,IAAc,CAAC,CAAC;YAC/C,CAAC;YAED,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;QAC7C,CAAC;KACF,CAAiC,CAAC;AACrC,CAAC","sourcesContent":["import {\n AnyTable,\n AnyUnion,\n DefinitionSchema,\n ExecutableQuery,\n OperationResult,\n RuntimeTables,\n RuntimeUnions,\n Schema,\n SQLQuery,\n SQLStatement,\n} from \"@dsqlbase/core\";\nimport { ModelClient } from \"./model.js\";\nimport { UnionClient } from \"./union.js\";\nimport { ClientContext } from \"../normalizers/context.js\";\nimport { GlobalIdResultOf } from \"../normalizers/base.js\";\n\nexport abstract class BaseClient<T extends DefinitionSchema> {\n protected readonly _ctx: ClientContext<T>;\n\n protected readonly _models = new Map<\n string,\n ModelClient<AnyTable, T> | UnionClient<AnyUnion, T>\n >();\n\n constructor(ctx: ClientContext<T>) {\n this._ctx = ctx;\n\n for (const [alias, table] of ctx.schema.getTableEntries()) {\n const model = new ModelClient<AnyTable, T>(ctx, table);\n this._models.set(alias, model);\n }\n\n for (const [alias, union] of ctx.schema.getUnions()) {\n const client = new UnionClient<AnyUnion, T>(ctx, union);\n this._models.set(alias, client);\n }\n }\n\n public $findByGlobalId(\n guid: string\n ): ExecutableQuery<OperationResult<\"one\", GlobalIdResultOf<Schema<T>>>> {\n const { table, request } = this._ctx.normalizer.resolveGlobalId(guid);\n const operation = this._ctx.operations.createSelectOperation(table, request);\n\n return new ExecutableQuery(operation, this._ctx.session);\n }\n\n $query<T = unknown>(sql: SQLQuery) {\n return new ExecutableQuery<T[]>(\n {\n mode: \"many\",\n name: \"anonymous_query\",\n type: \"select\",\n args: {},\n query: sql.toQuery(),\n resolve: (result) => result,\n },\n this._ctx.session\n );\n }\n\n async $execute<T = unknown>(query: SQLStatement): Promise<T[]> {\n // A `sql` template here is an object with no `text`: the session would run nothing.\n if (typeof (query as Partial<SQLStatement> | null)?.text !== \"string\") {\n throw new TypeError(\n \"$execute takes a rendered statement ({ text, params }); pass a sql`…` template to \" +\n \"$query, or call .toQuery() on it.\"\n );\n }\n\n return this._ctx.session.execute<T>(query);\n }\n}\n\nexport type Aliases<T extends DefinitionSchema> =\n | keyof RuntimeTables<Schema<T>>\n | keyof RuntimeUnions<Schema<T>>;\n\nexport type Models<T extends DefinitionSchema> = {\n readonly [K in Aliases<T>]: RuntimeTables<Schema<T>> extends Record<K, infer TTable>\n ? TTable extends AnyTable\n ? ModelClient<TTable, T>\n : never\n : RuntimeUnions<Schema<T>> extends Record<K, infer TUnion>\n ? TUnion extends AnyUnion\n ? UnionClient<TUnion, T>\n : never\n : never;\n};\n\nexport type WithModels<\n TSchema extends DefinitionSchema,\n TClient extends BaseClient<TSchema>,\n> = TClient & Models<TSchema>;\n\nexport function withModels<TSchema extends DefinitionSchema, TClient extends BaseClient<TSchema>>(\n client: TClient\n): WithModels<TSchema, TClient> {\n return new Proxy(client, {\n get(target, prop, receiver) {\n if (target[\"_models\"].has(prop as string)) {\n return target[\"_models\"].get(prop as string);\n }\n\n return Reflect.get(target, prop, receiver);\n },\n }) as WithModels<TSchema, TClient>;\n}\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { DefinitionSchema, Executable } from "@dsqlbase/core";
|
|
2
|
+
import { BaseClient, WithModels } from "./base.js";
|
|
3
|
+
import { TxClient } from "./transaction.js";
|
|
4
|
+
export declare class DsqlClient<TDefinition extends DefinitionSchema> extends BaseClient<TDefinition> {
|
|
5
|
+
/**
|
|
6
|
+
* A client carrying `values` for the settings row policies and `$onCreate` read, for the life
|
|
7
|
+
* of one request. Derive it per request, then open transactions on it: a transaction inherits
|
|
8
|
+
* its settings.
|
|
9
|
+
*
|
|
10
|
+
* Called on a client that already carries settings, it merges: `values` are laid over the ones
|
|
11
|
+
* it carries, and `null` unsets one.
|
|
12
|
+
*
|
|
13
|
+
* Declared settings are typed; any other key is accepted too. Nothing is checked: a setting
|
|
14
|
+
* left out renders `NULL`, and the policy that reads it decides — a comparison with `NULL` is
|
|
15
|
+
* never true. The client it is called on stays usable, and a query's SQL is fixed by the client
|
|
16
|
+
* that built it, so concurrent requests never see each other's values.
|
|
17
|
+
*/
|
|
18
|
+
$withClaims(values: Record<string, unknown>): WithModels<TDefinition, DsqlClient<TDefinition>>;
|
|
19
|
+
$transaction<const TQueries extends Executable<unknown>[]>(queries: TQueries): Promise<{
|
|
20
|
+
-readonly [K in keyof TQueries]: TQueries[K] extends Executable<infer TResult> ? TResult : never;
|
|
21
|
+
}>;
|
|
22
|
+
$transaction<TReturn = unknown>(callback: (tx: TxClient<TDefinition>) => Promise<TReturn>): Promise<TReturn>;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=dsql.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dsql.d.ts","sourceRoot":"","sources":["../../../src/client/clients/dsql.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,UAAU,EAAc,MAAM,WAAW,CAAC;AAC/D,OAAO,EAA2B,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAGrE,qBAAa,UAAU,CAAC,WAAW,SAAS,gBAAgB,CAAE,SAAQ,UAAU,CAAC,WAAW,CAAC;IAC3F;;;;;;;;;;;;OAYG;IACI,WAAW,CAChB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC9B,UAAU,CAAC,WAAW,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;IAUtC,YAAY,CAAC,KAAK,CAAC,QAAQ,SAAS,UAAU,CAAC,OAAO,CAAC,EAAE,EACpE,OAAO,EAAE,QAAQ,GAChB,OAAO,CAAC;QACT,CAAC,UAAU,CAAC,IAAI,MAAM,QAAQ,GAAG,QAAQ,CAAC,CAAC,CAAC,SAAS,UAAU,CAAC,MAAM,OAAO,CAAC,GAC1E,OAAO,GACP,KAAK;KACV,CAAC;IACW,YAAY,CAAC,OAAO,GAAG,OAAO,EACzC,QAAQ,EAAE,CAAC,EAAE,EAAE,QAAQ,CAAC,WAAW,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,GACxD,OAAO,CAAC,OAAO,CAAC;CAOpB"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { BaseClient, withModels } from "./base.js";
|
|
2
|
+
import { createTransactionRunner } from "./transaction.js";
|
|
3
|
+
import { ClientContext } from "../normalizers/context.js";
|
|
4
|
+
export class DsqlClient extends BaseClient {
|
|
5
|
+
/**
|
|
6
|
+
* A client carrying `values` for the settings row policies and `$onCreate` read, for the life
|
|
7
|
+
* of one request. Derive it per request, then open transactions on it: a transaction inherits
|
|
8
|
+
* its settings.
|
|
9
|
+
*
|
|
10
|
+
* Called on a client that already carries settings, it merges: `values` are laid over the ones
|
|
11
|
+
* it carries, and `null` unsets one.
|
|
12
|
+
*
|
|
13
|
+
* Declared settings are typed; any other key is accepted too. Nothing is checked: a setting
|
|
14
|
+
* left out renders `NULL`, and the policy that reads it decides — a comparison with `NULL` is
|
|
15
|
+
* never true. The client it is called on stays usable, and a query's SQL is fixed by the client
|
|
16
|
+
* that built it, so concurrent requests never see each other's values.
|
|
17
|
+
*/
|
|
18
|
+
$withClaims(values) {
|
|
19
|
+
const ctx = new ClientContext({
|
|
20
|
+
...this._ctx,
|
|
21
|
+
pagination: this._ctx.pagination.options,
|
|
22
|
+
settings: Object.freeze({ ...this._ctx.settings, ...values }),
|
|
23
|
+
});
|
|
24
|
+
return withModels(new DsqlClient(ctx));
|
|
25
|
+
}
|
|
26
|
+
async $transaction(arg) {
|
|
27
|
+
const runner = await createTransactionRunner(this._ctx);
|
|
28
|
+
return runner(arg);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=dsql.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dsql.js","sourceRoot":"","sources":["../../../src/client/clients/dsql.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAc,UAAU,EAAE,MAAM,WAAW,CAAC;AAC/D,OAAO,EAAE,uBAAuB,EAAY,MAAM,kBAAkB,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAE1D,MAAM,OAAO,UAAiD,SAAQ,UAAuB;IAC3F;;;;;;;;;;;;OAYG;IACI,WAAW,CAChB,MAA+B;QAE/B,MAAM,GAAG,GAAG,IAAI,aAAa,CAAc;YACzC,GAAG,IAAI,CAAC,IAAI;YACZ,UAAU,EAAE,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO;YACxC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,GAAG,MAAM,EAAE,CAAC;SAC9D,CAAC,CAAC;QAEH,OAAO,UAAU,CAAC,IAAI,UAAU,CAAc,GAAG,CAAC,CAAC,CAAC;IACtD,CAAC;IAYM,KAAK,CAAC,YAAY,CACvB,GAAkF;QAElF,MAAM,MAAM,GAAG,MAAM,uBAAuB,CAAc,IAAI,CAAC,IAAI,CAAC,CAAC;QACrE,OAAO,MAAM,CAAU,GAAG,CAAC,CAAC;IAC9B,CAAC;CACF","sourcesContent":["import { DefinitionSchema, Executable } from \"@dsqlbase/core\";\nimport { BaseClient, WithModels, withModels } from \"./base.js\";\nimport { createTransactionRunner, TxClient } from \"./transaction.js\";\nimport { ClientContext } from \"../normalizers/context.js\";\n\nexport class DsqlClient<TDefinition extends DefinitionSchema> extends BaseClient<TDefinition> {\n /**\n * A client carrying `values` for the settings row policies and `$onCreate` read, for the life\n * of one request. Derive it per request, then open transactions on it: a transaction inherits\n * its settings.\n *\n * Called on a client that already carries settings, it merges: `values` are laid over the ones\n * it carries, and `null` unsets one.\n *\n * Declared settings are typed; any other key is accepted too. Nothing is checked: a setting\n * left out renders `NULL`, and the policy that reads it decides — a comparison with `NULL` is\n * never true. The client it is called on stays usable, and a query's SQL is fixed by the client\n * that built it, so concurrent requests never see each other's values.\n */\n public $withClaims(\n values: Record<string, unknown>\n ): WithModels<TDefinition, DsqlClient<TDefinition>> {\n const ctx = new ClientContext<TDefinition>({\n ...this._ctx,\n pagination: this._ctx.pagination.options,\n settings: Object.freeze({ ...this._ctx.settings, ...values }),\n });\n\n return withModels(new DsqlClient<TDefinition>(ctx));\n }\n\n public async $transaction<const TQueries extends Executable<unknown>[]>(\n queries: TQueries\n ): Promise<{\n -readonly [K in keyof TQueries]: TQueries[K] extends Executable<infer TResult>\n ? TResult\n : never;\n }>;\n public async $transaction<TReturn = unknown>(\n callback: (tx: TxClient<TDefinition>) => Promise<TReturn>\n ): Promise<TReturn>;\n public async $transaction<TReturn = unknown>(\n arg: Executable<unknown>[] | ((client: TxClient<TDefinition>) => Promise<TReturn>)\n ): Promise<TReturn> {\n const runner = await createTransactionRunner<TDefinition>(this._ctx);\n return runner<TReturn>(arg);\n }\n}\n"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { type Aliases, BaseClient, type Models, type WithModels, withModels } from "./base.js";
|
|
2
|
+
export { DsqlClient } from "./dsql.js";
|
|
3
|
+
export { ModelClient } from "./model.js";
|
|
4
|
+
export { TransactionClient, type TxClient, createTransactionRunner } from "./transaction.js";
|
|
5
|
+
export { UnionClient } from "./union.js";
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/client/clients/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,UAAU,EAAE,KAAK,MAAM,EAAE,KAAK,UAAU,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAC/F,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,iBAAiB,EAAE,KAAK,QAAQ,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAC7F,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { BaseClient, withModels } from "./base.js";
|
|
2
|
+
export { DsqlClient } from "./dsql.js";
|
|
3
|
+
export { ModelClient } from "./model.js";
|
|
4
|
+
export { TransactionClient, createTransactionRunner } from "./transaction.js";
|
|
5
|
+
export { UnionClient } from "./union.js";
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/client/clients/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,UAAU,EAAgC,UAAU,EAAE,MAAM,WAAW,CAAC;AAC/F,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,iBAAiB,EAAiB,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAC7F,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC","sourcesContent":["export { type Aliases, BaseClient, type Models, type WithModels, withModels } from \"./base.js\";\nexport { DsqlClient } from \"./dsql.js\";\nexport { ModelClient } from \"./model.js\";\nexport { TransactionClient, type TxClient, createTransactionRunner } from \"./transaction.js\";\nexport { UnionClient } from \"./union.js\";\n"]}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { DefinitionSchema } from "@dsqlbase/core";
|
|
2
|
+
import { TypedObject } from "@dsqlbase/core/utils";
|
|
3
|
+
import { AnyTable, ExecutableQuery, OperationResult, Schema } from "@dsqlbase/core/runtime";
|
|
4
|
+
import { CountArgs, QueryArgs, CreateArgs, UpdateArgs, DeleteArgs, QueryResultOf, MutationResultOf, GetArgs, PaginatedQueryArgs, PaginatedQueryResultOf, UpdateUniqueArgs, DeleteUniqueArgs } from "../normalizers/base.js";
|
|
5
|
+
import { ClientContext } from "../normalizers/context.js";
|
|
6
|
+
export declare class ModelClient<TTable extends AnyTable, TDefinition extends DefinitionSchema> implements TypedObject<Schema<TDefinition>> {
|
|
7
|
+
readonly __type: Schema<TDefinition>;
|
|
8
|
+
private readonly _ctx;
|
|
9
|
+
private readonly _table;
|
|
10
|
+
constructor(ctx: ClientContext<TDefinition>, table: TTable);
|
|
11
|
+
/**
|
|
12
|
+
* Finds first record matching the specified criteria.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const user = await dsql.users.findOne({
|
|
17
|
+
* where: { id: "123" },
|
|
18
|
+
* select: { id: true, name: true },
|
|
19
|
+
* join: {
|
|
20
|
+
* profile: {
|
|
21
|
+
* select: { bio: true }
|
|
22
|
+
* }
|
|
23
|
+
* }
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @param args
|
|
28
|
+
* @returns An executable query that can be awaited.
|
|
29
|
+
*
|
|
30
|
+
*/
|
|
31
|
+
findOne<TArgs extends QueryArgs<TTable, this["__type"]>>(args: TArgs): ExecutableQuery<OperationResult<"one", QueryResultOf<TTable, this["__type"], TArgs>>>;
|
|
32
|
+
/**
|
|
33
|
+
* Finds multiple records matching the specified criteria.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const users = await dsql.users.findMany({
|
|
38
|
+
* where: { age: { gt: 18 } },
|
|
39
|
+
* orderBy: { age: "desc" },
|
|
40
|
+
* limit: 10,
|
|
41
|
+
* });
|
|
42
|
+
* ```
|
|
43
|
+
* @param args The query arguments, including filtering, ordering, pagination, and field selection options.
|
|
44
|
+
* @returns An executable query that can be awaited to retrieve the matching records.
|
|
45
|
+
*/
|
|
46
|
+
findMany<TArgs extends QueryArgs<TTable, this["__type"]>>(args: TArgs): ExecutableQuery<OperationResult<"many", QueryResultOf<TTable, this["__type"], TArgs>>>;
|
|
47
|
+
/**
|
|
48
|
+
* Retrieves a single record based on its primary or unique key.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* const user = await dsql.users.get({ key: { id: "123" } });
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @param args - The query arguments with required key.
|
|
56
|
+
* @returns An executable query that can be awaited to retrieve the matching record.
|
|
57
|
+
*/
|
|
58
|
+
get<TArgs extends GetArgs<TTable, this["__type"]>>(args: TArgs): ExecutableQuery<OperationResult<"one", QueryResultOf<TTable, this["__type"], TArgs>>>;
|
|
59
|
+
/**
|
|
60
|
+
* Counts the records `where` selects — every record, when there is no `where`.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* const open = await dsql.tasks.count({ where: { status: "open" } });
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
count(args?: CountArgs<TTable>): ExecutableQuery<number>;
|
|
68
|
+
/**
|
|
69
|
+
* Retrieves records in a paginated manner based on the provided query arguments.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* const paginatedTasks = await dsql.tasks.paginatedQuery({
|
|
74
|
+
* where: { status: "open" },
|
|
75
|
+
* orderBy: { createdAt: "desc" },
|
|
76
|
+
* limit: 10,
|
|
77
|
+
* after: "<cursor>"
|
|
78
|
+
* });
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @param args The query arguments for the paginated query, including filtering, ordering, pagination, and field selection options.
|
|
82
|
+
* @returns An executable query that can be awaited to retrieve a records page.
|
|
83
|
+
*/
|
|
84
|
+
paginatedQuery<TArgs extends PaginatedQueryArgs<TTable, this["__type"]>>(args: TArgs): ExecutableQuery<PaginatedQueryResultOf<TTable, this["__type"], TArgs>>;
|
|
85
|
+
/**
|
|
86
|
+
* Creates a new record in the database with the specified values.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* const newUser = await dsql.users.create({
|
|
91
|
+
* data: {
|
|
92
|
+
* firstName: "John",
|
|
93
|
+
* lastName: "Doe",
|
|
94
|
+
* emailAddress: "john.doe@example.com"
|
|
95
|
+
* }
|
|
96
|
+
* });
|
|
97
|
+
* ```
|
|
98
|
+
* @param args The arguments for creating a new record, including the data to be inserted.
|
|
99
|
+
* @returns An executable query that can be awaited to retrieve the newly created record.
|
|
100
|
+
*/
|
|
101
|
+
create<TArgs extends CreateArgs<TTable>>(args: TArgs): ExecutableQuery<OperationResult<"one", MutationResultOf<TTable, TArgs>>>;
|
|
102
|
+
/**
|
|
103
|
+
* Updates the one record the `where` pins with the provided values. The `where` must compare
|
|
104
|
+
* every field of a unique key for equality with a value; `updateMany` changes every match.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* const updatedUser = await dsql.users.update({
|
|
109
|
+
* where: { id: "123" },
|
|
110
|
+
* set: { emailAddress: "new.email@example.com" }
|
|
111
|
+
* });
|
|
112
|
+
* ```
|
|
113
|
+
* @param args The arguments for updating records, including the criteria for selecting records and the values to be updated.
|
|
114
|
+
* @returns An executable query that can be awaited to retrieve the updated record.
|
|
115
|
+
*/
|
|
116
|
+
update<TArgs extends UpdateUniqueArgs<TTable>>(args: TArgs): ExecutableQuery<OperationResult<"one", MutationResultOf<TTable, TArgs>>>;
|
|
117
|
+
/**
|
|
118
|
+
* Updates every record the `where` matches and returns all of them.
|
|
119
|
+
*
|
|
120
|
+
* `update` changes one row and refuses a `where` that does not pin one; this is the form for
|
|
121
|
+
* a filter that may match many. The `where` is still required: `{}` would match every row.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* const archived = await dsql.posts.updateMany({
|
|
126
|
+
* where: { authorId: "123" },
|
|
127
|
+
* set: { status: "archived" },
|
|
128
|
+
* return: { id: true },
|
|
129
|
+
* });
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
updateMany<TArgs extends UpdateArgs<TTable>>(args: TArgs): ExecutableQuery<OperationResult<"many", MutationResultOf<TTable, TArgs>>>;
|
|
133
|
+
/**
|
|
134
|
+
* Deletes the one record the `where` pins. The `where` must compare every field of a unique
|
|
135
|
+
* key for equality with a value; `deleteMany` removes every match.
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* const deletedUser = await dsql.users.delete({
|
|
140
|
+
* where: { id: "123" }
|
|
141
|
+
* return: {
|
|
142
|
+
* id: true,
|
|
143
|
+
* name: true
|
|
144
|
+
* }
|
|
145
|
+
* });
|
|
146
|
+
* ```
|
|
147
|
+
* @notes
|
|
148
|
+
* * The `where` clause is required to ensure that the operation is deterministic and does not accidentally delete unintended records.
|
|
149
|
+
*
|
|
150
|
+
* @param args The arguments for deleting records, including the criteria for selecting records to delete.
|
|
151
|
+
* @returns An executable query that can be awaited to retrieve the deleted record.
|
|
152
|
+
*/
|
|
153
|
+
delete<TArgs extends DeleteUniqueArgs<TTable>>(args: TArgs): ExecutableQuery<OperationResult<"one", MutationResultOf<TTable, TArgs>>>;
|
|
154
|
+
/**
|
|
155
|
+
* Deletes every record the `where` matches and returns all of them.
|
|
156
|
+
*
|
|
157
|
+
* `delete` removes one row and refuses a `where` that does not pin one; this is the form for
|
|
158
|
+
* a filter that may match many. The `where` is still required: `{}` would match every row.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* const removed = await dsql.sessions.deleteMany({
|
|
163
|
+
* where: { userId: "123" }
|
|
164
|
+
* });
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
deleteMany<TArgs extends DeleteArgs<TTable>>(args: TArgs): ExecutableQuery<OperationResult<"many", MutationResultOf<TTable, TArgs>>>;
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../../src/client/clients/model.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAC5F,OAAO,EACL,SAAS,EACT,SAAS,EACT,UAAU,EACV,UAAU,EACV,UAAU,EACV,aAAa,EACb,gBAAgB,EACh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