@stonecrop/graphql-middleware 0.24.0 → 0.25.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/dist/index.js +246 -242
- package/dist/index.js.map +1 -1
- package/dist/src/plugin/postgraphile.d.ts.map +1 -1
- package/dist/src/plugin/postgraphile.js +25 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { readdirSync as
|
|
2
|
-
import { join as
|
|
3
|
-
import { flattenFields as
|
|
4
|
-
import { sideEffectWithPgClient as
|
|
5
|
-
import { object as
|
|
6
|
-
import { gql as
|
|
7
|
-
import { PostGraphileAmberPreset as
|
|
8
|
-
import { makePgService as
|
|
9
|
-
function
|
|
10
|
-
return
|
|
1
|
+
import { readdirSync as $e, statSync as ke, readFileSync as Ee } from "node:fs";
|
|
2
|
+
import { join as be } from "node:path";
|
|
3
|
+
import { flattenFields as Y, validateDoctype as ge, parseDoctype as me, isActionAllowedInState as ve, pascalToSnake as Re, camelToSnake as E, getDisplayField as Ae, getRecordIdField as ye, resolveLinkRenderMode as ae } from "@stonecrop/schema";
|
|
4
|
+
import { sideEffectWithPgClient as Me, loadOneWithPgClient as ue } from "@dataplan/pg";
|
|
5
|
+
import { object as re, constant as Oe, lambda as xe } from "postgraphile/grafast";
|
|
6
|
+
import { gql as Fe, extendSchema as Te, wrapPlans as Le } from "postgraphile/utils";
|
|
7
|
+
import { PostGraphileAmberPreset as Ne } from "postgraphile/presets/amber";
|
|
8
|
+
import { makePgService as dt } from "postgraphile/adaptors/pg";
|
|
9
|
+
function te(e) {
|
|
10
|
+
return Y(e).filter((t) => t.kind === "field" && !t.computed);
|
|
11
11
|
}
|
|
12
|
-
const
|
|
13
|
-
class
|
|
12
|
+
const H = /* @__PURE__ */ new Map();
|
|
13
|
+
class se extends Error {
|
|
14
14
|
constructor(t, o) {
|
|
15
15
|
const n = o.map((r) => ` ${r.path.join(".")}: ${r.message}`).join(`
|
|
16
16
|
`);
|
|
@@ -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 De(e, t = {}) {
|
|
24
|
+
const o = $e(e);
|
|
25
25
|
for (const n of o) {
|
|
26
|
-
const r =
|
|
27
|
-
|
|
26
|
+
const r = be(e, n);
|
|
27
|
+
ke(r).isDirectory() ? De(r, t) : n.endsWith(".json") && Be(r, t);
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
-
function
|
|
31
|
-
const o =
|
|
30
|
+
function Be(e, t) {
|
|
31
|
+
const o = Ee(e, "utf-8");
|
|
32
32
|
let n;
|
|
33
33
|
try {
|
|
34
34
|
n = JSON.parse(o);
|
|
@@ -41,56 +41,56 @@ function De(e, t) {
|
|
|
41
41
|
t.onError?.(e, [u]);
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
44
|
-
throw new
|
|
44
|
+
throw new se(e, [u]);
|
|
45
45
|
}
|
|
46
|
-
const r =
|
|
46
|
+
const r = ge(n);
|
|
47
47
|
if (!r.success) {
|
|
48
48
|
if (t.continueOnError) {
|
|
49
49
|
t.onError?.(e, r.errors);
|
|
50
50
|
return;
|
|
51
51
|
}
|
|
52
|
-
throw new
|
|
52
|
+
throw new se(e, r.errors);
|
|
53
53
|
}
|
|
54
|
-
const i =
|
|
55
|
-
|
|
54
|
+
const i = me(n);
|
|
55
|
+
H.set(i.name, i);
|
|
56
56
|
}
|
|
57
|
-
function
|
|
57
|
+
function et(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 = ge(r);
|
|
60
60
|
if (!i.success) {
|
|
61
61
|
if (t.continueOnError) {
|
|
62
62
|
t.onError?.(o, i.errors);
|
|
63
63
|
continue;
|
|
64
64
|
}
|
|
65
|
-
throw new
|
|
65
|
+
throw new se(o, i.errors);
|
|
66
66
|
}
|
|
67
|
-
const a =
|
|
68
|
-
|
|
67
|
+
const a = me(r);
|
|
68
|
+
H.set(a.name, a);
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
function L(e) {
|
|
72
|
-
const t =
|
|
72
|
+
const t = H.get(e);
|
|
73
73
|
if (t) return t;
|
|
74
|
-
for (const o of
|
|
74
|
+
for (const o of H.values())
|
|
75
75
|
if (o.slug === e) return o;
|
|
76
76
|
}
|
|
77
|
-
function
|
|
78
|
-
return Array.from(
|
|
77
|
+
function we() {
|
|
78
|
+
return Array.from(H.values());
|
|
79
79
|
}
|
|
80
|
-
function
|
|
81
|
-
return
|
|
80
|
+
function tt(e) {
|
|
81
|
+
return H.has(e);
|
|
82
82
|
}
|
|
83
|
-
function
|
|
84
|
-
|
|
83
|
+
function nt() {
|
|
84
|
+
H.clear();
|
|
85
85
|
}
|
|
86
86
|
function Ie() {
|
|
87
87
|
const e = [];
|
|
88
|
-
for (const t of
|
|
88
|
+
for (const t of H.values()) {
|
|
89
89
|
t.inherits && L(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 = Y(t.fields);
|
|
94
94
|
for (const n of o) {
|
|
95
95
|
if (n.kind !== "field") continue;
|
|
96
96
|
const r = n.doctype;
|
|
@@ -100,7 +100,7 @@ function Ie() {
|
|
|
100
100
|
});
|
|
101
101
|
}
|
|
102
102
|
if (t.links) {
|
|
103
|
-
const n = new Set(o.map((i) => i.fieldname)), r = new Set(
|
|
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
105
|
if (L(a.target) === void 0 && e.push({
|
|
106
106
|
path: [t.name, "links", i, "target"],
|
|
@@ -129,15 +129,15 @@ function Ie() {
|
|
|
129
129
|
}
|
|
130
130
|
return e;
|
|
131
131
|
}
|
|
132
|
-
const
|
|
133
|
-
function
|
|
134
|
-
|
|
132
|
+
const le = /* @__PURE__ */ new Map();
|
|
133
|
+
function ot(e, t) {
|
|
134
|
+
le.set(e, t);
|
|
135
135
|
}
|
|
136
|
-
function
|
|
137
|
-
return
|
|
136
|
+
function Ce(e) {
|
|
137
|
+
return le.get(e);
|
|
138
138
|
}
|
|
139
|
-
function
|
|
140
|
-
|
|
139
|
+
function rt() {
|
|
140
|
+
le.clear();
|
|
141
141
|
}
|
|
142
142
|
async function je(e, t, o) {
|
|
143
143
|
const n = e.label ?? "unknown", r = e.selfTransition === !0, i = r || e.nextState != null;
|
|
@@ -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 b = await t.writeData(o ?? {}, !1);
|
|
163
|
+
return !b || Object.keys(b).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: b, 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 (!ve(e, u ?? ""))
|
|
178
178
|
return {
|
|
179
179
|
success: !1,
|
|
180
180
|
data: null,
|
|
@@ -197,53 +197,53 @@ async function je(e, t, o) {
|
|
|
197
197
|
error: null
|
|
198
198
|
});
|
|
199
199
|
}
|
|
200
|
-
function
|
|
201
|
-
const o = t?.[e] ??
|
|
200
|
+
function W(e, t) {
|
|
201
|
+
const o = t?.[e] ?? Re(e), n = o.indexOf(".");
|
|
202
202
|
return n > 0 ? `"${o.slice(0, n)}"."${o.slice(n + 1)}"` : `"${o}"`;
|
|
203
203
|
}
|
|
204
|
-
function
|
|
204
|
+
function Z(e) {
|
|
205
205
|
if (!(e == null || typeof e == "object")) {
|
|
206
206
|
if (typeof e == "string") return e === "" ? void 0 : e;
|
|
207
207
|
if (typeof e == "number" || typeof e == "boolean" || typeof e == "bigint")
|
|
208
208
|
return String(e);
|
|
209
209
|
}
|
|
210
210
|
}
|
|
211
|
-
function
|
|
211
|
+
function Pe(e, t) {
|
|
212
212
|
const o = [];
|
|
213
|
-
for (const n of
|
|
213
|
+
for (const n of Y(e.fields)) {
|
|
214
214
|
if (n.kind !== "field" || !n.doctype) continue;
|
|
215
215
|
const r = L(n.doctype);
|
|
216
216
|
if (!r) continue;
|
|
217
|
-
const i =
|
|
217
|
+
const i = Ae(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: ye(r.fields),
|
|
222
|
+
targetTable: W(r.name, t)
|
|
223
223
|
});
|
|
224
224
|
}
|
|
225
225
|
return o;
|
|
226
226
|
}
|
|
227
|
-
async function
|
|
228
|
-
const i =
|
|
227
|
+
async function fe(e, t, o, n, r) {
|
|
228
|
+
const i = Pe(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();
|
|
232
232
|
for (const w of o) {
|
|
233
|
-
const f =
|
|
233
|
+
const f = Z(w[a.fieldname]);
|
|
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 $ = E(a.targetPkFieldname), k = E(a.displayField), b = k !== a.displayField ? `"${k}" AS "${a.displayField}"` : `"${a.displayField}"`, { rows: h } = await r(e, {
|
|
238
|
+
text: `SELECT "${$}" AS "${a.targetPkFieldname}", ${b} 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) {
|
|
242
|
-
const f =
|
|
242
|
+
const f = Z(w[a.targetPkFieldname]), s = Z(w[a.displayField]);
|
|
243
243
|
f === void 0 || s === void 0 || y.set(f, s);
|
|
244
244
|
}
|
|
245
245
|
for (const w of o) {
|
|
246
|
-
const f = w[a.fieldname], s =
|
|
246
|
+
const f = w[a.fieldname], s = Z(f);
|
|
247
247
|
if (s === void 0) continue;
|
|
248
248
|
const p = y.get(s);
|
|
249
249
|
p !== void 0 && (w[a.fieldname] = { id: f, displayText: p });
|
|
@@ -251,7 +251,7 @@ async function ue(e, t, o, n, r) {
|
|
|
251
251
|
})
|
|
252
252
|
);
|
|
253
253
|
}
|
|
254
|
-
const
|
|
254
|
+
const We = Fe`
|
|
255
255
|
type StonecropFieldMeta {
|
|
256
256
|
kind: String!
|
|
257
257
|
fieldname: String!
|
|
@@ -362,34 +362,34 @@ const Pe = xe`
|
|
|
362
362
|
stonecropAction(doctype: String!, action: String!, args: JSON): StonecropActionResult!
|
|
363
363
|
}
|
|
364
364
|
`;
|
|
365
|
-
class
|
|
365
|
+
class he extends Error {
|
|
366
366
|
constructor(t) {
|
|
367
367
|
super(t.error ?? "Action failed"), this.outcome = t, this.name = "ActionRolledBack";
|
|
368
368
|
}
|
|
369
369
|
outcome;
|
|
370
370
|
}
|
|
371
|
-
const
|
|
371
|
+
const pe = "__stonecropLinkFk_", Q = "status", it = (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 Te((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 && qe(e.actionHandlers), Ue(), {
|
|
378
|
+
typeDefs: We,
|
|
379
379
|
objects: {
|
|
380
380
|
Query: {
|
|
381
381
|
plans: {
|
|
382
382
|
stonecropMeta(a, { $doctype: u }) {
|
|
383
|
-
return
|
|
383
|
+
return xe(u, ($) => L(String($)) ?? null);
|
|
384
384
|
},
|
|
385
385
|
stonecropAllMeta() {
|
|
386
|
-
return
|
|
386
|
+
return Oe(we());
|
|
387
387
|
},
|
|
388
388
|
stonecropRecord(a, { $doctype: u, $id: $, $options: k }) {
|
|
389
|
-
return
|
|
389
|
+
return ue(
|
|
390
390
|
i,
|
|
391
|
-
|
|
392
|
-
async (
|
|
391
|
+
re({ doctype: u, id: $, options: k }),
|
|
392
|
+
async (b, 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);
|
|
@@ -399,154 +399,158 @@ const he = "__stonecropLinkFk_", V = "status", rt = (e = {}) => {
|
|
|
399
399
|
for (const [f, s] of y) {
|
|
400
400
|
const p = L(f);
|
|
401
401
|
if (!p) {
|
|
402
|
-
for (const
|
|
402
|
+
for (const d of s) w[d] = { data: null, doctype: f };
|
|
403
403
|
continue;
|
|
404
404
|
}
|
|
405
|
-
const
|
|
406
|
-
if (!
|
|
407
|
-
const I =
|
|
408
|
-
for (const [
|
|
409
|
-
if (
|
|
410
|
-
const M =
|
|
411
|
-
|
|
405
|
+
const g = V(p);
|
|
406
|
+
if (!g) throw ie(f);
|
|
407
|
+
const I = E(g.fieldname), _ = ee(p), x = s.map((d) => String(h[d].id)), N = new Map(Y(p.fields).map((d) => [d.fieldname, d])), T = [];
|
|
408
|
+
for (const [d, m] of Object.entries(p.links ?? {})) {
|
|
409
|
+
if (m.cardinality === "noneOrMany" || m.cardinality === "atLeastOne") continue;
|
|
410
|
+
const M = m.fieldname ?? d;
|
|
411
|
+
ae(m, N.get(M)?.component) !== "inline" && T.push({ fieldname: M, alias: `${pe}${M}` });
|
|
412
412
|
}
|
|
413
|
-
const
|
|
414
|
-
|
|
415
|
-
...T.map(({ fieldname:
|
|
416
|
-
].filter(Boolean).join(", "), { rows: l } = await t(
|
|
417
|
-
text: `SELECT ${
|
|
413
|
+
const C = [
|
|
414
|
+
_,
|
|
415
|
+
...T.map(({ fieldname: d, alias: m }) => `"${E(d)}" AS "${m}"`)
|
|
416
|
+
].filter(Boolean).join(", "), { rows: l } = await t(b, {
|
|
417
|
+
text: `SELECT ${C} FROM ${W(p.name, e.tables)} WHERE "${I}"::text = ANY($1::text[])`,
|
|
418
418
|
values: [x]
|
|
419
|
-
}),
|
|
420
|
-
if (l.length !==
|
|
419
|
+
}), v = new Map(l.map((d) => [String(d[g.fieldname]), d]));
|
|
420
|
+
if (l.length !== v.size)
|
|
421
421
|
throw new Error(
|
|
422
|
-
`Doctype "${f}" declares "${
|
|
422
|
+
`Doctype "${f}" declares "${g.fieldname}" as its identity, but that column is not unique in ${W(p.name, e.tables)} — ${l.length} rows matched ${v.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
|
-
for (const
|
|
426
|
-
const
|
|
425
|
+
for (const d of s) {
|
|
426
|
+
const m = String(h[d].id), M = v.get(m);
|
|
427
427
|
if (!M) {
|
|
428
|
-
w[
|
|
428
|
+
w[d] = { data: null, doctype: f };
|
|
429
429
|
continue;
|
|
430
430
|
}
|
|
431
|
-
const
|
|
432
|
-
S.push(
|
|
433
|
-
const
|
|
431
|
+
const c = { ...M };
|
|
432
|
+
S.push(c);
|
|
433
|
+
const R = [], 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 = K.handler,
|
|
440
|
-
|
|
435
|
+
for (const [j, D] of Object.entries(p.links)) {
|
|
436
|
+
const P = D.fieldname ?? j, K = D.fetch, ne = D.cardinality === "noneOrMany" || D.cardinality === "atLeastOne", ce = K?.method ?? (ne ? "sync" : "lazy"), Se = K?.method === "sync" ? K.limit : void 0, G = ne ? Se ?? o : void 0;
|
|
437
|
+
if (!(A || (F ? F.has(j) : ce === "sync"))) continue;
|
|
438
|
+
if (ce === "custom" && K?.method === "custom") {
|
|
439
|
+
const J = K.handler, B = Ce(J);
|
|
440
|
+
B && (c[P] = await B(b, c, D));
|
|
441
441
|
continue;
|
|
442
442
|
}
|
|
443
|
-
if (
|
|
444
|
-
const
|
|
445
|
-
if (!
|
|
446
|
-
const
|
|
447
|
-
if (
|
|
443
|
+
if (ae(D, N.get(P)?.component) === "inline") continue;
|
|
444
|
+
const U = L(D.target);
|
|
445
|
+
if (!U) continue;
|
|
446
|
+
const de = ee(U);
|
|
447
|
+
if (ne) {
|
|
448
448
|
if (!D.backlink) continue;
|
|
449
|
-
const J =
|
|
450
|
-
let
|
|
451
|
-
const
|
|
452
|
-
G != null && (
|
|
453
|
-
const { rows:
|
|
454
|
-
text:
|
|
455
|
-
values:
|
|
449
|
+
const J = E(D.backlink);
|
|
450
|
+
let B = `SELECT ${de} FROM ${W(U.name, e.tables)} WHERE "${J}"::text = $1`;
|
|
451
|
+
const z = [m], X = V(U);
|
|
452
|
+
X && (B += ` ORDER BY "${E(X.fieldname)}" ASC`), G != null && (B += " LIMIT $2", z.push(G + 1));
|
|
453
|
+
const { rows: oe } = await t(b, {
|
|
454
|
+
text: B,
|
|
455
|
+
values: z
|
|
456
456
|
});
|
|
457
|
-
G != null &&
|
|
457
|
+
G != null && oe.length > G ? (R.push(P), c[P] = oe.slice(0, G)) : c[P] = oe;
|
|
458
458
|
} else {
|
|
459
|
-
const J =
|
|
459
|
+
const J = c[`${pe}${P}`];
|
|
460
460
|
if (J == null) {
|
|
461
|
-
|
|
461
|
+
c[P] = null;
|
|
462
462
|
continue;
|
|
463
463
|
}
|
|
464
|
-
const
|
|
465
|
-
if (!
|
|
466
|
-
const
|
|
467
|
-
text: `SELECT ${
|
|
464
|
+
const B = V(U);
|
|
465
|
+
if (!B) throw ie(U.name);
|
|
466
|
+
const z = E(B.fieldname), { rows: X } = await t(b, {
|
|
467
|
+
text: `SELECT ${de} FROM ${W(U.name, e.tables)} WHERE "${z}" = $1`,
|
|
468
468
|
values: [J]
|
|
469
469
|
});
|
|
470
|
-
|
|
470
|
+
c[P] = X[0] ?? null;
|
|
471
471
|
}
|
|
472
472
|
}
|
|
473
|
-
for (const { alias:
|
|
474
|
-
const
|
|
475
|
-
w[
|
|
476
|
-
data:
|
|
473
|
+
for (const { alias: j } of T) delete c[j];
|
|
474
|
+
const q = F && p.links ? [...F].filter((j) => !(j in p.links)) : void 0;
|
|
475
|
+
w[d] = {
|
|
476
|
+
data: c,
|
|
477
477
|
doctype: f,
|
|
478
|
-
unknownLinks:
|
|
479
|
-
truncatedLinks:
|
|
478
|
+
unknownLinks: q?.length ? q : void 0,
|
|
479
|
+
truncatedLinks: R.length ? R : void 0
|
|
480
480
|
};
|
|
481
481
|
}
|
|
482
|
-
await
|
|
482
|
+
await fe(b, 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:
|
|
489
|
-
return
|
|
488
|
+
stonecropRecords(a, { $doctype: u, $filters: $, $orderBy: k, $limit: b, $offset: h, $includeTotal: y }) {
|
|
489
|
+
return ue(
|
|
490
490
|
i,
|
|
491
|
-
|
|
491
|
+
re({
|
|
492
492
|
doctype: u,
|
|
493
493
|
filters: $,
|
|
494
494
|
orderBy: k,
|
|
495
|
-
limit:
|
|
495
|
+
limit: b,
|
|
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),
|
|
502
|
-
if (!
|
|
501
|
+
const p = String(s.doctype), g = L(p);
|
|
502
|
+
if (!g)
|
|
503
503
|
return { data: [], doctype: p, hasMore: !1, count: s.includeTotal === !0 ? 0 : null };
|
|
504
|
-
const I = new Set(
|
|
504
|
+
const I = new Set(Y(g.fields).map((c) => c.fieldname)), _ = ee(g), x = [], N = [];
|
|
505
505
|
if (s.filters != null)
|
|
506
|
-
for (const [
|
|
507
|
-
if (!I.has(
|
|
508
|
-
throw new Error(`Unknown filter field: ${
|
|
509
|
-
x.push(
|
|
506
|
+
for (const [c, R] of Object.entries(s.filters)) {
|
|
507
|
+
if (!I.has(c))
|
|
508
|
+
throw new Error(`Unknown filter field: ${c} for doctype ${g.name}`);
|
|
509
|
+
x.push(R), N.push(`"${E(c)}" = $${x.length}`);
|
|
510
510
|
}
|
|
511
511
|
const T = N.length > 0 ? ` WHERE ${N.join(" AND ")}` : "";
|
|
512
|
-
let
|
|
512
|
+
let C = "";
|
|
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 c = s.orderBy, R = c.lastIndexOf("_");
|
|
517
|
+
if (R <= 0)
|
|
518
|
+
throw new Error(`Invalid orderBy format: "${c}". Expected FIELD_ASC or FIELD_DESC`);
|
|
519
|
+
const O = c.slice(R + 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 =
|
|
522
|
+
const A = c.slice(0, R);
|
|
523
523
|
if (!I.has(A))
|
|
524
|
-
throw new Error(`Unknown orderBy field: "${A}" for doctype ${
|
|
525
|
-
|
|
524
|
+
throw new Error(`Unknown orderBy field: "${A}" for doctype ${g.name}`);
|
|
525
|
+
C = ` ORDER BY "${E(A)}" ${O}`;
|
|
526
|
+
}
|
|
527
|
+
if (C === "") {
|
|
528
|
+
const c = V(g);
|
|
529
|
+
c && (C = ` ORDER BY "${E(c.fieldname)}" ASC`);
|
|
526
530
|
}
|
|
527
531
|
if (s.limit != null && typeof s.limit != "number")
|
|
528
532
|
throw new Error(`Invalid limit: expected number, got ${typeof s.limit}`);
|
|
529
533
|
const l = s.limit ?? o;
|
|
530
|
-
let
|
|
531
|
-
l != null && (x.push(l + 1),
|
|
534
|
+
let v = "";
|
|
535
|
+
l != null && (x.push(l + 1), v += ` LIMIT $${x.length}`), s.offset != null && (x.push(s.offset), v += ` OFFSET $${x.length}`);
|
|
532
536
|
const { rows: S } = await t(w, {
|
|
533
|
-
text: `SELECT ${
|
|
537
|
+
text: `SELECT ${_} FROM ${W(g.name, e.tables)}${T}${C}${v}`,
|
|
534
538
|
values: x
|
|
535
|
-
}),
|
|
536
|
-
await
|
|
539
|
+
}), d = l != null && S.length > l, m = [...d ? S.slice(0, l ?? S.length) : S];
|
|
540
|
+
await fe(w, g, m, e.tables, t);
|
|
537
541
|
let M = null;
|
|
538
542
|
if (s.includeTotal === !0) {
|
|
539
|
-
const
|
|
543
|
+
const c = [], R = [];
|
|
540
544
|
if (s.filters != null)
|
|
541
|
-
for (const [F,
|
|
542
|
-
I.has(F) && (
|
|
543
|
-
const O =
|
|
544
|
-
text: `SELECT COUNT(*) AS row_count FROM ${
|
|
545
|
-
values:
|
|
545
|
+
for (const [F, q] of Object.entries(s.filters))
|
|
546
|
+
I.has(F) && (c.push(q), R.push(`"${E(F)}" = $${c.length}`));
|
|
547
|
+
const O = R.length > 0 ? ` WHERE ${R.join(" AND ")}` : "", { rows: A } = await t(w, {
|
|
548
|
+
text: `SELECT COUNT(*) AS row_count FROM ${W(g.name, e.tables)}${O}`,
|
|
549
|
+
values: c
|
|
546
550
|
});
|
|
547
551
|
M = parseInt(A[0]?.row_count ?? "0", 10);
|
|
548
552
|
}
|
|
549
|
-
return { data:
|
|
553
|
+
return { data: m, doctype: p, hasMore: d, count: M };
|
|
550
554
|
})
|
|
551
555
|
)
|
|
552
556
|
);
|
|
@@ -556,10 +560,10 @@ const he = "__stonecropLinkFk_", V = "status", rt = (e = {}) => {
|
|
|
556
560
|
Mutation: {
|
|
557
561
|
plans: {
|
|
558
562
|
stonecropAction(a, { $doctype: u, $action: $, $args: k }) {
|
|
559
|
-
return
|
|
563
|
+
return Me(
|
|
560
564
|
i,
|
|
561
|
-
|
|
562
|
-
async (
|
|
565
|
+
re({ doctype: u, action: $, actionArgs: k }),
|
|
566
|
+
async (b, h) => {
|
|
563
567
|
const y = L(String(h.doctype));
|
|
564
568
|
if (!y)
|
|
565
569
|
return {
|
|
@@ -574,48 +578,48 @@ const he = "__stonecropLinkFk_", V = "status", rt = (e = {}) => {
|
|
|
574
578
|
data: null,
|
|
575
579
|
error: `Unknown action: ${h.action} on ${h.doctype}`
|
|
576
580
|
};
|
|
577
|
-
const f =
|
|
578
|
-
const l =
|
|
579
|
-
if (!l) throw
|
|
580
|
-
return
|
|
581
|
-
}, p = Array.isArray(h.actionArgs) ? h.actionArgs : [],
|
|
582
|
-
|
|
583
|
-
), N =
|
|
584
|
-
(l) =>
|
|
585
|
-
), T = [],
|
|
586
|
-
const
|
|
587
|
-
for (const [
|
|
588
|
-
if (!x.has(
|
|
589
|
-
T.push(
|
|
581
|
+
const f = W(y.name, e.tables), s = () => {
|
|
582
|
+
const l = V(y);
|
|
583
|
+
if (!l) throw ie(String(h.doctype));
|
|
584
|
+
return E(l.fieldname);
|
|
585
|
+
}, p = Array.isArray(h.actionArgs) ? h.actionArgs : [], g = p[0]?.id, I = p[0]?.data ?? {}, _ = e.actionHandlers?.[y.name]?.[String(h.action)], x = new Map(
|
|
586
|
+
te(y.fields).filter((l) => E(l.fieldname) !== Q).map((l) => [l.fieldname, l])
|
|
587
|
+
), N = te(y.fields).some(
|
|
588
|
+
(l) => E(l.fieldname) === Q
|
|
589
|
+
), T = [], C = (l) => {
|
|
590
|
+
const v = [], S = [];
|
|
591
|
+
for (const [d, m] of Object.entries(l)) {
|
|
592
|
+
if (!x.has(d)) {
|
|
593
|
+
T.push(d);
|
|
590
594
|
continue;
|
|
591
595
|
}
|
|
592
|
-
if (
|
|
593
|
-
T.push(
|
|
596
|
+
if (m !== null && typeof m == "object") {
|
|
597
|
+
T.push(d);
|
|
594
598
|
continue;
|
|
595
599
|
}
|
|
596
|
-
|
|
600
|
+
v.push(E(d)), S.push(m);
|
|
597
601
|
}
|
|
598
|
-
return { cols:
|
|
602
|
+
return { cols: v, values: S };
|
|
599
603
|
};
|
|
600
604
|
try {
|
|
601
|
-
return await
|
|
602
|
-
const
|
|
605
|
+
return await b.withTransaction(async (l) => {
|
|
606
|
+
const v = await je(
|
|
603
607
|
w,
|
|
604
608
|
{
|
|
605
609
|
readState: async () => {
|
|
606
|
-
if (
|
|
607
|
-
const S = N ? `"${
|
|
610
|
+
if (g == null) return null;
|
|
611
|
+
const S = N ? `"${Q}"` : `"${s()}"`, { rows: d } = await t(l, {
|
|
608
612
|
text: `SELECT ${S} FROM ${f} WHERE "${s()}"::text = $1 FOR UPDATE`,
|
|
609
|
-
values: [String(
|
|
613
|
+
values: [String(g)]
|
|
610
614
|
});
|
|
611
|
-
if (
|
|
615
|
+
if (d.length === 0) return null;
|
|
612
616
|
if (N)
|
|
613
|
-
return
|
|
617
|
+
return d[0][Q] ?? void 0;
|
|
614
618
|
},
|
|
615
619
|
writeState: async (S) => {
|
|
616
620
|
await t(l, {
|
|
617
|
-
text: `UPDATE ${f} SET "${
|
|
618
|
-
values: [S, String(
|
|
621
|
+
text: `UPDATE ${f} SET "${Q}" = $1 WHERE "${s()}"::text = $2`,
|
|
622
|
+
values: [S, String(g)]
|
|
619
623
|
});
|
|
620
624
|
},
|
|
621
625
|
// The upsert, and the only write path for record data. `exists`
|
|
@@ -627,35 +631,35 @@ const he = "__stonecropLinkFk_", V = "status", rt = (e = {}) => {
|
|
|
627
631
|
// `itemId` would receive `item_id`, find no identity, and file a
|
|
628
632
|
// created record nowhere — which is that exact bug, already once
|
|
629
633
|
// shipped and patched downstream.
|
|
630
|
-
writeData: async (S,
|
|
631
|
-
const { cols:
|
|
632
|
-
if (!
|
|
633
|
-
if (
|
|
634
|
+
writeData: async (S, d) => {
|
|
635
|
+
const { cols: m, values: M } = C(S), c = ee(y);
|
|
636
|
+
if (!d) {
|
|
637
|
+
if (g != null)
|
|
634
638
|
throw new Error(
|
|
635
|
-
`Action "${w.label ?? h.action}" targets ${y.name} "${String(
|
|
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.`
|
|
636
640
|
);
|
|
637
|
-
const A =
|
|
641
|
+
const A = m.length ? `INSERT INTO ${f} (${m.map((q) => `"${q}"`).join(", ")}) VALUES (${m.map((q, j) => `$${j + 1}`).join(", ")}) RETURNING ${c}` : `INSERT INTO ${f} DEFAULT VALUES RETURNING ${c}`, { rows: F } = await t(l, { text: A, values: M });
|
|
638
642
|
return F[0] ?? {};
|
|
639
643
|
}
|
|
640
|
-
if (
|
|
644
|
+
if (m.length === 0) {
|
|
641
645
|
const { rows: A } = await t(l, {
|
|
642
|
-
text: `SELECT ${
|
|
643
|
-
values: [String(
|
|
646
|
+
text: `SELECT ${c} FROM ${f} WHERE "${s()}"::text = $1`,
|
|
647
|
+
values: [String(g)]
|
|
644
648
|
});
|
|
645
649
|
return A[0] ?? {};
|
|
646
650
|
}
|
|
647
|
-
const
|
|
648
|
-
text: `UPDATE ${f} SET ${
|
|
649
|
-
values: [...M, String(
|
|
651
|
+
const R = m.map((A, F) => `"${A}" = $${F + 1}`).join(", "), { rows: O } = await t(l, {
|
|
652
|
+
text: `UPDATE ${f} SET ${R} WHERE "${s()}"::text = $${m.length + 1} RETURNING ${c}`,
|
|
653
|
+
values: [...M, String(g)]
|
|
650
654
|
});
|
|
651
655
|
return O[0] ?? {};
|
|
652
656
|
},
|
|
653
|
-
runEffect:
|
|
657
|
+
runEffect: _ ? (S) => _({
|
|
654
658
|
pgClient: l,
|
|
655
659
|
doctype: String(h.doctype),
|
|
656
660
|
action: String(h.action),
|
|
657
661
|
meta: y,
|
|
658
|
-
recordId:
|
|
662
|
+
recordId: g,
|
|
659
663
|
data: I,
|
|
660
664
|
args: p,
|
|
661
665
|
currentState: S
|
|
@@ -666,11 +670,11 @@ const he = "__stonecropLinkFk_", V = "status", rt = (e = {}) => {
|
|
|
666
670
|
// without this argument saves an empty record and reports success.
|
|
667
671
|
I
|
|
668
672
|
);
|
|
669
|
-
if (!
|
|
670
|
-
return T.length ? { ...
|
|
673
|
+
if (!v.success) throw new he(v);
|
|
674
|
+
return T.length ? { ...v, droppedFields: T } : v;
|
|
671
675
|
});
|
|
672
676
|
} catch (l) {
|
|
673
|
-
return l instanceof
|
|
677
|
+
return l instanceof he ? l.outcome : {
|
|
674
678
|
success: !1,
|
|
675
679
|
data: null,
|
|
676
680
|
error: l instanceof Error ? l.message : String(l)
|
|
@@ -685,31 +689,31 @@ const he = "__stonecropLinkFk_", V = "status", rt = (e = {}) => {
|
|
|
685
689
|
};
|
|
686
690
|
});
|
|
687
691
|
};
|
|
688
|
-
function
|
|
692
|
+
function He(e, t) {
|
|
689
693
|
const o = [];
|
|
690
|
-
for (const n of
|
|
694
|
+
for (const n of te(e)) {
|
|
691
695
|
const r = t.get(n.fieldname);
|
|
692
|
-
if (r &&
|
|
693
|
-
const i =
|
|
696
|
+
if (r && ae(r, n.component) !== "inline") continue;
|
|
697
|
+
const i = E(n.fieldname);
|
|
694
698
|
o.push(i !== n.fieldname ? `"${i}" AS "${n.fieldname}"` : `"${n.fieldname}"`);
|
|
695
699
|
}
|
|
696
700
|
return o;
|
|
697
701
|
}
|
|
698
|
-
function
|
|
702
|
+
function ee(e) {
|
|
699
703
|
const t = /* @__PURE__ */ new Map();
|
|
700
704
|
if (e.links)
|
|
701
705
|
for (const [o, n] of Object.entries(e.links))
|
|
702
706
|
t.set(n.fieldname ?? o, n);
|
|
703
|
-
return
|
|
707
|
+
return He(e.fields, t).join(", ");
|
|
704
708
|
}
|
|
705
|
-
function
|
|
706
|
-
const t =
|
|
709
|
+
function V(e) {
|
|
710
|
+
const t = ye(e.fields);
|
|
707
711
|
return e.fields.find((o) => o.kind === "field" && o.fieldname === t);
|
|
708
712
|
}
|
|
709
|
-
function
|
|
713
|
+
function qe(e) {
|
|
710
714
|
const t = Object.entries(e);
|
|
711
715
|
if (t.length === 0) return;
|
|
712
|
-
if (
|
|
716
|
+
if (we().length === 0) {
|
|
713
717
|
console.warn(
|
|
714
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."
|
|
715
719
|
);
|
|
@@ -739,7 +743,7 @@ function He(e) {
|
|
|
739
743
|
`)}`
|
|
740
744
|
);
|
|
741
745
|
}
|
|
742
|
-
function
|
|
746
|
+
function Ue() {
|
|
743
747
|
const e = Ie();
|
|
744
748
|
if (e.length !== 0)
|
|
745
749
|
throw new Error(
|
|
@@ -748,12 +752,12 @@ function qe() {
|
|
|
748
752
|
`)
|
|
749
753
|
);
|
|
750
754
|
}
|
|
751
|
-
function
|
|
755
|
+
function ie(e) {
|
|
752
756
|
return new Error(
|
|
753
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.`
|
|
754
758
|
);
|
|
755
759
|
}
|
|
756
|
-
const
|
|
760
|
+
const at = (e = {}) => {
|
|
757
761
|
const { logPlans: t = !0, logTiming: o = !1 } = e, n = (r, i) => ({
|
|
758
762
|
plan: (a) => {
|
|
759
763
|
t && console.log(`[@stonecrop/graphql-middleware] Building plan for ${r}.${i}`);
|
|
@@ -763,7 +767,7 @@ const it = (e = {}) => {
|
|
|
763
767
|
), $;
|
|
764
768
|
}
|
|
765
769
|
});
|
|
766
|
-
return
|
|
770
|
+
return Le({
|
|
767
771
|
Query: {
|
|
768
772
|
stonecropRecord: n("Query", "stonecropRecord"),
|
|
769
773
|
stonecropRecords: n("Query", "stonecropRecords"),
|
|
@@ -774,7 +778,7 @@ const it = (e = {}) => {
|
|
|
774
778
|
stonecropAction: n("Mutation", "stonecropAction")
|
|
775
779
|
}
|
|
776
780
|
});
|
|
777
|
-
},
|
|
781
|
+
}, _e = {
|
|
778
782
|
name: "StonecropFieldCasingPlugin",
|
|
779
783
|
version: "0.0.0",
|
|
780
784
|
inflection: {
|
|
@@ -784,27 +788,27 @@ const it = (e = {}) => {
|
|
|
784
788
|
}
|
|
785
789
|
}
|
|
786
790
|
}
|
|
787
|
-
},
|
|
788
|
-
extends: [
|
|
789
|
-
plugins: e?.fieldCasing === "pascal" ? [
|
|
790
|
-
}),
|
|
791
|
+
}, Je = (e) => ({
|
|
792
|
+
extends: [Ne],
|
|
793
|
+
plugins: e?.fieldCasing === "pascal" ? [_e] : []
|
|
794
|
+
}), st = Je();
|
|
791
795
|
export {
|
|
792
|
-
|
|
793
|
-
|
|
796
|
+
se as DoctypeValidationError,
|
|
797
|
+
st as StonecropPreset,
|
|
794
798
|
je as applyGuardedTransition,
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
799
|
+
rt as clearFetchHandlers,
|
|
800
|
+
nt as clearRegistry,
|
|
801
|
+
at as createDebugPlugin,
|
|
802
|
+
it as createStonecropPlugin,
|
|
803
|
+
Je as createStonecropPreset,
|
|
804
|
+
we as getAllMeta,
|
|
805
|
+
Ce as getFetchHandler,
|
|
802
806
|
L as getMeta,
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
807
|
+
tt as hasMeta,
|
|
808
|
+
De as loadDoctypes,
|
|
809
|
+
et as loadDoctypesFromObject,
|
|
810
|
+
dt as makePgService,
|
|
811
|
+
ot as registerFetchHandler,
|
|
812
|
+
We as typeDefs
|
|
809
813
|
};
|
|
810
814
|
//# sourceMappingURL=index.js.map
|