UNPKG

tspace-mysql

Version:

Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.

167 lines 5.66 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.sql = void 0; const DB_1 = require("./DB"); class SqlLike { constructor() { this.$db = new DB_1.DB(); this.$action = 'SELECT'; this.$values = []; this.$state = { from: [], select: [], distinct: [], join: [], leftJoin: [], rightJoin: [], where: [], orderBy: [], groupBy: [], offset: [], limit: [], having: [], insert: [], update: [], delete: [] }; this.$returning = null; } select(...selecteds) { this.$state.select.push(() => this.$db.select(...selecteds)); return this; } distinct() { this.$state.distinct.push(() => this.$db.distinct()); return this; } from(table) { this.$state.from.push(() => this.$db.from(table)); return this; } join(localKey, referenceKey) { this.$state.join.push(() => this.$db.join(localKey, referenceKey)); return this; } leftJoin(localKey, referenceKey) { this.$state.leftJoin.push(() => this.$db.leftJoin(localKey, referenceKey)); return this; } rightJoin(localKey, referenceKey) { this.$state.rightJoin.push(() => this.$db.rightJoin(localKey, referenceKey)); return this; } where(column, operator, value) { this.$state.where.push(() => this.$db.where(column, operator, value)); return this; } orderBy(column, order = 'ASC') { this.$state.orderBy.push(() => this.$db.orderBy(column, order)); return this; } groupBy(...columns) { this.$state.groupBy.push(() => this.$db.groupBy(...columns)); return this; } offset(n = 1) { this.$state.offset.push(() => this.$db.offset(n)); return this; } limit(n = 1) { this.$state.limit.push(() => this.$db.limit(n)); return this; } having(condition) { this.$state.having.push(() => this.$db.having(condition)); return this; } dd() { this.$db.dd(); return this; } delete(table) { this.$action = 'DELETE'; this.from(table); this.$state.delete.push(() => this.$db.delete()); return this; } update(table) { this.$action = 'UPDATE'; this.from(table); return this; } set(values) { this.$state.insert.push(() => this.$db.update(values)); this.$values = [values]; return this; } insert(table) { this.$action = 'INSERT'; this.from(table); return this; } values(values) { if (Array.isArray(values)) { this.$state.insert.push(() => this.$db.insertMultiple(values)); this.$values = values; this.$action = 'INSERT_MUTIPLE'; return this; } this.$state.insert.push(() => this.$db.insert(values)); this.$values = [values]; return this; } returning(returning) { this.$returning = returning === undefined ? '*' : returning; return this; } then(onFulfilled, onRejected) { Object .values(this.$state) .forEach(arr => arr.forEach(v => v())); const returning = () => __awaiter(this, void 0, void 0, function* () { let results = []; if (this.$action === 'INSERT') { const inserting = yield this.$db.rawQuery(this.$db['_queryBuilder']().any()); results = yield this.$db.where('id', inserting.insertId).get(); } if (this.$action === 'INSERT_MUTIPLE') { const inserting = yield this.$db.rawQuery(this.$db['_queryBuilder']().any()); results = yield this.$db .whereIn('id', Array.from({ length: this.$values.length }, (_, i) => inserting.insertId + i)) .get(); } if (this.$action === 'UPDATE') { yield this.$db.rawQuery(this.$db['_queryBuilder']().any()); results = yield this.$db.get(); } if (this.$returning != null && this.$returning != '*') { for (const r of results) { for (const key in r) { if (!this.$returning[key]) { delete r[key]; } } } } return results; }); return Promise .resolve(this.$returning == null ? this.$db.rawQuery(this.$db['_queryBuilder']().any()) : returning()) .then(onFulfilled, onRejected); } } const sql = () => new SqlLike(); exports.sql = sql; exports.default = sql; //# sourceMappingURL=SqlLike.js.map