@mrnafisia/type-query
Advanced tools
Comparing version 1.0.48 to 1.0.49
@@ -1,2 +0,2 @@ | ||
import Table from './types/table'; | ||
import { Table } from './types/table'; | ||
import { Context, ContextScope } from './types/context'; | ||
@@ -3,0 +3,0 @@ declare const createContext: <T extends Table>(table: T, alias?: string) => Context<T["columns"]>; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createContextScopeHelper = exports.createContext = void 0; | ||
var U_1 = __importDefault(require("./U")); | ||
var U_1 = require("./U"); | ||
var createContext = function (table, alias) { | ||
@@ -14,35 +11,35 @@ if (alias === void 0) { alias = ''; } | ||
if (_alias === void 0) { _alias = alias; } | ||
return U_1.default.col(table, column, !!_alias, _alias); | ||
return U_1.U.col(table, column, !!_alias, _alias); | ||
}, | ||
colNull: function (column, op, _alias) { | ||
if (_alias === void 0) { _alias = alias; } | ||
return U_1.default.nullOp(U_1.default.col(table, column, !!_alias, _alias), op); | ||
return U_1.U.nullOp(U_1.U.col(table, column, !!_alias, _alias), op); | ||
}, | ||
colBool: function (column, op, _alias) { | ||
if (_alias === void 0) { _alias = alias; } | ||
return U_1.default.boolOp(U_1.default.col(table, column, !!_alias, _alias), op); | ||
return U_1.U.boolOp(U_1.U.col(table, column, !!_alias, _alias), op); | ||
}, | ||
colCmp: function (column, op, v, _alias) { | ||
if (_alias === void 0) { _alias = alias; } | ||
return U_1.default.cmpOp(U_1.default.col(table, column, !!_alias, _alias), op, v); | ||
return U_1.U.cmpOp(U_1.U.col(table, column, !!_alias, _alias), op, v); | ||
}, | ||
colList: function (column, op, v, _alias) { | ||
if (_alias === void 0) { _alias = alias; } | ||
return U_1.default.listOp(U_1.default.col(table, column, !!_alias, _alias), op, v); | ||
return U_1.U.listOp(U_1.U.col(table, column, !!_alias, _alias), op, v); | ||
}, | ||
colLike: function (column, op, v, _alias) { | ||
if (_alias === void 0) { _alias = alias; } | ||
return U_1.default.likeOp(U_1.default.col(table, column, !!_alias, _alias), op, v); | ||
return U_1.U.likeOp(U_1.U.col(table, column, !!_alias, _alias), op, v); | ||
}, | ||
colJson: function (column, op, v, _alias) { | ||
if (_alias === void 0) { _alias = alias; } | ||
return U_1.default.jsonOp(U_1.default.col(table, column, !!_alias, _alias), op, v); | ||
return U_1.U.jsonOp(U_1.U.col(table, column, !!_alias, _alias), op, v); | ||
}, | ||
colsAnd: function (rules, _alias) { | ||
if (_alias === void 0) { _alias = alias; } | ||
return U_1.default.andAllOp(contextScopeHelper(rules, _alias)); | ||
return U_1.U.andAllOp(contextScopeHelper(rules, _alias)); | ||
}, | ||
colsOr: function (rules, _alias) { | ||
if (_alias === void 0) { _alias = alias; } | ||
return U_1.default.orAllOp(contextScopeHelper(rules, _alias)); | ||
return U_1.U.orAllOp(contextScopeHelper(rules, _alias)); | ||
} | ||
@@ -53,36 +50,38 @@ }; | ||
var createContextScopeHelper = function (table) { | ||
return function (rules, alias) { return Object.entries(rules).map(function (_a) { | ||
var key = _a[0], value = _a[1]; | ||
switch (value[0]) { | ||
case '= null': | ||
case '!= null': | ||
return U_1.default.nullOp(U_1.default.col(table, key, !!alias, alias), value[0]); | ||
case '= true': | ||
case '= false': | ||
return U_1.default.boolOp(U_1.default.col(table, key, !!alias, alias), value[0]); | ||
case '=': | ||
case '!=': | ||
case '>': | ||
case '>=': | ||
case '<': | ||
case '<=': | ||
return U_1.default.cmpOp(U_1.default.col(table, key, !!alias, alias), value[0], value[1]); | ||
case 'in': | ||
case 'not in': | ||
return U_1.default.listOp(U_1.default.col(table, key, !!alias, alias), value[0], value[1]); | ||
case 'like': | ||
case 'like all': | ||
case 'like some': | ||
return U_1.default.likeOp(U_1.default.col(table, key, !!alias, alias), value[0], value[1]); | ||
case '@>': | ||
case '<@': | ||
case '?': | ||
case '?&': | ||
case '?|': | ||
return U_1.default.jsonOp(U_1.default.col(table, key, !!alias, alias), value[0], value[1]); | ||
} | ||
throw "do not except this. first element must be a reserved key. ".concat(JSON.stringify(value)); | ||
}); }; | ||
return function (rules, alias) { | ||
return Object.entries(rules).map(function (_a) { | ||
var key = _a[0], value = _a[1]; | ||
switch (value[0]) { | ||
case '= null': | ||
case '!= null': | ||
return U_1.U.nullOp(U_1.U.col(table, key, !!alias, alias), value[0]); | ||
case '= true': | ||
case '= false': | ||
return U_1.U.boolOp(U_1.U.col(table, key, !!alias, alias), value[0]); | ||
case '=': | ||
case '!=': | ||
case '>': | ||
case '>=': | ||
case '<': | ||
case '<=': | ||
return U_1.U.cmpOp(U_1.U.col(table, key, !!alias, alias), value[0], value[1]); | ||
case 'in': | ||
case 'not in': | ||
return U_1.U.listOp(U_1.U.col(table, key, !!alias, alias), value[0], value[1]); | ||
case 'like': | ||
case 'like all': | ||
case 'like some': | ||
return U_1.U.likeOp(U_1.U.col(table, key, !!alias, alias), value[0], value[1]); | ||
case '@>': | ||
case '<@': | ||
case '?': | ||
case '?&': | ||
case '?|': | ||
return U_1.U.jsonOp(U_1.U.col(table, key, !!alias, alias), value[0], value[1]); | ||
} | ||
throw "do not except this. first element must be a reserved key. ".concat(JSON.stringify(value)); | ||
}); | ||
}; | ||
}; | ||
exports.createContextScopeHelper = createContextScopeHelper; | ||
//# sourceMappingURL=context.js.map |
import type { JoinType } from './types/entity'; | ||
import { ReservedExpressionKeys } from './entity'; | ||
import type { OrderDirection, PostgresType } from './types/postgres'; | ||
import type { ReferenceActions } from './types/table'; | ||
import type { TransactionIsolationLevel } from './types/pool'; | ||
import type { OrderDirection, PostgresType } from './types/postgres'; | ||
declare const toTransactionMode: (isolationLevel: TransactionIsolationLevel, readOnly: boolean) => string; | ||
@@ -7,0 +7,0 @@ declare const toOrderDirection: (v: OrderDirection) => "ASC" | "DESC"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -27,3 +27,3 @@ "use strict"; | ||
exports.partialQuery = exports.resolveColumn = exports.getTableDataOfJoinSelectColumn = exports.resolveExpression = exports.resolveReturning = exports.resolveResult = exports.createQueryResult = exports.ReservedExpressionKeys = exports.createJoinSelectEntity = exports.createEntity = void 0; | ||
var U_1 = __importDefault(require("./U")); | ||
var U_1 = require("./U"); | ||
var decimal_js_1 = __importDefault(require("decimal.js")); | ||
@@ -34,391 +34,436 @@ var context_1 = require("./context"); | ||
// entity | ||
var createEntity = function (table) { return ({ | ||
table: table, | ||
context: (0, context_1.createContext)(table), | ||
select: function (returning, where, options) { | ||
var _this = this; | ||
var _a, _b, _c, _d, _e, _f; | ||
var ignoreInWhere = (_a = options === null || options === void 0 ? void 0 : options.ignoreInWhere) !== null && _a !== void 0 ? _a : false; | ||
var ignoreInReturning = (_b = options === null || options === void 0 ? void 0 : options.ignoreInReturning) !== null && _b !== void 0 ? _b : false; | ||
var ignoreInGroupBy = (_c = options === null || options === void 0 ? void 0 : options.ignoreInGroupBy) !== null && _c !== void 0 ? _c : false; | ||
var distinct = (_d = options === null || options === void 0 ? void 0 : options.distinct) !== null && _d !== void 0 ? _d : false; | ||
var groupBy = (_e = options === null || options === void 0 ? void 0 : options.groupBy) !== null && _e !== void 0 ? _e : []; | ||
var orders = (_f = options === null || options === void 0 ? void 0 : options.orders) !== null && _f !== void 0 ? _f : []; | ||
var start = options === null || options === void 0 ? void 0 : options.start; | ||
var step = options === null || options === void 0 ? void 0 : options.step; | ||
var _returning = typeof returning === 'function' ? returning(this.context) : returning; | ||
var createQuery = function (params) { | ||
var tokens = ['SELECT']; | ||
// distinct | ||
if (distinct) { | ||
tokens.push('DISTINCT'); | ||
} | ||
// select | ||
var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning); | ||
if (!resolvedReturning.ok) { | ||
return (0, never_catch_1.err)("<select> -> ".concat(resolvedReturning.error)); | ||
} | ||
params.push.apply(params, resolvedReturning.value.params); | ||
tokens.push(resolvedReturning.value.text); | ||
// from | ||
tokens.push("FROM \"".concat(table.schema, "\".\"").concat(table.title, "\"")); | ||
// where | ||
var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.context) : where, params.length + 1, ignoreInWhere); | ||
if (!resolvedWhereResult.ok) { | ||
return (0, never_catch_1.err)("<select>[where] -> ".concat(resolvedWhereResult.error)); | ||
} | ||
if (resolvedWhereResult.value.text === '' && !ignoreInWhere) { | ||
return (0, never_catch_1.err)("<select>[where] -> neutral"); | ||
} | ||
params.push.apply(params, resolvedWhereResult.value.params); | ||
tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'TRUE' : resolvedWhereResult.value.text); | ||
// groupBy | ||
var _groupBy = typeof groupBy === 'function' ? groupBy(_this.context) : groupBy; | ||
if (groupBy.length !== 0) { | ||
var groupByTextArray = []; | ||
for (var _i = 0, _groupBy_1 = _groupBy; _i < _groupBy_1.length; _i++) { | ||
var aGroupBy = _groupBy_1[_i]; | ||
var resolvedGroupBy = resolveExpression(aGroupBy, params.length + 1, ignoreInGroupBy); | ||
if (!resolvedGroupBy.ok) { | ||
return (0, never_catch_1.err)("<select> -> ".concat(resolvedGroupBy.error)); | ||
var createEntity = function (table) { | ||
return ({ | ||
table: table, | ||
context: (0, context_1.createContext)(table), | ||
select: function (returning, where, options) { | ||
var _this = this; | ||
var _a, _b, _c, _d, _e, _f; | ||
var ignoreInWhere = (_a = options === null || options === void 0 ? void 0 : options.ignoreInWhere) !== null && _a !== void 0 ? _a : false; | ||
var ignoreInReturning = (_b = options === null || options === void 0 ? void 0 : options.ignoreInReturning) !== null && _b !== void 0 ? _b : false; | ||
var ignoreInGroupBy = (_c = options === null || options === void 0 ? void 0 : options.ignoreInGroupBy) !== null && _c !== void 0 ? _c : false; | ||
var distinct = (_d = options === null || options === void 0 ? void 0 : options.distinct) !== null && _d !== void 0 ? _d : false; | ||
var groupBy = (_e = options === null || options === void 0 ? void 0 : options.groupBy) !== null && _e !== void 0 ? _e : []; | ||
var orders = (_f = options === null || options === void 0 ? void 0 : options.orders) !== null && _f !== void 0 ? _f : []; | ||
var start = options === null || options === void 0 ? void 0 : options.start; | ||
var step = options === null || options === void 0 ? void 0 : options.step; | ||
var _returning = typeof returning === 'function' ? returning(this.context) : returning; | ||
var createQuery = function (params) { | ||
var tokens = ['SELECT']; | ||
// distinct | ||
if (distinct) { | ||
tokens.push('DISTINCT'); | ||
} | ||
// select | ||
var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning); | ||
if (!resolvedReturning.ok) { | ||
return (0, never_catch_1.err)("<select> -> ".concat(resolvedReturning.error)); | ||
} | ||
params.push.apply(params, resolvedReturning.value.params); | ||
tokens.push(resolvedReturning.value.text); | ||
// from | ||
tokens.push("FROM \"".concat(table.schema, "\".\"").concat(table.title, "\"")); | ||
// where | ||
var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.context) : where, params.length + 1, ignoreInWhere); | ||
if (!resolvedWhereResult.ok) { | ||
return (0, never_catch_1.err)("<select>[where] -> ".concat(resolvedWhereResult.error)); | ||
} | ||
if (resolvedWhereResult.value.text === '' && !ignoreInWhere) { | ||
return (0, never_catch_1.err)("<select>[where] -> neutral"); | ||
} | ||
params.push.apply(params, resolvedWhereResult.value.params); | ||
tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'TRUE' : resolvedWhereResult.value.text); | ||
// groupBy | ||
var _groupBy = typeof groupBy === 'function' ? groupBy(_this.context) : groupBy; | ||
if (_groupBy.length !== 0) { | ||
var groupByTextArray = []; | ||
for (var _i = 0, _groupBy_1 = _groupBy; _i < _groupBy_1.length; _i++) { | ||
var aGroupBy = _groupBy_1[_i]; | ||
var resolvedGroupBy = resolveExpression(aGroupBy, params.length + 1, ignoreInGroupBy); | ||
if (!resolvedGroupBy.ok) { | ||
return (0, never_catch_1.err)("<select> -> ".concat(resolvedGroupBy.error)); | ||
} | ||
params.push.apply(params, resolvedGroupBy.value.params); | ||
groupByTextArray.push(resolvedGroupBy.value.text); | ||
} | ||
params.push.apply(params, resolvedGroupBy.value.params); | ||
groupByTextArray.push(resolvedGroupBy.value.text); | ||
tokens.push('GROUP BY', groupByTextArray.join(', ')); | ||
} | ||
tokens.push('GROUP BY', groupByTextArray.join(', ')); | ||
} | ||
// orders | ||
if (orders.length !== 0) { | ||
var ordersTextArray = []; | ||
for (var _a = 0, orders_1 = orders; _a < orders_1.length; _a++) { | ||
var order = orders_1[_a]; | ||
var by = order.by, direction = order.direction; | ||
ordersTextArray.push("".concat(resolveColumn(table, by, false), " ").concat((0, dictionary_1.toOrderDirection)(direction))); | ||
// orders | ||
if (orders.length !== 0) { | ||
var ordersTextArray = []; | ||
for (var _a = 0, orders_1 = orders; _a < orders_1.length; _a++) { | ||
var order = orders_1[_a]; | ||
var by = order.by, direction = order.direction; | ||
ordersTextArray.push("".concat(resolveColumn(table, by, false), " ").concat((0, dictionary_1.toOrderDirection)(direction))); | ||
} | ||
tokens.push('ORDER BY', ordersTextArray.join(', ')); | ||
} | ||
tokens.push('ORDER BY', ordersTextArray.join(', ')); | ||
} | ||
// pagination | ||
if (start !== undefined) { | ||
if (start < 0) { | ||
return (0, never_catch_1.err)("<select>[start] -> invalid"); | ||
// pagination | ||
if (start !== undefined) { | ||
if (start < 0) { | ||
return (0, never_catch_1.err)("<select>[start] -> invalid"); | ||
} | ||
tokens.push('OFFSET', start.toString()); | ||
} | ||
tokens.push('OFFSET', start.toString()); | ||
} | ||
if (step !== undefined) { | ||
if (step <= 0) { | ||
return (0, never_catch_1.err)("<select>[step] -> invalid"); | ||
if (step !== undefined) { | ||
if (step <= 0) { | ||
return (0, never_catch_1.err)("<select>[step] -> invalid"); | ||
} | ||
tokens.push('LIMIT', step.toString()); | ||
} | ||
tokens.push('LIMIT', step.toString()); | ||
} | ||
tokens.push(';'); | ||
var sql = tokens.join(' '); | ||
return (0, never_catch_1.ok)({ sql: sql, params: params }); | ||
}; | ||
return createQueryResult(function (column) { return [table.columns[column].type, table.columns[column].nullable]; }, createQuery, _returning); | ||
}, | ||
insert: function (rows, returning, options) { | ||
var _this = this; | ||
var _a, _b, _c; | ||
var nullableDefaultColumns = (_a = (options === null || options === void 0 ? void 0 : options.nullableDefaultColumns)) !== null && _a !== void 0 ? _a : []; | ||
var ignoreInValues = (_b = (options === null || options === void 0 ? void 0 : options.ignoreInValues)) !== null && _b !== void 0 ? _b : false; | ||
var ignoreInReturning = (_c = (options === null || options === void 0 ? void 0 : options.ignoreInValues)) !== null && _c !== void 0 ? _c : false; | ||
var _returning = typeof returning === 'function' ? returning(this.context) : returning; | ||
var createQuery = function (params) { | ||
var tokens = ["INSERT INTO \"".concat(table.schema, "\".\"").concat(table.title, "\"")]; | ||
// columns | ||
var insertingColumns = []; | ||
var columnsTextArray = []; | ||
for (var column in table.columns) { | ||
if (nullableDefaultColumns.includes(column) || !table.columns[column].nullable) { | ||
insertingColumns.push(column); | ||
columnsTextArray.push(resolveColumn(table, column, false)); | ||
tokens.push(';'); | ||
var sql = tokens.join(' '); | ||
return (0, never_catch_1.ok)({ sql: sql, params: params }); | ||
}; | ||
return createQueryResult(function (column) { return [table.columns[column].type, table.columns[column].nullable]; }, createQuery, _returning); | ||
}, | ||
insert: function (rows, returning, options) { | ||
var _this = this; | ||
var _a, _b, _c; | ||
var nullableDefaultColumns = (_a = options === null || options === void 0 ? void 0 : options.nullableDefaultColumns) !== null && _a !== void 0 ? _a : []; | ||
var ignoreInValues = (_b = options === null || options === void 0 ? void 0 : options.ignoreInValues) !== null && _b !== void 0 ? _b : false; | ||
var ignoreInReturning = (_c = options === null || options === void 0 ? void 0 : options.ignoreInValues) !== null && _c !== void 0 ? _c : false; | ||
var _returning = typeof returning === 'function' ? returning(this.context) : returning; | ||
var createQuery = function (params) { | ||
var tokens = ["INSERT INTO \"".concat(table.schema, "\".\"").concat(table.title, "\"")]; | ||
// columns | ||
var insertingColumns = []; | ||
var columnsTextArray = []; | ||
for (var column in table.columns) { | ||
if (nullableDefaultColumns.includes(column) || !table.columns[column].nullable) { | ||
insertingColumns.push(column); | ||
columnsTextArray.push(resolveColumn(table, column, false)); | ||
} | ||
} | ||
} | ||
tokens.push("( ".concat(columnsTextArray.join(', '), " )"), 'VALUES'); | ||
// values | ||
if (rows.length === 0) { | ||
return (0, never_catch_1.err)('<insert>[values] -> empty'); | ||
} | ||
var valuesTextArray = []; | ||
var _rows = typeof rows === 'function' ? rows(_this.context) : rows; | ||
for (var _i = 0, _rows_1 = _rows; _i < _rows_1.length; _i++) { | ||
var _row = _rows_1[_i]; | ||
var rowTokens = []; | ||
for (var _a = 0, insertingColumns_1 = insertingColumns; _a < insertingColumns_1.length; _a++) { | ||
var insertingColumn = insertingColumns_1[_a]; | ||
if (_row[insertingColumn] === undefined) { | ||
var column = table.columns[insertingColumn]; | ||
switch (column.default) { | ||
case 'value': | ||
rowTokens.push(U_1.default.stringify(column.value, true)); | ||
tokens.push("( ".concat(columnsTextArray.join(', '), " )"), 'VALUES'); | ||
// values | ||
var valuesTextArray = []; | ||
var _rows = typeof rows === 'function' ? rows(_this.context) : rows; | ||
if (_rows.length === 0) { | ||
return (0, never_catch_1.err)('<insert>[values] -> empty'); | ||
} | ||
for (var _i = 0, _rows_1 = _rows; _i < _rows_1.length; _i++) { | ||
var _row = _rows_1[_i]; | ||
var rowTokens = []; | ||
for (var _a = 0, insertingColumns_1 = insertingColumns; _a < insertingColumns_1.length; _a++) { | ||
var insertingColumn = insertingColumns_1[_a]; | ||
if (_row[insertingColumn] === undefined) { | ||
var column = table.columns[insertingColumn]; | ||
switch (column.default) { | ||
case 'value': | ||
rowTokens.push(U_1.U.stringify(column.value, true)); | ||
continue; | ||
case true: | ||
case 'auto-increment': | ||
rowTokens.push('DEFAULT'); | ||
continue; | ||
case 'created-at': | ||
case 'updated-at': | ||
rowTokens.push(U_1.U.stringify(new Date(), true)); | ||
continue; | ||
} | ||
if (column.nullable) { | ||
rowTokens.push(U_1.U.stringify(null)); | ||
continue; | ||
case true: | ||
case 'auto-increment': | ||
rowTokens.push('DEFAULT'); | ||
continue; | ||
case 'created-at': | ||
case 'updated-at': | ||
rowTokens.push(U_1.default.stringify(new Date(), true)); | ||
continue; | ||
} | ||
// never going to happen! | ||
return (0, never_catch_1.err)("<insert>[rows][".concat(_rows.indexOf(_row), "][").concat(insertingColumn, "] -> no-value")); | ||
} | ||
if (column.nullable) { | ||
rowTokens.push(U_1.default.stringify(null)); | ||
continue; | ||
else { | ||
var resolvedExpressionResult = resolveExpression(_row[insertingColumn], params.length + 1, ignoreInValues); | ||
if (!resolvedExpressionResult.ok) { | ||
return (0, never_catch_1.err)("<insert>[rows][".concat(_rows.indexOf(_row), "][").concat(insertingColumn, "] -> ").concat(resolvedExpressionResult.error)); | ||
} | ||
if (resolvedExpressionResult.value.text === '') { | ||
return (0, never_catch_1.err)("<insert>[rows][".concat(_rows.indexOf(_row), "][").concat(insertingColumn, "] -> neutral")); | ||
} | ||
params.push.apply(params, resolvedExpressionResult.value.params); | ||
rowTokens.push(resolvedExpressionResult.value.text); | ||
} | ||
// never going to happen! | ||
return (0, never_catch_1.err)("<insert>[rows][".concat(_rows.indexOf(_row), "][").concat(insertingColumn, "] -> no-value")); | ||
} | ||
else { | ||
var resolvedExpressionResult = resolveExpression(_row[insertingColumn], params.length + 1, ignoreInValues); | ||
if (!resolvedExpressionResult.ok) { | ||
return (0, never_catch_1.err)("<insert>[rows][".concat(_rows.indexOf(_row), "][").concat(insertingColumn, "] -> ").concat(resolvedExpressionResult.error)); | ||
valuesTextArray.push("( ".concat(rowTokens.join(', '), " )")); | ||
} | ||
tokens.push(valuesTextArray.join(', ')); | ||
// returning | ||
if (_returning.length !== 0) { | ||
var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning); | ||
if (!resolvedReturning.ok) { | ||
return (0, never_catch_1.err)("<insert> -> ".concat(resolvedReturning.error)); | ||
} | ||
params.push.apply(params, resolvedReturning.value.params); | ||
tokens.push('RETURNING', resolvedReturning.value.text); | ||
} | ||
tokens.push(';'); | ||
var sql = tokens.join(' '); | ||
return (0, never_catch_1.ok)({ sql: sql, params: params }); | ||
}; | ||
return createQueryResult(function (column) { return [table.columns[column].type, table.columns[column].nullable]; }, createQuery, _returning); | ||
}, | ||
update: function (sets, where, returning, options) { | ||
var _this = this; | ||
var _a, _b, _c; | ||
var ignoreInSets = (_a = options === null || options === void 0 ? void 0 : options.ignoreInSets) !== null && _a !== void 0 ? _a : false; | ||
var ignoreInWhere = (_b = options === null || options === void 0 ? void 0 : options.ignoreInWhere) !== null && _b !== void 0 ? _b : false; | ||
var ignoreInReturning = (_c = options === null || options === void 0 ? void 0 : options.ignoreInReturning) !== null && _c !== void 0 ? _c : false; | ||
var _returning = typeof returning === 'function' ? returning(this.context) : returning; | ||
var createQuery = function (params) { | ||
var tokens = ["UPDATE \"".concat(table.schema, "\".\"").concat(table.title, "\" SET")]; | ||
// set | ||
var _set = typeof sets === 'function' ? sets(_this.context) : sets; | ||
var setsTextArray = []; | ||
var key; | ||
for (key in _set) { | ||
var setExpressionResult = resolveExpression(_set[key], params.length + 1, ignoreInSets); | ||
if (!setExpressionResult.ok) { | ||
return (0, never_catch_1.err)("<update>[sets][".concat(key, "] -> ").concat(setExpressionResult.error)); | ||
} | ||
if (setExpressionResult.value.text === '') { | ||
if (ignoreInSets) { | ||
continue; | ||
} | ||
if (resolvedExpressionResult.value.text === '') { | ||
return (0, never_catch_1.err)("<insert>[rows][".concat(_rows.indexOf(_row), "][").concat(insertingColumn, "] -> neutral")); | ||
else { | ||
return (0, never_catch_1.err)("<update>[sets][".concat(key, "] -> neutral")); | ||
} | ||
params.push.apply(params, resolvedExpressionResult.value.params); | ||
rowTokens.push(resolvedExpressionResult.value.text); | ||
} | ||
params.push.apply(params, setExpressionResult.value.params); | ||
setsTextArray.push("".concat(resolveColumn(table, key, false), " = ").concat(setExpressionResult.value.text)); | ||
} | ||
valuesTextArray.push("( ".concat(rowTokens.join(', '), " )")); | ||
} | ||
tokens.push(valuesTextArray.join(', ')); | ||
// returning | ||
if (_returning.length !== 0) { | ||
var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning); | ||
if (!resolvedReturning.ok) { | ||
return (0, never_catch_1.err)("<insert> -> ".concat(resolvedReturning.error)); | ||
for (var column in table.columns) { | ||
switch (table.columns[column].default) { | ||
case 'updated-at': | ||
setsTextArray.push("".concat(resolveColumn(table, column, false), " = ").concat(U_1.U.stringify(new Date(), true))); | ||
break; | ||
} | ||
} | ||
params.push.apply(params, resolvedReturning.value.params); | ||
tokens.push('RETURNING', resolvedReturning.value.text); | ||
} | ||
tokens.push(';'); | ||
var sql = tokens.join(' '); | ||
return (0, never_catch_1.ok)({ sql: sql, params: params }); | ||
}; | ||
return createQueryResult(function (column) { return [table.columns[column].type, table.columns[column].nullable]; }, createQuery, _returning); | ||
}, | ||
update: function (sets, where, returning, options) { | ||
var _this = this; | ||
var _a, _b, _c; | ||
var ignoreInSets = (_a = (options === null || options === void 0 ? void 0 : options.ignoreInSets)) !== null && _a !== void 0 ? _a : false; | ||
var ignoreInWhere = (_b = (options === null || options === void 0 ? void 0 : options.ignoreInWhere)) !== null && _b !== void 0 ? _b : false; | ||
var ignoreInReturning = (_c = (options === null || options === void 0 ? void 0 : options.ignoreInReturning)) !== null && _c !== void 0 ? _c : false; | ||
var _returning = typeof returning === 'function' ? returning(this.context) : returning; | ||
var createQuery = function (params) { | ||
var tokens = ["UPDATE \"".concat(table.schema, "\".\"").concat(table.title, "\" SET")]; | ||
// set | ||
var _set = typeof sets === 'function' ? sets(_this.context) : sets; | ||
var setsTextArray = []; | ||
var key; | ||
for (key in _set) { | ||
var setExpressionResult = resolveExpression(_set[key], params.length + 1, ignoreInSets); | ||
if (!setExpressionResult.ok) { | ||
return (0, never_catch_1.err)("<update>[sets][".concat(key, "] -> ").concat(setExpressionResult.error)); | ||
if (setsTextArray.length === 0) { | ||
return (0, never_catch_1.err)('<update>[sets] -> empty'); | ||
} | ||
if (setExpressionResult.value.text === '') { | ||
if (ignoreInSets) { | ||
continue; | ||
tokens.push(setsTextArray.join(', ')); | ||
// where | ||
var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.context) : where, params.length + 1, ignoreInWhere); | ||
if (!resolvedWhereResult.ok) { | ||
return (0, never_catch_1.err)("<update>[where] -> ".concat(resolvedWhereResult.error)); | ||
} | ||
if (resolvedWhereResult.value.text === '' && !ignoreInWhere) { | ||
return (0, never_catch_1.err)("<update>[where] -> neutral"); | ||
} | ||
params.push.apply(params, resolvedWhereResult.value.params); | ||
tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'FALSE' : resolvedWhereResult.value.text); | ||
// returning | ||
if (_returning.length !== 0) { | ||
var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning); | ||
if (!resolvedReturning.ok) { | ||
return (0, never_catch_1.err)("<update> -> ".concat(resolvedReturning.error)); | ||
} | ||
else { | ||
return (0, never_catch_1.err)("<update>[sets][".concat(key, "] -> neutral")); | ||
params.push.apply(params, resolvedReturning.value.params); | ||
tokens.push('RETURNING', resolvedReturning.value.text); | ||
} | ||
tokens.push(';'); | ||
var sql = tokens.join(' '); | ||
return (0, never_catch_1.ok)({ sql: sql, params: params }); | ||
}; | ||
return createQueryResult(function (column) { return [table.columns[column].type, table.columns[column].nullable]; }, createQuery, _returning); | ||
}, | ||
delete: function (where, returning, options) { | ||
var _this = this; | ||
var _a, _b; | ||
var ignoreInWhere = (_a = options === null || options === void 0 ? void 0 : options.ignoreInWhere) !== null && _a !== void 0 ? _a : false; | ||
var ignoreInReturning = (_b = options === null || options === void 0 ? void 0 : options.ignoreInReturning) !== null && _b !== void 0 ? _b : false; | ||
var _returning = typeof returning === 'function' ? returning(this.context) : returning; | ||
var createQuery = function (params) { | ||
var tokens = ["DELETE FROM \"".concat(table.schema, "\".\"").concat(table.title, "\"")]; | ||
// where | ||
var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.context) : where, params.length + 1, ignoreInWhere); | ||
if (!resolvedWhereResult.ok) { | ||
return (0, never_catch_1.err)("<delete>[where] -> ".concat(resolvedWhereResult.error)); | ||
} | ||
if (resolvedWhereResult.value.text === '' && !ignoreInWhere) { | ||
return (0, never_catch_1.err)("<delete>[where] -> neutral"); | ||
} | ||
params.push.apply(params, resolvedWhereResult.value.params); | ||
tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'FALSE' : resolvedWhereResult.value.text); | ||
// returning | ||
if (_returning.length !== 0) { | ||
var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning); | ||
if (!resolvedReturning.ok) { | ||
return (0, never_catch_1.err)("<delete> -> ".concat(resolvedReturning.error)); | ||
} | ||
params.push.apply(params, resolvedReturning.value.params); | ||
tokens.push('RETURNING', resolvedReturning.value.text); | ||
} | ||
params.push.apply(params, setExpressionResult.value.params); | ||
setsTextArray.push("".concat(resolveColumn(table, key, false), " = ").concat(setExpressionResult.value.text)); | ||
} | ||
for (var column in table.columns) { | ||
switch (table.columns[column].default) { | ||
case 'updated-at': | ||
setsTextArray.push("".concat(resolveColumn(table, column, false), " = ").concat(U_1.default.stringify(new Date(), true))); | ||
break; | ||
tokens.push(';'); | ||
var sql = tokens.join(' '); | ||
return (0, never_catch_1.ok)({ sql: sql, params: params }); | ||
}; | ||
return createQueryResult(function (column) { return [table.columns[column].type, table.columns[column].nullable]; }, createQuery, _returning); | ||
}, | ||
join: function (mainAlias, joinType, joinTable, joinAlias, on) { | ||
var _a; | ||
return createJoinSelectEntity({ table: table, alias: mainAlias }, [{ table: joinTable, joinType: joinType, alias: joinAlias, on: on }], (_a = {}, | ||
_a[mainAlias] = (0, context_1.createContext)(table, mainAlias), | ||
_a[joinAlias] = (0, context_1.createContext)(joinTable, joinAlias), | ||
_a)); | ||
} | ||
}); | ||
}; | ||
exports.createEntity = createEntity; | ||
var createJoinSelectEntity = function (main, joinTables, contexts) { | ||
return ({ | ||
contexts: contexts, | ||
select: function (returning, where, options) { | ||
var _this = this; | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
var ignoreInWhere = (_a = options === null || options === void 0 ? void 0 : options.ignoreInWhere) !== null && _a !== void 0 ? _a : false; | ||
var ignoreInReturning = (_b = options === null || options === void 0 ? void 0 : options.ignoreInReturning) !== null && _b !== void 0 ? _b : false; | ||
var ignoreInJoin = (_c = options === null || options === void 0 ? void 0 : options.ignoreInJoin) !== null && _c !== void 0 ? _c : false; | ||
var ignoreInGroupBy = (_d = options === null || options === void 0 ? void 0 : options.ignoreInGroupBy) !== null && _d !== void 0 ? _d : false; | ||
var distinct = (_e = options === null || options === void 0 ? void 0 : options.distinct) !== null && _e !== void 0 ? _e : false; | ||
var groupBy = (_f = options === null || options === void 0 ? void 0 : options.groupBy) !== null && _f !== void 0 ? _f : []; | ||
var orders = (_g = options === null || options === void 0 ? void 0 : options.orders) !== null && _g !== void 0 ? _g : []; | ||
var start = options === null || options === void 0 ? void 0 : options.start; | ||
var step = options === null || options === void 0 ? void 0 : options.step; | ||
var allTables = __spreadArray([main], joinTables, true); | ||
var _returning = typeof returning === 'function' ? returning(this.contexts) : returning; | ||
var createQuery = function (params) { | ||
var tokens = ['SELECT']; | ||
// distinct | ||
if (distinct) { | ||
tokens.push('DISTINCT'); | ||
} | ||
} | ||
if (setsTextArray.length === 0) { | ||
return (0, never_catch_1.err)('<update>[sets] -> empty'); | ||
} | ||
tokens.push(setsTextArray.join(', ')); | ||
// where | ||
var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.context) : where, params.length + 1, ignoreInWhere); | ||
if (!resolvedWhereResult.ok) { | ||
return (0, never_catch_1.err)("<update>[where] -> ".concat(resolvedWhereResult.error)); | ||
} | ||
if (resolvedWhereResult.value.text === '' && !ignoreInWhere) { | ||
return (0, never_catch_1.err)("<update>[where] -> neutral"); | ||
} | ||
params.push.apply(params, resolvedWhereResult.value.params); | ||
tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'FALSE' : resolvedWhereResult.value.text); | ||
// returning | ||
if (_returning.length !== 0) { | ||
var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning); | ||
// select | ||
var resolvedReturning = resolveReturning(function (column) { | ||
var _a = getTableDataOfJoinSelectColumn(allTables, column), table = _a.table, alias = _a.alias; | ||
var columnKey = column.substring((alias + '_').length); | ||
return { type: table.columns[columnKey].type, title: table.columns[columnKey].title, alias: alias }; | ||
}, _returning, params.length + 1, ignoreInReturning); | ||
if (!resolvedReturning.ok) { | ||
return (0, never_catch_1.err)("<update> -> ".concat(resolvedReturning.error)); | ||
return (0, never_catch_1.err)("<join-select>[columns] -> ".concat(resolvedReturning.error)); | ||
} | ||
params.push.apply(params, resolvedReturning.value.params); | ||
tokens.push('RETURNING', resolvedReturning.value.text); | ||
} | ||
tokens.push(';'); | ||
var sql = tokens.join(' '); | ||
return (0, never_catch_1.ok)({ sql: sql, params: params }); | ||
}; | ||
return createQueryResult(function (column) { return [table.columns[column].type, table.columns[column].nullable]; }, createQuery, _returning); | ||
}, | ||
delete: function (where, returning, options) { | ||
var _this = this; | ||
var _a, _b; | ||
var ignoreInWhere = (_a = (options === null || options === void 0 ? void 0 : options.ignoreInWhere)) !== null && _a !== void 0 ? _a : false; | ||
var ignoreInReturning = (_b = (options === null || options === void 0 ? void 0 : options.ignoreInReturning)) !== null && _b !== void 0 ? _b : false; | ||
var _returning = typeof returning === 'function' ? returning(this.context) : returning; | ||
var createQuery = function (params) { | ||
var tokens = ["DELETE FROM \"".concat(table.schema, "\".\"").concat(table.title, "\"")]; | ||
// where | ||
var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.context) : where, params.length + 1, ignoreInWhere); | ||
if (!resolvedWhereResult.ok) { | ||
return (0, never_catch_1.err)("<delete>[where] -> ".concat(resolvedWhereResult.error)); | ||
} | ||
if (resolvedWhereResult.value.text === '' && !ignoreInWhere) { | ||
return (0, never_catch_1.err)("<delete>[where] -> neutral"); | ||
} | ||
params.push.apply(params, resolvedWhereResult.value.params); | ||
tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'FALSE' : resolvedWhereResult.value.text); | ||
// returning | ||
if (_returning.length !== 0) { | ||
var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning); | ||
if (!resolvedReturning.ok) { | ||
return (0, never_catch_1.err)("<delete> -> ".concat(resolvedReturning.error)); | ||
tokens.push(resolvedReturning.value.text, "FROM \"".concat(main.table.schema, "\".\"").concat(main.table.title, "\" \"").concat(main.alias, "\"")); | ||
// join | ||
for (var _i = 0, joinTables_1 = joinTables; _i < joinTables_1.length; _i++) { | ||
var joinTable = joinTables_1[_i]; | ||
var onExpressionResult = resolveExpression(typeof joinTable.on === 'function' ? joinTable.on(_this.contexts) : joinTable.on, params.length + 1, ignoreInJoin); | ||
if (!onExpressionResult.ok) { | ||
return (0, never_catch_1.err)("<join-select>[join][".concat(joinTables.indexOf(joinTable), "] -> ").concat(onExpressionResult.error)); | ||
} | ||
if (onExpressionResult.value.text === '' && !ignoreInJoin) { | ||
return (0, never_catch_1.err)("<join-select>[join][".concat(joinTables.indexOf(joinTable), "] -> neutral")); | ||
} | ||
params.push.apply(params, onExpressionResult.value.params); | ||
tokens.push("".concat((0, dictionary_1.toJoinType)(joinTable.joinType), " \"").concat(joinTable.table.schema, "\".\"").concat(joinTable.table.title, "\" \"").concat(joinTable.alias, "\" ON ").concat(onExpressionResult.value.text)); | ||
} | ||
params.push.apply(params, resolvedReturning.value.params); | ||
tokens.push('RETURNING', resolvedReturning.value.text); | ||
} | ||
tokens.push(';'); | ||
var sql = tokens.join(' '); | ||
return (0, never_catch_1.ok)({ sql: sql, params: params }); | ||
}; | ||
return createQueryResult(function (column) { return [table.columns[column].type, table.columns[column].nullable]; }, createQuery, _returning); | ||
}, | ||
join: function (mainAlias, joinType, joinTable, joinAlias, on) { | ||
var _a; | ||
return createJoinSelectEntity({ table: table, alias: mainAlias }, [{ table: joinTable, joinType: joinType, alias: joinAlias, on: on }], (_a = {}, _a[mainAlias] = (0, context_1.createContext)(table, mainAlias), _a[joinAlias] = (0, context_1.createContext)(joinTable, joinAlias), _a)); | ||
} | ||
}); }; | ||
exports.createEntity = createEntity; | ||
var createJoinSelectEntity = function (main, joinTables, contexts) { return ({ | ||
contexts: contexts, | ||
select: function (returning, where, options) { | ||
var _this = this; | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
var ignoreInWhere = (_a = options === null || options === void 0 ? void 0 : options.ignoreInWhere) !== null && _a !== void 0 ? _a : false; | ||
var ignoreInReturning = (_b = options === null || options === void 0 ? void 0 : options.ignoreInReturning) !== null && _b !== void 0 ? _b : false; | ||
var ignoreInJoin = (_c = options === null || options === void 0 ? void 0 : options.ignoreInJoin) !== null && _c !== void 0 ? _c : false; | ||
var ignoreInGroupBy = (_d = options === null || options === void 0 ? void 0 : options.ignoreInGroupBy) !== null && _d !== void 0 ? _d : false; | ||
var distinct = (_e = options === null || options === void 0 ? void 0 : options.distinct) !== null && _e !== void 0 ? _e : false; | ||
var groupBy = (_f = options === null || options === void 0 ? void 0 : options.groupBy) !== null && _f !== void 0 ? _f : []; | ||
var orders = (_g = options === null || options === void 0 ? void 0 : options.orders) !== null && _g !== void 0 ? _g : []; | ||
var start = options === null || options === void 0 ? void 0 : options.start; | ||
var step = options === null || options === void 0 ? void 0 : options.step; | ||
var allTables = __spreadArray([main], joinTables, true); | ||
var _returning = typeof returning === 'function' ? returning(this.contexts) : returning; | ||
var createQuery = function (params) { | ||
var tokens = ['SELECT']; | ||
// distinct | ||
if (distinct) { | ||
tokens.push('DISTINCT'); | ||
} | ||
// select | ||
var resolvedReturning = resolveReturning(function (column) { | ||
var _a = getTableDataOfJoinSelectColumn(allTables, column), table = _a.table, alias = _a.alias; | ||
var columnKey = column.substring((alias + '_').length); | ||
return { type: table.columns[columnKey].type, title: table.columns[columnKey].title, alias: alias }; | ||
}, _returning, params.length + 1, ignoreInReturning); | ||
if (!resolvedReturning.ok) { | ||
return (0, never_catch_1.err)("<join-select>[columns] -> ".concat(resolvedReturning.error)); | ||
} | ||
params.push.apply(params, resolvedReturning.value.params); | ||
tokens.push(resolvedReturning.value.text, "FROM \"".concat(main.table.schema, "\".\"").concat(main.table.title, "\" \"").concat(main.alias, "\"")); | ||
// join | ||
for (var _i = 0, joinTables_1 = joinTables; _i < joinTables_1.length; _i++) { | ||
var joinTable = joinTables_1[_i]; | ||
var onExpressionResult = resolveExpression(typeof joinTable.on === 'function' ? joinTable.on(_this.contexts) : joinTable.on, params.length + 1, ignoreInJoin); | ||
if (!onExpressionResult.ok) { | ||
return (0, never_catch_1.err)("<join-select>[join][".concat(joinTables.indexOf(joinTable), "] -> ").concat(onExpressionResult.error)); | ||
// where | ||
var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.contexts) : where, params.length + 1, ignoreInWhere); | ||
if (!resolvedWhereResult.ok) { | ||
return (0, never_catch_1.err)("<join-select>[where] -> ".concat(resolvedWhereResult.error)); | ||
} | ||
if (onExpressionResult.value.text === '' && !ignoreInJoin) { | ||
return (0, never_catch_1.err)("<join-select>[join][".concat(joinTables.indexOf(joinTable), "] -> neutral")); | ||
if (resolvedWhereResult.value.text === '' && !ignoreInWhere) { | ||
return (0, never_catch_1.err)("<join-select>[where] -> neutral"); | ||
} | ||
params.push.apply(params, onExpressionResult.value.params); | ||
tokens.push("".concat((0, dictionary_1.toJoinType)(joinTable.joinType), " \"").concat(joinTable.table.schema, "\".\"").concat(joinTable.table.title, "\" \"").concat(joinTable.alias, "\" ON ").concat(onExpressionResult.value.text)); | ||
} | ||
// where | ||
var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.contexts) : where, params.length + 1, ignoreInWhere); | ||
if (!resolvedWhereResult.ok) { | ||
return (0, never_catch_1.err)("<join-select>[where] -> ".concat(resolvedWhereResult.error)); | ||
} | ||
if (resolvedWhereResult.value.text === '' && !ignoreInWhere) { | ||
return (0, never_catch_1.err)("<join-select>[where] -> neutral"); | ||
} | ||
params.push.apply(params, resolvedWhereResult.value.params); | ||
tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'FALSE' : resolvedWhereResult.value.text); | ||
// groupBy | ||
var _groupBy = typeof groupBy === 'function' ? groupBy(_this.contexts) : groupBy; | ||
if (groupBy.length !== 0) { | ||
var groupByTextArray = []; | ||
for (var _a = 0, _groupBy_2 = _groupBy; _a < _groupBy_2.length; _a++) { | ||
var aGroupBy = _groupBy_2[_a]; | ||
var resolvedGroupBy = resolveExpression(aGroupBy, params.length + 1, ignoreInGroupBy); | ||
if (!resolvedGroupBy.ok) { | ||
return (0, never_catch_1.err)("<join-select> -> ".concat(resolvedGroupBy.error)); | ||
params.push.apply(params, resolvedWhereResult.value.params); | ||
tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'FALSE' : resolvedWhereResult.value.text); | ||
// groupBy | ||
var _groupBy = typeof groupBy === 'function' ? groupBy(_this.contexts) : groupBy; | ||
if (_groupBy.length !== 0) { | ||
var groupByTextArray = []; | ||
for (var _a = 0, _groupBy_2 = _groupBy; _a < _groupBy_2.length; _a++) { | ||
var aGroupBy = _groupBy_2[_a]; | ||
var resolvedGroupBy = resolveExpression(aGroupBy, params.length + 1, ignoreInGroupBy); | ||
if (!resolvedGroupBy.ok) { | ||
return (0, never_catch_1.err)("<join-select> -> ".concat(resolvedGroupBy.error)); | ||
} | ||
params.push.apply(params, resolvedGroupBy.value.params); | ||
groupByTextArray.push(resolvedGroupBy.value.text); | ||
} | ||
params.push.apply(params, resolvedGroupBy.value.params); | ||
groupByTextArray.push(resolvedGroupBy.value.text); | ||
tokens.push('GROUP BY', groupByTextArray.join(', ')); | ||
} | ||
tokens.push('GROUP BY', groupByTextArray.join(', ')); | ||
} | ||
// orders | ||
if (orders.length !== 0) { | ||
var ordersTextArray = []; | ||
for (var _b = 0, orders_2 = orders; _b < orders_2.length; _b++) { | ||
var order = orders_2[_b]; | ||
var by = order.by, direction = order.direction; | ||
ordersTextArray.push("\"".concat(by, "\" ").concat(direction)); | ||
// orders | ||
if (orders.length !== 0) { | ||
var ordersTextArray = []; | ||
for (var _b = 0, orders_2 = orders; _b < orders_2.length; _b++) { | ||
var order = orders_2[_b]; | ||
var by = order.by, direction = order.direction; | ||
ordersTextArray.push("\"".concat(by, "\" ").concat(direction)); | ||
} | ||
tokens.push('ORDER BY', ordersTextArray.join(', ')); | ||
} | ||
tokens.push('ORDER BY', ordersTextArray.join(', ')); | ||
} | ||
// pagination | ||
if (start !== undefined) { | ||
if (start < 0) { | ||
return (0, never_catch_1.err)("<join-select>[start] -> invalid"); | ||
// pagination | ||
if (start !== undefined) { | ||
if (start < 0) { | ||
return (0, never_catch_1.err)("<join-select>[start] -> invalid"); | ||
} | ||
tokens.push('OFFSET', start.toString()); | ||
} | ||
tokens.push('OFFSET', start.toString()); | ||
} | ||
if (step !== undefined) { | ||
if (step <= 0) { | ||
return (0, never_catch_1.err)("<join-select>[step] -> invalid"); | ||
if (step !== undefined) { | ||
if (step <= 0) { | ||
return (0, never_catch_1.err)("<join-select>[step] -> invalid"); | ||
} | ||
tokens.push('LIMIT', step.toString()); | ||
} | ||
tokens.push('LIMIT', step.toString()); | ||
} | ||
tokens.push(';'); | ||
var sql = tokens.join(' '); | ||
return (0, never_catch_1.ok)({ sql: sql, params: params }); | ||
}; | ||
return createQueryResult(function (column) { | ||
var _a = getTableDataOfJoinSelectColumn(allTables, column), alias = _a.alias, columns = _a.table.columns; | ||
var targetCol = columns[column.substring((alias + '_').length)]; | ||
return [targetCol.type, targetCol.nullable]; | ||
}, createQuery, _returning); | ||
}, | ||
join: function (joinType, joinTable, joinAlias, on) { | ||
var _a; | ||
joinTables.push({ table: joinTable, on: on, joinType: joinType, alias: joinAlias }); | ||
return createJoinSelectEntity(main, joinTables, __assign(__assign({}, contexts), (_a = {}, _a[joinAlias] = (0, context_1.createContext)(joinTable, joinAlias), _a))); | ||
} | ||
}); }; | ||
tokens.push(';'); | ||
var sql = tokens.join(' '); | ||
return (0, never_catch_1.ok)({ sql: sql, params: params }); | ||
}; | ||
return createQueryResult(function (column) { | ||
var _a = getTableDataOfJoinSelectColumn(allTables, column), alias = _a.alias, columns = _a.table.columns; | ||
var targetCol = columns[column.substring((alias + '_').length)]; | ||
return [targetCol.type, targetCol.nullable]; | ||
}, createQuery, _returning); | ||
}, | ||
join: function (joinType, joinTable, joinAlias, on) { | ||
var _a; | ||
joinTables.push({ table: joinTable, on: on, joinType: joinType, alias: joinAlias }); | ||
return createJoinSelectEntity(main, joinTables, __assign(__assign({}, contexts), (_a = {}, _a[joinAlias] = (0, context_1.createContext)(joinTable, joinAlias), _a))); | ||
} | ||
}); | ||
}; | ||
exports.createJoinSelectEntity = createJoinSelectEntity; | ||
// utils | ||
var ReservedExpressionKeys = ['val', '=n', '!=n', '=t', '=f', 'not', '+', '-', '*', '/', '||', 'and', 'or', | ||
'**', 'fun', 'swt', 'col', 'raw', '=', '!=', '>', '>=', '<', '<=', 'lk', '@>', '<@', '?', 'j-', 'in', 'nin', | ||
'lka', 'lks', '?|', '?&', 'j-a', 'bt', 'qry', 'exists']; | ||
var ReservedExpressionKeys = [ | ||
'val', | ||
'=n', | ||
'!=n', | ||
'=t', | ||
'=f', | ||
'not', | ||
'+', | ||
'-', | ||
'*', | ||
'/', | ||
'||', | ||
'and', | ||
'or', | ||
'**', | ||
'fun', | ||
'swt', | ||
'col', | ||
'raw', | ||
'=', | ||
'!=', | ||
'>', | ||
'>=', | ||
'<', | ||
'<=', | ||
'lk', | ||
'@>', | ||
'<@', | ||
'?', | ||
'j-', | ||
'in', | ||
'nin', | ||
'lka', | ||
'lks', | ||
'?|', | ||
'?&', | ||
'j-a', | ||
'bt', | ||
'qry', | ||
'exists' | ||
]; | ||
exports.ReservedExpressionKeys = ReservedExpressionKeys; | ||
var createQueryResult = function (getColumnType, createQuery, returning) { | ||
var query = undefined; | ||
return ({ | ||
return { | ||
getData: function (params) { | ||
@@ -448,3 +493,4 @@ if (params === void 0) { params = []; } | ||
} | ||
return client.query(query.sql, query.params) | ||
return client | ||
.query(query.sql, query.params) | ||
.then(function (_a) { | ||
@@ -456,9 +502,9 @@ var rows = _a.rows; | ||
} | ||
}); | ||
}; | ||
}; | ||
exports.createQueryResult = createQueryResult; | ||
/* | ||
* take getColumnType instead of table to support join-select too. | ||
* this cause hard to call this function directly, but it is only solution I could come up with. | ||
*/ | ||
* take getColumnType instead of table to support join-select too. | ||
* this cause hard to call this function directly, but it is only solution I could come up with. | ||
*/ | ||
var resolveResult = function (getColumnType, columns, rows, mode) { | ||
@@ -476,3 +522,3 @@ // check size in count and get mode | ||
if (typeof column !== 'object') { | ||
rows[i][column] = U_1.default.cast(rows[i][column], getColumnType(column)); | ||
rows[i][column] = U_1.U.cast(rows[i][column], getColumnType(column)); | ||
} | ||
@@ -491,5 +537,5 @@ }); | ||
/* | ||
* take getColumnTypeTitleAlias instead of table to support join-select too. | ||
* this cause hard to call this function directly, but it is only solution I could come up with. | ||
*/ | ||
* take getColumnTypeTitleAlias instead of table to support join-select too. | ||
* this cause hard to call this function directly, but it is only solution I could come up with. | ||
*/ | ||
var resolveReturning = function (getColumnTypeTitleAlias, columns, paramsStart, ignore) { | ||
@@ -517,5 +563,3 @@ var tokens = []; | ||
if (alias !== undefined) { | ||
tokens.push("\"".concat(alias, "\".") + | ||
"\"".concat(column.substring((alias + '_').length), "\"") + | ||
" AS \"".concat(column, "\"")); | ||
tokens.push("\"".concat(alias, "\".") + "\"".concat(column.substring((alias + '_').length), "\"") + " AS \"".concat(column, "\"")); | ||
} | ||
@@ -527,5 +571,3 @@ else { | ||
else { | ||
tokens.push((alias !== undefined ? "\"".concat(alias, "\".") : '') + | ||
"\"".concat(title, "\"") + | ||
" AS \"".concat(column, "\"")); | ||
tokens.push((alias !== undefined ? "\"".concat(alias, "\".") : '') + "\"".concat(title, "\"") + " AS \"".concat(column, "\"")); | ||
} | ||
@@ -541,12 +583,12 @@ } | ||
/* | ||
** currently all errors are handled with types and no dynamic check is required. | ||
** errors return only when ignore is false and an expression needs ignorance. | ||
** so, some errors written in function, but they will never be returned. | ||
** e.g. | ||
** if (e1Result.value.text === '') { | ||
** return ignore ? ok(partialQuery()) : err(`<${toDescription(expression[0])}> -> neutral`); | ||
** } | ||
* if result is neutral then ignore is true, so error in this example will never be returned. | ||
* but they exist in case new errors with dynamic check added. | ||
*/ | ||
** currently all errors are handled with types and no dynamic check is required. | ||
** errors return only when ignore is false and an expression needs ignorance. | ||
** so, some errors written in function, but they will never be returned. | ||
** e.g. | ||
** if (e1Result.value.text === '') { | ||
** return ignore ? ok(partialQuery()) : err(`<${toDescription(expression[0])}> -> neutral`); | ||
** } | ||
* if result is neutral then ignore is true, so error in this example will never be returned. | ||
* but they exist in case new errors with dynamic check added. | ||
*/ | ||
var resolveExpression = function (expression, paramsStart, ignore) { | ||
@@ -558,11 +600,15 @@ if (ignore === void 0) { ignore = false; } | ||
} | ||
if (expression === null || typeof expression === 'boolean' || expression instanceof decimal_js_1.default | ||
|| expression instanceof Date || typeof expression === 'number' || typeof expression === 'bigint') { | ||
return (0, never_catch_1.ok)(partialQuery("".concat(U_1.default.stringify(expression, true)))); | ||
if (expression === null || | ||
typeof expression === 'boolean' || | ||
expression instanceof decimal_js_1.default || | ||
expression instanceof Date || | ||
typeof expression === 'number' || | ||
typeof expression === 'bigint') { | ||
return (0, never_catch_1.ok)(partialQuery("".concat(U_1.U.stringify(expression, true)))); | ||
} | ||
if (typeof expression === 'string') { | ||
return (0, never_catch_1.ok)(partialQuery("$".concat(paramsStart++), [U_1.default.stringify(expression, false)])); | ||
return (0, never_catch_1.ok)(partialQuery("$".concat(paramsStart++), [U_1.U.stringify(expression, false)])); | ||
} | ||
if (!(Array.isArray(expression) && ReservedExpressionKeys.includes(expression[0]))) { | ||
return (0, never_catch_1.ok)(partialQuery("$".concat(paramsStart++, "::jsonb"), [U_1.default.stringify(expression, false)])); | ||
return (0, never_catch_1.ok)(partialQuery("$".concat(paramsStart++, "::jsonb"), [U_1.U.stringify(expression, false)])); | ||
} | ||
@@ -578,3 +624,3 @@ // wrapped expression | ||
} | ||
params.push(U_1.default.stringify(expression[1], false)); | ||
params.push(U_1.U.stringify(expression[1], false)); | ||
return (0, never_catch_1.ok)(partialQuery("$".concat(paramsStart++), params)); | ||
@@ -637,4 +683,3 @@ case '=n': | ||
case 0: | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) | ||
: (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), "> -> no operands given")); | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), "> -> no operands given")); | ||
case 1: | ||
@@ -669,8 +714,6 @@ return (0, never_catch_1.ok)(partialQuery(tokens[0], params)); | ||
if (expression[1] === undefined) { | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) | ||
: (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[name] -> undefined")); | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[name] -> undefined")); | ||
} | ||
if (expression[2] === undefined) { | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) | ||
: (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[parameters] -> undefined")); | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[parameters] -> undefined")); | ||
} | ||
@@ -684,3 +727,4 @@ for (var _b = 0, _c = expression[2]; _b < _c.length; _b++) { | ||
if (v2Result.value.text === '') { | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) | ||
return ignore | ||
? (0, never_catch_1.ok)(partialQuery()) | ||
: (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[parameters][").concat(expression[2].indexOf(v2), "] -> neutral")); | ||
@@ -821,4 +865,3 @@ } | ||
if (e1Result.value.text === '') { | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) | ||
: (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> netural")); | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> netural")); | ||
} | ||
@@ -832,3 +875,4 @@ params.push.apply(params, e1Result.value.params); | ||
if (e2Result.value.text === '') { | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) | ||
return ignore | ||
? (0, never_catch_1.ok)(partialQuery()) | ||
: (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> netural")); | ||
@@ -874,4 +918,3 @@ } | ||
if (e1Result.value.text === '') { | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) | ||
: (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> netural")); | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> netural")); | ||
} | ||
@@ -881,3 +924,4 @@ params.push.apply(params, e1Result.value.params); | ||
if (expression[2] === undefined) { | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) | ||
return ignore | ||
? (0, never_catch_1.ok)(partialQuery()) | ||
: (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> undefined")); | ||
@@ -905,3 +949,5 @@ } | ||
case 0: | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> empty")); | ||
return ignore | ||
? (0, never_catch_1.ok)(partialQuery()) | ||
: (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> empty")); | ||
case 1: | ||
@@ -948,4 +994,3 @@ switch (expression[0]) { | ||
if (e1Result.value.text === '') { | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) | ||
: (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> netural")); | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> netural")); | ||
} | ||
@@ -959,3 +1004,4 @@ params.push.apply(params, e1Result.value.params); | ||
if (e2Result.value.text === '') { | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) | ||
return ignore | ||
? (0, never_catch_1.ok)(partialQuery()) | ||
: (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> netural")); | ||
@@ -970,4 +1016,3 @@ } | ||
if (e3Result.value.text === '') { | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) | ||
: (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[third operand] -> netural")); | ||
return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[third operand] -> netural")); | ||
} | ||
@@ -993,3 +1038,4 @@ params.push.apply(params, e3Result.value.params); | ||
var tableData = tablesData_1[_i]; | ||
if (tableData.alias === tableAlias && tableData.table.columns[splitColumn.slice(tableAliasUntilIndex).join('_')] !== undefined) { | ||
if (tableData.alias === tableAlias && | ||
tableData.table.columns[splitColumn.slice(tableAliasUntilIndex).join('_')] !== undefined) { | ||
return tableData; | ||
@@ -996,0 +1042,0 @@ } |
declare const PostgresErrors: { | ||
readonly DATABASE_ALREADY_EXISTS: "42P04"; | ||
}; | ||
export default PostgresErrors; | ||
export { PostgresErrors }; | ||
//# sourceMappingURL=error.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PostgresErrors = void 0; | ||
var PostgresErrors = { | ||
DATABASE_ALREADY_EXISTS: '42P04' | ||
}; | ||
exports.default = PostgresErrors; | ||
exports.PostgresErrors = PostgresErrors; | ||
//# sourceMappingURL=error.js.map |
@@ -1,37 +0,19 @@ | ||
import U from './U'; | ||
import PostgresErrors from './error'; | ||
import type { Pool } from './types/pool'; | ||
import { createContext } from './context'; | ||
import { createModelUtils } from './model'; | ||
import type { Context } from './types/context'; | ||
import type { ClientBase, PoolClient } from 'pg'; | ||
import Table, { TableCheck } from './types/table'; | ||
import type { TestTableData } from './types/testUtil'; | ||
import type { SimpleModel, Model } from './types/model'; | ||
import { createPool, addHook, removeHook } from './pool'; | ||
import { testTransaction, createTestTableData } from './testUtil'; | ||
import type { JSON, JsonObject, JsonArray, BaseJsonValue } from './types/json'; | ||
import { createEntity, resolveResult, resolveReturning, resolveExpression } from './entity'; | ||
import type { OrderDirection, PostgresType, PostgresTypeMapper, ColumnTypeByColumns, ColumnTypeByTable } from './types/postgres'; | ||
import { createTables, dropTables, resolveTablesDependency, createSequencesSQL, dropSequencesSQL, createTableSQL, dropTableSQL, getSequenceName } from './schema'; | ||
import * as Parser from './parser'; | ||
import type { JoinType, Param, QueryData, TableWithAlias, JoinData, Mode, CustomColumn, ExpressionTypes, Expression, ValueExpression, QueryExpression, InsertValue, UpdateSets, Query, QueryResult, QueryResultRow, PartialQuery } from './types/entity'; | ||
export type { Pool }; | ||
export type { Context }; | ||
export type { ClientBase, PoolClient }; | ||
export type { Table, TableCheck }; | ||
export type { TestTableData }; | ||
export type { SimpleModel, Model }; | ||
export type { JSON, JsonObject, JsonArray, BaseJsonValue }; | ||
export type { OrderDirection, PostgresType, PostgresTypeMapper, ColumnTypeByColumns, ColumnTypeByTable }; | ||
export type { JoinType, Param, QueryData, TableWithAlias, JoinData, Mode, CustomColumn, ExpressionTypes, Expression, ValueExpression, QueryExpression, InsertValue, UpdateSets, Query, QueryResult, QueryResultRow, PartialQuery }; | ||
export { U }; | ||
export { PostgresErrors }; | ||
export { createContext }; | ||
export { createModelUtils }; | ||
export { createPool, addHook, removeHook }; | ||
export { testTransaction, createTestTableData }; | ||
export { createEntity, resolveResult, resolveReturning, resolveExpression }; | ||
export { createTables, dropTables, resolveTablesDependency, createSequencesSQL, dropSequencesSQL, createTableSQL, dropTableSQL, getSequenceName }; | ||
export { Parser }; | ||
export { U } from './U'; | ||
export { PostgresErrors } from './error'; | ||
export type { Pool } from './types/pool'; | ||
export { createContext } from './context'; | ||
export { createModelUtils } from './model'; | ||
export type { Context } from './types/context'; | ||
export type { ClientBase, PoolClient } from 'pg'; | ||
export type { TestTableData } from './types/testUtil'; | ||
export type { Table, TableCheck } from './types/table'; | ||
export type { SimpleModel, Model } from './types/model'; | ||
export { createPool, addHook, removeHook } from './pool'; | ||
export { testTransaction, createTestTableData } from './testUtil'; | ||
export type { JSON, JsonObject, JsonArray, BaseJsonValue } from './types/json'; | ||
export { createEntity, resolveResult, resolveReturning, resolveExpression } from './entity'; | ||
export type { OrderDirection, PostgresType, PostgresTypeMapper, ColumnTypeByColumns, ColumnTypeByTable } from './types/postgres'; | ||
export { createTables, dropTables, resolveTablesDependency, createSequencesSQL, dropSequencesSQL, createTableSQL, dropTableSQL, getSequenceName } from './schema'; | ||
export * as Parser from './parser'; | ||
export type { JoinType, Param, QueryData, TableWithAlias, JoinData, Mode, CustomColumn, ExpressionTypes, Expression, ValueExpression, QueryExpression, InsertValue, UpdateSets, Query, QueryResult, QueryResultRow, PartialQuery } from './types/entity'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -25,11 +25,8 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Parser = exports.getSequenceName = exports.dropTableSQL = exports.createTableSQL = exports.dropSequencesSQL = exports.createSequencesSQL = exports.resolveTablesDependency = exports.dropTables = exports.createTables = exports.resolveExpression = exports.resolveReturning = exports.resolveResult = exports.createEntity = exports.createTestTableData = exports.testTransaction = exports.removeHook = exports.addHook = exports.createPool = exports.createModelUtils = exports.createContext = exports.PostgresErrors = exports.U = void 0; | ||
var U_1 = __importDefault(require("./U")); | ||
exports.U = U_1.default; | ||
var error_1 = __importDefault(require("./error")); | ||
exports.PostgresErrors = error_1.default; | ||
var U_1 = require("./U"); | ||
Object.defineProperty(exports, "U", { enumerable: true, get: function () { return U_1.U; } }); | ||
var error_1 = require("./error"); | ||
Object.defineProperty(exports, "PostgresErrors", { enumerable: true, get: function () { return error_1.PostgresErrors; } }); | ||
var context_1 = require("./context"); | ||
@@ -60,4 +57,3 @@ Object.defineProperty(exports, "createContext", { enumerable: true, get: function () { return context_1.createContext; } }); | ||
Object.defineProperty(exports, "getSequenceName", { enumerable: true, get: function () { return schema_1.getSequenceName; } }); | ||
var Parser = __importStar(require("./parser")); | ||
exports.Parser = Parser; | ||
exports.Parser = __importStar(require("./parser")); | ||
//# sourceMappingURL=index.js.map |
import Decimal from 'decimal.js'; | ||
import Table from './types/table'; | ||
import { Table } from './types/table'; | ||
import { ModelUtils } from './types/model'; | ||
@@ -4,0 +4,0 @@ import { ColumnTypeByColumns } from './types/postgres'; |
@@ -0,0 +0,0 @@ "use strict"; |
import Decimal from 'decimal.js'; | ||
import { JSON } from './types/json'; | ||
import { JSON as Json } from './types/json'; | ||
declare const boolean: (v: unknown) => boolean | undefined; | ||
@@ -10,4 +10,4 @@ declare const number: (v: unknown) => number | undefined; | ||
declare const date: (v: unknown) => Date | undefined; | ||
declare const json: (v: unknown) => JSON | undefined; | ||
declare const json: (v: unknown) => Json | undefined; | ||
export { boolean, number, integer, bigInt, decimal, string, date, json }; | ||
//# sourceMappingURL=parser.d.ts.map |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import type { AddHook, Pool, RemoveHook } from './types/pool'; |
@@ -75,3 +75,4 @@ "use strict"; | ||
return pool.connect().then(function (client) { | ||
return client.query("BEGIN TRANSACTION ISOLATION LEVEL ".concat((0, dictionary_1.toTransactionMode)(isolationLevel, readOnly), " ;")) | ||
return client | ||
.query("BEGIN TRANSACTION ISOLATION LEVEL ".concat((0, dictionary_1.toTransactionMode)(isolationLevel, readOnly), " ;")) | ||
.then(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -78,0 +79,0 @@ var result; |
import type { ClientBase } from 'pg'; | ||
import type Table from './types/table'; | ||
import type { Table } from './types/table'; | ||
import { type Result } from 'never-catch'; | ||
@@ -4,0 +4,0 @@ declare const createTables: (client: ClientBase, tables: Table[], resolve?: boolean) => Promise<Result<undefined, string | { |
@@ -76,4 +76,6 @@ "use strict"; | ||
switch (_d.label) { | ||
case 0: return [4 /*yield*/, client.query(sequenceSQL) | ||
.then(function () { return true; }).catch(function (e) { return ({ db: e, query: sequenceSQL }); })]; | ||
case 0: return [4 /*yield*/, client | ||
.query(sequenceSQL) | ||
.then(function () { return true; }) | ||
.catch(function (e) { return ({ db: e, query: sequenceSQL }); })]; | ||
case 1: | ||
@@ -112,4 +114,6 @@ result = _d.sent(); | ||
tableSQL = createTableSQL(table); | ||
return [4 /*yield*/, client.query(tableSQL) | ||
.then(function () { return true; }).catch(function (e) { return ({ db: e, query: tableSQL }); })]; | ||
return [4 /*yield*/, client | ||
.query(tableSQL) | ||
.then(function () { return true; }) | ||
.catch(function (e) { return ({ db: e, query: tableSQL }); })]; | ||
case 1: | ||
@@ -165,4 +169,6 @@ result = _e.sent(); | ||
tableSQL = dropTableSQL(table); | ||
return [4 /*yield*/, client.query(tableSQL) | ||
.then(function () { return true; }).catch(function (e) { return ({ db: e, query: tableSQL }); })]; | ||
return [4 /*yield*/, client | ||
.query(tableSQL) | ||
.then(function () { return true; }) | ||
.catch(function (e) { return ({ db: e, query: tableSQL }); })]; | ||
case 1: | ||
@@ -202,4 +208,6 @@ result = _d.sent(); | ||
switch (_e.label) { | ||
case 0: return [4 /*yield*/, client.query(sequenceSQL) | ||
.then(function () { return true; }).catch(function (e) { return ({ db: e, query: sequenceSQL }); })]; | ||
case 0: return [4 /*yield*/, client | ||
.query(sequenceSQL) | ||
.then(function () { return true; }) | ||
.catch(function (e) { return ({ db: e, query: sequenceSQL }); })]; | ||
case 1: | ||
@@ -381,7 +389,9 @@ result = _e.sent(); | ||
if (primaryKeys.length !== 0) { | ||
constraints.push("CONSTRAINT \"".concat(table.title, "_pk\" PRIMARY KEY( ").concat(primaryKeys.map(function (_a) { | ||
constraints.push("CONSTRAINT \"".concat(table.title, "_pk\" PRIMARY KEY( ").concat(primaryKeys | ||
.map(function (_a) { | ||
var _b; | ||
var key = _a[0], column = _a[1]; | ||
return "\"".concat((_b = column.title) !== null && _b !== void 0 ? _b : key, "\""); | ||
}).join(', '), " )")); | ||
}) | ||
.join(', '), " )")); | ||
} | ||
@@ -399,4 +409,4 @@ // add columns and constrains union | ||
exports.dropTableSQL = dropTableSQL; | ||
var getSequenceName = function (tableSchema, tableTitle, columnKey, column) { var _a, _b; return "\"".concat(tableSchema, "\".\"") + ((_a = column.seqTitle) !== null && _a !== void 0 ? _a : (tableTitle + '_' + ((_b = column.title) !== null && _b !== void 0 ? _b : columnKey) + '_seq')) + '"'; }; | ||
var getSequenceName = function (tableSchema, tableTitle, columnKey, column) { var _a, _b; return "\"".concat(tableSchema, "\".\"") + ((_a = column.seqTitle) !== null && _a !== void 0 ? _a : tableTitle + '_' + ((_b = column.title) !== null && _b !== void 0 ? _b : columnKey) + '_seq') + '"'; }; | ||
exports.getSequenceName = getSequenceName; | ||
//# sourceMappingURL=schema.js.map |
@@ -0,0 +0,0 @@ import { CreateTestTableData, TestTransaction } from './types/testUtil'; |
@@ -77,10 +77,13 @@ "use strict"; | ||
.insert(tableWithData.startData, [{ exp: true, as: 'confirm' }], { | ||
nullableDefaultColumns: Object.entries(tableWithData.table.columns).filter(function (_a) { | ||
nullableDefaultColumns: Object.entries(tableWithData.table.columns) | ||
.filter(function (_a) { | ||
var _ = _a[0], value = _a[1]; | ||
return value.nullable || value.default !== false; | ||
}).map(function (_a) { | ||
}) | ||
.map(function (_a) { | ||
var key = _a[0], _ = _a[1]; | ||
return key; | ||
}) | ||
}).exec(client, ['count', tableWithData.startData.length])]; | ||
}) | ||
.exec(client, ['count', tableWithData.startData.length])]; | ||
case 3: | ||
@@ -108,11 +111,12 @@ insertResult = _d.sent(); | ||
case 0: | ||
primaryKeys = Object.entries(table.columns).filter(function (_a) { | ||
primaryKeys = Object.entries(table.columns) | ||
.filter(function (_a) { | ||
var _ = _a[0], value = _a[1]; | ||
return !value.nullable && value.primary; | ||
}).map(function (_a) { | ||
}) | ||
.map(function (_a) { | ||
var key = _a[0], _ = _a[1]; | ||
return key; | ||
}); | ||
return [4 /*yield*/, (0, entity_1.createEntity)(table) | ||
.select(Object.keys(table.columns), true).exec(client, [])]; | ||
return [4 /*yield*/, (0, entity_1.createEntity)(table).select(Object.keys(table.columns), true).exec(client, [])]; | ||
case 1: | ||
@@ -334,4 +338,10 @@ selectResult = _h.sent(); | ||
exports.testTransaction = testTransaction; | ||
var createTestTableData = function (table, startData, finalData, skipIt, lengthCheck) { return ({ table: table, startData: startData, finalData: finalData, skipIt: skipIt, lengthCheck: lengthCheck }); }; | ||
var createTestTableData = function (table, startData, finalData, skipIt, lengthCheck) { return ({ | ||
table: table, | ||
startData: startData, | ||
finalData: finalData, | ||
skipIt: skipIt, | ||
lengthCheck: lengthCheck | ||
}); }; | ||
exports.createTestTableData = createTestTableData; | ||
//# sourceMappingURL=testUtil.js.map |
import Decimal from 'decimal.js'; | ||
import type Table from './table'; | ||
import type { JSON } from './json'; | ||
import type { Table } from './table'; | ||
import type { ColumnTypeByColumns } from './postgres'; | ||
@@ -12,4 +12,4 @@ import type { Expression, ValueExpression } from './entity'; | ||
colList: <columnKey extends keyof Columns & string>(column: columnKey, op: ListOperatorCompatible<ColumnTypeByColumns<Columns, columnKey>, ListOperator, never>, v: readonly Expression<ColumnTypeByColumns<Columns, columnKey>>[] | undefined, alias?: string) => ValueExpression<boolean>; | ||
colLike: <columnKey extends keyof Columns & string, Op extends (string extends ColumnTypeByColumns<Columns, columnKey> ? LikeOperator : never)>(column: columnKey, op: Op, v: Op extends 'like' ? Expression<ColumnTypeByColumns<Columns, columnKey>> : readonly Expression<ColumnTypeByColumns<Columns, columnKey>>[] | undefined, alias?: string) => ValueExpression<boolean>; | ||
colJson: <columnKey extends keyof Columns & string, Op extends (JSON extends ColumnTypeByColumns<Columns, columnKey> ? JsonOperator : never)>(column: columnKey, op: Op, v: Op extends '@>' | '<@' ? Expression<ColumnTypeByColumns<Columns, columnKey>> : Op extends '?' ? Expression<string> : Op extends '?&' | '?|' ? readonly Expression<string>[] | undefined : never, alias?: string) => ValueExpression<boolean>; | ||
colLike: <columnKey extends keyof Columns & string, Op extends string extends ColumnTypeByColumns<Columns, columnKey> ? LikeOperator : never>(column: columnKey, op: Op, v: Op extends 'like' ? Expression<ColumnTypeByColumns<Columns, columnKey>> : readonly Expression<ColumnTypeByColumns<Columns, columnKey>>[] | undefined, alias?: string) => ValueExpression<boolean>; | ||
colJson: <columnKey extends keyof Columns & string, Op extends JSON extends ColumnTypeByColumns<Columns, columnKey> ? JsonOperator : never>(column: columnKey, op: Op, v: Op extends '@>' | '<@' ? Expression<ColumnTypeByColumns<Columns, columnKey>> : Op extends '?' ? Expression<string> : Op extends '?&' | '?|' ? readonly Expression<string>[] | undefined : never, alias?: string) => ValueExpression<boolean>; | ||
colsAnd: ContextScope<Columns>; | ||
@@ -19,7 +19,3 @@ colsOr: ContextScope<Columns>; | ||
type ContextScope<Columns extends Table['columns']> = (rules: { | ||
[columnKey in keyof Columns]?: [ | ||
op: null extends ColumnTypeByColumns<Columns, columnKey> ? NullOperator : never | ||
] | [ | ||
op: ColumnTypeByColumns<Columns, columnKey> extends boolean ? BooleanOperator : never | ||
] | [ | ||
[columnKey in keyof Columns]?: [op: null extends ColumnTypeByColumns<Columns, columnKey> ? NullOperator : never] | [op: ColumnTypeByColumns<Columns, columnKey> extends boolean ? BooleanOperator : never] | [ | ||
op: CompareOperatorCompatible<ColumnTypeByColumns<Columns, columnKey>, CompareOperator, never>, | ||
@@ -26,0 +22,0 @@ v: Expression<ColumnTypeByColumns<Columns, columnKey>> |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=context.js.map |
import { JSON } from './json'; | ||
import Decimal from 'decimal.js'; | ||
import type Table from './table'; | ||
import type { Table } from './table'; | ||
import { Context } from './context'; | ||
@@ -5,0 +5,0 @@ import type { ClientBase } from 'pg'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=entity.js.map |
@@ -0,0 +0,0 @@ import Decimal from 'decimal.js'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=json.js.map |
@@ -1,4 +0,4 @@ | ||
import type Table from './table'; | ||
import type { Table } from './table'; | ||
import type { ColumnTypeByColumns } from './postgres'; | ||
import { Result } from "never-catch"; | ||
import { Result } from 'never-catch'; | ||
type SimpleModel<Columns extends Table['columns']> = { | ||
@@ -5,0 +5,0 @@ [col in keyof Columns & string]: ColumnTypeByColumns<Columns, col>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=model.js.map |
import { Param } from './entity'; | ||
import { Result } from 'never-catch'; | ||
import type { Pool as PgPool, PoolClient } from 'pg'; | ||
import { Result } from "never-catch"; | ||
type TransactionIsolationLevel = 'read-uncommitted' | 'read-committed' | 'repeatable-read' | 'serializable'; | ||
@@ -5,0 +5,0 @@ type Pool = { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=pool.js.map |
import Decimal from 'decimal.js'; | ||
import type Table from './table'; | ||
import type { Table } from './table'; | ||
import type { JSON } from './json'; | ||
type OrderDirection = 'asc' | 'desc'; | ||
type PostgresType = 'boolean' | 'smallint' | 'integer' | 'bigint' | 'real' | 'double precision' | 'numeric' | 'character' | 'character varying' | 'text' | 'uuid' | 'date' | 'timestamp without time zone' | 'timestamp with time zone' | 'json' | 'jsonb'; | ||
type PostgresTypeMapper<Type extends PostgresType, Nullable extends boolean> = Type extends 'boolean' ? (true extends Nullable ? null : never) | boolean : Type extends ('smallint' | 'integer' | 'real' | 'double precision') ? (true extends Nullable ? null : never) | number : Type extends 'bigint' ? (true extends Nullable ? null : never) | bigint : Type extends 'numeric' ? (true extends Nullable ? null : never) | Decimal : Type extends ('character' | 'character varying' | 'text' | 'uuid') ? (true extends Nullable ? null : never) | string : Type extends ('date' | 'timestamp without time zone' | 'timestamp with time zone') ? (true extends Nullable ? null : never) | Date : Type extends ('json' | 'jsonb') ? (true extends Nullable ? null : never) | JSON : never; | ||
type PostgresTypeMapper<Type extends PostgresType, Nullable extends boolean> = Type extends 'boolean' ? (true extends Nullable ? null : never) | boolean : Type extends 'smallint' | 'integer' | 'real' | 'double precision' ? (true extends Nullable ? null : never) | number : Type extends 'bigint' ? (true extends Nullable ? null : never) | bigint : Type extends 'numeric' ? (true extends Nullable ? null : never) | Decimal : Type extends 'character' | 'character varying' | 'text' | 'uuid' ? (true extends Nullable ? null : never) | string : Type extends 'date' | 'timestamp without time zone' | 'timestamp with time zone' ? (true extends Nullable ? null : never) | Date : Type extends 'json' | 'jsonb' ? (true extends Nullable ? null : never) | JSON : never; | ||
type ColumnTypeByColumns<Columns extends Table['columns'], columnKey extends keyof Columns> = PostgresTypeMapper<Columns[columnKey]['type'], Columns[columnKey]['nullable']>; | ||
@@ -8,0 +8,0 @@ type ColumnTypeByTable<T extends Table, columnKey extends keyof T['columns']> = PostgresTypeMapper<T['columns'][columnKey]['type'], T['columns'][columnKey]['nullable']>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=postgres.js.map |
@@ -233,4 +233,3 @@ import Decimal from 'decimal.js'; | ||
}; | ||
export default Table; | ||
export type { ReferenceActions, Column, TableCheck }; | ||
export type { Table, ReferenceActions, Column, TableCheck }; | ||
//# sourceMappingURL=table.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=table.js.map |
@@ -1,2 +0,2 @@ | ||
import Table from './table'; | ||
import { Table } from './table'; | ||
import { PoolClient } from 'pg'; | ||
@@ -3,0 +3,0 @@ import { Result } from 'never-catch'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=testUtil.js.map |
import * as U from './utils'; | ||
export default U; | ||
export { U }; | ||
//# sourceMappingURL=U.d.ts.map |
@@ -26,4 +26,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.U = void 0; | ||
var U = __importStar(require("./utils")); | ||
exports.default = U; | ||
exports.U = U; | ||
//# sourceMappingURL=U.js.map |
import Decimal from 'decimal.js'; | ||
import type Table from './types/table'; | ||
import type { JSON } from './types/json'; | ||
import { JSON as Json } from './types/json'; | ||
import type { Table } from './types/table'; | ||
import type { ColumnTypeByTable, PostgresType } from './types/postgres'; | ||
@@ -14,3 +14,3 @@ import type { ExpressionTypes, ValueExpression, Expression, QueryExpression, Query } from './types/entity'; | ||
declare function val(v: Date | undefined): ValueExpression<Date>; | ||
declare function val(v: JSON | undefined): ValueExpression<JSON>; | ||
declare function val(v: Json | undefined): ValueExpression<Json>; | ||
declare function col<T extends Table, C extends keyof T['columns'] & string>(table: T, column: C, full?: boolean, alias?: string): ValueExpression<ColumnTypeByTable<T, C>>; | ||
@@ -32,7 +32,7 @@ declare function fun<T extends ExpressionTypes>(name: string | undefined, args: readonly Expression<any>[] | undefined, cast?: string): ValueExpression<T>; | ||
declare function conOp(v1: Expression<string>, v2: Expression<string>): ValueExpression<string>; | ||
declare function conOp(v1: Expression<JSON>, v2: Expression<JSON>): ValueExpression<JSON>; | ||
declare function conOp(v1: Expression<Json>, v2: Expression<Json>): ValueExpression<Json>; | ||
declare function conAllOp(v: readonly Expression<string>[] | undefined): ValueExpression<string>; | ||
declare function conAllOp(v: readonly Expression<JSON>[] | undefined): ValueExpression<JSON>; | ||
declare function jMinusOp(v1: Expression<JSON>, v2: Expression<string>): ValueExpression<JSON>; | ||
declare function jArrMinusOp(v1: Expression<JSON>, v2: readonly Expression<string>[] | undefined): ValueExpression<JSON>; | ||
declare function conAllOp(v: readonly Expression<Json>[] | undefined): ValueExpression<Json>; | ||
declare function jMinusOp(v1: Expression<Json>, v2: Expression<string>): ValueExpression<Json>; | ||
declare function jArrMinusOp(v1: Expression<Json>, v2: readonly Expression<string>[] | undefined): ValueExpression<Json>; | ||
declare function andOp(v1: Expression<boolean>, v2: Expression<boolean>): ValueExpression<boolean>; | ||
@@ -61,5 +61,5 @@ declare function andAllOp(v: readonly Expression<boolean>[] | undefined): ValueExpression<boolean>; | ||
declare function likeOp(v1: Expression<string>, op: Extract<LikeOperator, 'like all' | 'like some'>, v2: readonly Expression<string>[] | undefined): ValueExpression<boolean>; | ||
declare function jsonOp(v1: Expression<JSON>, op: Extract<JsonOperator, '@>' | '<@'>, v2: Expression<JSON>): ValueExpression<boolean>; | ||
declare function jsonOp(v1: Expression<JSON>, op: Extract<JsonOperator, '?'>, v2: Expression<string>): ValueExpression<boolean>; | ||
declare function jsonOp(v1: Expression<JSON>, op: Extract<JsonOperator, '?&' | '?|'>, v2: readonly Expression<string>[] | undefined): ValueExpression<boolean>; | ||
declare function jsonOp(v1: Expression<Json>, op: Extract<JsonOperator, '@>' | '<@'>, v2: Expression<Json>): ValueExpression<boolean>; | ||
declare function jsonOp(v1: Expression<Json>, op: Extract<JsonOperator, '?'>, v2: Expression<string>): ValueExpression<boolean>; | ||
declare function jsonOp(v1: Expression<Json>, op: Extract<JsonOperator, '?&' | '?|'>, v2: readonly Expression<string>[] | undefined): ValueExpression<boolean>; | ||
declare function existsOp(v: QueryExpression<any>): ValueExpression<boolean>; | ||
@@ -66,0 +66,0 @@ declare const stringify: <T extends ExpressionTypes>(v: T, inline?: boolean) => T extends number ? number : T extends bigint ? bigint : string; |
@@ -201,3 +201,3 @@ "use strict"; | ||
else { | ||
result = inline ? "'".concat(v, "'") : v; | ||
result = inline ? "'".concat(v, "'") : "".concat(v); | ||
} | ||
@@ -204,0 +204,0 @@ return result; |
{ | ||
"name": "@mrnafisia/type-query", | ||
"version": "1.0.48", | ||
"version": "1.0.49", | ||
"description": "mini-orm with full type support for postgres.", | ||
@@ -25,16 +25,17 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@types/jest": "^28.1.0", | ||
"@types/lodash": "^4.14.182", | ||
"@types/node": "^17.0.39", | ||
"@types/pg": "^8.6.5", | ||
"jest": "^28.1.0", | ||
"ts-jest": "^28.0.4", | ||
"typescript": "^4.7.2" | ||
"@types/jest": "^29.5.2", | ||
"@types/lodash": "^4.14.195", | ||
"@types/node": "^20.2.5", | ||
"@types/pg": "^8.10.1", | ||
"jest": "^29.5.0", | ||
"prettier": "^2.8.8", | ||
"ts-jest": "^29.1.0", | ||
"typescript": "^4.9.5" | ||
}, | ||
"dependencies": { | ||
"decimal.js": "^10.3.1", | ||
"decimal.js": "^10.4.3", | ||
"lodash": "^4.17.21", | ||
"never-catch": "^1.0.6", | ||
"pg": "^8.7.3" | ||
"never-catch": "^1.0.9", | ||
"pg": "^8.11.0" | ||
} | ||
} |
# type-query | ||
SQL Query Builder written in TypeScript |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9253
584429
8
Updateddecimal.js@^10.4.3
Updatednever-catch@^1.0.9
Updatedpg@^8.11.0