pqb 0.30.1 → 0.30.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +262 -222
- package/dist/index.js +316 -293
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +288 -266
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -29,8 +29,8 @@ const templateLiteralToSQL = (template, ctx, quotedAs) => {
|
|
|
29
29
|
class RawSQL extends orchidCore.RawSQLBase {
|
|
30
30
|
constructor(sql, values, type) {
|
|
31
31
|
super(sql, values);
|
|
32
|
+
this.result = { value: type };
|
|
32
33
|
if (type) {
|
|
33
|
-
this.result = { value: type };
|
|
34
34
|
Object.assign(this, type.operators);
|
|
35
35
|
}
|
|
36
36
|
}
|
|
@@ -836,102 +836,6 @@ const columnCode = (type, t, code, migration, data = type.data, skip) => {
|
|
|
836
836
|
return code.length === 1 && typeof code[0] === "string" ? code[0] : code;
|
|
837
837
|
};
|
|
838
838
|
|
|
839
|
-
function simpleColumnToSQL(ctx, key, column, quotedAs) {
|
|
840
|
-
if (!column)
|
|
841
|
-
return `"${key}"`;
|
|
842
|
-
const { data } = column;
|
|
843
|
-
return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
|
|
844
|
-
}
|
|
845
|
-
function simpleExistingColumnToSQL(ctx, key, column, quotedAs) {
|
|
846
|
-
const { data } = column;
|
|
847
|
-
return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
|
|
848
|
-
}
|
|
849
|
-
const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
|
|
850
|
-
var _a, _b, _c, _d, _e;
|
|
851
|
-
const index = column.indexOf(".");
|
|
852
|
-
if (index !== -1) {
|
|
853
|
-
const table = column.slice(0, index);
|
|
854
|
-
const key = column.slice(index + 1);
|
|
855
|
-
if (key === "*") {
|
|
856
|
-
if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
|
|
857
|
-
return select ? `row_to_json("${table}".*)` : `"${table}".r`;
|
|
858
|
-
}
|
|
859
|
-
return column;
|
|
860
|
-
}
|
|
861
|
-
const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
|
|
862
|
-
const quoted = `"${table}"`;
|
|
863
|
-
const col = quoted === quotedAs ? shape[key] : (_d = (_c = data.joinedShapes) == null ? void 0 : _c[tableName]) == null ? void 0 : _d[key];
|
|
864
|
-
if (col) {
|
|
865
|
-
if (col.data.name) {
|
|
866
|
-
return `"${tableName}"."${col.data.name}"`;
|
|
867
|
-
}
|
|
868
|
-
if (col.data.computed) {
|
|
869
|
-
return `${col.data.computed.toSQL(ctx, quoted)}`;
|
|
870
|
-
}
|
|
871
|
-
return `"${tableName}"."${key}"`;
|
|
872
|
-
}
|
|
873
|
-
return `"${tableName}"."${key}"`;
|
|
874
|
-
}
|
|
875
|
-
if (!select && ((_e = data.joinedShapes) == null ? void 0 : _e[column])) {
|
|
876
|
-
return `"${column}".r`;
|
|
877
|
-
}
|
|
878
|
-
return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
|
|
879
|
-
};
|
|
880
|
-
const columnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
|
|
881
|
-
var _a, _b, _c, _d;
|
|
882
|
-
const index = column.indexOf(".");
|
|
883
|
-
if (index !== -1) {
|
|
884
|
-
const table = column.slice(0, index);
|
|
885
|
-
const key = column.slice(index + 1);
|
|
886
|
-
if (key === "*") {
|
|
887
|
-
if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
|
|
888
|
-
return select ? `row_to_json("${table}".*) "${table}"` : `"${table}".r "${table}"`;
|
|
889
|
-
}
|
|
890
|
-
return column;
|
|
891
|
-
}
|
|
892
|
-
const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
|
|
893
|
-
const quoted = `"${table}"`;
|
|
894
|
-
const col2 = quoted === quotedAs ? data.shape[key] : (_c = data.joinedShapes) == null ? void 0 : _c[tableName][key];
|
|
895
|
-
if (col2) {
|
|
896
|
-
if (col2.data.name && col2.data.name !== key) {
|
|
897
|
-
return `"${tableName}"."${col2.data.name}" "${key}"`;
|
|
898
|
-
}
|
|
899
|
-
if (col2.data.computed) {
|
|
900
|
-
return `${col2.data.computed.toSQL(ctx, quoted)} "${key}"`;
|
|
901
|
-
}
|
|
902
|
-
}
|
|
903
|
-
return `"${tableName}"."${key}"`;
|
|
904
|
-
}
|
|
905
|
-
if (!select && ((_d = data.joinedShapes) == null ? void 0 : _d[column])) {
|
|
906
|
-
return select ? `row_to_json("${column}".*) "${column}"` : `"${column}".r "${column}"`;
|
|
907
|
-
}
|
|
908
|
-
const col = data.shape[column];
|
|
909
|
-
if (col) {
|
|
910
|
-
if (col.data.name && col.data.name !== column) {
|
|
911
|
-
return `${quotedAs ? `${quotedAs}.` : ""}"${col.data.name}" "${column}"`;
|
|
912
|
-
}
|
|
913
|
-
if (col.data.computed) {
|
|
914
|
-
return `${col.data.computed.toSQL(ctx, quotedAs)} "${column}"`;
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
return `${quotedAs ? `${quotedAs}.` : ""}"${column}"`;
|
|
918
|
-
};
|
|
919
|
-
const ownColumnToSql = (data, column, quotedAs) => {
|
|
920
|
-
var _a;
|
|
921
|
-
const name = (_a = data.shape[column]) == null ? void 0 : _a.data.name;
|
|
922
|
-
return `${quotedAs ? `${quotedAs}.` : ""}"${name || column}"${name && name !== column ? ` "${column}"` : ""}`;
|
|
923
|
-
};
|
|
924
|
-
const rawOrColumnToSql = (ctx, data, expr, quotedAs, shape = data.shape, select) => {
|
|
925
|
-
return typeof expr === "string" ? columnToSql(ctx, data, shape, expr, quotedAs, select) : expr.toSQL(ctx, quotedAs);
|
|
926
|
-
};
|
|
927
|
-
const quoteSchemaAndTable = (schema, table) => {
|
|
928
|
-
return schema ? `"${schema}"."${table}"` : `"${table}"`;
|
|
929
|
-
};
|
|
930
|
-
const addValue = (values, value) => {
|
|
931
|
-
values.push(value);
|
|
932
|
-
return `$${values.length}`;
|
|
933
|
-
};
|
|
934
|
-
|
|
935
839
|
var __defProp$f = Object.defineProperty;
|
|
936
840
|
var __defProps$7 = Object.defineProperties;
|
|
937
841
|
var __getOwnPropDescs$7 = Object.getOwnPropertyDescriptors;
|
|
@@ -979,7 +883,7 @@ const make = (_op) => {
|
|
|
979
883
|
const quoteValue$1 = (arg, ctx, quotedAs, jsonArray) => {
|
|
980
884
|
if (arg && typeof arg === "object") {
|
|
981
885
|
if (!jsonArray && Array.isArray(arg)) {
|
|
982
|
-
return `(${arg.map((value) => addValue(ctx.values, value)).join(", ")})`;
|
|
886
|
+
return `(${arg.map((value) => orchidCore.addValue(ctx.values, value)).join(", ")})`;
|
|
983
887
|
}
|
|
984
888
|
if (orchidCore.isExpression(arg)) {
|
|
985
889
|
return arg.toSQL(ctx, quotedAs);
|
|
@@ -988,7 +892,7 @@ const quoteValue$1 = (arg, ctx, quotedAs, jsonArray) => {
|
|
|
988
892
|
return `(${arg.toSQL({ values: ctx.values }).text})`;
|
|
989
893
|
}
|
|
990
894
|
}
|
|
991
|
-
return addValue(ctx.values, arg);
|
|
895
|
+
return orchidCore.addValue(ctx.values, arg);
|
|
992
896
|
};
|
|
993
897
|
const base = {
|
|
994
898
|
equals: make(
|
|
@@ -1861,6 +1765,98 @@ const queryTypeWithLimitOne = {
|
|
|
1861
1765
|
};
|
|
1862
1766
|
const isQueryReturnsAll = (q) => !q.q.returnType || q.q.returnType === "all";
|
|
1863
1767
|
|
|
1768
|
+
function simpleColumnToSQL(ctx, key, column, quotedAs) {
|
|
1769
|
+
if (!column)
|
|
1770
|
+
return `"${key}"`;
|
|
1771
|
+
const { data } = column;
|
|
1772
|
+
return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
|
|
1773
|
+
}
|
|
1774
|
+
function simpleExistingColumnToSQL(ctx, key, column, quotedAs) {
|
|
1775
|
+
const { data } = column;
|
|
1776
|
+
return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
|
|
1777
|
+
}
|
|
1778
|
+
const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
|
|
1779
|
+
var _a, _b, _c, _d, _e;
|
|
1780
|
+
const index = column.indexOf(".");
|
|
1781
|
+
if (index !== -1) {
|
|
1782
|
+
const table = column.slice(0, index);
|
|
1783
|
+
const key = column.slice(index + 1);
|
|
1784
|
+
if (key === "*") {
|
|
1785
|
+
if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
|
|
1786
|
+
return select ? `row_to_json("${table}".*)` : `"${table}".r`;
|
|
1787
|
+
}
|
|
1788
|
+
return column;
|
|
1789
|
+
}
|
|
1790
|
+
const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
|
|
1791
|
+
const quoted = `"${table}"`;
|
|
1792
|
+
const col = quoted === quotedAs ? shape[key] : (_d = (_c = data.joinedShapes) == null ? void 0 : _c[tableName]) == null ? void 0 : _d[key];
|
|
1793
|
+
if (col) {
|
|
1794
|
+
if (col.data.name) {
|
|
1795
|
+
return `"${tableName}"."${col.data.name}"`;
|
|
1796
|
+
}
|
|
1797
|
+
if (col.data.computed) {
|
|
1798
|
+
return `${col.data.computed.toSQL(ctx, quoted)}`;
|
|
1799
|
+
}
|
|
1800
|
+
return `"${tableName}"."${key}"`;
|
|
1801
|
+
}
|
|
1802
|
+
return `"${tableName}"."${key}"`;
|
|
1803
|
+
}
|
|
1804
|
+
if (!select && ((_e = data.joinedShapes) == null ? void 0 : _e[column])) {
|
|
1805
|
+
return `"${column}".r`;
|
|
1806
|
+
}
|
|
1807
|
+
return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
|
|
1808
|
+
};
|
|
1809
|
+
const columnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
|
|
1810
|
+
var _a, _b, _c, _d;
|
|
1811
|
+
const index = column.indexOf(".");
|
|
1812
|
+
if (index !== -1) {
|
|
1813
|
+
const table = column.slice(0, index);
|
|
1814
|
+
const key = column.slice(index + 1);
|
|
1815
|
+
if (key === "*") {
|
|
1816
|
+
if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
|
|
1817
|
+
return select ? `row_to_json("${table}".*) "${table}"` : `"${table}".r "${table}"`;
|
|
1818
|
+
}
|
|
1819
|
+
return column;
|
|
1820
|
+
}
|
|
1821
|
+
const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
|
|
1822
|
+
const quoted = `"${table}"`;
|
|
1823
|
+
const col2 = quoted === quotedAs ? data.shape[key] : (_c = data.joinedShapes) == null ? void 0 : _c[tableName][key];
|
|
1824
|
+
if (col2) {
|
|
1825
|
+
if (col2.data.name && col2.data.name !== key) {
|
|
1826
|
+
return `"${tableName}"."${col2.data.name}" "${key}"`;
|
|
1827
|
+
}
|
|
1828
|
+
if (col2.data.computed) {
|
|
1829
|
+
return `${col2.data.computed.toSQL(ctx, quoted)} "${key}"`;
|
|
1830
|
+
}
|
|
1831
|
+
}
|
|
1832
|
+
return `"${tableName}"."${key}"`;
|
|
1833
|
+
}
|
|
1834
|
+
if (!select && ((_d = data.joinedShapes) == null ? void 0 : _d[column])) {
|
|
1835
|
+
return select ? `row_to_json("${column}".*) "${column}"` : `"${column}".r "${column}"`;
|
|
1836
|
+
}
|
|
1837
|
+
const col = data.shape[column];
|
|
1838
|
+
if (col) {
|
|
1839
|
+
if (col.data.name && col.data.name !== column) {
|
|
1840
|
+
return `${quotedAs ? `${quotedAs}.` : ""}"${col.data.name}" "${column}"`;
|
|
1841
|
+
}
|
|
1842
|
+
if (col.data.computed) {
|
|
1843
|
+
return `${col.data.computed.toSQL(ctx, quotedAs)} "${column}"`;
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
return `${quotedAs ? `${quotedAs}.` : ""}"${column}"`;
|
|
1847
|
+
};
|
|
1848
|
+
const ownColumnToSql = (data, column, quotedAs) => {
|
|
1849
|
+
var _a;
|
|
1850
|
+
const name = (_a = data.shape[column]) == null ? void 0 : _a.data.name;
|
|
1851
|
+
return `${quotedAs ? `${quotedAs}.` : ""}"${name || column}"${name && name !== column ? ` "${column}"` : ""}`;
|
|
1852
|
+
};
|
|
1853
|
+
const rawOrColumnToSql = (ctx, data, expr, quotedAs, shape = data.shape, select) => {
|
|
1854
|
+
return typeof expr === "string" ? columnToSql(ctx, data, shape, expr, quotedAs, select) : expr.toSQL(ctx, quotedAs);
|
|
1855
|
+
};
|
|
1856
|
+
const quoteSchemaAndTable = (schema, table) => {
|
|
1857
|
+
return schema ? `"${schema}"."${table}"` : `"${table}"`;
|
|
1858
|
+
};
|
|
1859
|
+
|
|
1864
1860
|
const pushDistinctSql = (ctx, table, distinct, quotedAs) => {
|
|
1865
1861
|
ctx.sql.push("DISTINCT");
|
|
1866
1862
|
if (distinct.length) {
|
|
@@ -2021,7 +2017,7 @@ const processAnds = (and, ctx, table, query, quotedAs, parens) => {
|
|
|
2021
2017
|
return parens && ands.length > 1 ? `(${sql})` : sql;
|
|
2022
2018
|
};
|
|
2023
2019
|
const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
2024
|
-
var _a, _b;
|
|
2020
|
+
var _a, _b, _c, _d;
|
|
2025
2021
|
if (typeof data === "function") {
|
|
2026
2022
|
const qb = Object.create(table);
|
|
2027
2023
|
qb.q = getClonedQueryData(query);
|
|
@@ -2090,10 +2086,10 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2090
2086
|
);
|
|
2091
2087
|
const rightPath = item[3];
|
|
2092
2088
|
ands.push(
|
|
2093
|
-
`jsonb_path_query_first(${leftColumn}, ${addValue(
|
|
2089
|
+
`jsonb_path_query_first(${leftColumn}, ${orchidCore.addValue(
|
|
2094
2090
|
ctx.values,
|
|
2095
2091
|
leftPath
|
|
2096
|
-
)}) = jsonb_path_query_first(${rightColumn}, ${addValue(
|
|
2092
|
+
)}) = jsonb_path_query_first(${rightColumn}, ${orchidCore.addValue(
|
|
2097
2093
|
ctx.values,
|
|
2098
2094
|
rightPath
|
|
2099
2095
|
)})`
|
|
@@ -2183,7 +2179,8 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2183
2179
|
column = quotedAs === quoted ? query.shape[name] : (_b = (_a = query.joinedShapes) == null ? void 0 : _a[table2]) == null ? void 0 : _b[name];
|
|
2184
2180
|
quotedColumn = simpleColumnToSQL(ctx, name, column, quoted);
|
|
2185
2181
|
} else {
|
|
2186
|
-
|
|
2182
|
+
column = (_d = (_c = query.joinedShapes) == null ? void 0 : _c[key]) == null ? void 0 : _d.value;
|
|
2183
|
+
quotedColumn = `"${key}".r`;
|
|
2187
2184
|
}
|
|
2188
2185
|
if (!column || !quotedColumn) {
|
|
2189
2186
|
throw new Error(`Unknown column ${key} provided to condition`);
|
|
@@ -2212,7 +2209,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2212
2209
|
}
|
|
2213
2210
|
} else {
|
|
2214
2211
|
ands.push(
|
|
2215
|
-
`${columnToSql(ctx, query, query.shape, key, quotedAs)} ${value === null ? "IS NULL" : `= ${addValue(ctx.values, value)}`}`
|
|
2212
|
+
`${columnToSql(ctx, query, query.shape, key, quotedAs)} ${value === null ? "IS NULL" : `= ${orchidCore.addValue(ctx.values, value)}`}`
|
|
2216
2213
|
);
|
|
2217
2214
|
}
|
|
2218
2215
|
}
|
|
@@ -2225,7 +2222,7 @@ const pushIn = (ctx, query, ands, quotedAs, arg) => {
|
|
|
2225
2222
|
let value;
|
|
2226
2223
|
if (Array.isArray(arg.values)) {
|
|
2227
2224
|
value = `${arg.values.map(
|
|
2228
|
-
multiple ? (arr) => `(${arr.map((value2) => addValue(ctx.values, value2)).join(", ")})` : (arr) => `${arr.map((value2) => addValue(ctx.values, value2)).join(", ")}`
|
|
2225
|
+
multiple ? (arr) => `(${arr.map((value2) => orchidCore.addValue(ctx.values, value2)).join(", ")})` : (arr) => `${arr.map((value2) => orchidCore.addValue(ctx.values, value2)).join(", ")}`
|
|
2229
2226
|
).join(", ")}`;
|
|
2230
2227
|
value = `(${value})`;
|
|
2231
2228
|
} else if (orchidCore.isExpression(arg.values)) {
|
|
@@ -3456,6 +3453,8 @@ const getShapeFromSelect = (q, isSubQuery) => {
|
|
|
3456
3453
|
}
|
|
3457
3454
|
}
|
|
3458
3455
|
}
|
|
3456
|
+
} else if (orchidCore.isExpression(item)) {
|
|
3457
|
+
result.value = item.result.value;
|
|
3459
3458
|
}
|
|
3460
3459
|
}
|
|
3461
3460
|
}
|
|
@@ -3687,7 +3686,6 @@ class From {
|
|
|
3687
3686
|
* ```ts
|
|
3688
3687
|
* const value = 123;
|
|
3689
3688
|
* db.table.fromSql`value = ${value}`;
|
|
3690
|
-
* db.table.fromSql(db.table.sql`value = ${value}`);
|
|
3691
3689
|
* ```
|
|
3692
3690
|
*
|
|
3693
3691
|
* @param args - SQL expression
|
|
@@ -3757,7 +3755,7 @@ const jsonToSql = (ctx, table, item, values, quotedAs) => {
|
|
|
3757
3755
|
column,
|
|
3758
3756
|
values,
|
|
3759
3757
|
quotedAs
|
|
3760
|
-
)}, ${addValue(values, path)}${(options == null ? void 0 : options.vars) ? `, ${addValue(values, options.vars)}` : ""}${(options == null ? void 0 : options.silent) ? ", true" : ""})`;
|
|
3758
|
+
)}, ${orchidCore.addValue(values, path)}${(options == null ? void 0 : options.vars) ? `, ${orchidCore.addValue(values, options.vars)}` : ""}${(options == null ? void 0 : options.silent) ? ", true" : ""})`;
|
|
3761
3759
|
} else if (json[0] === "set") {
|
|
3762
3760
|
const [, , , column, path, value, options] = json;
|
|
3763
3761
|
return `jsonb_set(${jsonColumnOrMethodToSql(
|
|
@@ -3766,7 +3764,7 @@ const jsonToSql = (ctx, table, item, values, quotedAs) => {
|
|
|
3766
3764
|
column,
|
|
3767
3765
|
values,
|
|
3768
3766
|
quotedAs
|
|
3769
|
-
)}, '{${path.join(", ")}}', ${addValue(values, JSON.stringify(value))}${(options == null ? void 0 : options.createIfMissing) ? ", true" : ""})`;
|
|
3767
|
+
)}, '{${path.join(", ")}}', ${orchidCore.addValue(values, JSON.stringify(value))}${(options == null ? void 0 : options.createIfMissing) ? ", true" : ""})`;
|
|
3770
3768
|
} else if (json[0] === "insert") {
|
|
3771
3769
|
const [, , , column, path, value, options] = json;
|
|
3772
3770
|
return `jsonb_insert(${jsonColumnOrMethodToSql(
|
|
@@ -3775,7 +3773,7 @@ const jsonToSql = (ctx, table, item, values, quotedAs) => {
|
|
|
3775
3773
|
column,
|
|
3776
3774
|
values,
|
|
3777
3775
|
quotedAs
|
|
3778
|
-
)}, '{${path.join(", ")}}', ${addValue(values, JSON.stringify(value))}${(options == null ? void 0 : options.insertAfter) ? ", true" : ""})`;
|
|
3776
|
+
)}, '{${path.join(", ")}}', ${orchidCore.addValue(values, JSON.stringify(value))}${(options == null ? void 0 : options.insertAfter) ? ", true" : ""})`;
|
|
3779
3777
|
} else if (json[0] === "remove") {
|
|
3780
3778
|
const [, , , column, path] = json;
|
|
3781
3779
|
return `${jsonColumnOrMethodToSql(
|
|
@@ -3908,7 +3906,7 @@ const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
|
|
|
3908
3906
|
const coalesce = (ctx, query, sql, quotedAs) => {
|
|
3909
3907
|
const { coalesceValue } = query.q;
|
|
3910
3908
|
if (coalesceValue !== void 0) {
|
|
3911
|
-
const value = orchidCore.isExpression(coalesceValue) ? coalesceValue.toSQL(ctx, quotedAs) : addValue(ctx.values, coalesceValue);
|
|
3909
|
+
const value = orchidCore.isExpression(coalesceValue) ? coalesceValue.toSQL(ctx, quotedAs) : orchidCore.addValue(ctx.values, coalesceValue);
|
|
3912
3910
|
return `COALESCE(${sql}, ${value})`;
|
|
3913
3911
|
}
|
|
3914
3912
|
return sql;
|
|
@@ -3938,7 +3936,7 @@ const addOrder = (ctx, data, column, quotedAs, dir) => {
|
|
|
3938
3936
|
if ((_a = data.sources) == null ? void 0 : _a[column]) {
|
|
3939
3937
|
const search = data.sources[column];
|
|
3940
3938
|
const order = dir || (!search.order || search.order === true ? orchidCore.emptyObject : search.order);
|
|
3941
|
-
return `${order.coverDensity ? "ts_rank_cd" : "ts_rank"}(${order.weights ? `${addValue(ctx.values, `{${order.weights}}`)}, ` : ""}${search.vectorSQL}, "${column}"${order.normalization !== void 0 ? `, ${addValue(ctx.values, order.normalization)}` : ""}) ${order.dir || "DESC"}`;
|
|
3939
|
+
return `${order.coverDensity ? "ts_rank_cd" : "ts_rank"}(${order.weights ? `${orchidCore.addValue(ctx.values, `{${order.weights}}`)}, ` : ""}${search.vectorSQL}, "${column}"${order.normalization !== void 0 ? `, ${orchidCore.addValue(ctx.values, order.normalization)}` : ""}) ${order.dir || "DESC"}`;
|
|
3942
3940
|
}
|
|
3943
3941
|
return `${columnToSql(ctx, data, data.shape, column, quotedAs)} ${dir || "ASC"}`;
|
|
3944
3942
|
};
|
|
@@ -4090,7 +4088,7 @@ const fromToSql = (ctx, data, from, quotedAs) => {
|
|
|
4090
4088
|
};
|
|
4091
4089
|
const getSearchLang = (ctx, data, source, quotedAs) => {
|
|
4092
4090
|
var _a;
|
|
4093
|
-
return (_a = source.langSQL) != null ? _a : source.langSQL = "languageColumn" in source ? columnToSql(ctx, data, data.shape, source.languageColumn, quotedAs) : orchidCore.isRawSQL(source.language) ? source.language.toSQL(ctx) : addValue(ctx.values, source.language || data.language || "english");
|
|
4091
|
+
return (_a = source.langSQL) != null ? _a : source.langSQL = "languageColumn" in source ? columnToSql(ctx, data, data.shape, source.languageColumn, quotedAs) : orchidCore.isRawSQL(source.language) ? source.language.toSQL(ctx) : orchidCore.addValue(ctx.values, source.language || data.language || "english");
|
|
4094
4092
|
};
|
|
4095
4093
|
const getSearchText = (ctx, data, source, quotedAs, forHeadline) => {
|
|
4096
4094
|
let sql = source.textSQL;
|
|
@@ -4116,7 +4114,7 @@ const getSearchText = (ctx, data, source, quotedAs, forHeadline) => {
|
|
|
4116
4114
|
sql = columnToSql(ctx, data, data.shape, source.vector, quotedAs);
|
|
4117
4115
|
} else {
|
|
4118
4116
|
if (typeof source.text === "string") {
|
|
4119
|
-
sql = addValue(ctx.values, source.text);
|
|
4117
|
+
sql = orchidCore.addValue(ctx.values, source.text);
|
|
4120
4118
|
} else {
|
|
4121
4119
|
sql = source.text.toSQL(ctx, quotedAs);
|
|
4122
4120
|
}
|
|
@@ -4132,7 +4130,7 @@ const getTsVector = (ctx, data, lang, source, quotedAs) => {
|
|
|
4132
4130
|
let tsVector = "";
|
|
4133
4131
|
let i = 0;
|
|
4134
4132
|
for (const key in source.in) {
|
|
4135
|
-
tsVector = (tsVector ? `${tsVector} || ` : "") + `setweight(to_tsvector(${lang}, ${text[i++]}), ${addValue(
|
|
4133
|
+
tsVector = (tsVector ? `${tsVector} || ` : "") + `setweight(to_tsvector(${lang}, ${text[i++]}), ${orchidCore.addValue(
|
|
4136
4134
|
ctx.values,
|
|
4137
4135
|
source.in[key]
|
|
4138
4136
|
)})`;
|
|
@@ -4195,7 +4193,7 @@ const pushInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
4195
4193
|
if (orchidCore.isExpression(values)) {
|
|
4196
4194
|
let valuesSql = values.toSQL(ctx, quotedAs);
|
|
4197
4195
|
if (runtimeDefaults) {
|
|
4198
|
-
valuesSql += `, ${runtimeDefaults.map((fn) => addValue(ctx.values, fn())).join(", ")}`;
|
|
4196
|
+
valuesSql += `, ${runtimeDefaults.map((fn) => orchidCore.addValue(ctx.values, fn())).join(", ")}`;
|
|
4199
4197
|
}
|
|
4200
4198
|
ctx.sql.push(`VALUES (${valuesSql})`);
|
|
4201
4199
|
} else {
|
|
@@ -4205,7 +4203,7 @@ const pushInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
4205
4203
|
sql = values.map(
|
|
4206
4204
|
(raw) => (
|
|
4207
4205
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
4208
|
-
`(${raw.toSQL(ctx, quotedAs)}, ${runtimeDefaults.map((fn) => addValue(v, fn())).join(", ")})`
|
|
4206
|
+
`(${raw.toSQL(ctx, quotedAs)}, ${runtimeDefaults.map((fn) => orchidCore.addValue(v, fn())).join(", ")})`
|
|
4209
4207
|
)
|
|
4210
4208
|
).join(", ");
|
|
4211
4209
|
} else {
|
|
@@ -4285,7 +4283,7 @@ const pushInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
4285
4283
|
const arr = [];
|
|
4286
4284
|
for (const key in set) {
|
|
4287
4285
|
arr.push(
|
|
4288
|
-
`"${((_d = shape[key]) == null ? void 0 : _d.data.name) || key}" = ${addValue(
|
|
4286
|
+
`"${((_d = shape[key]) == null ? void 0 : _d.data.name) || key}" = ${orchidCore.addValue(
|
|
4289
4287
|
ctx.values,
|
|
4290
4288
|
set[key]
|
|
4291
4289
|
)}`
|
|
@@ -4319,11 +4317,11 @@ const encodeRow = (ctx, q, QueryClass, row, runtimeDefaults, quotedAs) => {
|
|
|
4319
4317
|
return `(${joinSubQuery(q, value).toSQL(ctx).text})`;
|
|
4320
4318
|
}
|
|
4321
4319
|
}
|
|
4322
|
-
return value === void 0 ? "DEFAULT" : addValue(ctx.values, value);
|
|
4320
|
+
return value === void 0 ? "DEFAULT" : orchidCore.addValue(ctx.values, value);
|
|
4323
4321
|
});
|
|
4324
4322
|
if (runtimeDefaults) {
|
|
4325
4323
|
for (const fn of runtimeDefaults) {
|
|
4326
|
-
arr.push(addValue(ctx.values, fn()));
|
|
4324
|
+
arr.push(orchidCore.addValue(ctx.values, fn()));
|
|
4327
4325
|
}
|
|
4328
4326
|
}
|
|
4329
4327
|
return arr.join(", ");
|
|
@@ -4439,10 +4437,10 @@ const processValue = (ctx, table, QueryClass, key, value, quotedAs) => {
|
|
|
4439
4437
|
} else if (value instanceof QueryClass) {
|
|
4440
4438
|
return `(${joinSubQuery(table, value).toSQL(ctx).text})`;
|
|
4441
4439
|
} else if ("op" in value && "arg" in value) {
|
|
4442
|
-
return `"${table.q.shape[key].data.name || key}" ${value.op} ${addValue(ctx.values, value.arg)}`;
|
|
4440
|
+
return `"${table.q.shape[key].data.name || key}" ${value.op} ${orchidCore.addValue(ctx.values, value.arg)}`;
|
|
4443
4441
|
}
|
|
4444
4442
|
}
|
|
4445
|
-
return addValue(ctx.values, value);
|
|
4443
|
+
return orchidCore.addValue(ctx.values, value);
|
|
4446
4444
|
};
|
|
4447
4445
|
|
|
4448
4446
|
const pushDeleteSql = (ctx, table, query, quotedAs) => {
|
|
@@ -4507,14 +4505,14 @@ const pushTruncateSql = (ctx, table, query) => {
|
|
|
4507
4505
|
const pushColumnInfoSql = (ctx, table, query) => {
|
|
4508
4506
|
var _a;
|
|
4509
4507
|
ctx.sql.push(
|
|
4510
|
-
`SELECT * FROM information_schema.columns WHERE table_name = ${addValue(
|
|
4508
|
+
`SELECT * FROM information_schema.columns WHERE table_name = ${orchidCore.addValue(
|
|
4511
4509
|
ctx.values,
|
|
4512
4510
|
table.table
|
|
4513
4511
|
)} AND table_catalog = current_database() AND table_schema = ${query.schema || "current_schema()"}`
|
|
4514
4512
|
);
|
|
4515
4513
|
if (query.column) {
|
|
4516
4514
|
ctx.sql.push(
|
|
4517
|
-
`AND column_name = ${addValue(
|
|
4515
|
+
`AND column_name = ${orchidCore.addValue(
|
|
4518
4516
|
ctx.values,
|
|
4519
4517
|
((_a = table.q.shape[query.column]) == null ? void 0 : _a.data.name) || query.column
|
|
4520
4518
|
)}`
|
|
@@ -4724,7 +4722,7 @@ const makeSQL = (table, options) => {
|
|
|
4724
4722
|
}
|
|
4725
4723
|
pushLimitSQL(sql, values, query);
|
|
4726
4724
|
if (query.offset) {
|
|
4727
|
-
sql.push(`OFFSET ${addValue(values, query.offset)}`);
|
|
4725
|
+
sql.push(`OFFSET ${orchidCore.addValue(values, query.offset)}`);
|
|
4728
4726
|
}
|
|
4729
4727
|
if (query.for) {
|
|
4730
4728
|
sql.push("FOR", query.for.type);
|
|
@@ -4745,7 +4743,7 @@ function pushLimitSQL(sql, values, q) {
|
|
|
4745
4743
|
if (queryTypeWithLimitOne[q.returnType]) {
|
|
4746
4744
|
sql.push(`LIMIT 1`);
|
|
4747
4745
|
} else if (q.limit) {
|
|
4748
|
-
sql.push(`LIMIT ${addValue(values, q.limit)}`);
|
|
4746
|
+
sql.push(`LIMIT ${orchidCore.addValue(values, q.limit)}`);
|
|
4749
4747
|
}
|
|
4750
4748
|
}
|
|
4751
4749
|
}
|
|
@@ -5359,7 +5357,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
5359
5357
|
for (const key in pairs) {
|
|
5360
5358
|
args.push(
|
|
5361
5359
|
// ::text is needed to bypass "could not determine data type of parameter" postgres error
|
|
5362
|
-
`${addValue(values, key)}::text, ${rawOrColumnToSql(
|
|
5360
|
+
`${orchidCore.addValue(values, key)}::text, ${rawOrColumnToSql(
|
|
5363
5361
|
ctx,
|
|
5364
5362
|
this.q,
|
|
5365
5363
|
pairs[key],
|
|
@@ -5369,7 +5367,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
5369
5367
|
}
|
|
5370
5368
|
return args.join(", ");
|
|
5371
5369
|
} else {
|
|
5372
|
-
return addValue(values, arg.value);
|
|
5370
|
+
return orchidCore.addValue(values, arg.value);
|
|
5373
5371
|
}
|
|
5374
5372
|
}).join(", ")
|
|
5375
5373
|
);
|
|
@@ -5411,7 +5409,8 @@ class FnExpression extends orchidCore.Expression {
|
|
|
5411
5409
|
}
|
|
5412
5410
|
}
|
|
5413
5411
|
function makeFnExpression(self, type, fn, args, options) {
|
|
5414
|
-
const q = self.
|
|
5412
|
+
const q = extendQuery(self, type.operators);
|
|
5413
|
+
q.baseQuery.type = orchidCore.ExpressionTypeMethod.prototype.type;
|
|
5415
5414
|
new FnExpression(
|
|
5416
5415
|
q,
|
|
5417
5416
|
fn,
|
|
@@ -5433,49 +5432,6 @@ nullableInt.parseItem = nullableInt.parseFn = (input) => input === null ? null :
|
|
|
5433
5432
|
const nullableFloat = new RealColumn(defaultSchemaConfig);
|
|
5434
5433
|
nullableFloat.parseItem = nullableFloat.parseFn = (input) => input === null ? null : parseFloat(input);
|
|
5435
5434
|
class AggregateMethods {
|
|
5436
|
-
/**
|
|
5437
|
-
* `fn` allows to call an arbitrary SQL function.
|
|
5438
|
-
*
|
|
5439
|
-
* For example, calling `sqrt` function to get a square root from some numeric column:
|
|
5440
|
-
*
|
|
5441
|
-
* ```ts
|
|
5442
|
-
* const q = await User.select({
|
|
5443
|
-
* // todo: use type callback instead of generic type
|
|
5444
|
-
* sqrt: (q) => q.fn<number>('sqrt', ['numericColumn']),
|
|
5445
|
-
* }).take();
|
|
5446
|
-
*
|
|
5447
|
-
* q.sqrt; // has type `number` just as provided
|
|
5448
|
-
* ```
|
|
5449
|
-
*
|
|
5450
|
-
* If this is an aggregate function, you can specify aggregation options via third parameter.
|
|
5451
|
-
*
|
|
5452
|
-
* Forth parameter is for runtime column type. When specified, allows to chain the function with the column operators:
|
|
5453
|
-
*
|
|
5454
|
-
* ```ts
|
|
5455
|
-
* const q = await User.select({
|
|
5456
|
-
* // chain `sqrt("numericColumn")` with the "greater than 5"
|
|
5457
|
-
* sqrtIsGreaterThan5: (q) => q.fn('sqrt', ['numericColumn'], {}, (t) => t.float()).gt(5),
|
|
5458
|
-
* }).take();
|
|
5459
|
-
*
|
|
5460
|
-
* // Return type is boolean | null
|
|
5461
|
-
* // todo: it should be just boolean if the column is not nullable, but for now it's always nullable
|
|
5462
|
-
* q.sqrtIsGreaterThan5
|
|
5463
|
-
* ```
|
|
5464
|
-
*
|
|
5465
|
-
* @param fn
|
|
5466
|
-
* @param args
|
|
5467
|
-
* @param options
|
|
5468
|
-
* @param type
|
|
5469
|
-
*/
|
|
5470
|
-
fn(fn, args, options, type) {
|
|
5471
|
-
return makeFnExpression(
|
|
5472
|
-
this,
|
|
5473
|
-
(type == null ? void 0 : type(this.columnTypes)) || orchidCore.emptyObject,
|
|
5474
|
-
fn,
|
|
5475
|
-
args,
|
|
5476
|
-
options
|
|
5477
|
-
);
|
|
5478
|
-
}
|
|
5479
5435
|
/**
|
|
5480
5436
|
* Use `exists()` to check if there is at least one record-matching condition.
|
|
5481
5437
|
*
|
|
@@ -5820,7 +5776,7 @@ class AggregateMethods {
|
|
|
5820
5776
|
* // select a column with alias
|
|
5821
5777
|
* nameAlias: 'name',
|
|
5822
5778
|
* // select raw SQL with alias
|
|
5823
|
-
* foo:
|
|
5779
|
+
* foo: sql<string>`"bar" || "baz"`,
|
|
5824
5780
|
* },
|
|
5825
5781
|
* aggregateOptions,
|
|
5826
5782
|
* );
|
|
@@ -5830,7 +5786,7 @@ class AggregateMethods {
|
|
|
5830
5786
|
* object: (q) =>
|
|
5831
5787
|
* q.jsonObjectAgg({
|
|
5832
5788
|
* nameAlias: 'name',
|
|
5833
|
-
* foo:
|
|
5789
|
+
* foo: sql<string>`"bar" || "baz"`,
|
|
5834
5790
|
* }),
|
|
5835
5791
|
* });
|
|
5836
5792
|
* ```
|
|
@@ -6302,7 +6258,7 @@ class Create {
|
|
|
6302
6258
|
*
|
|
6303
6259
|
* await db.table.create({
|
|
6304
6260
|
* // raw SQL
|
|
6305
|
-
* column1:
|
|
6261
|
+
* column1: sql`'John' || ' ' || 'Doe'`,
|
|
6306
6262
|
*
|
|
6307
6263
|
* // query that returns a single value
|
|
6308
6264
|
* // returning multiple values will result in Postgres error
|
|
@@ -6365,7 +6321,7 @@ class Create {
|
|
|
6365
6321
|
* ```ts
|
|
6366
6322
|
* const oneRecord = await db.table.createRaw({
|
|
6367
6323
|
* columns: ['name', 'amount'],
|
|
6368
|
-
* values:
|
|
6324
|
+
* values: sql`'name', random()`,
|
|
6369
6325
|
* });
|
|
6370
6326
|
* ```
|
|
6371
6327
|
*
|
|
@@ -6394,7 +6350,7 @@ class Create {
|
|
|
6394
6350
|
* ```ts
|
|
6395
6351
|
* const manyRecords = await db.table.createManyRaw({
|
|
6396
6352
|
* columns: ['name', 'amount'],
|
|
6397
|
-
* values: [
|
|
6353
|
+
* values: [sql`'one', 2`, sql`'three', 4`],
|
|
6398
6354
|
* });
|
|
6399
6355
|
* ```
|
|
6400
6356
|
*
|
|
@@ -6548,11 +6504,11 @@ class Create {
|
|
|
6548
6504
|
* // raw SQL expression:
|
|
6549
6505
|
* db.table
|
|
6550
6506
|
* .create(data)
|
|
6551
|
-
* .onConfict(
|
|
6507
|
+
* .onConfict(sql`(email) where condition`)
|
|
6552
6508
|
* .merge();
|
|
6553
6509
|
* ```
|
|
6554
6510
|
*
|
|
6555
|
-
* You can use the
|
|
6511
|
+
* You can use the `sql` function exported from your `BaseTable` file in onConflict.
|
|
6556
6512
|
* It can be useful to specify a condition when you have a partial index:
|
|
6557
6513
|
*
|
|
6558
6514
|
* ```ts
|
|
@@ -6563,7 +6519,7 @@ class Create {
|
|
|
6563
6519
|
* active: true,
|
|
6564
6520
|
* })
|
|
6565
6521
|
* // ignore only when having conflicting email and when active is true.
|
|
6566
|
-
* .onConflict(
|
|
6522
|
+
* .onConflict(sql`(email) where active`)
|
|
6567
6523
|
* .ignore();
|
|
6568
6524
|
* ```
|
|
6569
6525
|
*
|
|
@@ -6663,7 +6619,7 @@ class OnConflictQueryBuilder {
|
|
|
6663
6619
|
* db.table
|
|
6664
6620
|
* .create(data)
|
|
6665
6621
|
* .onConflict()
|
|
6666
|
-
* .set(
|
|
6622
|
+
* .set(sql`raw SQL expression`);
|
|
6667
6623
|
*
|
|
6668
6624
|
* // update records only on certain conditions
|
|
6669
6625
|
* db.table
|
|
@@ -6837,16 +6793,20 @@ class For {
|
|
|
6837
6793
|
|
|
6838
6794
|
class QueryGet {
|
|
6839
6795
|
/**
|
|
6840
|
-
* `.get` returns a single value,
|
|
6841
|
-
*
|
|
6796
|
+
* `.get` returns a single value, adds `LIMIT 1` to the query, and accepts a column name or a raw SQL expression.
|
|
6797
|
+
*
|
|
6798
|
+
* `get` throws a `NotFoundError` when not found, and `getOptional` returns `undefined`.
|
|
6842
6799
|
*
|
|
6843
6800
|
* ```ts
|
|
6844
6801
|
* import { NumberColumn } from 'orchid-orm';
|
|
6802
|
+
* import { sql } from './baseTable';
|
|
6845
6803
|
*
|
|
6846
6804
|
* const firstName: string = await db.table.get('name');
|
|
6847
6805
|
*
|
|
6848
|
-
* const rawResult: number = await db.table.get(
|
|
6849
|
-
*
|
|
6806
|
+
* const rawResult: number = await db.table.get(sql((t) => t.integer())`1 + 1`);
|
|
6807
|
+
*
|
|
6808
|
+
* const firstNameOptional: string | undefined = await db.table.getOptional(
|
|
6809
|
+
* 'name',
|
|
6850
6810
|
* );
|
|
6851
6811
|
* ```
|
|
6852
6812
|
*
|
|
@@ -6934,7 +6894,7 @@ class Having {
|
|
|
6934
6894
|
* Provide SQL expression for the `HAVING` SQL statement:
|
|
6935
6895
|
*
|
|
6936
6896
|
* ```ts
|
|
6937
|
-
* db.table.
|
|
6897
|
+
* db.table.havingSql`count(*) >= ${10}`;
|
|
6938
6898
|
* ```
|
|
6939
6899
|
*
|
|
6940
6900
|
* @param args - SQL expression
|
|
@@ -7190,15 +7150,14 @@ class Join {
|
|
|
7190
7150
|
* profile: (q) => q.profile,
|
|
7191
7151
|
* });
|
|
7192
7152
|
*
|
|
7193
|
-
* // select posts with counts of comments, order by comments count
|
|
7153
|
+
* // select posts with counts of comments, filter and order by comments count
|
|
7194
7154
|
* // result type is Array<Post & { commentsCount: number }>
|
|
7195
7155
|
* await db.post
|
|
7196
7156
|
* .select('*', {
|
|
7197
7157
|
* commentsCount: (q) => q.comments.count(),
|
|
7198
7158
|
* })
|
|
7199
|
-
* .
|
|
7200
|
-
*
|
|
7201
|
-
* });
|
|
7159
|
+
* .where({ commentsCount: { gt: 10 } })
|
|
7160
|
+
* .order({ commentsCount: 'DESC' });
|
|
7202
7161
|
*
|
|
7203
7162
|
* // select authors with array of their book titles
|
|
7204
7163
|
* // result type is Array<Author & { books: string[] }>
|
|
@@ -7449,7 +7408,8 @@ class Join {
|
|
|
7449
7408
|
* ```ts
|
|
7450
7409
|
* db.user.join(
|
|
7451
7410
|
* db.message,
|
|
7452
|
-
*
|
|
7411
|
+
* // `sql` can be imported from your `BaseTable` file
|
|
7412
|
+
* sql`lower("message"."text") = lower("user"."name")`,
|
|
7453
7413
|
* );
|
|
7454
7414
|
* ```
|
|
7455
7415
|
*
|
|
@@ -7458,16 +7418,16 @@ class Join {
|
|
|
7458
7418
|
* ```ts
|
|
7459
7419
|
* db.user.join(
|
|
7460
7420
|
* db.message,
|
|
7461
|
-
*
|
|
7462
|
-
*
|
|
7421
|
+
* sql`lower("message"."text")`,
|
|
7422
|
+
* sql`lower("user"."name")`,
|
|
7463
7423
|
* );
|
|
7464
7424
|
*
|
|
7465
7425
|
* // with operator:
|
|
7466
7426
|
* db.user.join(
|
|
7467
7427
|
* db.message,
|
|
7468
|
-
*
|
|
7428
|
+
* sql`lower("message"."text")`,
|
|
7469
7429
|
* '!=',
|
|
7470
|
-
*
|
|
7430
|
+
* sql`lower("user"."name")`,
|
|
7471
7431
|
* );
|
|
7472
7432
|
* ```
|
|
7473
7433
|
*
|
|
@@ -7481,7 +7441,7 @@ class Join {
|
|
|
7481
7441
|
* 'message.userId': 'user.id',
|
|
7482
7442
|
*
|
|
7483
7443
|
* // value can be a raw SQL expression:
|
|
7484
|
-
* text:
|
|
7444
|
+
* text: sql`lower("user"."name")`,
|
|
7485
7445
|
* });
|
|
7486
7446
|
* ```
|
|
7487
7447
|
*
|
|
@@ -8271,7 +8231,7 @@ class With {
|
|
|
8271
8231
|
* id: columnTypes.integer(),
|
|
8272
8232
|
* name: columnTypes.text(3, 100),
|
|
8273
8233
|
* },
|
|
8274
|
-
*
|
|
8234
|
+
* sql`SELECT id, name FROM "someTable"`,
|
|
8275
8235
|
* );
|
|
8276
8236
|
*
|
|
8277
8237
|
* // accepts query:
|
|
@@ -8279,7 +8239,7 @@ class With {
|
|
|
8279
8239
|
*
|
|
8280
8240
|
* // accepts a callback for a query builder:
|
|
8281
8241
|
* db.table.with('alias', (qb) =>
|
|
8282
|
-
* qb.select({ one:
|
|
8242
|
+
* qb.select({ one: sql`1`.type((t) => t.integer()) }),
|
|
8283
8243
|
* );
|
|
8284
8244
|
*
|
|
8285
8245
|
* // All mentioned forms can accept options as a second argument:
|
|
@@ -8331,7 +8291,7 @@ class Union {
|
|
|
8331
8291
|
* SomeTable.select('id', 'name').union(
|
|
8332
8292
|
* [
|
|
8333
8293
|
* OtherTable.select('id', 'name'),
|
|
8334
|
-
*
|
|
8294
|
+
* sql`SELECT id, name FROM "thirdTable"`,
|
|
8335
8295
|
* ],
|
|
8336
8296
|
* true, // optional wrap parameter
|
|
8337
8297
|
* );
|
|
@@ -8526,7 +8486,8 @@ class Where {
|
|
|
8526
8486
|
* },
|
|
8527
8487
|
*
|
|
8528
8488
|
* // where column equals to raw SQL
|
|
8529
|
-
*
|
|
8489
|
+
* // import `sql` from your `BaseTable`
|
|
8490
|
+
* column: sql`sql expression`,
|
|
8530
8491
|
* });
|
|
8531
8492
|
* ```
|
|
8532
8493
|
*
|
|
@@ -8575,12 +8536,7 @@ class Where {
|
|
|
8575
8536
|
* `where` supports raw SQL:
|
|
8576
8537
|
*
|
|
8577
8538
|
* ```ts
|
|
8578
|
-
* db.table.where(
|
|
8579
|
-
*
|
|
8580
|
-
* // or
|
|
8581
|
-
* import { raw } from 'orchid-orm';
|
|
8582
|
-
*
|
|
8583
|
-
* db.table.where(raw`a = b`);
|
|
8539
|
+
* db.table.where(sql`a = b`);
|
|
8584
8540
|
* ```
|
|
8585
8541
|
*
|
|
8586
8542
|
* `where` can accept a callback with a specific query builder containing all "where" methods such as `where`, `orWhere`, `whereNot`, `whereIn`, `whereExists`:
|
|
@@ -8601,7 +8557,7 @@ class Where {
|
|
|
8601
8557
|
* db.table.where(
|
|
8602
8558
|
* { id: 1 },
|
|
8603
8559
|
* db.table.where({ name: 'John' }),
|
|
8604
|
-
*
|
|
8560
|
+
* sql`a = b`,
|
|
8605
8561
|
* );
|
|
8606
8562
|
* ```
|
|
8607
8563
|
*
|
|
@@ -8714,8 +8670,6 @@ class Where {
|
|
|
8714
8670
|
* All column operators can take a value of the same type as the column, a sub-query, or a raw SQL expression:
|
|
8715
8671
|
*
|
|
8716
8672
|
* ```ts
|
|
8717
|
-
* import { sql } from 'orchid-orm';
|
|
8718
|
-
*
|
|
8719
8673
|
* db.table.where({
|
|
8720
8674
|
* numericColumn: {
|
|
8721
8675
|
* // lower than 5
|
|
@@ -8764,7 +8718,7 @@ class Where {
|
|
|
8764
8718
|
* // WHERE "column" IN (SELECT "column" FROM "otherTable")
|
|
8765
8719
|
* in: OtherTable.select('column'),
|
|
8766
8720
|
*
|
|
8767
|
-
* in:
|
|
8721
|
+
* in: sql`('a', 'b')`,
|
|
8768
8722
|
* },
|
|
8769
8723
|
* });
|
|
8770
8724
|
* ```
|
|
@@ -8811,7 +8765,7 @@ class Where {
|
|
|
8811
8765
|
* between: [1, 10],
|
|
8812
8766
|
*
|
|
8813
8767
|
* // sub-query and raw SQL expression
|
|
8814
|
-
* between: [OtherTable.select('column').take(),
|
|
8768
|
+
* between: [OtherTable.select('column').take(), sql`2 + 2`],
|
|
8815
8769
|
* },
|
|
8816
8770
|
* });
|
|
8817
8771
|
* ```
|
|
@@ -8903,15 +8857,7 @@ class Where {
|
|
|
8903
8857
|
* Use a custom SQL expression in `WHERE` statement:
|
|
8904
8858
|
*
|
|
8905
8859
|
* ```ts
|
|
8906
|
-
* db.table.
|
|
8907
|
-
*
|
|
8908
|
-
* // or
|
|
8909
|
-
* db.table.where(db.table.sql`a = b`);
|
|
8910
|
-
*
|
|
8911
|
-
* // or
|
|
8912
|
-
* import { raw } from 'orchid-orm';
|
|
8913
|
-
*
|
|
8914
|
-
* db.table.where(raw`a = b`);
|
|
8860
|
+
* db.table.whereSql`a = b`;
|
|
8915
8861
|
* ```
|
|
8916
8862
|
*
|
|
8917
8863
|
* @param args - SQL expression
|
|
@@ -8944,10 +8890,10 @@ class Where {
|
|
|
8944
8890
|
);
|
|
8945
8891
|
}
|
|
8946
8892
|
/**
|
|
8947
|
-
* `
|
|
8893
|
+
* `whereNotSql` is a version of `whereNot` accepting SQL expression:
|
|
8948
8894
|
*
|
|
8949
8895
|
* ```ts
|
|
8950
|
-
* db.table.
|
|
8896
|
+
* db.table.whereNotSql`sql expression`
|
|
8951
8897
|
* ```
|
|
8952
8898
|
*
|
|
8953
8899
|
* @param args - SQL expression
|
|
@@ -9024,7 +8970,7 @@ class Where {
|
|
|
9024
8970
|
* It supports raw SQL expression:
|
|
9025
8971
|
*
|
|
9026
8972
|
* ```ts
|
|
9027
|
-
* db.table.whereIn(['id', 'name'],
|
|
8973
|
+
* db.table.whereIn(['id', 'name'], sql`((1, 'one'), (2, 'two'))`);
|
|
9028
8974
|
* ```
|
|
9029
8975
|
*/
|
|
9030
8976
|
whereIn(...args) {
|
|
@@ -9338,7 +9284,7 @@ class Update {
|
|
|
9338
9284
|
* column1: 123,
|
|
9339
9285
|
*
|
|
9340
9286
|
* // use raw SQL to update the column
|
|
9341
|
-
* column2:
|
|
9287
|
+
* column2: sql`2 + 2`,
|
|
9342
9288
|
*
|
|
9343
9289
|
* // use query that returns a single value
|
|
9344
9290
|
* // returning multiple values will result in Postgres error
|
|
@@ -9466,7 +9412,7 @@ class Update {
|
|
|
9466
9412
|
* const updatedCount = await db.table.find(1).updateRaw`name = ${value}`;
|
|
9467
9413
|
*
|
|
9468
9414
|
* // or update with `sql` function:
|
|
9469
|
-
* await db.table.find(1).updateRaw(
|
|
9415
|
+
* await db.table.find(1).updateRaw(sql`name = ${value}`);
|
|
9470
9416
|
* ```
|
|
9471
9417
|
* @param args - raw SQL via a template string or by using a `sql` method
|
|
9472
9418
|
*/
|
|
@@ -9732,7 +9678,7 @@ class Headline extends orchidCore.Expression {
|
|
|
9732
9678
|
const { q, source, params } = this;
|
|
9733
9679
|
const lang = getSearchLang(ctx, q, source, quotedAs);
|
|
9734
9680
|
const text = (params == null ? void 0 : params.text) ? params.text instanceof orchidCore.Expression ? params.text.toSQL(ctx, quotedAs) : columnToSql(ctx, q, q.shape, params.text, quotedAs) : getSearchText(ctx, q, source, quotedAs, true);
|
|
9735
|
-
const options = (params == null ? void 0 : params.options) ? `, ${params.options instanceof orchidCore.Expression ? params.options.toSQL(ctx, quotedAs) : addValue(ctx.values, params.options)}` : "";
|
|
9681
|
+
const options = (params == null ? void 0 : params.options) ? `, ${params.options instanceof orchidCore.Expression ? params.options.toSQL(ctx, quotedAs) : orchidCore.addValue(ctx.values, params.options)}` : "";
|
|
9736
9682
|
return `ts_headline(${lang}, ${text}, "${source.as}"${options})`;
|
|
9737
9683
|
}
|
|
9738
9684
|
}
|
|
@@ -10149,7 +10095,7 @@ class QueryUpsertOrCreate {
|
|
|
10149
10095
|
}
|
|
10150
10096
|
}
|
|
10151
10097
|
|
|
10152
|
-
class
|
|
10098
|
+
class SqlMethod {
|
|
10153
10099
|
sql(...args) {
|
|
10154
10100
|
const sql = raw(...args);
|
|
10155
10101
|
sql.columnTypes = this.columnTypes;
|
|
@@ -10390,6 +10336,118 @@ class RefExpression extends orchidCore.Expression {
|
|
|
10390
10336
|
return columnToSql(ctx, this.q, this.q.shape, this.ref, quotedAs);
|
|
10391
10337
|
}
|
|
10392
10338
|
}
|
|
10339
|
+
class ExpressionMethods {
|
|
10340
|
+
/**
|
|
10341
|
+
* `column` references a table column, this can be used in raw SQL or when building a column expression.
|
|
10342
|
+
* Only for referencing a column in the query's table. For referencing joined table's columns, see [ref](#ref).
|
|
10343
|
+
*
|
|
10344
|
+
* ```ts
|
|
10345
|
+
* await db.table.select({
|
|
10346
|
+
* // select `("table"."id" = 1 OR "table"."name" = 'name') AS "one"`,
|
|
10347
|
+
* // returns a boolean
|
|
10348
|
+
* one: (q) =>
|
|
10349
|
+
* q.sql<boolean>`${q.column('id')} = ${1} OR ${q.column('name')} = ${'name'}`,
|
|
10350
|
+
*
|
|
10351
|
+
* // selects the same as above, but by building a query
|
|
10352
|
+
* two: (q) => q.column('id').equals(1).or(q.column('name').equals('name')),
|
|
10353
|
+
* });
|
|
10354
|
+
* ```
|
|
10355
|
+
*
|
|
10356
|
+
* @param name - column name
|
|
10357
|
+
*/
|
|
10358
|
+
column(name) {
|
|
10359
|
+
const column = this.shape[name];
|
|
10360
|
+
return new ColumnRefExpression(
|
|
10361
|
+
column,
|
|
10362
|
+
name
|
|
10363
|
+
);
|
|
10364
|
+
}
|
|
10365
|
+
/**
|
|
10366
|
+
* `ref` is similar to [column](#column), but it also allows to reference a column of joined table,
|
|
10367
|
+
* and other dynamically defined columns.
|
|
10368
|
+
*
|
|
10369
|
+
* ```ts
|
|
10370
|
+
* await db.table.join('otherTable').select({
|
|
10371
|
+
* // select `("otherTable"."id" = 1 OR "otherTable"."name" = 'name') AS "one"`,
|
|
10372
|
+
* // returns a boolean
|
|
10373
|
+
* one: (q) =>
|
|
10374
|
+
* q.sql<boolean>`${q.ref('otherTable.id')} = ${1} OR ${q.ref(
|
|
10375
|
+
* 'otherTable.name',
|
|
10376
|
+
* )} = ${'name'}`,
|
|
10377
|
+
*
|
|
10378
|
+
* // selects the same as above, but by building a query
|
|
10379
|
+
* two: (q) =>
|
|
10380
|
+
* q
|
|
10381
|
+
* .ref('otherTable.id')
|
|
10382
|
+
* .equals(1)
|
|
10383
|
+
* .or(q.ref('otherTable.name').equals('name')),
|
|
10384
|
+
* });
|
|
10385
|
+
* ```
|
|
10386
|
+
*
|
|
10387
|
+
* @param arg - any available column name, such as of a joined table
|
|
10388
|
+
*/
|
|
10389
|
+
ref(arg) {
|
|
10390
|
+
const q = this.clone();
|
|
10391
|
+
const { shape } = q.q;
|
|
10392
|
+
let column;
|
|
10393
|
+
const index = arg.indexOf(".");
|
|
10394
|
+
if (index !== -1) {
|
|
10395
|
+
const table = arg.slice(0, index);
|
|
10396
|
+
const col = arg.slice(index + 1);
|
|
10397
|
+
if (table === (q.q.as || q.table)) {
|
|
10398
|
+
column = shape[col];
|
|
10399
|
+
} else {
|
|
10400
|
+
column = q.q.joinedShapes[table][col];
|
|
10401
|
+
}
|
|
10402
|
+
} else {
|
|
10403
|
+
column = shape[arg];
|
|
10404
|
+
}
|
|
10405
|
+
return new RefExpression(column, q.q, arg);
|
|
10406
|
+
}
|
|
10407
|
+
val(value) {
|
|
10408
|
+
return new orchidCore.ValExpression(value);
|
|
10409
|
+
}
|
|
10410
|
+
/**
|
|
10411
|
+
* `fn` allows to call an arbitrary SQL function.
|
|
10412
|
+
*
|
|
10413
|
+
* For example, calling `sqrt` function to get a square root from some numeric column:
|
|
10414
|
+
*
|
|
10415
|
+
* ```ts
|
|
10416
|
+
* const q = await User.select({
|
|
10417
|
+
* sqrt: (q) => q.fn<number>('sqrt', ['numericColumn']),
|
|
10418
|
+
* }).take();
|
|
10419
|
+
*
|
|
10420
|
+
* q.sqrt; // has type `number` just as provided
|
|
10421
|
+
* ```
|
|
10422
|
+
*
|
|
10423
|
+
* If this is an aggregate function, you can specify aggregation options (see [Aggregate](/guide/aggregate.html)) via third parameter.
|
|
10424
|
+
*
|
|
10425
|
+
* Use `type` method to specify a column type so that its operators such as `lt` and `gt` become available:
|
|
10426
|
+
*
|
|
10427
|
+
* ```ts
|
|
10428
|
+
* const q = await User.select({
|
|
10429
|
+
* // Produces `sqrt("numericColumn") > 5`
|
|
10430
|
+
* sqrtIsGreaterThan5: (q) =>
|
|
10431
|
+
* q
|
|
10432
|
+
* .fn('sqrt', ['numericColumn'])
|
|
10433
|
+
* .type((t) => t.float())
|
|
10434
|
+
* .gt(5),
|
|
10435
|
+
* }).take();
|
|
10436
|
+
*
|
|
10437
|
+
* // Return type is boolean | null
|
|
10438
|
+
* // todo: it should be just boolean if the column is not nullable, but for now it's always nullable
|
|
10439
|
+
* q.sqrtIsGreaterThan5;
|
|
10440
|
+
* ```
|
|
10441
|
+
*
|
|
10442
|
+
* @param fn
|
|
10443
|
+
* @param args
|
|
10444
|
+
* @param options
|
|
10445
|
+
*/
|
|
10446
|
+
fn(fn, args, options) {
|
|
10447
|
+
return makeFnExpression(this, orchidCore.emptyObject, fn, args, options);
|
|
10448
|
+
}
|
|
10449
|
+
}
|
|
10450
|
+
|
|
10393
10451
|
const _queryAll = (q) => {
|
|
10394
10452
|
q.q.returnType = "all";
|
|
10395
10453
|
q.q.all = true;
|
|
@@ -10534,11 +10592,13 @@ class QueryMethods {
|
|
|
10534
10592
|
* db.table.distinct().select('name');
|
|
10535
10593
|
* ```
|
|
10536
10594
|
*
|
|
10537
|
-
* Can accept column names or raw expressions to place it to `DISTINCT ON (...)`:
|
|
10595
|
+
* Can accept column names or raw SQL expressions to place it to `DISTINCT ON (...)`:
|
|
10538
10596
|
*
|
|
10539
10597
|
* ```ts
|
|
10598
|
+
* import { sql } from './baseTable';
|
|
10599
|
+
*
|
|
10540
10600
|
* // Distinct on the name and raw SQL
|
|
10541
|
-
* db.table.distinct('name',
|
|
10601
|
+
* db.table.distinct('name', sql`raw sql`).select('id', 'name');
|
|
10542
10602
|
* ```
|
|
10543
10603
|
*
|
|
10544
10604
|
* @param columns - column names or a raw SQL
|
|
@@ -10698,9 +10758,11 @@ class QueryMethods {
|
|
|
10698
10758
|
* Also, it's possible to group by a selected value:
|
|
10699
10759
|
*
|
|
10700
10760
|
* ```ts
|
|
10761
|
+
* import { sql } from './baseTable';
|
|
10762
|
+
*
|
|
10701
10763
|
* const results = db.product
|
|
10702
10764
|
* .select({
|
|
10703
|
-
* month:
|
|
10765
|
+
* month: sql`extract(month from "createdAt")`.type((t) =>
|
|
10704
10766
|
* // month is returned as string, parse it to int
|
|
10705
10767
|
* t.string().parse(parseInt),
|
|
10706
10768
|
* ),
|
|
@@ -10804,8 +10866,6 @@ class QueryMethods {
|
|
|
10804
10866
|
*
|
|
10805
10867
|
* ```ts
|
|
10806
10868
|
* db.table.orderSql`raw sql`;
|
|
10807
|
-
* // or
|
|
10808
|
-
* db.table.orderSql(db.table.sql`raw sql`);
|
|
10809
10869
|
* ```
|
|
10810
10870
|
*
|
|
10811
10871
|
* @param args - SQL expression
|
|
@@ -11000,71 +11060,32 @@ class QueryMethods {
|
|
|
11000
11060
|
};
|
|
11001
11061
|
}
|
|
11002
11062
|
/**
|
|
11003
|
-
*
|
|
11004
|
-
*
|
|
11005
|
-
*
|
|
11006
|
-
* ```ts
|
|
11007
|
-
* await db.table.select({
|
|
11008
|
-
* // select `("table"."id" = 1 OR "table"."name" = 'name') AS "one"`,
|
|
11009
|
-
* // returns a boolean
|
|
11010
|
-
* one: (q) =>
|
|
11011
|
-
* q.sql<boolean>`${q.column('id')} = ${1} OR ${q.column('name')} = ${'name'}`,
|
|
11012
|
-
*
|
|
11013
|
-
* // selects the same as above, but by building a query
|
|
11014
|
-
* two: (q) => q.column('id').equals(1).or(q.column('name').equals('name')),
|
|
11015
|
-
* });
|
|
11016
|
-
* ```
|
|
11063
|
+
* Narrows a part of the query output type.
|
|
11064
|
+
* Use with caution, type-safety isn't guaranteed with it.
|
|
11065
|
+
* This is similar so using `as` keyword from TypeScript, except that it applies only to a part of the result.
|
|
11017
11066
|
*
|
|
11018
|
-
*
|
|
11019
|
-
*/
|
|
11020
|
-
column(name) {
|
|
11021
|
-
const column = this.shape[name];
|
|
11022
|
-
return new ColumnRefExpression(
|
|
11023
|
-
column,
|
|
11024
|
-
name
|
|
11025
|
-
);
|
|
11026
|
-
}
|
|
11027
|
-
/**
|
|
11028
|
-
* `ref` is similar to [column](#column), but it also allows to reference a column of joined table,
|
|
11029
|
-
* and other dynamically defined columns.
|
|
11067
|
+
* The syntax `()<{ ... }>()` is enforced by internal limitations.
|
|
11030
11068
|
*
|
|
11031
11069
|
* ```ts
|
|
11032
|
-
*
|
|
11033
|
-
* //
|
|
11034
|
-
*
|
|
11035
|
-
*
|
|
11036
|
-
*
|
|
11037
|
-
* 'otherTable.name',
|
|
11038
|
-
* )} = ${'name'}`,
|
|
11070
|
+
* const rows = db.table
|
|
11071
|
+
* // filter out records where the `nullableColumn` is null
|
|
11072
|
+
* .where({ nullableColumn: { not: null } });
|
|
11073
|
+
* // narrows only a specified column, the rest of result is unchanged
|
|
11074
|
+
* .narrowType()<{ nullableColumn: string }>()
|
|
11039
11075
|
*
|
|
11040
|
-
*
|
|
11041
|
-
*
|
|
11042
|
-
* q
|
|
11043
|
-
* .ref('otherTable.id')
|
|
11044
|
-
* .equals(1)
|
|
11045
|
-
* .or(q.ref('otherTable.name').equals('name')),
|
|
11046
|
-
* });
|
|
11047
|
-
* ```
|
|
11076
|
+
* // the column had type `string | null`, now it is `string`
|
|
11077
|
+
* rows[0].nullableColumn
|
|
11048
11078
|
*
|
|
11049
|
-
*
|
|
11079
|
+
* // imagine that table has a enum column kind with variants 'first' | 'second'
|
|
11080
|
+
* // and a boolean `approved`
|
|
11081
|
+
* db.table
|
|
11082
|
+
* .where({ kind: 'first', approved: true })
|
|
11083
|
+
* // after applying such `where`, it's safe to narrow the type to receive the literal values
|
|
11084
|
+
* .narrowType()<{ kind: 'first', approved: true }>();
|
|
11085
|
+
* ```
|
|
11050
11086
|
*/
|
|
11051
|
-
|
|
11052
|
-
|
|
11053
|
-
const { shape } = q.q;
|
|
11054
|
-
let column;
|
|
11055
|
-
const index = arg.indexOf(".");
|
|
11056
|
-
if (index !== -1) {
|
|
11057
|
-
const table = arg.slice(0, index);
|
|
11058
|
-
const col = arg.slice(index + 1);
|
|
11059
|
-
if (table === (q.q.as || q.table)) {
|
|
11060
|
-
column = shape[col];
|
|
11061
|
-
} else {
|
|
11062
|
-
column = q.q.joinedShapes[table][col];
|
|
11063
|
-
}
|
|
11064
|
-
} else {
|
|
11065
|
-
column = shape[arg];
|
|
11066
|
-
}
|
|
11067
|
-
return new RefExpression(column, q.q, arg);
|
|
11087
|
+
narrowType() {
|
|
11088
|
+
return () => this;
|
|
11068
11089
|
}
|
|
11069
11090
|
}
|
|
11070
11091
|
orchidCore.applyMixins(QueryMethods, [
|
|
@@ -11094,10 +11115,11 @@ orchidCore.applyMixins(QueryMethods, [
|
|
|
11094
11115
|
QueryUpsertOrCreate,
|
|
11095
11116
|
QueryGet,
|
|
11096
11117
|
MergeQueryMethods,
|
|
11097
|
-
|
|
11118
|
+
SqlMethod,
|
|
11098
11119
|
TransformMethods,
|
|
11099
11120
|
ScopeMethods,
|
|
11100
|
-
SoftDeleteMethods
|
|
11121
|
+
SoftDeleteMethods,
|
|
11122
|
+
ExpressionMethods
|
|
11101
11123
|
]);
|
|
11102
11124
|
|
|
11103
11125
|
const makeIndex = (columns, first, second) => {
|
|
@@ -11705,6 +11727,7 @@ exports.DomainColumn = DomainColumn;
|
|
|
11705
11727
|
exports.DoublePrecisionColumn = DoublePrecisionColumn;
|
|
11706
11728
|
exports.DynamicRawSQL = DynamicRawSQL;
|
|
11707
11729
|
exports.EnumColumn = EnumColumn;
|
|
11730
|
+
exports.ExpressionMethods = ExpressionMethods;
|
|
11708
11731
|
exports.FnExpression = FnExpression;
|
|
11709
11732
|
exports.For = For;
|
|
11710
11733
|
exports.From = From;
|
|
@@ -11745,7 +11768,6 @@ exports.QueryLog = QueryLog;
|
|
|
11745
11768
|
exports.QueryMethods = QueryMethods;
|
|
11746
11769
|
exports.QueryUpsertOrCreate = QueryUpsertOrCreate;
|
|
11747
11770
|
exports.RawSQL = RawSQL;
|
|
11748
|
-
exports.RawSqlMethods = RawSqlMethods;
|
|
11749
11771
|
exports.RealColumn = RealColumn;
|
|
11750
11772
|
exports.RefExpression = RefExpression;
|
|
11751
11773
|
exports.SearchMethods = SearchMethods;
|
|
@@ -11753,6 +11775,7 @@ exports.Select = Select;
|
|
|
11753
11775
|
exports.SerialColumn = SerialColumn;
|
|
11754
11776
|
exports.SmallIntColumn = SmallIntColumn;
|
|
11755
11777
|
exports.SmallSerialColumn = SmallSerialColumn;
|
|
11778
|
+
exports.SqlMethod = SqlMethod;
|
|
11756
11779
|
exports.StringColumn = StringColumn;
|
|
11757
11780
|
exports.TextBaseColumn = TextBaseColumn;
|
|
11758
11781
|
exports.TextColumn = TextColumn;
|