@revenexx/app-sdk 2.0.0 → 2.1.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 +17 -1
- package/dist/{chunk-AVWPY5D2.js → chunk-TLWWVUJ4.js} +50 -8
- package/dist/{chunk-AVWPY5D2.js.map → chunk-TLWWVUJ4.js.map} +1 -1
- package/dist/{errors-BsKwZ07u.d.cts → errors-CkBo_7bc.d.cts} +6 -0
- package/dist/{errors-BsKwZ07u.d.ts → errors-CkBo_7bc.d.ts} +6 -0
- package/dist/index.cjs +98 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -2
- package/dist/index.d.ts +11 -2
- package/dist/index.js +52 -1
- package/dist/index.js.map +1 -1
- package/dist/router/index.cjs +13 -7
- package/dist/router/index.cjs.map +1 -1
- package/dist/router/index.d.cts +2 -2
- package/dist/router/index.d.ts +2 -2
- package/dist/router/index.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -253,6 +253,41 @@ function isList(data) {
|
|
|
253
253
|
return keys.length > 0 && keys.every((k) => INDEX.test(k) && Number(k) < keys.length);
|
|
254
254
|
}
|
|
255
255
|
var ENTITY_DESCRIPTOR = /* @__PURE__ */ Symbol.for("revenexx.app-sdk.entity");
|
|
256
|
+
var MANY_RECORDS_MESSAGE = "createMany takes a list of records";
|
|
257
|
+
function bulkRecords(entity, data) {
|
|
258
|
+
if (Array.isArray(data) && data.length === 0) throw badRequest(EMPTY_CREATE_MESSAGE, void 0, { code: "empty_create" });
|
|
259
|
+
if (!isList(data)) throw badRequest(MANY_RECORDS_MESSAGE, void 0, { code: "bad_request" });
|
|
260
|
+
const records = Array.isArray(data) ? data : Object.keys(data).sort((a, b) => Number(a) - Number(b)).map((k) => data[k]);
|
|
261
|
+
let shape2;
|
|
262
|
+
records.forEach((record, index) => {
|
|
263
|
+
if (!record || typeof record !== "object" || isList(record)) {
|
|
264
|
+
throw badRequest(`record ${index} of a bulk create is not an object \u2014 each element is one record`, { index }, { code: "bad_request" });
|
|
265
|
+
}
|
|
266
|
+
const fields = updateFields(record);
|
|
267
|
+
if (fields.length === 0) {
|
|
268
|
+
throw badRequest(`record ${index} of a bulk create has no field \u2014 ${EMPTY_CREATE_MESSAGE}`, { index }, { code: "empty_create" });
|
|
269
|
+
}
|
|
270
|
+
const unknown = unknownFields(entity, record);
|
|
271
|
+
if (unknown.length > 0) {
|
|
272
|
+
const quoted = unknown.map((k) => `'${k}'`).join(", ");
|
|
273
|
+
throw badRequest(
|
|
274
|
+
`unknown field${unknown.length > 1 ? "s" : ""} ${quoted} in record ${index} \u2014 not a column of '${entity.name}'. A field that cannot be written is refused rather than dropped; see details.allowed.`,
|
|
275
|
+
{ index, unknown, allowed: writableColumns(entity) },
|
|
276
|
+
{ code: "unknown_field" }
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
const key = [...fields].sort().join("\0");
|
|
280
|
+
if (shape2 === void 0) shape2 = key;
|
|
281
|
+
else if (key !== shape2) {
|
|
282
|
+
throw badRequest(
|
|
283
|
+
`record ${index} of a bulk create names other fields than record 0 \u2014 every record of a bulk create names the same fields`,
|
|
284
|
+
{ index },
|
|
285
|
+
{ code: "bad_request" }
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
return records;
|
|
290
|
+
}
|
|
256
291
|
function oneRecordRefusal() {
|
|
257
292
|
return badRequest(ONE_RECORD_MESSAGE, void 0, { code: "bad_request" });
|
|
258
293
|
}
|
|
@@ -359,6 +394,30 @@ function createMockAdapter(seed = {}) {
|
|
|
359
394
|
tbl(entity).set(row[entity.pk], row);
|
|
360
395
|
return { ...row };
|
|
361
396
|
},
|
|
397
|
+
// One statement in Postgres: a primary key twice in the batch, or one the
|
|
398
|
+
// table already holds, is the 409 the remote adapter maps from 23505, and
|
|
399
|
+
// nothing is stored.
|
|
400
|
+
async createMany(entity, data) {
|
|
401
|
+
const rows = bulkRecords(entity, data).map((record) => {
|
|
402
|
+
const row = applyDefaults(entity, record);
|
|
403
|
+
if (row[entity.pk] === void 0) row[entity.pk] = (0, import_node_crypto.randomUUID)();
|
|
404
|
+
return row;
|
|
405
|
+
});
|
|
406
|
+
const table = tbl(entity);
|
|
407
|
+
const seen = /* @__PURE__ */ new Set();
|
|
408
|
+
for (const row of rows) {
|
|
409
|
+
const id = row[entity.pk];
|
|
410
|
+
if (seen.has(id) || table.has(id)) {
|
|
411
|
+
throw new HttpError(409, "a record with this value already exists", void 0, {
|
|
412
|
+
code: "unique_violation",
|
|
413
|
+
serverDetail: { source: "mock", entity: entity.name, duplicate: id }
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
seen.add(id);
|
|
417
|
+
}
|
|
418
|
+
for (const row of rows) table.set(row[entity.pk], row);
|
|
419
|
+
return rows.map((row) => ({ ...row }));
|
|
420
|
+
},
|
|
362
421
|
async update(entity, id, patch) {
|
|
363
422
|
assertKnownFields(entity, patch);
|
|
364
423
|
const cur = tbl(entity).get(id);
|
|
@@ -573,6 +632,23 @@ function createRemoteAdapter(config) {
|
|
|
573
632
|
})
|
|
574
633
|
);
|
|
575
634
|
},
|
|
635
|
+
// One POST of the whole list: PostgREST inserts it in one statement and
|
|
636
|
+
// answers every row, in order. Checked here as well as in the client.
|
|
637
|
+
async createMany(entity, data) {
|
|
638
|
+
const records = bulkRecords(entity, data);
|
|
639
|
+
const rows = await req("POST", `/${entity.table}`, {
|
|
640
|
+
body: records,
|
|
641
|
+
prefer: "return=representation",
|
|
642
|
+
table: entity.table
|
|
643
|
+
});
|
|
644
|
+
if (!Array.isArray(rows) || rows.length < records.length) {
|
|
645
|
+
throw new HttpError(500, "the records were created but could not be read back", void 0, {
|
|
646
|
+
code: "empty_representation",
|
|
647
|
+
serverDetail: { source: "postgrest", method: "POST", table: entity.table, sent: records.length }
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
return rows;
|
|
651
|
+
},
|
|
576
652
|
async update(entity, id, patch) {
|
|
577
653
|
assertKnownFields(entity, patch);
|
|
578
654
|
const rows = await req("PATCH", `/${entity.table}?${entity.pk}=eq.${encodeURIComponent(String(id))}`, {
|
|
@@ -758,6 +834,15 @@ function entityClient(adapter, entity) {
|
|
|
758
834
|
assertCreatable(data);
|
|
759
835
|
return adapter.create(entity, data);
|
|
760
836
|
}),
|
|
837
|
+
// A bulk create is its own call: checked whole before the one request
|
|
838
|
+
// (./patch.ts `bulkRecords`), never emulated as one create per record.
|
|
839
|
+
createMany: guard("create", async (rows) => {
|
|
840
|
+
const records = bulkRecords(entity, rows);
|
|
841
|
+
if (!adapter.createMany) {
|
|
842
|
+
throw new Error(`${entity.name}.createMany() is not supported by the '${adapter.kind}' adapter`);
|
|
843
|
+
}
|
|
844
|
+
return adapter.createMany(entity, records);
|
|
845
|
+
}, "createMany"),
|
|
761
846
|
// A patch with no fields is a malformed request, not a write: PostgREST
|
|
762
847
|
// performs no update and returns no row, which used to leave the App
|
|
763
848
|
// serialising `undefined` and answering 500 (see ./patch.ts). Rejected here
|
|
@@ -1094,17 +1179,23 @@ function backdatingDb(db, entities) {
|
|
|
1094
1179
|
new Proxy(repo, {
|
|
1095
1180
|
get(r, key, rr) {
|
|
1096
1181
|
const member = Reflect.get(r, key, rr);
|
|
1097
|
-
if (key !== "create" || typeof member !== "function") return member;
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
}
|
|
1102
|
-
const at = now();
|
|
1182
|
+
if (key !== "create" && key !== "createMany" || typeof member !== "function") return member;
|
|
1183
|
+
const call = (data, rest) => member.call(r, data, ...rest);
|
|
1184
|
+
const stamp = (data, at) => {
|
|
1185
|
+
if (!data || typeof data !== "object" || Array.isArray(data) || isList(data)) return data;
|
|
1103
1186
|
const row = { ...data };
|
|
1104
1187
|
if (row.created_at === void 0) row.created_at = at;
|
|
1105
1188
|
if ("updated_at" in columns && row.updated_at === void 0) row.updated_at = at;
|
|
1106
|
-
return
|
|
1189
|
+
return row;
|
|
1107
1190
|
};
|
|
1191
|
+
if (key === "createMany") {
|
|
1192
|
+
return (data, ...rest) => {
|
|
1193
|
+
if (!backdated() || !Array.isArray(data)) return call(data, rest);
|
|
1194
|
+
const at = now();
|
|
1195
|
+
return call(data.map((record) => stamp(record, at)), rest);
|
|
1196
|
+
};
|
|
1197
|
+
}
|
|
1198
|
+
return (data, ...rest) => backdated() ? call(stamp(data, now()), rest) : call(data, rest);
|
|
1108
1199
|
}
|
|
1109
1200
|
})
|
|
1110
1201
|
);
|