kysely-mapper
Version:
Flexible Kysely-based utility for mapping between tables and objects
64 lines • 2.84 kB
JavaScript
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 { CompilingMappingDeleteQuery } from './compiling-delete-query.js';
/**
* Mapping query for deleting rows from a database table.
*/
export class MappingDeleteQuery {
constructor(db, qb, transforms) {
this.db = db;
this.qb = qb;
this.transforms = transforms;
}
/**
* 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 delete query.
*/
compile() {
return new CompilingMappingDeleteQuery(this.db, this.qb, this.transforms);
}
/**
* Runs the query, returning the number of rows deleted, converted to
* the required client representation.
* @returns Number of rows deleted, in client-requested representation.
*/
returnCount() {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this.qb.executeTakeFirst();
return this.transforms.countTransform === undefined
? result.numDeletedRows
: this.transforms.countTransform(result.numDeletedRows);
});
}
/**
* Modifies the underlying Kysely query builder.
* @param factory A function that takes the current query builder and
* returns a new query builder.
*/
modify(factory) {
return new MappingDeleteQuery(this.db, factory(this.qb), this.transforms);
}
/**
* Runs the query, deleting the indicated rows, returning nothing.
* @returns `true` if any rows were deleted, `false` otherwise.
*/
run() {
return __awaiter(this, void 0, void 0, function* () {
const results = yield this.qb.executeTakeFirst();
return results.numDeletedRows !== BigInt(0);
});
}
}
//# sourceMappingURL=delete-query.js.map