// @ts-nocheck import { formatAst, parsePrismaSchema } from '@loancrate/prisma-schema-parser'; import * as fs from 'fs'; import { log } from '../index.js'; /** * A helper function which returns every models' required, optional and relation fields * * @returns {{}} An object containing every model and their required, optional and relation fields */ function returnAllModelFieldData() { const ast = parsePrismaSchema(fs.readFileSync('./prisma/schema.prisma', { encoding: 'utf8' })); const modelData: Record = {}; Object.keys(ast.declarations).forEach((key) => { if (ast.declarations[key].kind === 'model') { log.helper.debug('Found model: ', ast.declarations[key].name.value); Object.keys(ast.declarations[key].members).forEach((key2) => { if (ast.declarations[key].members[key2].kind === 'field') { const currentField = ast.declarations[key].members[key2]; switch (currentField.type.kind) { case 'optional': log.helper.debug('Found optional field:', currentField.name.value); modelData[ast.declarations[key].name.value].optional.push(currentField.name.value); break; case 'typeId': // Required fields are not always required for our purposes, fields with a default value are not required let isRequired = true; if (currentField.attributes.length > 0) { Object.keys(currentField.attributes).forEach((key3) => { if (currentField.attributes[key3].path != {}) { if (currentField.attributes[key3].path.value == 'default') { const defValue = currentField.attributes[key3].args[0].value; log.helper.debug('Found default field:', currentField.name.value, 'with value: ', defValue); modelData[ast.declarations[key].name.value].optional.push(currentField.name.value); isRequired = false; } } }); } if (isRequired) { modelData[ast.declarations[key].name.value].required.push(currentField.name.value); log.helper.debug('Found required field: ', currentField.name.value); } break; case 'list': log.helper.debug('Found relation/list field:', currentField.name.value); modelData[ast.declarations[key].name.value].relation.push(currentField.name.value); break; default: log.helper.error('Unable to determine field type:', currentField.name.value, currentField.type.kind); } } }); } }); return modelData; }