@noy-db/as-xlsx 0.2.0-pre.23 → 0.2.0-pre.24
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 +91 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +95 -1
- package/dist/index.d.ts +95 -1
- package/dist/index.js +90 -0
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -40,6 +40,7 @@ __export(index_exports, {
|
|
|
40
40
|
styled: () => styled,
|
|
41
41
|
toBytes: () => toBytes,
|
|
42
42
|
toBytesFromCollection: () => toBytesFromCollection,
|
|
43
|
+
toBytesMultiVault: () => toBytesMultiVault,
|
|
43
44
|
write: () => write,
|
|
44
45
|
writeXlsx: () => writeXlsx,
|
|
45
46
|
zodSourceFor: () => zodSourceFor
|
|
@@ -612,6 +613,95 @@ async function write(vault, path, options) {
|
|
|
612
613
|
const { writeFile } = await import("fs/promises");
|
|
613
614
|
await writeFile(path, bytes);
|
|
614
615
|
}
|
|
616
|
+
async function toBytesMultiVault(entries, options = {}) {
|
|
617
|
+
const sep = options.sheetSeparator ?? "_";
|
|
618
|
+
for (const entry of entries) {
|
|
619
|
+
entry.vault.assertCanExport("plaintext", "xlsx");
|
|
620
|
+
}
|
|
621
|
+
const loaded = [];
|
|
622
|
+
const denormIndex = /* @__PURE__ */ new Map();
|
|
623
|
+
for (const entry of entries) {
|
|
624
|
+
const prefix = entry.label ?? entry.vault.name;
|
|
625
|
+
for (const s of entry.sheets) {
|
|
626
|
+
const all = await entry.vault.collection(s.collection).list();
|
|
627
|
+
const records = [];
|
|
628
|
+
for (const item of all) {
|
|
629
|
+
const r = item;
|
|
630
|
+
const allow = entry.closure?.get(s.collection);
|
|
631
|
+
if (allow && !allow.has(String(r.id))) continue;
|
|
632
|
+
if (s.filter && !s.filter(r)) continue;
|
|
633
|
+
records.push(r);
|
|
634
|
+
}
|
|
635
|
+
loaded.push({ entry, prefix, sheetOpt: s, records });
|
|
636
|
+
const indexKey = `${prefix}/${s.collection}`;
|
|
637
|
+
const collectionIndex = denormIndex.get(indexKey) ?? /* @__PURE__ */ new Map();
|
|
638
|
+
denormIndex.set(indexKey, collectionIndex);
|
|
639
|
+
for (const r of records) {
|
|
640
|
+
const idVal = r.id;
|
|
641
|
+
if (idVal != null) collectionIndex.set(safeStringify(idVal), r);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
for (const ls of loaded) {
|
|
646
|
+
for (const d of ls.sheetOpt.denormalize ?? []) {
|
|
647
|
+
if (d.from.keyField === "id") continue;
|
|
648
|
+
const indexKey = `${d.from.label}/${d.from.collection}`;
|
|
649
|
+
const idx = denormIndex.get(indexKey);
|
|
650
|
+
if (!idx) continue;
|
|
651
|
+
const srcLoaded = loaded.find(
|
|
652
|
+
(l) => l.prefix === d.from.label && l.sheetOpt.collection === d.from.collection
|
|
653
|
+
);
|
|
654
|
+
if (!srcLoaded) continue;
|
|
655
|
+
for (const r of srcLoaded.records) {
|
|
656
|
+
const kv = r[d.from.keyField];
|
|
657
|
+
if (kv != null) idx.set(safeStringify(kv), r);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
const allSheets = [];
|
|
662
|
+
const manifestRows = [];
|
|
663
|
+
for (const { prefix, sheetOpt: s, records } of loaded) {
|
|
664
|
+
const baseColumns = s.columns ?? inferColumns(records);
|
|
665
|
+
const denormDefs = s.denormalize ?? [];
|
|
666
|
+
if (denormDefs.length === 0) {
|
|
667
|
+
const sheetName2 = `${prefix}${sep}${s.name}`;
|
|
668
|
+
allSheets.push(buildFlatSheet(sheetName2, baseColumns, records));
|
|
669
|
+
manifestRows.push([prefix, s.collection, records.length]);
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
const allColumns = [...baseColumns, ...denormDefs.map((d) => d.column)];
|
|
673
|
+
const sheetName = `${prefix}${sep}${s.name}`;
|
|
674
|
+
const rows = records.map((r) => {
|
|
675
|
+
const baseCells = baseColumns.map((c) => r[c] ?? null);
|
|
676
|
+
const denormCells = denormDefs.map((d) => {
|
|
677
|
+
const idxKey = `${d.from.label}/${d.from.collection}`;
|
|
678
|
+
const idx = denormIndex.get(idxKey);
|
|
679
|
+
if (!idx) return "";
|
|
680
|
+
const fkVal = r[d.localField];
|
|
681
|
+
if (fkVal == null) return "";
|
|
682
|
+
const supporting = idx.get(safeStringify(fkVal));
|
|
683
|
+
if (!supporting) return "";
|
|
684
|
+
return supporting[d.from.pick] ?? "";
|
|
685
|
+
});
|
|
686
|
+
return [...baseCells, ...denormCells];
|
|
687
|
+
});
|
|
688
|
+
allSheets.push({ name: sheetName, header: allColumns, rows });
|
|
689
|
+
manifestRows.push([prefix, s.collection, records.length]);
|
|
690
|
+
}
|
|
691
|
+
allSheets.unshift({
|
|
692
|
+
name: "_manifest",
|
|
693
|
+
header: ["Vault", "Collection", "Records"],
|
|
694
|
+
rows: manifestRows
|
|
695
|
+
});
|
|
696
|
+
return writeXlsx(allSheets);
|
|
697
|
+
}
|
|
698
|
+
function buildFlatSheet(name, columns, records) {
|
|
699
|
+
return {
|
|
700
|
+
name,
|
|
701
|
+
header: [...columns],
|
|
702
|
+
rows: records.map((r) => columns.map((c) => r[c] ?? null))
|
|
703
|
+
};
|
|
704
|
+
}
|
|
615
705
|
function safeStringify(v) {
|
|
616
706
|
if (typeof v === "string") return v;
|
|
617
707
|
if (typeof v === "number" || typeof v === "boolean" || typeof v === "bigint") return String(v);
|
|
@@ -1072,6 +1162,7 @@ function excelSerialToMs(serial) {
|
|
|
1072
1162
|
styled,
|
|
1073
1163
|
toBytes,
|
|
1074
1164
|
toBytesFromCollection,
|
|
1165
|
+
toBytesMultiVault,
|
|
1075
1166
|
write,
|
|
1076
1167
|
writeXlsx,
|
|
1077
1168
|
zodSourceFor
|