@objectstack/rest 12.1.0 → 12.2.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.cjs +140 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +140 -4
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -628,9 +628,9 @@ function runImport(opts) {
|
|
|
628
628
|
resolveRef
|
|
629
629
|
});
|
|
630
630
|
if (errors.length > 0) {
|
|
631
|
-
const
|
|
631
|
+
const first2 = errors[0];
|
|
632
632
|
errCount++;
|
|
633
|
-
results.push({ row: rowNo, ok: false, action: "failed", field:
|
|
633
|
+
results.push({ row: rowNo, ok: false, action: "failed", field: first2.field, code: first2.code, error: first2.message });
|
|
634
634
|
} else {
|
|
635
635
|
let existing = "none";
|
|
636
636
|
let handled = false;
|
|
@@ -708,6 +708,116 @@ function runImport(opts) {
|
|
|
708
708
|
})();
|
|
709
709
|
}
|
|
710
710
|
|
|
711
|
+
// src/import-mapping.ts
|
|
712
|
+
function unwrapEnvelope(r) {
|
|
713
|
+
if (r && typeof r === "object" && "item" in r) {
|
|
714
|
+
return r.item;
|
|
715
|
+
}
|
|
716
|
+
return r;
|
|
717
|
+
}
|
|
718
|
+
async function resolveNamedMapping(p, opts) {
|
|
719
|
+
const { mappingName, objectName, detectedFormat } = opts;
|
|
720
|
+
if (typeof p?.getMetaItem !== "function") {
|
|
721
|
+
return { ok: false, status: 500, code: "INTERNAL", error: "Metadata protocol unavailable; cannot resolve mappingName" };
|
|
722
|
+
}
|
|
723
|
+
let artifact;
|
|
724
|
+
try {
|
|
725
|
+
artifact = unwrapEnvelope(await p.getMetaItem({ type: "mapping", name: mappingName }));
|
|
726
|
+
} catch {
|
|
727
|
+
}
|
|
728
|
+
if (!artifact || typeof artifact !== "object" || !Array.isArray(artifact.fieldMapping)) {
|
|
729
|
+
return { ok: false, status: 404, code: "MAPPING_NOT_FOUND", error: `No mapping artifact named "${mappingName}" is registered` };
|
|
730
|
+
}
|
|
731
|
+
if (artifact.targetObject !== objectName) {
|
|
732
|
+
return {
|
|
733
|
+
ok: false,
|
|
734
|
+
status: 400,
|
|
735
|
+
code: "MAPPING_TARGET_MISMATCH",
|
|
736
|
+
error: `Mapping "${mappingName}" targets object "${artifact.targetObject}", not "${objectName}"`
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
const declared = artifact.sourceFormat;
|
|
740
|
+
if (declared === "xml" || declared === "sql") {
|
|
741
|
+
return {
|
|
742
|
+
ok: false,
|
|
743
|
+
status: 400,
|
|
744
|
+
code: "MAPPING_FORMAT_UNSUPPORTED",
|
|
745
|
+
error: `Mapping "${mappingName}" declares sourceFormat "${declared}", which the import endpoint does not accept (csv/json/xlsx)`
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
const compatible = declared === void 0 || declared === "json" && detectedFormat === "json" || declared === "csv" && (detectedFormat === "csv" || detectedFormat === "xlsx");
|
|
749
|
+
if (!compatible) {
|
|
750
|
+
return {
|
|
751
|
+
ok: false,
|
|
752
|
+
status: 400,
|
|
753
|
+
code: "MAPPING_FORMAT_MISMATCH",
|
|
754
|
+
error: `Mapping "${mappingName}" declares sourceFormat "${declared}" but the payload is "${detectedFormat}"`
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
for (const entry of artifact.fieldMapping) {
|
|
758
|
+
if (entry?.transform === "javascript") {
|
|
759
|
+
return {
|
|
760
|
+
ok: false,
|
|
761
|
+
status: 400,
|
|
762
|
+
code: "UNSUPPORTED_TRANSFORM",
|
|
763
|
+
error: `Mapping "${mappingName}" uses transform "javascript", which the import path does not execute (no server-side sandbox; see framework#2611)`
|
|
764
|
+
};
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
return { ok: true, artifact };
|
|
768
|
+
}
|
|
769
|
+
var first = (v) => Array.isArray(v) ? v[0] : v;
|
|
770
|
+
function applyMappingToRows(rows, artifact) {
|
|
771
|
+
const out = [];
|
|
772
|
+
for (const row of rows) {
|
|
773
|
+
const mapped = {};
|
|
774
|
+
for (const entry of artifact.fieldMapping) {
|
|
775
|
+
const transform = entry.transform ?? "none";
|
|
776
|
+
const sep = entry.params?.separator ?? " ";
|
|
777
|
+
switch (transform) {
|
|
778
|
+
case "none":
|
|
779
|
+
case "lookup": {
|
|
780
|
+
mapped[first(entry.target)] = row[first(entry.source)];
|
|
781
|
+
break;
|
|
782
|
+
}
|
|
783
|
+
case "constant": {
|
|
784
|
+
mapped[first(entry.target)] = entry.params?.value;
|
|
785
|
+
break;
|
|
786
|
+
}
|
|
787
|
+
case "map": {
|
|
788
|
+
const raw = row[first(entry.source)];
|
|
789
|
+
const valueMap = entry.params?.valueMap ?? {};
|
|
790
|
+
mapped[first(entry.target)] = typeof raw === "string" && raw in valueMap ? valueMap[raw] : raw;
|
|
791
|
+
break;
|
|
792
|
+
}
|
|
793
|
+
case "split": {
|
|
794
|
+
const raw = row[first(entry.source)];
|
|
795
|
+
const targets = Array.isArray(entry.target) ? entry.target : [entry.target];
|
|
796
|
+
const parts = typeof raw === "string" ? raw.split(sep) : [];
|
|
797
|
+
targets.forEach((t, i) => {
|
|
798
|
+
mapped[t] = parts[i]?.trim();
|
|
799
|
+
});
|
|
800
|
+
break;
|
|
801
|
+
}
|
|
802
|
+
case "join": {
|
|
803
|
+
const sources = Array.isArray(entry.source) ? entry.source : [entry.source];
|
|
804
|
+
mapped[first(entry.target)] = sources.map((s) => row[s]).filter((v) => v !== void 0 && v !== null && v !== "").join(sep);
|
|
805
|
+
break;
|
|
806
|
+
}
|
|
807
|
+
default:
|
|
808
|
+
return {
|
|
809
|
+
ok: false,
|
|
810
|
+
status: 400,
|
|
811
|
+
code: "UNSUPPORTED_TRANSFORM",
|
|
812
|
+
error: `Mapping "${artifact.name}" uses unknown transform "${transform}"`
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
out.push(mapped);
|
|
817
|
+
}
|
|
818
|
+
return { ok: true, rows: out };
|
|
819
|
+
}
|
|
820
|
+
|
|
711
821
|
// src/rest-server.ts
|
|
712
822
|
var logError = (...args) => globalThis.console?.error(...args);
|
|
713
823
|
var TRANSLATABLE_META_TYPES = /* @__PURE__ */ new Set(["view", "action", "object", "app", "dashboard"]);
|
|
@@ -971,13 +1081,34 @@ async function parseXlsxToRows(buffer, mapping = {}, sheet) {
|
|
|
971
1081
|
async function prepareImportRequest(body, opts) {
|
|
972
1082
|
const { p, objectName, environmentId, maxRows } = opts;
|
|
973
1083
|
const dryRun = body?.dryRun === true;
|
|
974
|
-
|
|
975
|
-
|
|
1084
|
+
let writeMode = body?.writeMode === "update" || body?.writeMode === "upsert" ? body.writeMode : "insert";
|
|
1085
|
+
let matchFields = Array.isArray(body?.matchFields) ? body.matchFields.filter((f) => typeof f === "string" && f.length > 0) : [];
|
|
976
1086
|
const runAutomations = body?.runAutomations === true;
|
|
977
1087
|
const trimWhitespace = body?.trimWhitespace !== false;
|
|
978
1088
|
const nullValues = Array.isArray(body?.nullValues) ? body.nullValues.filter((v) => typeof v === "string") : void 0;
|
|
979
1089
|
const createMissingOptions = body?.createMissingOptions === true;
|
|
980
1090
|
const skipBlankMatchKey = body?.skipBlankMatchKey === true;
|
|
1091
|
+
const mappingName = typeof body?.mappingName === "string" && body.mappingName.length > 0 ? body.mappingName : void 0;
|
|
1092
|
+
const hasInlineMapping = Array.isArray(body?.mapping) && body.mapping.length > 0 || !Array.isArray(body?.mapping) && body?.mapping && typeof body.mapping === "object" && Object.keys(body.mapping).length > 0;
|
|
1093
|
+
if (mappingName && hasInlineMapping) {
|
|
1094
|
+
return { ok: false, status: 400, code: "CONFLICTING_MAPPING", error: "Provide either mappingName or an inline mapping, not both" };
|
|
1095
|
+
}
|
|
1096
|
+
let mappingArtifact;
|
|
1097
|
+
if (mappingName) {
|
|
1098
|
+
const detectedFormat = body?.format === "json" && Array.isArray(body?.rows) || Array.isArray(body) ? "json" : typeof body?.csv === "string" ? "csv" : typeof body?.xlsxBase64 === "string" ? "xlsx" : void 0;
|
|
1099
|
+
if (!detectedFormat) {
|
|
1100
|
+
return { ok: false, status: 400, code: "INVALID_REQUEST", error: 'Provide format:"csv" with csv text, format:"json" with rows[], or format:"xlsx" with xlsxBase64' };
|
|
1101
|
+
}
|
|
1102
|
+
const resolved = await resolveNamedMapping(p, { mappingName, objectName, detectedFormat });
|
|
1103
|
+
if (!resolved.ok) return resolved;
|
|
1104
|
+
mappingArtifact = resolved.artifact;
|
|
1105
|
+
if (body?.writeMode === void 0 && (mappingArtifact.mode === "update" || mappingArtifact.mode === "upsert")) {
|
|
1106
|
+
writeMode = mappingArtifact.mode;
|
|
1107
|
+
}
|
|
1108
|
+
if (matchFields.length === 0 && Array.isArray(mappingArtifact.upsertKey)) {
|
|
1109
|
+
matchFields = mappingArtifact.upsertKey.filter((f) => typeof f === "string" && f.length > 0);
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
981
1112
|
if (writeMode !== "insert" && matchFields.length === 0) {
|
|
982
1113
|
return { ok: false, status: 400, code: "INVALID_REQUEST", error: `writeMode "${writeMode}" requires a non-empty matchFields[]` };
|
|
983
1114
|
}
|
|
@@ -1019,6 +1150,11 @@ async function prepareImportRequest(body, opts) {
|
|
|
1019
1150
|
if (rows.length > maxRows) {
|
|
1020
1151
|
return { ok: false, status: 413, code: "PAYLOAD_TOO_LARGE", error: `Import limit is ${maxRows} rows per request (got ${rows.length}).` };
|
|
1021
1152
|
}
|
|
1153
|
+
if (mappingArtifact) {
|
|
1154
|
+
const applied = applyMappingToRows(rows, mappingArtifact);
|
|
1155
|
+
if (!applied.ok) return applied;
|
|
1156
|
+
rows = applied.rows;
|
|
1157
|
+
}
|
|
1022
1158
|
let metaMap = /* @__PURE__ */ new Map();
|
|
1023
1159
|
try {
|
|
1024
1160
|
let schema = void 0;
|