kysely-mapper
Version:
Flexible Kysely-based utility for mapping between tables and objects
77 lines • 3.81 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _CompilingValuesQuery_compiledQueryNoReturns, _CompilingValuesQuery_compiledQueryWithReturns;
import { ParameterizedValue } from 'kysely-params';
/**
* Parameterized value placeholder for inserted or added column values.
*/
class ColumnParameter {
constructor(columnName) {
this.columnName = columnName;
}
}
/**
* Base class for compilable inserting and updating mapping queriees.
*/
export class CompilingValuesQuery {
constructor(db, returnColumns) {
this.db = db;
this.returnColumns = returnColumns;
this.qb = null;
_CompilingValuesQuery_compiledQueryNoReturns.set(this, void 0);
_CompilingValuesQuery_compiledQueryWithReturns.set(this, void 0);
}
getParameterizedObject(columnsToAllow) {
return Object.fromEntries(columnsToAllow.map((col) => [col, new ColumnParameter(col)]));
}
instantiateNoReturns(params, obj) {
this.compileQueries();
return this.instantiate(__classPrivateFieldGet(this, _CompilingValuesQuery_compiledQueryNoReturns, "f"), params, obj);
}
instantiateWithReturns(params, obj) {
this.compileQueries();
return this.instantiate(__classPrivateFieldGet(this, _CompilingValuesQuery_compiledQueryWithReturns, "f"), params, obj);
}
compileQueries() {
if (this.qb !== null) {
__classPrivateFieldSet(this, _CompilingValuesQuery_compiledQueryNoReturns, this.qb.compile(), "f");
__classPrivateFieldSet(this, _CompilingValuesQuery_compiledQueryWithReturns, this.getReturningQB().compile(), "f");
this.qb = null;
}
}
getReturningQB() {
return this.returnColumns[0] == '*'
? this.qb.returningAll()
: this.qb.returning(this.returnColumns);
}
instantiate(compiledQuery, params, obj) {
return {
query: compiledQuery.query,
sql: compiledQuery.sql,
parameters: compiledQuery.parameters.map((value) => value instanceof ColumnParameter
? this.verifiedValue(obj, value.columnName)
: value instanceof ParameterizedValue
? params[value.parameterName]
: value),
};
}
verifiedValue(obj, column) {
const value = obj[column];
// ensure the output of the applied transform works for the present query
if (value === undefined) {
throw new Error(`Specified column '${column}' missing from values object (compiled)`);
}
return value;
}
}
_CompilingValuesQuery_compiledQueryNoReturns = new WeakMap(), _CompilingValuesQuery_compiledQueryWithReturns = new WeakMap();
//# sourceMappingURL=compiling-values-query.js.map