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.

156 lines 4.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sql = void 0; const DB_1 = require("./DB"); class SqlLike { $db = new DB_1.DB(); $action = 'SELECT'; $values = []; $state = { from: [], select: [], distinct: [], join: [], leftJoin: [], rightJoin: [], where: [], orderBy: [], groupBy: [], offset: [], limit: [], having: [], insert: [], update: [], delete: [] }; $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 = async () => { let results = []; if (this.$action === 'INSERT') { const inserting = await this.$db.rawQuery(this.$db['_queryBuilder']().any()); results = await this.$db.where('id', inserting.insertId).get(); } if (this.$action === 'INSERT_MUTIPLE') { const inserting = await this.$db.rawQuery(this.$db['_queryBuilder']().any()); results = await this.$db .whereIn('id', Array.from({ length: this.$values.length }, (_, i) => inserting.insertId + i)) .get(); } if (this.$action === 'UPDATE') { await this.$db.rawQuery(this.$db['_queryBuilder']().any()); results = await 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