sql-code-generator
Version:
Generate code from your SQL schema and queries for type safety and development speed.
95 lines • 5.08 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.extractDeclarationsFromGlobedFile = exports.DeclarationType = void 0;
const error_fns_1 = require("@ehmpathy/error-fns");
const domain_1 = require("../../../../domain");
const extractSqlFromFile_1 = require("../../../common/extractSqlFromFile");
var DeclarationType;
(function (DeclarationType) {
DeclarationType["QUERY"] = "QUERY";
DeclarationType["RESOURCE"] = "RESOURCE\",";
})(DeclarationType || (exports.DeclarationType = DeclarationType = {}));
const extractDeclarationsFromGlobedFile = (_a) => __awaiter(void 0, [_a], void 0, function* ({ rootDir, relativePath, type, }) {
// define the file path
const filePath = `${rootDir}/${relativePath}`;
// extract the sql
const sql = yield (0, extractSqlFromFile_1.extractSqlFromFile)({ filePath });
// define the declaration for a query
if (type === DeclarationType.QUERY) {
// note: we only support one declaration per query, since we require the comments to define the query name
return [
new domain_1.QueryDeclaration({
path: relativePath,
sql,
}),
];
}
// define the declarations for a resource
if (type === DeclarationType.RESOURCE) {
// split up the sql by create resource statements, to support multiple resources per sql file
const sqlCreateResourceStatements = sql
.split(/(create(?:\sor\sreplace)?\s(?:table|function|view))/gi)
.reduce((pairs, thisPart) => {
// define what kind of part this is
const isThisPartCreateDeclaration = thisPart
.trim()
.toLowerCase()
.startsWith('create');
// define the last pair
const lastPair = pairs.slice(-1)[0];
// if the last pair is not defined yet
if (!lastPair) {
// if this part is not a create declaration, then do nothing; we can ignore the prefixes
if (!isThisPartCreateDeclaration)
return pairs;
// otherwise, since it is a create declaration, start the first pair
return [{ createDeclaration: thisPart }];
}
// if the last pair already has a resource declaration
if (lastPair.resourceDeclaration) {
// if this part is a create declaration, then start a new pair
if (isThisPartCreateDeclaration)
return [...pairs, { createDeclaration: thisPart }];
// otherwise, this is unexpected, so fail fast
throw new error_fns_1.UnexpectedCodePathError('lastPair.resourceDeclaration already exists but thisPart.!isCreateDeclaration. how is that possible?', { lastPair, thisPart });
}
// if the last pair does not have a resource declaration
if (!lastPair.resourceDeclaration) {
// if this part is a resource declaration, then complete this pair
if (!isThisPartCreateDeclaration)
return [
...pairs.slice(0, -1),
Object.assign(Object.assign({}, lastPair), { resourceDeclaration: thisPart }),
];
// otherwise, this is unexpected, so fail fast
throw new error_fns_1.UnexpectedCodePathError('lastPair.resourceDeclaration does not exist, but thisPart.isCreateDeclaration. how is that possible?', {
lastPair,
thisPart,
});
}
// we should never reach here, as the above should have captured every scenario, so fail fast
throw new error_fns_1.UnexpectedCodePathError('should never reach here. why?', {
lastPair,
thisPart,
});
}, [])
.map((pair) => [pair.createDeclaration, pair.resourceDeclaration].join(''));
// return a declaration for each
return sqlCreateResourceStatements.map((sql) => new domain_1.ResourceDeclaration({
path: relativePath,
sql,
}));
}
throw new Error('unexpected'); // fail fast
});
exports.extractDeclarationsFromGlobedFile = extractDeclarationsFromGlobedFile;
//# sourceMappingURL=extractDeclarationsFromGlobedFile.js.map