pqb 0.30.2 → 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 +210 -207
- package/dist/index.js +288 -295
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +260 -268
- 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) {
|
|
@@ -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
|
)})`
|
|
@@ -2213,7 +2209,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2213
2209
|
}
|
|
2214
2210
|
} else {
|
|
2215
2211
|
ands.push(
|
|
2216
|
-
`${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)}`}`
|
|
2217
2213
|
);
|
|
2218
2214
|
}
|
|
2219
2215
|
}
|
|
@@ -2226,7 +2222,7 @@ const pushIn = (ctx, query, ands, quotedAs, arg) => {
|
|
|
2226
2222
|
let value;
|
|
2227
2223
|
if (Array.isArray(arg.values)) {
|
|
2228
2224
|
value = `${arg.values.map(
|
|
2229
|
-
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(", ")}`
|
|
2230
2226
|
).join(", ")}`;
|
|
2231
2227
|
value = `(${value})`;
|
|
2232
2228
|
} else if (orchidCore.isExpression(arg.values)) {
|
|
@@ -3690,7 +3686,6 @@ class From {
|
|
|
3690
3686
|
* ```ts
|
|
3691
3687
|
* const value = 123;
|
|
3692
3688
|
* db.table.fromSql`value = ${value}`;
|
|
3693
|
-
* db.table.fromSql(db.table.sql`value = ${value}`);
|
|
3694
3689
|
* ```
|
|
3695
3690
|
*
|
|
3696
3691
|
* @param args - SQL expression
|
|
@@ -3760,7 +3755,7 @@ const jsonToSql = (ctx, table, item, values, quotedAs) => {
|
|
|
3760
3755
|
column,
|
|
3761
3756
|
values,
|
|
3762
3757
|
quotedAs
|
|
3763
|
-
)}, ${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" : ""})`;
|
|
3764
3759
|
} else if (json[0] === "set") {
|
|
3765
3760
|
const [, , , column, path, value, options] = json;
|
|
3766
3761
|
return `jsonb_set(${jsonColumnOrMethodToSql(
|
|
@@ -3769,7 +3764,7 @@ const jsonToSql = (ctx, table, item, values, quotedAs) => {
|
|
|
3769
3764
|
column,
|
|
3770
3765
|
values,
|
|
3771
3766
|
quotedAs
|
|
3772
|
-
)}, '{${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" : ""})`;
|
|
3773
3768
|
} else if (json[0] === "insert") {
|
|
3774
3769
|
const [, , , column, path, value, options] = json;
|
|
3775
3770
|
return `jsonb_insert(${jsonColumnOrMethodToSql(
|
|
@@ -3778,7 +3773,7 @@ const jsonToSql = (ctx, table, item, values, quotedAs) => {
|
|
|
3778
3773
|
column,
|
|
3779
3774
|
values,
|
|
3780
3775
|
quotedAs
|
|
3781
|
-
)}, '{${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" : ""})`;
|
|
3782
3777
|
} else if (json[0] === "remove") {
|
|
3783
3778
|
const [, , , column, path] = json;
|
|
3784
3779
|
return `${jsonColumnOrMethodToSql(
|
|
@@ -3911,7 +3906,7 @@ const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
|
|
|
3911
3906
|
const coalesce = (ctx, query, sql, quotedAs) => {
|
|
3912
3907
|
const { coalesceValue } = query.q;
|
|
3913
3908
|
if (coalesceValue !== void 0) {
|
|
3914
|
-
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);
|
|
3915
3910
|
return `COALESCE(${sql}, ${value})`;
|
|
3916
3911
|
}
|
|
3917
3912
|
return sql;
|
|
@@ -3941,7 +3936,7 @@ const addOrder = (ctx, data, column, quotedAs, dir) => {
|
|
|
3941
3936
|
if ((_a = data.sources) == null ? void 0 : _a[column]) {
|
|
3942
3937
|
const search = data.sources[column];
|
|
3943
3938
|
const order = dir || (!search.order || search.order === true ? orchidCore.emptyObject : search.order);
|
|
3944
|
-
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"}`;
|
|
3945
3940
|
}
|
|
3946
3941
|
return `${columnToSql(ctx, data, data.shape, column, quotedAs)} ${dir || "ASC"}`;
|
|
3947
3942
|
};
|
|
@@ -4093,7 +4088,7 @@ const fromToSql = (ctx, data, from, quotedAs) => {
|
|
|
4093
4088
|
};
|
|
4094
4089
|
const getSearchLang = (ctx, data, source, quotedAs) => {
|
|
4095
4090
|
var _a;
|
|
4096
|
-
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");
|
|
4097
4092
|
};
|
|
4098
4093
|
const getSearchText = (ctx, data, source, quotedAs, forHeadline) => {
|
|
4099
4094
|
let sql = source.textSQL;
|
|
@@ -4119,7 +4114,7 @@ const getSearchText = (ctx, data, source, quotedAs, forHeadline) => {
|
|
|
4119
4114
|
sql = columnToSql(ctx, data, data.shape, source.vector, quotedAs);
|
|
4120
4115
|
} else {
|
|
4121
4116
|
if (typeof source.text === "string") {
|
|
4122
|
-
sql = addValue(ctx.values, source.text);
|
|
4117
|
+
sql = orchidCore.addValue(ctx.values, source.text);
|
|
4123
4118
|
} else {
|
|
4124
4119
|
sql = source.text.toSQL(ctx, quotedAs);
|
|
4125
4120
|
}
|
|
@@ -4135,7 +4130,7 @@ const getTsVector = (ctx, data, lang, source, quotedAs) => {
|
|
|
4135
4130
|
let tsVector = "";
|
|
4136
4131
|
let i = 0;
|
|
4137
4132
|
for (const key in source.in) {
|
|
4138
|
-
tsVector = (tsVector ? `${tsVector} || ` : "") + `setweight(to_tsvector(${lang}, ${text[i++]}), ${addValue(
|
|
4133
|
+
tsVector = (tsVector ? `${tsVector} || ` : "") + `setweight(to_tsvector(${lang}, ${text[i++]}), ${orchidCore.addValue(
|
|
4139
4134
|
ctx.values,
|
|
4140
4135
|
source.in[key]
|
|
4141
4136
|
)})`;
|
|
@@ -4198,7 +4193,7 @@ const pushInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
4198
4193
|
if (orchidCore.isExpression(values)) {
|
|
4199
4194
|
let valuesSql = values.toSQL(ctx, quotedAs);
|
|
4200
4195
|
if (runtimeDefaults) {
|
|
4201
|
-
valuesSql += `, ${runtimeDefaults.map((fn) => addValue(ctx.values, fn())).join(", ")}`;
|
|
4196
|
+
valuesSql += `, ${runtimeDefaults.map((fn) => orchidCore.addValue(ctx.values, fn())).join(", ")}`;
|
|
4202
4197
|
}
|
|
4203
4198
|
ctx.sql.push(`VALUES (${valuesSql})`);
|
|
4204
4199
|
} else {
|
|
@@ -4208,7 +4203,7 @@ const pushInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
4208
4203
|
sql = values.map(
|
|
4209
4204
|
(raw) => (
|
|
4210
4205
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
4211
|
-
`(${raw.toSQL(ctx, quotedAs)}, ${runtimeDefaults.map((fn) => addValue(v, fn())).join(", ")})`
|
|
4206
|
+
`(${raw.toSQL(ctx, quotedAs)}, ${runtimeDefaults.map((fn) => orchidCore.addValue(v, fn())).join(", ")})`
|
|
4212
4207
|
)
|
|
4213
4208
|
).join(", ");
|
|
4214
4209
|
} else {
|
|
@@ -4288,7 +4283,7 @@ const pushInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
4288
4283
|
const arr = [];
|
|
4289
4284
|
for (const key in set) {
|
|
4290
4285
|
arr.push(
|
|
4291
|
-
`"${((_d = shape[key]) == null ? void 0 : _d.data.name) || key}" = ${addValue(
|
|
4286
|
+
`"${((_d = shape[key]) == null ? void 0 : _d.data.name) || key}" = ${orchidCore.addValue(
|
|
4292
4287
|
ctx.values,
|
|
4293
4288
|
set[key]
|
|
4294
4289
|
)}`
|
|
@@ -4322,11 +4317,11 @@ const encodeRow = (ctx, q, QueryClass, row, runtimeDefaults, quotedAs) => {
|
|
|
4322
4317
|
return `(${joinSubQuery(q, value).toSQL(ctx).text})`;
|
|
4323
4318
|
}
|
|
4324
4319
|
}
|
|
4325
|
-
return value === void 0 ? "DEFAULT" : addValue(ctx.values, value);
|
|
4320
|
+
return value === void 0 ? "DEFAULT" : orchidCore.addValue(ctx.values, value);
|
|
4326
4321
|
});
|
|
4327
4322
|
if (runtimeDefaults) {
|
|
4328
4323
|
for (const fn of runtimeDefaults) {
|
|
4329
|
-
arr.push(addValue(ctx.values, fn()));
|
|
4324
|
+
arr.push(orchidCore.addValue(ctx.values, fn()));
|
|
4330
4325
|
}
|
|
4331
4326
|
}
|
|
4332
4327
|
return arr.join(", ");
|
|
@@ -4442,10 +4437,10 @@ const processValue = (ctx, table, QueryClass, key, value, quotedAs) => {
|
|
|
4442
4437
|
} else if (value instanceof QueryClass) {
|
|
4443
4438
|
return `(${joinSubQuery(table, value).toSQL(ctx).text})`;
|
|
4444
4439
|
} else if ("op" in value && "arg" in value) {
|
|
4445
|
-
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)}`;
|
|
4446
4441
|
}
|
|
4447
4442
|
}
|
|
4448
|
-
return addValue(ctx.values, value);
|
|
4443
|
+
return orchidCore.addValue(ctx.values, value);
|
|
4449
4444
|
};
|
|
4450
4445
|
|
|
4451
4446
|
const pushDeleteSql = (ctx, table, query, quotedAs) => {
|
|
@@ -4510,14 +4505,14 @@ const pushTruncateSql = (ctx, table, query) => {
|
|
|
4510
4505
|
const pushColumnInfoSql = (ctx, table, query) => {
|
|
4511
4506
|
var _a;
|
|
4512
4507
|
ctx.sql.push(
|
|
4513
|
-
`SELECT * FROM information_schema.columns WHERE table_name = ${addValue(
|
|
4508
|
+
`SELECT * FROM information_schema.columns WHERE table_name = ${orchidCore.addValue(
|
|
4514
4509
|
ctx.values,
|
|
4515
4510
|
table.table
|
|
4516
4511
|
)} AND table_catalog = current_database() AND table_schema = ${query.schema || "current_schema()"}`
|
|
4517
4512
|
);
|
|
4518
4513
|
if (query.column) {
|
|
4519
4514
|
ctx.sql.push(
|
|
4520
|
-
`AND column_name = ${addValue(
|
|
4515
|
+
`AND column_name = ${orchidCore.addValue(
|
|
4521
4516
|
ctx.values,
|
|
4522
4517
|
((_a = table.q.shape[query.column]) == null ? void 0 : _a.data.name) || query.column
|
|
4523
4518
|
)}`
|
|
@@ -4727,7 +4722,7 @@ const makeSQL = (table, options) => {
|
|
|
4727
4722
|
}
|
|
4728
4723
|
pushLimitSQL(sql, values, query);
|
|
4729
4724
|
if (query.offset) {
|
|
4730
|
-
sql.push(`OFFSET ${addValue(values, query.offset)}`);
|
|
4725
|
+
sql.push(`OFFSET ${orchidCore.addValue(values, query.offset)}`);
|
|
4731
4726
|
}
|
|
4732
4727
|
if (query.for) {
|
|
4733
4728
|
sql.push("FOR", query.for.type);
|
|
@@ -4748,7 +4743,7 @@ function pushLimitSQL(sql, values, q) {
|
|
|
4748
4743
|
if (queryTypeWithLimitOne[q.returnType]) {
|
|
4749
4744
|
sql.push(`LIMIT 1`);
|
|
4750
4745
|
} else if (q.limit) {
|
|
4751
|
-
sql.push(`LIMIT ${addValue(values, q.limit)}`);
|
|
4746
|
+
sql.push(`LIMIT ${orchidCore.addValue(values, q.limit)}`);
|
|
4752
4747
|
}
|
|
4753
4748
|
}
|
|
4754
4749
|
}
|
|
@@ -5362,7 +5357,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
5362
5357
|
for (const key in pairs) {
|
|
5363
5358
|
args.push(
|
|
5364
5359
|
// ::text is needed to bypass "could not determine data type of parameter" postgres error
|
|
5365
|
-
`${addValue(values, key)}::text, ${rawOrColumnToSql(
|
|
5360
|
+
`${orchidCore.addValue(values, key)}::text, ${rawOrColumnToSql(
|
|
5366
5361
|
ctx,
|
|
5367
5362
|
this.q,
|
|
5368
5363
|
pairs[key],
|
|
@@ -5372,7 +5367,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
5372
5367
|
}
|
|
5373
5368
|
return args.join(", ");
|
|
5374
5369
|
} else {
|
|
5375
|
-
return addValue(values, arg.value);
|
|
5370
|
+
return orchidCore.addValue(values, arg.value);
|
|
5376
5371
|
}
|
|
5377
5372
|
}).join(", ")
|
|
5378
5373
|
);
|
|
@@ -5414,7 +5409,8 @@ class FnExpression extends orchidCore.Expression {
|
|
|
5414
5409
|
}
|
|
5415
5410
|
}
|
|
5416
5411
|
function makeFnExpression(self, type, fn, args, options) {
|
|
5417
|
-
const q = self.
|
|
5412
|
+
const q = extendQuery(self, type.operators);
|
|
5413
|
+
q.baseQuery.type = orchidCore.ExpressionTypeMethod.prototype.type;
|
|
5418
5414
|
new FnExpression(
|
|
5419
5415
|
q,
|
|
5420
5416
|
fn,
|
|
@@ -5436,49 +5432,6 @@ nullableInt.parseItem = nullableInt.parseFn = (input) => input === null ? null :
|
|
|
5436
5432
|
const nullableFloat = new RealColumn(defaultSchemaConfig);
|
|
5437
5433
|
nullableFloat.parseItem = nullableFloat.parseFn = (input) => input === null ? null : parseFloat(input);
|
|
5438
5434
|
class AggregateMethods {
|
|
5439
|
-
/**
|
|
5440
|
-
* `fn` allows to call an arbitrary SQL function.
|
|
5441
|
-
*
|
|
5442
|
-
* For example, calling `sqrt` function to get a square root from some numeric column:
|
|
5443
|
-
*
|
|
5444
|
-
* ```ts
|
|
5445
|
-
* const q = await User.select({
|
|
5446
|
-
* // todo: use type callback instead of generic type
|
|
5447
|
-
* sqrt: (q) => q.fn<number>('sqrt', ['numericColumn']),
|
|
5448
|
-
* }).take();
|
|
5449
|
-
*
|
|
5450
|
-
* q.sqrt; // has type `number` just as provided
|
|
5451
|
-
* ```
|
|
5452
|
-
*
|
|
5453
|
-
* If this is an aggregate function, you can specify aggregation options via third parameter.
|
|
5454
|
-
*
|
|
5455
|
-
* Forth parameter is for runtime column type. When specified, allows to chain the function with the column operators:
|
|
5456
|
-
*
|
|
5457
|
-
* ```ts
|
|
5458
|
-
* const q = await User.select({
|
|
5459
|
-
* // chain `sqrt("numericColumn")` with the "greater than 5"
|
|
5460
|
-
* sqrtIsGreaterThan5: (q) => q.fn('sqrt', ['numericColumn'], {}, (t) => t.float()).gt(5),
|
|
5461
|
-
* }).take();
|
|
5462
|
-
*
|
|
5463
|
-
* // Return type is boolean | null
|
|
5464
|
-
* // todo: it should be just boolean if the column is not nullable, but for now it's always nullable
|
|
5465
|
-
* q.sqrtIsGreaterThan5
|
|
5466
|
-
* ```
|
|
5467
|
-
*
|
|
5468
|
-
* @param fn
|
|
5469
|
-
* @param args
|
|
5470
|
-
* @param options
|
|
5471
|
-
* @param type
|
|
5472
|
-
*/
|
|
5473
|
-
fn(fn, args, options, type) {
|
|
5474
|
-
return makeFnExpression(
|
|
5475
|
-
this,
|
|
5476
|
-
(type == null ? void 0 : type(this.columnTypes)) || orchidCore.emptyObject,
|
|
5477
|
-
fn,
|
|
5478
|
-
args,
|
|
5479
|
-
options
|
|
5480
|
-
);
|
|
5481
|
-
}
|
|
5482
5435
|
/**
|
|
5483
5436
|
* Use `exists()` to check if there is at least one record-matching condition.
|
|
5484
5437
|
*
|
|
@@ -5823,7 +5776,7 @@ class AggregateMethods {
|
|
|
5823
5776
|
* // select a column with alias
|
|
5824
5777
|
* nameAlias: 'name',
|
|
5825
5778
|
* // select raw SQL with alias
|
|
5826
|
-
* foo:
|
|
5779
|
+
* foo: sql<string>`"bar" || "baz"`,
|
|
5827
5780
|
* },
|
|
5828
5781
|
* aggregateOptions,
|
|
5829
5782
|
* );
|
|
@@ -5833,7 +5786,7 @@ class AggregateMethods {
|
|
|
5833
5786
|
* object: (q) =>
|
|
5834
5787
|
* q.jsonObjectAgg({
|
|
5835
5788
|
* nameAlias: 'name',
|
|
5836
|
-
* foo:
|
|
5789
|
+
* foo: sql<string>`"bar" || "baz"`,
|
|
5837
5790
|
* }),
|
|
5838
5791
|
* });
|
|
5839
5792
|
* ```
|
|
@@ -6305,7 +6258,7 @@ class Create {
|
|
|
6305
6258
|
*
|
|
6306
6259
|
* await db.table.create({
|
|
6307
6260
|
* // raw SQL
|
|
6308
|
-
* column1:
|
|
6261
|
+
* column1: sql`'John' || ' ' || 'Doe'`,
|
|
6309
6262
|
*
|
|
6310
6263
|
* // query that returns a single value
|
|
6311
6264
|
* // returning multiple values will result in Postgres error
|
|
@@ -6368,7 +6321,7 @@ class Create {
|
|
|
6368
6321
|
* ```ts
|
|
6369
6322
|
* const oneRecord = await db.table.createRaw({
|
|
6370
6323
|
* columns: ['name', 'amount'],
|
|
6371
|
-
* values:
|
|
6324
|
+
* values: sql`'name', random()`,
|
|
6372
6325
|
* });
|
|
6373
6326
|
* ```
|
|
6374
6327
|
*
|
|
@@ -6397,7 +6350,7 @@ class Create {
|
|
|
6397
6350
|
* ```ts
|
|
6398
6351
|
* const manyRecords = await db.table.createManyRaw({
|
|
6399
6352
|
* columns: ['name', 'amount'],
|
|
6400
|
-
* values: [
|
|
6353
|
+
* values: [sql`'one', 2`, sql`'three', 4`],
|
|
6401
6354
|
* });
|
|
6402
6355
|
* ```
|
|
6403
6356
|
*
|
|
@@ -6551,11 +6504,11 @@ class Create {
|
|
|
6551
6504
|
* // raw SQL expression:
|
|
6552
6505
|
* db.table
|
|
6553
6506
|
* .create(data)
|
|
6554
|
-
* .onConfict(
|
|
6507
|
+
* .onConfict(sql`(email) where condition`)
|
|
6555
6508
|
* .merge();
|
|
6556
6509
|
* ```
|
|
6557
6510
|
*
|
|
6558
|
-
* You can use the
|
|
6511
|
+
* You can use the `sql` function exported from your `BaseTable` file in onConflict.
|
|
6559
6512
|
* It can be useful to specify a condition when you have a partial index:
|
|
6560
6513
|
*
|
|
6561
6514
|
* ```ts
|
|
@@ -6566,7 +6519,7 @@ class Create {
|
|
|
6566
6519
|
* active: true,
|
|
6567
6520
|
* })
|
|
6568
6521
|
* // ignore only when having conflicting email and when active is true.
|
|
6569
|
-
* .onConflict(
|
|
6522
|
+
* .onConflict(sql`(email) where active`)
|
|
6570
6523
|
* .ignore();
|
|
6571
6524
|
* ```
|
|
6572
6525
|
*
|
|
@@ -6666,7 +6619,7 @@ class OnConflictQueryBuilder {
|
|
|
6666
6619
|
* db.table
|
|
6667
6620
|
* .create(data)
|
|
6668
6621
|
* .onConflict()
|
|
6669
|
-
* .set(
|
|
6622
|
+
* .set(sql`raw SQL expression`);
|
|
6670
6623
|
*
|
|
6671
6624
|
* // update records only on certain conditions
|
|
6672
6625
|
* db.table
|
|
@@ -6840,16 +6793,20 @@ class For {
|
|
|
6840
6793
|
|
|
6841
6794
|
class QueryGet {
|
|
6842
6795
|
/**
|
|
6843
|
-
* `.get` returns a single value,
|
|
6844
|
-
*
|
|
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`.
|
|
6845
6799
|
*
|
|
6846
6800
|
* ```ts
|
|
6847
6801
|
* import { NumberColumn } from 'orchid-orm';
|
|
6802
|
+
* import { sql } from './baseTable';
|
|
6848
6803
|
*
|
|
6849
6804
|
* const firstName: string = await db.table.get('name');
|
|
6850
6805
|
*
|
|
6851
|
-
* const rawResult: number = await db.table.get(
|
|
6852
|
-
*
|
|
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',
|
|
6853
6810
|
* );
|
|
6854
6811
|
* ```
|
|
6855
6812
|
*
|
|
@@ -6937,7 +6894,7 @@ class Having {
|
|
|
6937
6894
|
* Provide SQL expression for the `HAVING` SQL statement:
|
|
6938
6895
|
*
|
|
6939
6896
|
* ```ts
|
|
6940
|
-
* db.table.
|
|
6897
|
+
* db.table.havingSql`count(*) >= ${10}`;
|
|
6941
6898
|
* ```
|
|
6942
6899
|
*
|
|
6943
6900
|
* @param args - SQL expression
|
|
@@ -7451,7 +7408,8 @@ class Join {
|
|
|
7451
7408
|
* ```ts
|
|
7452
7409
|
* db.user.join(
|
|
7453
7410
|
* db.message,
|
|
7454
|
-
*
|
|
7411
|
+
* // `sql` can be imported from your `BaseTable` file
|
|
7412
|
+
* sql`lower("message"."text") = lower("user"."name")`,
|
|
7455
7413
|
* );
|
|
7456
7414
|
* ```
|
|
7457
7415
|
*
|
|
@@ -7460,16 +7418,16 @@ class Join {
|
|
|
7460
7418
|
* ```ts
|
|
7461
7419
|
* db.user.join(
|
|
7462
7420
|
* db.message,
|
|
7463
|
-
*
|
|
7464
|
-
*
|
|
7421
|
+
* sql`lower("message"."text")`,
|
|
7422
|
+
* sql`lower("user"."name")`,
|
|
7465
7423
|
* );
|
|
7466
7424
|
*
|
|
7467
7425
|
* // with operator:
|
|
7468
7426
|
* db.user.join(
|
|
7469
7427
|
* db.message,
|
|
7470
|
-
*
|
|
7428
|
+
* sql`lower("message"."text")`,
|
|
7471
7429
|
* '!=',
|
|
7472
|
-
*
|
|
7430
|
+
* sql`lower("user"."name")`,
|
|
7473
7431
|
* );
|
|
7474
7432
|
* ```
|
|
7475
7433
|
*
|
|
@@ -7483,7 +7441,7 @@ class Join {
|
|
|
7483
7441
|
* 'message.userId': 'user.id',
|
|
7484
7442
|
*
|
|
7485
7443
|
* // value can be a raw SQL expression:
|
|
7486
|
-
* text:
|
|
7444
|
+
* text: sql`lower("user"."name")`,
|
|
7487
7445
|
* });
|
|
7488
7446
|
* ```
|
|
7489
7447
|
*
|
|
@@ -8273,7 +8231,7 @@ class With {
|
|
|
8273
8231
|
* id: columnTypes.integer(),
|
|
8274
8232
|
* name: columnTypes.text(3, 100),
|
|
8275
8233
|
* },
|
|
8276
|
-
*
|
|
8234
|
+
* sql`SELECT id, name FROM "someTable"`,
|
|
8277
8235
|
* );
|
|
8278
8236
|
*
|
|
8279
8237
|
* // accepts query:
|
|
@@ -8281,7 +8239,7 @@ class With {
|
|
|
8281
8239
|
*
|
|
8282
8240
|
* // accepts a callback for a query builder:
|
|
8283
8241
|
* db.table.with('alias', (qb) =>
|
|
8284
|
-
* qb.select({ one:
|
|
8242
|
+
* qb.select({ one: sql`1`.type((t) => t.integer()) }),
|
|
8285
8243
|
* );
|
|
8286
8244
|
*
|
|
8287
8245
|
* // All mentioned forms can accept options as a second argument:
|
|
@@ -8333,7 +8291,7 @@ class Union {
|
|
|
8333
8291
|
* SomeTable.select('id', 'name').union(
|
|
8334
8292
|
* [
|
|
8335
8293
|
* OtherTable.select('id', 'name'),
|
|
8336
|
-
*
|
|
8294
|
+
* sql`SELECT id, name FROM "thirdTable"`,
|
|
8337
8295
|
* ],
|
|
8338
8296
|
* true, // optional wrap parameter
|
|
8339
8297
|
* );
|
|
@@ -8528,7 +8486,8 @@ class Where {
|
|
|
8528
8486
|
* },
|
|
8529
8487
|
*
|
|
8530
8488
|
* // where column equals to raw SQL
|
|
8531
|
-
*
|
|
8489
|
+
* // import `sql` from your `BaseTable`
|
|
8490
|
+
* column: sql`sql expression`,
|
|
8532
8491
|
* });
|
|
8533
8492
|
* ```
|
|
8534
8493
|
*
|
|
@@ -8577,12 +8536,7 @@ class Where {
|
|
|
8577
8536
|
* `where` supports raw SQL:
|
|
8578
8537
|
*
|
|
8579
8538
|
* ```ts
|
|
8580
|
-
* db.table.where(
|
|
8581
|
-
*
|
|
8582
|
-
* // or
|
|
8583
|
-
* import { raw } from 'orchid-orm';
|
|
8584
|
-
*
|
|
8585
|
-
* db.table.where(raw`a = b`);
|
|
8539
|
+
* db.table.where(sql`a = b`);
|
|
8586
8540
|
* ```
|
|
8587
8541
|
*
|
|
8588
8542
|
* `where` can accept a callback with a specific query builder containing all "where" methods such as `where`, `orWhere`, `whereNot`, `whereIn`, `whereExists`:
|
|
@@ -8603,7 +8557,7 @@ class Where {
|
|
|
8603
8557
|
* db.table.where(
|
|
8604
8558
|
* { id: 1 },
|
|
8605
8559
|
* db.table.where({ name: 'John' }),
|
|
8606
|
-
*
|
|
8560
|
+
* sql`a = b`,
|
|
8607
8561
|
* );
|
|
8608
8562
|
* ```
|
|
8609
8563
|
*
|
|
@@ -8716,8 +8670,6 @@ class Where {
|
|
|
8716
8670
|
* All column operators can take a value of the same type as the column, a sub-query, or a raw SQL expression:
|
|
8717
8671
|
*
|
|
8718
8672
|
* ```ts
|
|
8719
|
-
* import { sql } from 'orchid-orm';
|
|
8720
|
-
*
|
|
8721
8673
|
* db.table.where({
|
|
8722
8674
|
* numericColumn: {
|
|
8723
8675
|
* // lower than 5
|
|
@@ -8766,7 +8718,7 @@ class Where {
|
|
|
8766
8718
|
* // WHERE "column" IN (SELECT "column" FROM "otherTable")
|
|
8767
8719
|
* in: OtherTable.select('column'),
|
|
8768
8720
|
*
|
|
8769
|
-
* in:
|
|
8721
|
+
* in: sql`('a', 'b')`,
|
|
8770
8722
|
* },
|
|
8771
8723
|
* });
|
|
8772
8724
|
* ```
|
|
@@ -8813,7 +8765,7 @@ class Where {
|
|
|
8813
8765
|
* between: [1, 10],
|
|
8814
8766
|
*
|
|
8815
8767
|
* // sub-query and raw SQL expression
|
|
8816
|
-
* between: [OtherTable.select('column').take(),
|
|
8768
|
+
* between: [OtherTable.select('column').take(), sql`2 + 2`],
|
|
8817
8769
|
* },
|
|
8818
8770
|
* });
|
|
8819
8771
|
* ```
|
|
@@ -8905,15 +8857,7 @@ class Where {
|
|
|
8905
8857
|
* Use a custom SQL expression in `WHERE` statement:
|
|
8906
8858
|
*
|
|
8907
8859
|
* ```ts
|
|
8908
|
-
* db.table.
|
|
8909
|
-
*
|
|
8910
|
-
* // or
|
|
8911
|
-
* db.table.where(db.table.sql`a = b`);
|
|
8912
|
-
*
|
|
8913
|
-
* // or
|
|
8914
|
-
* import { raw } from 'orchid-orm';
|
|
8915
|
-
*
|
|
8916
|
-
* db.table.where(raw`a = b`);
|
|
8860
|
+
* db.table.whereSql`a = b`;
|
|
8917
8861
|
* ```
|
|
8918
8862
|
*
|
|
8919
8863
|
* @param args - SQL expression
|
|
@@ -8946,10 +8890,10 @@ class Where {
|
|
|
8946
8890
|
);
|
|
8947
8891
|
}
|
|
8948
8892
|
/**
|
|
8949
|
-
* `
|
|
8893
|
+
* `whereNotSql` is a version of `whereNot` accepting SQL expression:
|
|
8950
8894
|
*
|
|
8951
8895
|
* ```ts
|
|
8952
|
-
* db.table.
|
|
8896
|
+
* db.table.whereNotSql`sql expression`
|
|
8953
8897
|
* ```
|
|
8954
8898
|
*
|
|
8955
8899
|
* @param args - SQL expression
|
|
@@ -9026,7 +8970,7 @@ class Where {
|
|
|
9026
8970
|
* It supports raw SQL expression:
|
|
9027
8971
|
*
|
|
9028
8972
|
* ```ts
|
|
9029
|
-
* db.table.whereIn(['id', 'name'],
|
|
8973
|
+
* db.table.whereIn(['id', 'name'], sql`((1, 'one'), (2, 'two'))`);
|
|
9030
8974
|
* ```
|
|
9031
8975
|
*/
|
|
9032
8976
|
whereIn(...args) {
|
|
@@ -9340,7 +9284,7 @@ class Update {
|
|
|
9340
9284
|
* column1: 123,
|
|
9341
9285
|
*
|
|
9342
9286
|
* // use raw SQL to update the column
|
|
9343
|
-
* column2:
|
|
9287
|
+
* column2: sql`2 + 2`,
|
|
9344
9288
|
*
|
|
9345
9289
|
* // use query that returns a single value
|
|
9346
9290
|
* // returning multiple values will result in Postgres error
|
|
@@ -9468,7 +9412,7 @@ class Update {
|
|
|
9468
9412
|
* const updatedCount = await db.table.find(1).updateRaw`name = ${value}`;
|
|
9469
9413
|
*
|
|
9470
9414
|
* // or update with `sql` function:
|
|
9471
|
-
* await db.table.find(1).updateRaw(
|
|
9415
|
+
* await db.table.find(1).updateRaw(sql`name = ${value}`);
|
|
9472
9416
|
* ```
|
|
9473
9417
|
* @param args - raw SQL via a template string or by using a `sql` method
|
|
9474
9418
|
*/
|
|
@@ -9734,7 +9678,7 @@ class Headline extends orchidCore.Expression {
|
|
|
9734
9678
|
const { q, source, params } = this;
|
|
9735
9679
|
const lang = getSearchLang(ctx, q, source, quotedAs);
|
|
9736
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);
|
|
9737
|
-
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)}` : "";
|
|
9738
9682
|
return `ts_headline(${lang}, ${text}, "${source.as}"${options})`;
|
|
9739
9683
|
}
|
|
9740
9684
|
}
|
|
@@ -10151,7 +10095,7 @@ class QueryUpsertOrCreate {
|
|
|
10151
10095
|
}
|
|
10152
10096
|
}
|
|
10153
10097
|
|
|
10154
|
-
class
|
|
10098
|
+
class SqlMethod {
|
|
10155
10099
|
sql(...args) {
|
|
10156
10100
|
const sql = raw(...args);
|
|
10157
10101
|
sql.columnTypes = this.columnTypes;
|
|
@@ -10392,6 +10336,118 @@ class RefExpression extends orchidCore.Expression {
|
|
|
10392
10336
|
return columnToSql(ctx, this.q, this.q.shape, this.ref, quotedAs);
|
|
10393
10337
|
}
|
|
10394
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
|
+
|
|
10395
10451
|
const _queryAll = (q) => {
|
|
10396
10452
|
q.q.returnType = "all";
|
|
10397
10453
|
q.q.all = true;
|
|
@@ -10536,11 +10592,13 @@ class QueryMethods {
|
|
|
10536
10592
|
* db.table.distinct().select('name');
|
|
10537
10593
|
* ```
|
|
10538
10594
|
*
|
|
10539
|
-
* 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 (...)`:
|
|
10540
10596
|
*
|
|
10541
10597
|
* ```ts
|
|
10598
|
+
* import { sql } from './baseTable';
|
|
10599
|
+
*
|
|
10542
10600
|
* // Distinct on the name and raw SQL
|
|
10543
|
-
* db.table.distinct('name',
|
|
10601
|
+
* db.table.distinct('name', sql`raw sql`).select('id', 'name');
|
|
10544
10602
|
* ```
|
|
10545
10603
|
*
|
|
10546
10604
|
* @param columns - column names or a raw SQL
|
|
@@ -10700,9 +10758,11 @@ class QueryMethods {
|
|
|
10700
10758
|
* Also, it's possible to group by a selected value:
|
|
10701
10759
|
*
|
|
10702
10760
|
* ```ts
|
|
10761
|
+
* import { sql } from './baseTable';
|
|
10762
|
+
*
|
|
10703
10763
|
* const results = db.product
|
|
10704
10764
|
* .select({
|
|
10705
|
-
* month:
|
|
10765
|
+
* month: sql`extract(month from "createdAt")`.type((t) =>
|
|
10706
10766
|
* // month is returned as string, parse it to int
|
|
10707
10767
|
* t.string().parse(parseInt),
|
|
10708
10768
|
* ),
|
|
@@ -10806,8 +10866,6 @@ class QueryMethods {
|
|
|
10806
10866
|
*
|
|
10807
10867
|
* ```ts
|
|
10808
10868
|
* db.table.orderSql`raw sql`;
|
|
10809
|
-
* // or
|
|
10810
|
-
* db.table.orderSql(db.table.sql`raw sql`);
|
|
10811
10869
|
* ```
|
|
10812
10870
|
*
|
|
10813
10871
|
* @param args - SQL expression
|
|
@@ -11001,73 +11059,6 @@ class QueryMethods {
|
|
|
11001
11059
|
return fn(q, ...args);
|
|
11002
11060
|
};
|
|
11003
11061
|
}
|
|
11004
|
-
/**
|
|
11005
|
-
* `column` references a table column, this can be used in raw SQL or when building a column expression.
|
|
11006
|
-
* Only for referencing a column in the query's table. For referencing joined table's columns, see [ref](#ref).
|
|
11007
|
-
*
|
|
11008
|
-
* ```ts
|
|
11009
|
-
* await db.table.select({
|
|
11010
|
-
* // select `("table"."id" = 1 OR "table"."name" = 'name') AS "one"`,
|
|
11011
|
-
* // returns a boolean
|
|
11012
|
-
* one: (q) =>
|
|
11013
|
-
* q.sql<boolean>`${q.column('id')} = ${1} OR ${q.column('name')} = ${'name'}`,
|
|
11014
|
-
*
|
|
11015
|
-
* // selects the same as above, but by building a query
|
|
11016
|
-
* two: (q) => q.column('id').equals(1).or(q.column('name').equals('name')),
|
|
11017
|
-
* });
|
|
11018
|
-
* ```
|
|
11019
|
-
*
|
|
11020
|
-
* @param name - column name
|
|
11021
|
-
*/
|
|
11022
|
-
column(name) {
|
|
11023
|
-
const column = this.shape[name];
|
|
11024
|
-
return new ColumnRefExpression(
|
|
11025
|
-
column,
|
|
11026
|
-
name
|
|
11027
|
-
);
|
|
11028
|
-
}
|
|
11029
|
-
/**
|
|
11030
|
-
* `ref` is similar to [column](#column), but it also allows to reference a column of joined table,
|
|
11031
|
-
* and other dynamically defined columns.
|
|
11032
|
-
*
|
|
11033
|
-
* ```ts
|
|
11034
|
-
* await db.table.join('otherTable').select({
|
|
11035
|
-
* // select `("otherTable"."id" = 1 OR "otherTable"."name" = 'name') AS "one"`,
|
|
11036
|
-
* // returns a boolean
|
|
11037
|
-
* one: (q) =>
|
|
11038
|
-
* q.sql<boolean>`${q.ref('otherTable.id')} = ${1} OR ${q.ref(
|
|
11039
|
-
* 'otherTable.name',
|
|
11040
|
-
* )} = ${'name'}`,
|
|
11041
|
-
*
|
|
11042
|
-
* // selects the same as above, but by building a query
|
|
11043
|
-
* two: (q) =>
|
|
11044
|
-
* q
|
|
11045
|
-
* .ref('otherTable.id')
|
|
11046
|
-
* .equals(1)
|
|
11047
|
-
* .or(q.ref('otherTable.name').equals('name')),
|
|
11048
|
-
* });
|
|
11049
|
-
* ```
|
|
11050
|
-
*
|
|
11051
|
-
* @param arg - any available column name, such as of a joined table
|
|
11052
|
-
*/
|
|
11053
|
-
ref(arg) {
|
|
11054
|
-
const q = this.clone();
|
|
11055
|
-
const { shape } = q.q;
|
|
11056
|
-
let column;
|
|
11057
|
-
const index = arg.indexOf(".");
|
|
11058
|
-
if (index !== -1) {
|
|
11059
|
-
const table = arg.slice(0, index);
|
|
11060
|
-
const col = arg.slice(index + 1);
|
|
11061
|
-
if (table === (q.q.as || q.table)) {
|
|
11062
|
-
column = shape[col];
|
|
11063
|
-
} else {
|
|
11064
|
-
column = q.q.joinedShapes[table][col];
|
|
11065
|
-
}
|
|
11066
|
-
} else {
|
|
11067
|
-
column = shape[arg];
|
|
11068
|
-
}
|
|
11069
|
-
return new RefExpression(column, q.q, arg);
|
|
11070
|
-
}
|
|
11071
11062
|
/**
|
|
11072
11063
|
* Narrows a part of the query output type.
|
|
11073
11064
|
* Use with caution, type-safety isn't guaranteed with it.
|
|
@@ -11124,10 +11115,11 @@ orchidCore.applyMixins(QueryMethods, [
|
|
|
11124
11115
|
QueryUpsertOrCreate,
|
|
11125
11116
|
QueryGet,
|
|
11126
11117
|
MergeQueryMethods,
|
|
11127
|
-
|
|
11118
|
+
SqlMethod,
|
|
11128
11119
|
TransformMethods,
|
|
11129
11120
|
ScopeMethods,
|
|
11130
|
-
SoftDeleteMethods
|
|
11121
|
+
SoftDeleteMethods,
|
|
11122
|
+
ExpressionMethods
|
|
11131
11123
|
]);
|
|
11132
11124
|
|
|
11133
11125
|
const makeIndex = (columns, first, second) => {
|
|
@@ -11735,6 +11727,7 @@ exports.DomainColumn = DomainColumn;
|
|
|
11735
11727
|
exports.DoublePrecisionColumn = DoublePrecisionColumn;
|
|
11736
11728
|
exports.DynamicRawSQL = DynamicRawSQL;
|
|
11737
11729
|
exports.EnumColumn = EnumColumn;
|
|
11730
|
+
exports.ExpressionMethods = ExpressionMethods;
|
|
11738
11731
|
exports.FnExpression = FnExpression;
|
|
11739
11732
|
exports.For = For;
|
|
11740
11733
|
exports.From = From;
|
|
@@ -11775,7 +11768,6 @@ exports.QueryLog = QueryLog;
|
|
|
11775
11768
|
exports.QueryMethods = QueryMethods;
|
|
11776
11769
|
exports.QueryUpsertOrCreate = QueryUpsertOrCreate;
|
|
11777
11770
|
exports.RawSQL = RawSQL;
|
|
11778
|
-
exports.RawSqlMethods = RawSqlMethods;
|
|
11779
11771
|
exports.RealColumn = RealColumn;
|
|
11780
11772
|
exports.RefExpression = RefExpression;
|
|
11781
11773
|
exports.SearchMethods = SearchMethods;
|
|
@@ -11783,6 +11775,7 @@ exports.Select = Select;
|
|
|
11783
11775
|
exports.SerialColumn = SerialColumn;
|
|
11784
11776
|
exports.SmallIntColumn = SmallIntColumn;
|
|
11785
11777
|
exports.SmallSerialColumn = SmallSerialColumn;
|
|
11778
|
+
exports.SqlMethod = SqlMethod;
|
|
11786
11779
|
exports.StringColumn = StringColumn;
|
|
11787
11780
|
exports.TextBaseColumn = TextBaseColumn;
|
|
11788
11781
|
exports.TextColumn = TextColumn;
|