refql
Version:
A Node.js and Deno library for composing and running SQL queries.
58 lines (57 loc) • 2.1 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isInsertRQLTag = void 0;
exports.createInsertRQLTag = createInsertRQLTag;
const _1 = require(".");
const consts_1 = require("../common/consts");
const Raw_1 = __importDefault(require("../SQLTag/Raw"));
const sql_1 = require("../SQLTag/sql");
const Values2D_1 = __importDefault(require("../SQLTag/Values2D"));
const CUD_1 = require("./CUD");
const getStandardProps_1 = __importDefault(require("./getStandardProps"));
const type = "refql/InsertRQLTag";
let prototype = Object.assign({}, CUD_1.CUDPrototype, {
constructor: createInsertRQLTag,
[consts_1.refqlType]: type,
interpret
});
function createInsertRQLTag(table, nodes, options) {
const tag = ((params) => {
return options.runner(tag, params);
});
Object.setPrototypeOf(tag, Object.assign(Object.create(Function.prototype), prototype, {
table,
nodes,
options
}));
return tag;
}
function interpret() {
const { nodes, table } = this;
let returning;
for (const node of nodes) {
if ((0, _1.isRQLTag)(node)) {
returning = returning ? returning.concat(node) : node;
}
else {
throw new Error(`Unknown Insertable RQLNode Type: "${String(node)}"`);
}
}
const props = (0, getStandardProps_1.default)(table);
let tag = (0, sql_1.sqlX) `
insert into ${(0, Raw_1.default)(`${table} (${props.map(f => f.col || f.as).join(", ")})`)}
values ${(0, Values2D_1.default)((params) => params.data.map(x => props.map(f => x[f.as] == null ? (0, Raw_1.default)("DEFAULT") : x[f.as])))}
returning ${(0, Raw_1.default)(`${props.map(p => `${table.name}.${p.col || p.as} "${p.as}"`).join(", ")}`)}
`;
return {
tag,
returning
};
}
const isInsertRQLTag = function (x) {
return x != null && x[consts_1.refqlType] === type;
};
exports.isInsertRQLTag = isInsertRQLTag;
;