|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 12 | +exports.MySQLPoolConnection = void 0; |
| 13 | +const Connection_1 = require("./Connection"); |
| 14 | +class MySQLPoolConnection extends Connection_1.Connection { |
| 15 | + constructor(pool) { |
| 16 | + super(); |
| 17 | + this.pool = pool; |
| 18 | + } |
| 19 | + query(schema, criteria) { |
| 20 | + return __awaiter(this, void 0, void 0, function* () { |
| 21 | + const keys = Object.keys(criteria); |
| 22 | + const values = Object.values(criteria); |
| 23 | + const placeholders = keys.map(key => `${key} = ?`).join(' AND '); |
| 24 | + const [rows] = yield this.pool.query(`SELECT * |
| 25 | + FROM ${schema} |
| 26 | + WHERE ${placeholders}`, values); |
| 27 | + return rows; |
| 28 | + }); |
| 29 | + } |
| 30 | + insert(schema, item) { |
| 31 | + return __awaiter(this, void 0, void 0, function* () { |
| 32 | + const [result] = yield this.pool.query(`INSERT INTO ${schema} |
| 33 | + SET ?`, item); |
| 34 | + if ('insertId' in result) { |
| 35 | + return Object.assign(Object.assign({}, item), { id: result.insertId }); |
| 36 | + } |
| 37 | + throw new Error('Insert operation did not return an insertId.'); |
| 38 | + }); |
| 39 | + } |
| 40 | + update(schema, criteria, item) { |
| 41 | + return __awaiter(this, void 0, void 0, function* () { |
| 42 | + const updateKeys = Object.keys(item); |
| 43 | + const updateValues = Object.values(item); |
| 44 | + const updatePlaceholders = updateKeys.map(key => `${key} = ?`).join(', '); |
| 45 | + const criteriaKeys = Object.keys(criteria); |
| 46 | + const criteriaValues = Object.values(criteria); |
| 47 | + const criteriaPlaceholders = criteriaKeys.map(key => `${key} = ?`).join(' AND '); |
| 48 | + yield this.pool.query(`UPDATE ${schema} |
| 49 | + SET ${updatePlaceholders} |
| 50 | + WHERE ${criteriaPlaceholders}`, [...updateValues, ...criteriaValues]); |
| 51 | + }); |
| 52 | + } |
| 53 | + upsert(schema, criteria, item) { |
| 54 | + return __awaiter(this, void 0, void 0, function* () { |
| 55 | + const keys = Object.keys(item); |
| 56 | + const values = Object.values(item); |
| 57 | + const placeholders = keys.map(() => `?`).join(', '); |
| 58 | + const updatePlaceholders = keys.map(key => `${key} = VALUES(${key})`).join(', '); |
| 59 | + yield this.pool.query(`INSERT INTO ${schema} (${keys.join(', ')}) |
| 60 | + VALUES (${placeholders}) ON DUPLICATE KEY |
| 61 | + UPDATE ${updatePlaceholders}`, [...values, ...values]); |
| 62 | + }); |
| 63 | + } |
| 64 | + delete(schema, criteria) { |
| 65 | + return __awaiter(this, void 0, void 0, function* () { |
| 66 | + const keys = Object.keys(criteria); |
| 67 | + const values = Object.values(criteria); |
| 68 | + const placeholders = keys.map(key => `${key} = ?`).join(' AND '); |
| 69 | + yield this.pool.query(`DELETE |
| 70 | + FROM ${schema} |
| 71 | + WHERE ${placeholders}`, values); |
| 72 | + }); |
| 73 | + } |
| 74 | +} |
| 75 | +exports.MySQLPoolConnection = MySQLPoolConnection; |
0 commit comments