better-supabase 0.0.0 → 0.1.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 (154) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +88 -1
  3. package/bin/better-supabase.js +3 -0
  4. package/dist/casing-da0uRTqt.js +14 -0
  5. package/dist/cli/bin.d.ts +1 -0
  6. package/dist/cli/bin.js +15 -0
  7. package/dist/cli/index.d.ts +226 -0
  8. package/dist/cli/index.js +3 -0
  9. package/dist/cli-CoByWYja.js +6472 -0
  10. package/dist/client/index.d.ts +2 -0
  11. package/dist/client/index.js +112 -0
  12. package/dist/compiler-DL18ukPe.d.ts +14 -0
  13. package/dist/config/index.d.ts +3 -0
  14. package/dist/config/index.js +3 -0
  15. package/dist/config-D-aR2ZoN.js +276 -0
  16. package/dist/define-CTfWw-SS.d.ts +662 -0
  17. package/dist/define-QGkq04oe.js +1068 -0
  18. package/dist/edge/index.d.ts +43 -0
  19. package/dist/edge/index.js +103 -0
  20. package/dist/entitlements-BBg61DQZ.d.ts +20 -0
  21. package/dist/entitlements-BC_khwRy.js +17 -0
  22. package/dist/env/index.d.ts +2 -0
  23. package/dist/env/index.js +2 -0
  24. package/dist/env-BEToab0I.js +204 -0
  25. package/dist/errors-Bl3rkOzo.js +196 -0
  26. package/dist/events/index.d.ts +2 -0
  27. package/dist/events/index.js +2 -0
  28. package/dist/events-2OJsVG_d.d.ts +84 -0
  29. package/dist/events-BuzTgn2D.js +135 -0
  30. package/dist/executor-Bazhgsgq.d.ts +195 -0
  31. package/dist/executor-Bje4suQF.d.ts +25 -0
  32. package/dist/executor-Ceq60hPM.js +89 -0
  33. package/dist/hono/index.d.ts +45 -0
  34. package/dist/hono/index.js +70 -0
  35. package/dist/index-BD6wKthp.d.ts +597 -0
  36. package/dist/index-DQVvHzjW.d.ts +74 -0
  37. package/dist/index-DRid0qIv.d.ts +76 -0
  38. package/dist/index-Dup-nSe9.d.ts +117 -0
  39. package/dist/index-Dy6kuF0P.d.ts +71 -0
  40. package/dist/index-Hf8PP9sP.d.ts +116 -0
  41. package/dist/index-JXHBuS53.d.ts +144 -0
  42. package/dist/index-Nxo3TJcm.d.ts +208 -0
  43. package/dist/index-Sw43J8HG.d.ts +62 -0
  44. package/dist/index-cEeweTeM.d.ts +38 -0
  45. package/dist/index.d.ts +110 -0
  46. package/dist/index.js +42 -0
  47. package/dist/invalidate-DbxuL2jL.js +18 -0
  48. package/dist/jobs/index.d.ts +173 -0
  49. package/dist/jobs/index.js +477 -0
  50. package/dist/json-schema-BbwO2mZg.js +181 -0
  51. package/dist/lint/index.d.ts +75 -0
  52. package/dist/lint/index.js +167 -0
  53. package/dist/list/index.d.ts +2 -0
  54. package/dist/list/index.js +410 -0
  55. package/dist/live-6b9i72Ux.d.ts +180 -0
  56. package/dist/live-D8rV1Ldw.js +168 -0
  57. package/dist/mcp/index.d.ts +105 -0
  58. package/dist/mcp/index.js +330 -0
  59. package/dist/mfa-CpogP66z.js +521 -0
  60. package/dist/mfa-TVia554c.d.ts +44 -0
  61. package/dist/next/image/index.d.ts +34 -0
  62. package/dist/next/image/index.js +40 -0
  63. package/dist/next/index.d.ts +210 -0
  64. package/dist/next/index.js +426 -0
  65. package/dist/openapi/index.d.ts +2 -0
  66. package/dist/openapi/index.js +332 -0
  67. package/dist/orpc/index.d.ts +46 -0
  68. package/dist/orpc/index.js +50 -0
  69. package/dist/otel/index.d.ts +40 -0
  70. package/dist/otel/index.js +168 -0
  71. package/dist/path-Cv__-OTj.d.ts +17 -0
  72. package/dist/plugin-BqR6wKMB.js +23 -0
  73. package/dist/plugin-HvGqCurB.d.ts +118 -0
  74. package/dist/plugins/actor/index.d.ts +13 -0
  75. package/dist/plugins/actor/index.js +43 -0
  76. package/dist/plugins/rules/index.d.ts +88 -0
  77. package/dist/plugins/rules/index.js +174 -0
  78. package/dist/plugins/soft-delete/index.d.ts +38 -0
  79. package/dist/plugins/soft-delete/index.js +69 -0
  80. package/dist/plugins/tenant/index.d.ts +43 -0
  81. package/dist/plugins/tenant/index.js +73 -0
  82. package/dist/plugins/timestamps/index.d.ts +9 -0
  83. package/dist/plugins/timestamps/index.js +37 -0
  84. package/dist/plugins/validation/index.d.ts +24 -0
  85. package/dist/plugins/validation/index.js +59 -0
  86. package/dist/pool-BFeqzwQS.d.ts +37 -0
  87. package/dist/postgres/index.d.ts +28 -0
  88. package/dist/postgres/index.js +79 -0
  89. package/dist/postgrest-Dm-vhY6R.d.ts +43 -0
  90. package/dist/problem-DPu6Fh-t.js +97 -0
  91. package/dist/problem-DlgB0lYl.d.ts +47 -0
  92. package/dist/query/index.d.ts +2 -0
  93. package/dist/query/index.js +3 -0
  94. package/dist/query-ChvDbsDE.js +186 -0
  95. package/dist/react/index.d.ts +7 -0
  96. package/dist/react/index.js +5 -0
  97. package/dist/react/server.d.ts +13 -0
  98. package/dist/react/server.js +25 -0
  99. package/dist/react/session.d.ts +2 -0
  100. package/dist/react/session.js +3 -0
  101. package/dist/react-CrOvrnIP.js +241 -0
  102. package/dist/read-set-DhX2d04c.js +216 -0
  103. package/dist/realtime/index.d.ts +3 -0
  104. package/dist/realtime/index.js +253 -0
  105. package/dist/resolve-DW8h9NPm.d.ts +155 -0
  106. package/dist/resource-CYiu6Ygi.js +175 -0
  107. package/dist/resource-SkPTRJDD.d.ts +65 -0
  108. package/dist/respond-4-dhoV7x.js +380 -0
  109. package/dist/respond-PoTpy5DT.d.ts +169 -0
  110. package/dist/result-CTkCZ6JA.d.ts +198 -0
  111. package/dist/result-DWXatkd6.js +103 -0
  112. package/dist/scope-CDHcS7dE.js +90 -0
  113. package/dist/seed-CzecODcJ.js +101 -0
  114. package/dist/server/index.d.ts +42 -0
  115. package/dist/server/index.js +68 -0
  116. package/dist/session-C3osL21k.d.ts +21 -0
  117. package/dist/session-CD-3orO_.js +19 -0
  118. package/dist/shared-_6-uPL7E.js +57 -0
  119. package/dist/simplify-oHiDKRVQ.js +665 -0
  120. package/dist/spec-pins-CJPVghHy.d.ts +20 -0
  121. package/dist/spec-pins-t6sPaHvl.js +20 -0
  122. package/dist/sql-M8l3fnS0.js +260 -0
  123. package/dist/ssr/index.d.ts +5 -0
  124. package/dist/ssr/index.js +4 -0
  125. package/dist/standard-CyhRzctb.d.ts +7 -0
  126. package/dist/standard-FUi3Z3-7.js +21 -0
  127. package/dist/stats-3iy8u3_P.js +566 -0
  128. package/dist/storage/index.d.ts +4 -0
  129. package/dist/storage/index.js +2 -0
  130. package/dist/storage-BWXVUXGk.js +461 -0
  131. package/dist/tables-rZxRxNPW.js +59 -0
  132. package/dist/template-BeOGnynM.js +93 -0
  133. package/dist/template-mbvlysOo.d.ts +6 -0
  134. package/dist/testing/index.d.ts +294 -0
  135. package/dist/testing/index.js +751 -0
  136. package/dist/types-6cYK9pbJ.js +29 -0
  137. package/dist/types-C2fslP1z.d.ts +195 -0
  138. package/dist/version-Cqq1m3UY.js +4 -0
  139. package/dist/view-B0ltUmTi.d.ts +43 -0
  140. package/dist/view-B6F9QbG-.js +35 -0
  141. package/dist/webhooks/index.d.ts +152 -0
  142. package/dist/webhooks/index.js +2 -0
  143. package/dist/webhooks-DbMTJJX2.js +139 -0
  144. package/package.json +246 -17
  145. package/schemas/config-v1.json +328 -0
  146. package/schemas/doctor-report-v1.json +105 -0
  147. package/schemas/snapshot-v2.json +1002 -0
  148. package/skills/better-supabase/SKILL.md +84 -0
  149. package/skills/better-supabase/references/plugins.md +56 -0
  150. package/skills/better-supabase/references/troubleshooting.md +39 -0
  151. package/skills/better-supabase-api/SKILL.md +67 -0
  152. package/skills/better-supabase-api/references/adapters.md +108 -0
  153. package/skills/better-supabase-testing/SKILL.md +65 -0
  154. package/index.js +0 -1
@@ -0,0 +1,566 @@
1
+ import { c as mapDbError, n as dbError } from "./errors-Bl3rkOzo.js";
2
+ import { a as invalidRequest, n as simplifyOrFalse } from "./simplify-oHiDKRVQ.js";
3
+ import { i as ok, n as err, o as toDbError } from "./result-DWXatkd6.js";
4
+ //#region src/compile/postgrest.ts
5
+ function scalar(value) {
6
+ if (value instanceof Date) return value.toISOString();
7
+ if (typeof value === "string") return value;
8
+ if (typeof value === "number" || typeof value === "bigint" || typeof value === "boolean") return String(value);
9
+ if (value === null) return "null";
10
+ return JSON.stringify(value);
11
+ }
12
+ /** Double-quotes a value for PostgREST lists and logic trees. */
13
+ function quote(value) {
14
+ return `"${scalar(value).replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
15
+ }
16
+ function list(values, open, close) {
17
+ return `${open}${values.map(quote).join(",")}${close}`;
18
+ }
19
+ function isBoolLike(value) {
20
+ return value === null || typeof value === "boolean";
21
+ }
22
+ function operatorAndValue(condition, inLogic) {
23
+ const { op, value } = condition;
24
+ const plain = (text) => inLogic ? quote(text) : scalar(text);
25
+ switch (op) {
26
+ case "eq":
27
+ case "neq":
28
+ case "gt":
29
+ case "gte":
30
+ case "lt":
31
+ case "lte":
32
+ case "like":
33
+ case "ilike": return {
34
+ operator: op,
35
+ value: plain(value)
36
+ };
37
+ case "in":
38
+ if (!Array.isArray(value)) invalidRequest("\"in\" needs an array");
39
+ return {
40
+ operator: "in",
41
+ value: list(value, "(", ")")
42
+ };
43
+ case "is":
44
+ if (!isBoolLike(value)) invalidRequest("\"is\" needs null, true or false");
45
+ return {
46
+ operator: "is",
47
+ value: String(value)
48
+ };
49
+ case "contains":
50
+ case "containedBy":
51
+ case "overlaps": {
52
+ const operator = op === "contains" ? "cs" : op === "containedBy" ? "cd" : "ov";
53
+ if (Array.isArray(value)) return {
54
+ operator,
55
+ value: list(value, "{", "}")
56
+ };
57
+ const json = JSON.stringify(value);
58
+ return {
59
+ operator,
60
+ value: inLogic ? quote(json) : json
61
+ };
62
+ }
63
+ case "fts": return {
64
+ operator: condition.config ? `wfts(${condition.config})` : "wfts",
65
+ value: plain(value)
66
+ };
67
+ default: return op;
68
+ }
69
+ }
70
+ var PostgrestCompiler = class {
71
+ filters = [];
72
+ orders = [];
73
+ limits = [];
74
+ #counter = 0;
75
+ nextAlias() {
76
+ this.#counter += 1;
77
+ return `_bs${this.#counter}`;
78
+ }
79
+ /** Compiles a condition at `path` where top-level items are ANDed. */
80
+ andContext(condition, path, embeds) {
81
+ switch (condition.kind) {
82
+ case "and":
83
+ for (const item of condition.items) this.andContext(item, path, embeds);
84
+ return;
85
+ case "column": {
86
+ const { operator, value } = operatorAndValue(condition, false);
87
+ this.filters.push({
88
+ kind: "filter",
89
+ path: path ? `${path}.${condition.column}` : condition.column,
90
+ operator,
91
+ value
92
+ });
93
+ return;
94
+ }
95
+ case "not": {
96
+ const inner = condition.item;
97
+ if (inner.kind === "column") {
98
+ const { operator, value } = operatorAndValue(inner, false);
99
+ this.filters.push({
100
+ kind: "filter",
101
+ path: path ? `${path}.${inner.column}` : inner.column,
102
+ operator: `not.${operator}`,
103
+ value
104
+ });
105
+ return;
106
+ }
107
+ if (inner.kind === "relation") {
108
+ this.relationAnd(negateRelation(inner), path, embeds);
109
+ return;
110
+ }
111
+ if (inner.kind === "not") {
112
+ this.andContext(inner.item, path, embeds);
113
+ return;
114
+ }
115
+ this.filters.push({
116
+ kind: "or",
117
+ expression: this.logic(condition, embeds),
118
+ referencedTable: path
119
+ });
120
+ return;
121
+ }
122
+ case "or":
123
+ this.filters.push({
124
+ kind: "or",
125
+ expression: this.logic(condition, embeds).replace(/^or\(/, "").replace(/\)$/, ""),
126
+ referencedTable: path
127
+ });
128
+ return;
129
+ case "relation":
130
+ this.relationAnd(condition, path, embeds);
131
+ return;
132
+ default: return condition;
133
+ }
134
+ }
135
+ relationAnd(condition, path, embeds) {
136
+ if (condition.quantifier === "every" && !condition.where) return;
137
+ const alias = this.nextAlias();
138
+ const aliasPath = path ? `${path}.${alias}` : alias;
139
+ const node = filterEmbed(alias, condition, condition.quantifier === "some");
140
+ this.withPath(node.children, aliasPath);
141
+ embeds.push(node);
142
+ const where = condition.quantifier === "every" && condition.where ? {
143
+ kind: "not",
144
+ item: condition.where
145
+ } : condition.where;
146
+ if (where) this.andContext(where, aliasPath, node.children);
147
+ if (condition.quantifier !== "some") this.filters.push({
148
+ kind: "filter",
149
+ path: aliasPath,
150
+ operator: "is",
151
+ value: "null"
152
+ });
153
+ }
154
+ /** Compiles a condition into a PostgREST logic-tree term. */
155
+ logic(condition, embeds) {
156
+ switch (condition.kind) {
157
+ case "and":
158
+ case "or": return `${condition.kind}(${condition.items.map((item) => this.logic(item, embeds)).join(",")})`;
159
+ case "not": {
160
+ const inner = condition.item;
161
+ if (inner.kind === "column") {
162
+ const { operator, value } = operatorAndValue(inner, true);
163
+ return `${inner.column}.not.${operator}.${value}`;
164
+ }
165
+ if (inner.kind === "relation") return this.logic(negateRelation(inner), embeds);
166
+ if (inner.kind === "not") return this.logic(inner.item, embeds);
167
+ return `not.${this.logic(inner, embeds)}`;
168
+ }
169
+ case "column": {
170
+ const { operator, value } = operatorAndValue(condition, true);
171
+ return `${condition.column}.${operator}.${value}`;
172
+ }
173
+ case "relation": {
174
+ if (condition.quantifier === "every" && !condition.where) invalidRequest("every({}) must be simplified before compiling");
175
+ const alias = this.nextAlias();
176
+ const node = filterEmbed(alias, condition, false);
177
+ const nodePath = this.pathOf(alias, embeds);
178
+ this.withPath(node.children, nodePath);
179
+ embeds.push(node);
180
+ const where = condition.quantifier === "every" && condition.where ? {
181
+ kind: "not",
182
+ item: condition.where
183
+ } : condition.where;
184
+ if (where) this.andContext(where, nodePath, node.children);
185
+ return condition.quantifier === "some" ? `${alias}.not.is.null` : `${alias}.is.null`;
186
+ }
187
+ default: return condition;
188
+ }
189
+ }
190
+ #paths = /* @__PURE__ */ new WeakMap();
191
+ withPath(embeds, path) {
192
+ this.#paths.set(embeds, path);
193
+ return embeds;
194
+ }
195
+ pathOf(alias, embeds) {
196
+ const parent = this.#paths.get(embeds);
197
+ return parent ? `${parent}.${alias}` : alias;
198
+ }
199
+ include(include, path, embeds) {
200
+ const aliasPath = path ? `${path}.${include.alias}` : include.alias;
201
+ const node = {
202
+ alias: include.alias,
203
+ target: include.target,
204
+ relation: include.relation,
205
+ inner: include.required,
206
+ columns: include.selection.columns,
207
+ children: this.withPath([], aliasPath),
208
+ ...include.count === void 0 ? {} : { count: true },
209
+ ...include.selection.aggregate ? { measures: include.selection.aggregate.measures } : {}
210
+ };
211
+ embeds.push(node);
212
+ for (const child of include.selection.includes) this.include(child, aliasPath, node.children);
213
+ const where = simplifyOrFalse(include.where);
214
+ if (where.never) {
215
+ const [first] = include.target.primaryKey;
216
+ const column = first ? include.target.columns[first]?.db : void 0;
217
+ if (!column) invalidRequest(`Cannot filter "${include.alias}" to nothing`);
218
+ this.filters.push({
219
+ kind: "filter",
220
+ path: `${aliasPath}.${column}`,
221
+ operator: "is",
222
+ value: "null"
223
+ });
224
+ this.filters.push({
225
+ kind: "filter",
226
+ path: `${aliasPath}.${column}`,
227
+ operator: "not.is",
228
+ value: "null"
229
+ });
230
+ } else if (where.condition) this.andContext(where.condition, aliasPath, node.children);
231
+ for (const term of include.orderBy) this.order(term, aliasPath);
232
+ if (include.limit !== void 0) this.limits.push({
233
+ count: include.limit,
234
+ referencedTable: aliasPath
235
+ });
236
+ }
237
+ order(term, referencedTable) {
238
+ this.orders.push({
239
+ column: term.column,
240
+ ascending: term.direction === "asc",
241
+ nullsFirst: term.nulls === void 0 ? void 0 : term.nulls === "first",
242
+ referencedTable
243
+ });
244
+ }
245
+ };
246
+ function negateRelation(condition) {
247
+ switch (condition.quantifier) {
248
+ case "some": return {
249
+ ...condition,
250
+ quantifier: "none"
251
+ };
252
+ case "none": return {
253
+ ...condition,
254
+ quantifier: "some"
255
+ };
256
+ case "every": return {
257
+ ...condition,
258
+ quantifier: "some",
259
+ where: condition.where ? {
260
+ kind: "not",
261
+ item: condition.where
262
+ } : void 0
263
+ };
264
+ default: return condition.quantifier;
265
+ }
266
+ }
267
+ function filterEmbed(alias, condition, inner) {
268
+ return {
269
+ alias,
270
+ target: condition.target,
271
+ relation: condition.relation,
272
+ inner,
273
+ columns: [],
274
+ children: []
275
+ };
276
+ }
277
+ function columnList(columns) {
278
+ return columns.map(({ alias, column, cast }) => {
279
+ const source = cast ? `${column}::${cast}` : column;
280
+ return alias === column ? source : `${alias}:${source}`;
281
+ });
282
+ }
283
+ /** `key:column.sum()::text`, PostgREST's aggregate syntax (PostgREST 12+). */
284
+ function measureList(measures) {
285
+ return measures.map(({ key, column, fn, cast }) => `${key}:${column}.${fn}()${cast ? `::${cast}` : ""}`);
286
+ }
287
+ function aggregateList(aggregate) {
288
+ return [...aggregate.count ? ["_count:count()"] : [], ...measureList(aggregate.measures)];
289
+ }
290
+ function renderEmbeds(embeds) {
291
+ return embeds.map((node) => {
292
+ const hint = `${node.target.name}!${node.relation.foreignKey}${node.inner ? "!inner" : ""}`;
293
+ if (node.count) return `${node.alias}:${hint}(count)`;
294
+ if (node.measures) return `${node.alias}:${hint}(${measureList(node.measures).join(",")})`;
295
+ const inside = [...columnList(node.columns), ...renderEmbeds(node.children)];
296
+ return `${node.alias}:${hint}(${inside.join(",")})`;
297
+ });
298
+ }
299
+ function renderSelect(selection, embeds) {
300
+ const parts = [
301
+ ...columnList(selection.columns),
302
+ ...selection.aggregate ? aggregateList(selection.aggregate) : [],
303
+ ...renderEmbeds(embeds)
304
+ ];
305
+ return parts.length > 0 ? parts.join(",") : "*";
306
+ }
307
+ function applyWhere(compiler, condition, embeds) {
308
+ const where = simplifyOrFalse(condition);
309
+ if (where.never) return false;
310
+ if (where.condition) compiler.andContext(where.condition, void 0, embeds);
311
+ return true;
312
+ }
313
+ /** Compiles an operation into a PostgREST plan. */
314
+ function compilePostgrest(op) {
315
+ const compiler = new PostgrestCompiler();
316
+ const embeds = compiler.withPath([], void 0);
317
+ const selection = op.kind === "select" ? op.selection : op.kind === "insert" ? op.returning : op.returning;
318
+ if (selection) for (const include of selection.includes) compiler.include(include, void 0, embeds);
319
+ let matches = true;
320
+ if (op.kind === "select" || op.kind === "update" || op.kind === "delete") {
321
+ const before = embeds.length;
322
+ matches = applyWhere(compiler, op.where, embeds);
323
+ if (op.kind !== "select" && embeds.length > before) invalidRequest(`Relation filters are not supported in ${op.kind} on PostgREST; filter by key instead`, op.table.key);
324
+ }
325
+ let range;
326
+ if (op.kind === "select") {
327
+ for (const term of op.orderBy) compiler.order(term, void 0);
328
+ if (op.offset !== void 0) {
329
+ const from = op.offset;
330
+ range = {
331
+ from,
332
+ to: op.limit === void 0 ? Number.MAX_SAFE_INTEGER : from + op.limit - 1
333
+ };
334
+ } else if (op.limit !== void 0) compiler.limits.push({
335
+ count: op.limit,
336
+ referencedTable: void 0
337
+ });
338
+ }
339
+ return {
340
+ select: selection ? renderSelect(selection, embeds) : void 0,
341
+ filters: compiler.filters,
342
+ orders: compiler.orders,
343
+ limits: compiler.limits,
344
+ range,
345
+ never: !matches
346
+ };
347
+ }
348
+ //#endregion
349
+ //#region src/core/postgrest-executor.ts
350
+ function scope(client, schema) {
351
+ const loose = client;
352
+ return schema !== "public" && loose.schema ? loose.schema(schema) : loose;
353
+ }
354
+ function fromTable(client, schema, table) {
355
+ return scope(client, schema).from(table);
356
+ }
357
+ /** A POST to the function: arguments such as query vectors are too long for a URL. */
358
+ function fromFunction(client, source, options) {
359
+ const scoped = scope(client, source.schema);
360
+ if (!scoped.rpc) throw new TypeError(`better-supabase: the client has no rpc(), needed to read from ${source.name}`);
361
+ return scoped.rpc(source.name, source.args, options);
362
+ }
363
+ function applyFilters(builder, plan) {
364
+ let query = builder;
365
+ for (const filter of plan.filters) query = filter.kind === "filter" ? query.filter(filter.path, filter.operator, filter.value) : query.or(filter.expression, filter.referencedTable ? { referencedTable: filter.referencedTable } : {});
366
+ return query;
367
+ }
368
+ function applyShape(builder, plan) {
369
+ let query = builder;
370
+ for (const order of plan.orders) {
371
+ const options = { ascending: order.ascending };
372
+ if (order.nullsFirst !== void 0) options.nullsFirst = order.nullsFirst;
373
+ if (order.referencedTable) options.referencedTable = order.referencedTable;
374
+ query = query.order(order.column, options);
375
+ }
376
+ for (const limit of plan.limits) query = query.limit(limit.count, limit.referencedTable ? { referencedTable: limit.referencedTable } : {});
377
+ if (plan.range) query = query.range(plan.range.from, plan.range.to);
378
+ return query;
379
+ }
380
+ function build(client, op, plan) {
381
+ const base = fromTable(client, op.table.schema, op.table.name);
382
+ switch (op.kind) {
383
+ case "select": {
384
+ const options = {};
385
+ if (op.count) options.count = op.count;
386
+ if (op.head) options.head = true;
387
+ let query = applyShape(applyFilters(op.source ? fromFunction(client, op.source, options).select(plan.select) : base.select(plan.select, options), plan), plan);
388
+ if (op.single === "one") query = query.single();
389
+ if (op.single === "maybe") query = query.maybeSingle();
390
+ return query;
391
+ }
392
+ case "insert": {
393
+ const rows = op.rows.length === 1 ? op.rows[0] : op.rows;
394
+ const count = plan.select === void 0 ? { count: "exact" } : {};
395
+ let query = op.onConflict ? base.upsert(rows, {
396
+ onConflict: op.onConflict.columns.join(","),
397
+ ignoreDuplicates: op.onConflict.action === "ignore",
398
+ defaultToNull: op.defaultToNull,
399
+ ...count
400
+ }) : base.insert(rows, {
401
+ defaultToNull: op.defaultToNull,
402
+ ...count
403
+ });
404
+ if (plan.select !== void 0) query = query.select(plan.select);
405
+ return query;
406
+ }
407
+ case "update": {
408
+ const count = plan.select === void 0 ? { count: "exact" } : {};
409
+ let query = applyFilters(base.update(op.set, count), plan);
410
+ if (plan.select !== void 0) query = query.select(plan.select);
411
+ return query;
412
+ }
413
+ case "delete": {
414
+ const count = plan.select === void 0 ? { count: "exact" } : {};
415
+ let query = applyFilters(base.delete(count), plan);
416
+ if (plan.select !== void 0) query = query.select(plan.select);
417
+ return query;
418
+ }
419
+ default: return op;
420
+ }
421
+ }
422
+ function rowsOf(data) {
423
+ if (data === null || data === void 0) return [];
424
+ if (Array.isArray(data)) return data;
425
+ return [data];
426
+ }
427
+ /** Executes IR operations through a supabase-js client. */
428
+ const aborted = () => dbError("aborted", "The request was aborted");
429
+ function postgrestExecutor(client) {
430
+ return {
431
+ name: "postgrest",
432
+ functionSources: true,
433
+ async execute(op, context) {
434
+ let plan;
435
+ try {
436
+ plan = compilePostgrest(op);
437
+ } catch (cause) {
438
+ return err(toDbError(cause));
439
+ }
440
+ if (plan.never) return ok({
441
+ rows: [],
442
+ count: 0
443
+ });
444
+ if (context.signal?.aborted) return err(aborted());
445
+ let query = build(client, op, plan);
446
+ if (context.signal) query = query.abortSignal(context.signal);
447
+ const response = await query;
448
+ if (response.error) {
449
+ if (context.signal?.aborted) return err(aborted());
450
+ return err(mapDbError(response.error, context.errorMappers));
451
+ }
452
+ return ok({
453
+ rows: rowsOf(response.data),
454
+ count: response.count ?? null
455
+ });
456
+ },
457
+ async rpc(name, args, context) {
458
+ const scoped = scope(client, context.schema);
459
+ if (!scoped.rpc) return err(toDbError(/* @__PURE__ */ new Error("The client does not support rpc()")));
460
+ if (context.signal?.aborted) return err(aborted());
461
+ let query = context.get ? scoped.rpc(name, queryArgs(args), { get: true }) : scoped.rpc(name, args);
462
+ if (context.signal) query = query.abortSignal(context.signal);
463
+ const response = await query;
464
+ if (response.error) {
465
+ if (context.signal?.aborted) return err(aborted());
466
+ return err(mapDbError(response.error, context.errorMappers));
467
+ }
468
+ return ok(response.data);
469
+ }
470
+ };
471
+ }
472
+ /**
473
+ * GET arguments travel in the query string, where supabase-js would print an
474
+ * object as `[object Object]`; json and jsonb parameters take it as JSON.
475
+ */
476
+ function queryArgs(args) {
477
+ return Object.fromEntries(Object.entries(args).map(([key, value]) => [key, typeof value === "object" && value !== null && !Array.isArray(value) ? JSON.stringify(value) : value]));
478
+ }
479
+ /** The compiled plan for an operation; useful for debugging and tests. */
480
+ function explainPostgrest(op) {
481
+ return compilePostgrest(op);
482
+ }
483
+ //#endregion
484
+ //#region src/core/stats.ts
485
+ const EMPTY_STATS = {
486
+ calls: 0,
487
+ waves: 0,
488
+ tables: [],
489
+ ms: 0
490
+ };
491
+ /**
492
+ * Counts calls and waves. A recorder with a `parent` also reports to it, so
493
+ * a request-wide recorder sees every scope's calls.
494
+ */
495
+ var StatsRecorder = class {
496
+ #parent;
497
+ #now;
498
+ #tables = /* @__PURE__ */ new Set();
499
+ #calls = 0;
500
+ #waves = 0;
501
+ #inFlight = 0;
502
+ #first;
503
+ #last;
504
+ constructor(parent, now = () => performance.now()) {
505
+ this.#parent = parent;
506
+ this.#now = now;
507
+ }
508
+ /** Records a call to `table`; the returned function ends it. */
509
+ begin(table) {
510
+ const end = this.#parent?.begin(table);
511
+ const started = this.#now();
512
+ if (this.#inFlight === 0) this.#waves += 1;
513
+ this.#inFlight += 1;
514
+ this.#calls += 1;
515
+ this.#tables.add(table);
516
+ this.#first ??= started;
517
+ let ended = false;
518
+ return () => {
519
+ if (ended) return;
520
+ ended = true;
521
+ this.#inFlight -= 1;
522
+ this.#last = Math.max(this.#last ?? 0, this.#now());
523
+ end?.();
524
+ };
525
+ }
526
+ /** The totals so far. */
527
+ snapshot() {
528
+ return {
529
+ calls: this.#calls,
530
+ waves: this.#waves,
531
+ tables: [...this.#tables],
532
+ ms: this.#first === void 0 || this.#last === void 0 ? 0 : Math.round((this.#last - this.#first) * 100) / 100
533
+ };
534
+ }
535
+ };
536
+ /** Wraps an executor so every call is recorded. */
537
+ function recordStats(executor, recorder) {
538
+ const wrapped = {
539
+ name: executor.name,
540
+ async execute(op, context) {
541
+ const end = recorder.begin(op.table.key);
542
+ try {
543
+ return await executor.execute(op, context);
544
+ } finally {
545
+ end();
546
+ }
547
+ }
548
+ };
549
+ if (executor.rpc) {
550
+ const rpc = executor.rpc.bind(executor);
551
+ return {
552
+ ...wrapped,
553
+ async rpc(name, args, context) {
554
+ const end = recorder.begin(`${name}()`);
555
+ try {
556
+ return await rpc(name, args, context);
557
+ } finally {
558
+ end();
559
+ }
560
+ }
561
+ };
562
+ }
563
+ return wrapped;
564
+ }
565
+ //#endregion
566
+ export { postgrestExecutor as a, explainPostgrest as i, StatsRecorder as n, compilePostgrest as o, recordStats as r, EMPTY_STATS as t };
@@ -0,0 +1,4 @@
1
+ import { n as StoragePath, t as PathIn } from "../path-Cv__-OTj.js";
2
+ import { n as TemplateValues, t as TemplateParams } from "../template-mbvlysOo.js";
3
+ import { C as parseSize, S as fromStorageError, _ as TtlPreset, a as BucketDrift, b as UrlOptions, c as ReplaceOptions, d as StorageClient, f as StoredObject, g as TransformOptions, h as TTL, i as BucketConfig, l as ReplaceResult, m as SweepResult, n as Bucket, o as BucketPolicy, p as SweepOptions, r as BucketClient, s as ObjectTarget, t as ActualBucket, u as Reservation, v as UploadBody, x as defineBucket, y as UploadOptions } from "../index-Nxo3TJcm.js";
4
+ export { ActualBucket, Bucket, BucketClient, BucketConfig, BucketDrift, BucketPolicy, ObjectTarget, type PathIn, ReplaceOptions, ReplaceResult, Reservation, StorageClient, type StoragePath, StoredObject, SweepOptions, SweepResult, TTL, type TemplateParams, type TemplateValues, TransformOptions, TtlPreset, UploadBody, UploadOptions, UrlOptions, defineBucket, fromStorageError, parseSize };
@@ -0,0 +1,2 @@
1
+ import { i as parseSize, n as defineBucket, r as fromStorageError, t as TTL } from "../storage-BWXVUXGk.js";
2
+ export { TTL, defineBucket, fromStorageError, parseSize };