@aiao/rxdb-adapter-pglite 0.0.13 → 0.0.14
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/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PropertyType as
|
|
1
|
+
import { PropertyType as N, RxDBChange as ie, getEntityMetadata as D, EntityLocalCreatedEvent as oe, EntityLocalRemovedEvent as Ve, EntityLocalUpdatedEvent as me, RelationKind as F, RepositoryBase as Fn, getEntityStatus as J, RxDBBranch as ne, parseRxDBChangeKey as Ae, getEntityType as Re, RxDBAdapterLocalBase as kn } from "@aiao/rxdb";
|
|
2
2
|
import { EventDispatcher as Wn, traverseObjectKeys as Gn, isFunction as It, isString as Hn, AsyncQueueExecutor as Vn } from "@aiao/utils";
|
|
3
3
|
import { PGlite as Kn } from "@electric-sql/pglite";
|
|
4
4
|
import Ot from "object-hash";
|
|
@@ -20,55 +20,58 @@ class x extends Error {
|
|
|
20
20
|
}
|
|
21
21
|
const vt = (e, t = "public") => `"${t}"."${e}"`, P = (e) => vt(e.tableName, e.namespace), Dt = (e) => {
|
|
22
22
|
switch (e.type) {
|
|
23
|
-
case
|
|
23
|
+
case N.uuid:
|
|
24
24
|
return "uuid";
|
|
25
|
-
case
|
|
25
|
+
case N.string:
|
|
26
|
+
case N.enum:
|
|
26
27
|
return "varchar";
|
|
27
|
-
case
|
|
28
|
+
case N.json:
|
|
28
29
|
return "jsonb";
|
|
29
|
-
case
|
|
30
|
+
case N.number:
|
|
30
31
|
return "numeric";
|
|
31
|
-
case
|
|
32
|
+
case N.integer:
|
|
32
33
|
return "integer";
|
|
33
|
-
case
|
|
34
|
+
case N.boolean:
|
|
34
35
|
return "boolean";
|
|
35
|
-
case
|
|
36
|
+
case N.date:
|
|
36
37
|
return "timestamptz";
|
|
37
38
|
}
|
|
38
39
|
throw new x("rxDBColumnTypeToPGliteType: type '" + e.type + "' not support");
|
|
39
40
|
}, Xn = (e) => {
|
|
40
41
|
switch (e.type) {
|
|
41
|
-
case
|
|
42
|
+
case N.uuid:
|
|
42
43
|
return "uuid_ops";
|
|
43
|
-
case
|
|
44
|
+
case N.string:
|
|
45
|
+
case N.enum:
|
|
44
46
|
return "bpchar_ops";
|
|
45
|
-
case
|
|
47
|
+
case N.json:
|
|
46
48
|
return "jsonb_ops";
|
|
47
|
-
case
|
|
49
|
+
case N.number:
|
|
48
50
|
return "numeric_ops";
|
|
49
|
-
case
|
|
51
|
+
case N.integer:
|
|
50
52
|
return "int4_ops";
|
|
51
53
|
default:
|
|
52
54
|
throw new x(`rxDBColumnTypeToPGliteTypeIndexName: type '${e.type}' not support`);
|
|
53
55
|
}
|
|
54
|
-
},
|
|
56
|
+
}, Ct = (e, t) => `idx_${e.namespace}_${e.tableName}_${t.name}`, tt = (e, t) => {
|
|
55
57
|
if (e == null)
|
|
56
58
|
return null;
|
|
57
59
|
switch (t.type) {
|
|
58
|
-
case
|
|
59
|
-
case
|
|
60
|
-
case
|
|
61
|
-
case
|
|
60
|
+
case N.json:
|
|
61
|
+
case N.keyValue:
|
|
62
|
+
case N.stringArray:
|
|
63
|
+
case N.numberArray:
|
|
62
64
|
return JSON.stringify(e);
|
|
63
|
-
case
|
|
65
|
+
case N.date:
|
|
64
66
|
return e instanceof Date ? e.toISOString() : e;
|
|
65
|
-
case
|
|
67
|
+
case N.boolean:
|
|
66
68
|
return !!e;
|
|
67
|
-
case
|
|
68
|
-
case
|
|
69
|
+
case N.number:
|
|
70
|
+
case N.integer:
|
|
69
71
|
return Number(e);
|
|
70
|
-
case
|
|
71
|
-
case
|
|
72
|
+
case N.enum:
|
|
73
|
+
case N.string:
|
|
74
|
+
case N.uuid:
|
|
72
75
|
default:
|
|
73
76
|
return String(e);
|
|
74
77
|
}
|
|
@@ -89,14 +92,14 @@ const vt = (e, t = "public") => `"${t}"."${e}"`, P = (e) => vt(e.tableName, e.na
|
|
|
89
92
|
n[l.columnName] = tt(t[o], l);
|
|
90
93
|
continue;
|
|
91
94
|
}
|
|
92
|
-
const
|
|
93
|
-
if (
|
|
94
|
-
const f = e.propertyMap.get(
|
|
95
|
+
const d = e.columnNameToPropertyName?.get(o);
|
|
96
|
+
if (d) {
|
|
97
|
+
const f = e.propertyMap.get(d);
|
|
95
98
|
f && (n[o] = tt(t[o], f));
|
|
96
99
|
}
|
|
97
100
|
}
|
|
98
101
|
return n;
|
|
99
|
-
},
|
|
102
|
+
}, Lt = (e, t) => {
|
|
100
103
|
const n = {};
|
|
101
104
|
for (const [a, i] of e.propertyMap)
|
|
102
105
|
a in t && (n[i.columnName] = t[a]);
|
|
@@ -138,36 +141,36 @@ const vt = (e, t = "public") => `"${t}"."${e}"`, P = (e) => vt(e.tableName, e.na
|
|
|
138
141
|
const u = a?.get(i);
|
|
139
142
|
if (u) {
|
|
140
143
|
const l = e.propertyMap.get(u);
|
|
141
|
-
l && (l.type ===
|
|
144
|
+
l && (l.type === N.number && typeof o == "string" ? n[u] = parseFloat(o) : l.type === N.integer && typeof o == "string" ? n[u] = parseInt(o, 10) : n[u] = o);
|
|
142
145
|
return;
|
|
143
146
|
}
|
|
144
147
|
if (!a) {
|
|
145
148
|
const l = e.propertyMap.get(i);
|
|
146
149
|
if (l) {
|
|
147
|
-
l.type ===
|
|
150
|
+
l.type === N.number && typeof o == "string" ? n[i] = parseFloat(o) : l.type === N.integer && typeof o == "string" ? n[i] = parseInt(o, 10) : n[i] = o;
|
|
148
151
|
return;
|
|
149
152
|
}
|
|
150
153
|
}
|
|
151
154
|
e.isForeignKey(i), n[i] = o;
|
|
152
155
|
}), n;
|
|
153
|
-
},
|
|
156
|
+
}, Le = (e, t) => {
|
|
154
157
|
if (e == null) return e;
|
|
155
158
|
switch (t.type) {
|
|
156
|
-
case
|
|
159
|
+
case N.date:
|
|
157
160
|
return e instanceof Date ? e : new Date(e);
|
|
158
|
-
case
|
|
159
|
-
case
|
|
160
|
-
case
|
|
161
|
-
case
|
|
161
|
+
case N.json:
|
|
162
|
+
case N.keyValue:
|
|
163
|
+
case N.stringArray:
|
|
164
|
+
case N.numberArray:
|
|
162
165
|
return e;
|
|
163
|
-
case
|
|
166
|
+
case N.boolean:
|
|
164
167
|
return !!e;
|
|
165
168
|
default:
|
|
166
169
|
return e;
|
|
167
170
|
}
|
|
168
171
|
}, st = (e, t) => (Object.keys(t).forEach((n) => {
|
|
169
172
|
const r = t[n], s = e.propertyMap.get(n);
|
|
170
|
-
s && (t[n] =
|
|
173
|
+
s && (t[n] = Le(r, s));
|
|
171
174
|
}), t);
|
|
172
175
|
var es = Object.defineProperty, Mt = (e) => {
|
|
173
176
|
throw TypeError(e);
|
|
@@ -179,9 +182,9 @@ var es = Object.defineProperty, Mt = (e) => {
|
|
|
179
182
|
return h(e, t, r);
|
|
180
183
|
} }), H = ts(() => {
|
|
181
184
|
}), ns = {};
|
|
182
|
-
ye(ns, { ABSTIME: () => ms, ACLITEM: () =>
|
|
185
|
+
ye(ns, { ABSTIME: () => ms, ACLITEM: () => As, BIT: () => Is, BOOL: () => Me, BPCHAR: () => Ft, BYTEA: () => qe, CHAR: () => as, CID: () => ls, CIDR: () => Es, CIRCLE: () => _s, DATE: () => Wt, FLOAT4: () => xt, FLOAT8: () => Bt, GTSVECTOR: () => Gs, INET: () => $s, INT2: () => Pt, INT4: () => Ut, INT8: () => Pe, INTERVAL: () => bs, JSON: () => Ue, JSONB: () => Ht, MACADDR: () => Ns, MACADDR8: () => Ts, MONEY: () => ws, NUMERIC: () => vs, OID: () => jt, PATH: () => ds, PG_DEPENDENCIES: () => Fs, PG_LSN: () => xs, PG_NDISTINCT: () => Bs, PG_NODE_TREE: () => hs, POLYGON: () => ps, REFCURSOR: () => Ds, REGCLASS: () => qs, REGCONFIG: () => Hs, REGDICTIONARY: () => Vs, REGNAMESPACE: () => Ks, REGOPER: () => Ls, REGOPERATOR: () => Ms, REGPROC: () => is, REGPROCEDURE: () => Cs, REGROLE: () => Js, REGTYPE: () => Ps, RELTIME: () => ys, SMGR: () => fs, TEXT: () => ce, TID: () => os, TIME: () => Rs, TIMESTAMP: () => Gt, TIMESTAMPTZ: () => je, TIMETZ: () => Ss, TINTERVAL: () => gs, TSQUERY: () => Ws, TSVECTOR: () => ks, TXID_SNAPSHOT: () => js, UUID: () => Us, VARBIT: () => Os, VARCHAR: () => kt, XID: () => cs, XML: () => us, arrayParser: () => tr, arraySerializer: () => Kt, parseType: () => xe, parsers: () => Ys, serializers: () => Qs, types: () => Vt });
|
|
183
186
|
H();
|
|
184
|
-
var ss = globalThis.JSON.parse, rs = globalThis.JSON.stringify, Me = 16, qe = 17, as = 18, Pe = 20, Pt = 21, Ut = 23, is = 24, ce = 25, jt = 26, os = 27, cs = 28, ls = 29, Ue = 114, us = 142, hs = 194, fs = 210, ds = 602, ps = 604, Es = 650, xt = 700, Bt = 701, ms = 702, ys = 703,
|
|
187
|
+
var ss = globalThis.JSON.parse, rs = globalThis.JSON.stringify, Me = 16, qe = 17, as = 18, Pe = 20, Pt = 21, Ut = 23, is = 24, ce = 25, jt = 26, os = 27, cs = 28, ls = 29, Ue = 114, us = 142, hs = 194, fs = 210, ds = 602, ps = 604, Es = 650, xt = 700, Bt = 701, ms = 702, ys = 703, gs = 704, _s = 718, Ts = 774, ws = 790, Ns = 829, $s = 869, As = 1033, Ft = 1042, kt = 1043, Wt = 1082, Rs = 1083, Gt = 1114, je = 1184, bs = 1186, Ss = 1266, Is = 1560, Os = 1562, vs = 1700, Ds = 1790, Cs = 2202, Ls = 2203, Ms = 2204, qs = 2205, Ps = 2206, Us = 2950, js = 2970, xs = 3220, Bs = 3361, Fs = 3402, ks = 3614, Ws = 3615, Gs = 3642, Hs = 3734, Vs = 3769, Ht = 3802, Ks = 4089, Js = 4096, Vt = { string: { to: ce, from: [ce, kt, Ft], serialize: (e) => {
|
|
185
188
|
if (typeof e == "string") return e;
|
|
186
189
|
if (typeof e == "number") return e.toString();
|
|
187
190
|
throw new Error("Invalid input for string type");
|
|
@@ -259,7 +262,7 @@ function sr(e, t, n, r) {
|
|
|
259
262
|
case "dataRow": {
|
|
260
263
|
if (!a) break;
|
|
261
264
|
let u = c;
|
|
262
|
-
n?.rowMode === "array" ? a.rows.push(u.fields.map((l,
|
|
265
|
+
n?.rowMode === "array" ? a.rows.push(u.fields.map((l, d) => xe(l, a.fields[d].dataTypeID, o))) : a.rows.push(Object.fromEntries(u.fields.map((l, d) => [a.fields[d].name, xe(l, a.fields[d].dataTypeID, o)])));
|
|
263
266
|
break;
|
|
264
267
|
}
|
|
265
268
|
case "commandComplete": {
|
|
@@ -288,7 +291,7 @@ function Yt(e) {
|
|
|
288
291
|
return t ? t.dataTypeIDs : [];
|
|
289
292
|
}
|
|
290
293
|
var Qt = {};
|
|
291
|
-
ye(Qt, { AuthenticationCleartextPassword: () => on, AuthenticationMD5Password: () => cn, AuthenticationOk: () => an, AuthenticationSASL: () => ln, AuthenticationSASLContinue: () => un, AuthenticationSASLFinal: () => hn, BackendKeyDataMessage: () =>
|
|
294
|
+
ye(Qt, { AuthenticationCleartextPassword: () => on, AuthenticationMD5Password: () => cn, AuthenticationOk: () => an, AuthenticationSASL: () => ln, AuthenticationSASLContinue: () => un, AuthenticationSASLFinal: () => hn, BackendKeyDataMessage: () => gn, CommandCompleteMessage: () => wn, CopyDataMessage: () => fn, CopyResponse: () => dn, DataRowMessage: () => Nn, DatabaseError: () => Be, Field: () => pn, NoticeMessage: () => $n, NotificationResponseMessage: () => _n, ParameterDescriptionMessage: () => mn, ParameterStatusMessage: () => yn, ReadyForQueryMessage: () => Tn, RowDescriptionMessage: () => En, bindComplete: () => zt, closeComplete: () => Zt, copyDone: () => rn, emptyQuery: () => sn, noData: () => en, parseComplete: () => Xt, portalSuspended: () => tn, replicationStart: () => nn });
|
|
292
295
|
H();
|
|
293
296
|
var Xt = { name: "parseComplete", length: 5 }, zt = { name: "bindComplete", length: 5 }, Zt = { name: "closeComplete", length: 5 }, en = { name: "noData", length: 5 }, tn = { name: "portalSuspended", length: 5 }, nn = { name: "replicationStart", length: 4 }, sn = { name: "emptyQuery", length: 4 }, rn = { name: "copyDone", length: 4 }, an = class {
|
|
294
297
|
constructor(t) {
|
|
@@ -342,11 +345,11 @@ var Xt = { name: "parseComplete", length: 5 }, zt = { name: "bindComplete", leng
|
|
|
342
345
|
constructor(e, t, n) {
|
|
343
346
|
this.length = e, this.parameterName = t, this.parameterValue = n, this.name = "parameterStatus";
|
|
344
347
|
}
|
|
345
|
-
},
|
|
348
|
+
}, gn = class {
|
|
346
349
|
constructor(e, t, n) {
|
|
347
350
|
this.length = e, this.processID = t, this.secretKey = n, this.name = "backendKeyData";
|
|
348
351
|
}
|
|
349
|
-
},
|
|
352
|
+
}, _n = class {
|
|
350
353
|
constructor(e, t, n, r) {
|
|
351
354
|
this.length = e, this.processId = t, this.channel = n, this.payload = r, this.name = "notification";
|
|
352
355
|
}
|
|
@@ -362,17 +365,17 @@ var Xt = { name: "parseComplete", length: 5 }, zt = { name: "bindComplete", leng
|
|
|
362
365
|
constructor(e, t) {
|
|
363
366
|
this.length = e, this.fields = t, this.name = "dataRow", this.fieldCount = t.length;
|
|
364
367
|
}
|
|
365
|
-
},
|
|
368
|
+
}, $n = class {
|
|
366
369
|
constructor(e, t) {
|
|
367
370
|
this.length = e, this.message = t, this.name = "notice";
|
|
368
371
|
}
|
|
369
372
|
}, ar = {};
|
|
370
|
-
ye(ar, { Parser: () =>
|
|
373
|
+
ye(ar, { Parser: () => Cr, messages: () => Qt, serialize: () => re });
|
|
371
374
|
H();
|
|
372
375
|
H();
|
|
373
376
|
H();
|
|
374
377
|
H();
|
|
375
|
-
function
|
|
378
|
+
function ge(e) {
|
|
376
379
|
let t = e.length;
|
|
377
380
|
for (let n = e.length - 1; n >= 0; n--) {
|
|
378
381
|
let r = e.charCodeAt(n);
|
|
@@ -380,7 +383,7 @@ function _e(e) {
|
|
|
380
383
|
}
|
|
381
384
|
return t;
|
|
382
385
|
}
|
|
383
|
-
var U, M, te, Fe, ee, V, le, X,
|
|
386
|
+
var U, M, te, Fe, ee, V, le, X, An, _e = class {
|
|
384
387
|
constructor(e = 256) {
|
|
385
388
|
this.size = e, j(this, V), j(this, U), j(this, M, 5), j(this, te, !1), j(this, Fe, new TextEncoder()), j(this, ee, 0), v(this, U, O(this, V, le).call(this, e));
|
|
386
389
|
}
|
|
@@ -394,14 +397,14 @@ var U, M, te, Fe, ee, V, le, X, $n, ge = class {
|
|
|
394
397
|
return e && this.addString(e), O(this, V, X).call(this, 1), h(this, U).setUint8(h(this, M), 0), qt(this, M)._++, this;
|
|
395
398
|
}
|
|
396
399
|
addString(e = "") {
|
|
397
|
-
let t =
|
|
400
|
+
let t = ge(e);
|
|
398
401
|
return O(this, V, X).call(this, t), h(this, Fe).encodeInto(e, new Uint8Array(h(this, U).buffer, h(this, M))), v(this, M, h(this, M) + t), this;
|
|
399
402
|
}
|
|
400
403
|
add(e) {
|
|
401
404
|
return O(this, V, X).call(this, e.byteLength), new Uint8Array(h(this, U).buffer).set(new Uint8Array(e), h(this, M)), v(this, M, h(this, M) + e.byteLength), this;
|
|
402
405
|
}
|
|
403
406
|
flush(e) {
|
|
404
|
-
let t = O(this, V,
|
|
407
|
+
let t = O(this, V, An).call(this, e);
|
|
405
408
|
return v(this, M, 5), v(this, U, O(this, V, le).call(this, this.size)), new Uint8Array(t);
|
|
406
409
|
}
|
|
407
410
|
};
|
|
@@ -412,7 +415,7 @@ U = /* @__PURE__ */ new WeakMap(), M = /* @__PURE__ */ new WeakMap(), te = /* @_
|
|
|
412
415
|
let t = h(this, U).buffer, n = t.byteLength + (t.byteLength >> 1) + e;
|
|
413
416
|
v(this, U, O(this, V, le).call(this, n)), new Uint8Array(h(this, U).buffer).set(new Uint8Array(t));
|
|
414
417
|
}
|
|
415
|
-
},
|
|
418
|
+
}, An = function(e) {
|
|
416
419
|
if (e) {
|
|
417
420
|
h(this, U).setUint8(h(this, ee), e);
|
|
418
421
|
let t = h(this, M) - (h(this, ee) + 1);
|
|
@@ -420,46 +423,46 @@ U = /* @__PURE__ */ new WeakMap(), M = /* @__PURE__ */ new WeakMap(), te = /* @_
|
|
|
420
423
|
}
|
|
421
424
|
return h(this, U).buffer.slice(e ? 0 : 5, h(this, M));
|
|
422
425
|
};
|
|
423
|
-
var
|
|
424
|
-
|
|
425
|
-
for (let r of Object.keys(e))
|
|
426
|
-
|
|
427
|
-
let t =
|
|
428
|
-
return new
|
|
426
|
+
var C = new _e(), ir = (e) => {
|
|
427
|
+
C.addInt16(3).addInt16(0);
|
|
428
|
+
for (let r of Object.keys(e)) C.addCString(r).addCString(e[r]);
|
|
429
|
+
C.addCString("client_encoding").addCString("UTF8");
|
|
430
|
+
let t = C.addCString("").flush(), n = t.byteLength + 4;
|
|
431
|
+
return new _e().addInt32(n).add(t).flush();
|
|
429
432
|
}, or = () => {
|
|
430
433
|
let e = new DataView(new ArrayBuffer(8));
|
|
431
434
|
return e.setInt32(0, 8, !1), e.setInt32(4, 80877103, !1), new Uint8Array(e.buffer);
|
|
432
|
-
}, cr = (e) =>
|
|
435
|
+
}, cr = (e) => C.addCString(e).flush(112), lr = (e, t) => (C.addCString(e).addInt32(ge(t)).addString(t), C.flush(112)), ur = (e) => C.addString(e).flush(112), hr = (e) => C.addCString(e).flush(81), fr = [], dr = (e) => {
|
|
433
436
|
let t = e.name ?? "";
|
|
434
437
|
t.length > 63 && (console.error("Warning! Postgres only supports 63 characters for query names."), console.error("You supplied %s (%s)", t, t.length), console.error("This can cause conflicts and silent errors executing queries"));
|
|
435
|
-
let n =
|
|
436
|
-
return e.types?.forEach((r) => n.addInt32(r)),
|
|
437
|
-
}, z = new
|
|
438
|
+
let n = C.addCString(t).addCString(e.text).addInt16(e.types?.length ?? 0);
|
|
439
|
+
return e.types?.forEach((r) => n.addInt32(r)), C.flush(80);
|
|
440
|
+
}, z = new _e(), pr = (e, t) => {
|
|
438
441
|
for (let n = 0; n < e.length; n++) {
|
|
439
442
|
let r = t ? t(e[n], n) : e[n];
|
|
440
|
-
if (r === null)
|
|
443
|
+
if (r === null) C.addInt16(0), z.addInt32(-1);
|
|
441
444
|
else if (r instanceof ArrayBuffer || ArrayBuffer.isView(r)) {
|
|
442
445
|
let s = ArrayBuffer.isView(r) ? r.buffer.slice(r.byteOffset, r.byteOffset + r.byteLength) : r;
|
|
443
|
-
|
|
444
|
-
} else
|
|
446
|
+
C.addInt16(1), z.addInt32(s.byteLength), z.add(s);
|
|
447
|
+
} else C.addInt16(0), z.addInt32(ge(r)), z.addString(r);
|
|
445
448
|
}
|
|
446
449
|
}, Er = (e = {}) => {
|
|
447
450
|
let t = e.portal ?? "", n = e.statement ?? "", r = e.binary ?? !1, s = e.values ?? fr, a = s.length;
|
|
448
|
-
return
|
|
451
|
+
return C.addCString(t).addCString(n), C.addInt16(a), pr(s, e.valueMapper), C.addInt16(a), C.add(z.flush()), C.addInt16(r ? 1 : 0), C.flush(66);
|
|
449
452
|
}, mr = new Uint8Array([69, 0, 0, 0, 9, 0, 0, 0, 0, 0]), yr = (e) => {
|
|
450
453
|
if (!e || !e.portal && !e.rows) return mr;
|
|
451
|
-
let t = e.portal ?? "", n = e.rows ?? 0, r =
|
|
454
|
+
let t = e.portal ?? "", n = e.rows ?? 0, r = ge(t), s = 4 + r + 1 + 4, a = new DataView(new ArrayBuffer(1 + s));
|
|
452
455
|
return a.setUint8(0, 69), a.setInt32(1, s, !1), new TextEncoder().encodeInto(t, new Uint8Array(a.buffer, 5)), a.setUint8(r + 5, 0), a.setUint32(a.byteLength - 4, n, !1), new Uint8Array(a.buffer);
|
|
453
|
-
},
|
|
456
|
+
}, gr = (e, t) => {
|
|
454
457
|
let n = new DataView(new ArrayBuffer(16));
|
|
455
458
|
return n.setInt32(0, 16, !1), n.setInt16(4, 1234, !1), n.setInt16(6, 5678, !1), n.setInt32(8, e, !1), n.setInt32(12, t, !1), new Uint8Array(n.buffer);
|
|
456
459
|
}, Xe = (e, t) => {
|
|
457
|
-
let n = new
|
|
460
|
+
let n = new _e();
|
|
458
461
|
return n.addCString(t), n.flush(e);
|
|
459
|
-
},
|
|
462
|
+
}, _r = C.addCString("P").flush(68), Tr = C.addCString("S").flush(68), wr = (e) => e.name ? Xe(68, `${e.type}${e.name ?? ""}`) : e.type === "P" ? _r : Tr, Nr = (e) => {
|
|
460
463
|
let t = `${e.type}${e.name ?? ""}`;
|
|
461
464
|
return Xe(67, t);
|
|
462
|
-
},
|
|
465
|
+
}, $r = (e) => C.add(e).flush(100), Ar = (e) => Xe(102, e), Te = (e) => new Uint8Array([e, 0, 0, 0, 4]), Rr = Te(72), br = Te(83), Sr = Te(88), Ir = Te(99), re = { startup: ir, password: cr, requestSsl: or, sendSASLInitialResponseMessage: lr, sendSCRAMClientFinalMessage: ur, query: hr, parse: dr, bind: Er, execute: yr, describe: wr, close: Nr, flush: () => Rr, sync: () => br, end: () => Sr, copyData: $r, copyDone: () => Ir, copyFail: Ar, cancel: gr };
|
|
463
466
|
H();
|
|
464
467
|
H();
|
|
465
468
|
var rt = { text: 0, binary: 1 };
|
|
@@ -498,24 +501,24 @@ var Or = new ArrayBuffer(0), Y, q, ke, We, ae, vr = class {
|
|
|
498
501
|
}
|
|
499
502
|
};
|
|
500
503
|
Y = /* @__PURE__ */ new WeakMap(), q = /* @__PURE__ */ new WeakMap(), ke = /* @__PURE__ */ new WeakMap(), We = /* @__PURE__ */ new WeakMap(), ae = /* @__PURE__ */ new WeakMap();
|
|
501
|
-
var Ge = 1, Dr = 4, at = Ge + Dr, it = new ArrayBuffer(0), B, W, G, w,
|
|
504
|
+
var Ge = 1, Dr = 4, at = Ge + Dr, it = new ArrayBuffer(0), B, W, G, w, L, Rn, bn, ot, ct, lt, ut, ht, ve, ft, dt, pt, Et, mt, yt, gt, _t, De, Cr = class {
|
|
502
505
|
constructor() {
|
|
503
|
-
j(this,
|
|
506
|
+
j(this, L), j(this, B, new DataView(it)), j(this, W, 0), j(this, G, 0), j(this, w, new vr());
|
|
504
507
|
}
|
|
505
508
|
parse(e, t) {
|
|
506
|
-
O(this,
|
|
509
|
+
O(this, L, Rn).call(this, ArrayBuffer.isView(e) ? e.buffer.slice(e.byteOffset, e.byteOffset + e.byteLength) : e);
|
|
507
510
|
let n = h(this, G) + h(this, W), r = h(this, G);
|
|
508
511
|
for (; r + at <= n; ) {
|
|
509
512
|
let s = h(this, B).getUint8(r), a = h(this, B).getUint32(r + Ge, !1), i = Ge + a;
|
|
510
513
|
if (i + r <= n && a > 0) {
|
|
511
|
-
let o = O(this,
|
|
514
|
+
let o = O(this, L, bn).call(this, r + at, s, a, h(this, B).buffer);
|
|
512
515
|
t(o), r += i;
|
|
513
516
|
} else break;
|
|
514
517
|
}
|
|
515
518
|
r === n ? (v(this, B, new DataView(it)), v(this, W, 0), v(this, G, 0)) : (v(this, W, n - r), v(this, G, r));
|
|
516
519
|
}
|
|
517
520
|
};
|
|
518
|
-
B = /* @__PURE__ */ new WeakMap(), W = /* @__PURE__ */ new WeakMap(), G = /* @__PURE__ */ new WeakMap(), w = /* @__PURE__ */ new WeakMap(),
|
|
521
|
+
B = /* @__PURE__ */ new WeakMap(), W = /* @__PURE__ */ new WeakMap(), G = /* @__PURE__ */ new WeakMap(), w = /* @__PURE__ */ new WeakMap(), L = /* @__PURE__ */ new WeakSet(), Rn = function(e) {
|
|
519
522
|
if (h(this, W) > 0) {
|
|
520
523
|
let t = h(this, W) + e.byteLength;
|
|
521
524
|
if (t + h(this, G) > h(this, B).byteLength) {
|
|
@@ -549,33 +552,33 @@ B = /* @__PURE__ */ new WeakMap(), W = /* @__PURE__ */ new WeakMap(), G = /* @__
|
|
|
549
552
|
case 73:
|
|
550
553
|
return sn;
|
|
551
554
|
case 68:
|
|
552
|
-
return O(this,
|
|
555
|
+
return O(this, L, mt).call(this, e, n, r);
|
|
553
556
|
case 67:
|
|
554
|
-
return O(this,
|
|
557
|
+
return O(this, L, ct).call(this, e, n, r);
|
|
555
558
|
case 90:
|
|
556
|
-
return O(this,
|
|
559
|
+
return O(this, L, ot).call(this, e, n, r);
|
|
557
560
|
case 65:
|
|
558
|
-
return O(this,
|
|
561
|
+
return O(this, L, ft).call(this, e, n, r);
|
|
559
562
|
case 82:
|
|
560
|
-
return O(this,
|
|
563
|
+
return O(this, L, _t).call(this, e, n, r);
|
|
561
564
|
case 83:
|
|
562
|
-
return O(this,
|
|
565
|
+
return O(this, L, yt).call(this, e, n, r);
|
|
563
566
|
case 75:
|
|
564
|
-
return O(this,
|
|
567
|
+
return O(this, L, gt).call(this, e, n, r);
|
|
565
568
|
case 69:
|
|
566
|
-
return O(this,
|
|
569
|
+
return O(this, L, De).call(this, e, n, r, "error");
|
|
567
570
|
case 78:
|
|
568
|
-
return O(this,
|
|
571
|
+
return O(this, L, De).call(this, e, n, r, "notice");
|
|
569
572
|
case 84:
|
|
570
|
-
return O(this,
|
|
573
|
+
return O(this, L, dt).call(this, e, n, r);
|
|
571
574
|
case 116:
|
|
572
|
-
return O(this,
|
|
575
|
+
return O(this, L, Et).call(this, e, n, r);
|
|
573
576
|
case 71:
|
|
574
|
-
return O(this,
|
|
577
|
+
return O(this, L, ut).call(this, e, n, r);
|
|
575
578
|
case 72:
|
|
576
|
-
return O(this,
|
|
579
|
+
return O(this, L, ht).call(this, e, n, r);
|
|
577
580
|
case 100:
|
|
578
|
-
return O(this,
|
|
581
|
+
return O(this, L, lt).call(this, e, n, r);
|
|
579
582
|
default:
|
|
580
583
|
return new Be("received invalid response: " + t.toString(16), n, "error");
|
|
581
584
|
}
|
|
@@ -591,9 +594,9 @@ B = /* @__PURE__ */ new WeakMap(), W = /* @__PURE__ */ new WeakMap(), G = /* @__
|
|
|
591
594
|
let r = n.slice(e, e + (t - 4));
|
|
592
595
|
return new fn(t, new Uint8Array(r));
|
|
593
596
|
}, ut = function(e, t, n) {
|
|
594
|
-
return O(this,
|
|
597
|
+
return O(this, L, ve).call(this, e, t, n, "copyInResponse");
|
|
595
598
|
}, ht = function(e, t, n) {
|
|
596
|
-
return O(this,
|
|
599
|
+
return O(this, L, ve).call(this, e, t, n, "copyOutResponse");
|
|
597
600
|
}, ve = function(e, t, n, r) {
|
|
598
601
|
h(this, w).setBuffer(e, n);
|
|
599
602
|
let s = h(this, w).byte() !== 0, a = h(this, w).int16(), i = new dn(t, r, s, a);
|
|
@@ -602,11 +605,11 @@ B = /* @__PURE__ */ new WeakMap(), W = /* @__PURE__ */ new WeakMap(), G = /* @__
|
|
|
602
605
|
}, ft = function(e, t, n) {
|
|
603
606
|
h(this, w).setBuffer(e, n);
|
|
604
607
|
let r = h(this, w).int32(), s = h(this, w).cstring(), a = h(this, w).cstring();
|
|
605
|
-
return new
|
|
608
|
+
return new _n(t, r, s, a);
|
|
606
609
|
}, dt = function(e, t, n) {
|
|
607
610
|
h(this, w).setBuffer(e, n);
|
|
608
611
|
let r = h(this, w).int16(), s = new En(t, r);
|
|
609
|
-
for (let a = 0; a < r; a++) s.fields[a] = O(this,
|
|
612
|
+
for (let a = 0; a < r; a++) s.fields[a] = O(this, L, pt).call(this);
|
|
610
613
|
return s;
|
|
611
614
|
}, pt = function() {
|
|
612
615
|
let e = h(this, w).cstring(), t = h(this, w).int32(), n = h(this, w).int16(), r = h(this, w).int32(), s = h(this, w).int16(), a = h(this, w).int32(), i = h(this, w).int16() === 0 ? rt.text : rt.binary;
|
|
@@ -628,11 +631,11 @@ B = /* @__PURE__ */ new WeakMap(), W = /* @__PURE__ */ new WeakMap(), G = /* @__
|
|
|
628
631
|
h(this, w).setBuffer(e, n);
|
|
629
632
|
let r = h(this, w).cstring(), s = h(this, w).cstring();
|
|
630
633
|
return new yn(t, r, s);
|
|
631
|
-
},
|
|
634
|
+
}, gt = function(e, t, n) {
|
|
632
635
|
h(this, w).setBuffer(e, n);
|
|
633
636
|
let r = h(this, w).int32(), s = h(this, w).int32();
|
|
634
|
-
return new
|
|
635
|
-
},
|
|
637
|
+
return new gn(t, r, s);
|
|
638
|
+
}, _t = function(e, t, n) {
|
|
636
639
|
h(this, w).setBuffer(e, n);
|
|
637
640
|
let r = h(this, w).int32();
|
|
638
641
|
switch (r) {
|
|
@@ -661,7 +664,7 @@ B = /* @__PURE__ */ new WeakMap(), W = /* @__PURE__ */ new WeakMap(), G = /* @__
|
|
|
661
664
|
h(this, w).setBuffer(e, n);
|
|
662
665
|
let s = {}, a = h(this, w).string(1);
|
|
663
666
|
for (; a !== "\0"; ) s[a] = h(this, w).cstring(), a = h(this, w).string(1);
|
|
664
|
-
let i = s.M, o = r === "notice" ? new
|
|
667
|
+
let i = s.M, o = r === "notice" ? new $n(t, i) : new Be(i, t, r);
|
|
665
668
|
return o.severity = s.S, o.code = s.C, o.detail = s.D, o.hint = s.H, o.position = s.P, o.internalPosition = s.p, o.internalQuery = s.q, o.where = s.W, o.schema = s.s, o.table = s.t, o.column = s.c, o.dataType = s.d, o.constraint = s.n, o.file = s.F, o.line = s.L, o.routine = s.R, o;
|
|
666
669
|
};
|
|
667
670
|
H();
|
|
@@ -716,63 +719,63 @@ function Nt(e) {
|
|
|
716
719
|
};
|
|
717
720
|
}
|
|
718
721
|
H();
|
|
719
|
-
var
|
|
722
|
+
var Lr = 5, Mr = async (e, t) => {
|
|
720
723
|
let n = /* @__PURE__ */ new Set(), r = { async query(s, a, i) {
|
|
721
724
|
let o, c, u;
|
|
722
725
|
if (typeof s != "string" && (o = s.signal, a = s.params, i = s.callback, c = s.offset, u = s.limit, s = s.query), c === void 0 != (u === void 0)) throw new Error("offset and limit must be provided together");
|
|
723
|
-
let l = c !== void 0 && u !== void 0,
|
|
726
|
+
let l = c !== void 0 && u !== void 0, d;
|
|
724
727
|
if (l && (typeof c != "number" || isNaN(c) || typeof u != "number" || isNaN(u))) throw new Error("offset and limit must be numbers");
|
|
725
|
-
let f = i ? [i] : [],
|
|
726
|
-
await e.transaction(async (
|
|
727
|
-
let R = a && a.length > 0 ? await wt(e, s, a,
|
|
728
|
-
await
|
|
729
|
-
let I = await
|
|
730
|
-
await
|
|
731
|
-
PREPARE live_query_${
|
|
732
|
-
SELECT * FROM live_query_${
|
|
728
|
+
let f = i ? [i] : [], p = Tt().replace(/-/g, ""), _ = !1, S, $, A = async () => {
|
|
729
|
+
await e.transaction(async (g) => {
|
|
730
|
+
let R = a && a.length > 0 ? await wt(e, s, a, g) : s;
|
|
731
|
+
await g.exec(`CREATE OR REPLACE TEMP VIEW live_query_${p}_view AS ${R}`);
|
|
732
|
+
let I = await $t(g, `live_query_${p}_view`);
|
|
733
|
+
await At(g, I, n), l ? (await g.exec(`
|
|
734
|
+
PREPARE live_query_${p}_get(int, int) AS
|
|
735
|
+
SELECT * FROM live_query_${p}_view
|
|
733
736
|
LIMIT $1 OFFSET $2;
|
|
734
|
-
`), await
|
|
735
|
-
PREPARE live_query_${
|
|
736
|
-
SELECT COUNT(*) FROM live_query_${
|
|
737
|
-
`),
|
|
738
|
-
PREPARE live_query_${
|
|
739
|
-
SELECT * FROM live_query_${
|
|
740
|
-
`), S = await
|
|
737
|
+
`), await g.exec(`
|
|
738
|
+
PREPARE live_query_${p}_get_total_count AS
|
|
739
|
+
SELECT COUNT(*) FROM live_query_${p}_view;
|
|
740
|
+
`), d = (await g.query(`EXECUTE live_query_${p}_get_total_count;`)).rows[0].count, S = { ...await g.query(`EXECUTE live_query_${p}_get(${u}, ${c});`), offset: c, limit: u, totalCount: d }) : (await g.exec(`
|
|
741
|
+
PREPARE live_query_${p}_get AS
|
|
742
|
+
SELECT * FROM live_query_${p}_view;
|
|
743
|
+
`), S = await g.query(`EXECUTE live_query_${p}_get;`)), $ = await Promise.all(I.map((y) => g.listen(`"table_change__${y.schema_oid}__${y.table_oid}"`, async () => {
|
|
741
744
|
b();
|
|
742
745
|
})));
|
|
743
746
|
});
|
|
744
747
|
};
|
|
745
|
-
await
|
|
746
|
-
let b = Nt(async ({ offset:
|
|
747
|
-
if (!l && (
|
|
748
|
-
if (
|
|
749
|
-
c =
|
|
748
|
+
await A();
|
|
749
|
+
let b = Nt(async ({ offset: g, limit: R } = {}) => {
|
|
750
|
+
if (!l && (g !== void 0 || R !== void 0)) throw new Error("offset and limit cannot be provided for non-windowed queries");
|
|
751
|
+
if (g && (typeof g != "number" || isNaN(g)) || R && (typeof R != "number" || isNaN(R))) throw new Error("offset and limit must be numbers");
|
|
752
|
+
c = g ?? c, u = R ?? u;
|
|
750
753
|
let I = async (y = 0) => {
|
|
751
754
|
if (f.length !== 0) {
|
|
752
755
|
try {
|
|
753
|
-
l ? S = { ...await e.query(`EXECUTE live_query_${
|
|
756
|
+
l ? S = { ...await e.query(`EXECUTE live_query_${p}_get(${u}, ${c});`), offset: c, limit: u, totalCount: d } : S = await e.query(`EXECUTE live_query_${p}_get;`);
|
|
754
757
|
} catch (T) {
|
|
755
758
|
let k = T.message;
|
|
756
|
-
if (k.startsWith(`prepared statement "live_query_${
|
|
757
|
-
if (y >
|
|
758
|
-
await
|
|
759
|
+
if (k.startsWith(`prepared statement "live_query_${p}`) && k.endsWith("does not exist")) {
|
|
760
|
+
if (y > Lr) throw T;
|
|
761
|
+
await A(), I(y + 1);
|
|
759
762
|
} else throw T;
|
|
760
763
|
}
|
|
761
764
|
if (se(f, S), l) {
|
|
762
|
-
let T = (await e.query(`EXECUTE live_query_${
|
|
763
|
-
T !==
|
|
765
|
+
let T = (await e.query(`EXECUTE live_query_${p}_get_total_count;`)).rows[0].count;
|
|
766
|
+
T !== d && (d = T, b());
|
|
764
767
|
}
|
|
765
768
|
}
|
|
766
769
|
};
|
|
767
770
|
await I();
|
|
768
|
-
}), E = (
|
|
769
|
-
if (
|
|
770
|
-
f.push(
|
|
771
|
-
}, m = async (
|
|
772
|
-
|
|
773
|
-
await Promise.all(
|
|
774
|
-
DROP VIEW IF EXISTS live_query_${
|
|
775
|
-
DEALLOCATE live_query_${
|
|
771
|
+
}), E = (g) => {
|
|
772
|
+
if (_) throw new Error("Live query is no longer active and cannot be subscribed to");
|
|
773
|
+
f.push(g);
|
|
774
|
+
}, m = async (g) => {
|
|
775
|
+
g ? f = f.filter((R) => R !== R) : f = [], f.length === 0 && !_ && (_ = !0, await e.transaction(async (R) => {
|
|
776
|
+
await Promise.all($.map((I) => I(R))), await R.exec(`
|
|
777
|
+
DROP VIEW IF EXISTS live_query_${p}_view;
|
|
778
|
+
DEALLOCATE live_query_${p}_get;
|
|
776
779
|
`);
|
|
777
780
|
}));
|
|
778
781
|
};
|
|
@@ -782,12 +785,12 @@ var Cr = 5, Mr = async (e, t) => {
|
|
|
782
785
|
}, async changes(s, a, i, o) {
|
|
783
786
|
let c;
|
|
784
787
|
if (typeof s != "string" && (c = s.signal, a = s.params, i = s.key, o = s.callback, s = s.query), !i) throw new Error("key is required for changes queries");
|
|
785
|
-
let u = o ? [o] : [], l = Tt().replace(/-/g, ""),
|
|
788
|
+
let u = o ? [o] : [], l = Tt().replace(/-/g, ""), d = !1, f = 1, p, _, S = async () => {
|
|
786
789
|
await e.transaction(async (E) => {
|
|
787
790
|
let m = await wt(e, s, a, E);
|
|
788
791
|
await E.query(`CREATE OR REPLACE TEMP VIEW live_query_${l}_view AS ${m}`);
|
|
789
|
-
let
|
|
790
|
-
await
|
|
792
|
+
let g = await $t(E, `live_query_${l}_view`);
|
|
793
|
+
await At(E, g, n);
|
|
791
794
|
let R = [...(await E.query(`
|
|
792
795
|
SELECT column_name, data_type, udt_name
|
|
793
796
|
FROM information_schema.columns
|
|
@@ -846,38 +849,38 @@ var Cr = 5, Mr = async (e, t) => {
|
|
|
846
849
|
SELECT * FROM data_diff;
|
|
847
850
|
`);
|
|
848
851
|
}
|
|
849
|
-
|
|
850
|
-
|
|
852
|
+
_ = await Promise.all(g.map((I) => E.listen(`"table_change__${I.schema_oid}__${I.table_oid}"`, async () => {
|
|
853
|
+
$();
|
|
851
854
|
})));
|
|
852
855
|
});
|
|
853
856
|
};
|
|
854
857
|
await S();
|
|
855
|
-
let
|
|
856
|
-
if (u.length === 0 &&
|
|
858
|
+
let $ = Nt(async () => {
|
|
859
|
+
if (u.length === 0 && p) return;
|
|
857
860
|
let E = !1;
|
|
858
861
|
for (let m = 0; m < 5; m++) try {
|
|
859
|
-
await e.transaction(async (
|
|
860
|
-
await
|
|
862
|
+
await e.transaction(async (g) => {
|
|
863
|
+
await g.exec(`
|
|
861
864
|
INSERT INTO live_query_${l}_state${f}
|
|
862
865
|
SELECT * FROM live_query_${l}_view;
|
|
863
|
-
`),
|
|
866
|
+
`), p = await g.query(`EXECUTE live_query_${l}_diff${f};`), f = f === 1 ? 2 : 1, await g.exec(`
|
|
864
867
|
TRUNCATE live_query_${l}_state${f};
|
|
865
868
|
`);
|
|
866
869
|
});
|
|
867
870
|
break;
|
|
868
|
-
} catch (
|
|
869
|
-
if (
|
|
871
|
+
} catch (g) {
|
|
872
|
+
if (g.message === `relation "live_query_${l}_state${f}" does not exist`) {
|
|
870
873
|
E = !0, await S();
|
|
871
874
|
continue;
|
|
872
|
-
} else throw
|
|
875
|
+
} else throw g;
|
|
873
876
|
}
|
|
874
|
-
Pr(u, [...E ? [{ __op__: "RESET" }] : [], ...
|
|
875
|
-
}),
|
|
876
|
-
if (
|
|
877
|
+
Pr(u, [...E ? [{ __op__: "RESET" }] : [], ...p.rows]);
|
|
878
|
+
}), A = (E) => {
|
|
879
|
+
if (d) throw new Error("Live query is no longer active and cannot be subscribed to");
|
|
877
880
|
u.push(E);
|
|
878
881
|
}, b = async (E) => {
|
|
879
|
-
E ? u = u.filter((m) => m !== m) : u = [], u.length === 0 && !
|
|
880
|
-
await Promise.all(
|
|
882
|
+
E ? u = u.filter((m) => m !== m) : u = [], u.length === 0 && !d && (d = !0, await e.transaction(async (m) => {
|
|
883
|
+
await Promise.all(_.map((g) => g(m))), await m.exec(`
|
|
881
884
|
DROP VIEW IF EXISTS live_query_${l}_view;
|
|
882
885
|
DROP TABLE IF EXISTS live_query_${l}_state1;
|
|
883
886
|
DROP TABLE IF EXISTS live_query_${l}_state2;
|
|
@@ -888,55 +891,55 @@ var Cr = 5, Mr = async (e, t) => {
|
|
|
888
891
|
};
|
|
889
892
|
return c?.aborted ? await b() : c?.addEventListener("abort", () => {
|
|
890
893
|
b();
|
|
891
|
-
}, { once: !0 }), await
|
|
894
|
+
}, { once: !0 }), await $(), { fields: p.fields.filter((E) => !["__after__", "__op__", "__changed_columns__"].includes(E.name)), initialChanges: p.rows, subscribe: A, unsubscribe: b, refresh: $ };
|
|
892
895
|
}, async incrementalQuery(s, a, i, o) {
|
|
893
896
|
let c;
|
|
894
897
|
if (typeof s != "string" && (c = s.signal, a = s.params, i = s.key, o = s.callback, s = s.query), !i) throw new Error("key is required for incremental queries");
|
|
895
|
-
let u = o ? [o] : [], l = /* @__PURE__ */ new Map(),
|
|
898
|
+
let u = o ? [o] : [], l = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map(), f = [], p = !0, { fields: _, unsubscribe: S, refresh: $ } = await r.changes(s, a, i, (E) => {
|
|
896
899
|
for (let R of E) {
|
|
897
900
|
let { __op__: I, __changed_columns__: y, ...T } = R;
|
|
898
901
|
switch (I) {
|
|
899
902
|
case "RESET":
|
|
900
|
-
l.clear(),
|
|
903
|
+
l.clear(), d.clear();
|
|
901
904
|
break;
|
|
902
905
|
case "INSERT":
|
|
903
|
-
l.set(T[i], T),
|
|
906
|
+
l.set(T[i], T), d.set(T.__after__, T[i]);
|
|
904
907
|
break;
|
|
905
908
|
case "DELETE": {
|
|
906
909
|
let k = l.get(T[i]);
|
|
907
|
-
l.delete(T[i]), k.__after__ !== null &&
|
|
910
|
+
l.delete(T[i]), k.__after__ !== null && d.delete(k.__after__);
|
|
908
911
|
break;
|
|
909
912
|
}
|
|
910
913
|
case "UPDATE": {
|
|
911
914
|
let k = { ...l.get(T[i]) ?? {} };
|
|
912
|
-
for (let Q of y) k[Q] = T[Q], Q === "__after__" &&
|
|
915
|
+
for (let Q of y) k[Q] = T[Q], Q === "__after__" && d.set(T.__after__, T[i]);
|
|
913
916
|
l.set(T[i], k);
|
|
914
917
|
break;
|
|
915
918
|
}
|
|
916
919
|
}
|
|
917
920
|
}
|
|
918
|
-
let m = [],
|
|
921
|
+
let m = [], g = null;
|
|
919
922
|
for (let R = 0; R < l.size; R++) {
|
|
920
|
-
let I =
|
|
923
|
+
let I = d.get(g), y = l.get(I);
|
|
921
924
|
if (!y) break;
|
|
922
925
|
let T = { ...y };
|
|
923
|
-
delete T.__after__, m.push(T),
|
|
926
|
+
delete T.__after__, m.push(T), g = I;
|
|
924
927
|
}
|
|
925
|
-
f = m,
|
|
928
|
+
f = m, p || se(u, { rows: m, fields: _ });
|
|
926
929
|
});
|
|
927
|
-
|
|
928
|
-
let
|
|
930
|
+
p = !1, se(u, { rows: f, fields: _ });
|
|
931
|
+
let A = (E) => {
|
|
929
932
|
u.push(E);
|
|
930
933
|
}, b = async (E) => {
|
|
931
934
|
E ? u = u.filter((m) => m !== m) : u = [], u.length === 0 && await S();
|
|
932
935
|
};
|
|
933
936
|
return c?.aborted ? await b() : c?.addEventListener("abort", () => {
|
|
934
937
|
b();
|
|
935
|
-
}, { once: !0 }), { initialResults: { rows: f, fields:
|
|
938
|
+
}, { once: !0 }), { initialResults: { rows: f, fields: _ }, subscribe: A, unsubscribe: b, refresh: $ };
|
|
936
939
|
} };
|
|
937
940
|
return { namespaceObj: r };
|
|
938
941
|
}, qr = { name: "Live Queries", setup: Mr };
|
|
939
|
-
async function
|
|
942
|
+
async function $t(e, t) {
|
|
940
943
|
return (await e.query(`
|
|
941
944
|
WITH RECURSIVE view_dependencies AS (
|
|
942
945
|
-- Base case: Get the initial view's dependencies
|
|
@@ -983,7 +986,7 @@ async function At(e, t) {
|
|
|
983
986
|
WHERE NOT is_view; -- Exclude intermediate views
|
|
984
987
|
`, [t])).rows.map((n) => ({ table_name: n.table_name, schema_name: n.schema_name, table_oid: n.table_oid, schema_oid: n.schema_oid }));
|
|
985
988
|
}
|
|
986
|
-
async function
|
|
989
|
+
async function At(e, t, n) {
|
|
987
990
|
let r = t.filter((s) => !n.has(`${s.schema_oid}_${s.table_oid}`)).map((s) => `
|
|
988
991
|
CREATE OR REPLACE FUNCTION "_notify_${s.schema_oid}_${s.table_oid}"() RETURNS TRIGGER AS $$
|
|
989
992
|
BEGIN
|
|
@@ -1099,12 +1102,12 @@ const Sn = (e, t) => {
|
|
|
1099
1102
|
...e.foreignKeyColumnNames || e.foreignKeyNames
|
|
1100
1103
|
], o = [];
|
|
1101
1104
|
t.forEach((u) => {
|
|
1102
|
-
const l =
|
|
1105
|
+
const l = Lt(e, u);
|
|
1103
1106
|
s && (l.createdBy = n.userId), a && (l.updatedBy = n.userId);
|
|
1104
|
-
const
|
|
1105
|
-
i.forEach((
|
|
1106
|
-
const
|
|
1107
|
-
f.push(Je(
|
|
1107
|
+
const d = Ke(e, l), f = [];
|
|
1108
|
+
i.forEach((p) => {
|
|
1109
|
+
const _ = d[p];
|
|
1110
|
+
f.push(Je(_));
|
|
1108
1111
|
}), o.push(`(${f.join(",")})`);
|
|
1109
1112
|
});
|
|
1110
1113
|
const c = i.map((u) => `"${u}"`).join(",");
|
|
@@ -1161,7 +1164,7 @@ const Sn = (e, t) => {
|
|
|
1161
1164
|
break;
|
|
1162
1165
|
}
|
|
1163
1166
|
}
|
|
1164
|
-
},
|
|
1167
|
+
}, Ce = (e, t) => {
|
|
1165
1168
|
const { tableName: n } = t, [r, s] = Ur(n), a = e.rxdb.schemaManager.getEntityTypeByTableName(s, r);
|
|
1166
1169
|
if (!a) return;
|
|
1167
1170
|
const i = e.getRepository(a), o = e.rxdb.schemaManager.getEntityMetadataByTableName(s, r);
|
|
@@ -1174,7 +1177,7 @@ const Sn = (e, t) => {
|
|
|
1174
1177
|
if (!(!u || u.length === 0))
|
|
1175
1178
|
if (a === ie) {
|
|
1176
1179
|
if (xr(e, c), t.type === K.INSERT) {
|
|
1177
|
-
const l = D(ie),
|
|
1180
|
+
const l = D(ie), d = c.map((f) => ({
|
|
1178
1181
|
namespace: l.namespace,
|
|
1179
1182
|
entity: l.name,
|
|
1180
1183
|
type: f.type,
|
|
@@ -1183,11 +1186,11 @@ const Sn = (e, t) => {
|
|
|
1183
1186
|
inversePatch: null,
|
|
1184
1187
|
recordAt: f.createdAt || /* @__PURE__ */ new Date()
|
|
1185
1188
|
}));
|
|
1186
|
-
e.rxdb.dispatchEvent(new oe(
|
|
1189
|
+
e.rxdb.dispatchEvent(new oe(d));
|
|
1187
1190
|
}
|
|
1188
1191
|
} else {
|
|
1189
|
-
const l = jr(t.type),
|
|
1190
|
-
const
|
|
1192
|
+
const l = jr(t.type), d = u.map((f) => {
|
|
1193
|
+
const p = {
|
|
1191
1194
|
namespace: o.namespace,
|
|
1192
1195
|
entity: o.name,
|
|
1193
1196
|
type: l,
|
|
@@ -1199,17 +1202,17 @@ const Sn = (e, t) => {
|
|
|
1199
1202
|
switch (t.type) {
|
|
1200
1203
|
case K.INSERT:
|
|
1201
1204
|
return {
|
|
1202
|
-
...
|
|
1205
|
+
...p,
|
|
1203
1206
|
patch: { ...f }
|
|
1204
1207
|
};
|
|
1205
1208
|
case K.DELETE:
|
|
1206
1209
|
return {
|
|
1207
|
-
...
|
|
1210
|
+
...p,
|
|
1208
1211
|
inversePatch: { ...f }
|
|
1209
1212
|
};
|
|
1210
1213
|
case K.UPDATE:
|
|
1211
1214
|
return {
|
|
1212
|
-
...
|
|
1215
|
+
...p,
|
|
1213
1216
|
// 注意:这里无法获取变更前的数据,只能把整个 entity 作为 patch
|
|
1214
1217
|
// 这是 UPDATE hook 的限制,完整的 patch/inversePatch 应该从 RxDBChange 表获取
|
|
1215
1218
|
inversePatch: null,
|
|
@@ -1220,17 +1223,17 @@ const Sn = (e, t) => {
|
|
|
1220
1223
|
switch (l) {
|
|
1221
1224
|
case "UPDATE":
|
|
1222
1225
|
e.rxdb.dispatchEvent(
|
|
1223
|
-
new me(
|
|
1226
|
+
new me(d)
|
|
1224
1227
|
);
|
|
1225
1228
|
break;
|
|
1226
1229
|
case "DELETE":
|
|
1227
1230
|
e.rxdb.dispatchEvent(
|
|
1228
|
-
new Ve(
|
|
1231
|
+
new Ve(d)
|
|
1229
1232
|
);
|
|
1230
1233
|
break;
|
|
1231
1234
|
case "INSERT":
|
|
1232
1235
|
e.rxdb.dispatchEvent(
|
|
1233
|
-
new oe(
|
|
1236
|
+
new oe(d)
|
|
1234
1237
|
);
|
|
1235
1238
|
break;
|
|
1236
1239
|
}
|
|
@@ -1245,30 +1248,30 @@ const Sn = (e, t) => {
|
|
|
1245
1248
|
params: r
|
|
1246
1249
|
};
|
|
1247
1250
|
}, In = (e, t, n) => {
|
|
1248
|
-
const r = P(e), s =
|
|
1251
|
+
const r = P(e), s = Lt(e, t);
|
|
1249
1252
|
n?.userId && (e.propertyMap.has("createdBy") && (s.createdBy = n.userId), e.propertyMap.has("updatedBy") && (s.updatedBy = n.userId));
|
|
1250
1253
|
const a = {};
|
|
1251
1254
|
Object.keys(s).forEach((f) => {
|
|
1252
|
-
const
|
|
1253
|
-
|
|
1255
|
+
const p = s[f];
|
|
1256
|
+
p != null && (a[f] = p);
|
|
1254
1257
|
});
|
|
1255
|
-
const i = Object.keys(a), o = Ke(e, a), c = [], u = i.map((f,
|
|
1256
|
-
let
|
|
1257
|
-
return n?.returning !== !1 ?
|
|
1258
|
+
const i = Object.keys(a), o = Ke(e, a), c = [], u = i.map((f, p) => (c.push(o[f]), `$${p + 1}`)), l = i.map((f) => `"${f}"`).join(",");
|
|
1259
|
+
let d = `INSERT INTO ${r} (${l}) VALUES (${u.join(",")})`;
|
|
1260
|
+
return n?.returning !== !1 ? d += " RETURNING *;" : d += ";", { sql: d, params: c };
|
|
1258
1261
|
}, ze = (e, t, n, r) => {
|
|
1259
1262
|
const s = zn(e, n);
|
|
1260
1263
|
e.propertyMap.has("updatedAt") && (s.updatedAt = r?.updatedAt ?? /* @__PURE__ */ new Date()), r?.userId && e.propertyMap.has("updatedBy") && (s.updatedBy = r.userId);
|
|
1261
|
-
const a = Ke(e, s), o = Object.keys(s).map((f,
|
|
1262
|
-
let
|
|
1264
|
+
const a = Ke(e, s), o = Object.keys(s).map((f, p) => `"${f}" = $${p + 1}`).join(","), c = [...Object.values(a)], u = P(e), l = Array.isArray(t) ? t : [t];
|
|
1265
|
+
let d = `UPDATE ${u} SET ${o} WHERE id `;
|
|
1263
1266
|
if (Array.isArray(t)) {
|
|
1264
1267
|
const f = c.length + 1;
|
|
1265
|
-
|
|
1268
|
+
d += `= ANY($${f})`, c.push(l.map((p) => p.id));
|
|
1266
1269
|
} else {
|
|
1267
1270
|
const f = c.length + 1;
|
|
1268
|
-
|
|
1271
|
+
d += `= $${f}`, c.push(t.id);
|
|
1269
1272
|
}
|
|
1270
|
-
return r?.returning !== !1 ?
|
|
1271
|
-
sql:
|
|
1273
|
+
return r?.returning !== !1 ? d += " RETURNING *;" : d += ";", {
|
|
1274
|
+
sql: d,
|
|
1272
1275
|
params: c
|
|
1273
1276
|
};
|
|
1274
1277
|
}, Ze = "_", On = (e) => e.replaceAll(".", "_"), he = (e, t) => {
|
|
@@ -1291,12 +1294,12 @@ const Sn = (e, t) => {
|
|
|
1291
1294
|
if (i.length === 0) continue;
|
|
1292
1295
|
const { metaWalker: o, relPairs: c } = Fr(e, n, s, a);
|
|
1293
1296
|
if (!o || c.length === 0) continue;
|
|
1294
|
-
const u = i[0], l = i.slice(1).join("."),
|
|
1295
|
-
if (
|
|
1297
|
+
const u = i[0], l = i.slice(1).join("."), d = o.propertyMap.get(u);
|
|
1298
|
+
if (d && d.type === N.keyValue) {
|
|
1296
1299
|
const f = s.slice(0, a).join(".");
|
|
1297
1300
|
vn(e, t, c, f);
|
|
1298
|
-
const
|
|
1299
|
-
return t.fieldAliasMap.set(r, `"${S}"."${
|
|
1301
|
+
const p = c[c.length - 1].relation, _ = de(c, f, p), S = he(t, _), $ = l ? `$.${l}` : "$";
|
|
1302
|
+
return t.fieldAliasMap.set(r, `"${S}"."${d.columnName}" ->> '${$.replace("$.", "")}'`), !0;
|
|
1300
1303
|
}
|
|
1301
1304
|
}
|
|
1302
1305
|
return !1;
|
|
@@ -1305,14 +1308,14 @@ const Sn = (e, t) => {
|
|
|
1305
1308
|
let o = e.rxdb.schemaManager.findMappedRelation(s, a);
|
|
1306
1309
|
if (!o)
|
|
1307
1310
|
if (a.kind === F.ONE_TO_MANY || a.kind === F.MANY_TO_MANY) {
|
|
1308
|
-
const
|
|
1309
|
-
|
|
1311
|
+
const d = s.relationMap.get(a.mappedProperty);
|
|
1312
|
+
d && (o = { metadata: s, relation: d });
|
|
1310
1313
|
} else (a.kind === F.MANY_TO_ONE || a.kind === F.ONE_TO_ONE) && (o = { metadata: s, relation: a });
|
|
1311
1314
|
if (!o) throw new x("mappedRelation not found");
|
|
1312
1315
|
const c = de(n, r, a), u = he(t, c);
|
|
1313
1316
|
let l = Ze;
|
|
1314
1317
|
if (i > 0) {
|
|
1315
|
-
const
|
|
1318
|
+
const d = n[i - 1], f = de(n, r, d.relation);
|
|
1316
1319
|
l = he(t, f);
|
|
1317
1320
|
}
|
|
1318
1321
|
switch (a.kind) {
|
|
@@ -1360,22 +1363,22 @@ const Sn = (e, t) => {
|
|
|
1360
1363
|
}, Hr = (e, t, n, r, s) => {
|
|
1361
1364
|
const a = D(s.junctionEntityType), i = fe(e, a), o = Dn(e, On(`${s.name}_m_n`)), c = `"${o}"."${t.relation.columnName}" = ${et(r)}."id"`;
|
|
1362
1365
|
i.find(
|
|
1363
|
-
(
|
|
1366
|
+
(p) => p.joinTableName === o && p.on === c
|
|
1364
1367
|
) || i.push({
|
|
1365
1368
|
joinTableName: o,
|
|
1366
1369
|
on: c
|
|
1367
1370
|
});
|
|
1368
|
-
const l = fe(e, t.metadata),
|
|
1369
|
-
l.find((
|
|
1371
|
+
const l = fe(e, t.metadata), d = `"${n}"."id" = "${o}"."${s.columnName}"`;
|
|
1372
|
+
l.find((p) => p.joinTableName === n && p.on === d) || l.push({
|
|
1370
1373
|
joinTableName: n,
|
|
1371
|
-
on:
|
|
1374
|
+
on: d
|
|
1372
1375
|
});
|
|
1373
1376
|
}, fe = (e, t) => (e.joinMap.has(t) || e.joinMap.set(t, []), e.joinMap.get(t)), Dn = (e, t) => {
|
|
1374
1377
|
let n = t, r = 1;
|
|
1375
1378
|
for (; e.usedAliases.has(n); )
|
|
1376
1379
|
n = `${t}_${r++}`;
|
|
1377
1380
|
return e.usedAliases.add(n), n;
|
|
1378
|
-
}, de = (e, t, n) => e.length === 1 ? n.name : `${t}_${n.name}`, et = (e) => e === Ze ? e : `"${e}"`,
|
|
1381
|
+
}, de = (e, t, n) => e.length === 1 ? n.name : `${t}_${n.name}`, et = (e) => e === Ze ? e : `"${e}"`, Cn = (e, t, n) => {
|
|
1379
1382
|
const r = {
|
|
1380
1383
|
joinMap: /* @__PURE__ */ new Map(),
|
|
1381
1384
|
usedAliases: /* @__PURE__ */ new Set(),
|
|
@@ -1403,13 +1406,13 @@ const Sn = (e, t) => {
|
|
|
1403
1406
|
}
|
|
1404
1407
|
const l = r.replace(`.${u.propertyName}`, "");
|
|
1405
1408
|
vn(e, t, u.relations, l);
|
|
1406
|
-
const
|
|
1407
|
-
t.fieldAliasMap.set(r, `"${
|
|
1409
|
+
const d = u.relations[u.relations.length - 1], f = de(u.relations, l, d.relation), p = he(t, f);
|
|
1410
|
+
t.fieldAliasMap.set(r, `"${p}"."${u.property.columnName}"`);
|
|
1408
1411
|
return;
|
|
1409
1412
|
} catch {
|
|
1410
1413
|
}
|
|
1411
1414
|
const [i, ...o] = a, c = n.propertyMap.get(i);
|
|
1412
|
-
if (c && c.type ===
|
|
1415
|
+
if (c && c.type === N.keyValue && o.length > 0) {
|
|
1413
1416
|
const u = o.join(".");
|
|
1414
1417
|
t.fieldAliasMap.set(r, `"${Ze}"."${c.columnName}" ->> '${u}'`);
|
|
1415
1418
|
return;
|
|
@@ -1435,10 +1438,10 @@ const Sn = (e, t) => {
|
|
|
1435
1438
|
}, Jr = (e, t) => {
|
|
1436
1439
|
if (e?.length)
|
|
1437
1440
|
return e.map((n) => {
|
|
1438
|
-
const r =
|
|
1441
|
+
const r = Ln(n.field, t);
|
|
1439
1442
|
return `${pe}."${r}" ${n.sort.toUpperCase()}`;
|
|
1440
1443
|
}).join(", ");
|
|
1441
|
-
}, Yr = (e) => Kr[e] || e, Qr = (e) => e && typeof e == "object" && "rules" in e && Array.isArray(e.rules),
|
|
1444
|
+
}, Yr = (e) => Kr[e] || e, Qr = (e) => e && typeof e == "object" && "rules" in e && Array.isArray(e.rules), Ln = (e, t) => {
|
|
1442
1445
|
if (t && !e.includes(".")) {
|
|
1443
1446
|
const n = t.propertyMap.get(e);
|
|
1444
1447
|
if (n)
|
|
@@ -1451,11 +1454,11 @@ const Sn = (e, t) => {
|
|
|
1451
1454
|
}, Xr = (e, t, n) => {
|
|
1452
1455
|
if (t) return t;
|
|
1453
1456
|
if (!e.includes(".")) {
|
|
1454
|
-
const o =
|
|
1457
|
+
const o = Ln(e, n);
|
|
1455
1458
|
return o.toLowerCase() !== o ? `"${o}"` : o;
|
|
1456
1459
|
}
|
|
1457
1460
|
const r = e.split("."), s = r[0], a = n?.propertyMap.get(s);
|
|
1458
|
-
if (a && (a.type ===
|
|
1461
|
+
if (a && (a.type === N.json || a.type === N.keyValue) && r.length === 2)
|
|
1459
1462
|
return `"${a.columnName}" ->> '${r[1]}'`;
|
|
1460
1463
|
const i = e.lastIndexOf(".");
|
|
1461
1464
|
return `"${e.slice(0, i)}".${e.slice(i + 1)}`;
|
|
@@ -1468,13 +1471,13 @@ const Sn = (e, t) => {
|
|
|
1468
1471
|
let u = s;
|
|
1469
1472
|
a && s.includes(".") && (u = s.split(".").pop() || s);
|
|
1470
1473
|
const l = r?.propertyMap.get(u.split(".")[0]);
|
|
1471
|
-
if (l && (l.type ===
|
|
1474
|
+
if (l && (l.type === N.json || l.type === N.keyValue)) {
|
|
1472
1475
|
if (c === "contains" && typeof o == "object" && !Array.isArray(o))
|
|
1473
1476
|
return t.push(JSON.stringify(o)), `${i} @> $${t.length}::jsonb`;
|
|
1474
1477
|
if (c === "notContains" && typeof o == "object" && !Array.isArray(o))
|
|
1475
1478
|
return t.push(JSON.stringify(o)), `NOT (${i} @> $${t.length}::jsonb)`;
|
|
1476
1479
|
}
|
|
1477
|
-
if (l && (l.type ===
|
|
1480
|
+
if (l && (l.type === N.stringArray || l.type === N.numberArray) && (c === "in" || c === "notIn")) {
|
|
1478
1481
|
if (!Array.isArray(o)) return "";
|
|
1479
1482
|
t.push(JSON.stringify(o));
|
|
1480
1483
|
const f = `${i} @> $${t.length}::jsonb`;
|
|
@@ -1487,11 +1490,11 @@ const Sn = (e, t) => {
|
|
|
1487
1490
|
if (["contains", "notContains", "startsWith", "notStartsWith", "endsWith", "notEndsWith"].includes(c)) {
|
|
1488
1491
|
let f = "";
|
|
1489
1492
|
c === "contains" || c === "notContains" ? f = `%${o}%` : c === "startsWith" || c === "notStartsWith" ? f = `${o}%` : (c === "endsWith" || c === "notEndsWith") && (f = `%${o}`), t.push(f);
|
|
1490
|
-
const
|
|
1491
|
-
return `${i} ${
|
|
1493
|
+
const p = c.startsWith("not") ? "NOT LIKE" : "LIKE";
|
|
1494
|
+
return `${i} ${p} $${t.length}`;
|
|
1492
1495
|
}
|
|
1493
|
-
const
|
|
1494
|
-
return t.push(o), l && l.type ===
|
|
1496
|
+
const d = Yr(c);
|
|
1497
|
+
return t.push(o), l && l.type === N.uuid ? `${i}::uuid ${d} $${t.length}::uuid` : `${i} ${d} $${t.length}`;
|
|
1495
1498
|
}, we = (e, t, n = /* @__PURE__ */ new Map(), r) => {
|
|
1496
1499
|
const s = e.rules.map(
|
|
1497
1500
|
(i) => Qr(i) ? we(i, t, n, r) : zr(i, t, n, r)
|
|
@@ -1507,7 +1510,7 @@ const Sn = (e, t) => {
|
|
|
1507
1510
|
let t = `SELECT COUNT(*) as count FROM ${e.tableName} AS ${pe}`;
|
|
1508
1511
|
return e.join && (t += ` ${e.join}`), e.where && (t += ` WHERE ${e.where}`), t;
|
|
1509
1512
|
}, Ee = (e, t, n) => {
|
|
1510
|
-
const r = P(t), s = [], { joinSQL: a, fieldAliasMap: i } = n.where ?
|
|
1513
|
+
const r = P(t), s = [], { joinSQL: a, fieldAliasMap: i } = n.where ? Cn(e, t, n.where) : { joinSQL: "", fieldAliasMap: /* @__PURE__ */ new Map() }, o = n.where ? we(n.where, s, i, t) : void 0, c = Jr(n.orderBy, t), u = "limit" in n ? n.limit : void 0, l = "offset" in n ? n.offset : void 0;
|
|
1511
1514
|
return { sql: Zr(
|
|
1512
1515
|
{
|
|
1513
1516
|
tableName: r,
|
|
@@ -1522,7 +1525,7 @@ const Sn = (e, t) => {
|
|
|
1522
1525
|
}, ta = (e, t, n) => {
|
|
1523
1526
|
if (n.groupBy)
|
|
1524
1527
|
throw new x("groupBy not supported in count queries");
|
|
1525
|
-
const r = P(t), s = [], { joinSQL: a, fieldAliasMap: i } = n.where ?
|
|
1528
|
+
const r = P(t), s = [], { joinSQL: a, fieldAliasMap: i } = n.where ? Cn(e, t, n.where) : { joinSQL: "", fieldAliasMap: /* @__PURE__ */ new Map() }, o = n.where ? we(n.where, s, i, t) : void 0;
|
|
1526
1529
|
return { sql: ea({
|
|
1527
1530
|
tableName: r,
|
|
1528
1531
|
where: o,
|
|
@@ -1605,25 +1608,25 @@ class Mn extends sa {
|
|
|
1605
1608
|
const Ne = (e, t, n) => {
|
|
1606
1609
|
const { isCount: r, isFindDescendants: s, entityId: a, where: i } = n, o = !a, c = P(t);
|
|
1607
1610
|
let u = "";
|
|
1608
|
-
const
|
|
1611
|
+
const d = [`c.level < ${n.level || 0}`], f = [];
|
|
1609
1612
|
if (o || f.push(a), i) {
|
|
1610
|
-
const
|
|
1613
|
+
const A = /* @__PURE__ */ new Map(), b = (m) => {
|
|
1611
1614
|
if ("rules" in m)
|
|
1612
1615
|
return { ...m, rules: m.rules.map(b) };
|
|
1613
1616
|
if (m.field && typeof m.field == "string" && m.field.startsWith("children.")) {
|
|
1614
|
-
const
|
|
1615
|
-
|
|
1617
|
+
const g = m.field.substring(9), I = t.propertyMap.get(g)?.columnName ?? g;
|
|
1618
|
+
A.set(m.field, `children."${I}"`);
|
|
1616
1619
|
}
|
|
1617
1620
|
return m;
|
|
1618
1621
|
};
|
|
1619
1622
|
b(i);
|
|
1620
|
-
const E = we(i, f,
|
|
1621
|
-
E &&
|
|
1623
|
+
const E = we(i, f, A, t);
|
|
1624
|
+
E && d.push(E);
|
|
1622
1625
|
}
|
|
1623
|
-
const
|
|
1624
|
-
|
|
1625
|
-
let
|
|
1626
|
-
return r && (o ?
|
|
1626
|
+
const p = d.filter(Boolean).join(" AND ");
|
|
1627
|
+
p && (u = `WHERE ${p}`);
|
|
1628
|
+
let _ = "*";
|
|
1629
|
+
return r && (o ? _ = "count(*) AS count" : _ = "(count(*) - 1) AS count"), {
|
|
1627
1630
|
sql: `WITH RECURSIVE __children AS (
|
|
1628
1631
|
SELECT *, 0 AS level
|
|
1629
1632
|
FROM ${c}
|
|
@@ -1635,7 +1638,7 @@ const Ne = (e, t, n) => {
|
|
|
1635
1638
|
${u}
|
|
1636
1639
|
)
|
|
1637
1640
|
|
|
1638
|
-
SELECT ${
|
|
1641
|
+
SELECT ${_} FROM __children${r ? "" : " ORDER BY level, id"};`,
|
|
1639
1642
|
params: f
|
|
1640
1643
|
};
|
|
1641
1644
|
}, ra = (e, t, n) => Ne(e, t, { ...n, isFindDescendants: !0 }), aa = (e, t, n) => Ne(e, t, { ...n, isFindDescendants: !0, isCount: !0 }), ia = (e, t, n) => Ne(e, t, { ...n, isFindDescendants: !1 }), oa = (e, t, n) => Ne(e, t, { ...n, isFindDescendants: !1, isCount: !0 });
|
|
@@ -1667,18 +1670,18 @@ const la = async (e, t) => {
|
|
|
1667
1670
|
const n = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Date();
|
|
1668
1671
|
for (const [s, a] of t.create.entries()) {
|
|
1669
1672
|
const i = Array.from(a);
|
|
1670
|
-
i.forEach((
|
|
1673
|
+
i.forEach((_) => n.add(_));
|
|
1671
1674
|
const o = D(s), c = ue(o, i, e.rxdb.context);
|
|
1672
1675
|
await e.query(c);
|
|
1673
|
-
const u = i.map((
|
|
1676
|
+
const u = i.map((_) => _.id), { sql: l, params: d } = Ee(e, o, {
|
|
1674
1677
|
where: { combinator: "and", rules: [{ field: "id", operator: "in", value: u }] }
|
|
1675
|
-
}), f = await e.query(l,
|
|
1676
|
-
for (const
|
|
1677
|
-
const S = Z(o,
|
|
1678
|
-
if (
|
|
1679
|
-
const
|
|
1680
|
-
|
|
1681
|
-
const b = J(
|
|
1678
|
+
}), f = await e.query(l, d), p = e.getRepository(s);
|
|
1679
|
+
for (const _ of f.rows) {
|
|
1680
|
+
const S = Z(o, _), $ = S.id;
|
|
1681
|
+
if (p.hasEntityRef($)) {
|
|
1682
|
+
const A = p.getEntityRef($);
|
|
1683
|
+
p.updateEntity(A, S);
|
|
1684
|
+
const b = J(A);
|
|
1682
1685
|
b.local = !0, b.modified = !1;
|
|
1683
1686
|
}
|
|
1684
1687
|
}
|
|
@@ -1687,27 +1690,27 @@ const la = async (e, t) => {
|
|
|
1687
1690
|
const i = Array.from(a);
|
|
1688
1691
|
i.forEach((l) => n.add(l));
|
|
1689
1692
|
const o = D(s), c = Object.groupBy(i, (l) => {
|
|
1690
|
-
const
|
|
1691
|
-
return Ot(Object.keys(
|
|
1693
|
+
const d = J(l);
|
|
1694
|
+
return Ot(Object.keys(d.patch || {}).sort());
|
|
1692
1695
|
}), u = e.getRepository(s);
|
|
1693
1696
|
for (const l of Object.values(c)) {
|
|
1694
1697
|
if (!l || l.length === 0) continue;
|
|
1695
|
-
const
|
|
1698
|
+
const d = l[0], f = J(d), { sql: p, params: _ } = ze(o, l, f.patch || {}, {
|
|
1696
1699
|
...e.rxdb.context,
|
|
1697
1700
|
returning: !1,
|
|
1698
1701
|
// 后续用 SELECT 获取
|
|
1699
1702
|
updatedAt: r
|
|
1700
1703
|
});
|
|
1701
|
-
await e.query(
|
|
1702
|
-
const S = l.map((b) => b.id),
|
|
1704
|
+
await e.query(p, _);
|
|
1705
|
+
const S = l.map((b) => b.id), $ = Ee(e, o, {
|
|
1703
1706
|
where: { combinator: "and", rules: [{ field: "id", operator: "in", value: S }] }
|
|
1704
|
-
}),
|
|
1705
|
-
for (const b of
|
|
1707
|
+
}), A = await e.query($.sql, $.params);
|
|
1708
|
+
for (const b of A.rows) {
|
|
1706
1709
|
const E = Z(o, b), m = E.id;
|
|
1707
1710
|
if (u.hasEntityRef(m)) {
|
|
1708
|
-
const
|
|
1709
|
-
u.updateEntity(
|
|
1710
|
-
const R = J(
|
|
1711
|
+
const g = u.getEntityRef(m);
|
|
1712
|
+
u.updateEntity(g, E);
|
|
1713
|
+
const R = J(g);
|
|
1711
1714
|
R.local = !0, R.modified = !1;
|
|
1712
1715
|
}
|
|
1713
1716
|
}
|
|
@@ -1734,13 +1737,20 @@ const la = async (e, t) => {
|
|
|
1734
1737
|
let u = "";
|
|
1735
1738
|
u += `"${c.columnName}"`;
|
|
1736
1739
|
const l = Dt(c);
|
|
1737
|
-
if (c.primary ? c.type ===
|
|
1738
|
-
let
|
|
1739
|
-
It(c.default) && (
|
|
1740
|
+
if (c.primary ? c.type === N.integer ? u += " serial PRIMARY KEY" : u += ` ${l} PRIMARY KEY` : u += ` ${l}`, Reflect.get(c, "default") !== void 0) {
|
|
1741
|
+
let d = c.default;
|
|
1742
|
+
It(c.default) && (d = c.default()), Hn(d) ? ["CURRENT_TIMESTAMP", "now()"].includes(d) ? u += " DEFAULT now()" : u += ` DEFAULT '${d}'` : d instanceof Date ? u += ` DEFAULT '${d.toISOString()}'` : typeof d == "boolean" || typeof d == "number" ? u += ` DEFAULT ${String(d)}` : u += ` DEFAULT '${String(d)}'`;
|
|
1743
|
+
}
|
|
1744
|
+
if (c.nullable || (u += " NOT NULL"), c.unique && s.push(
|
|
1745
|
+
`CREATE UNIQUE INDEX "${Ct(t, c)}" on ${n} ("${c.columnName}" ${Xn(c)});`
|
|
1746
|
+
), c.type === N.enum) {
|
|
1747
|
+
const d = c;
|
|
1748
|
+
if (d.enum && d.enum.length > 0) {
|
|
1749
|
+
const f = d.enum.map((p) => `'${String(p).replace(/'/g, "''")}'`).join(", ");
|
|
1750
|
+
a.push(`CHECK ("${c.columnName}" IN (${f}))`);
|
|
1751
|
+
}
|
|
1740
1752
|
}
|
|
1741
|
-
|
|
1742
|
-
`CREATE UNIQUE INDEX "${Lt(t, c)}" on ${n} ("${c.columnName}" ${Xn(c)});`
|
|
1743
|
-
), i.push(u);
|
|
1753
|
+
i.push(u);
|
|
1744
1754
|
});
|
|
1745
1755
|
const o = [...i, ...a];
|
|
1746
1756
|
if (o.length)
|
|
@@ -1804,7 +1814,7 @@ ALTER TABLE ${r} ADD COLUMN "${a}" ${i}`, s.nullable || (n += " NOT NULL"), s.ki
|
|
|
1804
1814
|
ALTER TABLE ${r} ADD CONSTRAINT "${l}" FOREIGN KEY ("${a}") REFERENCES ${u}(id)`, s.onDelete ? n += ` ON DELETE ${s.onDelete}` : s.kind === F.MANY_TO_ONE && (s.nullable ? n += " ON DELETE SET NULL" : n += " ON DELETE CASCADE"), n += " DEFERRABLE INITIALLY DEFERRED", n += ";";
|
|
1805
1815
|
}
|
|
1806
1816
|
(s.unique || s.kind === F.ONE_TO_ONE) && (n += `
|
|
1807
|
-
CREATE UNIQUE INDEX "${
|
|
1817
|
+
CREATE UNIQUE INDEX "${Ct(t, s)}" ON ${r}("${a}");`);
|
|
1808
1818
|
}
|
|
1809
1819
|
break;
|
|
1810
1820
|
}
|
|
@@ -1889,7 +1899,7 @@ function pa(e = ["RxDBChange", "RxDBBranch", "RxDBMigration"]) {
|
|
|
1889
1899
|
|
|
1890
1900
|
`);
|
|
1891
1901
|
}
|
|
1892
|
-
function
|
|
1902
|
+
function $e(e, t = {}) {
|
|
1893
1903
|
const n = P(e), r = D(ie), s = P(r), {
|
|
1894
1904
|
propertyMap: a,
|
|
1895
1905
|
name: i,
|
|
@@ -1897,13 +1907,13 @@ function Ae(e, t = {}) {
|
|
|
1897
1907
|
foreignKeyNames: c,
|
|
1898
1908
|
foreignKeyColumnNames: u,
|
|
1899
1909
|
namespace: l
|
|
1900
|
-
} = e, { branchId:
|
|
1910
|
+
} = e, { branchId: d, transactionId: f } = t, p = `"${o}_change_trigger"`, _ = [];
|
|
1901
1911
|
for (const [y, T] of a)
|
|
1902
|
-
y !== "id" &&
|
|
1912
|
+
y !== "id" && _.push({ jsName: y, dbColumn: T.columnName });
|
|
1903
1913
|
const S = u || c;
|
|
1904
1914
|
for (let y = 0; y < c.length; y++)
|
|
1905
|
-
c[y] !== "id" &&
|
|
1906
|
-
const
|
|
1915
|
+
c[y] !== "id" && _.push({ jsName: c[y], dbColumn: S[y] });
|
|
1916
|
+
const $ = 'type, namespace, entity, "branchId", "transactionId", "entityId", "inversePatch", patch', A = f ? `'${f}'` : "NULL", b = d || "main", E = `"${l}"."${o}_change_trigger_fn"`, m = `'${l}','${i}','${b}',${A}`, g = `
|
|
1907
1917
|
-- 创建或替换触发器函数
|
|
1908
1918
|
CREATE OR REPLACE FUNCTION ${E}()
|
|
1909
1919
|
RETURNS TRIGGER AS $$
|
|
@@ -1913,29 +1923,29 @@ DECLARE
|
|
|
1913
1923
|
BEGIN
|
|
1914
1924
|
-- INSERT 操作
|
|
1915
1925
|
IF (TG_OP = 'INSERT') THEN
|
|
1916
|
-
INSERT INTO ${s} (${
|
|
1926
|
+
INSERT INTO ${s} (${$})
|
|
1917
1927
|
VALUES (
|
|
1918
1928
|
'INSERT',
|
|
1919
1929
|
${m},
|
|
1920
1930
|
NEW.id,
|
|
1921
1931
|
NULL,
|
|
1922
|
-
jsonb_build_object(${
|
|
1932
|
+
jsonb_build_object(${_.map((y) => `'${y.jsName}', NEW."${y.dbColumn}"`).join(", ")})
|
|
1923
1933
|
);
|
|
1924
1934
|
RETURN NEW;
|
|
1925
1935
|
|
|
1926
1936
|
-- UPDATE 操作
|
|
1927
1937
|
ELSIF (TG_OP = 'UPDATE') THEN
|
|
1928
1938
|
-- 只在字段真正变更时记录
|
|
1929
|
-
IF (${
|
|
1939
|
+
IF (${_.map((y) => `OLD."${y.dbColumn}" IS DISTINCT FROM NEW."${y.dbColumn}"`).join(" OR ")}) THEN
|
|
1930
1940
|
-- 构建变更前的值 (inversePatch - 只包含变更的字段)
|
|
1931
|
-
old_values := jsonb_build_object(${
|
|
1941
|
+
old_values := jsonb_build_object(${_.map((y) => `'${y.jsName}', CASE WHEN OLD."${y.dbColumn}" IS DISTINCT FROM NEW."${y.dbColumn}" THEN to_jsonb(OLD."${y.dbColumn}") ELSE NULL END`).join(", ")});
|
|
1932
1942
|
old_values := jsonb_strip_nulls(old_values);
|
|
1933
1943
|
|
|
1934
1944
|
-- 构建变更后的值 (patch - 只包含变更的字段)
|
|
1935
|
-
new_values := jsonb_build_object(${
|
|
1945
|
+
new_values := jsonb_build_object(${_.map((y) => `'${y.jsName}', CASE WHEN OLD."${y.dbColumn}" IS DISTINCT FROM NEW."${y.dbColumn}" THEN to_jsonb(NEW."${y.dbColumn}") ELSE NULL END`).join(", ")});
|
|
1936
1946
|
new_values := jsonb_strip_nulls(new_values);
|
|
1937
1947
|
|
|
1938
|
-
INSERT INTO ${s} (${
|
|
1948
|
+
INSERT INTO ${s} (${$})
|
|
1939
1949
|
VALUES (
|
|
1940
1950
|
'UPDATE',
|
|
1941
1951
|
${m},
|
|
@@ -1948,12 +1958,12 @@ BEGIN
|
|
|
1948
1958
|
|
|
1949
1959
|
-- DELETE 操作
|
|
1950
1960
|
ELSIF (TG_OP = 'DELETE') THEN
|
|
1951
|
-
INSERT INTO ${s} (${
|
|
1961
|
+
INSERT INTO ${s} (${$})
|
|
1952
1962
|
VALUES (
|
|
1953
1963
|
'DELETE',
|
|
1954
1964
|
${m},
|
|
1955
1965
|
OLD.id,
|
|
1956
|
-
jsonb_build_object(${
|
|
1966
|
+
jsonb_build_object(${_.map((y) => `'${y.jsName}', OLD."${y.dbColumn}"`).join(", ")}),
|
|
1957
1967
|
NULL
|
|
1958
1968
|
);
|
|
1959
1969
|
RETURN OLD;
|
|
@@ -1963,13 +1973,13 @@ BEGIN
|
|
|
1963
1973
|
END;
|
|
1964
1974
|
$$ LANGUAGE plpgsql;`, R = `
|
|
1965
1975
|
-- 删除已存在的触发器
|
|
1966
|
-
DROP TRIGGER IF EXISTS ${
|
|
1976
|
+
DROP TRIGGER IF EXISTS ${p} ON ${n}`, I = `
|
|
1967
1977
|
-- 创建触发器 (监听 INSERT, UPDATE, DELETE)
|
|
1968
|
-
CREATE TRIGGER ${
|
|
1978
|
+
CREATE TRIGGER ${p}
|
|
1969
1979
|
AFTER INSERT OR UPDATE OR DELETE ON ${n}
|
|
1970
1980
|
FOR EACH ROW
|
|
1971
1981
|
EXECUTE FUNCTION ${E}()`;
|
|
1972
|
-
return [
|
|
1982
|
+
return [g, R, I].join(`
|
|
1973
1983
|
---STATEMENT_SEPARATOR---
|
|
1974
1984
|
`);
|
|
1975
1985
|
}
|
|
@@ -2038,11 +2048,11 @@ const He = (e, t, n, r = !1) => {
|
|
|
2038
2048
|
const c = Z(s, o), u = c.id;
|
|
2039
2049
|
let l;
|
|
2040
2050
|
if (i.hasEntityRef(t, u)) {
|
|
2041
|
-
if (l = i.getEntityRef(t, u), s.computedPropertyMap.forEach((
|
|
2042
|
-
|
|
2051
|
+
if (l = i.getEntityRef(t, u), s.computedPropertyMap.forEach((p, _) => {
|
|
2052
|
+
_ in c && (l[_] = c[_]);
|
|
2043
2053
|
}), r) {
|
|
2044
|
-
const
|
|
2045
|
-
|
|
2054
|
+
const p = J(l);
|
|
2055
|
+
p.origin = { ...c }, Object.assign(l, c), p.local = !0, p.modified = !1;
|
|
2046
2056
|
}
|
|
2047
2057
|
} else
|
|
2048
2058
|
l = i.createEntityRef(t, c);
|
|
@@ -2060,95 +2070,95 @@ const He = (e, t, n, r = !1) => {
|
|
|
2060
2070
|
}
|
|
2061
2071
|
}, jn = (e, t) => {
|
|
2062
2072
|
const { deletes: n, inserts: r, updates: s } = t, a = { deletes: [], inserts: [], updates: [] }, i = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map();
|
|
2063
|
-
for (const [f,
|
|
2064
|
-
const [
|
|
2065
|
-
b ? b.add(
|
|
2066
|
-
let E = o.get(
|
|
2067
|
-
E || (E = /* @__PURE__ */ new Map(), o.set(
|
|
2068
|
-
patch:
|
|
2069
|
-
inversePatch:
|
|
2073
|
+
for (const [f, p] of n.entries()) {
|
|
2074
|
+
const [_, S, $] = Ae(f), A = `${_}:${S}`, b = i.get(A);
|
|
2075
|
+
b ? b.add($) : i.set(A, /* @__PURE__ */ new Set([$]));
|
|
2076
|
+
let E = o.get(A);
|
|
2077
|
+
E || (E = /* @__PURE__ */ new Map(), o.set(A, E)), E.set($, {
|
|
2078
|
+
patch: p.patch,
|
|
2079
|
+
inversePatch: p.inversePatch
|
|
2070
2080
|
});
|
|
2071
2081
|
}
|
|
2072
|
-
for (const [f,
|
|
2073
|
-
const [
|
|
2082
|
+
for (const [f, p] of i) {
|
|
2083
|
+
const [_, S] = f.split(":"), $ = e.rxdb.schemaManager.getEntityMetadata(S, _), A = P($), b = $.propertyMap.get("id").type === "integer", E = Array.from(p), m = b ? `DELETE FROM ${A} WHERE id = ANY($1::integer[]);` : `DELETE FROM ${A} WHERE id = ANY($1);`;
|
|
2074
2084
|
a.deletes.push({
|
|
2075
|
-
metadata:
|
|
2076
|
-
ids:
|
|
2085
|
+
metadata: $,
|
|
2086
|
+
ids: p,
|
|
2077
2087
|
sql: m,
|
|
2078
2088
|
params: [E],
|
|
2079
2089
|
changes: o.get(f)
|
|
2080
2090
|
});
|
|
2081
2091
|
}
|
|
2082
2092
|
const c = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map();
|
|
2083
|
-
for (const [f,
|
|
2084
|
-
const [
|
|
2085
|
-
b ? b.push(E) : c.set(
|
|
2086
|
-
let m = u.get(
|
|
2087
|
-
m || (m = /* @__PURE__ */ new Map(), u.set(
|
|
2088
|
-
patch:
|
|
2089
|
-
inversePatch:
|
|
2093
|
+
for (const [f, p] of r.entries()) {
|
|
2094
|
+
const [_, S, $] = Ae(f), A = `${_}:${S}`, b = c.get(A), E = { ...p.patch, id: $ };
|
|
2095
|
+
b ? b.push(E) : c.set(A, [E]);
|
|
2096
|
+
let m = u.get(A);
|
|
2097
|
+
m || (m = /* @__PURE__ */ new Map(), u.set(A, m)), m.set($, {
|
|
2098
|
+
patch: p.patch,
|
|
2099
|
+
inversePatch: p.inversePatch
|
|
2090
2100
|
});
|
|
2091
2101
|
}
|
|
2092
|
-
for (const [f,
|
|
2093
|
-
const [
|
|
2094
|
-
|
|
2095
|
-
for (const [I, y] of
|
|
2102
|
+
for (const [f, p] of c) {
|
|
2103
|
+
const [_, S] = f.split(":"), $ = e.rxdb.schemaManager.getEntityMetadata(S, _), A = [];
|
|
2104
|
+
p.forEach((E) => {
|
|
2105
|
+
for (const [I, y] of $.propertyMap)
|
|
2096
2106
|
E[I] === void 0 && y.default !== void 0 && (typeof y.default == "function" ? E[I] = y.default() : E[I] = y.default);
|
|
2097
|
-
const { sql: m, params:
|
|
2098
|
-
|
|
2107
|
+
const { sql: m, params: g } = In($, E, { returning: !1 }), R = m.replace(";", ga($));
|
|
2108
|
+
A.push(nt(R, g));
|
|
2099
2109
|
});
|
|
2100
|
-
const b =
|
|
2110
|
+
const b = A.join("---STATEMENT_SEPARATOR---");
|
|
2101
2111
|
a.inserts.push({
|
|
2102
|
-
metadata:
|
|
2103
|
-
ids: new Set(
|
|
2112
|
+
metadata: $,
|
|
2113
|
+
ids: new Set(p.filter((E) => E != null).map((E) => E.id)),
|
|
2104
2114
|
sql: b,
|
|
2105
2115
|
params: [],
|
|
2106
2116
|
// 已经内联到 SQL 中
|
|
2107
2117
|
changes: u.get(f)
|
|
2108
2118
|
});
|
|
2109
2119
|
}
|
|
2110
|
-
const l = /* @__PURE__ */ new Map(),
|
|
2111
|
-
for (const [f,
|
|
2112
|
-
const [
|
|
2113
|
-
b ? b.push(E) : l.set(
|
|
2114
|
-
let m =
|
|
2115
|
-
m || (m = /* @__PURE__ */ new Map(),
|
|
2116
|
-
const
|
|
2117
|
-
|
|
2118
|
-
const T =
|
|
2119
|
-
T && (R[y] =
|
|
2120
|
+
const l = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map();
|
|
2121
|
+
for (const [f, p] of s.entries()) {
|
|
2122
|
+
const [_, S, $] = Ae(f), A = `${_}:${S}`, b = l.get(A), E = { ...p.patch, id: $ };
|
|
2123
|
+
b ? b.push(E) : l.set(A, [E]);
|
|
2124
|
+
let m = d.get(A);
|
|
2125
|
+
m || (m = /* @__PURE__ */ new Map(), d.set(A, m));
|
|
2126
|
+
const g = e.rxdb.schemaManager.getEntityMetadata(S, _), R = p.patch ? { ...p.patch } : null, I = p.inversePatch ? { ...p.inversePatch } : null;
|
|
2127
|
+
g && (R && Object.keys(R).forEach((y) => {
|
|
2128
|
+
const T = g.propertyMap.get(y);
|
|
2129
|
+
T && (R[y] = Le(R[y], T));
|
|
2120
2130
|
}), I && Object.keys(I).forEach((y) => {
|
|
2121
|
-
const T =
|
|
2122
|
-
T && (I[y] =
|
|
2123
|
-
})), m.set(
|
|
2131
|
+
const T = g.propertyMap.get(y);
|
|
2132
|
+
T && (I[y] = Le(I[y], T));
|
|
2133
|
+
})), m.set($, {
|
|
2124
2134
|
patch: R,
|
|
2125
2135
|
inversePatch: I
|
|
2126
2136
|
});
|
|
2127
2137
|
}
|
|
2128
|
-
for (const [f,
|
|
2129
|
-
const [
|
|
2130
|
-
|
|
2138
|
+
for (const [f, p] of l) {
|
|
2139
|
+
const [_, S] = f.split(":"), $ = e.rxdb.schemaManager.getEntityMetadata(S, _), A = [];
|
|
2140
|
+
p.forEach((E) => {
|
|
2131
2141
|
let m;
|
|
2132
|
-
|
|
2133
|
-
const { id:
|
|
2142
|
+
$.propertyMap.has("updatedAt") && (m = E.updatedAt);
|
|
2143
|
+
const { id: g, ...R } = E, { sql: I, params: y } = ze($, { id: g }, R, {
|
|
2134
2144
|
updatedAt: m,
|
|
2135
2145
|
returning: !1
|
|
2136
2146
|
});
|
|
2137
|
-
|
|
2147
|
+
A.push(nt(I, y));
|
|
2138
2148
|
});
|
|
2139
|
-
const b =
|
|
2149
|
+
const b = A.join("---STATEMENT_SEPARATOR---");
|
|
2140
2150
|
a.updates.push({
|
|
2141
|
-
metadata:
|
|
2142
|
-
ids: new Set(
|
|
2151
|
+
metadata: $,
|
|
2152
|
+
ids: new Set(p.filter((E) => E != null).map((E) => E.id)),
|
|
2143
2153
|
sql: b,
|
|
2144
2154
|
params: [],
|
|
2145
2155
|
// 已经内联到 SQL 中
|
|
2146
|
-
changes:
|
|
2156
|
+
changes: d.get(f)
|
|
2147
2157
|
});
|
|
2148
2158
|
}
|
|
2149
2159
|
return a;
|
|
2150
|
-
},
|
|
2151
|
-
function
|
|
2160
|
+
}, ga = (e) => ` ON CONFLICT (id) DO UPDATE SET ${Array.from(e.propertyMap.values()).filter((n) => n.name !== "id" && n.name !== "createdAt" && n.name !== "createdBy").map((n) => `"${n.columnName}" = EXCLUDED."${n.columnName}"`).join(", ")};`;
|
|
2161
|
+
function _a(e) {
|
|
2152
2162
|
return {
|
|
2153
2163
|
rows: e.rows,
|
|
2154
2164
|
rowsAffected: e.affectedRows ?? e.rows.length,
|
|
@@ -2161,7 +2171,7 @@ const xn = (e, t) => {
|
|
|
2161
2171
|
const c = D(o);
|
|
2162
2172
|
if (c.log !== !1)
|
|
2163
2173
|
try {
|
|
2164
|
-
const u =
|
|
2174
|
+
const u = $e(c, {
|
|
2165
2175
|
branchId: t
|
|
2166
2176
|
});
|
|
2167
2177
|
u.trim() && n.push(u);
|
|
@@ -2192,9 +2202,9 @@ const xn = (e, t) => {
|
|
|
2192
2202
|
await e.transaction(async () => {
|
|
2193
2203
|
const u = Un(e);
|
|
2194
2204
|
if (u) {
|
|
2195
|
-
const l = u.split("---STATEMENT_SEPARATOR---").filter((
|
|
2196
|
-
for (const
|
|
2197
|
-
await e.query(
|
|
2205
|
+
const l = u.split("---STATEMENT_SEPARATOR---").filter((d) => d.trim());
|
|
2206
|
+
for (const d of l)
|
|
2207
|
+
await e.query(d.trim());
|
|
2198
2208
|
}
|
|
2199
2209
|
if (s) {
|
|
2200
2210
|
if (s.deletes.length)
|
|
@@ -2216,23 +2226,23 @@ const xn = (e, t) => {
|
|
|
2216
2226
|
}
|
|
2217
2227
|
const c = (u, l) => {
|
|
2218
2228
|
if (l.length === 0) return;
|
|
2219
|
-
const
|
|
2220
|
-
const
|
|
2229
|
+
const d = l.map((f) => {
|
|
2230
|
+
const p = { ...f };
|
|
2221
2231
|
return {
|
|
2222
2232
|
namespace: u.namespace,
|
|
2223
2233
|
entity: u.name,
|
|
2224
2234
|
type: "UPDATE",
|
|
2225
2235
|
id: f.id,
|
|
2226
|
-
patch:
|
|
2227
|
-
inversePatch:
|
|
2236
|
+
patch: p,
|
|
2237
|
+
inversePatch: p,
|
|
2228
2238
|
recordAt: f.createdAt
|
|
2229
2239
|
};
|
|
2230
2240
|
});
|
|
2231
|
-
e.rxdb.dispatchEvent(new me(
|
|
2241
|
+
e.rxdb.dispatchEvent(new me(d));
|
|
2232
2242
|
};
|
|
2233
2243
|
if (a) {
|
|
2234
|
-
const u =
|
|
2235
|
-
c(
|
|
2244
|
+
const u = _a(a), l = He(e, ne, u, !0), d = D(ne);
|
|
2245
|
+
c(d, l);
|
|
2236
2246
|
}
|
|
2237
2247
|
s && Bn(e, s);
|
|
2238
2248
|
} catch (i) {
|
|
@@ -2331,12 +2341,12 @@ function St(e, t, n) {
|
|
|
2331
2341
|
return e.rxdb.config.entities.forEach((s) => {
|
|
2332
2342
|
const a = D(s);
|
|
2333
2343
|
if (a.log !== !1) {
|
|
2334
|
-
const o =
|
|
2344
|
+
const o = $e(a, { branchId: t, transactionId: n }).split("---STATEMENT_SEPARATOR---").map((c) => c.trim()).filter((c) => c.length > 0);
|
|
2335
2345
|
r.push(...o);
|
|
2336
2346
|
}
|
|
2337
2347
|
}), r;
|
|
2338
2348
|
}
|
|
2339
|
-
class
|
|
2349
|
+
class Ca extends kn {
|
|
2340
2350
|
/**
|
|
2341
2351
|
* 构造函数
|
|
2342
2352
|
*
|
|
@@ -2400,8 +2410,8 @@ class La extends kn {
|
|
|
2400
2410
|
(async () => {
|
|
2401
2411
|
const s = this.rxdb.schemaManager.getEntityMetadata(t, "public")?.tableName ?? t, a = Object.keys(n[0]), i = a.map((o) => `"${o}"`).join(", ");
|
|
2402
2412
|
for (const o of n) {
|
|
2403
|
-
const c = a.map((f) => o[f]), u = a.map((f,
|
|
2404
|
-
await this.internalQuery(
|
|
2413
|
+
const c = a.map((f) => o[f]), u = a.map((f, p) => `$${p + 1}`).join(", "), l = a.filter((f) => f !== "id").map((f) => `"${f}" = EXCLUDED."${f}"`).join(", "), d = `INSERT INTO "public"."${s}" (${i}) VALUES (${u}) ON CONFLICT (id) DO UPDATE SET ${l}`;
|
|
2414
|
+
await this.internalQuery(d, c);
|
|
2405
2415
|
}
|
|
2406
2416
|
})()
|
|
2407
2417
|
));
|
|
@@ -2513,7 +2523,7 @@ class La extends kn {
|
|
|
2513
2523
|
*/
|
|
2514
2524
|
async connect() {
|
|
2515
2525
|
const t = await this.#n(), n = pa();
|
|
2516
|
-
return await t.exec(n), t instanceof Rt && (t.addEventListener(K.INSERT, (r) =>
|
|
2526
|
+
return await t.exec(n), t instanceof Rt && (t.addEventListener(K.INSERT, (r) => Ce(this, r)), t.addEventListener(K.UPDATE, (r) => Ce(this, r)), t.addEventListener(K.DELETE, (r) => Ce(this, r))), this;
|
|
2517
2527
|
}
|
|
2518
2528
|
/**
|
|
2519
2529
|
* 断开数据库连接
|
|
@@ -2639,7 +2649,7 @@ class La extends kn {
|
|
|
2639
2649
|
for (const i of t) {
|
|
2640
2650
|
const o = D(i);
|
|
2641
2651
|
if (o.log !== !1) {
|
|
2642
|
-
const c =
|
|
2652
|
+
const c = $e(o);
|
|
2643
2653
|
await this.#c(c.replace(/---STATEMENT_SEPARATOR---/g, ";"));
|
|
2644
2654
|
}
|
|
2645
2655
|
}
|
|
@@ -2849,14 +2859,14 @@ class Na {
|
|
|
2849
2859
|
return `UPDATE ${s} SET ${i} FROM (VALUES ($1)) AS temp(${a}, ${r.join(", ")}) WHERE ${s}.${a} = temp.${a} ${this.getReturningClause()}`;
|
|
2850
2860
|
}
|
|
2851
2861
|
}
|
|
2852
|
-
const
|
|
2862
|
+
const La = new Na();
|
|
2853
2863
|
function Ma(e, t, n) {
|
|
2854
2864
|
let r = "";
|
|
2855
2865
|
for (let s = 0; s < t.length; s++) {
|
|
2856
2866
|
const a = t[s], i = D(a), o = qn(e, i);
|
|
2857
2867
|
if (r += `
|
|
2858
2868
|
` + o, i.log !== !1) {
|
|
2859
|
-
const c =
|
|
2869
|
+
const c = $e(i);
|
|
2860
2870
|
r += `
|
|
2861
2871
|
` + c;
|
|
2862
2872
|
}
|
|
@@ -2880,22 +2890,22 @@ export {
|
|
|
2880
2890
|
K as PGliteChangeType,
|
|
2881
2891
|
Rt as PGliteClient,
|
|
2882
2892
|
Na as PostgreSQLDialect,
|
|
2883
|
-
|
|
2893
|
+
Ca as RxDBAdapterPGlite,
|
|
2884
2894
|
x as RxdbAdapterPGliteError,
|
|
2885
2895
|
Ma as create_tables_sql,
|
|
2886
2896
|
da as generateNotifyFunctionSQL,
|
|
2887
2897
|
pa as generateNotifyInfrastructureSQL,
|
|
2888
2898
|
Pn as generateNotifyTriggerSQL,
|
|
2889
|
-
|
|
2899
|
+
$e as generate_trigger_sql,
|
|
2890
2900
|
Z as getEntityObjectFromResult,
|
|
2891
2901
|
Je as getSqlValue,
|
|
2892
2902
|
nt as getSqlWithParams,
|
|
2893
|
-
|
|
2903
|
+
Ct as getTableColumnIndexName,
|
|
2894
2904
|
vt as getTableName,
|
|
2895
2905
|
P as getTableNameByMetadata,
|
|
2896
|
-
|
|
2906
|
+
Lt as normalizeCreateEntity,
|
|
2897
2907
|
zn as normalizeEntity,
|
|
2898
|
-
|
|
2908
|
+
La as pgDialect,
|
|
2899
2909
|
Da as removeNotifyTriggerSQL,
|
|
2900
2910
|
Un as remove_all_triggers_sql,
|
|
2901
2911
|
ma as remove_trigger_sql,
|
|
@@ -2904,5 +2914,5 @@ export {
|
|
|
2904
2914
|
st as transformEntityValuePGliteToJs,
|
|
2905
2915
|
Ke as transformEntityValueToSql,
|
|
2906
2916
|
tt as transformValueJsToPGlite,
|
|
2907
|
-
|
|
2917
|
+
Le as transformValuePGliteToJs
|
|
2908
2918
|
};
|