UNPKG

kysely-mapper

Version:

Flexible Kysely-based utility for mapping between tables and objects

96 lines 4.54 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 updating rows in a database table. */ export class CompilingMappingUpdateQuery extends CompilingValuesQuery { constructor(db, qb, columnsToUpdate, transforms, returnColumns) { super(db, returnColumns); this.columnsToUpdate = columnsToUpdate; this.transforms = transforms; const parameterizedValues = this.getParameterizedObject(columnsToUpdate); this.qb = qb.set(parameterizedValues); } /** * Runs the query, returning the number of rows updated, in the required * client representation. Accepts values for any parameters embedded in * the query. * * On the first execution, compiles and discards the underlying Kysely * query builder. Subsequent executions reuse the compiled query. * @param obj The object which which to update the rows. * @returns Number of rows updated, in client-requested representation. */ returnCount(params, obj) { return __awaiter(this, void 0, void 0, function* () { const transformedObj = this.applyUpdateTransform(obj); const compiledQuery = this.instantiateNoReturns(params, transformedObj); const result = yield this.db.executeQuery(compiledQuery); return this.transforms.countTransform === undefined ? result.numAffectedRows : this.transforms.countTransform(result.numAffectedRows); }); } returnAll(params, obj) { return __awaiter(this, void 0, void 0, function* () { if (this.returnColumns.length === 0) { yield this.run(params, obj); return; } const transformedObj = this.applyUpdateTransform(obj); const compiledQuery = this.instantiateWithReturns(params, transformedObj); const result = yield this.db.executeQuery(compiledQuery); return this.transforms.updateReturnTransform === undefined ? result.rows : result.rows.map((row) => this.applyUpdateReturnTransform(obj, row)); }); } returnOne(params, obj) { return __awaiter(this, void 0, void 0, function* () { if (this.returnColumns.length === 0) { yield this.run(params, obj); return; } const transformedObj = this.applyUpdateTransform(obj); const compiledQuery = this.instantiateWithReturns(params, transformedObj); const result = yield this.db.executeQuery(compiledQuery); if (result.rows.length === 0) { return null; } return this.applyUpdateReturnTransform(obj, result.rows[0]); }); } /** * Runs the query, updating rows, without returning any columns. Accepts * values for any parameters embedded in the query. * * On the first execution, compiles and discards the underlying Kysely * query builder. Subsequent executions reuse the compiled query. * @param obj The object which which to update the rows. * @returns `true` if any rows were updated, `false` otherwise. */ run(params, obj) { return __awaiter(this, void 0, void 0, function* () { return (yield this.returnCount(params, obj)) !== 0; }); } applyUpdateTransform(obj) { return this.transforms.updateTransform === undefined ? obj : this.transforms.updateTransform(obj, this.columnsToUpdate); } applyUpdateReturnTransform(source, returns) { return this.transforms.updateReturnTransform === undefined ? returns : this.transforms.updateReturnTransform(source, returns); } } //# sourceMappingURL=compiling-update-query.js.map