@stonecrop/graphql-middleware 0.25.0 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/dist/graphql-middleware.d.ts +3 -1
- package/dist/index.js +253 -220
- package/dist/index.js.map +1 -1
- package/dist/src/plugin/postgraphile.d.ts +3 -1
- package/dist/src/plugin/postgraphile.d.ts.map +1 -1
- package/dist/src/plugin/postgraphile.js +10 -5
- package/dist/src/preset.d.ts.map +1 -1
- package/dist/src/preset.js +95 -1
- package/package.json +2 -4
package/dist/index.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { readdirSync as
|
|
2
|
-
import { join as
|
|
3
|
-
import { flattenFields as
|
|
4
|
-
import { sideEffectWithPgClient as
|
|
5
|
-
import { object as re, constant as
|
|
6
|
-
import { gql as
|
|
7
|
-
import { PostGraphileAmberPreset as
|
|
8
|
-
import { makePgService as
|
|
1
|
+
import { readdirSync as be, statSync as Ee, readFileSync as Re } from "node:fs";
|
|
2
|
+
import { join as ve } from "node:path";
|
|
3
|
+
import { flattenFields as K, validateDoctype as ye, parseDoctype as we, isActionAllowedInState as Ae, pascalToSnake as Me, camelToSnake as b, getDisplayField as Oe, getRecordIdField as Se, resolveLinkRenderMode as ae } from "@stonecrop/schema";
|
|
4
|
+
import { sideEffectWithPgClient as xe, loadOneWithPgClient as ue } from "@dataplan/pg";
|
|
5
|
+
import { object as re, constant as Fe, lambda as Ne } from "postgraphile/grafast";
|
|
6
|
+
import { gql as Te, extendSchema as Le, wrapPlans as Ie } from "postgraphile/utils";
|
|
7
|
+
import { PostGraphileAmberPreset as De } from "postgraphile/presets/amber";
|
|
8
|
+
import { makePgService as ft } from "postgraphile/adaptors/pg";
|
|
9
9
|
function te(e) {
|
|
10
|
-
return
|
|
10
|
+
return K(e).filter((t) => t.kind === "field" && !t.computed);
|
|
11
11
|
}
|
|
12
|
-
const
|
|
12
|
+
const W = /* @__PURE__ */ new Map();
|
|
13
13
|
class se extends Error {
|
|
14
14
|
constructor(t, o) {
|
|
15
15
|
const n = o.map((r) => ` ${r.path.join(".")}: ${r.message}`).join(`
|
|
@@ -20,15 +20,15 @@ ${n}`), this.file = t, this.errors = o, this.name = "DoctypeValidationError";
|
|
|
20
20
|
file;
|
|
21
21
|
errors;
|
|
22
22
|
}
|
|
23
|
-
function
|
|
24
|
-
const o =
|
|
23
|
+
function Ce(e, t = {}) {
|
|
24
|
+
const o = be(e);
|
|
25
25
|
for (const n of o) {
|
|
26
|
-
const r =
|
|
27
|
-
|
|
26
|
+
const r = ve(e, n);
|
|
27
|
+
Ee(r).isDirectory() ? Ce(r, t) : n.endsWith(".json") && Be(r, t);
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
function Be(e, t) {
|
|
31
|
-
const o =
|
|
31
|
+
const o = Re(e, "utf-8");
|
|
32
32
|
let n;
|
|
33
33
|
try {
|
|
34
34
|
n = JSON.parse(o);
|
|
@@ -43,7 +43,7 @@ function Be(e, t) {
|
|
|
43
43
|
}
|
|
44
44
|
throw new se(e, [u]);
|
|
45
45
|
}
|
|
46
|
-
const r =
|
|
46
|
+
const r = ye(n);
|
|
47
47
|
if (!r.success) {
|
|
48
48
|
if (t.continueOnError) {
|
|
49
49
|
t.onError?.(e, r.errors);
|
|
@@ -51,12 +51,12 @@ function Be(e, t) {
|
|
|
51
51
|
}
|
|
52
52
|
throw new se(e, r.errors);
|
|
53
53
|
}
|
|
54
|
-
const i =
|
|
55
|
-
|
|
54
|
+
const i = we(n);
|
|
55
|
+
W.set(i.name, i);
|
|
56
56
|
}
|
|
57
|
-
function
|
|
57
|
+
function nt(e, t = {}) {
|
|
58
58
|
for (const [o, n] of Object.entries(e)) {
|
|
59
|
-
const r = { ...typeof n == "object" && n !== null ? n : {}, name: o }, i =
|
|
59
|
+
const r = { ...typeof n == "object" && n !== null ? n : {}, name: o }, i = ye(r);
|
|
60
60
|
if (!i.success) {
|
|
61
61
|
if (t.continueOnError) {
|
|
62
62
|
t.onError?.(o, i.errors);
|
|
@@ -64,37 +64,37 @@ function et(e, t = {}) {
|
|
|
64
64
|
}
|
|
65
65
|
throw new se(o, i.errors);
|
|
66
66
|
}
|
|
67
|
-
const a =
|
|
68
|
-
|
|
67
|
+
const a = we(r);
|
|
68
|
+
W.set(a.name, a);
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
|
-
function
|
|
72
|
-
const t =
|
|
71
|
+
function T(e) {
|
|
72
|
+
const t = W.get(e);
|
|
73
73
|
if (t) return t;
|
|
74
|
-
for (const o of
|
|
74
|
+
for (const o of W.values())
|
|
75
75
|
if (o.slug === e) return o;
|
|
76
76
|
}
|
|
77
|
-
function
|
|
78
|
-
return Array.from(
|
|
77
|
+
function $e() {
|
|
78
|
+
return Array.from(W.values());
|
|
79
79
|
}
|
|
80
|
-
function
|
|
81
|
-
return
|
|
80
|
+
function ot(e) {
|
|
81
|
+
return W.has(e);
|
|
82
82
|
}
|
|
83
|
-
function
|
|
84
|
-
|
|
83
|
+
function rt() {
|
|
84
|
+
W.clear();
|
|
85
85
|
}
|
|
86
|
-
function
|
|
86
|
+
function Pe() {
|
|
87
87
|
const e = [];
|
|
88
|
-
for (const t of
|
|
89
|
-
t.inherits &&
|
|
88
|
+
for (const t of W.values()) {
|
|
89
|
+
t.inherits && T(t.inherits) === void 0 && e.push({
|
|
90
90
|
path: [t.name, "inherits"],
|
|
91
91
|
message: `References unknown doctype: ${t.inherits}`
|
|
92
92
|
});
|
|
93
|
-
const o =
|
|
93
|
+
const o = K(t.fields);
|
|
94
94
|
for (const n of o) {
|
|
95
95
|
if (n.kind !== "field") continue;
|
|
96
96
|
const r = n.doctype;
|
|
97
|
-
r !== void 0 &&
|
|
97
|
+
r !== void 0 && T(r) === void 0 && e.push({
|
|
98
98
|
path: [t.name, "fields", n.fieldname, "doctype"],
|
|
99
99
|
message: `Link references unknown doctype: ${r}`
|
|
100
100
|
});
|
|
@@ -102,7 +102,7 @@ function Ie() {
|
|
|
102
102
|
if (t.links) {
|
|
103
103
|
const n = new Set(o.map((i) => i.fieldname)), r = new Set(te(t.fields).map((i) => i.fieldname));
|
|
104
104
|
for (const [i, a] of Object.entries(t.links)) {
|
|
105
|
-
if (
|
|
105
|
+
if (T(a.target) === void 0 && e.push({
|
|
106
106
|
path: [t.name, "links", i, "target"],
|
|
107
107
|
message: `Link references unknown doctype: ${a.target}`
|
|
108
108
|
}), a.fetch?.method === "custom") continue;
|
|
@@ -129,17 +129,17 @@ function Ie() {
|
|
|
129
129
|
}
|
|
130
130
|
return e;
|
|
131
131
|
}
|
|
132
|
-
const
|
|
133
|
-
function
|
|
134
|
-
|
|
132
|
+
const ce = /* @__PURE__ */ new Map();
|
|
133
|
+
function it(e, t) {
|
|
134
|
+
ce.set(e, t);
|
|
135
135
|
}
|
|
136
|
-
function
|
|
137
|
-
return
|
|
136
|
+
function je(e) {
|
|
137
|
+
return ce.get(e);
|
|
138
138
|
}
|
|
139
|
-
function
|
|
140
|
-
|
|
139
|
+
function at() {
|
|
140
|
+
ce.clear();
|
|
141
141
|
}
|
|
142
|
-
async function
|
|
142
|
+
async function _e(e, t, o) {
|
|
143
143
|
const n = e.label ?? "unknown", r = e.selfTransition === !0, i = r || e.nextState != null;
|
|
144
144
|
if (!i && !t.runEffect)
|
|
145
145
|
return {
|
|
@@ -159,12 +159,12 @@ async function je(e, t, o) {
|
|
|
159
159
|
const k = await t.readState();
|
|
160
160
|
if (k === null) {
|
|
161
161
|
if (r && t.writeData) {
|
|
162
|
-
const
|
|
163
|
-
return !
|
|
162
|
+
const E = await t.writeData(o ?? {}, !1);
|
|
163
|
+
return !E || Object.keys(E).length === 0 ? {
|
|
164
164
|
success: !1,
|
|
165
165
|
data: null,
|
|
166
166
|
error: `Action "${n}" targets a record that does not exist, and the backend did not create it.`
|
|
167
|
-
} : { success: !0, data:
|
|
167
|
+
} : { success: !0, data: E, error: null };
|
|
168
168
|
}
|
|
169
169
|
return {
|
|
170
170
|
success: !1,
|
|
@@ -174,7 +174,7 @@ async function je(e, t, o) {
|
|
|
174
174
|
}
|
|
175
175
|
u = k ?? "";
|
|
176
176
|
}
|
|
177
|
-
if (!
|
|
177
|
+
if (!Ae(e, u ?? ""))
|
|
178
178
|
return {
|
|
179
179
|
success: !1,
|
|
180
180
|
data: null,
|
|
@@ -197,8 +197,8 @@ async function je(e, t, o) {
|
|
|
197
197
|
error: null
|
|
198
198
|
});
|
|
199
199
|
}
|
|
200
|
-
function
|
|
201
|
-
const o = t?.[e] ??
|
|
200
|
+
function _(e, t) {
|
|
201
|
+
const o = t?.[e] ?? Me(e), n = o.indexOf(".");
|
|
202
202
|
return n > 0 ? `"${o.slice(0, n)}"."${o.slice(n + 1)}"` : `"${o}"`;
|
|
203
203
|
}
|
|
204
204
|
function Z(e) {
|
|
@@ -208,24 +208,24 @@ function Z(e) {
|
|
|
208
208
|
return String(e);
|
|
209
209
|
}
|
|
210
210
|
}
|
|
211
|
-
function
|
|
211
|
+
function We(e, t) {
|
|
212
212
|
const o = [];
|
|
213
|
-
for (const n of
|
|
213
|
+
for (const n of K(e.fields)) {
|
|
214
214
|
if (n.kind !== "field" || !n.doctype) continue;
|
|
215
|
-
const r =
|
|
215
|
+
const r = T(n.doctype);
|
|
216
216
|
if (!r) continue;
|
|
217
|
-
const i =
|
|
217
|
+
const i = Oe(r.fields, r.displayField);
|
|
218
218
|
i && o.push({
|
|
219
219
|
fieldname: n.fieldname,
|
|
220
220
|
displayField: i.fieldname,
|
|
221
|
-
targetPkFieldname:
|
|
222
|
-
targetTable:
|
|
221
|
+
targetPkFieldname: Se(r.fields),
|
|
222
|
+
targetTable: _(r.name, t)
|
|
223
223
|
});
|
|
224
224
|
}
|
|
225
225
|
return o;
|
|
226
226
|
}
|
|
227
227
|
async function fe(e, t, o, n, r) {
|
|
228
|
-
const i =
|
|
228
|
+
const i = We(t, n);
|
|
229
229
|
i.length === 0 || o.length === 0 || await Promise.all(
|
|
230
230
|
i.map(async (a) => {
|
|
231
231
|
const u = /* @__PURE__ */ new Set();
|
|
@@ -234,8 +234,8 @@ async function fe(e, t, o, n, r) {
|
|
|
234
234
|
f !== void 0 && u.add(f);
|
|
235
235
|
}
|
|
236
236
|
if (u.size === 0) return;
|
|
237
|
-
const $ =
|
|
238
|
-
text: `SELECT "${$}" AS "${a.targetPkFieldname}", ${
|
|
237
|
+
const $ = b(a.targetPkFieldname), k = b(a.displayField), E = k !== a.displayField ? `"${k}" AS "${a.displayField}"` : `"${a.displayField}"`, { rows: h } = await r(e, {
|
|
238
|
+
text: `SELECT "${$}" AS "${a.targetPkFieldname}", ${E} FROM ${a.targetTable} WHERE "${$}"::text = ANY($1::text[])`,
|
|
239
239
|
values: [Array.from(u)]
|
|
240
240
|
}), y = /* @__PURE__ */ new Map();
|
|
241
241
|
for (const w of h) {
|
|
@@ -251,7 +251,7 @@ async function fe(e, t, o, n, r) {
|
|
|
251
251
|
})
|
|
252
252
|
);
|
|
253
253
|
}
|
|
254
|
-
const
|
|
254
|
+
const He = Te`
|
|
255
255
|
type StonecropFieldMeta {
|
|
256
256
|
kind: String!
|
|
257
257
|
fieldname: String!
|
|
@@ -368,28 +368,28 @@ class he extends Error {
|
|
|
368
368
|
}
|
|
369
369
|
outcome;
|
|
370
370
|
}
|
|
371
|
-
const pe = "__stonecropLinkFk_",
|
|
371
|
+
const pe = "__stonecropLinkFk_", V = "status", st = (e = {}) => {
|
|
372
372
|
const t = e.debug ? (n, r) => (console.log(`[@stonecrop/graphql-middleware] ${r.text}`, r.values ?? []), n.query(r)) : (n, r) => n.query(r), o = e.defaultRecordLimit === void 0 ? 200 : e.defaultRecordLimit;
|
|
373
|
-
return
|
|
373
|
+
return Le((n) => {
|
|
374
374
|
const r = n.input?.pgRegistry?.pgExecutors, i = r ? Object.values(r)[0] : void 0;
|
|
375
375
|
if (!i)
|
|
376
376
|
throw new Error("StonecropPlugin: no pgExecutors found — ensure pgServices is configured");
|
|
377
|
-
return e.actionHandlers &&
|
|
378
|
-
typeDefs:
|
|
377
|
+
return e.actionHandlers && Ue(e.actionHandlers), Je(), {
|
|
378
|
+
typeDefs: He,
|
|
379
379
|
objects: {
|
|
380
380
|
Query: {
|
|
381
381
|
plans: {
|
|
382
382
|
stonecropMeta(a, { $doctype: u }) {
|
|
383
|
-
return
|
|
383
|
+
return Ne(u, ($) => T(String($)) ?? null);
|
|
384
384
|
},
|
|
385
385
|
stonecropAllMeta() {
|
|
386
|
-
return
|
|
386
|
+
return Fe($e());
|
|
387
387
|
},
|
|
388
388
|
stonecropRecord(a, { $doctype: u, $id: $, $options: k }) {
|
|
389
389
|
return ue(
|
|
390
390
|
i,
|
|
391
391
|
re({ doctype: u, id: $, options: k }),
|
|
392
|
-
async (
|
|
392
|
+
async (E, h) => {
|
|
393
393
|
const y = /* @__PURE__ */ new Map();
|
|
394
394
|
for (let f = 0; f < h.length; f++) {
|
|
395
395
|
const s = String(h[f].doctype);
|
|
@@ -397,156 +397,156 @@ const pe = "__stonecropLinkFk_", Q = "status", it = (e = {}) => {
|
|
|
397
397
|
}
|
|
398
398
|
const w = Array.from({ length: h.length });
|
|
399
399
|
for (const [f, s] of y) {
|
|
400
|
-
const p =
|
|
400
|
+
const p = T(f);
|
|
401
401
|
if (!p) {
|
|
402
402
|
for (const d of s) w[d] = { data: null, doctype: f };
|
|
403
403
|
continue;
|
|
404
404
|
}
|
|
405
|
-
const g =
|
|
405
|
+
const g = Y(p);
|
|
406
406
|
if (!g) throw ie(f);
|
|
407
|
-
const
|
|
407
|
+
const C = b(g.fieldname), U = ee(p), x = s.map((d) => String(h[d].id)), L = new Map(K(p.fields).map((d) => [d.fieldname, d])), N = [];
|
|
408
408
|
for (const [d, m] of Object.entries(p.links ?? {})) {
|
|
409
409
|
if (m.cardinality === "noneOrMany" || m.cardinality === "atLeastOne") continue;
|
|
410
410
|
const M = m.fieldname ?? d;
|
|
411
|
-
ae(m,
|
|
411
|
+
ae(m, L.get(M)?.component) !== "inline" && N.push({ fieldname: M, alias: `${pe}${M}` });
|
|
412
412
|
}
|
|
413
|
-
const
|
|
414
|
-
|
|
415
|
-
...
|
|
416
|
-
].filter(Boolean).join(", "), { rows:
|
|
417
|
-
text: `SELECT ${
|
|
413
|
+
const B = [
|
|
414
|
+
U,
|
|
415
|
+
...N.map(({ fieldname: d, alias: m }) => `"${b(d)}" AS "${m}"`)
|
|
416
|
+
].filter(Boolean).join(", "), { rows: c } = await t(E, {
|
|
417
|
+
text: `SELECT ${B} FROM ${_(p.name, e.tables)} WHERE "${C}"::text = ANY($1::text[])`,
|
|
418
418
|
values: [x]
|
|
419
|
-
}),
|
|
420
|
-
if (
|
|
419
|
+
}), R = new Map(c.map((d) => [String(d[g.fieldname]), d]));
|
|
420
|
+
if (c.length !== R.size)
|
|
421
421
|
throw new Error(
|
|
422
|
-
`Doctype "${f}" declares "${g.fieldname}" as its identity, but that column is not unique in ${
|
|
422
|
+
`Doctype "${f}" declares "${g.fieldname}" as its identity, but that column is not unique in ${_(p.name, e.tables)} — ${c.length} rows matched ${R.size} distinct values, so a lookup cannot say which record it means. Declare a field that uniquely identifies a record, or add a unique constraint.`
|
|
423
423
|
);
|
|
424
424
|
const S = [];
|
|
425
425
|
for (const d of s) {
|
|
426
|
-
const m = String(h[d].id), M =
|
|
426
|
+
const m = String(h[d].id), M = R.get(m);
|
|
427
427
|
if (!M) {
|
|
428
428
|
w[d] = { data: null, doctype: f };
|
|
429
429
|
continue;
|
|
430
430
|
}
|
|
431
|
-
const
|
|
432
|
-
S.push(
|
|
433
|
-
const
|
|
431
|
+
const l = { ...M };
|
|
432
|
+
S.push(l);
|
|
433
|
+
const v = [], O = h[d].options ?? {}, A = O.includeNested === !0, F = Array.isArray(O.includeNested) ? new Set(O.includeNested) : null;
|
|
434
434
|
if (p.links)
|
|
435
|
-
for (const [
|
|
436
|
-
const
|
|
437
|
-
if (!(A || (F ? F.has(
|
|
438
|
-
if (
|
|
439
|
-
const J =
|
|
440
|
-
|
|
435
|
+
for (const [P, I] of Object.entries(p.links)) {
|
|
436
|
+
const j = I.fieldname ?? P, G = I.fetch, ne = I.cardinality === "noneOrMany" || I.cardinality === "atLeastOne", le = G?.method ?? (ne ? "sync" : "lazy"), ke = G?.method === "sync" ? G.limit : void 0, Q = ne ? ke ?? o : void 0;
|
|
437
|
+
if (!(A || (F ? F.has(P) : le === "sync"))) continue;
|
|
438
|
+
if (le === "custom" && G?.method === "custom") {
|
|
439
|
+
const J = G.handler, D = je(J);
|
|
440
|
+
D && (l[j] = await D(E, l, I));
|
|
441
441
|
continue;
|
|
442
442
|
}
|
|
443
|
-
if (ae(
|
|
444
|
-
const
|
|
445
|
-
if (!
|
|
446
|
-
const de = ee(
|
|
443
|
+
if (ae(I, L.get(j)?.component) === "inline") continue;
|
|
444
|
+
const q = T(I.target);
|
|
445
|
+
if (!q) continue;
|
|
446
|
+
const de = ee(q);
|
|
447
447
|
if (ne) {
|
|
448
|
-
if (!
|
|
449
|
-
const J =
|
|
450
|
-
let
|
|
451
|
-
const z = [m], X =
|
|
452
|
-
X && (
|
|
453
|
-
const { rows: oe } = await t(
|
|
454
|
-
text:
|
|
448
|
+
if (!I.backlink) continue;
|
|
449
|
+
const J = b(I.backlink);
|
|
450
|
+
let D = `SELECT ${de} FROM ${_(q.name, e.tables)} WHERE "${J}"::text = $1`;
|
|
451
|
+
const z = [m], X = Y(q);
|
|
452
|
+
X && (D += ` ORDER BY "${b(X.fieldname)}" ASC`), Q != null && (D += " LIMIT $2", z.push(Q + 1));
|
|
453
|
+
const { rows: oe } = await t(E, {
|
|
454
|
+
text: D,
|
|
455
455
|
values: z
|
|
456
456
|
});
|
|
457
|
-
|
|
457
|
+
Q != null && oe.length > Q ? (v.push(j), l[j] = oe.slice(0, Q)) : l[j] = oe;
|
|
458
458
|
} else {
|
|
459
|
-
const J =
|
|
459
|
+
const J = l[`${pe}${j}`];
|
|
460
460
|
if (J == null) {
|
|
461
|
-
|
|
461
|
+
l[j] = null;
|
|
462
462
|
continue;
|
|
463
463
|
}
|
|
464
|
-
const
|
|
465
|
-
if (!
|
|
466
|
-
const z =
|
|
467
|
-
text: `SELECT ${de} FROM ${
|
|
464
|
+
const D = Y(q);
|
|
465
|
+
if (!D) throw ie(q.name);
|
|
466
|
+
const z = b(D.fieldname), { rows: X } = await t(E, {
|
|
467
|
+
text: `SELECT ${de} FROM ${_(q.name, e.tables)} WHERE "${z}" = $1`,
|
|
468
468
|
values: [J]
|
|
469
469
|
});
|
|
470
|
-
|
|
470
|
+
l[j] = X[0] ?? null;
|
|
471
471
|
}
|
|
472
472
|
}
|
|
473
|
-
for (const { alias:
|
|
474
|
-
const
|
|
473
|
+
for (const { alias: P } of N) delete l[P];
|
|
474
|
+
const H = F && p.links ? [...F].filter((P) => !(P in p.links)) : void 0;
|
|
475
475
|
w[d] = {
|
|
476
|
-
data:
|
|
476
|
+
data: l,
|
|
477
477
|
doctype: f,
|
|
478
|
-
unknownLinks:
|
|
479
|
-
truncatedLinks:
|
|
478
|
+
unknownLinks: H?.length ? H : void 0,
|
|
479
|
+
truncatedLinks: v.length ? v : void 0
|
|
480
480
|
};
|
|
481
481
|
}
|
|
482
|
-
await fe(
|
|
482
|
+
await fe(E, p, S, e.tables, t);
|
|
483
483
|
}
|
|
484
484
|
return w;
|
|
485
485
|
}
|
|
486
486
|
);
|
|
487
487
|
},
|
|
488
|
-
stonecropRecords(a, { $doctype: u, $filters: $, $orderBy: k, $limit:
|
|
488
|
+
stonecropRecords(a, { $doctype: u, $filters: $, $orderBy: k, $limit: E, $offset: h, $includeTotal: y }) {
|
|
489
489
|
return ue(
|
|
490
490
|
i,
|
|
491
491
|
re({
|
|
492
492
|
doctype: u,
|
|
493
493
|
filters: $,
|
|
494
494
|
orderBy: k,
|
|
495
|
-
limit:
|
|
495
|
+
limit: E,
|
|
496
496
|
offset: h,
|
|
497
497
|
includeTotal: y
|
|
498
498
|
}),
|
|
499
499
|
async (w, f) => await Promise.all(
|
|
500
500
|
f.map(async (s) => {
|
|
501
|
-
const p = String(s.doctype), g =
|
|
501
|
+
const p = String(s.doctype), g = T(p);
|
|
502
502
|
if (!g)
|
|
503
503
|
return { data: [], doctype: p, hasMore: !1, count: s.includeTotal === !0 ? 0 : null };
|
|
504
|
-
const
|
|
504
|
+
const C = new Set(K(g.fields).map((l) => l.fieldname)), U = ee(g), x = [], L = [];
|
|
505
505
|
if (s.filters != null)
|
|
506
|
-
for (const [
|
|
507
|
-
if (!
|
|
508
|
-
throw new Error(`Unknown filter field: ${
|
|
509
|
-
x.push(
|
|
506
|
+
for (const [l, v] of Object.entries(s.filters)) {
|
|
507
|
+
if (!C.has(l))
|
|
508
|
+
throw new Error(`Unknown filter field: ${l} for doctype ${g.name}`);
|
|
509
|
+
x.push(v), L.push(`"${b(l)}" = $${x.length}`);
|
|
510
510
|
}
|
|
511
|
-
const
|
|
512
|
-
let
|
|
511
|
+
const N = L.length > 0 ? ` WHERE ${L.join(" AND ")}` : "";
|
|
512
|
+
let B = "";
|
|
513
513
|
if (s.orderBy != null) {
|
|
514
514
|
if (typeof s.orderBy != "string")
|
|
515
515
|
throw new Error(`Invalid orderBy: expected string, got ${typeof s.orderBy}`);
|
|
516
|
-
const
|
|
517
|
-
if (
|
|
518
|
-
throw new Error(`Invalid orderBy format: "${
|
|
519
|
-
const O =
|
|
516
|
+
const l = s.orderBy, v = l.lastIndexOf("_");
|
|
517
|
+
if (v <= 0)
|
|
518
|
+
throw new Error(`Invalid orderBy format: "${l}". Expected FIELD_ASC or FIELD_DESC`);
|
|
519
|
+
const O = l.slice(v + 1).toUpperCase();
|
|
520
520
|
if (O !== "ASC" && O !== "DESC")
|
|
521
521
|
throw new Error(`Invalid orderBy direction: "${O}". Must be ASC or DESC`);
|
|
522
|
-
const A =
|
|
523
|
-
if (!
|
|
522
|
+
const A = l.slice(0, v);
|
|
523
|
+
if (!C.has(A))
|
|
524
524
|
throw new Error(`Unknown orderBy field: "${A}" for doctype ${g.name}`);
|
|
525
|
-
|
|
525
|
+
B = ` ORDER BY "${b(A)}" ${O}`;
|
|
526
526
|
}
|
|
527
|
-
if (
|
|
528
|
-
const
|
|
529
|
-
|
|
527
|
+
if (B === "") {
|
|
528
|
+
const l = Y(g);
|
|
529
|
+
l && (B = ` ORDER BY "${b(l.fieldname)}" ASC`);
|
|
530
530
|
}
|
|
531
531
|
if (s.limit != null && typeof s.limit != "number")
|
|
532
532
|
throw new Error(`Invalid limit: expected number, got ${typeof s.limit}`);
|
|
533
|
-
const
|
|
534
|
-
let
|
|
535
|
-
|
|
533
|
+
const c = s.limit ?? o;
|
|
534
|
+
let R = "";
|
|
535
|
+
c != null && (x.push(c + 1), R += ` LIMIT $${x.length}`), s.offset != null && (x.push(s.offset), R += ` OFFSET $${x.length}`);
|
|
536
536
|
const { rows: S } = await t(w, {
|
|
537
|
-
text: `SELECT ${
|
|
537
|
+
text: `SELECT ${U} FROM ${_(g.name, e.tables)}${N}${B}${R}`,
|
|
538
538
|
values: x
|
|
539
|
-
}), d =
|
|
539
|
+
}), d = c != null && S.length > c, m = [...d ? S.slice(0, c ?? S.length) : S];
|
|
540
540
|
await fe(w, g, m, e.tables, t);
|
|
541
541
|
let M = null;
|
|
542
542
|
if (s.includeTotal === !0) {
|
|
543
|
-
const
|
|
543
|
+
const l = [], v = [];
|
|
544
544
|
if (s.filters != null)
|
|
545
|
-
for (const [F,
|
|
546
|
-
|
|
547
|
-
const O =
|
|
548
|
-
text: `SELECT COUNT(*) AS row_count FROM ${
|
|
549
|
-
values:
|
|
545
|
+
for (const [F, H] of Object.entries(s.filters))
|
|
546
|
+
C.has(F) && (l.push(H), v.push(`"${b(F)}" = $${l.length}`));
|
|
547
|
+
const O = v.length > 0 ? ` WHERE ${v.join(" AND ")}` : "", { rows: A } = await t(w, {
|
|
548
|
+
text: `SELECT COUNT(*) AS row_count FROM ${_(g.name, e.tables)}${O}`,
|
|
549
|
+
values: l
|
|
550
550
|
});
|
|
551
551
|
M = parseInt(A[0]?.row_count ?? "0", 10);
|
|
552
552
|
}
|
|
@@ -560,11 +560,11 @@ const pe = "__stonecropLinkFk_", Q = "status", it = (e = {}) => {
|
|
|
560
560
|
Mutation: {
|
|
561
561
|
plans: {
|
|
562
562
|
stonecropAction(a, { $doctype: u, $action: $, $args: k }) {
|
|
563
|
-
return
|
|
563
|
+
return xe(
|
|
564
564
|
i,
|
|
565
565
|
re({ doctype: u, action: $, actionArgs: k }),
|
|
566
|
-
async (
|
|
567
|
-
const y =
|
|
566
|
+
async (E, h) => {
|
|
567
|
+
const y = T(String(h.doctype));
|
|
568
568
|
if (!y)
|
|
569
569
|
return {
|
|
570
570
|
success: !1,
|
|
@@ -578,47 +578,47 @@ const pe = "__stonecropLinkFk_", Q = "status", it = (e = {}) => {
|
|
|
578
578
|
data: null,
|
|
579
579
|
error: `Unknown action: ${h.action} on ${h.doctype}`
|
|
580
580
|
};
|
|
581
|
-
const f =
|
|
582
|
-
const
|
|
583
|
-
if (!
|
|
584
|
-
return
|
|
585
|
-
}, p = Array.isArray(h.actionArgs) ? h.actionArgs : [], g = p[0]?.id,
|
|
586
|
-
te(y.fields).filter((
|
|
587
|
-
),
|
|
588
|
-
(
|
|
589
|
-
),
|
|
590
|
-
const
|
|
591
|
-
for (const [d, m] of Object.entries(
|
|
581
|
+
const f = _(y.name, e.tables), s = () => {
|
|
582
|
+
const c = Y(y);
|
|
583
|
+
if (!c) throw ie(String(h.doctype));
|
|
584
|
+
return b(c.fieldname);
|
|
585
|
+
}, p = Array.isArray(h.actionArgs) ? h.actionArgs : [], g = p[0]?.id, C = p[0]?.data ?? {}, U = e.actionHandlers?.[y.name]?.[String(h.action)], x = new Map(
|
|
586
|
+
te(y.fields).filter((c) => b(c.fieldname) !== V).map((c) => [c.fieldname, c])
|
|
587
|
+
), L = te(y.fields).some(
|
|
588
|
+
(c) => b(c.fieldname) === V
|
|
589
|
+
), N = [], B = (c) => {
|
|
590
|
+
const R = [], S = [];
|
|
591
|
+
for (const [d, m] of Object.entries(c)) {
|
|
592
592
|
if (!x.has(d)) {
|
|
593
|
-
|
|
593
|
+
N.push(d);
|
|
594
594
|
continue;
|
|
595
595
|
}
|
|
596
596
|
if (m !== null && typeof m == "object") {
|
|
597
|
-
|
|
597
|
+
N.push(d);
|
|
598
598
|
continue;
|
|
599
599
|
}
|
|
600
|
-
|
|
600
|
+
R.push(b(d)), S.push(m);
|
|
601
601
|
}
|
|
602
|
-
return { cols:
|
|
602
|
+
return { cols: R, values: S };
|
|
603
603
|
};
|
|
604
604
|
try {
|
|
605
|
-
return await
|
|
606
|
-
const
|
|
605
|
+
return await E.withTransaction(async (c) => {
|
|
606
|
+
const R = await _e(
|
|
607
607
|
w,
|
|
608
608
|
{
|
|
609
609
|
readState: async () => {
|
|
610
610
|
if (g == null) return null;
|
|
611
|
-
const S =
|
|
611
|
+
const S = L ? `"${V}"` : `"${s()}"`, { rows: d } = await t(c, {
|
|
612
612
|
text: `SELECT ${S} FROM ${f} WHERE "${s()}"::text = $1 FOR UPDATE`,
|
|
613
613
|
values: [String(g)]
|
|
614
614
|
});
|
|
615
615
|
if (d.length === 0) return null;
|
|
616
|
-
if (
|
|
617
|
-
return d[0][
|
|
616
|
+
if (L)
|
|
617
|
+
return d[0][V] ?? void 0;
|
|
618
618
|
},
|
|
619
619
|
writeState: async (S) => {
|
|
620
|
-
await t(
|
|
621
|
-
text: `UPDATE ${f} SET "${
|
|
620
|
+
await t(c, {
|
|
621
|
+
text: `UPDATE ${f} SET "${V}" = $1 WHERE "${s()}"::text = $2`,
|
|
622
622
|
values: [S, String(g)]
|
|
623
623
|
});
|
|
624
624
|
},
|
|
@@ -632,35 +632,35 @@ const pe = "__stonecropLinkFk_", Q = "status", it = (e = {}) => {
|
|
|
632
632
|
// created record nowhere — which is that exact bug, already once
|
|
633
633
|
// shipped and patched downstream.
|
|
634
634
|
writeData: async (S, d) => {
|
|
635
|
-
const { cols: m, values: M } =
|
|
635
|
+
const { cols: m, values: M } = B(S), l = ee(y);
|
|
636
636
|
if (!d) {
|
|
637
637
|
if (g != null)
|
|
638
638
|
throw new Error(
|
|
639
639
|
`Action "${w.label ?? h.action}" targets ${y.name} "${String(g)}", which no longer exists. Nothing was created — a record dispatched with an identity is an update, and creating one is what dispatching no identity means.`
|
|
640
640
|
);
|
|
641
|
-
const A = m.length ? `INSERT INTO ${f} (${m.map((
|
|
641
|
+
const A = m.length ? `INSERT INTO ${f} (${m.map((H) => `"${H}"`).join(", ")}) VALUES (${m.map((H, P) => `$${P + 1}`).join(", ")}) RETURNING ${l}` : `INSERT INTO ${f} DEFAULT VALUES RETURNING ${l}`, { rows: F } = await t(c, { text: A, values: M });
|
|
642
642
|
return F[0] ?? {};
|
|
643
643
|
}
|
|
644
644
|
if (m.length === 0) {
|
|
645
|
-
const { rows: A } = await t(
|
|
646
|
-
text: `SELECT ${
|
|
645
|
+
const { rows: A } = await t(c, {
|
|
646
|
+
text: `SELECT ${l} FROM ${f} WHERE "${s()}"::text = $1`,
|
|
647
647
|
values: [String(g)]
|
|
648
648
|
});
|
|
649
649
|
return A[0] ?? {};
|
|
650
650
|
}
|
|
651
|
-
const
|
|
652
|
-
text: `UPDATE ${f} SET ${
|
|
651
|
+
const v = m.map((A, F) => `"${A}" = $${F + 1}`).join(", "), { rows: O } = await t(c, {
|
|
652
|
+
text: `UPDATE ${f} SET ${v} WHERE "${s()}"::text = $${m.length + 1} RETURNING ${l}`,
|
|
653
653
|
values: [...M, String(g)]
|
|
654
654
|
});
|
|
655
655
|
return O[0] ?? {};
|
|
656
656
|
},
|
|
657
|
-
runEffect:
|
|
658
|
-
pgClient:
|
|
657
|
+
runEffect: U ? (S) => U({
|
|
658
|
+
pgClient: c,
|
|
659
659
|
doctype: String(h.doctype),
|
|
660
660
|
action: String(h.action),
|
|
661
661
|
meta: y,
|
|
662
662
|
recordId: g,
|
|
663
|
-
data:
|
|
663
|
+
data: C,
|
|
664
664
|
args: p,
|
|
665
665
|
currentState: S
|
|
666
666
|
}) : void 0
|
|
@@ -668,16 +668,16 @@ const pe = "__stonecropLinkFk_", Q = "status", it = (e = {}) => {
|
|
|
668
668
|
// The patch a self-transition persists. This adapter never passed it, which
|
|
669
669
|
// was moot while it had no `writeData` — nothing consumed it. Wiring the write
|
|
670
670
|
// without this argument saves an empty record and reports success.
|
|
671
|
-
|
|
671
|
+
C
|
|
672
672
|
);
|
|
673
|
-
if (!
|
|
674
|
-
return
|
|
673
|
+
if (!R.success) throw new he(R);
|
|
674
|
+
return N.length ? { ...R, droppedFields: N } : R;
|
|
675
675
|
});
|
|
676
|
-
} catch (
|
|
677
|
-
return
|
|
676
|
+
} catch (c) {
|
|
677
|
+
return c instanceof he ? c.outcome : {
|
|
678
678
|
success: !1,
|
|
679
679
|
data: null,
|
|
680
|
-
error:
|
|
680
|
+
error: c instanceof Error ? c.message : String(c)
|
|
681
681
|
};
|
|
682
682
|
}
|
|
683
683
|
}
|
|
@@ -689,12 +689,12 @@ const pe = "__stonecropLinkFk_", Q = "status", it = (e = {}) => {
|
|
|
689
689
|
};
|
|
690
690
|
});
|
|
691
691
|
};
|
|
692
|
-
function
|
|
692
|
+
function qe(e, t) {
|
|
693
693
|
const o = [];
|
|
694
694
|
for (const n of te(e)) {
|
|
695
695
|
const r = t.get(n.fieldname);
|
|
696
696
|
if (r && ae(r, n.component) !== "inline") continue;
|
|
697
|
-
const i =
|
|
697
|
+
const i = b(n.fieldname);
|
|
698
698
|
o.push(i !== n.fieldname ? `"${i}" AS "${n.fieldname}"` : `"${n.fieldname}"`);
|
|
699
699
|
}
|
|
700
700
|
return o;
|
|
@@ -704,16 +704,16 @@ function ee(e) {
|
|
|
704
704
|
if (e.links)
|
|
705
705
|
for (const [o, n] of Object.entries(e.links))
|
|
706
706
|
t.set(n.fieldname ?? o, n);
|
|
707
|
-
return
|
|
707
|
+
return qe(e.fields, t).join(", ");
|
|
708
708
|
}
|
|
709
|
-
function
|
|
710
|
-
const t =
|
|
711
|
-
return e.fields.find((o) => o.kind === "field" && o.fieldname === t);
|
|
709
|
+
function Y(e) {
|
|
710
|
+
const t = Se(e.fields);
|
|
711
|
+
return K(e.fields).find((o) => o.kind === "field" && o.fieldname === t);
|
|
712
712
|
}
|
|
713
|
-
function
|
|
713
|
+
function Ue(e) {
|
|
714
714
|
const t = Object.entries(e);
|
|
715
715
|
if (t.length === 0) return;
|
|
716
|
-
if (
|
|
716
|
+
if ($e().length === 0) {
|
|
717
717
|
console.warn(
|
|
718
718
|
"[@stonecrop/graphql-middleware] Schema built with no doctypes registered, so the `actionHandlers` map could not be verified. Load doctypes before building the schema to have stale handler keys reported."
|
|
719
719
|
);
|
|
@@ -721,7 +721,7 @@ function qe(e) {
|
|
|
721
721
|
}
|
|
722
722
|
const o = [];
|
|
723
723
|
for (const [n, r] of t) {
|
|
724
|
-
const i =
|
|
724
|
+
const i = T(n);
|
|
725
725
|
if (!i) {
|
|
726
726
|
o.push(`"${n}": no doctype by that name is registered`);
|
|
727
727
|
continue;
|
|
@@ -743,8 +743,8 @@ function qe(e) {
|
|
|
743
743
|
`)}`
|
|
744
744
|
);
|
|
745
745
|
}
|
|
746
|
-
function
|
|
747
|
-
const e =
|
|
746
|
+
function Je() {
|
|
747
|
+
const e = Pe();
|
|
748
748
|
if (e.length !== 0)
|
|
749
749
|
throw new Error(
|
|
750
750
|
`StonecropPlugin: ${e.length} doctype reference${e.length === 1 ? "" : "s"} cannot be resolved and would silently drop the relation:
|
|
@@ -757,7 +757,7 @@ function ie(e) {
|
|
|
757
757
|
`Doctype "${e}" has no identity field: it declares no field with \`primaryKey: true\` and no field named \`id\`. Declare one of the two — records cannot be fetched or acted on until then.`
|
|
758
758
|
);
|
|
759
759
|
}
|
|
760
|
-
const
|
|
760
|
+
const ct = (e = {}) => {
|
|
761
761
|
const { logPlans: t = !0, logTiming: o = !1 } = e, n = (r, i) => ({
|
|
762
762
|
plan: (a) => {
|
|
763
763
|
t && console.log(`[@stonecrop/graphql-middleware] Building plan for ${r}.${i}`);
|
|
@@ -767,7 +767,7 @@ const at = (e = {}) => {
|
|
|
767
767
|
), $;
|
|
768
768
|
}
|
|
769
769
|
});
|
|
770
|
-
return
|
|
770
|
+
return Ie({
|
|
771
771
|
Query: {
|
|
772
772
|
stonecropRecord: n("Query", "stonecropRecord"),
|
|
773
773
|
stonecropRecords: n("Query", "stonecropRecords"),
|
|
@@ -778,7 +778,40 @@ const at = (e = {}) => {
|
|
|
778
778
|
stonecropAction: n("Mutation", "stonecropAction")
|
|
779
779
|
}
|
|
780
780
|
});
|
|
781
|
-
},
|
|
781
|
+
}, ge = {
|
|
782
|
+
name: "StonecropNaturalIdPlugin",
|
|
783
|
+
version: "0.0.0",
|
|
784
|
+
after: ["PgAttributesPlugin"],
|
|
785
|
+
inflection: {
|
|
786
|
+
replace: {
|
|
787
|
+
// Move Relay's global identifier off `id`...
|
|
788
|
+
nodeIdFieldName() {
|
|
789
|
+
return "nodeId";
|
|
790
|
+
},
|
|
791
|
+
// ...so the real column can keep its own name.
|
|
792
|
+
_attributeName(e, t, o) {
|
|
793
|
+
const n = e?.(o) ?? o.attributeName;
|
|
794
|
+
if (!o.skipRowId && n === "row_id") {
|
|
795
|
+
const { codec: r, attributeName: i } = o, a = r.attributes[i]?.extensions?.tags?.name ?? i;
|
|
796
|
+
if (typeof a == "string" && a.toLowerCase() === "id" && !r.isAnonymous)
|
|
797
|
+
return "id";
|
|
798
|
+
}
|
|
799
|
+
return n;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}, me = {
|
|
804
|
+
name: "StonecropOwnedRelationsPlugin",
|
|
805
|
+
version: "0.0.0",
|
|
806
|
+
gather: {
|
|
807
|
+
hooks: {
|
|
808
|
+
pgRelations_relation(e, t) {
|
|
809
|
+
const { relation: o, pgConstraint: n } = t;
|
|
810
|
+
o.isReferencee && n.confdeltype === "c" && (o.extensions ??= {}, o.extensions.tags ??= {}, o.extensions.tags.behavior = "+manyRelation:resource:connection");
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
}, Ke = {
|
|
782
815
|
name: "StonecropFieldCasingPlugin",
|
|
783
816
|
version: "0.0.0",
|
|
784
817
|
inflection: {
|
|
@@ -788,27 +821,27 @@ const at = (e = {}) => {
|
|
|
788
821
|
}
|
|
789
822
|
}
|
|
790
823
|
}
|
|
791
|
-
},
|
|
792
|
-
extends: [
|
|
793
|
-
plugins: e?.fieldCasing === "pascal" ? [
|
|
794
|
-
}),
|
|
824
|
+
}, Ge = (e) => ({
|
|
825
|
+
extends: [De],
|
|
826
|
+
plugins: e?.fieldCasing === "pascal" ? [ge, me, Ke] : [ge, me]
|
|
827
|
+
}), lt = Ge();
|
|
795
828
|
export {
|
|
796
829
|
se as DoctypeValidationError,
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
830
|
+
lt as StonecropPreset,
|
|
831
|
+
_e as applyGuardedTransition,
|
|
832
|
+
at as clearFetchHandlers,
|
|
833
|
+
rt as clearRegistry,
|
|
834
|
+
ct as createDebugPlugin,
|
|
835
|
+
st as createStonecropPlugin,
|
|
836
|
+
Ge as createStonecropPreset,
|
|
837
|
+
$e as getAllMeta,
|
|
838
|
+
je as getFetchHandler,
|
|
839
|
+
T as getMeta,
|
|
840
|
+
ot as hasMeta,
|
|
841
|
+
Ce as loadDoctypes,
|
|
842
|
+
nt as loadDoctypesFromObject,
|
|
843
|
+
ft as makePgService,
|
|
844
|
+
it as registerFetchHandler,
|
|
845
|
+
He as typeDefs
|
|
813
846
|
};
|
|
814
847
|
//# sourceMappingURL=index.js.map
|