UNPKG

kysely-mapper

Version:

Flexible Kysely-based utility for mapping between tables and objects

62 lines 2.96 kB
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()); }); }; import { CompilingValuesQuery } from './compiling-values-query.js'; /** * Compiling mapping query for inserting rows into a database table. */ export class CompilingMappingInsertQuery extends CompilingValuesQuery { constructor(db, qb, columnsToInsert, transforms, returnColumns) { super(db, returnColumns); this.columnsToInsert = columnsToInsert; this.transforms = transforms; const parameterizedValues = this.getParameterizedObject(columnsToInsert); this.qb = qb.values(parameterizedValues); } returnOne(obj) { return __awaiter(this, void 0, void 0, function* () { if (this.returnColumns.length === 0) { yield this.run(obj); return; } const transformedObj = this.applyInsertTransform(obj); const compiledQuery = this.instantiateWithReturns({}, transformedObj); const result = yield this.db.executeQuery(compiledQuery); if (result.rows.length === 0) { throw Error('No row returned from compiled insert expecting returned columns'); } return this.transforms.insertReturnTransform === undefined ? result.rows[0] : this.transforms.insertReturnTransform(obj, result.rows[0]); }); } /** * Runs the query, inserting rows into the table without returning any * columns. * * On the first execution, compiles and discards the underlying Kysely * query builder. Subsequent executions reuse the compiled query. * @param objOrObjs The object or objects to be inserted. * @returns Returns `true`; throws an exception on error. */ run(obj) { return __awaiter(this, void 0, void 0, function* () { const transformedObj = this.applyInsertTransform(obj); const compiledQuery = this.instantiateNoReturns({}, transformedObj); yield this.db.executeQuery(compiledQuery); return true; }); } applyInsertTransform(obj) { return this.transforms.insertTransform === undefined ? obj : this.transforms.insertTransform(obj, this.columnsToInsert); } } //# sourceMappingURL=compiling-insert-query.js.map