UNPKG

kysely-mapper

Version:

Flexible Kysely-based utility for mapping between tables and objects

138 lines 6.5 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()); }); }; 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 _MappingUpdateQuery_returningQB; /** * Mapping query for updating rows from a database table. */ export class MappingUpdateQuery { constructor(db, qb, transforms, returnColumns) { this.db = db; this.qb = qb; this.transforms = transforms; this.returnColumns = returnColumns; _MappingUpdateQuery_returningQB.set(this, null); } /** * 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 MappingUpdateQuery(this.db, factory(this.qb), this.transforms, this.returnColumns); } /** * Runs the query, returning the number of rows updated, in * the required client representation. * @param obj The object which which to update the rows. * @returns Number of rows updated, in client-requested representation. */ returnCount(obj) { return __awaiter(this, void 0, void 0, function* () { const result = yield this.loadUpdatingObject(this.qb, obj).executeTakeFirst(); return this.transforms.countTransform === undefined ? result.numUpdatedRows : this.transforms.countTransform(result.numUpdatedRows); }); } returnAll(obj) { return __awaiter(this, void 0, void 0, function* () { if (this.returnColumns.length === 0) { yield this.loadUpdatingObject(this.qb, obj).execute(); return; } const returns = yield this.loadUpdatingObject(this.getReturningQB(), obj).execute(); return this.transforms.updateReturnTransform === undefined ? returns : returns.map((row) => this.transforms.updateReturnTransform(obj, row)); }); } returnOne(obj) { return __awaiter(this, void 0, void 0, function* () { if (this.returnColumns.length === 0) { yield this.loadUpdatingObject(this.qb, obj).execute(); return; } const returns = yield this.loadUpdatingObject(this.getReturningQB(), obj).execute(); if (returns.length === 0) { return null; } return this.transforms.updateReturnTransform === undefined ? returns[0] : this.transforms.updateReturnTransform(obj, returns[0]); }); } /** * Runs the query, updating rows, without returning any columns. * @param obj The object which which to update the rows. * @returns `true` if any rows were updated, `false` otherwise. */ run(obj) { return __awaiter(this, void 0, void 0, function* () { const results = yield this.loadUpdatingObject(this.qb, obj).executeTakeFirst(); return results.numUpdatedRows !== BigInt(0); }); } /** * Returns an array of the columns to be updated, with * `['*']` indicating that all columns will be updated. * @returns An array of the columns to be updated. */ getUpdateColumns() { return ['*']; } /** * Returns a query builder for updating rows in the table and * returning values, caching the query builder for future use. * @returns A query builder for updating rows in the table and * returning values. */ getReturningQB() { if (__classPrivateFieldGet(this, _MappingUpdateQuery_returningQB, "f") === null) { __classPrivateFieldSet(this, _MappingUpdateQuery_returningQB, this.returnColumns[0] == '*' ? this.qb.returningAll() : this.qb.returning(this.returnColumns), "f"); } return __classPrivateFieldGet(this, _MappingUpdateQuery_returningQB, "f"); } /** * Loads the object with which to update rows. * @param qb The query builder to load the objects into. * @param obj The object with which to update rows. * @returns The query builder with the object loaded. */ loadUpdatingObject(qb, obj) { const updateColumns = this.getUpdateColumns(); const transformedObj = this.transforms.updateTransform === undefined ? obj : this.transforms.updateTransform(obj, updateColumns); return this.setColumnValues(qb, transformedObj); } /** * Sets the values of the updated columns. * @param qb The query builder to set the values into. * @param obj The object of column-value pairs to be updated. */ setColumnValues(qb, obj) { return qb.set(obj); } } _MappingUpdateQuery_returningQB = new WeakMap(); //# sourceMappingURL=update-query.js.map