@aiao/rxdb-adapter-supabase 0.0.18 → 0.0.20
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 +1 -1
- package/dist/RxDBAdapterSupabase.d.ts +2 -1
- package/dist/RxDBAdapterSupabase.d.ts.map +1 -1
- package/dist/SupabaseRepository.d.ts +40 -7
- package/dist/SupabaseRepository.d.ts.map +1 -1
- package/dist/SupabaseTreeRepository.d.ts.map +1 -1
- package/dist/index.js +925 -1030
- package/dist/rule_group_builder.d.ts.map +1 -1
- package/dist/schema.utils.d.ts +2 -0
- package/dist/schema.utils.d.ts.map +1 -0
- package/dist/supabase.interface.d.ts +22 -0
- package/dist/supabase.interface.d.ts.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,1051 +1,946 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createClient as
|
|
3
|
-
import { defer as
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
class
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
UPDATE: Q,
|
|
27
|
-
DELETE: L
|
|
1
|
+
import { EntityRemoteCreatedEvent as e, EntityRemoteRemovedEvent as t, EntityRemoteUpdatedEvent as n, PropertyType as r, RepositoryBase as i, RxDBAdapterRemoteBase as a, getEntityMetadata as o, isRuleGroup as s } from "@aiao/rxdb";
|
|
2
|
+
import { createClient as c } from "@supabase/supabase-js";
|
|
3
|
+
import { defer as l, from as u, map as d, of as f } from "rxjs";
|
|
4
|
+
//#region src/errors.ts
|
|
5
|
+
var p = class extends Error {
|
|
6
|
+
code;
|
|
7
|
+
constructor(e, t) {
|
|
8
|
+
super(e), this.code = t, this.name = "SupabaseSyncError";
|
|
9
|
+
}
|
|
10
|
+
}, m = class extends p {
|
|
11
|
+
constructor(e) {
|
|
12
|
+
super(e, "CONFIG_ERROR"), this.name = "SupabaseConfigError";
|
|
13
|
+
}
|
|
14
|
+
}, h = class extends p {
|
|
15
|
+
constructor(e) {
|
|
16
|
+
super(e, "NETWORK_ERROR"), this.name = "SupabaseNetworkError";
|
|
17
|
+
}
|
|
18
|
+
}, g = class extends p {
|
|
19
|
+
constructor(e) {
|
|
20
|
+
super(e, "DATA_ERROR"), this.name = "SupabaseDataError";
|
|
21
|
+
}
|
|
22
|
+
}, _ = {
|
|
23
|
+
INSERT: e,
|
|
24
|
+
UPDATE: n,
|
|
25
|
+
DELETE: t
|
|
28
26
|
};
|
|
29
|
-
function
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
);
|
|
27
|
+
function v(e, t) {
|
|
28
|
+
if (t.table !== "rxdb_change" || t.eventType !== "INSERT") return;
|
|
29
|
+
let n = t.new;
|
|
30
|
+
if (!n) return;
|
|
31
|
+
let { namespace: r, entity: i, entityId: a, type: o, branchId: s, patch: c, clientId: l } = n, u = e.rxdb.context.clientId;
|
|
32
|
+
if (l && u && l === u || !a || !o) return;
|
|
33
|
+
let d = o, f = _[d];
|
|
34
|
+
if (!f) return;
|
|
35
|
+
let p = {
|
|
36
|
+
id: a,
|
|
37
|
+
...c
|
|
38
|
+
};
|
|
39
|
+
e.rxdb.dispatchEvent(new f([{
|
|
40
|
+
type: d,
|
|
41
|
+
namespace: r || "public",
|
|
42
|
+
entity: i,
|
|
43
|
+
id: a,
|
|
44
|
+
branchId: s,
|
|
45
|
+
data: p,
|
|
46
|
+
recordAt: n.createdAt ? new Date(n.createdAt) : /* @__PURE__ */ new Date()
|
|
47
|
+
}]));
|
|
51
48
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/rule_group_builder.ts
|
|
51
|
+
var y = {
|
|
52
|
+
"=": "eq",
|
|
53
|
+
"!=": "neq",
|
|
54
|
+
"<": "lt",
|
|
55
|
+
"<=": "lte",
|
|
56
|
+
">": "gt",
|
|
57
|
+
">=": "gte",
|
|
58
|
+
in: "in",
|
|
59
|
+
contains: "ilike",
|
|
60
|
+
includes: "ilike",
|
|
61
|
+
startsWith: "ilike",
|
|
62
|
+
endsWith: "ilike"
|
|
64
63
|
};
|
|
65
|
-
function
|
|
66
|
-
|
|
64
|
+
function b(e) {
|
|
65
|
+
return e instanceof Date ? e.toISOString() : Array.isArray(e) ? e.map(b) : e;
|
|
67
66
|
}
|
|
68
|
-
function
|
|
69
|
-
|
|
67
|
+
function x(e, t) {
|
|
68
|
+
let n = b(e);
|
|
69
|
+
if (!Array.isArray(n) || n.length !== 2) throw Error(`${t} operator requires a two-item array`);
|
|
70
|
+
return [n[0], n[1]];
|
|
70
71
|
}
|
|
71
|
-
function
|
|
72
|
-
|
|
73
|
-
switch (c) {
|
|
74
|
-
case "=":
|
|
75
|
-
case "!=":
|
|
76
|
-
case "<":
|
|
77
|
-
case "<=":
|
|
78
|
-
case ">":
|
|
79
|
-
case ">=":
|
|
80
|
-
return `${q[c]}.${e}`;
|
|
81
|
-
case "in":
|
|
82
|
-
if (!Array.isArray(e)) throw new Error("IN operator requires array");
|
|
83
|
-
return `in.(${e.join(",")})`;
|
|
84
|
-
case "contains":
|
|
85
|
-
case "includes":
|
|
86
|
-
return `ilike.*${e}*`;
|
|
87
|
-
case "startsWith":
|
|
88
|
-
return `ilike.${e}*`;
|
|
89
|
-
case "endsWith":
|
|
90
|
-
return `ilike.*${e}`;
|
|
91
|
-
default:
|
|
92
|
-
throw new Error(`Unsupported operator: ${c}`);
|
|
93
|
-
}
|
|
72
|
+
function S(e, t, n) {
|
|
73
|
+
return !t || !e.mappedEntity ? n : t.getEntityMetadata(e.mappedEntity, e.mappedNamespace ?? "") ?? n;
|
|
94
74
|
}
|
|
95
|
-
function
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
75
|
+
function C(e, t) {
|
|
76
|
+
let n = b(t);
|
|
77
|
+
switch (e) {
|
|
78
|
+
case "=":
|
|
79
|
+
case "!=":
|
|
80
|
+
case "<":
|
|
81
|
+
case "<=":
|
|
82
|
+
case ">":
|
|
83
|
+
case ">=": return `${y[e]}.${n}`;
|
|
84
|
+
case "in":
|
|
85
|
+
if (!Array.isArray(n)) throw Error("IN operator requires array");
|
|
86
|
+
return `in.(${n.join(",")})`;
|
|
87
|
+
case "contains":
|
|
88
|
+
case "includes": return `ilike.*${n}*`;
|
|
89
|
+
case "startsWith": return `ilike.${n}*`;
|
|
90
|
+
case "endsWith": return `ilike.*${n}`;
|
|
91
|
+
default: throw Error(`Unsupported operator: ${e}`);
|
|
92
|
+
}
|
|
102
93
|
}
|
|
103
|
-
function
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
return `${r}.${tt(i, o)}`;
|
|
118
|
-
});
|
|
119
|
-
return c.or(s.join(","));
|
|
94
|
+
function w(e, t, n) {
|
|
95
|
+
let [r, ...i] = e.split(".");
|
|
96
|
+
return i.length === 0 ? {
|
|
97
|
+
field: e,
|
|
98
|
+
operator: t,
|
|
99
|
+
value: n
|
|
100
|
+
} : {
|
|
101
|
+
field: r,
|
|
102
|
+
operator: "exists",
|
|
103
|
+
where: {
|
|
104
|
+
combinator: "and",
|
|
105
|
+
rules: [w(i.join("."), t, n)]
|
|
106
|
+
}
|
|
107
|
+
};
|
|
120
108
|
}
|
|
121
|
-
function
|
|
122
|
-
|
|
123
|
-
|
|
109
|
+
function T(e, t, n, r) {
|
|
110
|
+
if (!t?.rules?.length) return e;
|
|
111
|
+
if (t.rules.length === 1) {
|
|
112
|
+
let i = t.rules[0];
|
|
113
|
+
return s(i) ? T(e, i, n, r) : E(e, i, n, r);
|
|
114
|
+
}
|
|
115
|
+
if (t.combinator === "and") return t.rules.reduce((e, t) => s(t) ? T(e, t, n, r) : E(e, t, n, r), e);
|
|
116
|
+
let i = t.rules.map((e) => {
|
|
117
|
+
if (s(e)) throw Error("Nested OR conditions not supported");
|
|
118
|
+
let { field: t, operator: n, value: r } = e;
|
|
119
|
+
return `${t}.${C(n, r)}`;
|
|
120
|
+
});
|
|
121
|
+
return e.or(i.join(","));
|
|
124
122
|
}
|
|
125
|
-
function
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
throw new Error(`Relation '${i}' not found in '${e?.name || "unknown"}'`);
|
|
129
|
-
const d = o.mappedEntity;
|
|
130
|
-
if (!r?.rules?.length)
|
|
131
|
-
return a === "exists" ? c.not(d, "is", null) : c.is(d, null);
|
|
132
|
-
const u = e ? O(o, n, e) : void 0;
|
|
133
|
-
if (!u) throw new Error(`Cannot resolve metadata for '${i}'`);
|
|
134
|
-
return v(c, d, r, u, n, d);
|
|
123
|
+
function E(e, t, n, r) {
|
|
124
|
+
let { field: i, operator: a, value: o } = t;
|
|
125
|
+
return typeof i == "string" && i.includes(".") ? E(e, w(i, a, o), n, r) : a === "exists" || a === "notExists" ? D(e, t, n, r) : k(e, i, a, o);
|
|
135
126
|
}
|
|
136
|
-
function
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
const { field: o, operator: d, value: u, where: b } = i;
|
|
145
|
-
if (d === "exists" || d === "notExists") {
|
|
146
|
-
const g = n?.relationMap?.get(String(o));
|
|
147
|
-
if (!g) throw new Error(`Relation '${o}' not found in ${t}`);
|
|
148
|
-
const h = g.mappedEntity, w = b, $ = O(g, s, n);
|
|
149
|
-
if (w?.rules?.length) {
|
|
150
|
-
const l = a ? `${a}.${h}` : h;
|
|
151
|
-
r = v(r, h, w, $, s, l);
|
|
152
|
-
} else
|
|
153
|
-
r = d === "exists" ? r.not(h, "is", null) : r.is(h, null);
|
|
154
|
-
continue;
|
|
155
|
-
}
|
|
156
|
-
const m = a ? `${a}.${o}` : `${t}.${o}`;
|
|
157
|
-
r = K(r, m, d, u);
|
|
158
|
-
}
|
|
159
|
-
return r;
|
|
127
|
+
function D(e, t, n, r) {
|
|
128
|
+
let { field: i, operator: a, where: o } = t, s = String(i), c = n?.relationMap?.get(s);
|
|
129
|
+
if (!c) throw Error(`Relation '${s}' not found in '${n?.name || "unknown"}'`);
|
|
130
|
+
let l = n ? S(c, r, n) : void 0;
|
|
131
|
+
if (!l) throw Error(`Cannot resolve metadata for '${s}'`);
|
|
132
|
+
return o?.rules?.length ? O(a === "exists" ? e.not(s, "is", null) : e, s, o, l, r, s) : a === "exists" ? e.not(s, "is", null) : e.is(s, null);
|
|
160
133
|
}
|
|
161
|
-
function
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
case "notContains":
|
|
188
|
-
return c.not(t, "ilike", `%${s}%`);
|
|
189
|
-
case "startsWith":
|
|
190
|
-
return c.ilike(t, `${s}%`);
|
|
191
|
-
case "notStartsWith":
|
|
192
|
-
return c.not(t, "ilike", `${s}%`);
|
|
193
|
-
case "endsWith":
|
|
194
|
-
return c.ilike(t, `%${s}`);
|
|
195
|
-
case "notEndsWith":
|
|
196
|
-
return c.not(t, "ilike", `%${s}`);
|
|
197
|
-
case "between": {
|
|
198
|
-
const [r, i] = s;
|
|
199
|
-
return c.gte(t, r).lte(t, i);
|
|
200
|
-
}
|
|
201
|
-
case "notBetween": {
|
|
202
|
-
const [r, i] = s;
|
|
203
|
-
return c.or(`${t}.lt.${r},${t}.gt.${i}`);
|
|
204
|
-
}
|
|
205
|
-
case "null":
|
|
206
|
-
case "isNull":
|
|
207
|
-
return c.is(t, null);
|
|
208
|
-
case "notNull":
|
|
209
|
-
case "isNotNull":
|
|
210
|
-
return c.not(t, "is", null);
|
|
211
|
-
default:
|
|
212
|
-
throw new Error(`Unsupported operator: ${e}`);
|
|
213
|
-
}
|
|
134
|
+
function O(e, t, n, r, i, a = "") {
|
|
135
|
+
if (!n?.rules?.length) return e;
|
|
136
|
+
let o = e;
|
|
137
|
+
for (let e of n.rules) {
|
|
138
|
+
if (s(e)) {
|
|
139
|
+
o = O(o, t, e, r, i, a);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
let { field: n, operator: c, value: l, where: u } = e;
|
|
143
|
+
if (c === "exists" || c === "notExists") {
|
|
144
|
+
let e = r?.relationMap?.get(String(n));
|
|
145
|
+
if (!e) throw Error(`Relation '${n}' not found in ${r.name}`);
|
|
146
|
+
let t = S(e, i, r), s = String(n), l = u;
|
|
147
|
+
if (l?.rules?.length) {
|
|
148
|
+
let e = a ? `${a}.${s}` : s;
|
|
149
|
+
o = O(c === "exists" ? o.not(e, "is", null) : o, s, l, t, i, e);
|
|
150
|
+
} else {
|
|
151
|
+
let e = a ? `${a}.${s}` : s;
|
|
152
|
+
o = c === "exists" ? o.not(e, "is", null) : o.is(e, null);
|
|
153
|
+
}
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
let d = a ? `${a}.${n}` : `${t}.${n}`;
|
|
157
|
+
o = k(o, d, c, l);
|
|
158
|
+
}
|
|
159
|
+
return o;
|
|
214
160
|
}
|
|
215
|
-
function
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
161
|
+
function k(e, t, n, r) {
|
|
162
|
+
let i = b(r);
|
|
163
|
+
if (t.includes(".") && n === "between") {
|
|
164
|
+
let [n, i] = x(r, "between");
|
|
165
|
+
return e.gte(t, n).lte(t, i);
|
|
166
|
+
}
|
|
167
|
+
switch (n) {
|
|
168
|
+
case "=": return i === null ? e.is(t, null) : e.eq(t, i);
|
|
169
|
+
case "!=": return i === null ? e.not(t, "is", null) : e.neq(t, i);
|
|
170
|
+
case "<": return e.lt(t, i);
|
|
171
|
+
case ">": return e.gt(t, i);
|
|
172
|
+
case "<=": return e.lte(t, i);
|
|
173
|
+
case ">=": return e.gte(t, i);
|
|
174
|
+
case "in": return e.in(t, i);
|
|
175
|
+
case "notIn": return e.not(t, "in", `(${i.join(",")})`);
|
|
176
|
+
case "contains":
|
|
177
|
+
case "includes": return e.ilike(t, `%${i}%`);
|
|
178
|
+
case "notContains": return e.not(t, "ilike", `%${i}%`);
|
|
179
|
+
case "startsWith": return e.ilike(t, `${i}%`);
|
|
180
|
+
case "notStartsWith": return e.not(t, "ilike", `${i}%`);
|
|
181
|
+
case "endsWith": return e.ilike(t, `%${i}`);
|
|
182
|
+
case "notEndsWith": return e.not(t, "ilike", `%${i}`);
|
|
183
|
+
case "between": {
|
|
184
|
+
let [n, i] = x(r, "between");
|
|
185
|
+
return e.gte(t, n).lte(t, i);
|
|
186
|
+
}
|
|
187
|
+
case "notBetween": {
|
|
188
|
+
let [n, i] = x(r, "notBetween");
|
|
189
|
+
return e.or(`${t}.lt.${n},${t}.gt.${i}`);
|
|
190
|
+
}
|
|
191
|
+
case "null":
|
|
192
|
+
case "isNull": return e.is(t, null);
|
|
193
|
+
case "notNull":
|
|
194
|
+
case "isNotNull": return e.not(t, "is", null);
|
|
195
|
+
default: throw Error(`Unsupported operator: ${n}`);
|
|
196
|
+
}
|
|
222
197
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
}
|
|
233
|
-
return e;
|
|
198
|
+
//#endregion
|
|
199
|
+
//#region src/RxDBAdapterSupabase.utils.ts
|
|
200
|
+
function A(e) {
|
|
201
|
+
let t = /* @__PURE__ */ new Map();
|
|
202
|
+
for (let n of e) {
|
|
203
|
+
let e = n.constructor;
|
|
204
|
+
t.has(e) || t.set(e, /* @__PURE__ */ new Set()), t.get(e).add(n);
|
|
205
|
+
}
|
|
206
|
+
return t;
|
|
234
207
|
}
|
|
235
|
-
function
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
208
|
+
function j(e, t) {
|
|
209
|
+
let n = [];
|
|
210
|
+
for (let [r, i] of e) {
|
|
211
|
+
let e = o(r), a = Array.from(i);
|
|
212
|
+
n.push({
|
|
213
|
+
table: e.tableName,
|
|
214
|
+
schema: e.namespace,
|
|
215
|
+
data: t ? a.map((e) => ({
|
|
216
|
+
...e,
|
|
217
|
+
createdBy: t,
|
|
218
|
+
updatedBy: t
|
|
219
|
+
})) : a
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
return n;
|
|
246
223
|
}
|
|
247
|
-
function
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
}
|
|
259
|
-
return c;
|
|
260
|
-
case N.boolean:
|
|
261
|
-
return !!c;
|
|
262
|
-
case N.json:
|
|
263
|
-
case N.stringArray:
|
|
264
|
-
case N.numberArray:
|
|
265
|
-
default:
|
|
266
|
-
return c;
|
|
267
|
-
}
|
|
224
|
+
function M(e) {
|
|
225
|
+
let t = [];
|
|
226
|
+
for (let [n, r] of e) {
|
|
227
|
+
let e = o(n);
|
|
228
|
+
t.push({
|
|
229
|
+
table: e.tableName,
|
|
230
|
+
schema: e.namespace,
|
|
231
|
+
ids: Array.from(r).map((e) => e.id)
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
return t;
|
|
268
235
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
236
|
+
//#endregion
|
|
237
|
+
//#region src/schema.utils.ts
|
|
238
|
+
var N = "rxdb", P = "public", F = (e) => e && (e === N ? P : e);
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/transform.ts
|
|
241
|
+
function I(e, t) {
|
|
242
|
+
if (e == null) return e;
|
|
243
|
+
switch (t.type) {
|
|
244
|
+
case r.date: return e instanceof Date ? e : new Date(e);
|
|
245
|
+
case r.keyValue:
|
|
246
|
+
if (typeof e == "object" && t.properties) {
|
|
247
|
+
let n = e, r = {};
|
|
248
|
+
for (let e of t.properties) n[e.name] !== void 0 && (r[e.name] = I(n[e.name], e));
|
|
249
|
+
return r;
|
|
250
|
+
}
|
|
251
|
+
return e;
|
|
252
|
+
case r.boolean: return !!e;
|
|
253
|
+
case r.json:
|
|
254
|
+
case r.stringArray:
|
|
255
|
+
case r.numberArray:
|
|
256
|
+
default: return e;
|
|
257
|
+
}
|
|
276
258
|
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
*/
|
|
285
|
-
async find(t) {
|
|
286
|
-
const e = this.extract_relation_fields(t.where);
|
|
287
|
-
let n = "*";
|
|
288
|
-
if (e.size > 0) {
|
|
289
|
-
const i = Array.from(e);
|
|
290
|
-
n = `*, ${i.filter(
|
|
291
|
-
(d) => !i.some((u) => u !== d && u.startsWith(d + "."))
|
|
292
|
-
).map((d) => this.build_relation_select(d)).join(", ")}`;
|
|
293
|
-
}
|
|
294
|
-
let s = this.get_client().select(n);
|
|
295
|
-
if (s = S(s, t.where, this.metadata, this.adapter.rxdb.schemaManager), t.orderBy?.length)
|
|
296
|
-
for (const i of t.orderBy)
|
|
297
|
-
s = s.order(i.field, { ascending: i.sort === "asc" });
|
|
298
|
-
if (t.offset !== void 0) {
|
|
299
|
-
const i = t.offset, o = t.limit !== void 0 ? i + t.limit - 1 : Number.MAX_SAFE_INTEGER;
|
|
300
|
-
s = s.range(i, o);
|
|
301
|
-
} else t.limit !== void 0 && (s = s.limit(t.limit));
|
|
302
|
-
const { data: a, error: r } = await s;
|
|
303
|
-
if (r) throw new p(`Failed to find entities: ${r.message}`);
|
|
304
|
-
return (a || []).map(
|
|
305
|
-
(i) => st(this.EntityType, this.metadata, i)
|
|
306
|
-
);
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* 查询实体数量
|
|
310
|
-
*/
|
|
311
|
-
async count(t) {
|
|
312
|
-
if (t.groupBy) throw new Error("groupBy not supported yet");
|
|
313
|
-
const e = this.extract_relation_fields(t.where);
|
|
314
|
-
let n = "*";
|
|
315
|
-
e.size > 0 && (n = `*, ${Array.from(e).map((i) => this.build_relation_select(i)).join(", ")}`);
|
|
316
|
-
let s = this.get_client().select(n, { count: "exact", head: !0 });
|
|
317
|
-
s = S(s, t.where, this.metadata, this.adapter.rxdb.schemaManager);
|
|
318
|
-
const { count: a, error: r } = await s;
|
|
319
|
-
if (r) {
|
|
320
|
-
if (!r.message)
|
|
321
|
-
return console.warn("Supabase count returned empty error, returning 0. Options:", JSON.stringify(t)), 0;
|
|
322
|
-
throw new p(`Failed to count entities: ${r.message}`);
|
|
323
|
-
}
|
|
324
|
-
return a ?? 0;
|
|
325
|
-
}
|
|
326
|
-
/**
|
|
327
|
-
* 创建实体
|
|
328
|
-
*/
|
|
329
|
-
async create(t) {
|
|
330
|
-
const e = { ...t }, n = this.rxdb.context.userId;
|
|
331
|
-
n && (e.createdBy = n);
|
|
332
|
-
const { data: s, error: a } = await this.get_client().insert(e).select().single();
|
|
333
|
-
if (a) throw new p(`Failed to create entity: ${a.message}`);
|
|
334
|
-
return s;
|
|
335
|
-
}
|
|
336
|
-
/**
|
|
337
|
-
* 更新实体
|
|
338
|
-
*/
|
|
339
|
-
async update(t, e) {
|
|
340
|
-
const { data: n, error: s } = await this.get_client().update(e).eq("id", t.id).select().single();
|
|
341
|
-
if (s) throw new p(`Failed to update entity: ${s.message}`);
|
|
342
|
-
return n;
|
|
343
|
-
}
|
|
344
|
-
/**
|
|
345
|
-
* 删除实体
|
|
346
|
-
* @param entity
|
|
347
|
-
* @returns
|
|
348
|
-
*/
|
|
349
|
-
async remove(t) {
|
|
350
|
-
const { error: e } = await this.get_client().delete().eq("id", t.id);
|
|
351
|
-
if (e) throw new p(`Failed to remove entity: ${e.message}`);
|
|
352
|
-
return t;
|
|
353
|
-
}
|
|
354
|
-
// ============================================
|
|
355
|
-
// 私有方法
|
|
356
|
-
// ============================================
|
|
357
|
-
/** 提取查询中的关系字段路径(用于 EXISTS 查询的 INNER JOIN) */
|
|
358
|
-
extract_relation_fields(t) {
|
|
359
|
-
const e = /* @__PURE__ */ new Set(), n = (s, a = "") => {
|
|
360
|
-
if (s?.rules) {
|
|
361
|
-
for (const r of s.rules)
|
|
362
|
-
if (C(r))
|
|
363
|
-
n(r, a);
|
|
364
|
-
else if (typeof r.field == "string" && r.field.includes(".")) {
|
|
365
|
-
const i = r.field.split(".");
|
|
366
|
-
for (let o = 1; o < i.length; o++) {
|
|
367
|
-
const d = i.slice(0, o).join(".");
|
|
368
|
-
e.add(a ? `${a}.${d}` : d);
|
|
369
|
-
}
|
|
370
|
-
} else if (r.operator === "exists" || r.operator === "notExists") {
|
|
371
|
-
const i = String(r.field), o = a ? `${a}.${i}` : i;
|
|
372
|
-
this.metadata.relationMap?.has(i) && e.add(o), "where" in r && r.where && n(r.where, o);
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
};
|
|
376
|
-
return n(t), e;
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* 构建关系字段的 SELECT 语法
|
|
380
|
-
* 支持嵌套路径:'orders.items' → Order!inner(*, OrderItem!inner(*))
|
|
381
|
-
*/
|
|
382
|
-
build_relation_select(t) {
|
|
383
|
-
const e = t.split(".");
|
|
384
|
-
if (e.length === 1) return this.build_single_relation_select(e[0]);
|
|
385
|
-
const n = [];
|
|
386
|
-
let s = this.metadata;
|
|
387
|
-
for (const r of e) {
|
|
388
|
-
const i = s.relationMap?.get(r);
|
|
389
|
-
if (!i) return `${r}(id)`;
|
|
390
|
-
n.push({ tableName: i.mappedEntity, kind: i.kind, sourceEntity: s.name });
|
|
391
|
-
const o = this.adapter.rxdb.schemaManager.getEntityMetadata(i.mappedEntity, i.mappedNamespace ?? "");
|
|
392
|
-
o && (s = o);
|
|
393
|
-
}
|
|
394
|
-
let a = this.build_relation_segment(n[n.length - 1], !0);
|
|
395
|
-
for (let r = n.length - 2; r >= 0; r--)
|
|
396
|
-
a = this.build_relation_segment(n[r], !0).replace("(*)", `(*, ${a})`);
|
|
397
|
-
return a;
|
|
398
|
-
}
|
|
399
|
-
/**
|
|
400
|
-
* 构建单层关系 SELECT
|
|
401
|
-
* 根据关系类型生成正确的消歧语法
|
|
402
|
-
*/
|
|
403
|
-
build_single_relation_select(t) {
|
|
404
|
-
const e = this.metadata.relationMap?.get(t);
|
|
405
|
-
if (!e) return `${t}(id)`;
|
|
406
|
-
const n = e.mappedEntity, s = this.metadata.tableName;
|
|
407
|
-
if (e.kind === "m:1" || e.kind === "1:1") {
|
|
408
|
-
const a = `${s}_${t}Id_fkey`;
|
|
409
|
-
return `${n}!${a}(*)`;
|
|
410
|
-
}
|
|
411
|
-
if (e.kind === "m:n") {
|
|
412
|
-
const a = this.get_join_table_name(s, n);
|
|
413
|
-
return `${n}!${a}(*)`;
|
|
414
|
-
}
|
|
415
|
-
return `${n}(*)`;
|
|
416
|
-
}
|
|
417
|
-
/** 构建关系 SELECT 片段 */
|
|
418
|
-
build_relation_segment(t, e) {
|
|
419
|
-
const n = e ? "!inner" : "";
|
|
420
|
-
if (t.kind === "m:n") {
|
|
421
|
-
const s = this.get_join_table_name(t.sourceEntity, t.tableName);
|
|
422
|
-
return `${t.tableName}!${s}${n}(*)`;
|
|
423
|
-
}
|
|
424
|
-
return `${t.tableName}${n}(*)`;
|
|
425
|
-
}
|
|
426
|
-
/** 获取 m:n 中间表名(按字母顺序) */
|
|
427
|
-
get_join_table_name(t, e) {
|
|
428
|
-
return [t, e].sort().join("_");
|
|
429
|
-
}
|
|
430
|
-
/** 获取带 schema 的 client */
|
|
431
|
-
get_client() {
|
|
432
|
-
return this.adapter.client.schema(this.metadata.namespace).from(this.metadata.tableName);
|
|
433
|
-
}
|
|
259
|
+
function L(e, t, n) {
|
|
260
|
+
let r = new e();
|
|
261
|
+
for (let e of Object.keys(n)) {
|
|
262
|
+
let i = t.propertyMap.get(e);
|
|
263
|
+
r[e] = i ? I(n[e], i) : n[e];
|
|
264
|
+
}
|
|
265
|
+
return r;
|
|
434
266
|
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
267
|
+
//#endregion
|
|
268
|
+
//#region src/SupabaseRepository.ts
|
|
269
|
+
var R = class extends i {
|
|
270
|
+
adapter;
|
|
271
|
+
metadata;
|
|
272
|
+
constructor(e, t) {
|
|
273
|
+
super(e.rxdb, t), this.adapter = e, this.metadata = o(t);
|
|
274
|
+
}
|
|
275
|
+
async find(e) {
|
|
276
|
+
let t = this.extract_relation_fields(e.where), n = "*";
|
|
277
|
+
if (t.size > 0) {
|
|
278
|
+
let e = Array.from(t);
|
|
279
|
+
n = `*, ${e.filter((t) => !e.some((e) => e !== t && e.startsWith(t + "."))).map((e) => this.build_relation_select(e)).join(", ")}`;
|
|
280
|
+
}
|
|
281
|
+
let r = () => {
|
|
282
|
+
let t = this.get_client().select(n);
|
|
283
|
+
if (t = T(t, e.where, this.metadata, this.adapter.rxdb.schemaManager), e.orderBy?.length) {
|
|
284
|
+
for (let n of e.orderBy) t = t.order(n.field, { ascending: n.sort === "asc" });
|
|
285
|
+
e.orderBy.some((e) => e.field === "id") || (t = t.order("id", { ascending: !0 }));
|
|
286
|
+
}
|
|
287
|
+
return t;
|
|
288
|
+
}, i = 1e3, a = [], o = e.offset ?? 0, s = e.limit;
|
|
289
|
+
for (;;) {
|
|
290
|
+
let e = s === void 0 ? i : Math.min(i, s);
|
|
291
|
+
if (e <= 0) break;
|
|
292
|
+
let { data: t, error: n } = await r().range(o, o + e - 1);
|
|
293
|
+
if (n) throw new g(`Failed to find entities: ${n.message}`);
|
|
294
|
+
let c = t ?? [];
|
|
295
|
+
if (a.push(...c), c.length < e || (o += c.length, s !== void 0 && (s -= c.length, s <= 0))) break;
|
|
296
|
+
}
|
|
297
|
+
return a.map((e) => L(this.EntityType, this.metadata, e));
|
|
298
|
+
}
|
|
299
|
+
async count(e) {
|
|
300
|
+
if (e.groupBy) throw Error("groupBy not supported yet");
|
|
301
|
+
let t = this.extract_relation_fields(e.where), n = "*";
|
|
302
|
+
t.size > 0 && (n = `*, ${Array.from(t).map((e) => this.build_relation_select(e)).join(", ")}`);
|
|
303
|
+
let r = this.get_client().select(n, {
|
|
304
|
+
count: "exact",
|
|
305
|
+
head: !0
|
|
306
|
+
});
|
|
307
|
+
r = T(r, e.where, this.metadata, this.adapter.rxdb.schemaManager);
|
|
308
|
+
let { count: i, error: a } = await r;
|
|
309
|
+
if (a) throw new g(`Failed to count entities: ${a.message || "unknown error"}`);
|
|
310
|
+
return i ?? 0;
|
|
311
|
+
}
|
|
312
|
+
async create(e) {
|
|
313
|
+
let t = { ...e }, n = this.rxdb.context.userId;
|
|
314
|
+
n && (t.createdBy = n, t.updatedBy = n);
|
|
315
|
+
let { data: r, error: i } = await this.get_client().insert(t).select().single();
|
|
316
|
+
if (i) throw new g(`Failed to create entity: ${i.message}`);
|
|
317
|
+
return r;
|
|
318
|
+
}
|
|
319
|
+
async update(e, t) {
|
|
320
|
+
let n = { ...t }, r = this.rxdb.context.userId;
|
|
321
|
+
r && (n.updatedBy = r);
|
|
322
|
+
let { data: i, error: a } = await this.get_client().update(n).eq("id", e.id).select().single();
|
|
323
|
+
if (a) throw new g(`Failed to update entity: ${a.message}`);
|
|
324
|
+
return i;
|
|
325
|
+
}
|
|
326
|
+
async remove(e) {
|
|
327
|
+
let { error: t } = await this.get_client().delete().eq("id", e.id);
|
|
328
|
+
if (t) throw new g(`Failed to remove entity: ${t.message}`);
|
|
329
|
+
return e;
|
|
330
|
+
}
|
|
331
|
+
extract_relation_fields(e) {
|
|
332
|
+
let t = /* @__PURE__ */ new Set(), n = (e, r = "") => {
|
|
333
|
+
if (e?.rules) {
|
|
334
|
+
for (let i of e.rules) if (s(i)) n(i, r);
|
|
335
|
+
else if (typeof i.field == "string" && i.field.includes(".")) {
|
|
336
|
+
let e = i.field.split(".");
|
|
337
|
+
for (let n = 1; n < e.length; n++) {
|
|
338
|
+
let i = e.slice(0, n).join(".");
|
|
339
|
+
t.add(r ? `${r}.${i}` : i);
|
|
340
|
+
}
|
|
341
|
+
} else if (i.operator === "exists" || i.operator === "notExists") {
|
|
342
|
+
let e = String(i.field), a = r ? `${r}.${e}` : e;
|
|
343
|
+
this.metadata.relationMap?.has(e) && t.add(a), "where" in i && i.where && n(i.where, a);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
return n(e), t;
|
|
348
|
+
}
|
|
349
|
+
build_relation_select(e) {
|
|
350
|
+
let t = e.split(".");
|
|
351
|
+
if (t.length === 1) return this.build_single_relation_select(t[0]);
|
|
352
|
+
let n = [], r = this.metadata;
|
|
353
|
+
for (let e of t) {
|
|
354
|
+
let t = r.relationMap?.get(e);
|
|
355
|
+
if (!t) return `${e}(id)`;
|
|
356
|
+
let i = this.adapter.rxdb.schemaManager.getEntityMetadata(t.mappedEntity, t.mappedNamespace ?? "");
|
|
357
|
+
n.push({
|
|
358
|
+
relationName: e,
|
|
359
|
+
targetTableName: i?.tableName ?? t.mappedEntity,
|
|
360
|
+
targetEntityName: t.mappedEntity,
|
|
361
|
+
kind: t.kind,
|
|
362
|
+
sourceEntityName: r.name,
|
|
363
|
+
sourceTableName: r.tableName
|
|
364
|
+
}), i && (r = i);
|
|
365
|
+
}
|
|
366
|
+
let i = this.build_relation_segment(n[n.length - 1], !0);
|
|
367
|
+
for (let e = n.length - 2; e >= 0; e--) i = this.build_relation_segment(n[e], !0).replace("(*)", `(*, ${i})`);
|
|
368
|
+
return i;
|
|
369
|
+
}
|
|
370
|
+
build_single_relation_select(e) {
|
|
371
|
+
let t = this.metadata.relationMap?.get(e);
|
|
372
|
+
if (!t) return `${e}(id)`;
|
|
373
|
+
let n = this.adapter.rxdb.schemaManager.getEntityMetadata(t.mappedEntity, t.mappedNamespace ?? "")?.tableName ?? t.mappedEntity, r = this.metadata.tableName;
|
|
374
|
+
return t.kind === "m:1" || t.kind === "1:1" ? `${e}:${n}!${`${r}_${e}Id_fkey`.toLowerCase()}(*)` : t.kind === "m:n" ? `${e}:${n}!${this.get_join_table_name(this.metadata.name, t.mappedEntity)}(*)` : `${e}:${n}(*)`;
|
|
375
|
+
}
|
|
376
|
+
build_relation_segment(e, t) {
|
|
377
|
+
let n = t ? "!inner" : "";
|
|
378
|
+
if (e.kind === "m:n") {
|
|
379
|
+
let t = this.get_join_table_name(e.sourceEntityName, e.targetEntityName);
|
|
380
|
+
return `${e.relationName}:${e.targetTableName}!${t}${n}(*)`;
|
|
381
|
+
}
|
|
382
|
+
if (e.kind === "m:1" || e.kind === "1:1") {
|
|
383
|
+
let t = `${e.sourceTableName}_${e.relationName}Id_fkey`.toLowerCase();
|
|
384
|
+
return `${e.relationName}:${e.targetTableName}!${t}${n}(*)`;
|
|
385
|
+
}
|
|
386
|
+
return `${e.relationName}:${e.targetTableName}${n}(*)`;
|
|
387
|
+
}
|
|
388
|
+
get_join_table_name(e, t) {
|
|
389
|
+
return [e, t].sort().join("_");
|
|
390
|
+
}
|
|
391
|
+
get_client() {
|
|
392
|
+
let e = F(this.metadata.namespace);
|
|
393
|
+
return e ? this.adapter.client.schema(e).from(this.metadata.tableName) : this.adapter.client.from(this.metadata.tableName);
|
|
394
|
+
}
|
|
395
|
+
}, z = class extends R {
|
|
396
|
+
constructor(e, t) {
|
|
397
|
+
super(e, t);
|
|
398
|
+
}
|
|
399
|
+
async findDescendants(e) {
|
|
400
|
+
let { entityId: t, level: n = 100 } = e, { data: r, error: i } = await this.adapter.client.rpc(t ? "get_descendants" : "get_root_descendants", t ? {
|
|
401
|
+
root_id: t,
|
|
402
|
+
max_level: n
|
|
403
|
+
} : { max_level: n });
|
|
404
|
+
if (i) {
|
|
405
|
+
if (i.message.includes("function") || i.message.includes("Could not find")) return this.findDescendantsFallback(e);
|
|
406
|
+
throw new g(`Failed to find descendants: ${i.message}`);
|
|
407
|
+
}
|
|
408
|
+
return (r || []).map((e) => this.transformRowToEntity(e));
|
|
409
|
+
}
|
|
410
|
+
async countDescendants(e) {
|
|
411
|
+
let { entityId: t } = e, n = await this.findDescendants(e);
|
|
412
|
+
return t ? n.length - 1 : n.length;
|
|
413
|
+
}
|
|
414
|
+
async findAncestors(e) {
|
|
415
|
+
let { entityId: t, level: n = 100 } = e;
|
|
416
|
+
if (!t) return [];
|
|
417
|
+
let { data: r, error: i } = await this.adapter.client.rpc("get_ancestors", {
|
|
418
|
+
node_id: t,
|
|
419
|
+
max_level: n
|
|
420
|
+
});
|
|
421
|
+
if (i) {
|
|
422
|
+
if (i.message.includes("function") || i.message.includes("Could not find")) return this.findAncestorsFallback(e);
|
|
423
|
+
throw new g(`Failed to find ancestors: ${i.message}`);
|
|
424
|
+
}
|
|
425
|
+
return (r || []).map((e) => this.transformRowToEntity(e));
|
|
426
|
+
}
|
|
427
|
+
async countAncestors(e) {
|
|
428
|
+
let t = await this.findAncestors(e);
|
|
429
|
+
return Math.max(0, t.length - 1);
|
|
430
|
+
}
|
|
431
|
+
async findDescendantsFallback(e) {
|
|
432
|
+
let { entityId: t, level: n = 100 } = e, r = this.metadata.name, i = this.metadata.features?.tree?.hasChildren, a = F(this.metadata.namespace) ?? "public", o = this.adapter.client.schema(a).from(r), s = [], c = /* @__PURE__ */ new Set(), l = async (e) => {
|
|
433
|
+
let { data: n, error: r } = await (e === void 0 ? t ? o.select("*").eq("id", t).limit(1) : o.select("*").is("parentId", null) : o.select("*").in("parentId", e));
|
|
434
|
+
if (r) throw new g(`Failed to find descendants: ${r.message}`);
|
|
435
|
+
return n ?? [];
|
|
436
|
+
}, u = await l(), d = 0;
|
|
437
|
+
for (; u.length > 0 && d <= n;) {
|
|
438
|
+
let e = [], t = u.filter((t) => {
|
|
439
|
+
let n = String(t.id);
|
|
440
|
+
return c.has(n) ? !1 : (c.add(n), e.push(n), !0);
|
|
441
|
+
});
|
|
442
|
+
if (t.length === 0) break;
|
|
443
|
+
let r = d < n && e.length > 0 ? await l(e) : [], a = /* @__PURE__ */ new Map();
|
|
444
|
+
for (let e of r) {
|
|
445
|
+
let t = e.parentId;
|
|
446
|
+
typeof t == "string" && a.set(t, (a.get(t) ?? 0) + 1);
|
|
447
|
+
}
|
|
448
|
+
for (let e of t) {
|
|
449
|
+
let t = this.transformRowToEntity(e);
|
|
450
|
+
if (i) {
|
|
451
|
+
let n = String(e.id);
|
|
452
|
+
t.hasChildren = (a.get(n) ?? 0) > 0;
|
|
453
|
+
}
|
|
454
|
+
s.push(t);
|
|
455
|
+
}
|
|
456
|
+
u = r, d += 1;
|
|
457
|
+
}
|
|
458
|
+
return s;
|
|
459
|
+
}
|
|
460
|
+
async findAncestorsFallback(e) {
|
|
461
|
+
let { entityId: t, level: n = 100 } = e;
|
|
462
|
+
if (!t) return [];
|
|
463
|
+
let r = this.metadata.name, i = this.metadata.features?.tree?.hasChildren, a = F(this.metadata.namespace) ?? "public", { data: o, error: s } = await this.adapter.client.schema(a).from(r).select("*");
|
|
464
|
+
if (s) throw new g(`Failed to find ancestors: ${s.message}`);
|
|
465
|
+
if (!o || o.length === 0) return [];
|
|
466
|
+
let c = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map();
|
|
467
|
+
for (let e of o) {
|
|
468
|
+
c.set(e.id, e);
|
|
469
|
+
let t = e.parentId ?? null;
|
|
470
|
+
l.has(t) || l.set(t, []), l.get(t).push(e);
|
|
471
|
+
}
|
|
472
|
+
let u = [], d = t, f = 0;
|
|
473
|
+
for (; d && f <= n;) {
|
|
474
|
+
let e = c.get(d);
|
|
475
|
+
if (!e) break;
|
|
476
|
+
let t = this.transformRowToEntity(e);
|
|
477
|
+
if (i) {
|
|
478
|
+
let n = l.get(e.id);
|
|
479
|
+
t.hasChildren = !!n && n.length > 0;
|
|
480
|
+
}
|
|
481
|
+
u.push(t), d = e.parentId ?? null, f++;
|
|
482
|
+
}
|
|
483
|
+
return u;
|
|
484
|
+
}
|
|
485
|
+
transformRowToEntity(e) {
|
|
486
|
+
let t = { ...e };
|
|
487
|
+
return delete t.level, L(this.EntityType, this.metadata, t);
|
|
488
|
+
}
|
|
489
|
+
}, B = "supabase", V = [
|
|
490
|
+
/invalid response was received from the upstream server/i,
|
|
491
|
+
/fetch failed/i,
|
|
492
|
+
/failed to fetch/i,
|
|
493
|
+
/gateway timeout/i,
|
|
494
|
+
/upstream connect error/i,
|
|
495
|
+
/connection terminated/i,
|
|
496
|
+
/temporarily unavailable/i
|
|
497
|
+
], H = 3, U = 150, W = "rxdb_check_rls", G = 500, K = 5e3, q = new Set([
|
|
498
|
+
"CHANNEL_ERROR",
|
|
499
|
+
"TIMED_OUT",
|
|
500
|
+
"CLOSED"
|
|
501
|
+
]);
|
|
502
|
+
function J(e) {
|
|
503
|
+
return new Promise((t) => setTimeout(t, e));
|
|
583
504
|
}
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
constructor(t, e) {
|
|
587
|
-
super(t), this.options = e, this.#t = e.client ?? H(e.supabaseUrl, e.supabaseKey);
|
|
588
|
-
}
|
|
589
|
-
#t;
|
|
590
|
-
#e = null;
|
|
591
|
-
name = rt;
|
|
592
|
-
get client() {
|
|
593
|
-
return this.#t;
|
|
594
|
-
}
|
|
595
|
-
// ============================================
|
|
596
|
-
// 连接管理
|
|
597
|
-
// ============================================
|
|
598
|
-
async connect() {
|
|
599
|
-
return this.#e || (this.#e = this.#t.channel("rxdb-changes").on(
|
|
600
|
-
"postgres_changes",
|
|
601
|
-
{ event: "INSERT", schema: "public", table: "rxdb_change" },
|
|
602
|
-
(t) => G(this, t)
|
|
603
|
-
).subscribe((t, e) => {
|
|
604
|
-
e && console.error("Supabase Realtime subscription failed:", e);
|
|
605
|
-
})), this;
|
|
606
|
-
}
|
|
607
|
-
async disconnect() {
|
|
608
|
-
this.#e && (await this.#t.removeChannel(this.#e), this.#e = null);
|
|
609
|
-
}
|
|
610
|
-
async version() {
|
|
611
|
-
return "2.86.0";
|
|
612
|
-
}
|
|
613
|
-
// ============================================
|
|
614
|
-
// 批量操作
|
|
615
|
-
// ============================================
|
|
616
|
-
/**
|
|
617
|
-
* 批量保存实体(upsert 语义,非事务)
|
|
618
|
-
*/
|
|
619
|
-
async saveMany(t) {
|
|
620
|
-
return t.length === 0 ? [] : this.executeUpsert(j(t));
|
|
621
|
-
}
|
|
622
|
-
/**
|
|
623
|
-
* 批量删除实体(非事务)
|
|
624
|
-
*/
|
|
625
|
-
async removeMany(t) {
|
|
626
|
-
return t.length === 0 ? [] : this.executeDelete(j(t));
|
|
627
|
-
}
|
|
628
|
-
/**
|
|
629
|
-
* 批量修改实体(事务)
|
|
630
|
-
*
|
|
631
|
-
* 通过 PostgreSQL RPC 调用 `rxdb_mutations` 存储过程,
|
|
632
|
-
* 在单个数据库事务中执行所有操作,确保原子性。
|
|
633
|
-
*/
|
|
634
|
-
async mutations(t) {
|
|
635
|
-
const e = this.rxdb.context.userId, n = [...P(t.create, e), ...P(t.update, e)], s = nt(t.remove), { data: a, error: r } = await this.#t.rpc("rxdb_mutations", {
|
|
636
|
-
p_upserts: n,
|
|
637
|
-
p_deletes: s
|
|
638
|
-
});
|
|
639
|
-
if (r)
|
|
640
|
-
throw new p(`Transaction failed: ${r.message}`);
|
|
641
|
-
const i = a?.upserted ?? [];
|
|
642
|
-
for (const [, o] of t.remove)
|
|
643
|
-
i.push(...o);
|
|
644
|
-
return i;
|
|
645
|
-
}
|
|
646
|
-
// ============================================
|
|
647
|
-
// Repository
|
|
648
|
-
// ============================================
|
|
649
|
-
getRepository(t) {
|
|
650
|
-
if (this.repository_cache.has(t))
|
|
651
|
-
return this.repository_cache.get(t);
|
|
652
|
-
const n = I(t).features?.tree ? new at(this, t) : new z(this, t);
|
|
653
|
-
return this.repository_cache.set(t, n), n;
|
|
654
|
-
}
|
|
655
|
-
// ============================================
|
|
656
|
-
// 工具方法
|
|
657
|
-
// ============================================
|
|
658
|
-
async isTableExisted(t) {
|
|
659
|
-
const e = I(t), s = await this.getSchemaClient(e.namespace).from(e.tableName).select("*", { count: "exact", head: !0 });
|
|
660
|
-
if (s.status === 200) return !0;
|
|
661
|
-
if (s.status === 204) return !1;
|
|
662
|
-
throw new p(
|
|
663
|
-
`Failed to check table existence: ${s.error?.message || `status ${s.status}`}`
|
|
664
|
-
);
|
|
665
|
-
}
|
|
666
|
-
// ============================================
|
|
667
|
-
// Pull/Push 同步方法
|
|
668
|
-
// ============================================
|
|
669
|
-
/**
|
|
670
|
-
* 从远程拉取变更记录
|
|
671
|
-
*
|
|
672
|
-
* @param sinceId - 拉取此 ID 之后的变更(不包含该 ID)
|
|
673
|
-
* @param limit - 最大拉取数量
|
|
674
|
-
* @param repositoryFilter - 可选的实体过滤列表(用于 repository-level sync)
|
|
675
|
-
* @param filter - 可选的行级过滤条件(用于 SyncType.Filter)
|
|
676
|
-
* @returns RxDBChange 记录数组,按 id ASC 排序
|
|
677
|
-
*
|
|
678
|
-
* @remarks
|
|
679
|
-
* 使用 id 而非 createdAt 作为游标,避免同毫秒内多条记录导致的重复问题
|
|
680
|
-
*
|
|
681
|
-
* 当提供 filter 参数时,会通过 JOIN 实体表并应用过滤条件,
|
|
682
|
-
* 只返回满足条件的实体对应的变更记录。
|
|
683
|
-
*/
|
|
684
|
-
async pullChanges(t, e = 1e3, n, s, a) {
|
|
685
|
-
const r = [];
|
|
686
|
-
let i;
|
|
687
|
-
if (s && n?.length === 1) {
|
|
688
|
-
const m = n[0];
|
|
689
|
-
if (i = await this.#n(m, s), i.length === 0)
|
|
690
|
-
return [];
|
|
691
|
-
}
|
|
692
|
-
let o = this.#t.from("rxdb_change").select("*").gt("id", t).order("id", { ascending: !0 }).limit(e);
|
|
693
|
-
n && n.length > 0 && (o = o.in("entity", n)), a && (o = o.eq("branchId", a)), i && (o = o.in("entityId", i));
|
|
694
|
-
const { data: d, error: u } = await o;
|
|
695
|
-
if (u)
|
|
696
|
-
throw new p(`Failed to pull changes: ${u.message}`);
|
|
697
|
-
const b = (d ?? []).map((m) => ({
|
|
698
|
-
...m,
|
|
699
|
-
createdAt: new Date(m.createdAt),
|
|
700
|
-
updatedAt: m.updatedAt ? new Date(m.updatedAt) : null
|
|
701
|
-
}));
|
|
702
|
-
return r.push(...b), r;
|
|
703
|
-
}
|
|
704
|
-
/**
|
|
705
|
-
* 获取远程变更数量(轻量级,不下载数据)(T042, US2)
|
|
706
|
-
*
|
|
707
|
-
* 此方法只查询远程有多少新变更,不返回实际数据。
|
|
708
|
-
* 用于实现 checkRepositoryUpdates() 功能,节省带宽。
|
|
709
|
-
*
|
|
710
|
-
* @param sinceId - 起始 changeId(不包含该 ID)
|
|
711
|
-
* @param repositoryFilter - 可选的实体过滤列表
|
|
712
|
-
* @returns 变更数量和最新 changeId
|
|
713
|
-
*
|
|
714
|
-
* @example
|
|
715
|
-
* ```typescript
|
|
716
|
-
* // 查询 Todo 实体的新变更数量
|
|
717
|
-
* const { count, latestChangeId } = await adapter.getChangeCount(100, ['Todo']);
|
|
718
|
-
* console.log(`有 ${count} 条新变更,最新 ID: ${latestChangeId}`);
|
|
719
|
-
* ```
|
|
720
|
-
*/
|
|
721
|
-
async getChangeCount(t, e, n) {
|
|
722
|
-
let s = this.#t.from("rxdb_change").select("*", { count: "exact", head: !0 }).gt("id", t);
|
|
723
|
-
e && e.length > 0 && (s = s.in("entity", e)), n && (s = s.eq("branchId", n));
|
|
724
|
-
const { count: a, error: r } = await s;
|
|
725
|
-
if (r)
|
|
726
|
-
throw new p(`Failed to get change count: ${r.message}`);
|
|
727
|
-
let i = t;
|
|
728
|
-
if (a && a > 0) {
|
|
729
|
-
let o = this.#t.from("rxdb_change").select("id").gt("id", t).order("id", { ascending: !1 }).limit(1);
|
|
730
|
-
e && e.length > 0 && (o = o.in("entity", e)), n && (o = o.eq("branchId", n));
|
|
731
|
-
const { data: d, error: u } = await o;
|
|
732
|
-
if (u)
|
|
733
|
-
throw new p(`Failed to get latest changeId: ${u.message}`);
|
|
734
|
-
d && d.length > 0 && (i = d[0].id);
|
|
735
|
-
}
|
|
736
|
-
return {
|
|
737
|
-
count: a ?? 0,
|
|
738
|
-
latestChangeId: i
|
|
739
|
-
};
|
|
740
|
-
}
|
|
741
|
-
/**
|
|
742
|
-
* 推送变更记录到远程(双写:RxDBChange 表 + 实体表)
|
|
743
|
-
*
|
|
744
|
-
* @deprecated 请使用 mergeChanges 代替,该方法不支持事务且效率较低
|
|
745
|
-
* @param changes - 待推送的变更(已压缩)
|
|
746
|
-
* @throws 当任何变更推送失败时抛出错误
|
|
747
|
-
*/
|
|
748
|
-
async pushChanges(t) {
|
|
749
|
-
if (t.length === 0) return;
|
|
750
|
-
const e = {
|
|
751
|
-
inserts: /* @__PURE__ */ new Map(),
|
|
752
|
-
updates: /* @__PURE__ */ new Map(),
|
|
753
|
-
deletes: /* @__PURE__ */ new Map()
|
|
754
|
-
};
|
|
755
|
-
for (const n of t) {
|
|
756
|
-
const { entityKey: s, finalType: a, finalPatch: r, inversePatch: i } = n;
|
|
757
|
-
if (a)
|
|
758
|
-
switch (a) {
|
|
759
|
-
case "INSERT":
|
|
760
|
-
e.inserts.set(s, { patch: r, inversePatch: i });
|
|
761
|
-
break;
|
|
762
|
-
case "UPDATE":
|
|
763
|
-
e.updates.set(s, { patch: r, inversePatch: i });
|
|
764
|
-
break;
|
|
765
|
-
case "DELETE":
|
|
766
|
-
e.deletes.set(s, { patch: null, inversePatch: i });
|
|
767
|
-
break;
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
await this.mergeChanges(e);
|
|
771
|
-
}
|
|
772
|
-
/**
|
|
773
|
-
* 应用压缩后的变更到远程(事务)
|
|
774
|
-
*
|
|
775
|
-
* 通过 rxdb_mutations RPC 在单个事务中:
|
|
776
|
-
* 1. 写入 RxDBChange 表(用于其他客户端 pull)
|
|
777
|
-
* 2. 应用 actions 到实体表(INSERT/UPDATE/DELETE)
|
|
778
|
-
* 3. 自动跳过同步触发器(p_skip_sync=true)
|
|
779
|
-
*
|
|
780
|
-
* @param actions - 压缩后的变更操作集合
|
|
781
|
-
* @param branchId - 分支 ID
|
|
782
|
-
* @param changes - 完整的原始变更记录(可选,用于保留完整历史)
|
|
783
|
-
*/
|
|
784
|
-
async mergeChanges(t, e, n) {
|
|
785
|
-
const s = (/* @__PURE__ */ new Date()).toISOString(), a = this.rxdb.context.userId, r = e ?? "main", i = [];
|
|
786
|
-
if (n?.length)
|
|
787
|
-
for (const l of n)
|
|
788
|
-
i.push({
|
|
789
|
-
namespace: l.namespace || "public",
|
|
790
|
-
entity: l.entity,
|
|
791
|
-
entityId: l.entityId,
|
|
792
|
-
type: l.type,
|
|
793
|
-
branchId: l.branchId ?? r,
|
|
794
|
-
patch: l.patch ?? null,
|
|
795
|
-
inversePatch: l.inversePatch ?? null,
|
|
796
|
-
clientId: l.clientId ?? this.rxdb.context.clientId,
|
|
797
|
-
localId: l.id,
|
|
798
|
-
createdAt: s,
|
|
799
|
-
updatedAt: s
|
|
800
|
-
});
|
|
801
|
-
else {
|
|
802
|
-
for (const [l, { inversePatch: f }] of t.deletes) {
|
|
803
|
-
const [y, _, E] = l.split(":");
|
|
804
|
-
i.push({
|
|
805
|
-
namespace: y || "public",
|
|
806
|
-
entity: _,
|
|
807
|
-
entityId: E,
|
|
808
|
-
type: "DELETE",
|
|
809
|
-
branchId: r,
|
|
810
|
-
patch: null,
|
|
811
|
-
inversePatch: f,
|
|
812
|
-
clientId: this.rxdb.context.clientId,
|
|
813
|
-
createdAt: s,
|
|
814
|
-
updatedAt: s
|
|
815
|
-
});
|
|
816
|
-
}
|
|
817
|
-
for (const [l, { patch: f, inversePatch: y }] of t.updates) {
|
|
818
|
-
const [_, E, x] = l.split(":");
|
|
819
|
-
i.push({
|
|
820
|
-
namespace: _ || "public",
|
|
821
|
-
entity: E,
|
|
822
|
-
entityId: x,
|
|
823
|
-
type: "UPDATE",
|
|
824
|
-
branchId: r,
|
|
825
|
-
patch: f,
|
|
826
|
-
inversePatch: y,
|
|
827
|
-
clientId: this.rxdb.context.clientId,
|
|
828
|
-
createdAt: s,
|
|
829
|
-
updatedAt: s
|
|
830
|
-
});
|
|
831
|
-
}
|
|
832
|
-
for (const [l, { patch: f, inversePatch: y }] of t.inserts) {
|
|
833
|
-
const [_, E, x] = l.split(":");
|
|
834
|
-
i.push({
|
|
835
|
-
namespace: _ || "public",
|
|
836
|
-
entity: E,
|
|
837
|
-
entityId: x,
|
|
838
|
-
type: "INSERT",
|
|
839
|
-
branchId: r,
|
|
840
|
-
patch: f,
|
|
841
|
-
inversePatch: y,
|
|
842
|
-
clientId: this.rxdb.context.clientId,
|
|
843
|
-
createdAt: s,
|
|
844
|
-
updatedAt: s
|
|
845
|
-
});
|
|
846
|
-
}
|
|
847
|
-
}
|
|
848
|
-
const o = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map(), u = (l, f) => {
|
|
849
|
-
const y = l || "public", E = this.rxdb.schemaManager.getEntityMetadata(f, y)?.tableName ?? f;
|
|
850
|
-
return `${y}.${E}`;
|
|
851
|
-
};
|
|
852
|
-
for (const [l] of t.deletes) {
|
|
853
|
-
const [f, y, _] = l.split(":"), E = u(f, y), x = d.get(E) ?? [];
|
|
854
|
-
x.push(_), d.set(E, x);
|
|
855
|
-
}
|
|
856
|
-
for (const [l, { patch: f }] of t.updates) {
|
|
857
|
-
const [y, _, E] = l.split(":"), x = u(y, _), R = o.get(x) ?? [], A = { id: E, ...f };
|
|
858
|
-
a && (A.updatedBy = a), R.push(A), o.set(x, R);
|
|
859
|
-
}
|
|
860
|
-
for (const [l, { patch: f }] of t.inserts) {
|
|
861
|
-
const [y, _, E] = l.split(":"), x = u(y, _), R = o.get(x) ?? [], A = { id: E, ...f };
|
|
862
|
-
a && (A.createdBy = a, A.updatedBy = a), R.push(A), o.set(x, R);
|
|
863
|
-
}
|
|
864
|
-
const b = r === "main", m = b ? Array.from(o.entries()).map(([l, f]) => {
|
|
865
|
-
const [y, _] = l.includes(".") ? l.split(".") : ["public", l];
|
|
866
|
-
return { table: _, schema: y, data: f };
|
|
867
|
-
}) : [], g = b ? Array.from(d.entries()).map(([l, f]) => {
|
|
868
|
-
const [y, _] = l.includes(".") ? l.split(".") : ["public", l];
|
|
869
|
-
return { table: _, schema: y, ids: f };
|
|
870
|
-
}) : [], { data: h, error: w } = await this.#t.rpc("rxdb_mutations", {
|
|
871
|
-
p_upserts: m,
|
|
872
|
-
p_deletes: g,
|
|
873
|
-
p_changes: i,
|
|
874
|
-
p_skip_sync: !0
|
|
875
|
-
});
|
|
876
|
-
if (w)
|
|
877
|
-
throw new p(`Failed to merge changes: ${w.message}`);
|
|
878
|
-
return {
|
|
879
|
-
maxChangeId: h?.max_change_id ?? void 0,
|
|
880
|
-
changeIdMapping: h?.change_id_mapping ?? void 0
|
|
881
|
-
};
|
|
882
|
-
}
|
|
883
|
-
/**
|
|
884
|
-
* 批量拉取多个实体的变更记录(单次 HTTP 请求)
|
|
885
|
-
*
|
|
886
|
-
* 通过 PostgREST 的 OR 过滤器实现:
|
|
887
|
-
* `or=(and(entity.eq.Todo,id.gt.6),and(entity.eq.RxDBBranch,id.gt.0),...)`
|
|
888
|
-
*
|
|
889
|
-
* 每个实体使用独立的 sinceId 水位线,避免下载多余数据。
|
|
890
|
-
*/
|
|
891
|
-
async pullChangesBatch(t, e = 1e3, n) {
|
|
892
|
-
if (t.length === 0) return [];
|
|
893
|
-
if (t.length === 1 && (!n || n.length <= 1))
|
|
894
|
-
return this.pullChanges(t[0].sinceId, e, [t[0].entity], void 0, n?.[0]);
|
|
895
|
-
const s = t.map((o) => `and(entity.eq.${o.entity},id.gt.${o.sinceId})`).join(",");
|
|
896
|
-
let a = this.#t.from("rxdb_change").select("*").or(s).order("id", { ascending: !0 }).limit(e);
|
|
897
|
-
n && n.length > 0 && (n.length === 1 ? a = a.eq("branchId", n[0]) : a = a.in("branchId", n));
|
|
898
|
-
const { data: r, error: i } = await a;
|
|
899
|
-
if (i)
|
|
900
|
-
throw new p(`Failed to batch pull changes: ${i.message}`);
|
|
901
|
-
return (r ?? []).map((o) => ({
|
|
902
|
-
...o,
|
|
903
|
-
createdAt: new Date(o.createdAt),
|
|
904
|
-
updatedAt: o.updatedAt ? new Date(o.updatedAt) : null
|
|
905
|
-
}));
|
|
906
|
-
}
|
|
907
|
-
// ============================================
|
|
908
|
-
// 分支同步
|
|
909
|
-
// ============================================
|
|
910
|
-
async pushBranches(t) {
|
|
911
|
-
if (t.length === 0)
|
|
912
|
-
return { synced: 0, skipped: [] };
|
|
913
|
-
const { data: e, error: n } = await this.#t.rpc("rxdb_enable_sync_for_branch", {
|
|
914
|
-
p_branches: t
|
|
915
|
-
});
|
|
916
|
-
if (n)
|
|
917
|
-
throw new p(`Failed to sync branches: ${n.message}`);
|
|
918
|
-
return {
|
|
919
|
-
synced: e?.synced ?? 0,
|
|
920
|
-
skipped: e?.skipped ?? []
|
|
921
|
-
};
|
|
922
|
-
}
|
|
923
|
-
async branchExists(t) {
|
|
924
|
-
const { count: e, error: n } = await this.#t.from("rxdb_branch").select("id", { count: "exact", head: !0 }).eq("id", t);
|
|
925
|
-
if (n)
|
|
926
|
-
throw new p(`Failed to check branch existence: ${n.message}`);
|
|
927
|
-
return (e ?? 0) > 0;
|
|
928
|
-
}
|
|
929
|
-
async pullBranches() {
|
|
930
|
-
const { data: t, error: e } = await this.#t.from("rxdb_branch").select("id, fromChangeId, parentId, createdAt, updatedAt");
|
|
931
|
-
if (e)
|
|
932
|
-
throw new p(`Failed to pull branches: ${e.message}`);
|
|
933
|
-
return (t ?? []).map((n) => ({
|
|
934
|
-
id: n.id,
|
|
935
|
-
fromChangeId: n.fromChangeId,
|
|
936
|
-
parentId: n.parentId,
|
|
937
|
-
createdAt: n.createdAt,
|
|
938
|
-
updatedAt: n.updatedAt
|
|
939
|
-
}));
|
|
940
|
-
}
|
|
941
|
-
// ============================================
|
|
942
|
-
// QueryCache 方法
|
|
943
|
-
// ============================================
|
|
944
|
-
/**
|
|
945
|
-
* 获取实体元数据,用于新鲜度比较(QueryCache 专用)
|
|
946
|
-
*
|
|
947
|
-
* 只返回 `{ id, updatedAt }` 元数据,网络传输量比完整数据减少 90%+。
|
|
948
|
-
*
|
|
949
|
-
* @param entityName - 实体名称
|
|
950
|
-
* @param query - 查询条件
|
|
951
|
-
* @returns Observable<QueryCacheEntityMetadata[]>
|
|
952
|
-
*
|
|
953
|
-
* @example
|
|
954
|
-
* ```typescript
|
|
955
|
-
* adapter.fetchMetadata('Product', { combinator: 'and', rules: [{ field: 'status', operator: 'eq', value: 'active' }] })
|
|
956
|
-
* .subscribe(metadata => console.log(metadata));
|
|
957
|
-
* ```
|
|
958
|
-
*/
|
|
959
|
-
fetchMetadata(t, e) {
|
|
960
|
-
return D(() => {
|
|
961
|
-
const s = this.rxdb.schemaManager.getEntityMetadata(t, "public")?.tableName ?? t;
|
|
962
|
-
let a = this.#t.from(s).select("id, updatedAt");
|
|
963
|
-
return a = S(a, e), F(a).pipe(
|
|
964
|
-
B(({ data: r, error: i }) => {
|
|
965
|
-
if (i)
|
|
966
|
-
throw new p(`Failed to fetch metadata: ${i.message}`);
|
|
967
|
-
return (r ?? []).map((o) => ({
|
|
968
|
-
id: o.id,
|
|
969
|
-
updatedAt: o.updatedAt
|
|
970
|
-
}));
|
|
971
|
-
})
|
|
972
|
-
);
|
|
973
|
-
});
|
|
974
|
-
}
|
|
975
|
-
/**
|
|
976
|
-
* 按 ID 列表批量获取完整数据(QueryCache 专用)
|
|
977
|
-
*
|
|
978
|
-
* @param entityName - 实体名称
|
|
979
|
-
* @param ids - 需要获取的实体 ID 列表
|
|
980
|
-
* @returns Observable<T[]>
|
|
981
|
-
*/
|
|
982
|
-
findByIds(t, e) {
|
|
983
|
-
return D(() => {
|
|
984
|
-
if (e.length === 0)
|
|
985
|
-
return Y([]);
|
|
986
|
-
const s = this.rxdb.schemaManager.getEntityMetadata(t, "public")?.tableName ?? t;
|
|
987
|
-
return F(this.#t.from(s).select("*").in("id", e)).pipe(
|
|
988
|
-
B(({ data: a, error: r }) => {
|
|
989
|
-
if (r)
|
|
990
|
-
throw new p(`Failed to find by ids: ${r.message}`);
|
|
991
|
-
return a ?? [];
|
|
992
|
-
})
|
|
993
|
-
);
|
|
994
|
-
});
|
|
995
|
-
}
|
|
996
|
-
// ============================================
|
|
997
|
-
// 私有方法
|
|
998
|
-
// ============================================
|
|
999
|
-
/** 获取带 schema 的客户端 */
|
|
1000
|
-
getSchemaClient(t) {
|
|
1001
|
-
return t ? this.#t.schema(t) : this.#t;
|
|
1002
|
-
}
|
|
1003
|
-
/** 执行 upsert(非事务) */
|
|
1004
|
-
async executeUpsert(t) {
|
|
1005
|
-
const e = [], n = this.rxdb.context.userId;
|
|
1006
|
-
for (const [s, a] of t) {
|
|
1007
|
-
const r = I(s), i = this.getSchemaClient(r.namespace), o = Array.from(a), d = n ? o.map((m) => ({ ...m, createdBy: n, updatedBy: n })) : o, { data: u, error: b } = await i.from(r.tableName).upsert(d).select();
|
|
1008
|
-
if (b)
|
|
1009
|
-
throw new p(`Failed to upsert: ${b.message}`);
|
|
1010
|
-
e.push(...u);
|
|
1011
|
-
}
|
|
1012
|
-
return e;
|
|
1013
|
-
}
|
|
1014
|
-
/** 执行 delete(非事务) */
|
|
1015
|
-
async executeDelete(t) {
|
|
1016
|
-
const e = [];
|
|
1017
|
-
for (const [n, s] of t) {
|
|
1018
|
-
const a = I(n), r = this.getSchemaClient(a.namespace), i = Array.from(s), o = i.map((u) => u.id), { error: d } = await r.from(a.tableName).delete().in("id", o);
|
|
1019
|
-
if (d)
|
|
1020
|
-
throw new p(`Failed to delete: ${d.message}`);
|
|
1021
|
-
e.push(...i);
|
|
1022
|
-
}
|
|
1023
|
-
return e;
|
|
1024
|
-
}
|
|
1025
|
-
/**
|
|
1026
|
-
* 获取满足 filter 条件的实体 ID 列表
|
|
1027
|
-
*
|
|
1028
|
-
* @param entityName - 实体表名
|
|
1029
|
-
* @param filter - 过滤条件
|
|
1030
|
-
* @returns 满足条件的 entityId 列表
|
|
1031
|
-
*/
|
|
1032
|
-
async #n(t, e) {
|
|
1033
|
-
const s = this.rxdb.schemaManager.getEntityMetadata(t, "public")?.tableName ?? t;
|
|
1034
|
-
let a = this.#t.from(s).select("id");
|
|
1035
|
-
a = S(a, e);
|
|
1036
|
-
const { data: r, error: i } = await a;
|
|
1037
|
-
if (i)
|
|
1038
|
-
throw new p(`Failed to query filtered entities: ${i.message}`);
|
|
1039
|
-
return (r ?? []).map((o) => o.id);
|
|
1040
|
-
}
|
|
505
|
+
function Y(e) {
|
|
506
|
+
return V.some((t) => t.test(e));
|
|
1041
507
|
}
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
508
|
+
var X = class extends a {
|
|
509
|
+
options;
|
|
510
|
+
#e;
|
|
511
|
+
#t = null;
|
|
512
|
+
#n = null;
|
|
513
|
+
#r = 0;
|
|
514
|
+
#i = !0;
|
|
515
|
+
#a = !1;
|
|
516
|
+
name = B;
|
|
517
|
+
get client() {
|
|
518
|
+
return this.#e;
|
|
519
|
+
}
|
|
520
|
+
constructor(e, t) {
|
|
521
|
+
super(e), this.options = t, this.#e = t.client ?? c(t.supabaseUrl, t.supabaseKey);
|
|
522
|
+
}
|
|
523
|
+
async connect() {
|
|
524
|
+
return await this.#o(), this.#i = !0, this.#h(), this.#t ||= this.#f(), this;
|
|
525
|
+
}
|
|
526
|
+
async disconnect() {
|
|
527
|
+
if (this.#i = !1, this.#h(), this.#r = 0, this.#t) {
|
|
528
|
+
let e = this.#t;
|
|
529
|
+
this.#t = null, await this.#e.removeChannel(e);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
async version() {
|
|
533
|
+
return "2.86.0";
|
|
534
|
+
}
|
|
535
|
+
async saveMany(e) {
|
|
536
|
+
return e.length === 0 ? [] : this.executeUpsert(A(e));
|
|
537
|
+
}
|
|
538
|
+
async removeMany(e) {
|
|
539
|
+
return e.length === 0 ? [] : this.executeDelete(A(e));
|
|
540
|
+
}
|
|
541
|
+
async mutations(e) {
|
|
542
|
+
let t = this.rxdb.context.userId, n = [...j(e.create, t), ...j(e.update, t)], r = M(e.remove);
|
|
543
|
+
if (n.length === 0 && r.length === 0) return [];
|
|
544
|
+
let { data: i, error: a } = await this.#e.rpc("rxdb_mutations", {
|
|
545
|
+
p_upserts: n,
|
|
546
|
+
p_deletes: r
|
|
547
|
+
});
|
|
548
|
+
if (a) throw new g(`Transaction failed: ${a.message}`);
|
|
549
|
+
let o = i?.upserted ?? [];
|
|
550
|
+
for (let [, t] of e.remove) o.push(...t);
|
|
551
|
+
return o;
|
|
552
|
+
}
|
|
553
|
+
getRepository(e) {
|
|
554
|
+
let t = this.repository_cache.get(e);
|
|
555
|
+
if (t) return t;
|
|
556
|
+
let n = o(e).features?.tree ? new z(this, e) : new R(this, e);
|
|
557
|
+
return this.repository_cache.set(e, n), n;
|
|
558
|
+
}
|
|
559
|
+
async isTableExisted(e) {
|
|
560
|
+
let t = o(e), n = await this.getSchemaClient(t.namespace).from(t.tableName).select("*", {
|
|
561
|
+
count: "exact",
|
|
562
|
+
head: !0
|
|
563
|
+
});
|
|
564
|
+
if (n.status === 200 || n.status === 206) return !0;
|
|
565
|
+
if (n.status === 204 || n.status === 404 || n.status === 406) return !1;
|
|
566
|
+
throw new g(`Failed to check table existence: ${n.error?.message || `status ${n.status}`}`);
|
|
567
|
+
}
|
|
568
|
+
async pullChanges(e, t = 1e3, n, r, i) {
|
|
569
|
+
let a = [], o;
|
|
570
|
+
if (r && n?.length === 1) {
|
|
571
|
+
let e = n[0];
|
|
572
|
+
if (o = await this.#g(e, r), o.length === 0) return [];
|
|
573
|
+
}
|
|
574
|
+
let s = this.#e.from("rxdb_change").select("*").gt("id", e).order("id", { ascending: !0 }).limit(t);
|
|
575
|
+
n && n.length > 0 && (s = s.in("entity", n)), i && (s = s.eq("branchId", i)), o && (s = s.in("entityId", o));
|
|
576
|
+
let { data: c, error: l } = await s;
|
|
577
|
+
if (l) throw new g(`Failed to pull changes: ${l.message}`);
|
|
578
|
+
let u = (c ?? []).map((e) => ({
|
|
579
|
+
...e,
|
|
580
|
+
createdAt: new Date(e.createdAt),
|
|
581
|
+
updatedAt: e.updatedAt ? new Date(e.updatedAt) : null
|
|
582
|
+
}));
|
|
583
|
+
return a.push(...u), a;
|
|
584
|
+
}
|
|
585
|
+
async getChangeCount(e, t, n) {
|
|
586
|
+
let r = this.#e.from("rxdb_change").select("id", { count: "exact" }).gt("id", e);
|
|
587
|
+
t && t.length > 0 && (r = r.in("entity", t)), n && (r = r.eq("branchId", n));
|
|
588
|
+
let { data: i, count: a, error: o } = await r.order("id", { ascending: !1 }).limit(1);
|
|
589
|
+
if (o) throw new g(`Failed to get change count: ${o.message}`);
|
|
590
|
+
return {
|
|
591
|
+
count: a ?? 0,
|
|
592
|
+
latestChangeId: a && a > 0 && i && i.length > 0 ? i[0].id : e
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
async pushChanges(e) {
|
|
596
|
+
if (e.length === 0) return;
|
|
597
|
+
let t = {
|
|
598
|
+
inserts: /* @__PURE__ */ new Map(),
|
|
599
|
+
updates: /* @__PURE__ */ new Map(),
|
|
600
|
+
deletes: /* @__PURE__ */ new Map()
|
|
601
|
+
};
|
|
602
|
+
for (let n of e) {
|
|
603
|
+
let { entityKey: e, finalType: r, finalPatch: i, inversePatch: a } = n;
|
|
604
|
+
if (r) switch (r) {
|
|
605
|
+
case "INSERT":
|
|
606
|
+
t.inserts.set(e, {
|
|
607
|
+
patch: i,
|
|
608
|
+
inversePatch: a
|
|
609
|
+
});
|
|
610
|
+
break;
|
|
611
|
+
case "UPDATE":
|
|
612
|
+
t.updates.set(e, {
|
|
613
|
+
patch: i,
|
|
614
|
+
inversePatch: a
|
|
615
|
+
});
|
|
616
|
+
break;
|
|
617
|
+
case "DELETE":
|
|
618
|
+
t.deletes.set(e, {
|
|
619
|
+
patch: null,
|
|
620
|
+
inversePatch: a
|
|
621
|
+
});
|
|
622
|
+
break;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
await this.mergeChanges(t);
|
|
626
|
+
}
|
|
627
|
+
async mergeChanges(e, t, n) {
|
|
628
|
+
let r = (/* @__PURE__ */ new Date()).toISOString(), i = this.rxdb.context.userId, a = t ?? "main", o = [];
|
|
629
|
+
if (n?.length) for (let e of n) o.push({
|
|
630
|
+
namespace: e.namespace || "public",
|
|
631
|
+
entity: e.entity,
|
|
632
|
+
entityId: e.entityId,
|
|
633
|
+
type: e.type,
|
|
634
|
+
branchId: e.branchId ?? a,
|
|
635
|
+
patch: e.patch ?? null,
|
|
636
|
+
inversePatch: e.inversePatch ?? null,
|
|
637
|
+
clientId: e.clientId ?? this.rxdb.context.clientId,
|
|
638
|
+
localId: e.id,
|
|
639
|
+
createdAt: r,
|
|
640
|
+
updatedAt: r
|
|
641
|
+
});
|
|
642
|
+
else {
|
|
643
|
+
for (let [t, { inversePatch: n }] of e.deletes) {
|
|
644
|
+
let [e, i, s] = t.split(":");
|
|
645
|
+
o.push({
|
|
646
|
+
namespace: e || "public",
|
|
647
|
+
entity: i,
|
|
648
|
+
entityId: s,
|
|
649
|
+
type: "DELETE",
|
|
650
|
+
branchId: a,
|
|
651
|
+
patch: null,
|
|
652
|
+
inversePatch: n,
|
|
653
|
+
clientId: this.rxdb.context.clientId,
|
|
654
|
+
createdAt: r,
|
|
655
|
+
updatedAt: r
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
for (let [t, { patch: n, inversePatch: i }] of e.updates) {
|
|
659
|
+
let [e, s, c] = t.split(":");
|
|
660
|
+
o.push({
|
|
661
|
+
namespace: e || "public",
|
|
662
|
+
entity: s,
|
|
663
|
+
entityId: c,
|
|
664
|
+
type: "UPDATE",
|
|
665
|
+
branchId: a,
|
|
666
|
+
patch: n,
|
|
667
|
+
inversePatch: i,
|
|
668
|
+
clientId: this.rxdb.context.clientId,
|
|
669
|
+
createdAt: r,
|
|
670
|
+
updatedAt: r
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
for (let [t, { patch: n, inversePatch: i }] of e.inserts) {
|
|
674
|
+
let [e, s, c] = t.split(":");
|
|
675
|
+
o.push({
|
|
676
|
+
namespace: e || "public",
|
|
677
|
+
entity: s,
|
|
678
|
+
entityId: c,
|
|
679
|
+
type: "INSERT",
|
|
680
|
+
branchId: a,
|
|
681
|
+
patch: n,
|
|
682
|
+
inversePatch: i,
|
|
683
|
+
clientId: this.rxdb.context.clientId,
|
|
684
|
+
createdAt: r,
|
|
685
|
+
updatedAt: r
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
let s = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map(), l = (e, t) => {
|
|
690
|
+
let n = e || "public";
|
|
691
|
+
return `${n}.${this.rxdb.schemaManager.getEntityMetadata(t, n)?.tableName ?? t}`;
|
|
692
|
+
};
|
|
693
|
+
for (let [t] of e.deletes) {
|
|
694
|
+
let [e, n, r] = t.split(":"), i = l(e, n), a = c.get(i) ?? [];
|
|
695
|
+
a.push(r), c.set(i, a);
|
|
696
|
+
}
|
|
697
|
+
for (let [t, { patch: n }] of e.updates) {
|
|
698
|
+
let [e, r, a] = t.split(":"), o = l(e, r), c = s.get(o) ?? [], u = {
|
|
699
|
+
id: a,
|
|
700
|
+
...n
|
|
701
|
+
};
|
|
702
|
+
i && (u.updatedBy = i), c.push(u), s.set(o, c);
|
|
703
|
+
}
|
|
704
|
+
for (let [t, { patch: n }] of e.inserts) {
|
|
705
|
+
let [e, r, a] = t.split(":"), o = l(e, r), c = s.get(o) ?? [], u = {
|
|
706
|
+
id: a,
|
|
707
|
+
...n
|
|
708
|
+
};
|
|
709
|
+
i && (u.createdBy = i, u.updatedBy = i), c.push(u), s.set(o, c);
|
|
710
|
+
}
|
|
711
|
+
let u = a === "main", d = u ? Array.from(s.entries()).map(([e, t]) => {
|
|
712
|
+
let [n, r] = e.includes(".") ? e.split(".") : ["public", e];
|
|
713
|
+
return {
|
|
714
|
+
table: r,
|
|
715
|
+
schema: n,
|
|
716
|
+
data: t
|
|
717
|
+
};
|
|
718
|
+
}) : [], f = u ? Array.from(c.entries()).map(([e, t]) => {
|
|
719
|
+
let [n, r] = e.includes(".") ? e.split(".") : ["public", e];
|
|
720
|
+
return {
|
|
721
|
+
table: r,
|
|
722
|
+
schema: n,
|
|
723
|
+
ids: t
|
|
724
|
+
};
|
|
725
|
+
}) : [], { data: p, error: m } = await this.#e.rpc("rxdb_mutations", {
|
|
726
|
+
p_upserts: d,
|
|
727
|
+
p_deletes: f,
|
|
728
|
+
p_changes: o,
|
|
729
|
+
p_skip_sync: !0
|
|
730
|
+
});
|
|
731
|
+
if (m) throw new g(`Failed to merge changes: ${m.message}`);
|
|
732
|
+
return {
|
|
733
|
+
maxChangeId: p?.max_change_id ?? void 0,
|
|
734
|
+
changeIdMapping: p?.change_id_mapping ?? void 0
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
async pullChangesBatch(e, t = 1e3, n) {
|
|
738
|
+
if (e.length === 0) return [];
|
|
739
|
+
if (e.length === 1 && (!n || n.length <= 1)) return this.pullChanges(e[0].sinceId, t, [e[0].entity], void 0, n?.[0]);
|
|
740
|
+
let r = e.map((e) => `and(entity.eq.${e.entity},id.gt.${e.sinceId})`).join(","), i = this.#e.from("rxdb_change").select("*").or(r).order("id", { ascending: !0 }).limit(t);
|
|
741
|
+
n && n.length > 0 && (i = n.length === 1 ? i.eq("branchId", n[0]) : i.in("branchId", n));
|
|
742
|
+
let { data: a, error: o } = await i;
|
|
743
|
+
if (o) throw new g(`Failed to batch pull changes: ${o.message}`);
|
|
744
|
+
return (a ?? []).map((e) => ({
|
|
745
|
+
...e,
|
|
746
|
+
createdAt: new Date(e.createdAt),
|
|
747
|
+
updatedAt: e.updatedAt ? new Date(e.updatedAt) : null
|
|
748
|
+
}));
|
|
749
|
+
}
|
|
750
|
+
async pushBranches(e) {
|
|
751
|
+
if (e.length === 0) return {
|
|
752
|
+
synced: 0,
|
|
753
|
+
skipped: []
|
|
754
|
+
};
|
|
755
|
+
let { data: t, error: n } = await this.#e.rpc("rxdb_enable_sync_for_branch", { p_branches: e });
|
|
756
|
+
if (n) throw new g(`Failed to sync branches: ${n.message}`);
|
|
757
|
+
return {
|
|
758
|
+
synced: t?.synced ?? 0,
|
|
759
|
+
skipped: t?.skipped ?? []
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
async branchExists(e) {
|
|
763
|
+
let { count: t, error: n } = await this.#e.from("rxdb_branch").select("id", {
|
|
764
|
+
count: "exact",
|
|
765
|
+
head: !0
|
|
766
|
+
}).eq("id", e);
|
|
767
|
+
if (n) throw new g(`Failed to check branch existence: ${n.message}`);
|
|
768
|
+
return (t ?? 0) > 0;
|
|
769
|
+
}
|
|
770
|
+
async pullBranches() {
|
|
771
|
+
let { data: e, error: t } = await this.#e.from("rxdb_branch").select("id, fromChangeId, parentId, createdAt, updatedAt");
|
|
772
|
+
if (t) throw new g(`Failed to pull branches: ${t.message}`);
|
|
773
|
+
return (e ?? []).map((e) => ({
|
|
774
|
+
id: e.id,
|
|
775
|
+
fromChangeId: e.fromChangeId,
|
|
776
|
+
parentId: e.parentId,
|
|
777
|
+
createdAt: e.createdAt,
|
|
778
|
+
updatedAt: e.updatedAt
|
|
779
|
+
}));
|
|
780
|
+
}
|
|
781
|
+
fetchMetadata(e, t) {
|
|
782
|
+
return l(() => {
|
|
783
|
+
let n = this.rxdb.schemaManager.getEntityMetadata(e, "public")?.tableName ?? e, r = this.#e.from(n).select("id, updatedAt");
|
|
784
|
+
return r = T(r, t), u(r).pipe(d(({ data: e, error: t }) => {
|
|
785
|
+
if (t) throw new g(`Failed to fetch metadata: ${t.message}`);
|
|
786
|
+
return (e ?? []).map((e) => ({
|
|
787
|
+
id: e.id,
|
|
788
|
+
updatedAt: e.updatedAt
|
|
789
|
+
}));
|
|
790
|
+
}));
|
|
791
|
+
});
|
|
792
|
+
}
|
|
793
|
+
findByIds(e, t) {
|
|
794
|
+
return l(() => {
|
|
795
|
+
if (t.length === 0) return f([]);
|
|
796
|
+
let n = this.rxdb.schemaManager.getEntityMetadata(e, "public")?.tableName ?? e;
|
|
797
|
+
return u(this.#e.from(n).select("*").in("id", t)).pipe(d(({ data: e, error: t }) => {
|
|
798
|
+
if (t) throw new g(`Failed to find by ids: ${t.message}`);
|
|
799
|
+
return e ?? [];
|
|
800
|
+
}));
|
|
801
|
+
});
|
|
802
|
+
}
|
|
803
|
+
getSchemaClient(e) {
|
|
804
|
+
let t = F(e);
|
|
805
|
+
return t ? this.#e.schema(t) : this.#e;
|
|
806
|
+
}
|
|
807
|
+
async executeRetryableWrite(e, t) {
|
|
808
|
+
let n = "Unknown error";
|
|
809
|
+
for (let r = 1; r <= H; r++) {
|
|
810
|
+
let { data: i, error: a } = await t();
|
|
811
|
+
if (!a) return i;
|
|
812
|
+
if (n = a.message || "Unknown error", !Y(n) || r === H) throw new g(`Failed to ${e}: ${n}`);
|
|
813
|
+
await J(U * r);
|
|
814
|
+
}
|
|
815
|
+
throw new g(`Failed to ${e}: ${n}`);
|
|
816
|
+
}
|
|
817
|
+
async executeUpsert(e) {
|
|
818
|
+
let t = [], n = this.rxdb.context.userId;
|
|
819
|
+
for (let [r, i] of e) {
|
|
820
|
+
let e = o(r), a = this.getSchemaClient(e.namespace), s = Array.from(i), c = n ? s.map((e) => ({
|
|
821
|
+
...e,
|
|
822
|
+
createdBy: n,
|
|
823
|
+
updatedBy: n
|
|
824
|
+
})) : s, l = await this.executeRetryableWrite("upsert", async () => {
|
|
825
|
+
let { data: t, error: n } = await a.from(e.tableName).upsert(c).select();
|
|
826
|
+
return {
|
|
827
|
+
data: t,
|
|
828
|
+
error: n
|
|
829
|
+
};
|
|
830
|
+
});
|
|
831
|
+
t.push(...l);
|
|
832
|
+
}
|
|
833
|
+
return t;
|
|
834
|
+
}
|
|
835
|
+
async executeDelete(e) {
|
|
836
|
+
let t = [];
|
|
837
|
+
for (let [n, r] of e) {
|
|
838
|
+
let e = o(n), i = this.getSchemaClient(e.namespace), a = Array.from(r), s = a.map((e) => e.id);
|
|
839
|
+
await this.executeRetryableWrite("delete", async () => {
|
|
840
|
+
let { error: t } = await i.from(e.tableName).delete().in("id", s);
|
|
841
|
+
return {
|
|
842
|
+
data: void 0,
|
|
843
|
+
error: t
|
|
844
|
+
};
|
|
845
|
+
}), t.push(...a);
|
|
846
|
+
}
|
|
847
|
+
return t;
|
|
848
|
+
}
|
|
849
|
+
async #o() {
|
|
850
|
+
if (this.#a || this.options.rlsCheck === !1) return;
|
|
851
|
+
this.#a = !0;
|
|
852
|
+
let e = this.#s(), t = this.#c(e.tables);
|
|
853
|
+
if (t.length !== 0) try {
|
|
854
|
+
let { data: n, error: r } = await this.#e.rpc(e.rpcName, { p_tables: t });
|
|
855
|
+
if (r) {
|
|
856
|
+
this.#d(this.#l(e.rpcName, r.message), e.failureMode);
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
let i = (Array.isArray(n) ? n : []).filter((e) => e.exists !== !1 && e.rlsEnabled !== !0);
|
|
860
|
+
if (i.length > 0) {
|
|
861
|
+
let t = i.map((e) => `${e.schema}.${e.table}`).sort().join(", ");
|
|
862
|
+
this.#d(`[RxDB Supabase] RLS is disabled for tables: ${t}. Fix the policies before exposing this adapter to untrusted clients.`, e.failureMode);
|
|
863
|
+
}
|
|
864
|
+
} catch (t) {
|
|
865
|
+
this.#d(this.#u(t), e.failureMode);
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
#s() {
|
|
869
|
+
return this.options.rlsCheck && typeof this.options.rlsCheck == "object" ? {
|
|
870
|
+
rpcName: this.options.rlsCheck.rpcName ?? W,
|
|
871
|
+
failureMode: this.options.rlsCheck.failureMode ?? "warn",
|
|
872
|
+
tables: this.options.rlsCheck.tables ?? []
|
|
873
|
+
} : {
|
|
874
|
+
rpcName: W,
|
|
875
|
+
failureMode: "warn",
|
|
876
|
+
tables: []
|
|
877
|
+
};
|
|
878
|
+
}
|
|
879
|
+
#c(e) {
|
|
880
|
+
let t = e.length > 0 ? e : this.rxdb.config.entities.map((e) => {
|
|
881
|
+
let t = o(e);
|
|
882
|
+
return {
|
|
883
|
+
schema: F(t.namespace),
|
|
884
|
+
table: t.tableName
|
|
885
|
+
};
|
|
886
|
+
}), n = /* @__PURE__ */ new Map();
|
|
887
|
+
for (let e of t) {
|
|
888
|
+
let t = e.schema ?? "public", r = `${t}:${e.table}`;
|
|
889
|
+
n.set(r, {
|
|
890
|
+
schema: t,
|
|
891
|
+
table: e.table
|
|
892
|
+
});
|
|
893
|
+
}
|
|
894
|
+
return [...n.values()];
|
|
895
|
+
}
|
|
896
|
+
#l(e, t) {
|
|
897
|
+
let n = t?.trim() || "unknown error";
|
|
898
|
+
return /does not exist/i.test(n) ? `[RxDB Supabase] RLS self-check skipped because RPC "${e}" is not installed. Apply docker/sql/04-rxdb-utils-functions.sql or set rlsCheck=false if you intentionally manage RLS verification elsewhere.` : `[RxDB Supabase] Failed to verify RLS via RPC "${e}": ${n}`;
|
|
899
|
+
}
|
|
900
|
+
#u(e) {
|
|
901
|
+
return `[RxDB Supabase] Failed to verify RLS: ${e instanceof Error ? e.message : String(e)}`;
|
|
902
|
+
}
|
|
903
|
+
#d(e, t) {
|
|
904
|
+
if (t === "throw") throw new g(e);
|
|
905
|
+
console.warn(e);
|
|
906
|
+
}
|
|
907
|
+
#f() {
|
|
908
|
+
let e = this.#e.channel("rxdb-changes").on("postgres_changes", {
|
|
909
|
+
event: "INSERT",
|
|
910
|
+
schema: "public",
|
|
911
|
+
table: "rxdb_change"
|
|
912
|
+
}, (e) => v(this, e));
|
|
913
|
+
return e.subscribe((t, n) => {
|
|
914
|
+
if (this.#t === e) {
|
|
915
|
+
if (t === "SUBSCRIBED") {
|
|
916
|
+
this.#r = 0;
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
n && console.error("Supabase Realtime subscription failed:", n), q.has(t) && this.#p(e, t, n?.message);
|
|
920
|
+
}
|
|
921
|
+
}), e;
|
|
922
|
+
}
|
|
923
|
+
#p(e, t, n) {
|
|
924
|
+
if (!this.#i || this.#n || this.#t !== e) return;
|
|
925
|
+
this.#r += 1;
|
|
926
|
+
let r = Math.min(G * 2 ** (this.#r - 1), K), i = n?.trim();
|
|
927
|
+
console.warn(`[RxDB Supabase] Realtime channel ${t} detected. Retrying in ${r}ms (attempt ${this.#r})${i ? `: ${i}` : ""}.`), this.#n = setTimeout(() => {
|
|
928
|
+
this.#n = null, this.#m(e);
|
|
929
|
+
}, r);
|
|
930
|
+
}
|
|
931
|
+
async #m(e) {
|
|
932
|
+
!this.#i || this.#t !== e || (this.#t = null, await this.#e.removeChannel(e), this.#i && (this.#t = this.#f()));
|
|
933
|
+
}
|
|
934
|
+
#h() {
|
|
935
|
+
this.#n &&= (clearTimeout(this.#n), null);
|
|
936
|
+
}
|
|
937
|
+
async #g(e, t) {
|
|
938
|
+
let n = this.rxdb.schemaManager.getEntityMetadata(e, "public")?.tableName ?? e, r = this.#e.from(n).select("id");
|
|
939
|
+
r = T(r, t);
|
|
940
|
+
let { data: i, error: a } = await r;
|
|
941
|
+
if (a) throw new g(`Failed to query filtered entities: ${a.message}`);
|
|
942
|
+
return (i ?? []).map((e) => e.id);
|
|
943
|
+
}
|
|
1051
944
|
};
|
|
945
|
+
//#endregion
|
|
946
|
+
export { B as ADAPTER_NAME, X as RxDBAdapterSupabase, m as SupabaseConfigError, g as SupabaseDataError, h as SupabaseNetworkError, R as SupabaseRepository, p as SupabaseSyncError, z as SupabaseTreeRepository };
|