kysely-mapper
Version:
Flexible Kysely-based utility for mapping between tables and objects
35 lines • 1.6 kB
JavaScript
import { MappingInsertQuery } from './insert-query.js';
import { CompilingMappingInsertQuery } from './compiling-insert-query.js';
import { restrictValues } from '../lib/restrict-values.js';
/**
* Mapping query for inserting rows into a database table,
* inserting a specified subset of the insertable columns.
*/
export class SubsettingMappingInsertQuery extends MappingInsertQuery {
constructor(db, qb, columnsToInsert, transforms, returnColumns) {
super(db, qb, transforms, returnColumns);
this.columnsToInsert = columnsToInsert;
}
/**
* Returns a compiling query that can be executed multiple times with
* different parameters (if any parameters were provided), but which only
* compiles the underlying Kysely query builder on the first execution.
* Frees the query builder on the first execution to reduce memory usage.
* @typeParam Parameters Record characterizing the parameter names and
* types that were previously embedded in the query, if any.
* @returns A compiling insert query.
*/
compile() {
return new CompilingMappingInsertQuery(this.db, this.qb, this.columnsToInsert, this.transforms, this.returnColumns);
}
getInsertColumns() {
return this.columnsToInsert;
}
setColumnValues(qb, objOrObjs) {
if (Array.isArray(objOrObjs)) {
return qb.values(objOrObjs.map((obj) => restrictValues(obj, this.columnsToInsert)));
}
return qb.values(restrictValues(objOrObjs, this.columnsToInsert));
}
}
//# sourceMappingURL=subsetting-insert-query.js.map