@marktiderman/genesis-core 1.12.0 → 1.14.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 +77 -0
- package/dist/chunk-3B4IFDMK.js +117 -0
- package/dist/chunk-3B4IFDMK.js.map +1 -0
- package/dist/{chunk-S2ZXUNXB.js → chunk-3LZAZ5KB.js} +3 -3
- package/dist/{chunk-S2ZXUNXB.js.map → chunk-3LZAZ5KB.js.map} +1 -1
- package/dist/chunk-7FVTGKTX.js +8 -0
- package/dist/chunk-7FVTGKTX.js.map +1 -0
- package/dist/{chunk-NMFABW4Y.js → chunk-7YHGNPPO.js} +5 -5
- package/dist/{chunk-NMFABW4Y.js.map → chunk-7YHGNPPO.js.map} +1 -1
- package/dist/chunk-AUVKRXRC.js +81 -0
- package/dist/chunk-AUVKRXRC.js.map +1 -0
- package/dist/{chunk-TOU2CMDH.js → chunk-K7K6NY3C.js} +168 -168
- package/dist/chunk-K7K6NY3C.js.map +1 -0
- package/dist/chunk-LFWSTJWI.js +108 -0
- package/dist/chunk-LFWSTJWI.js.map +1 -0
- package/dist/{chunk-RCAP3Q4T.js → chunk-OYIUIW7C.js} +3 -2
- package/dist/chunk-OYIUIW7C.js.map +1 -0
- package/dist/chunk-OZYF55E3.js +6 -0
- package/dist/chunk-OZYF55E3.js.map +1 -0
- package/dist/chunk-PA6WJE5E.js +115 -0
- package/dist/chunk-PA6WJE5E.js.map +1 -0
- package/dist/chunk-PP3XFFQM.js +85 -0
- package/dist/chunk-PP3XFFQM.js.map +1 -0
- package/dist/{chunk-BGTXN2E6.js → chunk-RF6YVWHX.js} +4 -4
- package/dist/{chunk-BGTXN2E6.js.map → chunk-RF6YVWHX.js.map} +1 -1
- package/dist/{chunk-NZPJI7FC.js → chunk-UGGMJUFQ.js} +45 -13
- package/dist/chunk-UGGMJUFQ.js.map +1 -0
- package/dist/{chunk-KXVHDK3Q.js → chunk-UV3ROHQA.js} +27 -5
- package/dist/chunk-UV3ROHQA.js.map +1 -0
- package/dist/chunk-YDDPFSQ6.js +35 -0
- package/dist/chunk-YDDPFSQ6.js.map +1 -0
- package/dist/{chunk-OSPLIKSH.js → chunk-YFTGLQDQ.js} +18 -8
- package/dist/chunk-YFTGLQDQ.js.map +1 -0
- package/dist/chunk-ZIMQUFXD.js +154 -0
- package/dist/chunk-ZIMQUFXD.js.map +1 -0
- package/dist/csv.d.ts +41 -0
- package/dist/csv.js +77 -0
- package/dist/csv.js.map +1 -0
- package/dist/dates.js +1 -115
- package/dist/dates.js.map +1 -1
- package/dist/errors.d.ts +69 -0
- package/dist/errors.js +7 -0
- package/dist/errors.js.map +1 -0
- package/dist/hooks/index.d.ts +7 -43
- package/dist/hooks/index.js +14 -11
- package/dist/hooks/live-cache.d.ts +15 -0
- package/dist/hooks/live-cache.js +3 -0
- package/dist/hooks/live-cache.js.map +1 -0
- package/dist/hooks/mutation-mode.d.ts +2 -2
- package/dist/hooks/mutation-mode.js +2 -2
- package/dist/hooks/related-keys.d.ts +2 -2
- package/dist/hooks/use-capabilities.d.ts +1 -1
- package/dist/hooks/use-infinite-resource.d.ts +18 -2
- package/dist/hooks/use-infinite-resource.js +4 -3
- package/dist/hooks/use-live.d.ts +15 -3
- package/dist/hooks/use-live.js +2 -1
- package/dist/hooks/use-one.d.ts +2 -2
- package/dist/hooks/use-one.js +4 -3
- package/dist/hooks/use-resource-form.d.ts +9 -2
- package/dist/hooks/use-resource-form.js +6 -5
- package/dist/hooks/use-resource.d.ts +2 -2
- package/dist/hooks/use-resource.js +5 -4
- package/dist/hooks/use-write.d.ts +2 -2
- package/dist/index.d.ts +14 -6
- package/dist/index.js +21 -14
- package/dist/provider/index.d.ts +11 -6
- package/dist/provider/index.js +3 -2
- package/dist/resource/index.d.ts +39 -0
- package/dist/resource/index.js +3 -0
- package/dist/resource/index.js.map +1 -0
- package/dist/resource-view/index.d.ts +468 -0
- package/dist/resource-view/index.js +542 -0
- package/dist/resource-view/index.js.map +1 -0
- package/dist/schema/index.d.ts +4 -4
- package/dist/schema/index.js +1 -0
- package/dist/status-BXRokZD9.d.ts +50 -0
- package/dist/testing/index.d.ts +3 -3
- package/dist/{types-B9DDfvWa.d.ts → types-CXhqXPN1.d.ts} +21 -4
- package/dist/{types-C7FJtFXV.d.ts → types-o8j7g6O6.d.ts} +38 -1
- package/dist/urls.d.ts +18 -0
- package/dist/urls.js +3 -0
- package/dist/urls.js.map +1 -0
- package/dist/viewer-scope-77MqxvmZ.d.ts +125 -0
- package/dist/{write-outcome-B2fE29yZ.d.ts → write-outcome-CFnpciUI.d.ts} +1 -1
- package/package.json +21 -1
- package/dist/chunk-5ORRAXIV.js +0 -30
- package/dist/chunk-5ORRAXIV.js.map +0 -1
- package/dist/chunk-KXVHDK3Q.js.map +0 -1
- package/dist/chunk-NZPJI7FC.js.map +0 -1
- package/dist/chunk-OSPLIKSH.js.map +0 -1
- package/dist/chunk-RCAP3Q4T.js.map +0 -1
- package/dist/chunk-TOU2CMDH.js.map +0 -1
|
@@ -1,116 +1,7 @@
|
|
|
1
|
+
import { toDataWriteError, readOutcomeFromCode } from './chunk-PA6WJE5E.js';
|
|
1
2
|
import { DataReadError, readFailureMessage } from './chunk-HUNGCTRA.js';
|
|
2
|
-
import { titleCase } from './chunk-RYEXWQFD.js';
|
|
3
3
|
import { isDataWriteError, DataWriteError } from './chunk-LG6TUBVM.js';
|
|
4
4
|
|
|
5
|
-
// src/provider/postgrest-errors.ts
|
|
6
|
-
function readOutcomeFromCode(code) {
|
|
7
|
-
if (!code) return "error";
|
|
8
|
-
if (code === "42703" || code === "42P01" || /^PGRST1\d\d$/.test(code) && code !== "PGRST116") {
|
|
9
|
-
return "invalid";
|
|
10
|
-
}
|
|
11
|
-
const write = writeOutcomeFromCode(code);
|
|
12
|
-
return write === "forbidden" || write === "gone" || write === "unauthenticated" || write === "invalid" ? write : "error";
|
|
13
|
-
}
|
|
14
|
-
function writeOutcomeFromCode(code) {
|
|
15
|
-
if (!code) return "error";
|
|
16
|
-
if (code === "42501") return "forbidden";
|
|
17
|
-
if (code === "PGRST116") return "gone";
|
|
18
|
-
if (code === "PGRST301" || code === "PGRST302" || code === "PGRST303") return "unauthenticated";
|
|
19
|
-
if (code === "28000" || code === "28P01") return "unauthenticated";
|
|
20
|
-
if (code === "23505") return "duplicate";
|
|
21
|
-
if (/^2[23][0-9A-Z]{3}$/.test(code)) return "invalid";
|
|
22
|
-
return "error";
|
|
23
|
-
}
|
|
24
|
-
var COLUMN = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
25
|
-
var WRAPPED_COLUMN = /^[A-Za-z_][A-Za-z0-9_]*\(\(?([A-Za-z_][A-Za-z0-9_]*)\)?(?:::[A-Za-z_][A-Za-z0-9_ ]*)?\)$/;
|
|
26
|
-
function columnOf(expression) {
|
|
27
|
-
if (COLUMN.test(expression)) return expression;
|
|
28
|
-
return WRAPPED_COLUMN.exec(expression)?.[1];
|
|
29
|
-
}
|
|
30
|
-
function keyColumns(details, tail) {
|
|
31
|
-
if (!details.startsWith("Key (") || !tail.test(details)) return [];
|
|
32
|
-
let depth = 1;
|
|
33
|
-
let end = -1;
|
|
34
|
-
for (let i = "Key (".length; i < details.length; i++) {
|
|
35
|
-
const ch = details[i];
|
|
36
|
-
if (ch === "(") depth++;
|
|
37
|
-
else if (ch === ")" && --depth === 0) {
|
|
38
|
-
end = i;
|
|
39
|
-
break;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (end < 0 || details.slice(end + 1, end + 3) !== "=(") return [];
|
|
43
|
-
const parts = [];
|
|
44
|
-
let level = 0;
|
|
45
|
-
let current = "";
|
|
46
|
-
for (const ch of details.slice("Key (".length, end)) {
|
|
47
|
-
if (ch === "(") level++;
|
|
48
|
-
else if (ch === ")") level--;
|
|
49
|
-
if (ch === "," && level === 0) {
|
|
50
|
-
parts.push(current.trim());
|
|
51
|
-
current = "";
|
|
52
|
-
} else {
|
|
53
|
-
current += ch;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
parts.push(current.trim());
|
|
57
|
-
const columns = parts.map(columnOf);
|
|
58
|
-
return columns.length > 0 && columns.every((c) => !!c) ? columns : [];
|
|
59
|
-
}
|
|
60
|
-
function listLabels(columns) {
|
|
61
|
-
const labels = columns.map((c) => titleCase(c));
|
|
62
|
-
if (labels.length === 1) return labels[0];
|
|
63
|
-
return `${labels.slice(0, -1).join(", ")} and ${labels[labels.length - 1]}`;
|
|
64
|
-
}
|
|
65
|
-
function fieldErrorsFromPostgrest(error) {
|
|
66
|
-
const code = typeof error.code === "string" ? error.code : void 0;
|
|
67
|
-
const details = typeof error.details === "string" ? error.details.trim() : "";
|
|
68
|
-
const message = typeof error.message === "string" ? error.message : "";
|
|
69
|
-
if (code === "23505") {
|
|
70
|
-
const columns = keyColumns(details, /\) already exists\.?$/);
|
|
71
|
-
if (columns.length === 0) return { fields: {}, message: "This value is already in use." };
|
|
72
|
-
const each = columns.length === 1 ? "This value is already in use." : "This combination is already in use.";
|
|
73
|
-
return {
|
|
74
|
-
fields: Object.fromEntries(columns.map((c) => [c, each])),
|
|
75
|
-
message: columns.length === 1 ? `${listLabels(columns)} is already in use.` : `This combination of ${listLabels(columns)} is already in use.`
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
if (code === "23502") {
|
|
79
|
-
const column = /^null value in column "([A-Za-z_][A-Za-z0-9_]*)"(?: |$)/.exec(message)?.[1];
|
|
80
|
-
if (!column || !COLUMN.test(column)) return void 0;
|
|
81
|
-
return {
|
|
82
|
-
fields: { [column]: "This field is required." },
|
|
83
|
-
message: `${titleCase(column)} is required.`
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
if (code === "23503") {
|
|
87
|
-
const tail = /\) is not present in table "[^"]*"\.?$/;
|
|
88
|
-
if (!tail.test(details)) return void 0;
|
|
89
|
-
const columns = keyColumns(details, tail);
|
|
90
|
-
if (columns.length === 0) return { fields: {}, message: "The selected item doesn't exist." };
|
|
91
|
-
return {
|
|
92
|
-
fields: Object.fromEntries(columns.map((c) => [c, "The selected item doesn't exist."])),
|
|
93
|
-
message: `${listLabels(columns)}: the selected item doesn't exist.`
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
return void 0;
|
|
97
|
-
}
|
|
98
|
-
function toDataWriteError(error) {
|
|
99
|
-
if (isDataWriteError(error)) return error;
|
|
100
|
-
const shape = typeof error === "object" && error !== null ? error : {};
|
|
101
|
-
const code = typeof shape.code === "string" ? shape.code : void 0;
|
|
102
|
-
const mapped = fieldErrorsFromPostgrest(shape);
|
|
103
|
-
if (mapped) {
|
|
104
|
-
return new DataWriteError(writeOutcomeFromCode(code), mapped.message, {
|
|
105
|
-
code,
|
|
106
|
-
fields: Object.keys(mapped.fields).length > 0 ? mapped.fields : void 0,
|
|
107
|
-
cause: error
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
const message = typeof shape.message === "string" && shape.message ? shape.message : error instanceof Error ? error.message : String(error);
|
|
111
|
-
return new DataWriteError(writeOutcomeFromCode(code), message, { code, cause: error });
|
|
112
|
-
}
|
|
113
|
-
|
|
114
5
|
// src/provider/supabase-provider.ts
|
|
115
6
|
function isMissingColumn(error, table, column) {
|
|
116
7
|
if (!error || typeof error !== "object") return false;
|
|
@@ -154,6 +45,7 @@ function decodeCursor(cursor) {
|
|
|
154
45
|
const state = JSON.parse(new TextDecoder().decode(bytes));
|
|
155
46
|
if (!state || typeof state !== "object" || typeof state.k !== "string" || !isIdentifier(state.id)) return void 0;
|
|
156
47
|
if (state.o !== "asc" && state.o !== "desc") return void 0;
|
|
48
|
+
if (state.d !== void 0 && state.d !== "b") return void 0;
|
|
157
49
|
return state;
|
|
158
50
|
} catch {
|
|
159
51
|
return void 0;
|
|
@@ -163,6 +55,7 @@ function quoteFilterValue(value) {
|
|
|
163
55
|
return `"${String(value).replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
164
56
|
}
|
|
165
57
|
function keysetClauses(state) {
|
|
58
|
+
if (state.d === "b") return keysetClausesBefore(state);
|
|
166
59
|
const k = state.k;
|
|
167
60
|
const K = quoteFilterValue(state.id);
|
|
168
61
|
if (state.s === null) return `${k}.${state.o === "asc" ? "gt" : "lt"}.${K}`;
|
|
@@ -177,12 +70,38 @@ function keysetClauses(state) {
|
|
|
177
70
|
const ties = afterInTies(`${s}.eq.${V}`);
|
|
178
71
|
return state.o === "asc" ? `${s}.gt.${V},${ties},${s}.is.null` : `${s}.lt.${V},${ties}`;
|
|
179
72
|
}
|
|
73
|
+
function keysetClausesBefore(state) {
|
|
74
|
+
const k = state.k;
|
|
75
|
+
const K = quoteFilterValue(state.id);
|
|
76
|
+
if (state.s === null) return `${k}.${state.o === "asc" ? "lt" : "gt"}.${K}`;
|
|
77
|
+
const s = state.s;
|
|
78
|
+
const v = state.v;
|
|
79
|
+
const beforeInTies = (eq) => `and(${eq},${k}.lt.${K})`;
|
|
80
|
+
if (v === null || v === void 0) {
|
|
81
|
+
const ties2 = beforeInTies(`${s}.is.null`);
|
|
82
|
+
return state.o === "asc" ? `${s}.not.is.null,${ties2}` : ties2;
|
|
83
|
+
}
|
|
84
|
+
const V = quoteFilterValue(v);
|
|
85
|
+
const ties = beforeInTies(`${s}.eq.${V}`);
|
|
86
|
+
return state.o === "asc" ? `${s}.lt.${V},${ties}` : `${s}.gt.${V},${ties},${s}.is.null`;
|
|
87
|
+
}
|
|
180
88
|
function realtimeTable(resource) {
|
|
181
89
|
const dot = resource.indexOf(".");
|
|
182
90
|
const strip = (name) => name.replace(/"/g, "");
|
|
183
91
|
return dot < 0 ? { schema: "public", table: strip(resource) } : { schema: strip(resource.slice(0, dot)), table: strip(resource.slice(dot + 1)) };
|
|
184
92
|
}
|
|
185
93
|
var realtimeChannelSeq = 0;
|
|
94
|
+
var REALTIME_ECHO_MS = 2e3;
|
|
95
|
+
function realtimeFilter(filters) {
|
|
96
|
+
for (const f of filters ?? []) {
|
|
97
|
+
if (f.operator !== "eq" || !/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(f.field)) continue;
|
|
98
|
+
const v = f.value;
|
|
99
|
+
if (typeof v !== "string" && typeof v !== "number" && typeof v !== "boolean") continue;
|
|
100
|
+
if (typeof v === "string" && (v === "" || /[,()]/.test(v))) continue;
|
|
101
|
+
return `${f.field}=eq.${String(v)}`;
|
|
102
|
+
}
|
|
103
|
+
return void 0;
|
|
104
|
+
}
|
|
186
105
|
var warnedRealtime = /* @__PURE__ */ new Set();
|
|
187
106
|
function createSupabaseProvider(client, options = {}) {
|
|
188
107
|
function applyFilters(query, filters) {
|
|
@@ -252,6 +171,9 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
252
171
|
await reportReadError(error, resource, operation);
|
|
253
172
|
}
|
|
254
173
|
const from = (resource) => client.from(resource);
|
|
174
|
+
function countMode(resource, params) {
|
|
175
|
+
return params.count ?? configFor(resource).count ?? "exact";
|
|
176
|
+
}
|
|
255
177
|
function readName(resource) {
|
|
256
178
|
const source = configFor(resource).source;
|
|
257
179
|
return typeof source === "string" ? source : source ? source.rpc : resource;
|
|
@@ -283,6 +205,29 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
283
205
|
}
|
|
284
206
|
const configFor = (resource) => options.resources?.[resource] ?? {};
|
|
285
207
|
const nounFor = (resource) => configFor(resource).noun ?? "record";
|
|
208
|
+
const recentWrites = /* @__PURE__ */ new Map();
|
|
209
|
+
function noteWrite(event) {
|
|
210
|
+
const now = Date.now();
|
|
211
|
+
for (const [key, mark] of recentWrites) if (now - mark.at > REALTIME_ECHO_MS) recentWrites.delete(key);
|
|
212
|
+
const eventType = event.kind === "create" ? "INSERT" : event.kind === "delete" ? "DELETE" : "UPDATE";
|
|
213
|
+
const openChannels = new Set(
|
|
214
|
+
[...sharedChannels.entries()].filter(([t]) => t.startsWith(`${event.resource}\0`)).map(([, c]) => c)
|
|
215
|
+
);
|
|
216
|
+
recentWrites.set(`${event.resource}\0${String(event.id)}`, { eventType, at: now, heardOn: /* @__PURE__ */ new Set(), openChannels });
|
|
217
|
+
}
|
|
218
|
+
const sharedChannels = /* @__PURE__ */ new Map();
|
|
219
|
+
function isEcho(resource, id, eventType, topic, channelObject) {
|
|
220
|
+
if (!isIdentifier(id)) return false;
|
|
221
|
+
const key = `${resource}\0${String(id)}`;
|
|
222
|
+
const mark = recentWrites.get(key);
|
|
223
|
+
if (!mark || mark.eventType !== eventType || mark.heardOn.has(topic) || !channelObject || !mark.openChannels.has(channelObject)) return false;
|
|
224
|
+
if (Date.now() - mark.at > REALTIME_ECHO_MS) {
|
|
225
|
+
recentWrites.delete(key);
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
mark.heardOn.add(topic);
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
286
231
|
function realtimeEnabled(resource) {
|
|
287
232
|
if (typeof client.channel !== "function") return false;
|
|
288
233
|
const own = configFor(resource).realtime;
|
|
@@ -349,6 +294,7 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
349
294
|
}
|
|
350
295
|
}
|
|
351
296
|
async function committed(event) {
|
|
297
|
+
if (event.id != null) noteWrite(event);
|
|
352
298
|
if (!options.onWrite) return;
|
|
353
299
|
try {
|
|
354
300
|
await options.onWrite(event);
|
|
@@ -395,43 +341,59 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
395
341
|
return refuseCursor(resource, "the cursor was made for a different sort; start again from the first page (cursor: null).");
|
|
396
342
|
}
|
|
397
343
|
}
|
|
398
|
-
|
|
344
|
+
const mode = countMode(resource, params);
|
|
345
|
+
let query = read(resource, params.select, cursor || mode === "none" ? void 0 : mode);
|
|
399
346
|
query = applyFilters(query, params.filters);
|
|
400
347
|
const search = searchClauses(resource, params);
|
|
401
348
|
const keyset = after ? keysetClauses(after) : "";
|
|
402
349
|
if (search && keyset) query = query.or(`and(or(${search}),or(${keyset}))`);
|
|
403
350
|
else if (search || keyset) query = query.or(search || keyset);
|
|
351
|
+
const back = after?.d === "b";
|
|
404
352
|
if (sortField) {
|
|
405
|
-
|
|
406
|
-
query = query.order(
|
|
353
|
+
const ascending = order === "asc" !== back;
|
|
354
|
+
query = query.order(sortField, { ascending, nullsFirst: !ascending });
|
|
355
|
+
query = query.order(key, { ascending: !back });
|
|
407
356
|
} else {
|
|
408
|
-
query = query.order(key, { ascending: order === "asc" });
|
|
357
|
+
query = query.order(key, { ascending: order === "asc" !== back });
|
|
409
358
|
}
|
|
410
359
|
query = query.range(0, perPage);
|
|
411
360
|
const result = await query;
|
|
412
361
|
await throwIfReadError(result, resource, "getList");
|
|
413
362
|
const rows = result.data ?? [];
|
|
363
|
+
const more = rows.length > perPage;
|
|
414
364
|
const page = rows.slice(0, perPage);
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
const id =
|
|
419
|
-
const v = sortField ?
|
|
420
|
-
if (!isIdentifier(id) || sortField && !(sortField in
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
if (v !== void 0 && v !== null && typeof v === "object") {
|
|
424
|
-
return refuseCursor(resource, `cursor paging can't sort by "${sortField}": its values aren't plain (text, number, date, boolean).`);
|
|
425
|
-
}
|
|
426
|
-
nextCursor = encodeCursor({
|
|
365
|
+
if (back) page.reverse();
|
|
366
|
+
const cursorAt = (row, direction) => {
|
|
367
|
+
if (!row) return null;
|
|
368
|
+
const id = row[key];
|
|
369
|
+
const v = sortField ? row[sortField] : void 0;
|
|
370
|
+
if (!isIdentifier(id) || sortField && !(sortField in row)) return false;
|
|
371
|
+
if (v !== void 0 && v !== null && typeof v === "object") return false;
|
|
372
|
+
return encodeCursor({
|
|
427
373
|
s: sortField,
|
|
428
374
|
o: order,
|
|
429
375
|
k: key,
|
|
430
376
|
...sortField ? { v: v ?? null } : {},
|
|
431
|
-
id
|
|
377
|
+
id,
|
|
378
|
+
...direction ? { d: direction } : {}
|
|
432
379
|
});
|
|
380
|
+
};
|
|
381
|
+
const next = back ? cursorAt(page[page.length - 1]) : more ? cursorAt(page[page.length - 1]) : null;
|
|
382
|
+
const prev = back ? more ? cursorAt(page[0], "b") : null : cursor ? cursorAt(page[0], "b") : null;
|
|
383
|
+
if (next === false || prev === false) {
|
|
384
|
+
const objectValued = !!sortField && page.some((row) => sortField in row && row[sortField] !== null && typeof row[sortField] === "object");
|
|
385
|
+
if (objectValued) {
|
|
386
|
+
return refuseCursor(resource, `cursor paging can't sort by "${sortField}": its values aren't plain (text, number, date, boolean).`);
|
|
387
|
+
}
|
|
388
|
+
return refuseCursor(resource, `the selected columns must include "${key}"${sortField ? ` and "${sortField}"` : ""} to page by cursor.`);
|
|
433
389
|
}
|
|
434
|
-
return {
|
|
390
|
+
return {
|
|
391
|
+
data: page,
|
|
392
|
+
total: cursor || mode === "none" ? -1 : result.count ?? 0,
|
|
393
|
+
...!cursor && (mode === "estimated" || mode === "planned") ? { totalIsEstimate: true } : {},
|
|
394
|
+
nextCursor: next,
|
|
395
|
+
prevCursor: prev
|
|
396
|
+
};
|
|
435
397
|
}
|
|
436
398
|
const provider = {
|
|
437
399
|
async getList(resource, params) {
|
|
@@ -455,8 +417,9 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
455
417
|
if (params.pagination && params.pagination.cursor !== void 0) {
|
|
456
418
|
return getCursorPage(resource, params, configuredKey === void 0 ? "id" : configuredKey);
|
|
457
419
|
}
|
|
420
|
+
const mode = countMode(resource, params);
|
|
458
421
|
const run2 = (key) => {
|
|
459
|
-
let query = read(resource, params.select, "
|
|
422
|
+
let query = read(resource, params.select, mode === "none" ? void 0 : mode);
|
|
460
423
|
query = applyFilters(query, params.filters);
|
|
461
424
|
const clauses = searchClauses(resource, params);
|
|
462
425
|
if (clauses) {
|
|
@@ -490,7 +453,11 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
490
453
|
result = await run2(false);
|
|
491
454
|
}
|
|
492
455
|
await throwIfReadError(result, resource, "getList");
|
|
493
|
-
return {
|
|
456
|
+
return {
|
|
457
|
+
data: result.data ?? [],
|
|
458
|
+
total: mode === "none" ? -1 : result.count ?? 0,
|
|
459
|
+
...mode === "estimated" || mode === "planned" ? { totalIsEstimate: true } : {}
|
|
460
|
+
};
|
|
494
461
|
},
|
|
495
462
|
async getOne(resource, params) {
|
|
496
463
|
const result = await read(resource, params.select).eq("id", params.id).single();
|
|
@@ -532,49 +499,81 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
532
499
|
};
|
|
533
500
|
}
|
|
534
501
|
const { schema, table } = realtimeTable(resource);
|
|
535
|
-
const
|
|
536
|
-
|
|
537
|
-
);
|
|
538
|
-
let
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
{ event: "*", schema, table },
|
|
552
|
-
(payload) => {
|
|
502
|
+
const serverFilter = params.id != null || configFor(resource).source !== void 0 ? void 0 : realtimeFilter(params.filters);
|
|
503
|
+
const topic = `${resource}\0${serverFilter ?? ""}`;
|
|
504
|
+
const sendRecord = configFor(resource).source === void 0;
|
|
505
|
+
let shared = sharedChannels.get(topic);
|
|
506
|
+
if (!shared) {
|
|
507
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
508
|
+
const channel = client.channel(`genesis:${resource}:${++realtimeChannelSeq}`);
|
|
509
|
+
let subscribedOnce = false;
|
|
510
|
+
let interrupted = false;
|
|
511
|
+
const broadcast = (event, rowId) => {
|
|
512
|
+
for (const l of listeners) {
|
|
513
|
+
if (l.id != null && isIdentifier(rowId) && String(rowId) !== String(l.id)) continue;
|
|
514
|
+
l.emit(event);
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
const onChange = (payload) => {
|
|
553
518
|
const row = payload.eventType === "DELETE" ? payload.old : payload.new;
|
|
554
519
|
const id = row?.["id"];
|
|
555
|
-
if (
|
|
520
|
+
if (isEcho(resource, id, payload.eventType, topic, sharedChannels.get(topic))) return;
|
|
556
521
|
const type = payload.eventType === "INSERT" ? "created" : payload.eventType === "UPDATE" ? "updated" : "deleted";
|
|
557
|
-
|
|
522
|
+
const event = isIdentifier(id) ? sendRecord && type !== "deleted" && row ? { type, id, record: row } : { type, id } : { type };
|
|
523
|
+
broadcast(event, id);
|
|
524
|
+
};
|
|
525
|
+
if (serverFilter) {
|
|
526
|
+
channel.on("postgres_changes", { event: "INSERT", schema, table, filter: serverFilter }, onChange);
|
|
527
|
+
channel.on("postgres_changes", { event: "UPDATE", schema, table }, onChange);
|
|
528
|
+
channel.on("postgres_changes", { event: "DELETE", schema, table }, onChange);
|
|
529
|
+
} else {
|
|
530
|
+
channel.on("postgres_changes", { event: "*", schema, table }, onChange);
|
|
558
531
|
}
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
532
|
+
channel.subscribe((status) => {
|
|
533
|
+
if (status === "SUBSCRIBED") {
|
|
534
|
+
if (subscribedOnce && interrupted) broadcast({ type: "changed" });
|
|
535
|
+
subscribedOnce = true;
|
|
536
|
+
interrupted = false;
|
|
537
|
+
} else if (status === "CHANNEL_ERROR" || status === "TIMED_OUT" || status === "CLOSED") {
|
|
538
|
+
interrupted = true;
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
shared = {
|
|
542
|
+
listeners,
|
|
543
|
+
close: () => {
|
|
544
|
+
try {
|
|
545
|
+
const done = typeof client.removeChannel === "function" ? client.removeChannel(channel) : channel.unsubscribe?.();
|
|
546
|
+
if (done && typeof done.catch === "function") {
|
|
547
|
+
done.catch(() => {
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
} catch {
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
sharedChannels.set(topic, shared);
|
|
555
|
+
}
|
|
556
|
+
let closed = false;
|
|
557
|
+
const listener = {
|
|
558
|
+
id: params.id,
|
|
559
|
+
emit: (event) => {
|
|
560
|
+
if (closed) return;
|
|
561
|
+
try {
|
|
562
|
+
callback(event);
|
|
563
|
+
} catch (error) {
|
|
564
|
+
console.error(`[SupabaseProvider] a realtime callback for "${resource}" threw:`, error);
|
|
565
|
+
}
|
|
566
566
|
}
|
|
567
|
-
}
|
|
567
|
+
};
|
|
568
|
+
shared.listeners.add(listener);
|
|
569
|
+
const mine = shared;
|
|
568
570
|
return () => {
|
|
569
571
|
if (closed) return;
|
|
570
572
|
closed = true;
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
});
|
|
576
|
-
}
|
|
577
|
-
} catch {
|
|
573
|
+
mine.listeners.delete(listener);
|
|
574
|
+
if (mine.listeners.size === 0 && sharedChannels.get(topic) === mine) {
|
|
575
|
+
sharedChannels.delete(topic);
|
|
576
|
+
mine.close();
|
|
578
577
|
}
|
|
579
578
|
};
|
|
580
579
|
},
|
|
@@ -685,6 +684,7 @@ function createSupabaseProvider(client, options = {}) {
|
|
|
685
684
|
return typeof keys === "function" ? keys(event) : keys;
|
|
686
685
|
}
|
|
687
686
|
};
|
|
687
|
+
Object.defineProperty(provider, "__recentWriteCount", { value: () => recentWrites.size, enumerable: false });
|
|
688
688
|
return provider;
|
|
689
689
|
}
|
|
690
690
|
|
|
@@ -750,6 +750,6 @@ async function createOrAdopt(provider, resource, data, options) {
|
|
|
750
750
|
}
|
|
751
751
|
}
|
|
752
752
|
|
|
753
|
-
export { DEFAULT_RPC_MESSAGES, RPC_FAILED, callRpc, createOrAdopt, createSupabaseProvider
|
|
754
|
-
//# sourceMappingURL=chunk-
|
|
755
|
-
//# sourceMappingURL=chunk-
|
|
753
|
+
export { DEFAULT_RPC_MESSAGES, RPC_FAILED, callRpc, createOrAdopt, createSupabaseProvider };
|
|
754
|
+
//# sourceMappingURL=chunk-K7K6NY3C.js.map
|
|
755
|
+
//# sourceMappingURL=chunk-K7K6NY3C.js.map
|