dsqlbase 0.2.0 → 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.
Files changed (164) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/README.md +41 -60
  3. package/dist/client/clients/base.d.ts +20 -0
  4. package/dist/client/clients/base.d.ts.map +1 -0
  5. package/dist/client/clients/base.js +52 -0
  6. package/dist/client/clients/base.js.map +1 -0
  7. package/dist/client/clients/dsql.d.ts +24 -0
  8. package/dist/client/clients/dsql.d.ts.map +1 -0
  9. package/dist/client/clients/dsql.js +31 -0
  10. package/dist/client/clients/dsql.js.map +1 -0
  11. package/dist/client/clients/index.d.ts +6 -0
  12. package/dist/client/clients/index.d.ts.map +1 -0
  13. package/dist/client/clients/index.js +6 -0
  14. package/dist/client/clients/index.js.map +1 -0
  15. package/dist/client/clients/model.d.ts +169 -0
  16. package/dist/client/clients/model.d.ts.map +1 -0
  17. package/dist/client/{model/client.js → clients/model.js} +82 -53
  18. package/dist/client/clients/model.js.map +1 -0
  19. package/dist/client/clients/transaction.d.ts +9 -0
  20. package/dist/client/clients/transaction.d.ts.map +1 -0
  21. package/dist/client/{transaction/transaction-client.js → clients/transaction.js} +7 -7
  22. package/dist/client/clients/transaction.js.map +1 -0
  23. package/dist/client/clients/union.d.ts +39 -0
  24. package/dist/client/clients/union.d.ts.map +1 -0
  25. package/dist/client/clients/union.js +54 -0
  26. package/dist/client/clients/union.js.map +1 -0
  27. package/dist/client/create.d.ts +15 -18
  28. package/dist/client/create.d.ts.map +1 -1
  29. package/dist/client/create.js +10 -27
  30. package/dist/client/create.js.map +1 -1
  31. package/dist/client/index.d.ts +4 -8
  32. package/dist/client/index.d.ts.map +1 -1
  33. package/dist/client/index.js +3 -6
  34. package/dist/client/index.js.map +1 -1
  35. package/dist/client/normalizers/base.d.ts +467 -0
  36. package/dist/client/normalizers/base.d.ts.map +1 -0
  37. package/dist/client/normalizers/base.js.map +1 -0
  38. package/dist/client/normalizers/context.d.ts +14 -0
  39. package/dist/client/normalizers/context.d.ts.map +1 -0
  40. package/dist/client/normalizers/context.js +16 -0
  41. package/dist/client/normalizers/context.js.map +1 -0
  42. package/dist/client/normalizers/index.d.ts +8 -0
  43. package/dist/client/normalizers/index.d.ts.map +1 -0
  44. package/dist/client/normalizers/index.js +8 -0
  45. package/dist/client/normalizers/index.js.map +1 -0
  46. package/dist/client/normalizers/pagination.d.ts +87 -0
  47. package/dist/client/normalizers/pagination.d.ts.map +1 -0
  48. package/dist/client/normalizers/pagination.js +245 -0
  49. package/dist/client/normalizers/pagination.js.map +1 -0
  50. package/dist/client/normalizers/request.d.ts +41 -0
  51. package/dist/client/normalizers/request.d.ts.map +1 -0
  52. package/dist/client/normalizers/request.js +346 -0
  53. package/dist/client/normalizers/request.js.map +1 -0
  54. package/dist/client/normalizers/rules.d.ts +132 -0
  55. package/dist/client/normalizers/rules.d.ts.map +1 -0
  56. package/dist/client/normalizers/rules.js +82 -0
  57. package/dist/client/normalizers/rules.js.map +1 -0
  58. package/dist/client/normalizers/utils.d.ts +6 -0
  59. package/dist/client/normalizers/utils.d.ts.map +1 -0
  60. package/dist/client/normalizers/utils.js +18 -0
  61. package/dist/client/normalizers/utils.js.map +1 -0
  62. package/dist/client/normalizers/where.d.ts +39 -0
  63. package/dist/client/normalizers/where.d.ts.map +1 -0
  64. package/dist/client/normalizers/where.js +230 -0
  65. package/dist/client/normalizers/where.js.map +1 -0
  66. package/dist/client/utils/occ-retry.d.ts.map +1 -0
  67. package/dist/client/utils/occ-retry.js.map +1 -0
  68. package/dist/index.d.ts +2 -3
  69. package/dist/index.d.ts.map +1 -1
  70. package/dist/index.js +2 -3
  71. package/dist/index.js.map +1 -1
  72. package/dist/schema/columns/guid.d.ts +21 -21
  73. package/dist/schema/columns/guid.d.ts.map +1 -1
  74. package/dist/schema/columns/guid.js +24 -25
  75. package/dist/schema/columns/guid.js.map +1 -1
  76. package/dist/schema/columns/numeric.d.ts +43 -2
  77. package/dist/schema/columns/numeric.d.ts.map +1 -1
  78. package/dist/schema/columns/numeric.js +42 -12
  79. package/dist/schema/columns/numeric.js.map +1 -1
  80. package/dist/schema/index.d.ts +1 -2
  81. package/dist/schema/index.d.ts.map +1 -1
  82. package/dist/schema/index.js +0 -2
  83. package/dist/schema/index.js.map +1 -1
  84. package/dist/schema/relations.d.ts +22 -13
  85. package/dist/schema/relations.d.ts.map +1 -1
  86. package/dist/schema/relations.js +9 -1
  87. package/dist/schema/relations.js.map +1 -1
  88. package/dist/schema/table.d.ts +2 -2
  89. package/dist/schema/table.d.ts.map +1 -1
  90. package/dist/schema/table.js.map +1 -1
  91. package/dist/schema/union.d.ts +13 -8
  92. package/dist/schema/union.d.ts.map +1 -1
  93. package/dist/schema/union.js +14 -9
  94. package/dist/schema/union.js.map +1 -1
  95. package/dist/schema/utils/global-id.d.ts +3 -3
  96. package/dist/schema/utils/global-id.js +3 -3
  97. package/dist/schema/utils/global-id.js.map +1 -1
  98. package/dist/schema/utils/guid-hooks.d.ts +49 -0
  99. package/dist/schema/utils/guid-hooks.d.ts.map +1 -0
  100. package/dist/schema/utils/guid-hooks.js +155 -0
  101. package/dist/schema/utils/guid-hooks.js.map +1 -0
  102. package/package.json +8 -6
  103. package/dist/client/database/base.d.ts +0 -99
  104. package/dist/client/database/base.d.ts.map +0 -1
  105. package/dist/client/database/base.js +0 -216
  106. package/dist/client/database/base.js.map +0 -1
  107. package/dist/client/database/client.d.ts +0 -26
  108. package/dist/client/database/client.d.ts.map +0 -1
  109. package/dist/client/database/client.js +0 -47
  110. package/dist/client/database/client.js.map +0 -1
  111. package/dist/client/database/index.d.ts +0 -65
  112. package/dist/client/database/index.d.ts.map +0 -1
  113. package/dist/client/database/index.js +0 -3
  114. package/dist/client/database/index.js.map +0 -1
  115. package/dist/client/model/base.d.ts +0 -747
  116. package/dist/client/model/base.d.ts.map +0 -1
  117. package/dist/client/model/base.js.map +0 -1
  118. package/dist/client/model/client.d.ts +0 -139
  119. package/dist/client/model/client.d.ts.map +0 -1
  120. package/dist/client/model/client.js.map +0 -1
  121. package/dist/client/model/filters.d.ts +0 -276
  122. package/dist/client/model/filters.d.ts.map +0 -1
  123. package/dist/client/model/filters.js +0 -470
  124. package/dist/client/model/filters.js.map +0 -1
  125. package/dist/client/model/normalizer.d.ts +0 -143
  126. package/dist/client/model/normalizer.d.ts.map +0 -1
  127. package/dist/client/model/normalizer.js +0 -641
  128. package/dist/client/model/normalizer.js.map +0 -1
  129. package/dist/client/nodes.d.ts +0 -62
  130. package/dist/client/nodes.d.ts.map +0 -1
  131. package/dist/client/nodes.js +0 -378
  132. package/dist/client/nodes.js.map +0 -1
  133. package/dist/client/pagination/cursor.d.ts +0 -40
  134. package/dist/client/pagination/cursor.d.ts.map +0 -1
  135. package/dist/client/pagination/cursor.js +0 -79
  136. package/dist/client/pagination/cursor.js.map +0 -1
  137. package/dist/client/pagination/page.d.ts +0 -23
  138. package/dist/client/pagination/page.d.ts.map +0 -1
  139. package/dist/client/pagination/page.js +0 -37
  140. package/dist/client/pagination/page.js.map +0 -1
  141. package/dist/client/transaction/index.d.ts +0 -2
  142. package/dist/client/transaction/index.d.ts.map +0 -1
  143. package/dist/client/transaction/index.js +0 -2
  144. package/dist/client/transaction/index.js.map +0 -1
  145. package/dist/client/transaction/occ-retry.d.ts.map +0 -1
  146. package/dist/client/transaction/occ-retry.js.map +0 -1
  147. package/dist/client/transaction/transaction-client.d.ts +0 -9
  148. package/dist/client/transaction/transaction-client.d.ts.map +0 -1
  149. package/dist/client/transaction/transaction-client.js.map +0 -1
  150. package/dist/client/union/client.d.ts +0 -55
  151. package/dist/client/union/client.d.ts.map +0 -1
  152. package/dist/client/union/client.js +0 -74
  153. package/dist/client/union/client.js.map +0 -1
  154. package/dist/schema/embedded.d.ts +0 -31
  155. package/dist/schema/embedded.d.ts.map +0 -1
  156. package/dist/schema/embedded.js +0 -33
  157. package/dist/schema/embedded.js.map +0 -1
  158. package/dist/schema/tenant.d.ts +0 -31
  159. package/dist/schema/tenant.d.ts.map +0 -1
  160. package/dist/schema/tenant.js +0 -33
  161. package/dist/schema/tenant.js.map +0 -1
  162. /package/dist/client/{model → normalizers}/base.js +0 -0
  163. /package/dist/client/{transaction → utils}/occ-retry.d.ts +0 -0
  164. /package/dist/client/{transaction → utils}/occ-retry.js +0 -0
@@ -1,17 +1,13 @@
1
- import { CompositeQuery, ExecutableQuery, } from "@dsqlbase/core/runtime";
2
- import { RequestNormalizer } from "./normalizer.js";
3
- import { shapePage } from "../pagination/page.js";
1
+ import { ExecutableQuery } from "@dsqlbase/core/runtime";
4
2
  export class ModelClient {
5
3
  _ctx;
6
4
  _table;
7
- _normalizer;
8
5
  constructor(ctx, table) {
9
6
  this._ctx = ctx;
10
7
  this._table = table;
11
- this._normalizer = new RequestNormalizer(ctx);
12
8
  }
13
9
  /**
14
- * Finds a single record matching the specified criteria.
10
+ * Finds first record matching the specified criteria.
15
11
  *
16
12
  * @example
17
13
  * ```ts
@@ -25,18 +21,13 @@ export class ModelClient {
25
21
  * }
26
22
  * });
27
23
  * ```
28
- * @notes
29
- * * The `where` clause is required to ensure that the operation is deterministic and does not accidentally return an unintended record.
30
- * * The `select` clause allows you to specify which fields to retrieve, if not provided, all fields will be selected by default.
31
- * * A relation may be named in `select` as `true` or a field map (`select: { profile: { bio: true } }`);
32
- * it reads as the same relation in `join`, which is where its `where` / `orderBy` / `limit` go.
33
24
  *
34
25
  * @param args
35
26
  * @returns An executable query that can be awaited.
36
27
  *
37
28
  */
38
29
  findOne(args) {
39
- const request = this._normalizer.normalizeSelect(this._table, args, "one");
30
+ const request = this._ctx.normalizer.createSelectRequest(this._table, args, "one");
40
31
  const operation = this._ctx.operations.createSelectOperation(this._table, request);
41
32
  return new ExecutableQuery(operation, this._ctx.session);
42
33
  }
@@ -55,49 +46,25 @@ export class ModelClient {
55
46
  * @returns An executable query that can be awaited to retrieve the matching records.
56
47
  */
57
48
  findMany(args) {
58
- const request = this._normalizer.normalizeSelect(this._table, args, "many");
49
+ const request = this._ctx.normalizer.createSelectRequest(this._table, args, "many");
59
50
  const operation = this._ctx.operations.createSelectOperation(this._table, request);
60
51
  return new ExecutableQuery(operation, this._ctx.session);
61
52
  }
62
53
  /**
63
- * Reads one page of records, ordered by `orderBy` and then the primary key, with a cursor
64
- * on each record to continue from.
54
+ * Retrieves a single record based on its primary or unique key.
65
55
  *
66
56
  * @example
67
57
  * ```ts
68
- * const page = await dsql.tasks.paginate({
69
- * where: { status: "open" },
70
- * orderBy: { createdAt: "desc" },
71
- * limit: 20,
72
- * after: previous?.endCursor,
73
- * });
74
- *
75
- * page.items; // each with $$meta.cursor
76
- * page.hasNextPage;
77
- * page.endCursor;
58
+ * const user = await dsql.users.get({ key: { id: "123" } });
78
59
  * ```
79
- * @notes
80
- * * A cursor is only valid under the `orderBy` it was taken with; `where`, `select` and
81
- * `join` are passed again on every page and may change between pages.
82
- * * `count: true` adds `totalCount`, from a second statement over the same `where`.
83
- * * A nullable order key sorts its nulls last ascending and first descending.
84
60
  *
85
- * @throws {InvalidCursorError} when `after` or `before` is not a cursor for this order.
61
+ * @param args - The query arguments with required key.
62
+ * @returns An executable query that can be awaited to retrieve the matching record.
86
63
  */
87
- paginate(args) {
88
- const plan = this._normalizer.normalizePaginate(this._table, args);
89
- const select = this._ctx.operations.createSelectOperation(this._table, plan.request);
90
- const page = new ExecutableQuery({ ...select, resolve: (rows) => shapePage(rows, select.resolve, plan) }, this._ctx.session);
91
- if (!args.count) {
92
- return page;
93
- }
94
- // Counted over the caller's filter alone — the very node the page filters by, without the
95
- // keyset — so the total does not shrink as the pages advance.
96
- const count = new ExecutableQuery(this._ctx.operations.createCountOperation(this._table, {
97
- mode: "one",
98
- args: { where: plan.where },
99
- }), this._ctx.session);
100
- return new CompositeQuery([page, count], ([result, totalCount]) => ({ ...result, totalCount }));
64
+ get(args) {
65
+ const request = this._ctx.normalizer.createGetRequest(this._table, args);
66
+ const operation = this._ctx.operations.createSelectOperation(this._table, request);
67
+ return new ExecutableQuery(operation, this._ctx.session);
101
68
  }
102
69
  /**
103
70
  * Counts the records `where` selects — every record, when there is no `where`.
@@ -107,11 +74,33 @@ export class ModelClient {
107
74
  * const open = await dsql.tasks.count({ where: { status: "open" } });
108
75
  * ```
109
76
  */
110
- count(args) {
111
- const request = this._normalizer.normalizeCount(this._table, args);
77
+ count(args = {}) {
78
+ const request = this._ctx.normalizer.createCountRequest(this._table, args);
112
79
  const operation = this._ctx.operations.createCountOperation(this._table, request);
113
80
  return new ExecutableQuery(operation, this._ctx.session);
114
81
  }
82
+ /**
83
+ * Retrieves records in a paginated manner based on the provided query arguments.
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * const paginatedTasks = await dsql.tasks.paginatedQuery({
88
+ * where: { status: "open" },
89
+ * orderBy: { createdAt: "desc" },
90
+ * limit: 10,
91
+ * after: "<cursor>"
92
+ * });
93
+ * ```
94
+ *
95
+ * @param args The query arguments for the paginated query, including filtering, ordering, pagination, and field selection options.
96
+ * @returns An executable query that can be awaited to retrieve a records page.
97
+ */
98
+ paginatedQuery(args) {
99
+ const request = this._ctx.normalizer.createSelectRequest(this._table, args, "many");
100
+ const withPagination = this._ctx.pagination.paginateRequest(this._table, args, request);
101
+ const operation = this._ctx.operations.createSelectOperation(this._table, request);
102
+ return new ExecutableQuery(withPagination(operation), this._ctx.session);
103
+ }
115
104
  /**
116
105
  * Creates a new record in the database with the specified values.
117
106
  *
@@ -129,12 +118,13 @@ export class ModelClient {
129
118
  * @returns An executable query that can be awaited to retrieve the newly created record.
130
119
  */
131
120
  create(args) {
132
- const request = this._normalizer.normalizeInsert(this._table, args, "one");
121
+ const request = this._ctx.normalizer.createInsertRequest(this._table, args, "one");
133
122
  const operation = this._ctx.operations.createInsertOperation(this._table, request);
134
123
  return new ExecutableQuery(operation, this._ctx.session);
135
124
  }
136
125
  /**
137
- * Updates records in the database matching the specified criteria with the provided values.
126
+ * Updates the one record the `where` pins with the provided values. The `where` must compare
127
+ * every field of a unique key for equality with a value; `updateMany` changes every match.
138
128
  *
139
129
  * @example
140
130
  * ```ts
@@ -147,12 +137,33 @@ export class ModelClient {
147
137
  * @returns An executable query that can be awaited to retrieve the updated record.
148
138
  */
149
139
  update(args) {
150
- const request = this._normalizer.normalizeUpdate(this._table, args, "one");
140
+ const request = this._ctx.normalizer.createUpdateUniqueRequest(this._table, args);
151
141
  const operation = this._ctx.operations.createUpdateOperation(this._table, request);
152
142
  return new ExecutableQuery(operation, this._ctx.session);
153
143
  }
154
144
  /**
155
- * Deletes records from the database matching the specified criteria.
145
+ * Updates every record the `where` matches and returns all of them.
146
+ *
147
+ * `update` changes one row and refuses a `where` that does not pin one; this is the form for
148
+ * a filter that may match many. The `where` is still required: `{}` would match every row.
149
+ *
150
+ * @example
151
+ * ```ts
152
+ * const archived = await dsql.posts.updateMany({
153
+ * where: { authorId: "123" },
154
+ * set: { status: "archived" },
155
+ * return: { id: true },
156
+ * });
157
+ * ```
158
+ */
159
+ updateMany(args) {
160
+ const request = this._ctx.normalizer.createUpdateRequest(this._table, args, "many");
161
+ const operation = this._ctx.operations.createUpdateOperation(this._table, request);
162
+ return new ExecutableQuery(operation, this._ctx.session);
163
+ }
164
+ /**
165
+ * Deletes the one record the `where` pins. The `where` must compare every field of a unique
166
+ * key for equality with a value; `deleteMany` removes every match.
156
167
  *
157
168
  * @example
158
169
  * ```ts
@@ -171,9 +182,27 @@ export class ModelClient {
171
182
  * @returns An executable query that can be awaited to retrieve the deleted record.
172
183
  */
173
184
  delete(args) {
174
- const request = this._normalizer.normalizeDelete(this._table, args, "one");
185
+ const request = this._ctx.normalizer.createDeleteUniqueRequest(this._table, args);
186
+ const operation = this._ctx.operations.createDeleteOperation(this._table, request);
187
+ return new ExecutableQuery(operation, this._ctx.session);
188
+ }
189
+ /**
190
+ * Deletes every record the `where` matches and returns all of them.
191
+ *
192
+ * `delete` removes one row and refuses a `where` that does not pin one; this is the form for
193
+ * a filter that may match many. The `where` is still required: `{}` would match every row.
194
+ *
195
+ * @example
196
+ * ```ts
197
+ * const removed = await dsql.sessions.deleteMany({
198
+ * where: { userId: "123" }
199
+ * });
200
+ * ```
201
+ */
202
+ deleteMany(args) {
203
+ const request = this._ctx.normalizer.createDeleteRequest(this._table, args, "many");
175
204
  const operation = this._ctx.operations.createDeleteOperation(this._table, request);
176
205
  return new ExecutableQuery(operation, this._ctx.session);
177
206
  }
178
207
  }
179
- //# sourceMappingURL=client.js.map
208
+ //# sourceMappingURL=model.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model.js","sourceRoot":"","sources":["../../../src/client/clients/model.ts"],"names":[],"mappings":"AAEA,OAAO,EAAY,eAAe,EAA2B,MAAM,wBAAwB,CAAC;AAiB5F,MAAM,OAAO,WAAW;IAML,IAAI,CAA6B;IACjC,MAAM,CAAS;IAEhC,YAAY,GAA+B,EAAE,KAAa;QACxD,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QAChB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IAEI,OAAO,CACZ,IAAW;QAEX,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,mBAAmB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;QACnF,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,qBAAqB,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAEnF,OAAO,IAAI,eAAe,CAAC,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC3D,CAAC;IAED;;;;;;;;;;;;;OAaG;IAEI,QAAQ,CACb,IAAW;QAEX,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,mBAAmB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;QACpF,MAAM,SAAS,GAAG,IAAI,CA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AAC,IAAI,CAAC,UAAU,CAAC,qBAAqB,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAEnF,OAAO,IAAI,eAAe,CAAC,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC3D,CAAC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IAEI,MAAM,CACX,IAAW;QAEX,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,yBAAyB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAClF,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,qBAAqB,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAEnF,OAAO,IAAI,eAAe,CAAC,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC3D,CAAC;IAED;;;;;;;;;;;;OAYG;IACI,UAAU,CACf,IAAW;QAEX,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,mBAAmB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;QACpF,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,qBAAqB,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAEnF,OAAO,IAAI,eAAe,CAAC,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC3D,CAAC;CACF","sourcesContent":["import { DefinitionSchema } from \"@dsqlbase/core\";\nimport { TypedObject } from \"@dsqlbase/core/utils\";\nimport { AnyTable, ExecutableQuery, OperationResult, Schema } from \"@dsqlbase/core/runtime\";\nimport {\n CountArgs,\n QueryArgs,\n CreateArgs,\n UpdateArgs,\n DeleteArgs,\n QueryResultOf,\n MutationResultOf,\n GetArgs,\n PaginatedQueryArgs,\n PaginatedQueryResultOf,\n UpdateUniqueArgs,\n DeleteUniqueArgs,\n} from \"../normalizers/base.js\";\nimport { ClientContext } from \"../normalizers/context.js\";\n\nexport class ModelClient<\n TTable extends AnyTable,\n TDefinition extends DefinitionSchema,\n> implements TypedObject<Schema<TDefinition>> {\n declare readonly __type: Schema<TDefinition>;\n\n private readonly _ctx: ClientContext<TDefinition>;\n private readonly _table: TTable;\n\n constructor(ctx: ClientContext<TDefinition>, table: TTable) {\n this._ctx = ctx;\n this._table = table;\n }\n\n /**\n * Finds first record matching the specified criteria.\n *\n * @example\n * ```ts\n * const user = await dsql.users.findOne({\n * where: { id: \"123\" },\n * select: { id: true, name: true },\n * join: {\n * profile: {\n * select: { bio: true }\n * }\n * }\n * });\n * ```\n *\n * @param args\n * @returns An executable query that can be awaited.\n *\n */\n\n public findOne<TArgs extends QueryArgs<TTable, this[\"__type\"]>>(\n args: TArgs\n ): ExecutableQuery<OperationResult<\"one\", QueryResultOf<TTable, this[\"__type\"], TArgs>>> {\n const request = this._ctx.normalizer.createSelectRequest(this._table, args, \"one\");\n const operation = this._ctx.operations.createSelectOperation(this._table, request);\n\n return new ExecutableQuery(operation, this._ctx.session);\n }\n\n /**\n * Finds multiple records matching the specified criteria.\n *\n * @example\n * ```ts\n * const users = await dsql.users.findMany({\n * where: { age: { gt: 18 } },\n * orderBy: { age: \"desc\" },\n * limit: 10,\n * });\n * ```\n * @param args The query arguments, including filtering, ordering, pagination, and field selection options.\n * @returns An executable query that can be awaited to retrieve the matching records.\n */\n\n public findMany<TArgs extends QueryArgs<TTable, this[\"__type\"]>>(\n args: TArgs\n ): ExecutableQuery<OperationResult<\"many\", QueryResultOf<TTable, this[\"__type\"], TArgs>>> {\n const request = this._ctx.normalizer.createSelectRequest(this._table, args, \"many\");\n const operation = this._ctx.operations.createSelectOperation(this._table, request);\n\n return new ExecutableQuery(operation, this._ctx.session);\n }\n\n /**\n * Retrieves a single record based on its primary or unique key.\n *\n * @example\n * ```ts\n * const user = await dsql.users.get({ key: { id: \"123\" } });\n * ```\n *\n * @param args - The query arguments with required key.\n * @returns An executable query that can be awaited to retrieve the matching record.\n */\n\n public get<TArgs extends GetArgs<TTable, this[\"__type\"]>>(\n args: TArgs\n ): ExecutableQuery<OperationResult<\"one\", QueryResultOf<TTable, this[\"__type\"], TArgs>>> {\n const request = this._ctx.normalizer.createGetRequest(this._table, args);\n const operation = this._ctx.operations.createSelectOperation(this._table, request);\n\n return new ExecutableQuery(operation, this._ctx.session);\n }\n\n /**\n * Counts the records `where` selects — every record, when there is no `where`.\n *\n * @example\n * ```ts\n * const open = await dsql.tasks.count({ where: { status: \"open\" } });\n * ```\n */\n\n public count(args: CountArgs<TTable> = {}): ExecutableQuery<number> {\n const request = this._ctx.normalizer.createCountRequest(this._table, args);\n const operation = this._ctx.operations.createCountOperation(this._table, request);\n\n return new ExecutableQuery(operation, this._ctx.session);\n }\n\n /**\n * Retrieves records in a paginated manner based on the provided query arguments.\n *\n * @example\n * ```ts\n * const paginatedTasks = await dsql.tasks.paginatedQuery({\n * where: { status: \"open\" },\n * orderBy: { createdAt: \"desc\" },\n * limit: 10,\n * after: \"<cursor>\"\n * });\n * ```\n *\n * @param args The query arguments for the paginated query, including filtering, ordering, pagination, and field selection options.\n * @returns An executable query that can be awaited to retrieve a records page.\n */\n\n public paginatedQuery<TArgs extends PaginatedQueryArgs<TTable, this[\"__type\"]>>(\n args: TArgs\n ): ExecutableQuery<PaginatedQueryResultOf<TTable, this[\"__type\"], TArgs>> {\n const request = this._ctx.normalizer.createSelectRequest(this._table, args, \"many\");\n const withPagination = this._ctx.pagination.paginateRequest(this._table, args, request);\n const operation = this._ctx.operations.createSelectOperation(this._table, request);\n\n return new ExecutableQuery(withPagination(operation), this._ctx.session);\n }\n\n /**\n * Creates a new record in the database with the specified values.\n *\n * @example\n * ```ts\n * const newUser = await dsql.users.create({\n * data: {\n * firstName: \"John\",\n * lastName: \"Doe\",\n * emailAddress: \"john.doe@example.com\"\n * }\n * });\n * ```\n * @param args The arguments for creating a new record, including the data to be inserted.\n * @returns An executable query that can be awaited to retrieve the newly created record.\n */\n\n public create<TArgs extends CreateArgs<TTable>>(\n args: TArgs\n ): ExecutableQuery<OperationResult<\"one\", MutationResultOf<TTable, TArgs>>> {\n const request = this._ctx.normalizer.createInsertRequest(this._table, args, \"one\");\n const operation = this._ctx.operations.createInsertOperation(this._table, request);\n\n return new ExecutableQuery(operation, this._ctx.session);\n }\n\n /**\n * Updates the one record the `where` pins with the provided values. The `where` must compare\n * every field of a unique key for equality with a value; `updateMany` changes every match.\n *\n * @example\n * ```ts\n * const updatedUser = await dsql.users.update({\n * where: { id: \"123\" },\n * set: { emailAddress: \"new.email@example.com\" }\n * });\n * ```\n * @param args The arguments for updating records, including the criteria for selecting records and the values to be updated.\n * @returns An executable query that can be awaited to retrieve the updated record.\n */\n\n public update<TArgs extends UpdateUniqueArgs<TTable>>(\n args: TArgs\n ): ExecutableQuery<OperationResult<\"one\", MutationResultOf<TTable, TArgs>>> {\n const request = this._ctx.normalizer.createUpdateUniqueRequest(this._table, args);\n const operation = this._ctx.operations.createUpdateOperation(this._table, request);\n\n return new ExecutableQuery(operation, this._ctx.session);\n }\n\n /**\n * Updates every record the `where` matches and returns all of them.\n *\n * `update` changes one row and refuses a `where` that does not pin one; this is the form for\n * a filter that may match many. The `where` is still required: `{}` would match every row.\n *\n * @example\n * ```ts\n * const archived = await dsql.posts.updateMany({\n * where: { authorId: \"123\" },\n * set: { status: \"archived\" },\n * return: { id: true },\n * });\n * ```\n */\n public updateMany<TArgs extends UpdateArgs<TTable>>(\n args: TArgs\n ): ExecutableQuery<OperationResult<\"many\", MutationResultOf<TTable, TArgs>>> {\n const request = this._ctx.normalizer.createUpdateRequest(this._table, args, \"many\");\n const operation = this._ctx.operations.createUpdateOperation(this._table, request);\n\n return new ExecutableQuery(operation, this._ctx.session);\n }\n\n /**\n * Deletes the one record the `where` pins. The `where` must compare every field of a unique\n * key for equality with a value; `deleteMany` removes every match.\n *\n * @example\n * ```ts\n * const deletedUser = await dsql.users.delete({\n * where: { id: \"123\" }\n * return: {\n * id: true,\n * name: true\n * }\n * });\n * ```\n * @notes\n * * The `where` clause is required to ensure that the operation is deterministic and does not accidentally delete unintended records.\n *\n * @param args The arguments for deleting records, including the criteria for selecting records to delete.\n * @returns An executable query that can be awaited to retrieve the deleted record.\n */\n\n public delete<TArgs extends DeleteUniqueArgs<TTable>>(\n args: TArgs\n ): ExecutableQuery<OperationResult<\"one\", MutationResultOf<TTable, TArgs>>> {\n const request = this._ctx.normalizer.createDeleteUniqueRequest(this._table, args);\n const operation = this._ctx.operations.createDeleteOperation(this._table, request);\n\n return new ExecutableQuery(operation, this._ctx.session);\n }\n\n /**\n * Deletes every record the `where` matches and returns all of them.\n *\n * `delete` removes one row and refuses a `where` that does not pin one; this is the form for\n * a filter that may match many. The `where` is still required: `{}` would match every row.\n *\n * @example\n * ```ts\n * const removed = await dsql.sessions.deleteMany({\n * where: { userId: \"123\" }\n * });\n * ```\n */\n public deleteMany<TArgs extends DeleteArgs<TTable>>(\n args: TArgs\n ): ExecutableQuery<OperationResult<\"many\", MutationResultOf<TTable, TArgs>>> {\n const request = this._ctx.normalizer.createDeleteRequest(this._table, args, \"many\");\n const operation = this._ctx.operations.createDeleteOperation(this._table, request);\n\n return new ExecutableQuery(operation, this._ctx.session);\n }\n}\n"]}
@@ -0,0 +1,9 @@
1
+ import { DefinitionSchema, Executable } from "@dsqlbase/core";
2
+ import { BaseClient, Models } from "./base.js";
3
+ import { OCCRetryOptions } from "../utils/occ-retry.js";
4
+ import { ClientContext } from "../normalizers/context.js";
5
+ export declare class TransactionClient<TDefinition extends DefinitionSchema> extends BaseClient<TDefinition> {
6
+ }
7
+ export declare function createTransactionRunner<TDefinition extends DefinitionSchema>(ctx: ClientContext<TDefinition>, options?: OCCRetryOptions): Promise<(<TReturn = unknown>(opsOrCallback: Executable<unknown>[] | ((client: TxClient<TDefinition>) => Promise<TReturn>), attempt?: number) => Promise<TReturn>)>;
8
+ export type TxClient<T extends DefinitionSchema> = TransactionClient<T> & Models<T>;
9
+ //# sourceMappingURL=transaction.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transaction.d.ts","sourceRoot":"","sources":["../../../src/client/clients/transaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,EAAc,MAAM,WAAW,CAAC;AAC3D,OAAO,EAA4B,eAAe,EAAS,MAAM,uBAAuB,CAAC;AACzF,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAE1D,qBAAa,iBAAiB,CAC5B,WAAW,SAAS,gBAAgB,CACpC,SAAQ,UAAU,CAAC,WAAW,CAAC;CAAG;AAEpC,wBAAsB,uBAAuB,CAAC,WAAW,SAAS,gBAAgB,EAChF,GAAG,EAAE,aAAa,CAAC,WAAW,CAAC,EAC/B,OAAO,GAAE,eAAoB,aAiBf,OAAO,2BACJ,UAAU,CAAC,OAAO,CAAC,EAAE,GAAG,CAAC,CAAC,MAAM,EAAE,QAAQ,CAAC,WAAW,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC,uBAE3F,OAAO,CAAC,OAAO,CAAC,GAiCpB;AAED,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,gBAAgB,IAAI,iBAAiB,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC"}
@@ -1,6 +1,6 @@
1
- import { ExecutionContext } from "@dsqlbase/core";
2
- import { attachModels, BaseClient } from "../database/base.js";
3
- import { backoffDelay, isOccError, sleep } from "./occ-retry.js";
1
+ import { BaseClient, withModels } from "./base.js";
2
+ import { backoffDelay, isOccError, sleep } from "../utils/occ-retry.js";
3
+ import { ClientContext } from "../normalizers/context.js";
4
4
  export class TransactionClient extends BaseClient {
5
5
  }
6
6
  export async function createTransactionRunner(ctx, options = {}) {
@@ -8,12 +8,12 @@ export async function createTransactionRunner(ctx, options = {}) {
8
8
  throw new Error("Session does not support transactions");
9
9
  }
10
10
  const session = await ctx.session.beginTransaction();
11
- const context = new ExecutionContext({
11
+ const context = new ClientContext({
12
12
  ...ctx,
13
13
  session: session,
14
+ pagination: ctx.pagination.options,
14
15
  });
15
- const txClient = new TransactionClient(context);
16
- attachModels(txClient, context);
16
+ const txClient = withModels(new TransactionClient(context));
17
17
  return async (opsOrCallback, attempt = 1) => {
18
18
  const { maxRetries = 3, delay = 50, maxDelay = 1000 } = options;
19
19
  try {
@@ -43,4 +43,4 @@ export async function createTransactionRunner(ctx, options = {}) {
43
43
  }
44
44
  };
45
45
  }
46
- //# sourceMappingURL=transaction-client.js.map
46
+ //# sourceMappingURL=transaction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transaction.js","sourceRoot":"","sources":["../../../src/client/clients/transaction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAU,UAAU,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAAE,YAAY,EAAE,UAAU,EAAmB,KAAK,EAAE,MAAM,uBAAuB,CAAC;AACzF,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAE1D,MAAM,OAAO,iBAEX,SAAQ,UAAuB;CAAG;AAEpC,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,GAA+B,EAC/B,UAA2B,EAAE;IAE7B,IAAI,OAAO,GAAG,CAAC,OAAO,CAAC,gBAAgB,KAAK,UAAU,EAAE,CAAC;QACvD,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;IAC3D,CAAC;IAED,MAAM,OAAO,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,gBAAgB,EAAE,CAAC;IACrD,MAAM,OAAO,GAAG,IAAI,aAAa,CAAc;QAC7C,GAAG,GAAG;QACN,OAAO,EAAE,OAAO;QAChB,UAAU,EAAE,GAAG,CAAC,UAAU,CAAC,OAAO;KACnC,CAAC,CAAC;IAEH,MA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AC,CAAC,CAAC;YAC5C,CAAC;YAED,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { DefinitionSchema, Executable } from \"@dsqlbase/core\";\nimport { BaseClient, Models, withModels } from \"./base.js\";\nimport { backoffDelay, isOccError, OCCRetryOptions, sleep } from \"../utils/occ-retry.js\";\nimport { ClientContext } from \"../normalizers/context.js\";\n\nexport class TransactionClient<\n TDefinition extends DefinitionSchema,\n> extends BaseClient<TDefinition> {}\n\nexport async function createTransactionRunner<TDefinition extends DefinitionSchema>(\n ctx: ClientContext<TDefinition>,\n options: OCCRetryOptions = {}\n) {\n if (typeof ctx.session.beginTransaction !== \"function\") {\n throw new Error(\"Session does not support transactions\");\n }\n\n const session = await ctx.session.beginTransaction();\n const context = new ClientContext<TDefinition>({\n ...ctx,\n session: session,\n pagination: ctx.pagination.options,\n });\n\n const txClient = withModels<TDefinition, TransactionClient<TDefinition>>(\n new TransactionClient(context)\n );\n\n return async <TReturn = unknown>(\n opsOrCallback: Executable<unknown>[] | ((client: TxClient<TDefinition>) => Promise<TReturn>),\n attempt = 1\n ): Promise<TReturn> => {\n const { maxRetries = 3, delay = 50, maxDelay = 1000 } = options;\n\n try {\n if (Array.isArray(opsOrCallback)) {\n const results: unknown[] = [];\n\n for (const op of opsOrCallback) {\n const result = await op.clone(session).execute();\n results.push(result);\n }\n\n await session.commit();\n return results as TReturn;\n }\n\n const result = await opsOrCallback(txClient);\n await session.commit();\n return result;\n } catch (error) {\n // The error that made it roll back is the one that counts: a rollback that fails too (the\n // connection broke) must not replace it, or a `40001` would no longer be retried.\n await session.rollback().catch(() => undefined);\n\n if (isOccError(error) && attempt < maxRetries) {\n await sleep(backoffDelay(attempt, delay, maxDelay));\n const runner = await createTransactionRunner<TDefinition>(ctx, options);\n return runner(opsOrCallback, attempt + 1);\n }\n\n throw error;\n }\n };\n}\n\nexport type TxClient<T extends DefinitionSchema> = TransactionClient<T> & Models<T>;\n"]}
@@ -0,0 +1,39 @@
1
+ import { AnyUnion, DefinitionSchema, ExecutableQuery, Schema } from "@dsqlbase/core";
2
+ import { TypedObject } from "@dsqlbase/core/utils";
3
+ import { CountArgs, PaginatedUnionQueryArgs, PaginatedUnionQueryResultOf, UnionQueryArgs, UnionQueryResultOf } from "../normalizers/base.js";
4
+ import { ClientContext } from "../normalizers/context.js";
5
+ export declare class UnionClient<TUnion extends AnyUnion, TDefinition extends DefinitionSchema> implements TypedObject<Schema<TDefinition>> {
6
+ readonly __type: Schema<TDefinition>;
7
+ private readonly _ctx;
8
+ private readonly _union;
9
+ constructor(ctx: ClientContext<TDefinition>, union: TUnion);
10
+ /**
11
+ * Reads the first row, across every member, that `where` selects — in `orderBy` order when
12
+ * one is given.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const post = await dsql.posts.findOne({ where: { id: { eq: postId } } });
17
+ * if (post?.$$key === "photos") post.photoUrl;
18
+ * ```
19
+ */
20
+ findOne<TArgs extends UnionQueryArgs<TUnion, this["__type"]>>(args: TArgs): ExecutableQuery<UnionQueryResultOf<TUnion, this["__type"], TArgs> | null>;
21
+ /**
22
+ * Reads rows from every member, as one list.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const feed = await dsql.posts.findMany({
27
+ * where: { userId: { eq: userId } },
28
+ * orderBy: { createdAt: "desc" },
29
+ * limit: 20,
30
+ * on: { photos: { select: { photoUrl: true } } },
31
+ * });
32
+ * ```
33
+ */
34
+ findMany<TArgs extends UnionQueryArgs<TUnion, this["__type"]>>(args?: TArgs): ExecutableQuery<UnionQueryResultOf<TUnion, this["__type"], TArgs>[]>;
35
+ paginatedQuery<TArgs extends PaginatedUnionQueryArgs<TUnion, this["__type"]>>(args: TArgs): ExecutableQuery<PaginatedUnionQueryResultOf<TUnion, this["__type"], TArgs>>;
36
+ /** Counts the rows `where` selects, across every member. */
37
+ count(args?: CountArgs<TUnion>): ExecutableQuery<number>;
38
+ }
39
+ //# sourceMappingURL=union.d.ts.map
@@ -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uB,CAAC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,EACjF,IAAI,EAAE,KAAK,GACV,eAAe,CAAC,2BAA2B,CAAC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,EAAE,KAAK,CAAC,CAAC;IAQ9E,4DAA4D;IACrD,KAAK,CAAC,IAAI,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,eAAe,CAAC,MAAM,CAAC;CAQhE"}
@@ -0,0 +1,54 @@
1
+ import { ExecutableQuery } from "@dsqlbase/core";
2
+ export class UnionClient {
3
+ _ctx;
4
+ _union;
5
+ constructor(ctx, union) {
6
+ this._ctx = ctx;
7
+ this._union = union;
8
+ }
9
+ /**
10
+ * Reads the first row, across every member, that `where` selects — in `orderBy` order when
11
+ * one is given.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const post = await dsql.posts.findOne({ where: { id: { eq: postId } } });
16
+ * if (post?.$$key === "photos") post.photoUrl;
17
+ * ```
18
+ */
19
+ findOne(args) {
20
+ const request = this._ctx.normalizer.createUnionSelectRequest(this._union, args, "one");
21
+ const operation = this._ctx.operations.createUnionSelectOperation(this._union, request);
22
+ return new ExecutableQuery(operation, this._ctx.session);
23
+ }
24
+ /**
25
+ * Reads rows from every member, as one list.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * const feed = await dsql.posts.findMany({
30
+ * where: { userId: { eq: userId } },
31
+ * orderBy: { createdAt: "desc" },
32
+ * limit: 20,
33
+ * on: { photos: { select: { photoUrl: true } } },
34
+ * });
35
+ * ```
36
+ */
37
+ findMany(args) {
38
+ const request = this._ctx.normalizer.createUnionSelectRequest(this._union, args ?? {}, "many");
39
+ const operation = this._ctx.operations.createUnionSelectOperation(this._union, request);
40
+ return new ExecutableQuery(operation, this._ctx.session);
41
+ }
42
+ paginatedQuery(args) {
43
+ const request = this._ctx.normalizer.createUnionSelectRequest(this._union, args, "many");
44
+ const withPagination = this._ctx.pagination.paginateUnionRequest(this._union, args, request);
45
+ const operation = this._ctx.operations.createUnionSelectOperation(this._union, request);
46
+ return new ExecutableQuery(withPagination(operation), this._ctx.session);
47
+ }
48
+ /** Counts the rows `where` selects, across every member. */
49
+ count(args) {
50
+ const request = this._ctx.normalizer.createUnionCountRequest(this._union, args);
51
+ return new ExecutableQuery(this._ctx.operations.createUnionCountOperation(this._union, request), this._ctx.session);
52
+ }
53
+ }
54
+ //# sourceMappingURL=union.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"union.js","sourceRoot":"","sources":["../../../src/client/clients/union.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,eAAe,EAAU,MAAM,gBAAgB,CAAC;AAWrF,MAAM,OAAO,WAAW;IAML,IAAI,CAA6B;IACjC,MAAM,CAAS;IAEhC,YAAY,GAA+B,EAAE,KAAa;QACxD,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QAChB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED;;;;;;;;;OASG;IACI,OAAO,CACZ,IAAW;QAEX,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,wBAAwB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;QACxF,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,0BAA0B,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAExF,OAAO,IAAI,eAAe,CAAC,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC3D,CAAC;IAED;;;;;;;;;;;;OAYG;IACI,QAAQ,CACb,IAAY;QAEZ,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,wBAAwB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,IAAI,EAAE,EAAE,MAAM,CAAC,CAAC;QAC/F,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,0BAA0B,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAExF,OAAO,IAAI,eAAe,CAAC,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC3D,CAAC;IAEM,cAAc,CACnB,IAAW;QAEX,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,wBAAwB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;QACzF,MAAM,cAAc,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,oBAAoB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;QAC7F,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,0BAA0B,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAExF,OAAO,IAAI,eAAe,CAAC,cAAc,CAAC,SAAS,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC3E,CAAC;IAED,4DAA4D;IACrD,KAAK,CAAC,IAAwB;QACnC,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,uBAAuB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAEhF,OAAO,IAAI,eAAe,CACxB,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,yBAAyB,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,EACpE,IAAI,CAAC,IAAI,CAAC,OAAO,CAClB,CAAC;IACJ,CAAC;CACF","sourcesContent":["import { AnyUnion, DefinitionSchema, ExecutableQuery, Schema } from \"@dsqlbase/core\";\nimport { TypedObject } from \"@dsqlbase/core/utils\";\nimport {\n CountArgs,\n PaginatedUnionQueryArgs,\n PaginatedUnionQueryResultOf,\n UnionQueryArgs,\n UnionQueryResultOf,\n} from \"../normalizers/base.js\";\nimport { ClientContext } from \"../normalizers/context.js\";\n\nexport class UnionClient<\n TUnion extends AnyUnion,\n TDefinition extends DefinitionSchema,\n> implements TypedObject<Schema<TDefinition>> {\n declare readonly __type: Schema<TDefinition>;\n\n private readonly _ctx: ClientContext<TDefinition>;\n private readonly _union: TUnion;\n\n constructor(ctx: ClientContext<TDefinition>, union: TUnion) {\n this._ctx = ctx;\n this._union = union;\n }\n\n /**\n * Reads the first row, across every member, that `where` selects — in `orderBy` order when\n * one is given.\n *\n * @example\n * ```ts\n * const post = await dsql.posts.findOne({ where: { id: { eq: postId } } });\n * if (post?.$$key === \"photos\") post.photoUrl;\n * ```\n */\n public findOne<TArgs extends UnionQueryArgs<TUnion, this[\"__type\"]>>(\n args: TArgs\n ): ExecutableQuery<UnionQueryResultOf<TUnion, this[\"__type\"], TArgs> | null> {\n const request = this._ctx.normalizer.createUnionSelectRequest(this._union, args, \"one\");\n const operation = this._ctx.operations.createUnionSelectOperation(this._union, request);\n\n return new ExecutableQuery(operation, this._ctx.session);\n }\n\n /**\n * Reads rows from every member, as one list.\n *\n * @example\n * ```ts\n * const feed = await dsql.posts.findMany({\n * where: { userId: { eq: userId } },\n * orderBy: { createdAt: \"desc\" },\n * limit: 20,\n * on: { photos: { select: { photoUrl: true } } },\n * });\n * ```\n */\n public findMany<TArgs extends UnionQueryArgs<TUnion, this[\"__type\"]>>(\n args?: TArgs\n ): ExecutableQuery<UnionQueryResultOf<TUnion, this[\"__type\"], TArgs>[]> {\n const request = this._ctx.normalizer.createUnionSelectRequest(this._union, args ?? {}, \"many\");\n const operation = this._ctx.operations.createUnionSelectOperation(this._union, request);\n\n return new ExecutableQuery(operation, this._ctx.session);\n }\n\n public paginatedQuery<TArgs extends PaginatedUnionQueryArgs<TUnion, this[\"__type\"]>>(\n args: TArgs\n ): ExecutableQuery<PaginatedUnionQueryResultOf<TUnion, this[\"__type\"], TArgs>> {\n const request = this._ctx.normalizer.createUnionSelectRequest(this._union, args, \"many\");\n const withPagination = this._ctx.pagination.paginateUnionRequest(this._union, args, request);\n const operation = this._ctx.operations.createUnionSelectOperation(this._union, request);\n\n return new ExecutableQuery(withPagination(operation), this._ctx.session);\n }\n\n /** Counts the rows `where` selects, across every member. */\n public count(args?: CountArgs<TUnion>): ExecutableQuery<number> {\n const request = this._ctx.normalizer.createUnionCountRequest(this._union, args);\n\n return new ExecutableQuery(\n this._ctx.operations.createUnionCountOperation(this._union, request),\n this._ctx.session\n );\n }\n}\n"]}
@@ -1,6 +1,8 @@
1
- import { DefinitionSchema, PaginationOptions, Session } from "@dsqlbase/core";
2
- import { QueryClient } from "./database/index.js";
3
- export type ClientOptions<TSchema extends DefinitionSchema, TEnforce extends boolean = true> = {
1
+ import { DefinitionSchema, Session } from "@dsqlbase/core";
2
+ import { WithModels } from "./clients/base.js";
3
+ import { DsqlClient } from "./clients/dsql.js";
4
+ import { PaginationOptions } from "./normalizers/pagination.js";
5
+ export type ClientOptions<TSchema extends DefinitionSchema> = {
4
6
  /**
5
7
  * Definition schema for the database, including relations configuration.
6
8
  */
@@ -10,19 +12,13 @@ export type ClientOptions<TSchema extends DefinitionSchema, TEnforce extends boo
10
12
  */
11
13
  session: Session;
12
14
  /**
13
- * How tables declared inside a `tenantScope()` behave on this client.
14
- *
15
- * With `enforce` on, which is the default, they are absent from the base client's type and
16
- * refuse to build a query until the client is scoped with `$identityClaims`. Set it to
17
- * `false` for a process that is meant to run unscoped — an internal worker rather than a
18
- * request handler — and every tenant table becomes readable across tenants.
19
- *
20
- * It is decided here, per client, rather than per call: a per-call escape hatch is a thing to
21
- * forget, and the processes that need one are separate deployments anyway. Inserting into a
22
- * tenant table still requires claims in both modes.
15
+ * `bypass: true` skips every row policy on this client and every client derived from it —
16
+ * no predicates on reads, no checks on writes. The `BYPASSRLS` analogue, for a worker or a
17
+ * migration script rather than a request handler. Decided per client, never per call: a
18
+ * per-call escape hatch is a thing to forget.
23
19
  */
24
- tenancy?: {
25
- enforce?: TEnforce;
20
+ policies?: {
21
+ bypass?: boolean;
26
22
  };
27
23
  /**
28
24
  * Page sizes for `paginate`. `defaultLimit` is used when a call names no `limit` (100 when
@@ -30,8 +26,9 @@ export type ClientOptions<TSchema extends DefinitionSchema, TEnforce extends boo
30
26
  */
31
27
  pagination?: PaginationOptions;
32
28
  };
29
+ export type DsqlQueryClient<TSchema extends DefinitionSchema> = WithModels<TSchema, DsqlClient<TSchema>>;
33
30
  /**
34
- * Creates a new client instance for interacting with the database using the provided schema definition.
31
+ * Creates a new {@link DsqlClient} instance for interacting with the database using the provided schema definition.
35
32
  *
36
33
  * @example
37
34
  * ```ts
@@ -86,8 +83,8 @@ export type ClientOptions<TSchema extends DefinitionSchema, TEnforce extends boo
86
83
  * @param options - Configuration options for the client, including the schema definition.
87
84
  * @returns A new instance of the DSQL client configured with the provided schema.
88
85
  * @throws Will throw an error if the schema definition is invalid or if there are issues initializing the client.
86
+ *
89
87
  * @see {@link ClientOptions} for more details on the configuration options.
90
- * @see {@link ModelClient} for information on the methods available for interacting with the database tables.
91
88
  */
92
- export declare function createClient<TSchema extends DefinitionSchema, TEnforce extends boolean = true>(options: ClientOptions<TSchema, TEnforce>): QueryClient<TSchema, TEnforce>;
89
+ export declare function createClient<TSchema extends DefinitionSchema>(options: ClientOptions<TSchema>): WithModels<TSchema, DsqlClient<TSchema>>;
93
90
  //# sourceMappingURL=create.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../../src/client/create.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAEhB,iBAAiB,EAGjB,OAAO,EACR,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EAAkB,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElE,MAAM,MAAM,aAAa,CACvB,OAAO,SAAS,gBAAgB,EAChC,QAAQ,SAAS,OAAO,GAAG,IAAI,IAC7B;IACF;;OAEG;IACH,MAAM,EAAE,OAAO,CAAC;IAEhB;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB;;;;;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Y,CAAC,OAAO,SAAS,gBAAgB,EAC3D,OAAO,EAAE,aAAa,CAAC,OAAO,CAAC,GAC9B,UAAU,CAAC,OAAO,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC,CAe1C"}
@@ -1,20 +1,9 @@
1
- import { ExecutionContext, QueryBuilder, SchemaRegistry, } from "@dsqlbase/core";
2
- import { attachModels } from "./database/base.js";
3
- import { registerNodes } from "./nodes.js";
4
- import { DatabaseClient } from "./database/index.js";
5
- function assertPagination(options) {
6
- for (const [name, value] of Object.entries(options ?? {})) {
7
- if (value !== undefined && (!Number.isInteger(value) || value <= 0)) {
8
- throw new Error(`pagination.${name} must be a positive integer (got ${String(value)}).`);
9
- }
10
- }
11
- const { defaultLimit, maxLimit } = options ?? {};
12
- if (defaultLimit !== undefined && maxLimit !== undefined && defaultLimit > maxLimit) {
13
- throw new Error(`pagination.defaultLimit (${defaultLimit}) exceeds pagination.maxLimit (${maxLimit}).`);
14
- }
15
- }
1
+ import { QueryBuilder, SchemaRegistry } from "@dsqlbase/core";
2
+ import { withModels } from "./clients/base.js";
3
+ import { DsqlClient } from "./clients/dsql.js";
4
+ import { ClientContext } from "./normalizers/context.js";
16
5
  /**
17
- * Creates a new client instance for interacting with the database using the provided schema definition.
6
+ * Creates a new {@link DsqlClient} instance for interacting with the database using the provided schema definition.
18
7
  *
19
8
  * @example
20
9
  * ```ts
@@ -69,26 +58,20 @@ function assertPagination(options) {
69
58
  * @param options - Configuration options for the client, including the schema definition.
70
59
  * @returns A new instance of the DSQL client configured with the provided schema.
71
60
  * @throws Will throw an error if the schema definition is invalid or if there are issues initializing the client.
61
+ *
72
62
  * @see {@link ClientOptions} for more details on the configuration options.
73
- * @see {@link ModelClient} for information on the methods available for interacting with the database tables.
74
63
  */
75
64
  export function createClient(options) {
76
- assertPagination(options.pagination);
77
65
  const schema = new SchemaRegistry(options.schema);
78
66
  const dialect = new QueryBuilder();
79
- // Resolves every guid() column against the tables it can reach. Here rather than inside
80
- // `SchemaRegistry`, because the shape of an id is a client concern: core sees a uuid
81
- // column with a codec, the same way it sees an interval column with a Duration codec.
82
- registerNodes(schema, options.schema);
83
- const context = new ExecutionContext({
67
+ const context = new ClientContext({
84
68
  schema,
85
69
  dialect,
86
70
  session: options.session,
87
- tenancy: options.tenancy,
71
+ policies: options.policies,
88
72
  pagination: options.pagination,
89
73
  });
90
- const dbClient = new DatabaseClient(context);
91
- attachModels(dbClient, context);
92
- return dbClient;
74
+ const client = new DsqlClient(context);
75
+ return withModels(client);
93
76
  }
94
77
  //# sourceMappingURL=create.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"create.js","sourceRoot":"","sources":["../../src/client/create.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,gBAAgB,EAEhB,YAAY,EACZ,cAAc,GAEf,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAe,MAAM,qBAAqB,CAAC;AAuClE,SAAS,gBAAgB,CAAC,OAAsC;IAC9D,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC,EAAE,CAAC;QAC1D,IAAI,KAAK,KAAK,SAAS,IAAI,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,EAAE,CAAC;YACpE,MAAM,IAAI,KAAK,CAAC,cAAc,IAAI,oCAAoC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC3F,CAAC;IACH,CAAC;IAED,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;IAEjD,IAAI,YAAY,KAAK,SAAS,IAAI,QAAQ,KAAK,SAAS,IAAI,YAAY,GAAG,QAAQ,EAAE,CAAC;QACpF,MAAM,IAAI,KAAK,CACb,4BAA4B,YAAY,kCAAkC,QAAQ,IAAI,CACvF,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AAEH,MAAM,UAAU,YAAY,CAC1B,OAAyC;IAEzC,gBAAgB,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IAErC,MAAM,MAAM,GAAG,IAAI,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAClD,MAAM,OAAO,GAAG,IAAI,YAAY,EAAE,CAAC;IAEnC,wFAAwF;IACxF,qFAAqF;IACrF,sFAAsF;IACtF,aAAa,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IAEtC,MAAM,OAAO,GAAG,IAAI,gBAAgB,CAAC;QACnC,MAAM;QACN,OAAO;QACP,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,UAAU,EAAE,OAAO,CAAC,UAAU;KAC/B,CAAC,CAAC;IAEH,MAAM,QAAQ,GAAG,IAAI,cAAc,CAAC,OAAO,CAAC,CAAC;IAC7C,YAAY,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAEhC,OAAO,QAAqD,CAAC;AAC/D,CAAC","sourcesContent":["import {\n DefinitionSchema,\n ExecutionContext,\n PaginationOptions,\n QueryBuilder,\n SchemaRegistry,\n Session,\n} from \"@dsqlbase/core\";\nimport { attachModels } from \"./database/base.js\";\nimport { registerNodes } from \"./nodes.js\";\nimport { DatabaseClient, QueryClient } from \"./database/index.js\";\n\nexport type ClientOptions<\n TSchema extends DefinitionSchema,\n TEnforce extends boolean = true,\n> = {\n /**\n * Definition schema for the database, including relations configuration.\n */\n schema: TSchema;\n\n /**\n * Session object for managing database connections and transactions.\n */\n session: Session;\n\n /**\n * How tables declared inside a `tenantScope()` behave on this client.\n *\n * With `enforce` on, which is the default, they are absent from the base client's type and\n * refuse to build a query until the client is scoped with `$identityClaims`. Set it to\n * `false` for a process that is meant to run unscoped — an internal worker rather than a\n * request handler — and every tenant table becomes readable across tenants.\n *\n * It is decided here, per client, rather than per call: a per-call escape hatch is a thing to\n * forget, and the processes that need one are separate deployments anyway. Inserting into a\n * tenant table still requires claims in both modes.\n */\n tenancy?: {\n enforce?: TEnforce;\n };\n\n /**\n * Page sizes for `paginate`. `defaultLimit` is used when a call names no `limit` (100 when\n * unset); `maxLimit`, when set, refuses any larger `limit` a call asks for.\n */\n pagination?: PaginationOptions;\n};\n\nfunction assertPagination(options: PaginationOptions | undefined) {\n for (const [name, value] of Object.entries(options ?? {})) {\n if (value !== undefined && (!Number.isInteger(value) || value <= 0)) {\n throw new Error(`pagination.${name} must be a positive integer (got ${String(value)}).`);\n }\n }\n\n const { defaultLimit, maxLimit } = options ?? {};\n\n if (defaultLimit !== undefined && maxLimit !== undefined && defaultLimit > maxLimit) {\n throw new Error(\n `pagination.defaultLimit (${defaultLimit}) exceeds pagination.maxLimit (${maxLimit}).`\n );\n }\n}\n\n/**\n * Creates a new client instance for interacting with the database using the provided schema definition.\n *\n * @example\n * ```ts\n * // schema.ts\n * import { table, text, uuid } from \"@dsqlbase/schema\";\n *\n * export const users = table(\"users\", {\n * id: uuid(\"id\").primaryKey().defaultRandom(),\n * firstName: text(\"first_name\").notNull(),\n * lastName: text(\"last_name\").notNull(),\n * emailAddress: text(\"email_address\").notNull().unique(),\n * });\n *\n * export const posts = table(\"posts\", {\n * id: uuid(\"id\").primaryKey().defaultRandom(),\n * title: text(\"title\").notNull(),\n * description: text(\"description\"),\n * content: text(\"content\").notNull(),\n * });\n *\n * export const userRelations = relations(users, {\n * posts: hasMany(posts, {\n * from: [users.columns.id],\n * to: [posts.columns.userId],\n * }),\n * });\n *\n * export const postRelations = relations(posts, {\n * owner: belongsTo(users, {\n * from: [posts.columns.userId],\n * to: [users.columns.id],\n * }),\n * });\n *\n * // client.ts\n * import { createClient } from \"dsqlbase\";\n * import * as schema from \"./schema\";\n *\n * export const dsql = createClient({ schema });\n *\n * // Example usage\n * import { dsql } from \"./client\";\n *\n * const newUser = await dsql.users.create({\n * data: {\n * firstName: \"John\",\n * lastName: \"Doe\",\n * emailAddress: \"john.doe@example.com\",\n * },\n * });\n * ```\n * @param options - Configuration options for the client, including the schema definition.\n * @returns A new instance of the DSQL client configured with the provided schema.\n * @throws Will throw an error if the schema definition is invalid or if there are issues initializing the client.\n * @see {@link ClientOptions} for more details on the configuration options.\n * @see {@link ModelClient} for information on the methods available for interacting with the database tables.\n */\n\nexport function createClient<TSchema extends DefinitionSchema, TEnforce extends boolean = true>(\n options: ClientOptions<TSchema, TEnforce>\n): QueryClient<TSchema, TEnforce> {\n assertPagination(options.pagination);\n\n const schema = new SchemaRegistry(options.schema);\n const dialect = new QueryBuilder();\n\n // Resolves every guid() column against the tables it can reach. Here rather than inside\n // `SchemaRegistry`, because the shape of an id is a client concern: core sees a uuid\n // column with a codec, the same way it sees an interval column with a Duration codec.\n registerNodes(schema, options.schema);\n\n const context = new ExecutionContext({\n schema,\n dialect,\n session: options.session,\n tenancy: options.tenancy,\n pagination: options.pagination,\n });\n\n const dbClient = new DatabaseClient(context);\n attachModels(dbClient, context);\n\n return dbClient as unknown as QueryClient<TSchema, TEnforce>;\n}\n"]}
1
+ {"version":3,"file":"create.js","sourceRoot":"","sources":["../../src/client/create.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,YAAY,EAAE,cAAc,EAAW,MAAM,gBAAgB,CAAC;AACzF,OAAO,EAAE,UAAU,EAAc,MAAM,mBAAmB,CAAC;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE/C,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAiCzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AAEH,MAAM,UAAU,YAAY,CAC1B,OAA+B;IAE/B,MAAM,MAAM,GAAG,IAAI,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAClD,MAAM,OAAO,GAAG,IAAI,YAAY,EAAE,CAAC;IAEnC,MAAM,OAAO,GAAG,IAAI,aAAa,CAAC;QAChC,MAAM;QACN,OAAO;QACP,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,QAAQ,EAAE,OAAO,CAAC,QAAQ;QAC1B,UAAU,EAAE,OAAO,CAAC,UAAU;KAC/B,CAAC,CAAC;IAEH,MAAM,MAAM,GAAG,IAAI,UAAU,CAAU,OAAO,CAAC,CAAC;IAEhD,OAAO,UAAU,CAA+B,MAAM,CAAC,CAAC;AAC1D,CAAC","sourcesContent":["import { DefinitionSchema, QueryBuilder, SchemaRegistry, Session } from \"@dsqlbase/core\";\nimport { withModels, WithModels } from \"./clients/base.js\";\nimport { DsqlClient } from \"./clients/dsql.js\";\nimport { PaginationOptions } from \"./normalizers/pagination.js\";\nimport { ClientContext } from \"./normalizers/context.js\";\n\nexport type ClientOptions<TSchema extends DefinitionSchema> = {\n /**\n * Definition schema for the database, including relations configuration.\n */\n schema: TSchema;\n\n /**\n * Session object for managing database connections and transactions.\n */\n session: Session;\n\n /**\n * `bypass: true` skips every row policy on this client and every client derived from it —\n * no predicates on reads, no checks on writes. The `BYPASSRLS` analogue, for a worker or a\n * migration script rather than a request handler. Decided per client, never per call: a\n * per-call escape hatch is a thing to forget.\n */\n policies?: { bypass?: boolean };\n\n /**\n * Page sizes for `paginate`. `defaultLimit` is used when a call names no `limit` (100 when\n * unset); `maxLimit`, when set, refuses any larger `limit` a call asks for.\n */\n pagination?: PaginationOptions;\n};\n\nexport type DsqlQueryClient<TSchema extends DefinitionSchema> = WithModels<\n TSchema,\n DsqlClient<TSchema>\n>;\n\n/**\n * Creates a new {@link DsqlClient} instance for interacting with the database using the provided schema definition.\n *\n * @example\n * ```ts\n * // schema.ts\n * import { table, text, uuid } from \"@dsqlbase/schema\";\n *\n * export const users = table(\"users\", {\n * id: uuid(\"id\").primaryKey().defaultRandom(),\n * firstName: text(\"first_name\").notNull(),\n * lastName: text(\"last_name\").notNull(),\n * emailAddress: text(\"email_address\").notNull().unique(),\n * });\n *\n * export const posts = table(\"posts\", {\n * id: uuid(\"id\").primaryKey().defaultRandom(),\n * title: text(\"title\").notNull(),\n * description: text(\"description\"),\n * content: text(\"content\").notNull(),\n * });\n *\n * export const userRelations = relations(users, {\n * posts: hasMany(posts, {\n * from: [users.columns.id],\n * to: [posts.columns.userId],\n * }),\n * });\n *\n * export const postRelations = relations(posts, {\n * owner: belongsTo(users, {\n * from: [posts.columns.userId],\n * to: [users.columns.id],\n * }),\n * });\n *\n * // client.ts\n * import { createClient } from \"dsqlbase\";\n * import * as schema from \"./schema\";\n *\n * export const dsql = createClient({ schema });\n *\n * // Example usage\n * import { dsql } from \"./client\";\n *\n * const newUser = await dsql.users.create({\n * data: {\n * firstName: \"John\",\n * lastName: \"Doe\",\n * emailAddress: \"john.doe@example.com\",\n * },\n * });\n * ```\n * @param options - Configuration options for the client, including the schema definition.\n * @returns A new instance of the DSQL client configured with the provided schema.\n * @throws Will throw an error if the schema definition is invalid or if there are issues initializing the client.\n *\n * @see {@link ClientOptions} for more details on the configuration options.\n */\n\nexport function createClient<TSchema extends DefinitionSchema>(\n options: ClientOptions<TSchema>\n): WithModels<TSchema, DsqlClient<TSchema>> {\n const schema = new SchemaRegistry(options.schema);\n const dialect = new QueryBuilder();\n\n const context = new ClientContext({\n schema,\n dialect,\n session: options.session,\n policies: options.policies,\n pagination: options.pagination,\n });\n\n const client = new DsqlClient<TSchema>(context);\n\n return withModels<TSchema, DsqlClient<TSchema>>(client);\n}\n"]}
@@ -1,9 +1,5 @@
1
- export type * from "./model/base.js";
2
- export { isFilterType, type ColumnFilterOf, type FilterCondition, type FilterOf, type WhereExpressionOf, } from "./model/filters.js";
3
- export { ModelClient } from "./model/client.js";
4
- export { UnionClient } from "./union/client.js";
5
- export { DatabaseClient, type Aliases, type ClaimsOf, type IdentityClient, type Models, type QueryClient, type UnionAliases, type VisibleAliases, type VisibleFor, } from "./database/index.js";
6
- export { TransactionClient, type TxClient } from "./transaction/index.js";
7
- export { createClient, type ClientOptions } from "./create.js";
8
- export { getNodes, type GuidBinding, type NodeTable } from "./nodes.js";
1
+ export * from "./clients/index.js";
2
+ export * from "./normalizers/index.js";
3
+ export { type OCCRetryOptions, backoffDelay, isOccError, sleep } from "./utils/occ-retry.js";
4
+ export { createClient, type DsqlQueryClient, type ClientOptions } from "./create.js";
9
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,mBAAmB,iBAAiB,CAAC;AACrC,OAAO,EACL,YAAY,EACZ,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,QAAQ,EACb,KAAK,iBAAiB,GACvB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EACL,cAAc,EACd,KAAK,OAAO,EACZ,KAAK,QAAQ,EACb,KAAK,cAAc,EACnB,KAAK,MAAM,EACX,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,cAAc,EACnB,KAAK,UAAU,GAChB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,KAAK,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAE,QAAQ,EAAE,KAAK,WAAW,EAAE,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,OAAO,EAAE,KAAK,eAAe,EAAE,YAAY,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC7F,OAAO,EAAE,YAAY,EAAE,KAAK,eAAe,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC"}
@@ -1,8 +1,5 @@
1
- export { isFilterType, } from "./model/filters.js";
2
- export { ModelClient } from "./model/client.js";
3
- export { UnionClient } from "./union/client.js";
4
- export { DatabaseClient, } from "./database/index.js";
5
- export { TransactionClient } from "./transaction/index.js";
1
+ export * from "./clients/index.js";
2
+ export * from "./normalizers/index.js";
3
+ export { backoffDelay, isOccError, sleep } from "./utils/occ-retry.js";
6
4
  export { createClient } from "./create.js";
7
- export { getNodes } from "./nodes.js";
8
5
  //# sourceMappingURL=index.js.map