UNPKG

functional-models-orm-sql

Version:
139 lines 6.2 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.knexWrapper = exports.ormQueryToKnex = void 0; const functional_models_1 = require("functional-models"); const flow_1 = __importDefault(require("lodash/flow")); const ormQueryToKnex = (knex, table, ormSearch) => { return Promise.resolve().then(() => __awaiter(void 0, void 0, void 0, function* () { (0, functional_models_1.validateOrmSearch)(ormSearch); const _ifExistsDo = (key, func) => (knexInstance) => { // @ts-ignore if (ormSearch[key]) { // @ts-ignore return func(knexInstance, ormSearch[key]); } return knexInstance; }; const pageQuery = _ifExistsDo('page', (knexInstance, page) => { return knexInstance.skip(page); }); const takeQuery = _ifExistsDo('take', (knexInstance, take) => { return knexInstance.limit(take); }); const sortQuery = _ifExistsDo('sort', (knexInstance, sort) => { const direction = sort.order === functional_models_1.SortOrder.asc ? undefined : 'desc'; return knexInstance.orderBy(sort.key, direction); }); const _createSearchString = (statement) => { const value = statement.value; const value2 = statement.options.startsWith ? `${value}%` : value; const value3 = statement.options.endsWith ? `%${value2}` : value2; return value3; }; const _getWhereFuncKey = (statement, andOr) => { if (statement.options.caseSensitive) { return andOr === 'and' ? 'whereILike' : 'orWhereILike'; } if (statement.options.startsWith || statement.options.endsWith) { return andOr === 'or' ? 'orWhereLike' : 'whereLike'; } return andOr === 'and' ? 'where' : 'orWhere'; }; const _whereArgs = (statement, searchString) => { if (statement.equalitySymbol === functional_models_1.EqualitySymbol.eq) { return [statement.key, searchString]; } return [statement.key, statement.equalitySymbol, searchString]; }; const _getPropertyArgs = (o) => { if (o.type === 'property') { const value = _createSearchString(o); return _whereArgs(o, value); } if (o.type === 'datesBefore') { const symbol = o.options.equalToAndBefore ? '<=' : '<'; return [o.key, symbol, o.date]; } if (o.type === 'datesAfter') { const symbol = o.options.equalToAndAfter ? '>=' : '>'; return [o.key, symbol, o.date]; } throw new Error('Impossible situation'); }; const _getSimpleWhereFunc = (andOr) => { return andOr === 'AND' ? 'where' : 'orWhere'; }; const _processToken = (o, andOr) => (knexInstance) => { if ((0, functional_models_1.isPropertyBasedQuery)(o)) { const whereKey = o.type === 'property' ? _getWhereFuncKey(o, andOr) : _getSimpleWhereFunc(andOr); return knexInstance[whereKey](..._getPropertyArgs(o)); } if (Array.isArray(o)) { // Are we dealing with just a string of queries? if (o.every(x => x !== 'AND' && x !== 'OR')) { // All ANDS return (0, flow_1.default)(o.map(s => (k) => { return _processToken(s, 'AND')(k); }))(knexInstance); } // We have a complex or intentional set of queries. return o.reduce(([k, ao], a) => { // If we have a link, we are just going to continue on if ((0, functional_models_1.isALinkToken)(a)) { return [k, a]; } const wKey = _getSimpleWhereFunc(ao || 'AND'); const k1 = k[wKey]((inner) => { return _processToken(a, 'AND')(inner); }); return [k1, undefined]; }, [knexInstance, andOr])[0]; } throw new Error('Should never get here'); }; const selectEverythingQuery = (k) => k.select('*'); const fromTable = (k) => k.table(table); const result = yield (0, flow_1.default)([ selectEverythingQuery, fromTable, _processToken(ormSearch.query, 'AND'), takeQuery, pageQuery, sortQuery, ])(knex); return { instances: result, }; })); }; exports.ormQueryToKnex = ormQueryToKnex; const knexWrapper = (knex) => { const getByPrimaryKey = (table, key, id) => __awaiter(void 0, void 0, void 0, function* () { return (yield knex(table) .where({ [key]: id }) .first()); }); const updateOrCreate = (table, idKey, data) => { return knex(table).insert(data).onConflict(idKey).merge(); }; return { updateOrCreate, getByPrimaryKey, }; }; exports.knexWrapper = knexWrapper; //# sourceMappingURL=knex.js.map