sql-code-generator
Version:
Generate code from your SQL schema and queries for type safety and development speed.
81 lines • 3.86 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readConfig = void 0;
const domain_1 = require("../../../../domain");
const getAllPathsMatchingGlobs_1 = require("../getAllPathsMatchingGlobs/getAllPathsMatchingGlobs");
const extractDeclarationsFromGlobedFile_1 = require("./extractDeclarationsFromGlobedFile");
const readYmlFile_1 = require("./utils/readYmlFile");
/*
1. read the yml file
2. get the connection (i.e., import from the specified connection file)
3. get the definitions and flatten them
*/
const readConfig = (_a) => __awaiter(void 0, [_a], void 0, function* ({ filePath }) {
const configDir = filePath.split('/').slice(0, -1).join('/'); // drops the file name
// get the yml
const contents = yield (0, readYmlFile_1.readYmlFile)({ filePath });
// get the output dir
if (!contents.generates)
throw new Error('generates key must be defined');
if (!contents.generates.types)
throw new Error('generates.types must specify where to output the generated types');
// get the language and dialect
if (!contents.language)
throw new Error('language must be defined');
const language = contents.language;
if (!contents.dialect)
throw new Error('dialect must be defined');
const dialect = `${contents.dialect}`; // ensure that we read it as a string, as it could be a number
// get the resource declarations
const resourceGlobs = contents.resources;
const resourcePaths = yield (0, getAllPathsMatchingGlobs_1.getAllPathsMatchingGlobs)({
globs: resourceGlobs,
root: configDir,
});
const resourceDeclarations = (yield Promise.all(resourcePaths
.sort() // for determinism in order
.map((relativePath) => (0, extractDeclarationsFromGlobedFile_1.extractDeclarationsFromGlobedFile)({
rootDir: configDir,
relativePath,
type: extractDeclarationsFromGlobedFile_1.DeclarationType.RESOURCE,
})))).flat();
// get the query declarations
const queryDeclarations = yield (() => __awaiter(void 0, void 0, void 0, function* () {
const queryGlobs = contents.queries;
if (!queryGlobs)
return [];
const queryPaths = yield (0, getAllPathsMatchingGlobs_1.getAllPathsMatchingGlobs)({
globs: queryGlobs,
root: configDir,
});
return (yield Promise.all(queryPaths
.sort() // for determinism in order
.map((relativePath) => (0, extractDeclarationsFromGlobedFile_1.extractDeclarationsFromGlobedFile)({
rootDir: configDir,
relativePath,
type: extractDeclarationsFromGlobedFile_1.DeclarationType.QUERY,
})))).flat();
}))();
// return the results
return new domain_1.GeneratorConfig({
rootDir: configDir,
generates: {
types: contents.generates.types,
queryFunctions: contents.generates.queryFunctions,
},
language,
dialect,
declarations: [...resourceDeclarations, ...queryDeclarations],
});
});
exports.readConfig = readConfig;
//# sourceMappingURL=readConfig.js.map