@marktiderman/genesis-core 2.16.1 → 2.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/dist/{aggregate-Y5IJ5aPn.d.ts → aggregate-CUirs7ka.d.ts} +1 -1
- package/dist/{chunk-M5IX7C6U.js → chunk-24X4HWL5.js} +4 -4
- package/dist/{chunk-M5IX7C6U.js.map → chunk-24X4HWL5.js.map} +1 -1
- package/dist/{chunk-XPBS5YE4.js → chunk-4POVYF6P.js} +4 -4
- package/dist/{chunk-XPBS5YE4.js.map → chunk-4POVYF6P.js.map} +1 -1
- package/dist/{chunk-IYGVJFCV.js → chunk-5KLIFJYG.js} +3 -3
- package/dist/{chunk-IYGVJFCV.js.map → chunk-5KLIFJYG.js.map} +1 -1
- package/dist/{chunk-7EFTWOMA.js → chunk-5PCGMSDX.js} +280 -41
- package/dist/chunk-5PCGMSDX.js.map +1 -0
- package/dist/chunk-6JOWGO2X.js +266 -0
- package/dist/chunk-6JOWGO2X.js.map +1 -0
- package/dist/{chunk-E3K2A3PK.js → chunk-6SHGMEA6.js} +3 -3
- package/dist/{chunk-E3K2A3PK.js.map → chunk-6SHGMEA6.js.map} +1 -1
- package/dist/chunk-6VNM6KAX.js +59 -0
- package/dist/chunk-6VNM6KAX.js.map +1 -0
- package/dist/chunk-AOIVTOHX.js +892 -0
- package/dist/chunk-AOIVTOHX.js.map +1 -0
- package/dist/{chunk-FXPODKCK.js → chunk-ATVK3VYS.js} +6 -6
- package/dist/{chunk-FXPODKCK.js.map → chunk-ATVK3VYS.js.map} +1 -1
- package/dist/chunk-BNKP2H7J.js +11 -0
- package/dist/chunk-BNKP2H7J.js.map +1 -0
- package/dist/{chunk-NOPS5EIH.js → chunk-CJ5V6JES.js} +5 -114
- package/dist/chunk-CJ5V6JES.js.map +1 -0
- package/dist/chunk-DJRT4DUF.js +53 -0
- package/dist/chunk-DJRT4DUF.js.map +1 -0
- package/dist/chunk-DTVYCATJ.js +220 -0
- package/dist/chunk-DTVYCATJ.js.map +1 -0
- package/dist/{chunk-POUI7GAI.js → chunk-JF7FXSQB.js} +5 -5
- package/dist/{chunk-POUI7GAI.js.map → chunk-JF7FXSQB.js.map} +1 -1
- package/dist/{chunk-LB6FMQQM.js → chunk-KV2HW4G4.js} +3 -11
- package/dist/chunk-KV2HW4G4.js.map +1 -0
- package/dist/chunk-LYNIHY3F.js +115 -0
- package/dist/chunk-LYNIHY3F.js.map +1 -0
- package/dist/{chunk-42NVJJTT.js → chunk-MMMC37AP.js} +3 -3
- package/dist/{chunk-42NVJJTT.js.map → chunk-MMMC37AP.js.map} +1 -1
- package/dist/chunk-MZVFOTAE.js +96 -0
- package/dist/chunk-MZVFOTAE.js.map +1 -0
- package/dist/{chunk-ALLWIK62.js → chunk-OCMYW2YE.js} +8 -6
- package/dist/chunk-OCMYW2YE.js.map +1 -0
- package/dist/{chunk-R3YXYVWF.js → chunk-OIZNABG4.js} +5 -4
- package/dist/chunk-OIZNABG4.js.map +1 -0
- package/dist/{chunk-SOO54YYY.js → chunk-OTUTSAZD.js} +4 -4
- package/dist/{chunk-SOO54YYY.js.map → chunk-OTUTSAZD.js.map} +1 -1
- package/dist/chunk-PH25HOAL.js +192 -0
- package/dist/chunk-PH25HOAL.js.map +1 -0
- package/dist/{chunk-QGWQ6LK3.js → chunk-QJOKZZKY.js} +9 -6
- package/dist/chunk-QJOKZZKY.js.map +1 -0
- package/dist/{chunk-ZYOJWU62.js → chunk-RZVLUUMY.js} +13 -54
- package/dist/chunk-RZVLUUMY.js.map +1 -0
- package/dist/chunk-SMEG63MO.js +115 -0
- package/dist/chunk-SMEG63MO.js.map +1 -0
- package/dist/chunk-UKJCF2CM.js +159 -0
- package/dist/chunk-UKJCF2CM.js.map +1 -0
- package/dist/chunk-UKWM32CG.js +31 -0
- package/dist/chunk-UKWM32CG.js.map +1 -0
- package/dist/{chunk-UDSTWXXZ.js → chunk-ZKLB2HB5.js} +3 -3
- package/dist/{chunk-UDSTWXXZ.js.map → chunk-ZKLB2HB5.js.map} +1 -1
- package/dist/csv.d.ts +12 -3
- package/dist/csv.js +4 -4
- package/dist/csv.js.map +1 -1
- package/dist/errors.js +2 -1
- package/dist/form-types.d.ts +1 -1
- package/dist/headless/index.d.ts +1 -1
- package/dist/hooks/index.d.ts +16 -6
- package/dist/hooks/index.js +29 -16
- package/dist/hooks/mutation-mode.d.ts +1 -1
- package/dist/hooks/mutation-mode.js +5 -4
- package/dist/hooks/related-keys.d.ts +1 -1
- package/dist/hooks/use-aggregate.d.ts +2 -2
- package/dist/hooks/use-aggregate.js +3 -3
- package/dist/hooks/use-board-columns.d.ts +79 -0
- package/dist/hooks/use-board-columns.js +4 -0
- package/dist/hooks/use-board-columns.js.map +1 -0
- package/dist/hooks/use-capabilities.d.ts +1 -1
- package/dist/hooks/use-capabilities.js +2 -2
- package/dist/hooks/use-infinite-resource.d.ts +1 -1
- package/dist/hooks/use-infinite-resource.js +3 -3
- package/dist/hooks/use-link-options.d.ts +1 -1
- package/dist/hooks/use-link-options.js +5 -5
- package/dist/hooks/use-list-state.d.ts +103 -0
- package/dist/hooks/use-list-state.js +22 -0
- package/dist/hooks/use-list-state.js.map +1 -0
- package/dist/hooks/use-live.d.ts +1 -1
- package/dist/hooks/use-live.js +2 -2
- package/dist/hooks/use-one.d.ts +7 -1
- package/dist/hooks/use-one.js +3 -3
- package/dist/hooks/use-persisted-ui-state.js +2 -2
- package/dist/hooks/use-relation-label.d.ts +1 -1
- package/dist/hooks/use-relation-label.js +2 -2
- package/dist/hooks/use-resource-form.d.ts +1 -1
- package/dist/hooks/use-resource-form.js +9 -8
- package/dist/hooks/use-resource.d.ts +7 -1
- package/dist/hooks/use-resource.js +7 -6
- package/dist/hooks/use-segment-counts.d.ts +23 -0
- package/dist/hooks/use-segment-counts.js +20 -0
- package/dist/hooks/use-segment-counts.js.map +1 -0
- package/dist/hooks/use-view-board.d.ts +120 -0
- package/dist/hooks/use-view-board.js +30 -0
- package/dist/hooks/use-view-board.js.map +1 -0
- package/dist/hooks/use-view-calendar-range.d.ts +91 -0
- package/dist/hooks/use-view-calendar-range.js +23 -0
- package/dist/hooks/use-view-calendar-range.js.map +1 -0
- package/dist/hooks/use-write.d.ts +1 -1
- package/dist/index.d.ts +42 -12
- package/dist/index.js +122 -28
- package/dist/index.js.map +1 -1
- package/dist/provider/index.d.ts +7 -325
- package/dist/provider/index.js +4 -3
- package/dist/{write-outcome-CkQXgtJq.d.ts → read-outcome-Cq0h2oAG.d.ts} +31 -2
- package/dist/resource-view/index.d.ts +162 -757
- package/dist/resource-view/index.js +32 -686
- package/dist/resource-view/index.js.map +1 -1
- package/dist/saved-views-Bcox3blZ.d.ts +155 -0
- package/dist/schema/index.d.ts +3 -4
- package/dist/schema/index.js +2 -1
- package/dist/supabase-provider-D5ditkHi.d.ts +465 -0
- package/dist/testing/index.d.ts +87 -3
- package/dist/testing/index.js +88 -1
- package/dist/testing/index.js.map +1 -1
- package/dist/{types-BriIV4e_.d.ts → types-BnOCVJzm.d.ts} +11 -1
- package/dist/types-CBtBvGw0.d.ts +1063 -0
- package/dist/{viewer-scope-Dx0N9AYk.d.ts → viewer-oOk0ZZk9.d.ts} +36 -34
- package/dist/{workspaces-CxpiFu2-.d.ts → workspaces-DeaFix5Z.d.ts} +1 -1
- package/dist/workspaces.d.ts +2 -2
- package/package.json +11 -1
- package/dist/chunk-7EFTWOMA.js.map +0 -1
- package/dist/chunk-ALLWIK62.js.map +0 -1
- package/dist/chunk-LB6FMQQM.js.map +0 -1
- package/dist/chunk-NOPS5EIH.js.map +0 -1
- package/dist/chunk-QGWQ6LK3.js.map +0 -1
- package/dist/chunk-R3YXYVWF.js.map +0 -1
- package/dist/chunk-ZYOJWU62.js.map +0 -1
- package/dist/read-outcome-BnboMYOV.d.ts +0 -30
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { resolveAggregate } from './chunk-J54XXKQD.js';
|
|
2
|
-
import {
|
|
2
|
+
import { reportError } from './chunk-CJ5V6JES.js';
|
|
3
|
+
import { toDataWriteError, readOutcomeFromCode } from './chunk-LYNIHY3F.js';
|
|
3
4
|
import { DataReadError, readFailureMessage } from './chunk-HUNGCTRA.js';
|
|
4
5
|
import { isDataWriteError, DataWriteError } from './chunk-WLDSIQJ2.js';
|
|
5
6
|
|
|
@@ -247,12 +248,12 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
247
248
|
const allowed = new Set(config.writeColumns);
|
|
248
249
|
return Object.fromEntries(Object.entries(values).filter(([key]) => allowed.has(key)));
|
|
249
250
|
}
|
|
250
|
-
if (!config.source || id === void 0) return values;
|
|
251
|
-
const row = await
|
|
251
|
+
if (!config.source || id === void 0 || !tableAllowed(resource, "read")) return values;
|
|
252
|
+
const row = (await run(from(resource).select("*").eq("id", id)))[0];
|
|
252
253
|
if (!row) return values;
|
|
253
254
|
return Object.fromEntries(Object.entries(values).filter(([key]) => key in row));
|
|
254
255
|
}
|
|
255
|
-
function read(resource, columns, count) {
|
|
256
|
+
function read(resource, columns, count, filters, used = /* @__PURE__ */ new Set()) {
|
|
256
257
|
const config = configFor(resource);
|
|
257
258
|
const cols = columns ?? config.select ?? "*";
|
|
258
259
|
const source = config.source;
|
|
@@ -260,14 +261,164 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
260
261
|
if (!client.rpc) {
|
|
261
262
|
throw new DataReadError("error", `[SupabaseProvider] "${resource}" reads from ${source.rpc}(), but the client has no rpc.`);
|
|
262
263
|
}
|
|
263
|
-
const builder = client.rpc(source.rpc, source.args?.() ?? {}, count ? { count } : void 0);
|
|
264
|
+
const builder = client.rpc(source.rpc, source.args?.(sourceArgs(resource, filters, used)) ?? {}, count ? { count } : void 0);
|
|
264
265
|
return builder.select(cols);
|
|
265
266
|
}
|
|
266
267
|
const table = relation(source ?? resource);
|
|
267
268
|
return count ? table.select(cols, { count }) : table.select(cols);
|
|
268
269
|
}
|
|
270
|
+
function sourceArgs(resource, filters, used) {
|
|
271
|
+
const list = filters ?? [];
|
|
272
|
+
return {
|
|
273
|
+
resource,
|
|
274
|
+
filters: list,
|
|
275
|
+
take: (field) => {
|
|
276
|
+
const f = list.find((x) => x.field === field && (x.operator === "eq" || x.operator === "is_null"));
|
|
277
|
+
if (!f) return void 0;
|
|
278
|
+
used.add(f);
|
|
279
|
+
return f.operator === "eq" ? f.value : null;
|
|
280
|
+
},
|
|
281
|
+
takeIn: (field) => {
|
|
282
|
+
const f = list.find((x) => x.field === field && (x.operator === "in" || x.operator === "eq") && !used.has(x));
|
|
283
|
+
if (!f) return void 0;
|
|
284
|
+
used.add(f);
|
|
285
|
+
const value = f.value;
|
|
286
|
+
return f.operator === "in" ? [...value] : [value];
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
function readFiltered(resource, columns, count, filters) {
|
|
291
|
+
const used = /* @__PURE__ */ new Set();
|
|
292
|
+
const query = read(resource, columns, count, filters, used);
|
|
293
|
+
return applyFilters(query, used.size ? filters.filter((f) => !used.has(f)) : filters);
|
|
294
|
+
}
|
|
295
|
+
async function readSourcePage(resource, params, offset, limit) {
|
|
296
|
+
const source = configFor(resource).source;
|
|
297
|
+
if (!source || typeof source !== "object" || !source.pages || !client.rpc) return void 0;
|
|
298
|
+
if (searchClauses(resource, params)) return void 0;
|
|
299
|
+
const order = source.pages.order ?? [];
|
|
300
|
+
const requested = params.sorts && params.sorts.length > 0 ? params.sorts : params.sort ? [params.sort] : [];
|
|
301
|
+
if (requested.length > 0 && (requested.length > order.length || requested.some((r, k) => r.field !== order[k].field || r.order !== order[k].order))) {
|
|
302
|
+
return void 0;
|
|
303
|
+
}
|
|
304
|
+
const used = /* @__PURE__ */ new Set();
|
|
305
|
+
const args = source.args?.(sourceArgs(resource, params.filters, used)) ?? {};
|
|
306
|
+
if ((params.filters ?? []).some((f) => !used.has(f))) return void 0;
|
|
307
|
+
const totalColumn = source.pages.total ?? "total_count";
|
|
308
|
+
const cols = params.select ?? configFor(resource).select ?? "*";
|
|
309
|
+
const select = cols.trim() === "*" ? cols : `${cols},${totalColumn}`;
|
|
310
|
+
const call = async (from2, take) => {
|
|
311
|
+
const builder = client.rpc(source.rpc, { ...args, page_limit: take, page_offset: from2 });
|
|
312
|
+
const result = await builder.select(select);
|
|
313
|
+
await throwIfReadError(result, resource, "getList");
|
|
314
|
+
return result.data ?? [];
|
|
315
|
+
};
|
|
316
|
+
const totalOf = (rows) => Number(rows[0]?.[totalColumn] ?? 0);
|
|
317
|
+
if (limit === 0) return { data: [], total: totalOf(await call(0, 0)) };
|
|
318
|
+
const data = await call(offset, limit);
|
|
319
|
+
const total = data.length > 0 || offset === 0 ? totalOf(data) : totalOf(await call(0, 0));
|
|
320
|
+
return { data, total };
|
|
321
|
+
}
|
|
269
322
|
const configFor = (resource) => options.resources?.[resource] ?? options.resources?.[otherSpelling(resource)] ?? {};
|
|
270
323
|
const nounFor = (resource) => configFor(resource).noun ?? "record";
|
|
324
|
+
const declared = options.access === "declared";
|
|
325
|
+
const anyActions = Object.values(options.resources ?? {}).some((r) => Object.keys(r.actions ?? {}).length > 0);
|
|
326
|
+
const warnedArgs = /* @__PURE__ */ new Set();
|
|
327
|
+
function warnArgsDropVersion(resource, use, call, contextAt) {
|
|
328
|
+
if (!call || typeof call === "string" || !call.args || !versionFor(resource)) return;
|
|
329
|
+
if (call.args.length >= contextAt) return;
|
|
330
|
+
const key = `${resource}\0${use}`;
|
|
331
|
+
if (warnedArgs.has(key)) return;
|
|
332
|
+
warnedArgs.add(key);
|
|
333
|
+
reportError(
|
|
334
|
+
new Error(
|
|
335
|
+
`[SupabaseProvider] "${resource}" has a version rule and its ${use} function's args takes no context. Take the context (argument ${contextAt}) and pass context.expectedVersion on as p_expected_version, or writes go unchecked.`
|
|
336
|
+
),
|
|
337
|
+
{ source: "core:versioned-writes", tags: { resource } }
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
function tableAllowed(resource, op) {
|
|
341
|
+
if (!declared) return true;
|
|
342
|
+
const table = configFor(resource).table;
|
|
343
|
+
if (!table) return false;
|
|
344
|
+
if (op === "read") return table.read === true;
|
|
345
|
+
return table.write === true || Array.isArray(table.write) && table.write.includes(op);
|
|
346
|
+
}
|
|
347
|
+
function undeclaredMessage(resource, op) {
|
|
348
|
+
const use = op === "read" ? "a `source` (a view, or a database function)" : `\`writes.${op}\` (a database function)`;
|
|
349
|
+
const optIn = op === "read" ? "read: true" : `write: ["${op}"]`;
|
|
350
|
+
return `[SupabaseProvider] "${resource}" declares no ${op === "read" ? "read" : op}. Give it ${use}, or opt in to its table with \`table: { ${optIn}, why: "\u2026" }\` (docs/DATA-ACCESS.md).`;
|
|
351
|
+
}
|
|
352
|
+
const readDeclared = (resource) => configFor(resource).source !== void 0 || tableAllowed(resource, "read");
|
|
353
|
+
async function assertReadDeclared(resource, operation) {
|
|
354
|
+
if (readDeclared(resource)) return;
|
|
355
|
+
await reportReadError(new DataReadError("forbidden", undeclaredMessage(resource, "read")), resource, operation);
|
|
356
|
+
}
|
|
357
|
+
function writeDeclared(resource, op) {
|
|
358
|
+
const config = configFor(resource);
|
|
359
|
+
if (config.writes?.[op]) return true;
|
|
360
|
+
if (op === "create" && config.create) return true;
|
|
361
|
+
if (op === "update" && config.update) return true;
|
|
362
|
+
return tableAllowed(resource, op);
|
|
363
|
+
}
|
|
364
|
+
function assertTableWrite(resource, op) {
|
|
365
|
+
if (tableAllowed(resource, op)) return;
|
|
366
|
+
throw new DataWriteError("forbidden", undeclaredMessage(resource, op));
|
|
367
|
+
}
|
|
368
|
+
function callFor(call, input, defaults) {
|
|
369
|
+
if (typeof call === "string") return { fn: call, args: defaults };
|
|
370
|
+
return { fn: call.fn, args: call.args ? call.args(...input) : defaults };
|
|
371
|
+
}
|
|
372
|
+
async function callWrite(resource, fn, args) {
|
|
373
|
+
if (!client.rpc) {
|
|
374
|
+
throw new DataWriteError("error", `[SupabaseProvider] "${resource}" writes through ${fn}(), but the client has no rpc.`);
|
|
375
|
+
}
|
|
376
|
+
let result;
|
|
377
|
+
try {
|
|
378
|
+
result = await client.rpc(fn, args);
|
|
379
|
+
} catch (error) {
|
|
380
|
+
throw toDataWriteError(error);
|
|
381
|
+
}
|
|
382
|
+
if (result.error) throw toDataWriteError(result.error);
|
|
383
|
+
if (Array.isArray(result.data)) return result.data.length > 0 ? result.data[0] : null;
|
|
384
|
+
return result.data;
|
|
385
|
+
}
|
|
386
|
+
const versionArgs = (expected) => expected === void 0 ? {} : { p_expected_version: expected };
|
|
387
|
+
async function readRecord(resource, id) {
|
|
388
|
+
if (!readDeclared(resource)) return void 0;
|
|
389
|
+
try {
|
|
390
|
+
return (await run(read(resource, void 0).eq("id", id)))[0];
|
|
391
|
+
} catch {
|
|
392
|
+
return void 0;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
async function readAfterWrite(resource, id, throughFunction) {
|
|
396
|
+
if (declared || throughFunction) return readRecord(resource, id);
|
|
397
|
+
return readBack(resource, id).catch(() => void 0);
|
|
398
|
+
}
|
|
399
|
+
function createRule(resource) {
|
|
400
|
+
const config = configFor(resource);
|
|
401
|
+
if (config.create) return config.create;
|
|
402
|
+
const call = config.writes?.create;
|
|
403
|
+
if (!call) return void 0;
|
|
404
|
+
return async (row) => {
|
|
405
|
+
const { fn, args } = callFor(call, [row], { p_values: row });
|
|
406
|
+
const returned = await callWrite(resource, fn, args);
|
|
407
|
+
return returned;
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
function updateRule(resource) {
|
|
411
|
+
const config = configFor(resource);
|
|
412
|
+
if (config.update) return config.update;
|
|
413
|
+
const call = config.writes?.update;
|
|
414
|
+
if (!call) return void 0;
|
|
415
|
+
warnArgsDropVersion(resource, "update", call, 3);
|
|
416
|
+
return async (id, row, context) => {
|
|
417
|
+
const expected = context?.expectedVersion;
|
|
418
|
+
const { fn, args } = callFor(call, [id, row, context ?? {}], { p_id: id, p_values: row, ...versionArgs(expected) });
|
|
419
|
+
return await callWrite(resource, fn, args);
|
|
420
|
+
};
|
|
421
|
+
}
|
|
271
422
|
const recentWrites = /* @__PURE__ */ new Map();
|
|
272
423
|
function noteWrite(event) {
|
|
273
424
|
const now = Date.now();
|
|
@@ -293,6 +444,7 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
293
444
|
}
|
|
294
445
|
function realtimeEnabled(resource) {
|
|
295
446
|
if (typeof client.channel !== "function") return false;
|
|
447
|
+
if (!readDeclared(resource)) return false;
|
|
296
448
|
const own = configFor(resource).realtime;
|
|
297
449
|
if (own !== void 0) return own;
|
|
298
450
|
const all = options.realtime;
|
|
@@ -321,10 +473,21 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
321
473
|
return result.data == null ? [] : [result.data];
|
|
322
474
|
}
|
|
323
475
|
async function isVisible(resource, id) {
|
|
324
|
-
|
|
476
|
+
if (!readDeclared(resource)) throw new DataWriteError("forbidden", undeclaredMessage(resource, "read"));
|
|
477
|
+
const query = tableAllowed(resource, "read") ? from(resource).select("id") : read(resource, "id");
|
|
478
|
+
return (await run(query.eq("id", id))).length > 0;
|
|
325
479
|
}
|
|
326
480
|
async function readBack(resource, id) {
|
|
327
|
-
|
|
481
|
+
if (!readDeclared(resource)) throw new DataWriteError("forbidden", undeclaredMessage(resource, "read"));
|
|
482
|
+
const query = tableAllowed(resource, "read") ? from(resource).select("*") : read(resource, void 0);
|
|
483
|
+
return (await run(query.eq("id", id)))[0];
|
|
484
|
+
}
|
|
485
|
+
function unreadableNoRowsError(resource, verb) {
|
|
486
|
+
const done = verb === "change" ? "changed" : "deleted";
|
|
487
|
+
return new DataWriteError(
|
|
488
|
+
"forbidden",
|
|
489
|
+
`This ${nounFor(resource)} was not ${done}: you can't ${verb} it, it no longer exists, or someone changed it first.`
|
|
490
|
+
);
|
|
328
491
|
}
|
|
329
492
|
function goneError(resource) {
|
|
330
493
|
return new DataWriteError(
|
|
@@ -333,6 +496,7 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
333
496
|
);
|
|
334
497
|
}
|
|
335
498
|
async function noRowsError(resource, id, verb) {
|
|
499
|
+
if (!readDeclared(resource)) return unreadableNoRowsError(resource, verb);
|
|
336
500
|
let visible;
|
|
337
501
|
try {
|
|
338
502
|
visible = await isVisible(resource, id);
|
|
@@ -363,6 +527,7 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
363
527
|
}
|
|
364
528
|
async function staleOrNoRowsError(resource, id, verb, expected, known) {
|
|
365
529
|
if (expected === void 0) return noRowsError(resource, id, verb);
|
|
530
|
+
if (!readDeclared(resource)) return unreadableNoRowsError(resource, verb);
|
|
366
531
|
const { field, kind } = versionFor(resource);
|
|
367
532
|
let current = known;
|
|
368
533
|
try {
|
|
@@ -450,8 +615,7 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
450
615
|
}
|
|
451
616
|
}
|
|
452
617
|
const mode = countMode(resource, params);
|
|
453
|
-
let query =
|
|
454
|
-
query = applyFilters(query, params.filters);
|
|
618
|
+
let query = readFiltered(resource, params.select, cursor || mode === "none" ? void 0 : mode, params.filters);
|
|
455
619
|
const search = searchClauses(resource, params);
|
|
456
620
|
const keyset = after ? keysetClauses(after) : "";
|
|
457
621
|
if (search && keyset) query = query.or(`and(or(${search}),or(${keyset}))`);
|
|
@@ -500,6 +664,7 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
500
664
|
}
|
|
501
665
|
const provider = {
|
|
502
666
|
async getList(resource, params) {
|
|
667
|
+
await assertReadDeclared(resource, "getList");
|
|
503
668
|
if (params.pagination && params.pagination.perPage > maxPerPage) {
|
|
504
669
|
const { perPage } = params.pagination;
|
|
505
670
|
await reportReadError(
|
|
@@ -521,9 +686,13 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
521
686
|
return getCursorPage(resource, params, configuredKey === void 0 ? "id" : configuredKey);
|
|
522
687
|
}
|
|
523
688
|
const mode = countMode(resource, params);
|
|
689
|
+
if (params.pagination) {
|
|
690
|
+
const { page, perPage } = params.pagination;
|
|
691
|
+
const paged = await readSourcePage(resource, params, (page - 1) * perPage, perPage);
|
|
692
|
+
if (paged) return { data: paged.data, total: mode === "none" ? -1 : paged.total };
|
|
693
|
+
}
|
|
524
694
|
const run2 = (key) => {
|
|
525
|
-
let query =
|
|
526
|
-
query = applyFilters(query, params.filters);
|
|
695
|
+
let query = readFiltered(resource, params.select, mode === "none" ? void 0 : mode, params.filters);
|
|
527
696
|
const clauses = searchClauses(resource, params);
|
|
528
697
|
if (clauses) {
|
|
529
698
|
query = query.or(clauses);
|
|
@@ -565,6 +734,7 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
565
734
|
};
|
|
566
735
|
},
|
|
567
736
|
async aggregate(resource, params) {
|
|
737
|
+
await assertReadDeclared(resource, "getList");
|
|
568
738
|
const entries = Object.entries(params.metrics);
|
|
569
739
|
const counts = entries.filter(([, m]) => m.fn === "count");
|
|
570
740
|
const others = entries.filter(([, m]) => m.fn !== "count");
|
|
@@ -578,14 +748,17 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
578
748
|
resource,
|
|
579
749
|
"getList"
|
|
580
750
|
);
|
|
581
|
-
const narrow = (
|
|
582
|
-
query =
|
|
751
|
+
const narrow = (columns, count) => {
|
|
752
|
+
const query = readFiltered(resource, columns, count, params.filters);
|
|
583
753
|
const clauses = searchClauses(resource, { search: params.search });
|
|
584
754
|
return clauses ? query.or(clauses) : query;
|
|
585
755
|
};
|
|
586
|
-
|
|
756
|
+
const pagedTotal = counts.length > 0 ? await readSourcePage(resource, { filters: params.filters, search: params.search }, 0, 0) : void 0;
|
|
757
|
+
if (pagedTotal) {
|
|
758
|
+
for (const [name] of counts) values[name] = pagedTotal.total;
|
|
759
|
+
} else if (counts.length > 0) {
|
|
587
760
|
const mode = params.count ?? "exact";
|
|
588
|
-
const result = await narrow(
|
|
761
|
+
const result = await narrow("*", mode).range(0, 0);
|
|
589
762
|
await throwIfReadError(result, resource, "getList");
|
|
590
763
|
for (const [name] of counts) values[name] = result.count ?? 0;
|
|
591
764
|
isEstimate = mode !== "exact";
|
|
@@ -596,7 +769,7 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
596
769
|
if (!SAFE.test(name) || !("field" in m) || !SAFE.test(m.field)) await refuse(`"${name}" and its field must be plain column names.`);
|
|
597
770
|
}
|
|
598
771
|
const cols = others.map(([name, m]) => `${name}:${m.field}.${m.fn}()`).join(",");
|
|
599
|
-
const result = await narrow(
|
|
772
|
+
const result = await narrow(cols);
|
|
600
773
|
await throwIfReadError(result, resource, "getList");
|
|
601
774
|
const row = result.data?.[0] ?? {};
|
|
602
775
|
for (const [name] of others) {
|
|
@@ -618,13 +791,15 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
618
791
|
return isEstimate ? { values, isEstimate } : { values };
|
|
619
792
|
},
|
|
620
793
|
async getOne(resource, params) {
|
|
621
|
-
|
|
794
|
+
await assertReadDeclared(resource, "getOne");
|
|
795
|
+
const result = await read(resource, params.select, void 0, params.scope).eq("id", params.id).single();
|
|
622
796
|
await throwIfReadError(result, resource, "getOne");
|
|
623
797
|
return { data: result.data };
|
|
624
798
|
},
|
|
625
799
|
async getMany(resource, params) {
|
|
800
|
+
await assertReadDeclared(resource, "getMany");
|
|
626
801
|
if (params.ids.length === 0) return { data: [] };
|
|
627
|
-
const result = await read(resource, void 0).in("id", params.ids);
|
|
802
|
+
const result = await read(resource, void 0, void 0, params.scope).in("id", params.ids);
|
|
628
803
|
await throwIfReadError(result, resource, "getMany");
|
|
629
804
|
return { data: result.data ?? [] };
|
|
630
805
|
},
|
|
@@ -638,8 +813,12 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
638
813
|
...resource && versionFor(resource) ? { version: { ...versionFor(resource) } } : {},
|
|
639
814
|
compositeKeys: false,
|
|
640
815
|
multiSort: true,
|
|
641
|
-
|
|
642
|
-
|
|
816
|
+
// A declared provider serves only the writes a resource declares.
|
|
817
|
+
create: resource ? writeDeclared(resource, "create") : true,
|
|
818
|
+
delete: resource ? writeDeclared(resource, "delete") : true,
|
|
819
|
+
// `runAction` serves the actions a resource declares. Said only when some resource declares
|
|
820
|
+
// one: a provider without any says nothing, as before, so an app's own runner still shows.
|
|
821
|
+
...anyActions ? { actions: resource ? Object.keys(configFor(resource).actions ?? {}).length > 0 : true } : {},
|
|
643
822
|
maxPerPage,
|
|
644
823
|
softDelete: resource ? isBinTable(resource) : false,
|
|
645
824
|
cursorPagination: resource ? configFor(resource).orderKey !== false : true,
|
|
@@ -651,7 +830,7 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
651
830
|
if (process.env.NODE_ENV !== "production" && !warnedRealtime.has(resource)) {
|
|
652
831
|
warnedRealtime.add(resource);
|
|
653
832
|
console.warn(
|
|
654
|
-
typeof client.channel !== "function" ? `[SupabaseProvider] "${resource}" was subscribed to, but the client has no channel() (Supabase Realtime), so nothing will be reported.` : `[SupabaseProvider] "${resource}" was subscribed to, but realtime is off for it. Set resources.${resource}.realtime (or the realtime option) to true.`
|
|
833
|
+
!readDeclared(resource) ? `${undeclaredMessage(resource, "read")} Its changes aren't reported either.` : typeof client.channel !== "function" ? `[SupabaseProvider] "${resource}" was subscribed to, but the client has no channel() (Supabase Realtime), so nothing will be reported.` : `[SupabaseProvider] "${resource}" was subscribed to, but realtime is off for it. Set resources.${resource}.realtime (or the realtime option) to true.`
|
|
655
834
|
);
|
|
656
835
|
}
|
|
657
836
|
return () => {
|
|
@@ -737,11 +916,11 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
737
916
|
};
|
|
738
917
|
},
|
|
739
918
|
async create(resource, params) {
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
);
|
|
744
|
-
const override =
|
|
919
|
+
if (!writeDeclared(resource, "create")) throw new DataWriteError("forbidden", undeclaredMessage(resource, "create"));
|
|
920
|
+
const prepared = await prepare(resource, params.data, { resource, action: "create" });
|
|
921
|
+
const throughFunction = !configFor(resource).create && configFor(resource).writes?.create !== void 0;
|
|
922
|
+
const row = throughFunction ? { ...prepared } : await tableValues(resource, prepared);
|
|
923
|
+
const override = createRule(resource);
|
|
745
924
|
let record;
|
|
746
925
|
if (override) {
|
|
747
926
|
let returned;
|
|
@@ -751,7 +930,7 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
751
930
|
throw toDataWriteError(error);
|
|
752
931
|
}
|
|
753
932
|
if (isIdentifier(returned)) {
|
|
754
|
-
record = await
|
|
933
|
+
record = await readAfterWrite(resource, returned, throughFunction) ?? (declared || throughFunction ? { ...row, id: returned } : { id: returned });
|
|
755
934
|
} else if (returned && typeof returned === "object" && isIdentifier(returned.id)) {
|
|
756
935
|
record = returned;
|
|
757
936
|
} else {
|
|
@@ -760,26 +939,36 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
760
939
|
`[SupabaseProvider] The create rule for "${resource}" returned neither the new row nor its id.`
|
|
761
940
|
);
|
|
762
941
|
}
|
|
763
|
-
} else {
|
|
942
|
+
} else if (!declared) {
|
|
764
943
|
const rows = await run(from(resource).insert(row).select("*").single());
|
|
765
944
|
record = rows[0];
|
|
945
|
+
} else {
|
|
946
|
+
assertTableWrite(resource, "create");
|
|
947
|
+
const version = versionFor(resource);
|
|
948
|
+
const rows = await run(from(resource).insert(row).select(version ? `id,${version.field}` : "id").single());
|
|
949
|
+
const written = rows[0] ?? {};
|
|
950
|
+
const id = written.id;
|
|
951
|
+
if (!isIdentifier(id)) {
|
|
952
|
+
throw new DataWriteError("error", `[SupabaseProvider] The insert into "${resource}" returned no id.`);
|
|
953
|
+
}
|
|
954
|
+
record = await readRecord(resource, id) ?? { ...row, ...written, id };
|
|
955
|
+
if (version && record[version.field] == null && written[version.field] != null) {
|
|
956
|
+
record = { ...row, ...written, id };
|
|
957
|
+
}
|
|
766
958
|
}
|
|
767
959
|
await committed({ resource, kind: "create", id: record.id, record });
|
|
768
960
|
return { data: record };
|
|
769
961
|
},
|
|
770
962
|
async update(resource, params) {
|
|
771
963
|
const { id } = params;
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
id
|
|
777
|
-
)
|
|
778
|
-
};
|
|
964
|
+
if (!writeDeclared(resource, "update")) throw new DataWriteError("forbidden", undeclaredMessage(resource, "update"));
|
|
965
|
+
const throughFunction = !configFor(resource).update && configFor(resource).writes?.update !== void 0;
|
|
966
|
+
const prepared = await prepare(resource, params.data, { resource, action: "update", id });
|
|
967
|
+
const row = throughFunction ? { ...prepared } : { ...await tableValues(resource, prepared, id) };
|
|
779
968
|
const version = versionFor(resource);
|
|
780
969
|
const expected = expectedFor(resource, params.expectedVersion);
|
|
781
970
|
if (version) delete row[version.field];
|
|
782
|
-
const override =
|
|
971
|
+
const override = updateRule(resource);
|
|
783
972
|
let record;
|
|
784
973
|
if (override && version && override.length < 3 && !warnedUncheckedOverride.has(resource)) {
|
|
785
974
|
warnedUncheckedOverride.add(resource);
|
|
@@ -801,7 +990,7 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
801
990
|
if (result == null || result === false) throw await staleOrNoRowsError(resource, id, "change", expected);
|
|
802
991
|
if (isIdentifier(result) || result === true) {
|
|
803
992
|
const readId = result === true ? id : result;
|
|
804
|
-
record = await
|
|
993
|
+
record = await readAfterWrite(resource, readId, throughFunction) ?? { ...row, id: readId };
|
|
805
994
|
} else if (typeof result === "object") {
|
|
806
995
|
record = { id, ...result };
|
|
807
996
|
} else {
|
|
@@ -810,12 +999,30 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
810
999
|
`[SupabaseProvider] The update rule for "${resource}" returned neither the row, its id, nor nothing.`
|
|
811
1000
|
);
|
|
812
1001
|
}
|
|
813
|
-
} else {
|
|
1002
|
+
} else if (!declared) {
|
|
814
1003
|
let query = from(resource).update(row).eq("id", id);
|
|
815
1004
|
if (expected !== void 0) query = query.eq(version.field, expected);
|
|
816
1005
|
const rows = await run(query.select("*"));
|
|
817
1006
|
if (rows.length === 0) throw await staleOrNoRowsError(resource, id, "change", expected);
|
|
818
1007
|
record = rows[0];
|
|
1008
|
+
} else {
|
|
1009
|
+
assertTableWrite(resource, "update");
|
|
1010
|
+
const config = configFor(resource);
|
|
1011
|
+
if (config.source !== void 0 && !config.writeColumns && !tableAllowed(resource, "read")) {
|
|
1012
|
+
throw new DataWriteError(
|
|
1013
|
+
"error",
|
|
1014
|
+
`[SupabaseProvider] "${resource}" updates its table but reads ${typeof config.source === "string" ? config.source : `${config.source.rpc}()`}. Name the table's columns with \`writeColumns\`, or declare \`table: { read: true, \u2026 }\` (docs/DATA-ACCESS.md).`
|
|
1015
|
+
);
|
|
1016
|
+
}
|
|
1017
|
+
let query = from(resource).update(row).eq("id", id);
|
|
1018
|
+
if (expected !== void 0) query = query.eq(version.field, expected);
|
|
1019
|
+
const rows = await run(query.select(version ? `id,${version.field}` : "id"));
|
|
1020
|
+
if (rows.length === 0) throw await staleOrNoRowsError(resource, id, "change", expected);
|
|
1021
|
+
const written = rows[0];
|
|
1022
|
+
record = await readRecord(resource, id) ?? { ...row, ...written, id };
|
|
1023
|
+
if (version && record[version.field] == null && written[version.field] != null) {
|
|
1024
|
+
record = { ...row, ...written, id };
|
|
1025
|
+
}
|
|
819
1026
|
}
|
|
820
1027
|
await committed({ resource, kind: "update", id, record });
|
|
821
1028
|
const newVersion = version && record[version.field] != null ? String(record[version.field]) : void 0;
|
|
@@ -825,7 +1032,19 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
825
1032
|
const { id } = params;
|
|
826
1033
|
let deletedId = id;
|
|
827
1034
|
const expected = expectedFor(resource, params.expectedVersion);
|
|
828
|
-
|
|
1035
|
+
const deleteCall = configFor(resource).writes?.delete;
|
|
1036
|
+
if (deleteCall) {
|
|
1037
|
+
warnArgsDropVersion(resource, "delete", deleteCall, 2);
|
|
1038
|
+
const { fn, args } = callFor(deleteCall, [id, expected === void 0 ? {} : { expectedVersion: expected }], {
|
|
1039
|
+
p_id: id,
|
|
1040
|
+
...versionArgs(expected)
|
|
1041
|
+
});
|
|
1042
|
+
const returned = await callWrite(resource, fn, args);
|
|
1043
|
+
if (returned == null || returned === false) throw await staleOrNoRowsError(resource, id, "delete", expected);
|
|
1044
|
+
const returnedId = isIdentifier(returned) ? returned : returned?.id;
|
|
1045
|
+
if (isIdentifier(returnedId)) deletedId = returnedId;
|
|
1046
|
+
} else if (isBinTable(resource)) {
|
|
1047
|
+
assertTableWrite(resource, "delete");
|
|
829
1048
|
if (expected !== void 0) {
|
|
830
1049
|
const current = await readBack(resource, id);
|
|
831
1050
|
if (!current) throw goneError(resource);
|
|
@@ -853,6 +1072,7 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
853
1072
|
throw new DataWriteError("forbidden", `You can't delete this ${nounFor(resource)}.`);
|
|
854
1073
|
}
|
|
855
1074
|
} else {
|
|
1075
|
+
assertTableWrite(resource, "delete");
|
|
856
1076
|
let query = from(resource).delete().eq("id", id);
|
|
857
1077
|
if (expected !== void 0) query = query.eq(versionFor(resource).field, expected);
|
|
858
1078
|
const rows = await run(query.select("id"));
|
|
@@ -863,6 +1083,24 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
863
1083
|
await committed({ resource, kind: "delete", id: deletedId });
|
|
864
1084
|
return { data: { id: deletedId } };
|
|
865
1085
|
},
|
|
1086
|
+
async runAction(resource, params) {
|
|
1087
|
+
const { id, action } = params;
|
|
1088
|
+
const call = configFor(resource).actions?.[action];
|
|
1089
|
+
if (!call) {
|
|
1090
|
+
throw new DataWriteError("invalid", `[SupabaseProvider] "${resource}" declares no action "${action}".`);
|
|
1091
|
+
}
|
|
1092
|
+
warnArgsDropVersion(resource, `action "${action}"`, call, 2);
|
|
1093
|
+
const expected = expectedFor(resource, params.expectedVersion);
|
|
1094
|
+
const { fn, args } = callFor(call, [id, expected === void 0 ? {} : { expectedVersion: expected }], {
|
|
1095
|
+
p_id: id,
|
|
1096
|
+
...versionArgs(expected)
|
|
1097
|
+
});
|
|
1098
|
+
const returned = await callWrite(resource, fn, args);
|
|
1099
|
+
if (returned == null || returned === false) throw await staleOrNoRowsError(resource, id, "change", expected);
|
|
1100
|
+
const data = returned !== null && typeof returned === "object" ? { id, ...returned } : await readRecord(resource, isIdentifier(returned) ? returned : id) ?? null;
|
|
1101
|
+
await committed({ resource, kind: "update", id, ...data ? { record: data } : {} });
|
|
1102
|
+
return { data };
|
|
1103
|
+
},
|
|
866
1104
|
toFormValues(resource, record) {
|
|
867
1105
|
const map = configFor(resource).toFormValues;
|
|
868
1106
|
return map ? map(record) : record;
|
|
@@ -873,6 +1111,7 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
873
1111
|
return typeof keys === "function" ? keys(event) : keys;
|
|
874
1112
|
}
|
|
875
1113
|
};
|
|
1114
|
+
if (!anyActions) delete provider.runAction;
|
|
876
1115
|
Object.defineProperty(provider, "__recentWriteCount", { value: () => recentWrites.size, enumerable: false });
|
|
877
1116
|
return provider;
|
|
878
1117
|
}
|
|
@@ -940,5 +1179,5 @@ async function createOrAdopt(provider, resource, data, options) {
|
|
|
940
1179
|
}
|
|
941
1180
|
|
|
942
1181
|
export { DEFAULT_RPC_MESSAGES, RPC_FAILED, callRpc, createOrAdopt, createSupabaseProvider };
|
|
943
|
-
//# sourceMappingURL=chunk-
|
|
944
|
-
//# sourceMappingURL=chunk-
|
|
1182
|
+
//# sourceMappingURL=chunk-5PCGMSDX.js.map
|
|
1183
|
+
//# sourceMappingURL=chunk-5PCGMSDX.js.map
|