UNPKG

venom-generator

Version:

In the way of code generation, complete the storage model->computation model (business API) transformation to achieve the goal of design and implementation

141 lines 5.82 kB
"use strict"; // 模型解析模块 Object.defineProperty(exports, "__esModule", { value: true }); const fs = require("fs"); const path = require("path"); const yaml = require("js-yaml"); const Ajv = require("ajv"); const prisma_datamodel_1 = require("prisma-datamodel"); const schema = require("prisma-json-schema/dist/schema.json"); const ajv = new Ajv().addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json')); const validate = ajv.compile(schema); function findDatamodelAndComputeSchema(configPath, config) { const typeDefs = getTypesString(config.datamodel, path.dirname(configPath)); const databaseType = getDatabaseType(config); const ParserInstance = prisma_datamodel_1.DefaultParser.create(databaseType); return { datamodel: ParserInstance.parseFromSchemaString(typeDefs), databaseType }; } exports.findDatamodelAndComputeSchema = findDatamodelAndComputeSchema; function readPrismaYml(prismaPath) { const configPath = findPrismaConfigFile(prismaPath); if (!configPath) { throw new Error('Could not find `prisma.yml` file'); } try { const file = fs.readFileSync(configPath, 'utf-8'); const config = yaml.safeLoad(file); const valid = validate(config); if (!valid) { let errorMessage = `Invalid prisma.yml file` + '\n' + ajv.errorsText(validate.errors); throw new Error(errorMessage); } if (!config.datamodel) { throw new Error('Invalid prisma.yml file: Missing `datamodel` property'); } if (!config.generate) { throw new Error('Invalid prisma.yml file: Missing `generate` property for a `prisma-client`'); } return { config, configPath }; } catch (e) { throw new Error(`Yaml parsing error in ${configPath}: ${e.message}`); } } exports.readPrismaYml = readPrismaYml; function findPrismaConfigFile(prismaPath) { let definitionPath = path.join(prismaPath, 'prisma.yml'); if (fs.existsSync(definitionPath)) { return definitionPath; } definitionPath = path.join(process.cwd(), 'prisma', 'prisma.yml'); if (fs.existsSync(definitionPath)) { return definitionPath; } return null; } function getPrismaClientDir(prismaClientDir, prisma, rootPath) { if (prismaClientDir) { return prismaClientDir.startsWith('/') ? prismaClientDir : path.resolve(rootPath, prismaClientDir); } const clientGenerators = prisma.config.generate.filter((gen) => ['typescript-client', 'javascript-client'].includes(gen.generator)); if (clientGenerators.length === 0) { throw new Error('No prisma-client generators were found in your prisma.yml file'); } if (clientGenerators.length > 1) { throw new Error('Several prisma-client generators are defined in your prisma.yml file. If all are needed, use the `--client` option to point to the right one.'); } return path.join(path.dirname(prisma.configPath), clientGenerators[0].output); } exports.getPrismaClientDir = getPrismaClientDir; function getTypesString(datamodel, definitionDir) { const typesPaths = datamodel ? (Array.isArray(datamodel) ? datamodel : [datamodel]) : []; let allTypes = ''; typesPaths.forEach(unresolvedTypesPath => { const typesPath = path.join(definitionDir, unresolvedTypesPath); if (fs.existsSync(typesPath)) { const types = fs.readFileSync(typesPath, 'utf-8'); allTypes += types + '\n'; } else { throw new Error(`The types definition file "${typesPath}" could not be found.`); } }); return allTypes; } function findRootDirectory() { const cwd = process.cwd(); const tsConfig = findConfigFile(cwd, 'tsconfig.json'); if (tsConfig) { return path.dirname(tsConfig); } const packageJson = findConfigFile(cwd, 'package.json'); if (packageJson) { return path.dirname(packageJson); } return cwd; } exports.findRootDirectory = findRootDirectory; function findConfigFile(searchPath, configName = 'package.json') { return forEachAncestorDirectory(searchPath, ancestor => { const fileName = path.join(ancestor, configName); return fs.existsSync(fileName) ? fileName : undefined; }); } /** Calls `callback` on `directory` and every ancestor directory it has, returning the first defined result. */ function forEachAncestorDirectory(directory, callback) { while (true) { const result = callback(directory); if (result !== undefined) { return result; } const parentPath = path.dirname(directory); if (parentPath === directory) { return undefined; } directory = parentPath; } } function getImportPathRelativeToOutput(importPath, outputDir) { let relativePath = path.relative(path.dirname(outputDir), importPath); if (!relativePath.startsWith('.')) { relativePath = './' + relativePath; } // remove .ts or .js file extension relativePath = relativePath.replace(/\.(ts|js)$/, ''); // remove /index relativePath = relativePath.replace(/\/index$/, ''); // replace \ with / relativePath = relativePath.replace(/\\/g, '/'); return relativePath; } exports.getImportPathRelativeToOutput = getImportPathRelativeToOutput; function getDatabaseType(definition) { if (!definition.databaseType) { return prisma_datamodel_1.DatabaseType.postgres; } return definition.databaseType === 'document' ? prisma_datamodel_1.DatabaseType.mongo : prisma_datamodel_1.DatabaseType.postgres; } //# sourceMappingURL=modelSchema.js.map