UNPKG

@augment-vir/node

Version:

A collection of augments, helpers types, functions, and classes only for Node.js (backend) JavaScript environments.

84 lines (83 loc) 3.6 kB
import { assert, check } from '@augment-vir/assert'; import { arrayToObject, awaitedForEach, ensureErrorAndPrependMessage, filterMap, getObjectTypedEntries, getObjectTypedValues, mergeDefinedProperties, omitObjectKeys, prismaModelCreateExclude, prismaModelCreateOmitId, setFirstLetterCasing, StringCase, } from '@augment-vir/common'; export async function addData(prismaClient, data) { const dataArray = (check.isArray(data) ? data : [data]); await awaitedForEach(dataArray, async (dataEntry) => { await addModelDataObject(prismaClient, dataEntry); }); } async function addModelDataObject(prismaClient, data) { /** Add the mock data to the mock prisma client. */ await awaitedForEach(getObjectTypedEntries(data), async ([modelName, mockData,]) => { /** * This type is dumbed down to just `AnyObject[]` because the union of all possible * model data is just way too big (and not helpful as the inputs to this function are * already type guarded). */ const mockModelInstances = Array.isArray(mockData) ? mockData : getObjectTypedValues(mockData); const modelApi = prismaClient[setFirstLetterCasing(modelName, StringCase.Lower)]; assert.isDefined(modelApi, `No PrismaClient API found for model '${modelName}'`); try { const allData = filterMap(mockModelInstances, (entry) => { return entry; }, (mapped, modelEntry) => !modelEntry[prismaModelCreateExclude]); await awaitedForEach(allData, async (modelEntry) => { if (modelEntry[prismaModelCreateOmitId]) { modelEntry = omitObjectKeys(modelEntry, ['id']); } await modelApi.create({ data: modelEntry, }); }); } catch (error) { throw ensureErrorAndPrependMessage(error, `Failed to create many '${modelName}' entries.\n\n${JSON.stringify(mockModelInstances, null, 4)}\n\n`); } }); } const prismockKeys = [ 'getData', 'setData', ]; /** These are not the real model names, they are the names on the PrismaClient (which are lowercase). */ export function getAllPrismaModelKeys(prismaClient) { return Object.keys(prismaClient) .filter((key) => !key.startsWith('$') && !key.startsWith('_') && !prismockKeys.includes(key) && key !== 'constructor') .sort(); } const defaultPrismaDumpDataOptions = { limit: 100, omitFields: [], }; export async function dumpData(prismaClient, options = {}) { const modelNames = getAllPrismaModelKeys(prismaClient); const finalOptions = mergeDefinedProperties(defaultPrismaDumpDataOptions, options); const data = await arrayToObject(modelNames, async (modelName) => { try { const entries = await prismaClient[modelName].findMany(finalOptions.limit > 0 ? { take: finalOptions.limit, } : {}); if (!entries.length) { return undefined; } const filteredEntries = finalOptions.omitFields.length ? entries.map((entry) => omitObjectKeys(entry, finalOptions.omitFields)) : entries; return { key: modelName, value: filteredEntries, }; } catch (error) { throw ensureErrorAndPrependMessage(error, `Failed to read data for model '${modelName}'`); } }); return data; }