UNPKG

@prisma-extensions/factory

Version:

Factory enables you to easily create and build database records for testing or seeding purposes.

40 lines (39 loc) 1.45 kB
import { mergeDeepWith } from 'immutable'; function mergeCallback(_oldVal, newVal) { return newVal; } function evaluateInput(input) { return typeof input === 'function' ? input() : input; } export function mergeData(baseInput, input) { const evaluatedBaseInput = { ...baseInput }; const evaluatedInput = { ...input }; const newKeys = new Set(Object.keys(input)); const newKeysToEvalute = []; for (const key in baseInput) { if (newKeys.has(key)) { newKeysToEvalute.push(key); continue; } evaluatedBaseInput[key] = evaluateInput(evaluatedBaseInput[key]); } for (const key of newKeysToEvalute) { evaluatedInput[key] = evaluateInput(evaluatedInput[key]); } return mergeDeepWith(mergeCallback, evaluatedBaseInput, evaluatedInput); } export function buildIncludes(data) { const include = {}; for (const key in data) { const value = data[key]; const isObject = typeof value === 'object' && value !== null; const hasRelation = isObject && (Object.prototype.hasOwnProperty.call(value, 'create') || Object.prototype.hasOwnProperty.call(value, 'connect') || Object.prototype.hasOwnProperty.call(value, 'connectOrCreate')); if (hasRelation) include[key] = true; } const hasInclude = Object.keys(include).length; return hasInclude ? include : undefined; }