assetflow/src/assets/helper.ts

94 lines
3.4 KiB
TypeScript

// @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<string, object> = {};
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;
}
/**
* Helper function for parsing a string into a prisma connect object
*
* @export
* @param {string} data
* @param {string} [relation_name='id']
* @returns {undefined || object} undefined or prisma connect object
*/
export function parseIntRelation(data: string, relation_name: string = 'id', doNotDisconnect: boolean = false) {
// This function is perfect. If data is not a valid number, return `undefined`
// If it is a valid number return `{connect: {relation_name: yourNumber}}}`
// This can be used by prisma to connect relations
// If the incoming data is null or empty, return a prisma disconnect object instead of a connect one
if (data === null || data === '') {
if (doNotDisconnect) {
return undefined;
}
return JSON.parse(`{
"disconnect": true
}`);
}
return isNaN(parseInt(data)) ? undefined : JSON.parse(`{
"connect": {
"${relation_name}": ${parseInt(data)}
}
}`);
}
export function parseIntOrUndefined(data: string) {
return isNaN(parseInt(data)) ? undefined : parseInt(data);
}