UNPKG

@openinc/parse-server-opendash

Version:
45 lines (44 loc) 1.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSourceMetaMap = getSourceMetaMap; const types_1 = require("../../../types"); /** * Get all meta values and fields from a list of sources * @param sources the sources to get the meta values and fields from * @returns a Map<string, string[]> where the key is the meta field and the value is an array of meta values found for that key in all sources */ async function getSourceMetaMap(params) { const sessionToken = params.user?.getSessionToken(); if (!sessionToken) { throw new Error("User is not authenticated"); } if (!params.sourceIds || params.sourceIds.length === 0) { throw new Error("No source IDs provided"); } const metaMap = new Map(); // metaField => metaValue const sources = await new Parse.Query(types_1.Source) .limit(1000000) .containedIn("objectId", params.sourceIds) .include("meta") .find({ sessionToken: sessionToken, }); // Fetch the sources sources.forEach((source) => { if (!source.meta?.data) return; Object.keys(source.meta.data).forEach((metaField) => { const existingMapValue = metaMap.get(metaField) || []; const metaValue = source.meta ? source.meta.data[metaField] : undefined; if (Array.isArray(metaValue)) { metaValue .filter(Boolean) .forEach((value) => existingMapValue.push(value)); } else { existingMapValue.push(metaValue); } metaMap.set(metaField, existingMapValue); }); }); return metaMap; }