@inferencebureau/apps-sdk 0.11.0 → 0.13.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/auto.d.ts +113 -0
- package/dist/auto.js +197 -0
- package/dist/index.d.ts +215 -4
- package/dist/index.js +123 -0
- package/dist/polaris.d.ts +30 -0
- package/dist/polaris.js +104 -0
- package/dist/react.d.ts +242 -0
- package/dist/react.js +488 -0
- package/dist/runtime.js +1 -1
- package/dist/test.js +222 -24
- package/package.json +25 -2
package/dist/test.js
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* File fields keep their files in memory: `base64` reads back as a `data:` URL, `copyURL` as the URL itself (not
|
|
19
19
|
* fetched, size 0), and `upload`/`createUpload` tokens as the upload (a `createUpload` needs no PUT here).
|
|
20
20
|
*/
|
|
21
|
-
import { ApiError, ShopifyError, createAiClient, newRecordId } from "./index.js";
|
|
21
|
+
import { ApiError, MAX_BULK, ShopifyError, createAiClient, describeSchema, newRecordId } from "./index.js";
|
|
22
22
|
import { runAction as runPhase } from "./runtime.js";
|
|
23
23
|
/** Kept in `data`; the rest are a vector, the other side of a relationship, or worked out (computed). */
|
|
24
24
|
const STORED = new Set(["string", "richText", "email", "url", "color", "number", "dateTime", "enum", "boolean", "roleList", "json", "belongsTo", "password", "encryptedString", "file"]);
|
|
@@ -26,6 +26,17 @@ const SYSTEM = ["id", "createdAt", "updatedAt"];
|
|
|
26
26
|
const MAX_PAGE = 250;
|
|
27
27
|
const MAX_RECORD_BYTES = 256 * 1024;
|
|
28
28
|
const invalid = (problems) => new ApiError(problems.map((p) => `${p.field}: ${p.message}`).join("; "), 400, "invalid_request", problems);
|
|
29
|
+
/** A related filter's or sort's problems, named from the relationship: `product.vendor`. */
|
|
30
|
+
function under(at, f) {
|
|
31
|
+
try {
|
|
32
|
+
return f();
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
if (e instanceof ApiError && e.problems.length)
|
|
36
|
+
throw invalid(e.problems.map((p) => ({ field: `${at}.${p.field}`, message: p.message })));
|
|
37
|
+
throw e;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
29
40
|
const notFound = (message) => new ApiError(message, 404, "not_found");
|
|
30
41
|
const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
31
42
|
/** What crosses the wire between Inference Bureau and the app: JSON (a Date arrives as its ISO string). */
|
|
@@ -35,9 +46,14 @@ function modelOf(name, schema) {
|
|
|
35
46
|
const dup = fields.find((f, i) => fields.findIndex((g) => g.key === f.key) !== i);
|
|
36
47
|
if (dup)
|
|
37
48
|
throw new Error(`${name}: two fields store their values under ${dup.key}; give ${dup.name} its own storageKey`);
|
|
38
|
-
|
|
49
|
+
const byName = new Map(fields.map((f) => [f.name, f]));
|
|
50
|
+
// Unique indexes (records.ts indexesOf) hold their fields' values unique together; the other kinds only speed reads.
|
|
51
|
+
const unique = (Array.isArray(schema.indexes) ? schema.indexes : []).filter((ix) => ix.unique === true && Array.isArray(ix.fields) && ix.fields.length)
|
|
52
|
+
.map((ix) => ix.fields.map((n) => byName.get(n)).filter((f) => !!f && STORED.has(f.type)));
|
|
53
|
+
return { name, fields, byName, unique };
|
|
39
54
|
}
|
|
40
55
|
const RELATIONSHIPS = new Set(["belongsTo", "hasOne", "hasMany", "hasManyThrough"]);
|
|
56
|
+
const LIST_OPS = ["some", "every", "none"];
|
|
41
57
|
const both = (a, b) => (a && b ? { AND: [a, b] } : a ?? b);
|
|
42
58
|
const ok = (value) => ({ ok: true, value });
|
|
43
59
|
const bad = (message) => ({ ok: false, message });
|
|
@@ -850,28 +866,83 @@ class Records {
|
|
|
850
866
|
}
|
|
851
867
|
if (!isObj(v))
|
|
852
868
|
throw invalid([{ field: k, message: "needs an operator, like { equals: … }" }]);
|
|
869
|
+
const f = m.byName.get(k);
|
|
870
|
+
// A belongsTo with operators filters on the id it holds; with fields, on its record.
|
|
871
|
+
if (f && RELATIONSHIPS.has(f.type) && (f.type !== "belongsTo" || !Object.keys(v).some((op) => OPS.includes(op)))) {
|
|
872
|
+
if (!this.relatedMatches(m, row, f, v, ctx))
|
|
873
|
+
return false;
|
|
874
|
+
continue;
|
|
875
|
+
}
|
|
853
876
|
for (const [op, arg] of Object.entries(v)) {
|
|
854
877
|
if (!OPS.includes(op))
|
|
855
|
-
throw invalid([{ field: k, message: `${op} isn't a filter; use ${OPS.join(", ")}` }]);
|
|
878
|
+
throw invalid([{ field: k, message: f?.type === "belongsTo" ? `${op} isn't a filter; use ${OPS.join(", ")} on the id it holds, or only its record's fields` : `${op} isn't a filter; use ${OPS.join(", ")}` }]);
|
|
856
879
|
if (!this.test(m, row, k, op, arg, ctx))
|
|
857
880
|
return false;
|
|
858
881
|
}
|
|
859
882
|
}
|
|
860
883
|
return true;
|
|
861
884
|
}
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
885
|
+
/** A relationship's records for a row (records.ts: relatedCondition), only those the caller can read, one deeper. */
|
|
886
|
+
reached(m, row, f, ctx) {
|
|
887
|
+
let rel;
|
|
888
|
+
try {
|
|
889
|
+
rel = this.relatedOf(m, f);
|
|
890
|
+
}
|
|
891
|
+
catch (e) {
|
|
892
|
+
throw invalid([{ field: f.name, message: e.message }]);
|
|
893
|
+
}
|
|
894
|
+
if (ctx.depth >= 3)
|
|
895
|
+
throw invalid([{ field: f.name, message: "filters and sorts can reach at most 3 relationships deep" }]);
|
|
896
|
+
const inner = { seen: ctx.seen, depth: ctx.depth + 1, readable: ctx.readable };
|
|
897
|
+
const t = rel.target;
|
|
898
|
+
const can = (x, r) => this.matches(x, r, ctx.readable?.(x), inner);
|
|
899
|
+
let rows;
|
|
900
|
+
if (f.type === "belongsTo") {
|
|
901
|
+
const r = typeof row.data[f.key] === "string" ? this.table(t).get(row.data[f.key]) : undefined;
|
|
902
|
+
rows = r ? [r] : [];
|
|
903
|
+
}
|
|
904
|
+
else if (rel.link)
|
|
905
|
+
rows = [...this.table(t).values()].filter((r) => r.data[rel.link.key] === row.id);
|
|
906
|
+
else {
|
|
907
|
+
const { model: jm, self, sibling } = rel.join;
|
|
908
|
+
rows = [...this.table(jm).values()].filter((j) => j.data[self.key] === row.id && can(jm, j)).map((j) => this.table(t).get(j.data[sibling.key])).filter((r) => !!r);
|
|
909
|
+
}
|
|
910
|
+
return { target: t, inner, rows: rows.filter((r) => can(t, r)) };
|
|
911
|
+
}
|
|
912
|
+
/**
|
|
913
|
+
* A filter through a relationship: a belongsTo or hasOne's record matching it, or a hasMany or hasManyThrough's
|
|
914
|
+
* records, `some`, `every` (none that doesn't) or `none` matching. Checked on a blank record first, as the API's SQL
|
|
915
|
+
* is checked whether or not there are records to try it on.
|
|
916
|
+
*/
|
|
917
|
+
relatedMatches(m, row, f, v, ctx) {
|
|
918
|
+
const { target, inner, rows } = this.reached(m, row, f, ctx);
|
|
919
|
+
const probe = { id: "", createdAt: "", updatedAt: "", data: {}, vectors: new Map() };
|
|
920
|
+
if (f.type === "belongsTo" || f.type === "hasOne") {
|
|
921
|
+
under(f.name, () => this.matches(target, probe, v, inner));
|
|
922
|
+
return rows.some((r) => this.matches(target, r, v, inner));
|
|
923
|
+
}
|
|
924
|
+
let ok = true;
|
|
925
|
+
for (const [op, arg] of Object.entries(v)) {
|
|
926
|
+
if (!LIST_OPS.includes(op))
|
|
927
|
+
throw invalid([{ field: f.name, message: `${op} isn't a filter; a ${f.type} takes ${LIST_OPS.join(", ")}, like { some: { status: { equals: … } } }` }]);
|
|
928
|
+
if (arg !== undefined && arg !== null && !isObj(arg))
|
|
929
|
+
throw invalid([{ field: `${f.name}.${op}`, message: "must be a filter on its records' fields" }]);
|
|
930
|
+
const filter = (arg ?? undefined);
|
|
931
|
+
under(`${f.name}.${op}`, () => this.matches(target, probe, filter, inner));
|
|
932
|
+
const hit = (r) => this.matches(target, r, filter, inner);
|
|
933
|
+
ok &&= op === "some" ? rows.some(hit) : op === "none" ? !rows.some(hit) : rows.every(hit);
|
|
934
|
+
}
|
|
935
|
+
return ok;
|
|
936
|
+
}
|
|
937
|
+
sorted(m, rows, sort, readable) {
|
|
938
|
+
const ctx = { ...CTX, readable };
|
|
939
|
+
const keys = (Array.isArray(sort) ? sort : sort ? [sort] : []).flatMap((s) => this.sortKeys(m, s, ctx));
|
|
869
940
|
if (!keys.length)
|
|
870
|
-
keys.push({
|
|
871
|
-
keys.push({
|
|
941
|
+
keys.push({ at: (r) => r.createdAt, desc: true });
|
|
942
|
+
keys.push({ at: (r) => r.id, desc: true });
|
|
872
943
|
return [...rows].sort((a, b) => {
|
|
873
944
|
for (const k of keys) {
|
|
874
|
-
const x =
|
|
945
|
+
const x = k.at(a), y = k.at(b);
|
|
875
946
|
if (x === y)
|
|
876
947
|
continue;
|
|
877
948
|
// Nulls last, either way.
|
|
@@ -885,6 +956,26 @@ class Records {
|
|
|
885
956
|
return 0;
|
|
886
957
|
});
|
|
887
958
|
}
|
|
959
|
+
/** A sort's values to order by, in turn (records.ts: sortTerms); a belongsTo's are read from its record, null without one the caller can read. */
|
|
960
|
+
sortKeys(m, sort, ctx) {
|
|
961
|
+
if (!isObj(sort))
|
|
962
|
+
throw invalid([{ field: "sort", message: "must be an object, like { createdAt: \"Descending\" }" }]);
|
|
963
|
+
return Object.entries(sort).flatMap(([name, dir]) => {
|
|
964
|
+
if (isObj(dir)) {
|
|
965
|
+
const f = m.byName.get(name);
|
|
966
|
+
if (f?.type !== "belongsTo")
|
|
967
|
+
throw invalid([{ field: name, message: f ? "sorts Ascending or Descending; only a belongsTo sorts by its record's fields" : `${m.name} has no field named ${name}` }]);
|
|
968
|
+
const probe = { id: "", createdAt: "", updatedAt: "", data: {}, vectors: new Map() };
|
|
969
|
+
const { target, inner } = this.reached(m, probe, f, ctx);
|
|
970
|
+
const parent = (row) => this.reached(m, row, f, ctx).rows[0];
|
|
971
|
+
return under(name, () => this.sortKeys(target, dir, inner)).map((k) => ({ at: (row) => { const p = parent(row); return p ? k.at(p) : null; }, desc: k.desc }));
|
|
972
|
+
}
|
|
973
|
+
if (dir !== "Ascending" && dir !== "Descending")
|
|
974
|
+
throw invalid([{ field: name, message: "sorts Ascending or Descending" }]);
|
|
975
|
+
const t = this.target(m, name, ctx);
|
|
976
|
+
return [{ at: (row) => this.comparable(row, t), desc: dir === "Descending" }];
|
|
977
|
+
});
|
|
978
|
+
}
|
|
888
979
|
/** As the API's text search: every word in one of the text fields, and the rows holding the whole phrase. */
|
|
889
980
|
textSearch(m, rows, text) {
|
|
890
981
|
const words = searchWords(text);
|
|
@@ -903,11 +994,12 @@ class Records {
|
|
|
903
994
|
const plan = this.plan(m, o.select);
|
|
904
995
|
const first = Math.min(Math.max(1, Math.floor(o.first ?? 50)), MAX_PAGE);
|
|
905
996
|
const offset = offsetOf(o.after);
|
|
906
|
-
|
|
997
|
+
const ctx = { ...CTX, readable: o.readable };
|
|
998
|
+
let rows = [...this.table(m).values()].filter((r) => this.matches(m, r, o.filter, ctx));
|
|
907
999
|
let distances = null;
|
|
908
1000
|
const text = typeof o.search === "string" ? this.textSearch(m, rows, o.search) : null;
|
|
909
1001
|
if (text) {
|
|
910
|
-
rows = this.sorted(m, text.rows, o.sort);
|
|
1002
|
+
rows = this.sorted(m, text.rows, o.sort, o.readable);
|
|
911
1003
|
// Unsorted, the rows holding the whole phrase come first (a stable sort keeps the default order within each).
|
|
912
1004
|
if (!o.sort)
|
|
913
1005
|
rows.sort((a, b) => Number(text.phrase.has(b.id)) - Number(text.phrase.has(a.id)));
|
|
@@ -935,7 +1027,7 @@ class Records {
|
|
|
935
1027
|
rows = rows.filter((r) => distances.has(r.id)).sort((a, b) => distances.get(a.id) - distances.get(b.id) || (a.id < b.id ? -1 : 1));
|
|
936
1028
|
}
|
|
937
1029
|
else
|
|
938
|
-
rows = this.sorted(m, rows, o.sort);
|
|
1030
|
+
rows = this.sorted(m, rows, o.sort, o.readable);
|
|
939
1031
|
const page = rows.slice(offset, offset + first);
|
|
940
1032
|
const budget = { left: 5000 };
|
|
941
1033
|
const read = (r) => (plan ? this.selected(m, r, plan, o.readable, budget, false) : this.present(m, r, false));
|
|
@@ -945,9 +1037,9 @@ class Records {
|
|
|
945
1037
|
endCursor: page.length ? cursorOf(offset + page.length) : null,
|
|
946
1038
|
};
|
|
947
1039
|
}
|
|
948
|
-
count(model, filter) {
|
|
1040
|
+
count(model, filter, readable) {
|
|
949
1041
|
const m = this.model(model);
|
|
950
|
-
return [...this.table(m).values()].filter((r) => this.matches(m, r, filter)).length;
|
|
1042
|
+
return [...this.table(m).values()].filter((r) => this.matches(m, r, filter, { ...CTX, readable })).length;
|
|
951
1043
|
}
|
|
952
1044
|
// ---- select (records.ts: planOf, attach) ----
|
|
953
1045
|
/** A select checked against the model, as records.ts's planOf checks it; null when unset. */
|
|
@@ -1047,20 +1139,21 @@ class Records {
|
|
|
1047
1139
|
for (const rel of plan.relations) {
|
|
1048
1140
|
const filter = both(readable?.(rel.target), rel.filter);
|
|
1049
1141
|
const t = rel.target;
|
|
1142
|
+
const ctx = { ...CTX, readable };
|
|
1050
1143
|
let rows;
|
|
1051
1144
|
if (rel.link)
|
|
1052
|
-
rows = this.sorted(t, [...this.table(t).values()].filter((r) => r.data[rel.link.key] === row.id && this.matches(t, r, filter)), rel.sort);
|
|
1145
|
+
rows = this.sorted(t, [...this.table(t).values()].filter((r) => r.data[rel.link.key] === row.id && this.matches(t, r, filter, ctx)), rel.sort, readable);
|
|
1053
1146
|
else if (rel.join) {
|
|
1054
1147
|
const { model: jm, self, sibling } = rel.join;
|
|
1055
1148
|
const jf = readable?.(jm);
|
|
1056
|
-
const reached = [...this.table(jm).values()].filter((j) => j.data[self.key] === row.id && this.matches(jm, j, jf))
|
|
1057
|
-
.map((j) => this.table(t).get(j.data[sibling.key])).filter((r) => !!r && this.matches(t, r, filter));
|
|
1058
|
-
rows = this.sorted(t, reached, rel.sort);
|
|
1149
|
+
const reached = [...this.table(jm).values()].filter((j) => j.data[self.key] === row.id && this.matches(jm, j, jf, ctx))
|
|
1150
|
+
.map((j) => this.table(t).get(j.data[sibling.key])).filter((r) => !!r && this.matches(t, r, filter, ctx));
|
|
1151
|
+
rows = this.sorted(t, reached, rel.sort, readable);
|
|
1059
1152
|
}
|
|
1060
1153
|
else {
|
|
1061
1154
|
const id = out[rel.field.name];
|
|
1062
1155
|
const r = typeof id === "string" ? this.table(t).get(id) : undefined;
|
|
1063
|
-
rows = r && this.matches(t, r, filter) ? [r] : [];
|
|
1156
|
+
rows = r && this.matches(t, r, filter, ctx) ? [r] : [];
|
|
1064
1157
|
}
|
|
1065
1158
|
const kept = rows.slice(0, rel.first + 1);
|
|
1066
1159
|
budget.left -= kept.length;
|
|
@@ -1166,6 +1259,18 @@ class Records {
|
|
|
1166
1259
|
problems.push({ field: f.name, message: "is already taken by another record" });
|
|
1167
1260
|
}
|
|
1168
1261
|
}
|
|
1262
|
+
// A unique index refuses the write as the database does (records.ts indexViolation): empty values don't count.
|
|
1263
|
+
for (const fields of m.unique) {
|
|
1264
|
+
if (!fields.length || fields.some((f) => data[f.key] === null || data[f.key] === undefined))
|
|
1265
|
+
continue;
|
|
1266
|
+
const dup = [...this.table(m).values()].some((r) => r.id !== current?.id && fields.every((f) => contains(r.data, { [f.key]: data[f.key] })));
|
|
1267
|
+
if (!dup)
|
|
1268
|
+
continue;
|
|
1269
|
+
const field = fields[fields.length - 1];
|
|
1270
|
+
const others = fields.slice(0, -1).map((f) => f.name);
|
|
1271
|
+
if (!problems.some((p) => p.field === field.name))
|
|
1272
|
+
problems.push({ field: field.name, message: others.length ? `is already taken by another record with the same ${others.join(", ")}` : "is already taken by another record" });
|
|
1273
|
+
}
|
|
1169
1274
|
if (problems.length)
|
|
1170
1275
|
throw invalid(problems);
|
|
1171
1276
|
if (JSON.stringify(data).length > MAX_RECORD_BYTES)
|
|
@@ -2044,7 +2149,64 @@ export function createTestApp(o = {}) {
|
|
|
2044
2149
|
actionFilter(perms, scope, store.model(name), "update");
|
|
2045
2150
|
}
|
|
2046
2151
|
};
|
|
2047
|
-
|
|
2152
|
+
/** The fields an upsert finds its record by, checked as the API checks them, as the filter that finds it. */
|
|
2153
|
+
const upsertBy = (m, on, values) => {
|
|
2154
|
+
const problems = [];
|
|
2155
|
+
const parts = [];
|
|
2156
|
+
if (!on.length)
|
|
2157
|
+
problems.push({ field: "on", message: "names the field (or fields) to upsert by" });
|
|
2158
|
+
for (const field of on) {
|
|
2159
|
+
const f = field === "id" ? null : m.byName.get(field);
|
|
2160
|
+
if (field !== "id" && !f) {
|
|
2161
|
+
problems.push({ field, message: `${m.name} has no field named ${field}` });
|
|
2162
|
+
continue;
|
|
2163
|
+
}
|
|
2164
|
+
if (f && (!STORED.has(f.type) || f.type === "password" || f.type === "encryptedString" || f.type === "file")) {
|
|
2165
|
+
problems.push({ field, message: `a ${f.type} field can't be upserted by` });
|
|
2166
|
+
continue;
|
|
2167
|
+
}
|
|
2168
|
+
const v = values[field];
|
|
2169
|
+
if (v === undefined || v === null || v === "") {
|
|
2170
|
+
problems.push({ field, message: "is needed to upsert by it" });
|
|
2171
|
+
continue;
|
|
2172
|
+
}
|
|
2173
|
+
const link = f?.type === "belongsTo" && v && typeof v === "object" && typeof v._link === "string" ? v._link : v;
|
|
2174
|
+
parts.push({ [field]: { equals: link } });
|
|
2175
|
+
}
|
|
2176
|
+
if (problems.length)
|
|
2177
|
+
throw invalid(problems);
|
|
2178
|
+
return parts.length === 1 ? parts[0] : { AND: parts };
|
|
2179
|
+
};
|
|
2180
|
+
/** A batch as the API answers it: each record the way a single write goes, its refusal as its failure. */
|
|
2181
|
+
const batch = async (items, upsert = false) => {
|
|
2182
|
+
if (items.length > MAX_BULK)
|
|
2183
|
+
throw new ApiError(`A bulk call takes at most ${MAX_BULK} records; this one has ${items.length}.`, 400, "invalid_request");
|
|
2184
|
+
const records = [], failed = [];
|
|
2185
|
+
const counts = { succeeded: 0, failed: 0, ...(upsert ? { created: 0, updated: 0 } : {}) };
|
|
2186
|
+
for (const [index, it] of items.entries()) {
|
|
2187
|
+
try {
|
|
2188
|
+
const r = await it.work();
|
|
2189
|
+
records.push(r.record);
|
|
2190
|
+
counts.succeeded++;
|
|
2191
|
+
if (upsert) {
|
|
2192
|
+
if (r.created)
|
|
2193
|
+
counts.created++;
|
|
2194
|
+
else
|
|
2195
|
+
counts.updated++;
|
|
2196
|
+
}
|
|
2197
|
+
}
|
|
2198
|
+
catch (e) {
|
|
2199
|
+
if (!(e instanceof ApiError))
|
|
2200
|
+
throw e;
|
|
2201
|
+
records.push(null);
|
|
2202
|
+
counts.failed++;
|
|
2203
|
+
failed.push({ index, ...(it.id ? { id: it.id } : {}), error: { code: e.code ?? "invalid_request", message: e.message, ...(e.problems.length ? { problems: e.problems } : {}), ...(e instanceof ActionFailedError ? { logs: e.logs } : {}) } });
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
return { records, failed, counts };
|
|
2207
|
+
};
|
|
2208
|
+
const client = {
|
|
2209
|
+
describe: async () => { const m = store.model(name); return describeSchema(m.name, { type: "ib/model-schema/v1", fields: Object.fromEntries(m.fields.map((f) => [f.name, f.def])) }); },
|
|
2048
2210
|
build: () => { throw new Error("build() makes a record for an action's run to save(); outside an action, use create()."); },
|
|
2049
2211
|
findOne: async (id, o = {}) => findVisible(name, id, readOf(), wire(o.select), readable),
|
|
2050
2212
|
findMany: async (q = {}) => store.findMany(name, narrow(wire(q))),
|
|
@@ -2073,6 +2235,27 @@ export function createTestApp(o = {}) {
|
|
|
2073
2235
|
await canRun(scope, name, "delete", id);
|
|
2074
2236
|
store.delete(name, id);
|
|
2075
2237
|
},
|
|
2238
|
+
// As through the API: the record the on fields find (one the call's update grant reaches) is updated, else one is created.
|
|
2239
|
+
upsert: async ({ on, values }) => {
|
|
2240
|
+
const m = store.model(name);
|
|
2241
|
+
const v = scoped ? scopedValues(scope, m, wire(values)) : wire(values);
|
|
2242
|
+
const by = upsertBy(m, Array.isArray(on) ? on : [on], v);
|
|
2243
|
+
if (scoped)
|
|
2244
|
+
actionFilter(perms, scope, m, "create");
|
|
2245
|
+
const reach = scoped ? actionFilter(perms, scope, m, "update") : undefined;
|
|
2246
|
+
const found = (await store.findMany(name, { filter: reach ? { AND: [by, reach] } : by, first: 1 })).records[0];
|
|
2247
|
+
if (found)
|
|
2248
|
+
return { record: registry[`${name}/update`] ? (await runFor(scope, name, "update", { id: found.id, params: v })).record : await store.update(name, found.id, v), created: false };
|
|
2249
|
+
return { record: registry[`${name}/create`] ? (await runFor(scope, name, "create", { params: v })).record : await store.create(name, v), created: true };
|
|
2250
|
+
},
|
|
2251
|
+
bulkCreate: (values) => batch(values.map((v) => ({ work: async () => ({ record: await client.create(v) }) }))),
|
|
2252
|
+
bulkUpdate: (records) => batch(records.map((r) => ({ id: r.id, work: async () => ({ record: await client.update(r.id, r.values) }) }))),
|
|
2253
|
+
bulkDelete: async (ids) => {
|
|
2254
|
+
const r = await batch(ids.map((id) => ({ id, work: async () => { await client.delete(id); return { record: null }; } })));
|
|
2255
|
+
const lost = new Set(r.failed.map((f) => f.index));
|
|
2256
|
+
return { deleted: ids.filter((_id, i) => !lost.has(i)), failed: r.failed, counts: r.counts };
|
|
2257
|
+
},
|
|
2258
|
+
bulkUpsert: (q) => batch(q.records.map((v) => ({ work: () => client.upsert({ on: q.on, values: v }) })), true),
|
|
2076
2259
|
// A record the call can't read answers false, as a missing one does.
|
|
2077
2260
|
checkPassword: async (id, password, field) => (scoped && !(await findVisible(name, id, readOf())) ? false : store.checkPassword(name, id, password, typeof field === "string" ? field : field?.field)),
|
|
2078
2261
|
run: async (action, q = {}) => {
|
|
@@ -2100,6 +2283,7 @@ export function createTestApp(o = {}) {
|
|
|
2100
2283
|
return { directUploadToken: token };
|
|
2101
2284
|
},
|
|
2102
2285
|
};
|
|
2286
|
+
return client;
|
|
2103
2287
|
};
|
|
2104
2288
|
const enqueue = async (action, params = {}, q = {}) => {
|
|
2105
2289
|
const t = (o.now ?? Date.now)();
|
|
@@ -2162,6 +2346,18 @@ export function createTestApp(o = {}) {
|
|
|
2162
2346
|
const shop = scopedShop(scope, q.shop ?? null);
|
|
2163
2347
|
return enqueue(action, params, shop ? { ...q, shop } : q);
|
|
2164
2348
|
};
|
|
2349
|
+
// Every job is checked before any is queued, as the API does.
|
|
2350
|
+
const queueMany = async (action, entries, q = {}) => {
|
|
2351
|
+
if (entries.length > MAX_BULK)
|
|
2352
|
+
throw new ApiError(`A batch queues at most ${MAX_BULK} jobs; this one has ${entries.length}.`, 400, "invalid_request");
|
|
2353
|
+
const n = parseName(action);
|
|
2354
|
+
for (const e of entries)
|
|
2355
|
+
await canRun(scope, n.model, n.action, e.id ?? null);
|
|
2356
|
+
const out = [];
|
|
2357
|
+
for (const e of entries)
|
|
2358
|
+
out.push(await queue(action, e.params, { ...q, ...(e.id ? { id: e.id } : {}) }));
|
|
2359
|
+
return out;
|
|
2360
|
+
};
|
|
2165
2361
|
const made = new Proxy({}, {
|
|
2166
2362
|
get(_t, prop) {
|
|
2167
2363
|
if (prop === "$global")
|
|
@@ -2170,6 +2366,8 @@ export function createTestApp(o = {}) {
|
|
|
2170
2366
|
return (name) => made[name];
|
|
2171
2367
|
if (prop === "enqueue")
|
|
2172
2368
|
return queue;
|
|
2369
|
+
if (prop === "enqueueMany")
|
|
2370
|
+
return queueMany;
|
|
2173
2371
|
if (prop === "forShop")
|
|
2174
2372
|
return (s) => apiFor({ ...scope, shop: s });
|
|
2175
2373
|
if (prop === "asRole")
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inferencebureau/apps-sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "The SDK an Inference Bureau app's code imports: action helpers, metadata types, the records API client and a test harness for its actions.",
|
|
3
|
+
"version": "0.13.0",
|
|
4
|
+
"description": "The SDK an Inference Bureau app's code imports: action helpers, metadata types, the records API client, React hooks with AutoTable and AutoForm, and a test harness for its actions.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"inferencebureau",
|
|
@@ -33,6 +33,16 @@
|
|
|
33
33
|
"import": "./dist/browser.js",
|
|
34
34
|
"default": "./dist/browser.js"
|
|
35
35
|
},
|
|
36
|
+
"./react": {
|
|
37
|
+
"types": "./dist/react.d.ts",
|
|
38
|
+
"import": "./dist/react.js",
|
|
39
|
+
"default": "./dist/react.js"
|
|
40
|
+
},
|
|
41
|
+
"./polaris": {
|
|
42
|
+
"types": "./dist/polaris.d.ts",
|
|
43
|
+
"import": "./dist/polaris.js",
|
|
44
|
+
"default": "./dist/polaris.js"
|
|
45
|
+
},
|
|
36
46
|
"./test": {
|
|
37
47
|
"types": "./dist/test.d.ts",
|
|
38
48
|
"import": "./dist/test.js",
|
|
@@ -60,7 +70,20 @@
|
|
|
60
70
|
"embed": "node scripts/embed.mjs"
|
|
61
71
|
},
|
|
62
72
|
"devDependencies": {
|
|
73
|
+
"@types/react": "^19.1.0",
|
|
74
|
+
"@types/react-dom": "^19.1.0",
|
|
75
|
+
"happy-dom": "^20.14.6",
|
|
76
|
+
"react": "^19.1.0",
|
|
77
|
+
"react-dom": "^19.1.0",
|
|
63
78
|
"typescript": "^5.8.0",
|
|
64
79
|
"vitest": "^3.2.0"
|
|
80
|
+
},
|
|
81
|
+
"peerDependencies": {
|
|
82
|
+
"react": ">=18"
|
|
83
|
+
},
|
|
84
|
+
"peerDependenciesMeta": {
|
|
85
|
+
"react": {
|
|
86
|
+
"optional": true
|
|
87
|
+
}
|
|
65
88
|
}
|
|
66
89
|
}
|