pqb 0.30.2 → 0.30.4
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 +282 -253
- package/dist/index.js +346 -305
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +318 -279
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ExpressionTypeMethod, Expression, RawSQLBase, emptyObject, isTemplateLiteralArgs, ColumnTypeBase, setColumnData, pushColumnData, quoteObjectKey, toArray, singleQuote, addCode, singleQuoteArray, objectHasValues, columnDefaultArgumentToCode, columnErrorMessagesToCode, getValueKey, isExpression, joinTruthy, numberDataToCode, stringDataToCode, getDefaultLanguage, dateDataToCode, pushOrNewArrayToObject, arrayDataToCode, noop, emptyArray, callWithThis, setParserToQuery, applyTransforms, isRawSQL, pushOrNewArray, setDefaultNowFn, setDefaultLanguage, makeTimestampsHelpers, setCurrentColumnName, setAdapterConnectRetry, isObjectEmpty, applyMixins, toSnakeCase, snakeCaseKey } from 'orchid-core';
|
|
1
|
+
import { ExpressionTypeMethod, Expression, RawSQLBase, emptyObject, isTemplateLiteralArgs, ColumnTypeBase, setColumnData, pushColumnData, quoteObjectKey, toArray, singleQuote, addCode, singleQuoteArray, objectHasValues, columnDefaultArgumentToCode, columnErrorMessagesToCode, getValueKey, addValue, isExpression, joinTruthy, numberDataToCode, stringDataToCode, getDefaultLanguage, dateDataToCode, pushOrNewArrayToObject, arrayDataToCode, noop, emptyArray, callWithThis, setParserToQuery, applyTransforms, isRawSQL, pushOrNewArray, setDefaultNowFn, setDefaultLanguage, makeTimestampsHelpers, setCurrentColumnName, setAdapterConnectRetry, isObjectEmpty, ValExpression, applyMixins, toSnakeCase, snakeCaseKey } from 'orchid-core';
|
|
2
2
|
import pg from 'pg';
|
|
3
3
|
import { inspect } from 'node:util';
|
|
4
4
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
@@ -27,8 +27,8 @@ const templateLiteralToSQL = (template, ctx, quotedAs) => {
|
|
|
27
27
|
class RawSQL extends RawSQLBase {
|
|
28
28
|
constructor(sql, values, type) {
|
|
29
29
|
super(sql, values);
|
|
30
|
+
this.result = { value: type };
|
|
30
31
|
if (type) {
|
|
31
|
-
this.result = { value: type };
|
|
32
32
|
Object.assign(this, type.operators);
|
|
33
33
|
}
|
|
34
34
|
}
|
|
@@ -834,102 +834,6 @@ const columnCode = (type, t, code, migration, data = type.data, skip) => {
|
|
|
834
834
|
return code.length === 1 && typeof code[0] === "string" ? code[0] : code;
|
|
835
835
|
};
|
|
836
836
|
|
|
837
|
-
function simpleColumnToSQL(ctx, key, column, quotedAs) {
|
|
838
|
-
if (!column)
|
|
839
|
-
return `"${key}"`;
|
|
840
|
-
const { data } = column;
|
|
841
|
-
return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
|
|
842
|
-
}
|
|
843
|
-
function simpleExistingColumnToSQL(ctx, key, column, quotedAs) {
|
|
844
|
-
const { data } = column;
|
|
845
|
-
return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
|
|
846
|
-
}
|
|
847
|
-
const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
|
|
848
|
-
var _a, _b, _c, _d, _e;
|
|
849
|
-
const index = column.indexOf(".");
|
|
850
|
-
if (index !== -1) {
|
|
851
|
-
const table = column.slice(0, index);
|
|
852
|
-
const key = column.slice(index + 1);
|
|
853
|
-
if (key === "*") {
|
|
854
|
-
if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
|
|
855
|
-
return select ? `row_to_json("${table}".*)` : `"${table}".r`;
|
|
856
|
-
}
|
|
857
|
-
return column;
|
|
858
|
-
}
|
|
859
|
-
const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
|
|
860
|
-
const quoted = `"${table}"`;
|
|
861
|
-
const col = quoted === quotedAs ? shape[key] : (_d = (_c = data.joinedShapes) == null ? void 0 : _c[tableName]) == null ? void 0 : _d[key];
|
|
862
|
-
if (col) {
|
|
863
|
-
if (col.data.name) {
|
|
864
|
-
return `"${tableName}"."${col.data.name}"`;
|
|
865
|
-
}
|
|
866
|
-
if (col.data.computed) {
|
|
867
|
-
return `${col.data.computed.toSQL(ctx, quoted)}`;
|
|
868
|
-
}
|
|
869
|
-
return `"${tableName}"."${key}"`;
|
|
870
|
-
}
|
|
871
|
-
return `"${tableName}"."${key}"`;
|
|
872
|
-
}
|
|
873
|
-
if (!select && ((_e = data.joinedShapes) == null ? void 0 : _e[column])) {
|
|
874
|
-
return `"${column}".r`;
|
|
875
|
-
}
|
|
876
|
-
return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
|
|
877
|
-
};
|
|
878
|
-
const columnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
|
|
879
|
-
var _a, _b, _c, _d;
|
|
880
|
-
const index = column.indexOf(".");
|
|
881
|
-
if (index !== -1) {
|
|
882
|
-
const table = column.slice(0, index);
|
|
883
|
-
const key = column.slice(index + 1);
|
|
884
|
-
if (key === "*") {
|
|
885
|
-
if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
|
|
886
|
-
return select ? `row_to_json("${table}".*) "${table}"` : `"${table}".r "${table}"`;
|
|
887
|
-
}
|
|
888
|
-
return column;
|
|
889
|
-
}
|
|
890
|
-
const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
|
|
891
|
-
const quoted = `"${table}"`;
|
|
892
|
-
const col2 = quoted === quotedAs ? data.shape[key] : (_c = data.joinedShapes) == null ? void 0 : _c[tableName][key];
|
|
893
|
-
if (col2) {
|
|
894
|
-
if (col2.data.name && col2.data.name !== key) {
|
|
895
|
-
return `"${tableName}"."${col2.data.name}" "${key}"`;
|
|
896
|
-
}
|
|
897
|
-
if (col2.data.computed) {
|
|
898
|
-
return `${col2.data.computed.toSQL(ctx, quoted)} "${key}"`;
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
return `"${tableName}"."${key}"`;
|
|
902
|
-
}
|
|
903
|
-
if (!select && ((_d = data.joinedShapes) == null ? void 0 : _d[column])) {
|
|
904
|
-
return select ? `row_to_json("${column}".*) "${column}"` : `"${column}".r "${column}"`;
|
|
905
|
-
}
|
|
906
|
-
const col = data.shape[column];
|
|
907
|
-
if (col) {
|
|
908
|
-
if (col.data.name && col.data.name !== column) {
|
|
909
|
-
return `${quotedAs ? `${quotedAs}.` : ""}"${col.data.name}" "${column}"`;
|
|
910
|
-
}
|
|
911
|
-
if (col.data.computed) {
|
|
912
|
-
return `${col.data.computed.toSQL(ctx, quotedAs)} "${column}"`;
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
|
-
return `${quotedAs ? `${quotedAs}.` : ""}"${column}"`;
|
|
916
|
-
};
|
|
917
|
-
const ownColumnToSql = (data, column, quotedAs) => {
|
|
918
|
-
var _a;
|
|
919
|
-
const name = (_a = data.shape[column]) == null ? void 0 : _a.data.name;
|
|
920
|
-
return `${quotedAs ? `${quotedAs}.` : ""}"${name || column}"${name && name !== column ? ` "${column}"` : ""}`;
|
|
921
|
-
};
|
|
922
|
-
const rawOrColumnToSql = (ctx, data, expr, quotedAs, shape = data.shape, select) => {
|
|
923
|
-
return typeof expr === "string" ? columnToSql(ctx, data, shape, expr, quotedAs, select) : expr.toSQL(ctx, quotedAs);
|
|
924
|
-
};
|
|
925
|
-
const quoteSchemaAndTable = (schema, table) => {
|
|
926
|
-
return schema ? `"${schema}"."${table}"` : `"${table}"`;
|
|
927
|
-
};
|
|
928
|
-
const addValue = (values, value) => {
|
|
929
|
-
values.push(value);
|
|
930
|
-
return `$${values.length}`;
|
|
931
|
-
};
|
|
932
|
-
|
|
933
837
|
var __defProp$f = Object.defineProperty;
|
|
934
838
|
var __defProps$7 = Object.defineProperties;
|
|
935
839
|
var __getOwnPropDescs$7 = Object.getOwnPropertyDescriptors;
|
|
@@ -1859,6 +1763,98 @@ const queryTypeWithLimitOne = {
|
|
|
1859
1763
|
};
|
|
1860
1764
|
const isQueryReturnsAll = (q) => !q.q.returnType || q.q.returnType === "all";
|
|
1861
1765
|
|
|
1766
|
+
function simpleColumnToSQL(ctx, key, column, quotedAs) {
|
|
1767
|
+
if (!column)
|
|
1768
|
+
return `"${key}"`;
|
|
1769
|
+
const { data } = column;
|
|
1770
|
+
return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
|
|
1771
|
+
}
|
|
1772
|
+
function simpleExistingColumnToSQL(ctx, key, column, quotedAs) {
|
|
1773
|
+
const { data } = column;
|
|
1774
|
+
return data.computed ? data.computed.toSQL(ctx, quotedAs) : `${quotedAs ? `${quotedAs}.` : ""}"${data.name || key}"`;
|
|
1775
|
+
}
|
|
1776
|
+
const columnToSql = (ctx, data, shape, column, quotedAs, select) => {
|
|
1777
|
+
var _a, _b, _c, _d, _e;
|
|
1778
|
+
const index = column.indexOf(".");
|
|
1779
|
+
if (index !== -1) {
|
|
1780
|
+
const table = column.slice(0, index);
|
|
1781
|
+
const key = column.slice(index + 1);
|
|
1782
|
+
if (key === "*") {
|
|
1783
|
+
if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
|
|
1784
|
+
return select ? `row_to_json("${table}".*)` : `"${table}".r`;
|
|
1785
|
+
}
|
|
1786
|
+
return column;
|
|
1787
|
+
}
|
|
1788
|
+
const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
|
|
1789
|
+
const quoted = `"${table}"`;
|
|
1790
|
+
const col = quoted === quotedAs ? shape[key] : (_d = (_c = data.joinedShapes) == null ? void 0 : _c[tableName]) == null ? void 0 : _d[key];
|
|
1791
|
+
if (col) {
|
|
1792
|
+
if (col.data.name) {
|
|
1793
|
+
return `"${tableName}"."${col.data.name}"`;
|
|
1794
|
+
}
|
|
1795
|
+
if (col.data.computed) {
|
|
1796
|
+
return `${col.data.computed.toSQL(ctx, quoted)}`;
|
|
1797
|
+
}
|
|
1798
|
+
return `"${tableName}"."${key}"`;
|
|
1799
|
+
}
|
|
1800
|
+
return `"${tableName}"."${key}"`;
|
|
1801
|
+
}
|
|
1802
|
+
if (!select && ((_e = data.joinedShapes) == null ? void 0 : _e[column])) {
|
|
1803
|
+
return `"${column}".r`;
|
|
1804
|
+
}
|
|
1805
|
+
return simpleColumnToSQL(ctx, column, shape[column], quotedAs);
|
|
1806
|
+
};
|
|
1807
|
+
const columnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
|
|
1808
|
+
var _a, _b, _c, _d;
|
|
1809
|
+
const index = column.indexOf(".");
|
|
1810
|
+
if (index !== -1) {
|
|
1811
|
+
const table = column.slice(0, index);
|
|
1812
|
+
const key = column.slice(index + 1);
|
|
1813
|
+
if (key === "*") {
|
|
1814
|
+
if ((_a = data.joinedShapes) == null ? void 0 : _a[table]) {
|
|
1815
|
+
return select ? `row_to_json("${table}".*) "${table}"` : `"${table}".r "${table}"`;
|
|
1816
|
+
}
|
|
1817
|
+
return column;
|
|
1818
|
+
}
|
|
1819
|
+
const tableName = ((_b = data.joinOverrides) == null ? void 0 : _b[table]) || table;
|
|
1820
|
+
const quoted = `"${table}"`;
|
|
1821
|
+
const col2 = quoted === quotedAs ? data.shape[key] : (_c = data.joinedShapes) == null ? void 0 : _c[tableName][key];
|
|
1822
|
+
if (col2) {
|
|
1823
|
+
if (col2.data.name && col2.data.name !== key) {
|
|
1824
|
+
return `"${tableName}"."${col2.data.name}" "${key}"`;
|
|
1825
|
+
}
|
|
1826
|
+
if (col2.data.computed) {
|
|
1827
|
+
return `${col2.data.computed.toSQL(ctx, quoted)} "${key}"`;
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
return `"${tableName}"."${key}"`;
|
|
1831
|
+
}
|
|
1832
|
+
if (!select && ((_d = data.joinedShapes) == null ? void 0 : _d[column])) {
|
|
1833
|
+
return select ? `row_to_json("${column}".*) "${column}"` : `"${column}".r "${column}"`;
|
|
1834
|
+
}
|
|
1835
|
+
const col = data.shape[column];
|
|
1836
|
+
if (col) {
|
|
1837
|
+
if (col.data.name && col.data.name !== column) {
|
|
1838
|
+
return `${quotedAs ? `${quotedAs}.` : ""}"${col.data.name}" "${column}"`;
|
|
1839
|
+
}
|
|
1840
|
+
if (col.data.computed) {
|
|
1841
|
+
return `${col.data.computed.toSQL(ctx, quotedAs)} "${column}"`;
|
|
1842
|
+
}
|
|
1843
|
+
}
|
|
1844
|
+
return `${quotedAs ? `${quotedAs}.` : ""}"${column}"`;
|
|
1845
|
+
};
|
|
1846
|
+
const ownColumnToSql = (data, column, quotedAs) => {
|
|
1847
|
+
var _a;
|
|
1848
|
+
const name = (_a = data.shape[column]) == null ? void 0 : _a.data.name;
|
|
1849
|
+
return `${quotedAs ? `${quotedAs}.` : ""}"${name || column}"${name && name !== column ? ` "${column}"` : ""}`;
|
|
1850
|
+
};
|
|
1851
|
+
const rawOrColumnToSql = (ctx, data, expr, quotedAs, shape = data.shape, select) => {
|
|
1852
|
+
return typeof expr === "string" ? columnToSql(ctx, data, shape, expr, quotedAs, select) : expr.toSQL(ctx, quotedAs);
|
|
1853
|
+
};
|
|
1854
|
+
const quoteSchemaAndTable = (schema, table) => {
|
|
1855
|
+
return schema ? `"${schema}"."${table}"` : `"${table}"`;
|
|
1856
|
+
};
|
|
1857
|
+
|
|
1862
1858
|
const pushDistinctSql = (ctx, table, distinct, quotedAs) => {
|
|
1863
1859
|
ctx.sql.push("DISTINCT");
|
|
1864
1860
|
if (distinct.length) {
|
|
@@ -2026,13 +2022,23 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2026
2022
|
qb.q.and = qb.q.or = void 0;
|
|
2027
2023
|
qb.q.isSubQuery = true;
|
|
2028
2024
|
const res = resolveSubQueryCallback(qb, data);
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
const q = joinSubQuery(table, res);
|
|
2032
|
-
q.q.select = [expr];
|
|
2033
|
-
ands.push(`(${makeSQL(q, ctx).text})`);
|
|
2025
|
+
if (isExpression(res)) {
|
|
2026
|
+
ands.push(`(${res.toSQL(ctx, quotedAs)})`);
|
|
2034
2027
|
} else {
|
|
2035
|
-
|
|
2028
|
+
if (res.q.expr) {
|
|
2029
|
+
const q = joinSubQuery(table, res);
|
|
2030
|
+
q.q.select = [res.q.expr];
|
|
2031
|
+
ands.push(`(${makeSQL(q, ctx).text})`);
|
|
2032
|
+
} else {
|
|
2033
|
+
pushWhereToSql(
|
|
2034
|
+
ands,
|
|
2035
|
+
ctx,
|
|
2036
|
+
res,
|
|
2037
|
+
res.q,
|
|
2038
|
+
quotedAs,
|
|
2039
|
+
true
|
|
2040
|
+
);
|
|
2041
|
+
}
|
|
2036
2042
|
}
|
|
2037
2043
|
return;
|
|
2038
2044
|
}
|
|
@@ -2210,9 +2216,15 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2210
2216
|
}
|
|
2211
2217
|
}
|
|
2212
2218
|
} else {
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2219
|
+
const column = columnToSql(ctx, query, query.shape, key, quotedAs);
|
|
2220
|
+
if (typeof value === "function") {
|
|
2221
|
+
const expr = value(table);
|
|
2222
|
+
ands.push(`${column} = ${expr.toSQL(ctx, quotedAs)}`);
|
|
2223
|
+
} else {
|
|
2224
|
+
ands.push(
|
|
2225
|
+
`${column} ${value === null ? "IS NULL" : `= ${addValue(ctx.values, value)}`}`
|
|
2226
|
+
);
|
|
2227
|
+
}
|
|
2216
2228
|
}
|
|
2217
2229
|
}
|
|
2218
2230
|
};
|
|
@@ -3688,7 +3700,6 @@ class From {
|
|
|
3688
3700
|
* ```ts
|
|
3689
3701
|
* const value = 123;
|
|
3690
3702
|
* db.table.fromSql`value = ${value}`;
|
|
3691
|
-
* db.table.fromSql(db.table.sql`value = ${value}`);
|
|
3692
3703
|
* ```
|
|
3693
3704
|
*
|
|
3694
3705
|
* @param args - SQL expression
|
|
@@ -4709,7 +4720,7 @@ const makeSQL = (table, options) => {
|
|
|
4709
4720
|
sql.push(`WINDOW ${window.join(", ")}`);
|
|
4710
4721
|
}
|
|
4711
4722
|
if (query.union) {
|
|
4712
|
-
query.union
|
|
4723
|
+
for (const item of query.union) {
|
|
4713
4724
|
let itemSql;
|
|
4714
4725
|
if (isExpression(item.arg)) {
|
|
4715
4726
|
itemSql = item.arg.toSQL(ctx, quotedAs);
|
|
@@ -4718,7 +4729,7 @@ const makeSQL = (table, options) => {
|
|
|
4718
4729
|
itemSql = argSql.text;
|
|
4719
4730
|
}
|
|
4720
4731
|
sql.push(`${item.kind} ${item.wrap ? `(${itemSql})` : itemSql}`);
|
|
4721
|
-
}
|
|
4732
|
+
}
|
|
4722
4733
|
}
|
|
4723
4734
|
if (query.order) {
|
|
4724
4735
|
pushOrderBySql(ctx, query, quotedAs, query.order);
|
|
@@ -5412,7 +5423,8 @@ class FnExpression extends Expression {
|
|
|
5412
5423
|
}
|
|
5413
5424
|
}
|
|
5414
5425
|
function makeFnExpression(self, type, fn, args, options) {
|
|
5415
|
-
const q = self.
|
|
5426
|
+
const q = extendQuery(self, type.operators);
|
|
5427
|
+
q.baseQuery.type = ExpressionTypeMethod.prototype.type;
|
|
5416
5428
|
new FnExpression(
|
|
5417
5429
|
q,
|
|
5418
5430
|
fn,
|
|
@@ -5434,49 +5446,6 @@ nullableInt.parseItem = nullableInt.parseFn = (input) => input === null ? null :
|
|
|
5434
5446
|
const nullableFloat = new RealColumn(defaultSchemaConfig);
|
|
5435
5447
|
nullableFloat.parseItem = nullableFloat.parseFn = (input) => input === null ? null : parseFloat(input);
|
|
5436
5448
|
class AggregateMethods {
|
|
5437
|
-
/**
|
|
5438
|
-
* `fn` allows to call an arbitrary SQL function.
|
|
5439
|
-
*
|
|
5440
|
-
* For example, calling `sqrt` function to get a square root from some numeric column:
|
|
5441
|
-
*
|
|
5442
|
-
* ```ts
|
|
5443
|
-
* const q = await User.select({
|
|
5444
|
-
* // todo: use type callback instead of generic type
|
|
5445
|
-
* sqrt: (q) => q.fn<number>('sqrt', ['numericColumn']),
|
|
5446
|
-
* }).take();
|
|
5447
|
-
*
|
|
5448
|
-
* q.sqrt; // has type `number` just as provided
|
|
5449
|
-
* ```
|
|
5450
|
-
*
|
|
5451
|
-
* If this is an aggregate function, you can specify aggregation options via third parameter.
|
|
5452
|
-
*
|
|
5453
|
-
* Forth parameter is for runtime column type. When specified, allows to chain the function with the column operators:
|
|
5454
|
-
*
|
|
5455
|
-
* ```ts
|
|
5456
|
-
* const q = await User.select({
|
|
5457
|
-
* // chain `sqrt("numericColumn")` with the "greater than 5"
|
|
5458
|
-
* sqrtIsGreaterThan5: (q) => q.fn('sqrt', ['numericColumn'], {}, (t) => t.float()).gt(5),
|
|
5459
|
-
* }).take();
|
|
5460
|
-
*
|
|
5461
|
-
* // Return type is boolean | null
|
|
5462
|
-
* // todo: it should be just boolean if the column is not nullable, but for now it's always nullable
|
|
5463
|
-
* q.sqrtIsGreaterThan5
|
|
5464
|
-
* ```
|
|
5465
|
-
*
|
|
5466
|
-
* @param fn
|
|
5467
|
-
* @param args
|
|
5468
|
-
* @param options
|
|
5469
|
-
* @param type
|
|
5470
|
-
*/
|
|
5471
|
-
fn(fn, args, options, type) {
|
|
5472
|
-
return makeFnExpression(
|
|
5473
|
-
this,
|
|
5474
|
-
(type == null ? void 0 : type(this.columnTypes)) || emptyObject,
|
|
5475
|
-
fn,
|
|
5476
|
-
args,
|
|
5477
|
-
options
|
|
5478
|
-
);
|
|
5479
|
-
}
|
|
5480
5449
|
/**
|
|
5481
5450
|
* Use `exists()` to check if there is at least one record-matching condition.
|
|
5482
5451
|
*
|
|
@@ -5821,7 +5790,7 @@ class AggregateMethods {
|
|
|
5821
5790
|
* // select a column with alias
|
|
5822
5791
|
* nameAlias: 'name',
|
|
5823
5792
|
* // select raw SQL with alias
|
|
5824
|
-
* foo:
|
|
5793
|
+
* foo: sql<string>`"bar" || "baz"`,
|
|
5825
5794
|
* },
|
|
5826
5795
|
* aggregateOptions,
|
|
5827
5796
|
* );
|
|
@@ -5831,7 +5800,7 @@ class AggregateMethods {
|
|
|
5831
5800
|
* object: (q) =>
|
|
5832
5801
|
* q.jsonObjectAgg({
|
|
5833
5802
|
* nameAlias: 'name',
|
|
5834
|
-
* foo:
|
|
5803
|
+
* foo: sql<string>`"bar" || "baz"`,
|
|
5835
5804
|
* }),
|
|
5836
5805
|
* });
|
|
5837
5806
|
* ```
|
|
@@ -6303,7 +6272,7 @@ class Create {
|
|
|
6303
6272
|
*
|
|
6304
6273
|
* await db.table.create({
|
|
6305
6274
|
* // raw SQL
|
|
6306
|
-
* column1:
|
|
6275
|
+
* column1: sql`'John' || ' ' || 'Doe'`,
|
|
6307
6276
|
*
|
|
6308
6277
|
* // query that returns a single value
|
|
6309
6278
|
* // returning multiple values will result in Postgres error
|
|
@@ -6366,7 +6335,7 @@ class Create {
|
|
|
6366
6335
|
* ```ts
|
|
6367
6336
|
* const oneRecord = await db.table.createRaw({
|
|
6368
6337
|
* columns: ['name', 'amount'],
|
|
6369
|
-
* values:
|
|
6338
|
+
* values: sql`'name', random()`,
|
|
6370
6339
|
* });
|
|
6371
6340
|
* ```
|
|
6372
6341
|
*
|
|
@@ -6395,7 +6364,7 @@ class Create {
|
|
|
6395
6364
|
* ```ts
|
|
6396
6365
|
* const manyRecords = await db.table.createManyRaw({
|
|
6397
6366
|
* columns: ['name', 'amount'],
|
|
6398
|
-
* values: [
|
|
6367
|
+
* values: [sql`'one', 2`, sql`'three', 4`],
|
|
6399
6368
|
* });
|
|
6400
6369
|
* ```
|
|
6401
6370
|
*
|
|
@@ -6549,11 +6518,11 @@ class Create {
|
|
|
6549
6518
|
* // raw SQL expression:
|
|
6550
6519
|
* db.table
|
|
6551
6520
|
* .create(data)
|
|
6552
|
-
* .onConfict(
|
|
6521
|
+
* .onConfict(sql`(email) where condition`)
|
|
6553
6522
|
* .merge();
|
|
6554
6523
|
* ```
|
|
6555
6524
|
*
|
|
6556
|
-
* You can use the
|
|
6525
|
+
* You can use the `sql` function exported from your `BaseTable` file in onConflict.
|
|
6557
6526
|
* It can be useful to specify a condition when you have a partial index:
|
|
6558
6527
|
*
|
|
6559
6528
|
* ```ts
|
|
@@ -6564,7 +6533,7 @@ class Create {
|
|
|
6564
6533
|
* active: true,
|
|
6565
6534
|
* })
|
|
6566
6535
|
* // ignore only when having conflicting email and when active is true.
|
|
6567
|
-
* .onConflict(
|
|
6536
|
+
* .onConflict(sql`(email) where active`)
|
|
6568
6537
|
* .ignore();
|
|
6569
6538
|
* ```
|
|
6570
6539
|
*
|
|
@@ -6664,7 +6633,7 @@ class OnConflictQueryBuilder {
|
|
|
6664
6633
|
* db.table
|
|
6665
6634
|
* .create(data)
|
|
6666
6635
|
* .onConflict()
|
|
6667
|
-
* .set(
|
|
6636
|
+
* .set(sql`raw SQL expression`);
|
|
6668
6637
|
*
|
|
6669
6638
|
* // update records only on certain conditions
|
|
6670
6639
|
* db.table
|
|
@@ -6838,16 +6807,20 @@ class For {
|
|
|
6838
6807
|
|
|
6839
6808
|
class QueryGet {
|
|
6840
6809
|
/**
|
|
6841
|
-
* `.get` returns a single value,
|
|
6842
|
-
*
|
|
6810
|
+
* `.get` returns a single value, adds `LIMIT 1` to the query, and accepts a column name or a raw SQL expression.
|
|
6811
|
+
*
|
|
6812
|
+
* `get` throws a `NotFoundError` when not found, and `getOptional` returns `undefined`.
|
|
6843
6813
|
*
|
|
6844
6814
|
* ```ts
|
|
6845
6815
|
* import { NumberColumn } from 'orchid-orm';
|
|
6816
|
+
* import { sql } from './baseTable';
|
|
6846
6817
|
*
|
|
6847
6818
|
* const firstName: string = await db.table.get('name');
|
|
6848
6819
|
*
|
|
6849
|
-
* const rawResult: number = await db.table.get(
|
|
6850
|
-
*
|
|
6820
|
+
* const rawResult: number = await db.table.get(sql((t) => t.integer())`1 + 1`);
|
|
6821
|
+
*
|
|
6822
|
+
* const firstNameOptional: string | undefined = await db.table.getOptional(
|
|
6823
|
+
* 'name',
|
|
6851
6824
|
* );
|
|
6852
6825
|
* ```
|
|
6853
6826
|
*
|
|
@@ -6935,7 +6908,7 @@ class Having {
|
|
|
6935
6908
|
* Provide SQL expression for the `HAVING` SQL statement:
|
|
6936
6909
|
*
|
|
6937
6910
|
* ```ts
|
|
6938
|
-
* db.table.
|
|
6911
|
+
* db.table.havingSql`count(*) >= ${10}`;
|
|
6939
6912
|
* ```
|
|
6940
6913
|
*
|
|
6941
6914
|
* @param args - SQL expression
|
|
@@ -7449,7 +7422,8 @@ class Join {
|
|
|
7449
7422
|
* ```ts
|
|
7450
7423
|
* db.user.join(
|
|
7451
7424
|
* db.message,
|
|
7452
|
-
*
|
|
7425
|
+
* // `sql` can be imported from your `BaseTable` file
|
|
7426
|
+
* sql`lower("message"."text") = lower("user"."name")`,
|
|
7453
7427
|
* );
|
|
7454
7428
|
* ```
|
|
7455
7429
|
*
|
|
@@ -7458,16 +7432,16 @@ class Join {
|
|
|
7458
7432
|
* ```ts
|
|
7459
7433
|
* db.user.join(
|
|
7460
7434
|
* db.message,
|
|
7461
|
-
*
|
|
7462
|
-
*
|
|
7435
|
+
* sql`lower("message"."text")`,
|
|
7436
|
+
* sql`lower("user"."name")`,
|
|
7463
7437
|
* );
|
|
7464
7438
|
*
|
|
7465
7439
|
* // with operator:
|
|
7466
7440
|
* db.user.join(
|
|
7467
7441
|
* db.message,
|
|
7468
|
-
*
|
|
7442
|
+
* sql`lower("message"."text")`,
|
|
7469
7443
|
* '!=',
|
|
7470
|
-
*
|
|
7444
|
+
* sql`lower("user"."name")`,
|
|
7471
7445
|
* );
|
|
7472
7446
|
* ```
|
|
7473
7447
|
*
|
|
@@ -7481,7 +7455,7 @@ class Join {
|
|
|
7481
7455
|
* 'message.userId': 'user.id',
|
|
7482
7456
|
*
|
|
7483
7457
|
* // value can be a raw SQL expression:
|
|
7484
|
-
* text:
|
|
7458
|
+
* text: sql`lower("user"."name")`,
|
|
7485
7459
|
* });
|
|
7486
7460
|
* ```
|
|
7487
7461
|
*
|
|
@@ -8271,7 +8245,7 @@ class With {
|
|
|
8271
8245
|
* id: columnTypes.integer(),
|
|
8272
8246
|
* name: columnTypes.text(3, 100),
|
|
8273
8247
|
* },
|
|
8274
|
-
*
|
|
8248
|
+
* sql`SELECT id, name FROM "someTable"`,
|
|
8275
8249
|
* );
|
|
8276
8250
|
*
|
|
8277
8251
|
* // accepts query:
|
|
@@ -8279,7 +8253,7 @@ class With {
|
|
|
8279
8253
|
*
|
|
8280
8254
|
* // accepts a callback for a query builder:
|
|
8281
8255
|
* db.table.with('alias', (qb) =>
|
|
8282
|
-
* qb.select({ one:
|
|
8256
|
+
* qb.select({ one: sql`1`.type((t) => t.integer()) }),
|
|
8283
8257
|
* );
|
|
8284
8258
|
*
|
|
8285
8259
|
* // All mentioned forms can accept options as a second argument:
|
|
@@ -8331,7 +8305,7 @@ class Union {
|
|
|
8331
8305
|
* SomeTable.select('id', 'name').union(
|
|
8332
8306
|
* [
|
|
8333
8307
|
* OtherTable.select('id', 'name'),
|
|
8334
|
-
*
|
|
8308
|
+
* sql`SELECT id, name FROM "thirdTable"`,
|
|
8335
8309
|
* ],
|
|
8336
8310
|
* true, // optional wrap parameter
|
|
8337
8311
|
* );
|
|
@@ -8526,7 +8500,13 @@ class Where {
|
|
|
8526
8500
|
* },
|
|
8527
8501
|
*
|
|
8528
8502
|
* // where column equals to raw SQL
|
|
8529
|
-
*
|
|
8503
|
+
* // import `sql` from your `BaseTable`
|
|
8504
|
+
* column: sql`sql expression`,
|
|
8505
|
+
* // or use `(q) => q.sql` for the same
|
|
8506
|
+
* column2: (q) => q.sql`sql expression`,
|
|
8507
|
+
*
|
|
8508
|
+
* // reference other columns in such a way:
|
|
8509
|
+
* firstName: (q) => q.ref('lastName'),
|
|
8530
8510
|
* });
|
|
8531
8511
|
* ```
|
|
8532
8512
|
*
|
|
@@ -8575,12 +8555,7 @@ class Where {
|
|
|
8575
8555
|
* `where` supports raw SQL:
|
|
8576
8556
|
*
|
|
8577
8557
|
* ```ts
|
|
8578
|
-
* db.table.where(
|
|
8579
|
-
*
|
|
8580
|
-
* // or
|
|
8581
|
-
* import { raw } from 'orchid-orm';
|
|
8582
|
-
*
|
|
8583
|
-
* db.table.where(raw`a = b`);
|
|
8558
|
+
* db.table.where(sql`a = b`);
|
|
8584
8559
|
* ```
|
|
8585
8560
|
*
|
|
8586
8561
|
* `where` can accept a callback with a specific query builder containing all "where" methods such as `where`, `orWhere`, `whereNot`, `whereIn`, `whereExists`:
|
|
@@ -8601,7 +8576,7 @@ class Where {
|
|
|
8601
8576
|
* db.table.where(
|
|
8602
8577
|
* { id: 1 },
|
|
8603
8578
|
* db.table.where({ name: 'John' }),
|
|
8604
|
-
*
|
|
8579
|
+
* sql`a = b`,
|
|
8605
8580
|
* );
|
|
8606
8581
|
* ```
|
|
8607
8582
|
*
|
|
@@ -8714,8 +8689,6 @@ class Where {
|
|
|
8714
8689
|
* All column operators can take a value of the same type as the column, a sub-query, or a raw SQL expression:
|
|
8715
8690
|
*
|
|
8716
8691
|
* ```ts
|
|
8717
|
-
* import { sql } from 'orchid-orm';
|
|
8718
|
-
*
|
|
8719
8692
|
* db.table.where({
|
|
8720
8693
|
* numericColumn: {
|
|
8721
8694
|
* // lower than 5
|
|
@@ -8764,7 +8737,7 @@ class Where {
|
|
|
8764
8737
|
* // WHERE "column" IN (SELECT "column" FROM "otherTable")
|
|
8765
8738
|
* in: OtherTable.select('column'),
|
|
8766
8739
|
*
|
|
8767
|
-
* in:
|
|
8740
|
+
* in: sql`('a', 'b')`,
|
|
8768
8741
|
* },
|
|
8769
8742
|
* });
|
|
8770
8743
|
* ```
|
|
@@ -8811,7 +8784,7 @@ class Where {
|
|
|
8811
8784
|
* between: [1, 10],
|
|
8812
8785
|
*
|
|
8813
8786
|
* // sub-query and raw SQL expression
|
|
8814
|
-
* between: [OtherTable.select('column').take(),
|
|
8787
|
+
* between: [OtherTable.select('column').take(), sql`2 + 2`],
|
|
8815
8788
|
* },
|
|
8816
8789
|
* });
|
|
8817
8790
|
* ```
|
|
@@ -8903,15 +8876,7 @@ class Where {
|
|
|
8903
8876
|
* Use a custom SQL expression in `WHERE` statement:
|
|
8904
8877
|
*
|
|
8905
8878
|
* ```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`);
|
|
8879
|
+
* db.table.whereSql`a = b`;
|
|
8915
8880
|
* ```
|
|
8916
8881
|
*
|
|
8917
8882
|
* @param args - SQL expression
|
|
@@ -8944,10 +8909,10 @@ class Where {
|
|
|
8944
8909
|
);
|
|
8945
8910
|
}
|
|
8946
8911
|
/**
|
|
8947
|
-
* `
|
|
8912
|
+
* `whereNotSql` is a version of `whereNot` accepting SQL expression:
|
|
8948
8913
|
*
|
|
8949
8914
|
* ```ts
|
|
8950
|
-
* db.table.
|
|
8915
|
+
* db.table.whereNotSql`sql expression`
|
|
8951
8916
|
* ```
|
|
8952
8917
|
*
|
|
8953
8918
|
* @param args - SQL expression
|
|
@@ -9024,7 +8989,7 @@ class Where {
|
|
|
9024
8989
|
* It supports raw SQL expression:
|
|
9025
8990
|
*
|
|
9026
8991
|
* ```ts
|
|
9027
|
-
* db.table.whereIn(['id', 'name'],
|
|
8992
|
+
* db.table.whereIn(['id', 'name'], sql`((1, 'one'), (2, 'two'))`);
|
|
9028
8993
|
* ```
|
|
9029
8994
|
*/
|
|
9030
8995
|
whereIn(...args) {
|
|
@@ -9338,7 +9303,7 @@ class Update {
|
|
|
9338
9303
|
* column1: 123,
|
|
9339
9304
|
*
|
|
9340
9305
|
* // use raw SQL to update the column
|
|
9341
|
-
* column2:
|
|
9306
|
+
* column2: sql`2 + 2`,
|
|
9342
9307
|
*
|
|
9343
9308
|
* // use query that returns a single value
|
|
9344
9309
|
* // returning multiple values will result in Postgres error
|
|
@@ -9466,7 +9431,7 @@ class Update {
|
|
|
9466
9431
|
* const updatedCount = await db.table.find(1).updateRaw`name = ${value}`;
|
|
9467
9432
|
*
|
|
9468
9433
|
* // or update with `sql` function:
|
|
9469
|
-
* await db.table.find(1).updateRaw(
|
|
9434
|
+
* await db.table.find(1).updateRaw(sql`name = ${value}`);
|
|
9470
9435
|
* ```
|
|
9471
9436
|
* @param args - raw SQL via a template string or by using a `sql` method
|
|
9472
9437
|
*/
|
|
@@ -10149,7 +10114,7 @@ class QueryUpsertOrCreate {
|
|
|
10149
10114
|
}
|
|
10150
10115
|
}
|
|
10151
10116
|
|
|
10152
|
-
class
|
|
10117
|
+
class SqlMethod {
|
|
10153
10118
|
sql(...args) {
|
|
10154
10119
|
const sql = raw(...args);
|
|
10155
10120
|
sql.columnTypes = this.columnTypes;
|
|
@@ -10390,6 +10355,144 @@ class RefExpression extends Expression {
|
|
|
10390
10355
|
return columnToSql(ctx, this.q, this.q.shape, this.ref, quotedAs);
|
|
10391
10356
|
}
|
|
10392
10357
|
}
|
|
10358
|
+
class OrExpression extends Expression {
|
|
10359
|
+
constructor(args) {
|
|
10360
|
+
super();
|
|
10361
|
+
this.args = args;
|
|
10362
|
+
this.q = { expr: this };
|
|
10363
|
+
}
|
|
10364
|
+
makeSQL(ctx, quotedAs) {
|
|
10365
|
+
const res = [];
|
|
10366
|
+
for (const arg of this.args) {
|
|
10367
|
+
if (arg) {
|
|
10368
|
+
if (isExpression(arg)) {
|
|
10369
|
+
const sql = arg.toSQL(ctx, quotedAs);
|
|
10370
|
+
if (sql)
|
|
10371
|
+
res.push(sql);
|
|
10372
|
+
} else {
|
|
10373
|
+
res.push(`(${arg.toSQL(ctx).text})`);
|
|
10374
|
+
}
|
|
10375
|
+
}
|
|
10376
|
+
}
|
|
10377
|
+
return `(${res.join(" OR ")})`;
|
|
10378
|
+
}
|
|
10379
|
+
}
|
|
10380
|
+
Object.assign(OrExpression.prototype, Operators.boolean);
|
|
10381
|
+
class ExpressionMethods {
|
|
10382
|
+
/**
|
|
10383
|
+
* `column` references a table column, this can be used in raw SQL or when building a column expression.
|
|
10384
|
+
* Only for referencing a column in the query's table. For referencing joined table's columns, see [ref](#ref).
|
|
10385
|
+
*
|
|
10386
|
+
* ```ts
|
|
10387
|
+
* await db.table.select({
|
|
10388
|
+
* // select `("table"."id" = 1 OR "table"."name" = 'name') AS "one"`,
|
|
10389
|
+
* // returns a boolean
|
|
10390
|
+
* one: (q) =>
|
|
10391
|
+
* q.sql<boolean>`${q.column('id')} = ${1} OR ${q.column('name')} = ${'name'}`,
|
|
10392
|
+
*
|
|
10393
|
+
* // selects the same as above, but by building a query
|
|
10394
|
+
* two: (q) => q.column('id').equals(1).or(q.column('name').equals('name')),
|
|
10395
|
+
* });
|
|
10396
|
+
* ```
|
|
10397
|
+
*
|
|
10398
|
+
* @param name - column name
|
|
10399
|
+
*/
|
|
10400
|
+
column(name) {
|
|
10401
|
+
const column = this.shape[name];
|
|
10402
|
+
return new ColumnRefExpression(
|
|
10403
|
+
column,
|
|
10404
|
+
name
|
|
10405
|
+
);
|
|
10406
|
+
}
|
|
10407
|
+
/**
|
|
10408
|
+
* `ref` is similar to [column](#column), but it also allows to reference a column of joined table,
|
|
10409
|
+
* and other dynamically defined columns.
|
|
10410
|
+
*
|
|
10411
|
+
* ```ts
|
|
10412
|
+
* await db.table.join('otherTable').select({
|
|
10413
|
+
* // select `("otherTable"."id" = 1 OR "otherTable"."name" = 'name') AS "one"`,
|
|
10414
|
+
* // returns a boolean
|
|
10415
|
+
* one: (q) =>
|
|
10416
|
+
* q.sql<boolean>`${q.ref('otherTable.id')} = ${1} OR ${q.ref(
|
|
10417
|
+
* 'otherTable.name',
|
|
10418
|
+
* )} = ${'name'}`,
|
|
10419
|
+
*
|
|
10420
|
+
* // selects the same as above, but by building a query
|
|
10421
|
+
* two: (q) =>
|
|
10422
|
+
* q
|
|
10423
|
+
* .ref('otherTable.id')
|
|
10424
|
+
* .equals(1)
|
|
10425
|
+
* .or(q.ref('otherTable.name').equals('name')),
|
|
10426
|
+
* });
|
|
10427
|
+
* ```
|
|
10428
|
+
*
|
|
10429
|
+
* @param arg - any available column name, such as of a joined table
|
|
10430
|
+
*/
|
|
10431
|
+
ref(arg) {
|
|
10432
|
+
const q = this.clone();
|
|
10433
|
+
const { shape } = q.q;
|
|
10434
|
+
let column;
|
|
10435
|
+
const index = arg.indexOf(".");
|
|
10436
|
+
if (index !== -1) {
|
|
10437
|
+
const table = arg.slice(0, index);
|
|
10438
|
+
const col = arg.slice(index + 1);
|
|
10439
|
+
if (table === (q.q.as || q.table)) {
|
|
10440
|
+
column = shape[col];
|
|
10441
|
+
} else {
|
|
10442
|
+
column = q.q.joinedShapes[table][col];
|
|
10443
|
+
}
|
|
10444
|
+
} else {
|
|
10445
|
+
column = shape[arg];
|
|
10446
|
+
}
|
|
10447
|
+
return new RefExpression(column, q.q, arg);
|
|
10448
|
+
}
|
|
10449
|
+
val(value) {
|
|
10450
|
+
return new ValExpression(value);
|
|
10451
|
+
}
|
|
10452
|
+
/**
|
|
10453
|
+
* `fn` allows to call an arbitrary SQL function.
|
|
10454
|
+
*
|
|
10455
|
+
* For example, calling `sqrt` function to get a square root from some numeric column:
|
|
10456
|
+
*
|
|
10457
|
+
* ```ts
|
|
10458
|
+
* const q = await User.select({
|
|
10459
|
+
* sqrt: (q) => q.fn<number>('sqrt', ['numericColumn']),
|
|
10460
|
+
* }).take();
|
|
10461
|
+
*
|
|
10462
|
+
* q.sqrt; // has type `number` just as provided
|
|
10463
|
+
* ```
|
|
10464
|
+
*
|
|
10465
|
+
* If this is an aggregate function, you can specify aggregation options (see [Aggregate](/guide/aggregate.html)) via third parameter.
|
|
10466
|
+
*
|
|
10467
|
+
* Use `type` method to specify a column type so that its operators such as `lt` and `gt` become available:
|
|
10468
|
+
*
|
|
10469
|
+
* ```ts
|
|
10470
|
+
* const q = await User.select({
|
|
10471
|
+
* // Produces `sqrt("numericColumn") > 5`
|
|
10472
|
+
* sqrtIsGreaterThan5: (q) =>
|
|
10473
|
+
* q
|
|
10474
|
+
* .fn('sqrt', ['numericColumn'])
|
|
10475
|
+
* .type((t) => t.float())
|
|
10476
|
+
* .gt(5),
|
|
10477
|
+
* }).take();
|
|
10478
|
+
*
|
|
10479
|
+
* // Return type is boolean | null
|
|
10480
|
+
* // todo: it should be just boolean if the column is not nullable, but for now it's always nullable
|
|
10481
|
+
* q.sqrtIsGreaterThan5;
|
|
10482
|
+
* ```
|
|
10483
|
+
*
|
|
10484
|
+
* @param fn
|
|
10485
|
+
* @param args
|
|
10486
|
+
* @param options
|
|
10487
|
+
*/
|
|
10488
|
+
fn(fn, args, options) {
|
|
10489
|
+
return makeFnExpression(this, emptyObject, fn, args, options);
|
|
10490
|
+
}
|
|
10491
|
+
or(...args) {
|
|
10492
|
+
return new OrExpression(args);
|
|
10493
|
+
}
|
|
10494
|
+
}
|
|
10495
|
+
|
|
10393
10496
|
const _queryAll = (q) => {
|
|
10394
10497
|
q.q.returnType = "all";
|
|
10395
10498
|
q.q.all = true;
|
|
@@ -10534,11 +10637,13 @@ class QueryMethods {
|
|
|
10534
10637
|
* db.table.distinct().select('name');
|
|
10535
10638
|
* ```
|
|
10536
10639
|
*
|
|
10537
|
-
* Can accept column names or raw expressions to place it to `DISTINCT ON (...)`:
|
|
10640
|
+
* Can accept column names or raw SQL expressions to place it to `DISTINCT ON (...)`:
|
|
10538
10641
|
*
|
|
10539
10642
|
* ```ts
|
|
10643
|
+
* import { sql } from './baseTable';
|
|
10644
|
+
*
|
|
10540
10645
|
* // Distinct on the name and raw SQL
|
|
10541
|
-
* db.table.distinct('name',
|
|
10646
|
+
* db.table.distinct('name', sql`raw sql`).select('id', 'name');
|
|
10542
10647
|
* ```
|
|
10543
10648
|
*
|
|
10544
10649
|
* @param columns - column names or a raw SQL
|
|
@@ -10698,9 +10803,11 @@ class QueryMethods {
|
|
|
10698
10803
|
* Also, it's possible to group by a selected value:
|
|
10699
10804
|
*
|
|
10700
10805
|
* ```ts
|
|
10806
|
+
* import { sql } from './baseTable';
|
|
10807
|
+
*
|
|
10701
10808
|
* const results = db.product
|
|
10702
10809
|
* .select({
|
|
10703
|
-
* month:
|
|
10810
|
+
* month: sql`extract(month from "createdAt")`.type((t) =>
|
|
10704
10811
|
* // month is returned as string, parse it to int
|
|
10705
10812
|
* t.string().parse(parseInt),
|
|
10706
10813
|
* ),
|
|
@@ -10804,8 +10911,6 @@ class QueryMethods {
|
|
|
10804
10911
|
*
|
|
10805
10912
|
* ```ts
|
|
10806
10913
|
* db.table.orderSql`raw sql`;
|
|
10807
|
-
* // or
|
|
10808
|
-
* db.table.orderSql(db.table.sql`raw sql`);
|
|
10809
10914
|
* ```
|
|
10810
10915
|
*
|
|
10811
10916
|
* @param args - SQL expression
|
|
@@ -10999,73 +11104,6 @@ class QueryMethods {
|
|
|
10999
11104
|
return fn(q, ...args);
|
|
11000
11105
|
};
|
|
11001
11106
|
}
|
|
11002
|
-
/**
|
|
11003
|
-
* `column` references a table column, this can be used in raw SQL or when building a column expression.
|
|
11004
|
-
* Only for referencing a column in the query's table. For referencing joined table's columns, see [ref](#ref).
|
|
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
|
-
* ```
|
|
11017
|
-
*
|
|
11018
|
-
* @param name - column name
|
|
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.
|
|
11030
|
-
*
|
|
11031
|
-
* ```ts
|
|
11032
|
-
* await db.table.join('otherTable').select({
|
|
11033
|
-
* // select `("otherTable"."id" = 1 OR "otherTable"."name" = 'name') AS "one"`,
|
|
11034
|
-
* // returns a boolean
|
|
11035
|
-
* one: (q) =>
|
|
11036
|
-
* q.sql<boolean>`${q.ref('otherTable.id')} = ${1} OR ${q.ref(
|
|
11037
|
-
* 'otherTable.name',
|
|
11038
|
-
* )} = ${'name'}`,
|
|
11039
|
-
*
|
|
11040
|
-
* // selects the same as above, but by building a query
|
|
11041
|
-
* two: (q) =>
|
|
11042
|
-
* q
|
|
11043
|
-
* .ref('otherTable.id')
|
|
11044
|
-
* .equals(1)
|
|
11045
|
-
* .or(q.ref('otherTable.name').equals('name')),
|
|
11046
|
-
* });
|
|
11047
|
-
* ```
|
|
11048
|
-
*
|
|
11049
|
-
* @param arg - any available column name, such as of a joined table
|
|
11050
|
-
*/
|
|
11051
|
-
ref(arg) {
|
|
11052
|
-
const q = this.clone();
|
|
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);
|
|
11068
|
-
}
|
|
11069
11107
|
/**
|
|
11070
11108
|
* Narrows a part of the query output type.
|
|
11071
11109
|
* Use with caution, type-safety isn't guaranteed with it.
|
|
@@ -11122,10 +11160,11 @@ applyMixins(QueryMethods, [
|
|
|
11122
11160
|
QueryUpsertOrCreate,
|
|
11123
11161
|
QueryGet,
|
|
11124
11162
|
MergeQueryMethods,
|
|
11125
|
-
|
|
11163
|
+
SqlMethod,
|
|
11126
11164
|
TransformMethods,
|
|
11127
11165
|
ScopeMethods,
|
|
11128
|
-
SoftDeleteMethods
|
|
11166
|
+
SoftDeleteMethods,
|
|
11167
|
+
ExpressionMethods
|
|
11129
11168
|
]);
|
|
11130
11169
|
|
|
11131
11170
|
const makeIndex = (columns, first, second) => {
|
|
@@ -11702,5 +11741,5 @@ function copyTableData(query, arg) {
|
|
|
11702
11741
|
return q;
|
|
11703
11742
|
}
|
|
11704
11743
|
|
|
11705
|
-
export { Adapter, AggregateMethods, ArrayColumn, AsMethods, BigIntColumn, BigSerialColumn, BitColumn, BitVaryingColumn, BooleanColumn, BoxColumn, ByteaColumn, CharColumn, CidrColumn, CircleColumn, CitextColumn, Clear, ColumnRefExpression, ColumnType, Create, CustomTypeColumn, DateBaseColumn, DateColumn, DateTimeBaseClass, DateTimeTzBaseClass, Db, DecimalColumn, Delete, DomainColumn, DoublePrecisionColumn, DynamicRawSQL, EnumColumn, FnExpression, For, From, Having, InetColumn, IntegerBaseColumn, IntegerColumn, IntervalColumn, JSONColumn, JSONTextColumn, Join, JsonMethods, JsonModifiers, LimitedTextBaseColumn, LineColumn, LsegColumn, MacAddr8Column, MacAddrColumn, MergeQueryMethods, MoneyColumn, MoreThanOneRowError, NotFoundError, NumberAsStringBaseColumn, NumberBaseColumn, OnConflictQueryBuilder, OnMethods, Operators, OrchidOrmError, OrchidOrmInternalError, PathColumn, PointColumn, PolygonColumn, QueryBase, QueryError, QueryGet, QueryHooks, QueryLog, QueryMethods, QueryUpsertOrCreate, RawSQL,
|
|
11744
|
+
export { Adapter, AggregateMethods, ArrayColumn, AsMethods, BigIntColumn, BigSerialColumn, BitColumn, BitVaryingColumn, BooleanColumn, BoxColumn, ByteaColumn, CharColumn, CidrColumn, CircleColumn, CitextColumn, Clear, ColumnRefExpression, ColumnType, Create, CustomTypeColumn, DateBaseColumn, DateColumn, DateTimeBaseClass, DateTimeTzBaseClass, Db, DecimalColumn, Delete, DomainColumn, DoublePrecisionColumn, DynamicRawSQL, EnumColumn, ExpressionMethods, FnExpression, For, From, Having, InetColumn, IntegerBaseColumn, IntegerColumn, IntervalColumn, JSONColumn, JSONTextColumn, Join, JsonMethods, JsonModifiers, LimitedTextBaseColumn, LineColumn, LsegColumn, MacAddr8Column, MacAddrColumn, MergeQueryMethods, MoneyColumn, MoreThanOneRowError, NotFoundError, NumberAsStringBaseColumn, NumberBaseColumn, OnConflictQueryBuilder, OnMethods, Operators, OrExpression, OrchidOrmError, OrchidOrmInternalError, PathColumn, PointColumn, PolygonColumn, QueryBase, QueryError, QueryGet, QueryHooks, QueryLog, QueryMethods, QueryUpsertOrCreate, RawSQL, RealColumn, RefExpression, SearchMethods, Select, SerialColumn, SmallIntColumn, SmallSerialColumn, SqlMethod, StringColumn, TextBaseColumn, TextColumn, Then, TimeColumn, TimestampColumn, TimestampTZColumn, Transaction, TransactionAdapter, TransformMethods, TsQueryColumn, TsVectorColumn, UUIDColumn, UnhandledTypeError, Union, UnknownColumn, Update, VarCharColumn, VirtualColumn, Where, With, XMLColumn, _initQueryBuilder, _queryAfterSaveCommit, _queryAll, _queryAs, _queryChangeCounter, _queryCreate, _queryCreateFrom, _queryCreateMany, _queryCreateManyFrom, _queryCreateManyRaw, _queryCreateRaw, _queryDefaults, _queryDelete, _queryExec, _queryFindBy, _queryFindByOptional, _queryGet, _queryGetOptional, _queryHookAfterCreate, _queryHookAfterCreateCommit, _queryHookAfterDelete, _queryHookAfterDeleteCommit, _queryHookAfterQuery, _queryHookAfterSave, _queryHookAfterUpdate, _queryHookAfterUpdateCommit, _queryHookBeforeCreate, _queryHookBeforeDelete, _queryHookBeforeQuery, _queryHookBeforeSave, _queryHookBeforeUpdate, _queryInsert, _queryInsertFrom, _queryInsertMany, _queryInsertManyFrom, _queryInsertManyRaw, _queryInsertRaw, _queryJoinOn, _queryJoinOnJsonPathEquals, _queryJoinOrOn, _queryOr, _queryOrNot, _queryRows, _querySelect, _queryTake, _queryTakeOptional, _queryUpdate, _queryUpdateOrThrow, _queryUpdateRaw, _queryWhere, _queryWhereExists, _queryWhereIn, _queryWhereNot, _queryWhereNotSql, _queryWhereSql, addComputedColumns, addParserForRawExpression, addParserForSelectItem, addQueryOn, anyShape, checkIfASimpleQuery, cloneQuery, cloneQueryBaseUnscoped, columnCheckToCode, columnCode, columnForeignKeysToCode, columnIndexesToCode, columnsShapeToCode, constraintInnerToCode, constraintToCode, copyTableData, countSelect, createDb, defaultSchemaConfig, extendQuery, foreignKeyArgumentToCode, getClonedQueryData, getColumnInfo, getColumnTypes, getPrimaryKeys, getQueryAs, getShapeFromSelect, handleResult, identityToCode, indexInnerToCode, indexToCode, instantiateColumn, isDefaultTimeStamp, isQueryReturnsAll, isSelectingCount, joinSubQuery, logColors, logParamToLogObject, makeColumnTypes, makeColumnsByType, makeFnExpression, makeRegexToFindInSql, makeSQL, parseRecord, parseResult, parseTableData, parseTableDataInput, primaryKeyInnerToCode, processSelectArg, pushLimitSQL, pushQueryArray, pushQueryOn, pushQueryOrOn, pushQueryValue, pushTableDataCode, queryFrom, queryFromSql, queryJson, queryMethodByReturnType, queryTypeWithLimitOne, queryWrap, quote, quoteString, raw, referencesArgsToCode, resolveSubQueryCallback, saveSearchAlias, setParserForSelectedString, setQueryObjectValue, setQueryOperators, simplifyColumnDefault, sqlFn, sqlQueryArgsToExpression, tableDataMethods, templateLiteralToSQL, testTransaction, throwIfNoWhere, toSQL, toSQLCacheKey };
|
|
11706
11745
|
//# sourceMappingURL=index.mjs.map
|