Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function compileSqlx(results: ISqlxParseResults, path: string, defaultSchema: string) {
const resConfig =
results.config != null
? JSON.parse(results.config.replace(/(['"])?([a-zA-Z0-9_]+)(['"])?:/g, '"$2": '))
: null;
const queryConfigSchema = "schema" in resConfig && resConfig != null ? resConfig.schema : null;
const fQName = [queryConfigSchema || defaultSchema, utils.baseFilename(path)].join(".");
return `
const parsedConfig = ${results.config || "{}"};
// sqlxConfig should conform to the ISqlxConfig interface.
const sqlxConfig = {
name: "${fQName}",
type: "operations",
dependencies: [],
tags: [],
...parsedConfig
};
const sqlStatementCount = ${results.sql.length};
const hasIncremental = ${!!results.incremental};
const hasPreOperations = ${results.preOperations.length > 1 || results.preOperations[0] !== ""};
const hasPostOperations = ${results.postOperations.length > 1 || results.postOperations[0] !== ""};
const hasInputs = ${Object.keys(results.input).length > 0};
function compileSqlx(results: ISqlxParseResults, path: string) {
return `
const parsedConfig = ${results.config || "{}"};
// sqlxConfig should conform to the ISqlxConfig interface.
const sqlxConfig = {
name: "${utils.baseFilename(path)}",
type: "operations",
dependencies: [],
tags: [],
...parsedConfig
};
const sqlStatementCount = ${results.sql.length};
const hasIncremental = ${!!results.incremental};
const hasPreOperations = ${results.preOperations.length > 1 || results.preOperations[0] !== ""};
const hasPostOperations = ${results.postOperations.length > 1 || results.postOperations[0] !== ""};
const hasInputs = ${Object.keys(results.input).length > 0};
const action = session.sqlxAction({
sqlxConfig,
sqlStatementCount,
hasIncremental,
.map(path => {
return `
try { global.${utils.baseFilename(path)} = require("./${path}"); } catch (e) {
if (global.session.compileError) {
global.session.compileError(e, "${path}");
} else {
console.error('Error:', e.message, 'Path: "${path}"');
}
}`;
})
.join("\n");
.map(path => {
return `
try { global.${utils.baseFilename(path)} = require("./${path}"); } catch (e) {
if (global.session.compileError) {
global.session.compileError(e, "${path}");
} else {
console.error('Error:', e.message, 'Path: "${path}"');
}
}`;
})
.join("\n");
function compileOperationSql(code: string, path: string) {
const { sql, js } = extractJsBlocks(code);
const functionsBindings = getFunctionPropertyNames(OperationContext.prototype).map(
name => `const ${name} = ctx.${name}.bind(ctx);`
);
return `
operate("${utils.baseFilename(path)}").queries(ctx => {
${functionsBindings.join("\n")}
${js}
return \`${sql}\`.split("\\n---\\n");
})`;
}
function compileTableSql(code: string, path: string) {
const { sql, js } = extractJsBlocks(code);
const functionsBindings = getFunctionPropertyNames(TableContext.prototype).map(
name => `const ${name} = ${safelyBindCtxFunction(name)};`
);
return `
publish("${utils.baseFilename(path)}").query(ctx => {
${functionsBindings.join("\n")}
${js}
return \`${sql}\`;
})`;
}
function compileAssertionSql(code: string, path: string) {
const { sql, js } = extractJsBlocks(code);
const functionsBindings = getFunctionPropertyNames(AssertionContext.prototype).map(
name => `const ${name} = ${safelyBindCtxFunction(name)};`
);
return `
assert("${utils.baseFilename(path)}").query(ctx => {
${functionsBindings.join("\n")}
${js}
return \`${sql}\`;
})`;
}
function compileAssertionSql(code: string, path: string) {
const { sql, js } = extractJsBlocks(code);
const functionsBindings = getFunctionPropertyNames(AssertionContext.prototype).map(
name => `const ${name} = ctx.${name}.bind(ctx);`
);
return `
assert("${utils.baseFilename(path)}").query(ctx => {
${functionsBindings.join("\n")}
${js}
return \`${sql}\`;
})`;
}
function compileTableSql(code: string, path: string) {
const { sql, js } = extractJsBlocks(code);
const functionsBindings = getFunctionPropertyNames(TableContext.prototype).map(
name => `const ${name} = ctx.${name}.bind(ctx);`
);
return `
publish("${utils.baseFilename(path)}").query(ctx => {
${functionsBindings.join("\n")}
${js}
return \`${sql}\`;
})`;
}