@saboosanket/code-generator
Version:
This npm package is a versatile setup tool tailored for Node.js projects, enabling users to generate customized project structures and functionalities. It supports integration with Google Cloud Platform (GCP), RabbitMQ, Redis, a Prisma query generator, an
91 lines (85 loc) • 3.48 kB
JavaScript
import { createQueries } from './query_generator.js'
import fs from 'fs'
import path from 'path'
import chalk from 'chalk'
/**
* Parses a Prisma schema to extract model names, primary keys, and foreign keys.
*
* @function findModelsAndKeys
*
* @param {string} schema - The content of the Prisma schema file as a string.
* @param {string} [modelName] - (Optional) A specific model name to filter the results by.
*
* @returns {Array<Object>} - An array of model objects, where each object contains:
* @property {string} model - The name of the model.
* @property {Array<string>} primary_key - An array of primary key field names.
* @property {Array<Object>} foreign_keys - An array of foreign key objects, each containing:
* @property {string} field - The field in the current model that holds the relationship.
* @property {string} foreignKey - The field in the current model that acts as the foreign key.
* @property {string} references - The field in the referenced model that the foreign key relates to.
*/
function findModelsAndKeys(schema, modelName = null) {
const modelRegex = /model\s+(\w+)\s*{[^}]*@id[^}]*}/g
const idFieldRegex = /(\w+)\s+\w+\s+@id/g
const foreignKeyRegex = /(\w+)\s+\w+\s+@relation\(fields: \[(\w+)\], references: \[(\w+)\]\)/g
const models = []
let match
// Find all models
while ((match = modelRegex.exec(schema)) !== null) {
const model = match[1]
// If a modelName is provided, skip other models
if (modelName && model !== modelName) {
continue
}
const modelContent = match[0]
// Find the primary key within the model
const primaryKey = [...modelContent.matchAll(idFieldRegex)].map(idMatch => idMatch[1])
// Find foreign keys within the model
const foreignKeys = [...modelContent.matchAll(foreignKeyRegex)].map(fkMatch => ({
field: fkMatch[1],
foreignKey: fkMatch[2],
references: fkMatch[3]
}))
models.push({ model, primary_key: primaryKey, foreign_keys: foreignKeys })
}
return models
}
/**
* Reads the Prisma schema file from the given path.
*
* @function prismaFileReader
*
* @param {string} prismaFilePath - The path to the Prisma schema file.
*
* @returns {string|null} - The content of the Prisma schema file as a string, or null if the file is not found.
*/
function prismaFileReader(prismaFilePath) {
if (fs.existsSync(prismaFilePath)) {
const schemaContent = fs.readFileSync(prismaFilePath, 'utf-8')
console.info(chalk.green('Prisma schema file content found'))
return schemaContent
} else {
console.error(chalk.red(`Prisma schema file not found at: ${prismaFilePath}`))
return null
}
}
/**
* Generates queries for models from the Prisma schema.
*
* @function createPrismaQueries
*
* @param {string} [modelName] - (Optional) The name of a specific model to generate queries for.
*/
async function createPrismaQueries(modelName = null) {
const prismaFilePath = path.join(process.cwd(), 'prisma', 'schema.prisma')
const prismaFileContent = prismaFileReader(prismaFilePath)
if (!prismaFileContent) {
console.error(chalk.red(`Prisma schema file not found at: ${prismaFilePath}`))
return
}
const modelInfo = findModelsAndKeys(prismaFileContent, modelName)
await createQueries(modelInfo)
}
export {
createPrismaQueries
}