pqb 0.30.1 → 0.30.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +262 -222
- package/dist/index.js +316 -293
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +288 -266
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.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) {
|
|
@@ -2019,7 +2015,7 @@ const processAnds = (and, ctx, table, query, quotedAs, parens) => {
|
|
|
2019
2015
|
return parens && ands.length > 1 ? `(${sql})` : sql;
|
|
2020
2016
|
};
|
|
2021
2017
|
const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
2022
|
-
var _a, _b;
|
|
2018
|
+
var _a, _b, _c, _d;
|
|
2023
2019
|
if (typeof data === "function") {
|
|
2024
2020
|
const qb = Object.create(table);
|
|
2025
2021
|
qb.q = getClonedQueryData(query);
|
|
@@ -2181,7 +2177,8 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2181
2177
|
column = quotedAs === quoted ? query.shape[name] : (_b = (_a = query.joinedShapes) == null ? void 0 : _a[table2]) == null ? void 0 : _b[name];
|
|
2182
2178
|
quotedColumn = simpleColumnToSQL(ctx, name, column, quoted);
|
|
2183
2179
|
} else {
|
|
2184
|
-
|
|
2180
|
+
column = (_d = (_c = query.joinedShapes) == null ? void 0 : _c[key]) == null ? void 0 : _d.value;
|
|
2181
|
+
quotedColumn = `"${key}".r`;
|
|
2185
2182
|
}
|
|
2186
2183
|
if (!column || !quotedColumn) {
|
|
2187
2184
|
throw new Error(`Unknown column ${key} provided to condition`);
|
|
@@ -3454,6 +3451,8 @@ const getShapeFromSelect = (q, isSubQuery) => {
|
|
|
3454
3451
|
}
|
|
3455
3452
|
}
|
|
3456
3453
|
}
|
|
3454
|
+
} else if (isExpression(item)) {
|
|
3455
|
+
result.value = item.result.value;
|
|
3457
3456
|
}
|
|
3458
3457
|
}
|
|
3459
3458
|
}
|
|
@@ -3685,7 +3684,6 @@ class From {
|
|
|
3685
3684
|
* ```ts
|
|
3686
3685
|
* const value = 123;
|
|
3687
3686
|
* db.table.fromSql`value = ${value}`;
|
|
3688
|
-
* db.table.fromSql(db.table.sql`value = ${value}`);
|
|
3689
3687
|
* ```
|
|
3690
3688
|
*
|
|
3691
3689
|
* @param args - SQL expression
|
|
@@ -5409,7 +5407,8 @@ class FnExpression extends Expression {
|
|
|
5409
5407
|
}
|
|
5410
5408
|
}
|
|
5411
5409
|
function makeFnExpression(self, type, fn, args, options) {
|
|
5412
|
-
const q = self.
|
|
5410
|
+
const q = extendQuery(self, type.operators);
|
|
5411
|
+
q.baseQuery.type = ExpressionTypeMethod.prototype.type;
|
|
5413
5412
|
new FnExpression(
|
|
5414
5413
|
q,
|
|
5415
5414
|
fn,
|
|
@@ -5431,49 +5430,6 @@ nullableInt.parseItem = nullableInt.parseFn = (input) => input === null ? null :
|
|
|
5431
5430
|
const nullableFloat = new RealColumn(defaultSchemaConfig);
|
|
5432
5431
|
nullableFloat.parseItem = nullableFloat.parseFn = (input) => input === null ? null : parseFloat(input);
|
|
5433
5432
|
class AggregateMethods {
|
|
5434
|
-
/**
|
|
5435
|
-
* `fn` allows to call an arbitrary SQL function.
|
|
5436
|
-
*
|
|
5437
|
-
* For example, calling `sqrt` function to get a square root from some numeric column:
|
|
5438
|
-
*
|
|
5439
|
-
* ```ts
|
|
5440
|
-
* const q = await User.select({
|
|
5441
|
-
* // todo: use type callback instead of generic type
|
|
5442
|
-
* sqrt: (q) => q.fn<number>('sqrt', ['numericColumn']),
|
|
5443
|
-
* }).take();
|
|
5444
|
-
*
|
|
5445
|
-
* q.sqrt; // has type `number` just as provided
|
|
5446
|
-
* ```
|
|
5447
|
-
*
|
|
5448
|
-
* If this is an aggregate function, you can specify aggregation options via third parameter.
|
|
5449
|
-
*
|
|
5450
|
-
* Forth parameter is for runtime column type. When specified, allows to chain the function with the column operators:
|
|
5451
|
-
*
|
|
5452
|
-
* ```ts
|
|
5453
|
-
* const q = await User.select({
|
|
5454
|
-
* // chain `sqrt("numericColumn")` with the "greater than 5"
|
|
5455
|
-
* sqrtIsGreaterThan5: (q) => q.fn('sqrt', ['numericColumn'], {}, (t) => t.float()).gt(5),
|
|
5456
|
-
* }).take();
|
|
5457
|
-
*
|
|
5458
|
-
* // Return type is boolean | null
|
|
5459
|
-
* // todo: it should be just boolean if the column is not nullable, but for now it's always nullable
|
|
5460
|
-
* q.sqrtIsGreaterThan5
|
|
5461
|
-
* ```
|
|
5462
|
-
*
|
|
5463
|
-
* @param fn
|
|
5464
|
-
* @param args
|
|
5465
|
-
* @param options
|
|
5466
|
-
* @param type
|
|
5467
|
-
*/
|
|
5468
|
-
fn(fn, args, options, type) {
|
|
5469
|
-
return makeFnExpression(
|
|
5470
|
-
this,
|
|
5471
|
-
(type == null ? void 0 : type(this.columnTypes)) || emptyObject,
|
|
5472
|
-
fn,
|
|
5473
|
-
args,
|
|
5474
|
-
options
|
|
5475
|
-
);
|
|
5476
|
-
}
|
|
5477
5433
|
/**
|
|
5478
5434
|
* Use `exists()` to check if there is at least one record-matching condition.
|
|
5479
5435
|
*
|
|
@@ -5818,7 +5774,7 @@ class AggregateMethods {
|
|
|
5818
5774
|
* // select a column with alias
|
|
5819
5775
|
* nameAlias: 'name',
|
|
5820
5776
|
* // select raw SQL with alias
|
|
5821
|
-
* foo:
|
|
5777
|
+
* foo: sql<string>`"bar" || "baz"`,
|
|
5822
5778
|
* },
|
|
5823
5779
|
* aggregateOptions,
|
|
5824
5780
|
* );
|
|
@@ -5828,7 +5784,7 @@ class AggregateMethods {
|
|
|
5828
5784
|
* object: (q) =>
|
|
5829
5785
|
* q.jsonObjectAgg({
|
|
5830
5786
|
* nameAlias: 'name',
|
|
5831
|
-
* foo:
|
|
5787
|
+
* foo: sql<string>`"bar" || "baz"`,
|
|
5832
5788
|
* }),
|
|
5833
5789
|
* });
|
|
5834
5790
|
* ```
|
|
@@ -6300,7 +6256,7 @@ class Create {
|
|
|
6300
6256
|
*
|
|
6301
6257
|
* await db.table.create({
|
|
6302
6258
|
* // raw SQL
|
|
6303
|
-
* column1:
|
|
6259
|
+
* column1: sql`'John' || ' ' || 'Doe'`,
|
|
6304
6260
|
*
|
|
6305
6261
|
* // query that returns a single value
|
|
6306
6262
|
* // returning multiple values will result in Postgres error
|
|
@@ -6363,7 +6319,7 @@ class Create {
|
|
|
6363
6319
|
* ```ts
|
|
6364
6320
|
* const oneRecord = await db.table.createRaw({
|
|
6365
6321
|
* columns: ['name', 'amount'],
|
|
6366
|
-
* values:
|
|
6322
|
+
* values: sql`'name', random()`,
|
|
6367
6323
|
* });
|
|
6368
6324
|
* ```
|
|
6369
6325
|
*
|
|
@@ -6392,7 +6348,7 @@ class Create {
|
|
|
6392
6348
|
* ```ts
|
|
6393
6349
|
* const manyRecords = await db.table.createManyRaw({
|
|
6394
6350
|
* columns: ['name', 'amount'],
|
|
6395
|
-
* values: [
|
|
6351
|
+
* values: [sql`'one', 2`, sql`'three', 4`],
|
|
6396
6352
|
* });
|
|
6397
6353
|
* ```
|
|
6398
6354
|
*
|
|
@@ -6546,11 +6502,11 @@ class Create {
|
|
|
6546
6502
|
* // raw SQL expression:
|
|
6547
6503
|
* db.table
|
|
6548
6504
|
* .create(data)
|
|
6549
|
-
* .onConfict(
|
|
6505
|
+
* .onConfict(sql`(email) where condition`)
|
|
6550
6506
|
* .merge();
|
|
6551
6507
|
* ```
|
|
6552
6508
|
*
|
|
6553
|
-
* You can use the
|
|
6509
|
+
* You can use the `sql` function exported from your `BaseTable` file in onConflict.
|
|
6554
6510
|
* It can be useful to specify a condition when you have a partial index:
|
|
6555
6511
|
*
|
|
6556
6512
|
* ```ts
|
|
@@ -6561,7 +6517,7 @@ class Create {
|
|
|
6561
6517
|
* active: true,
|
|
6562
6518
|
* })
|
|
6563
6519
|
* // ignore only when having conflicting email and when active is true.
|
|
6564
|
-
* .onConflict(
|
|
6520
|
+
* .onConflict(sql`(email) where active`)
|
|
6565
6521
|
* .ignore();
|
|
6566
6522
|
* ```
|
|
6567
6523
|
*
|
|
@@ -6661,7 +6617,7 @@ class OnConflictQueryBuilder {
|
|
|
6661
6617
|
* db.table
|
|
6662
6618
|
* .create(data)
|
|
6663
6619
|
* .onConflict()
|
|
6664
|
-
* .set(
|
|
6620
|
+
* .set(sql`raw SQL expression`);
|
|
6665
6621
|
*
|
|
6666
6622
|
* // update records only on certain conditions
|
|
6667
6623
|
* db.table
|
|
@@ -6835,16 +6791,20 @@ class For {
|
|
|
6835
6791
|
|
|
6836
6792
|
class QueryGet {
|
|
6837
6793
|
/**
|
|
6838
|
-
* `.get` returns a single value,
|
|
6839
|
-
*
|
|
6794
|
+
* `.get` returns a single value, adds `LIMIT 1` to the query, and accepts a column name or a raw SQL expression.
|
|
6795
|
+
*
|
|
6796
|
+
* `get` throws a `NotFoundError` when not found, and `getOptional` returns `undefined`.
|
|
6840
6797
|
*
|
|
6841
6798
|
* ```ts
|
|
6842
6799
|
* import { NumberColumn } from 'orchid-orm';
|
|
6800
|
+
* import { sql } from './baseTable';
|
|
6843
6801
|
*
|
|
6844
6802
|
* const firstName: string = await db.table.get('name');
|
|
6845
6803
|
*
|
|
6846
|
-
* const rawResult: number = await db.table.get(
|
|
6847
|
-
*
|
|
6804
|
+
* const rawResult: number = await db.table.get(sql((t) => t.integer())`1 + 1`);
|
|
6805
|
+
*
|
|
6806
|
+
* const firstNameOptional: string | undefined = await db.table.getOptional(
|
|
6807
|
+
* 'name',
|
|
6848
6808
|
* );
|
|
6849
6809
|
* ```
|
|
6850
6810
|
*
|
|
@@ -6932,7 +6892,7 @@ class Having {
|
|
|
6932
6892
|
* Provide SQL expression for the `HAVING` SQL statement:
|
|
6933
6893
|
*
|
|
6934
6894
|
* ```ts
|
|
6935
|
-
* db.table.
|
|
6895
|
+
* db.table.havingSql`count(*) >= ${10}`;
|
|
6936
6896
|
* ```
|
|
6937
6897
|
*
|
|
6938
6898
|
* @param args - SQL expression
|
|
@@ -7188,15 +7148,14 @@ class Join {
|
|
|
7188
7148
|
* profile: (q) => q.profile,
|
|
7189
7149
|
* });
|
|
7190
7150
|
*
|
|
7191
|
-
* // select posts with counts of comments, order by comments count
|
|
7151
|
+
* // select posts with counts of comments, filter and order by comments count
|
|
7192
7152
|
* // result type is Array<Post & { commentsCount: number }>
|
|
7193
7153
|
* await db.post
|
|
7194
7154
|
* .select('*', {
|
|
7195
7155
|
* commentsCount: (q) => q.comments.count(),
|
|
7196
7156
|
* })
|
|
7197
|
-
* .
|
|
7198
|
-
*
|
|
7199
|
-
* });
|
|
7157
|
+
* .where({ commentsCount: { gt: 10 } })
|
|
7158
|
+
* .order({ commentsCount: 'DESC' });
|
|
7200
7159
|
*
|
|
7201
7160
|
* // select authors with array of their book titles
|
|
7202
7161
|
* // result type is Array<Author & { books: string[] }>
|
|
@@ -7447,7 +7406,8 @@ class Join {
|
|
|
7447
7406
|
* ```ts
|
|
7448
7407
|
* db.user.join(
|
|
7449
7408
|
* db.message,
|
|
7450
|
-
*
|
|
7409
|
+
* // `sql` can be imported from your `BaseTable` file
|
|
7410
|
+
* sql`lower("message"."text") = lower("user"."name")`,
|
|
7451
7411
|
* );
|
|
7452
7412
|
* ```
|
|
7453
7413
|
*
|
|
@@ -7456,16 +7416,16 @@ class Join {
|
|
|
7456
7416
|
* ```ts
|
|
7457
7417
|
* db.user.join(
|
|
7458
7418
|
* db.message,
|
|
7459
|
-
*
|
|
7460
|
-
*
|
|
7419
|
+
* sql`lower("message"."text")`,
|
|
7420
|
+
* sql`lower("user"."name")`,
|
|
7461
7421
|
* );
|
|
7462
7422
|
*
|
|
7463
7423
|
* // with operator:
|
|
7464
7424
|
* db.user.join(
|
|
7465
7425
|
* db.message,
|
|
7466
|
-
*
|
|
7426
|
+
* sql`lower("message"."text")`,
|
|
7467
7427
|
* '!=',
|
|
7468
|
-
*
|
|
7428
|
+
* sql`lower("user"."name")`,
|
|
7469
7429
|
* );
|
|
7470
7430
|
* ```
|
|
7471
7431
|
*
|
|
@@ -7479,7 +7439,7 @@ class Join {
|
|
|
7479
7439
|
* 'message.userId': 'user.id',
|
|
7480
7440
|
*
|
|
7481
7441
|
* // value can be a raw SQL expression:
|
|
7482
|
-
* text:
|
|
7442
|
+
* text: sql`lower("user"."name")`,
|
|
7483
7443
|
* });
|
|
7484
7444
|
* ```
|
|
7485
7445
|
*
|
|
@@ -8269,7 +8229,7 @@ class With {
|
|
|
8269
8229
|
* id: columnTypes.integer(),
|
|
8270
8230
|
* name: columnTypes.text(3, 100),
|
|
8271
8231
|
* },
|
|
8272
|
-
*
|
|
8232
|
+
* sql`SELECT id, name FROM "someTable"`,
|
|
8273
8233
|
* );
|
|
8274
8234
|
*
|
|
8275
8235
|
* // accepts query:
|
|
@@ -8277,7 +8237,7 @@ class With {
|
|
|
8277
8237
|
*
|
|
8278
8238
|
* // accepts a callback for a query builder:
|
|
8279
8239
|
* db.table.with('alias', (qb) =>
|
|
8280
|
-
* qb.select({ one:
|
|
8240
|
+
* qb.select({ one: sql`1`.type((t) => t.integer()) }),
|
|
8281
8241
|
* );
|
|
8282
8242
|
*
|
|
8283
8243
|
* // All mentioned forms can accept options as a second argument:
|
|
@@ -8329,7 +8289,7 @@ class Union {
|
|
|
8329
8289
|
* SomeTable.select('id', 'name').union(
|
|
8330
8290
|
* [
|
|
8331
8291
|
* OtherTable.select('id', 'name'),
|
|
8332
|
-
*
|
|
8292
|
+
* sql`SELECT id, name FROM "thirdTable"`,
|
|
8333
8293
|
* ],
|
|
8334
8294
|
* true, // optional wrap parameter
|
|
8335
8295
|
* );
|
|
@@ -8524,7 +8484,8 @@ class Where {
|
|
|
8524
8484
|
* },
|
|
8525
8485
|
*
|
|
8526
8486
|
* // where column equals to raw SQL
|
|
8527
|
-
*
|
|
8487
|
+
* // import `sql` from your `BaseTable`
|
|
8488
|
+
* column: sql`sql expression`,
|
|
8528
8489
|
* });
|
|
8529
8490
|
* ```
|
|
8530
8491
|
*
|
|
@@ -8573,12 +8534,7 @@ class Where {
|
|
|
8573
8534
|
* `where` supports raw SQL:
|
|
8574
8535
|
*
|
|
8575
8536
|
* ```ts
|
|
8576
|
-
* db.table.where(
|
|
8577
|
-
*
|
|
8578
|
-
* // or
|
|
8579
|
-
* import { raw } from 'orchid-orm';
|
|
8580
|
-
*
|
|
8581
|
-
* db.table.where(raw`a = b`);
|
|
8537
|
+
* db.table.where(sql`a = b`);
|
|
8582
8538
|
* ```
|
|
8583
8539
|
*
|
|
8584
8540
|
* `where` can accept a callback with a specific query builder containing all "where" methods such as `where`, `orWhere`, `whereNot`, `whereIn`, `whereExists`:
|
|
@@ -8599,7 +8555,7 @@ class Where {
|
|
|
8599
8555
|
* db.table.where(
|
|
8600
8556
|
* { id: 1 },
|
|
8601
8557
|
* db.table.where({ name: 'John' }),
|
|
8602
|
-
*
|
|
8558
|
+
* sql`a = b`,
|
|
8603
8559
|
* );
|
|
8604
8560
|
* ```
|
|
8605
8561
|
*
|
|
@@ -8712,8 +8668,6 @@ class Where {
|
|
|
8712
8668
|
* All column operators can take a value of the same type as the column, a sub-query, or a raw SQL expression:
|
|
8713
8669
|
*
|
|
8714
8670
|
* ```ts
|
|
8715
|
-
* import { sql } from 'orchid-orm';
|
|
8716
|
-
*
|
|
8717
8671
|
* db.table.where({
|
|
8718
8672
|
* numericColumn: {
|
|
8719
8673
|
* // lower than 5
|
|
@@ -8762,7 +8716,7 @@ class Where {
|
|
|
8762
8716
|
* // WHERE "column" IN (SELECT "column" FROM "otherTable")
|
|
8763
8717
|
* in: OtherTable.select('column'),
|
|
8764
8718
|
*
|
|
8765
|
-
* in:
|
|
8719
|
+
* in: sql`('a', 'b')`,
|
|
8766
8720
|
* },
|
|
8767
8721
|
* });
|
|
8768
8722
|
* ```
|
|
@@ -8809,7 +8763,7 @@ class Where {
|
|
|
8809
8763
|
* between: [1, 10],
|
|
8810
8764
|
*
|
|
8811
8765
|
* // sub-query and raw SQL expression
|
|
8812
|
-
* between: [OtherTable.select('column').take(),
|
|
8766
|
+
* between: [OtherTable.select('column').take(), sql`2 + 2`],
|
|
8813
8767
|
* },
|
|
8814
8768
|
* });
|
|
8815
8769
|
* ```
|
|
@@ -8901,15 +8855,7 @@ class Where {
|
|
|
8901
8855
|
* Use a custom SQL expression in `WHERE` statement:
|
|
8902
8856
|
*
|
|
8903
8857
|
* ```ts
|
|
8904
|
-
* db.table.
|
|
8905
|
-
*
|
|
8906
|
-
* // or
|
|
8907
|
-
* db.table.where(db.table.sql`a = b`);
|
|
8908
|
-
*
|
|
8909
|
-
* // or
|
|
8910
|
-
* import { raw } from 'orchid-orm';
|
|
8911
|
-
*
|
|
8912
|
-
* db.table.where(raw`a = b`);
|
|
8858
|
+
* db.table.whereSql`a = b`;
|
|
8913
8859
|
* ```
|
|
8914
8860
|
*
|
|
8915
8861
|
* @param args - SQL expression
|
|
@@ -8942,10 +8888,10 @@ class Where {
|
|
|
8942
8888
|
);
|
|
8943
8889
|
}
|
|
8944
8890
|
/**
|
|
8945
|
-
* `
|
|
8891
|
+
* `whereNotSql` is a version of `whereNot` accepting SQL expression:
|
|
8946
8892
|
*
|
|
8947
8893
|
* ```ts
|
|
8948
|
-
* db.table.
|
|
8894
|
+
* db.table.whereNotSql`sql expression`
|
|
8949
8895
|
* ```
|
|
8950
8896
|
*
|
|
8951
8897
|
* @param args - SQL expression
|
|
@@ -9022,7 +8968,7 @@ class Where {
|
|
|
9022
8968
|
* It supports raw SQL expression:
|
|
9023
8969
|
*
|
|
9024
8970
|
* ```ts
|
|
9025
|
-
* db.table.whereIn(['id', 'name'],
|
|
8971
|
+
* db.table.whereIn(['id', 'name'], sql`((1, 'one'), (2, 'two'))`);
|
|
9026
8972
|
* ```
|
|
9027
8973
|
*/
|
|
9028
8974
|
whereIn(...args) {
|
|
@@ -9336,7 +9282,7 @@ class Update {
|
|
|
9336
9282
|
* column1: 123,
|
|
9337
9283
|
*
|
|
9338
9284
|
* // use raw SQL to update the column
|
|
9339
|
-
* column2:
|
|
9285
|
+
* column2: sql`2 + 2`,
|
|
9340
9286
|
*
|
|
9341
9287
|
* // use query that returns a single value
|
|
9342
9288
|
* // returning multiple values will result in Postgres error
|
|
@@ -9464,7 +9410,7 @@ class Update {
|
|
|
9464
9410
|
* const updatedCount = await db.table.find(1).updateRaw`name = ${value}`;
|
|
9465
9411
|
*
|
|
9466
9412
|
* // or update with `sql` function:
|
|
9467
|
-
* await db.table.find(1).updateRaw(
|
|
9413
|
+
* await db.table.find(1).updateRaw(sql`name = ${value}`);
|
|
9468
9414
|
* ```
|
|
9469
9415
|
* @param args - raw SQL via a template string or by using a `sql` method
|
|
9470
9416
|
*/
|
|
@@ -10147,7 +10093,7 @@ class QueryUpsertOrCreate {
|
|
|
10147
10093
|
}
|
|
10148
10094
|
}
|
|
10149
10095
|
|
|
10150
|
-
class
|
|
10096
|
+
class SqlMethod {
|
|
10151
10097
|
sql(...args) {
|
|
10152
10098
|
const sql = raw(...args);
|
|
10153
10099
|
sql.columnTypes = this.columnTypes;
|
|
@@ -10388,6 +10334,118 @@ class RefExpression extends Expression {
|
|
|
10388
10334
|
return columnToSql(ctx, this.q, this.q.shape, this.ref, quotedAs);
|
|
10389
10335
|
}
|
|
10390
10336
|
}
|
|
10337
|
+
class ExpressionMethods {
|
|
10338
|
+
/**
|
|
10339
|
+
* `column` references a table column, this can be used in raw SQL or when building a column expression.
|
|
10340
|
+
* Only for referencing a column in the query's table. For referencing joined table's columns, see [ref](#ref).
|
|
10341
|
+
*
|
|
10342
|
+
* ```ts
|
|
10343
|
+
* await db.table.select({
|
|
10344
|
+
* // select `("table"."id" = 1 OR "table"."name" = 'name') AS "one"`,
|
|
10345
|
+
* // returns a boolean
|
|
10346
|
+
* one: (q) =>
|
|
10347
|
+
* q.sql<boolean>`${q.column('id')} = ${1} OR ${q.column('name')} = ${'name'}`,
|
|
10348
|
+
*
|
|
10349
|
+
* // selects the same as above, but by building a query
|
|
10350
|
+
* two: (q) => q.column('id').equals(1).or(q.column('name').equals('name')),
|
|
10351
|
+
* });
|
|
10352
|
+
* ```
|
|
10353
|
+
*
|
|
10354
|
+
* @param name - column name
|
|
10355
|
+
*/
|
|
10356
|
+
column(name) {
|
|
10357
|
+
const column = this.shape[name];
|
|
10358
|
+
return new ColumnRefExpression(
|
|
10359
|
+
column,
|
|
10360
|
+
name
|
|
10361
|
+
);
|
|
10362
|
+
}
|
|
10363
|
+
/**
|
|
10364
|
+
* `ref` is similar to [column](#column), but it also allows to reference a column of joined table,
|
|
10365
|
+
* and other dynamically defined columns.
|
|
10366
|
+
*
|
|
10367
|
+
* ```ts
|
|
10368
|
+
* await db.table.join('otherTable').select({
|
|
10369
|
+
* // select `("otherTable"."id" = 1 OR "otherTable"."name" = 'name') AS "one"`,
|
|
10370
|
+
* // returns a boolean
|
|
10371
|
+
* one: (q) =>
|
|
10372
|
+
* q.sql<boolean>`${q.ref('otherTable.id')} = ${1} OR ${q.ref(
|
|
10373
|
+
* 'otherTable.name',
|
|
10374
|
+
* )} = ${'name'}`,
|
|
10375
|
+
*
|
|
10376
|
+
* // selects the same as above, but by building a query
|
|
10377
|
+
* two: (q) =>
|
|
10378
|
+
* q
|
|
10379
|
+
* .ref('otherTable.id')
|
|
10380
|
+
* .equals(1)
|
|
10381
|
+
* .or(q.ref('otherTable.name').equals('name')),
|
|
10382
|
+
* });
|
|
10383
|
+
* ```
|
|
10384
|
+
*
|
|
10385
|
+
* @param arg - any available column name, such as of a joined table
|
|
10386
|
+
*/
|
|
10387
|
+
ref(arg) {
|
|
10388
|
+
const q = this.clone();
|
|
10389
|
+
const { shape } = q.q;
|
|
10390
|
+
let column;
|
|
10391
|
+
const index = arg.indexOf(".");
|
|
10392
|
+
if (index !== -1) {
|
|
10393
|
+
const table = arg.slice(0, index);
|
|
10394
|
+
const col = arg.slice(index + 1);
|
|
10395
|
+
if (table === (q.q.as || q.table)) {
|
|
10396
|
+
column = shape[col];
|
|
10397
|
+
} else {
|
|
10398
|
+
column = q.q.joinedShapes[table][col];
|
|
10399
|
+
}
|
|
10400
|
+
} else {
|
|
10401
|
+
column = shape[arg];
|
|
10402
|
+
}
|
|
10403
|
+
return new RefExpression(column, q.q, arg);
|
|
10404
|
+
}
|
|
10405
|
+
val(value) {
|
|
10406
|
+
return new ValExpression(value);
|
|
10407
|
+
}
|
|
10408
|
+
/**
|
|
10409
|
+
* `fn` allows to call an arbitrary SQL function.
|
|
10410
|
+
*
|
|
10411
|
+
* For example, calling `sqrt` function to get a square root from some numeric column:
|
|
10412
|
+
*
|
|
10413
|
+
* ```ts
|
|
10414
|
+
* const q = await User.select({
|
|
10415
|
+
* sqrt: (q) => q.fn<number>('sqrt', ['numericColumn']),
|
|
10416
|
+
* }).take();
|
|
10417
|
+
*
|
|
10418
|
+
* q.sqrt; // has type `number` just as provided
|
|
10419
|
+
* ```
|
|
10420
|
+
*
|
|
10421
|
+
* If this is an aggregate function, you can specify aggregation options (see [Aggregate](/guide/aggregate.html)) via third parameter.
|
|
10422
|
+
*
|
|
10423
|
+
* Use `type` method to specify a column type so that its operators such as `lt` and `gt` become available:
|
|
10424
|
+
*
|
|
10425
|
+
* ```ts
|
|
10426
|
+
* const q = await User.select({
|
|
10427
|
+
* // Produces `sqrt("numericColumn") > 5`
|
|
10428
|
+
* sqrtIsGreaterThan5: (q) =>
|
|
10429
|
+
* q
|
|
10430
|
+
* .fn('sqrt', ['numericColumn'])
|
|
10431
|
+
* .type((t) => t.float())
|
|
10432
|
+
* .gt(5),
|
|
10433
|
+
* }).take();
|
|
10434
|
+
*
|
|
10435
|
+
* // Return type is boolean | null
|
|
10436
|
+
* // todo: it should be just boolean if the column is not nullable, but for now it's always nullable
|
|
10437
|
+
* q.sqrtIsGreaterThan5;
|
|
10438
|
+
* ```
|
|
10439
|
+
*
|
|
10440
|
+
* @param fn
|
|
10441
|
+
* @param args
|
|
10442
|
+
* @param options
|
|
10443
|
+
*/
|
|
10444
|
+
fn(fn, args, options) {
|
|
10445
|
+
return makeFnExpression(this, emptyObject, fn, args, options);
|
|
10446
|
+
}
|
|
10447
|
+
}
|
|
10448
|
+
|
|
10391
10449
|
const _queryAll = (q) => {
|
|
10392
10450
|
q.q.returnType = "all";
|
|
10393
10451
|
q.q.all = true;
|
|
@@ -10532,11 +10590,13 @@ class QueryMethods {
|
|
|
10532
10590
|
* db.table.distinct().select('name');
|
|
10533
10591
|
* ```
|
|
10534
10592
|
*
|
|
10535
|
-
* Can accept column names or raw expressions to place it to `DISTINCT ON (...)`:
|
|
10593
|
+
* Can accept column names or raw SQL expressions to place it to `DISTINCT ON (...)`:
|
|
10536
10594
|
*
|
|
10537
10595
|
* ```ts
|
|
10596
|
+
* import { sql } from './baseTable';
|
|
10597
|
+
*
|
|
10538
10598
|
* // Distinct on the name and raw SQL
|
|
10539
|
-
* db.table.distinct('name',
|
|
10599
|
+
* db.table.distinct('name', sql`raw sql`).select('id', 'name');
|
|
10540
10600
|
* ```
|
|
10541
10601
|
*
|
|
10542
10602
|
* @param columns - column names or a raw SQL
|
|
@@ -10696,9 +10756,11 @@ class QueryMethods {
|
|
|
10696
10756
|
* Also, it's possible to group by a selected value:
|
|
10697
10757
|
*
|
|
10698
10758
|
* ```ts
|
|
10759
|
+
* import { sql } from './baseTable';
|
|
10760
|
+
*
|
|
10699
10761
|
* const results = db.product
|
|
10700
10762
|
* .select({
|
|
10701
|
-
* month:
|
|
10763
|
+
* month: sql`extract(month from "createdAt")`.type((t) =>
|
|
10702
10764
|
* // month is returned as string, parse it to int
|
|
10703
10765
|
* t.string().parse(parseInt),
|
|
10704
10766
|
* ),
|
|
@@ -10802,8 +10864,6 @@ class QueryMethods {
|
|
|
10802
10864
|
*
|
|
10803
10865
|
* ```ts
|
|
10804
10866
|
* db.table.orderSql`raw sql`;
|
|
10805
|
-
* // or
|
|
10806
|
-
* db.table.orderSql(db.table.sql`raw sql`);
|
|
10807
10867
|
* ```
|
|
10808
10868
|
*
|
|
10809
10869
|
* @param args - SQL expression
|
|
@@ -10998,71 +11058,32 @@ class QueryMethods {
|
|
|
10998
11058
|
};
|
|
10999
11059
|
}
|
|
11000
11060
|
/**
|
|
11001
|
-
*
|
|
11002
|
-
*
|
|
11003
|
-
*
|
|
11004
|
-
* ```ts
|
|
11005
|
-
* await db.table.select({
|
|
11006
|
-
* // select `("table"."id" = 1 OR "table"."name" = 'name') AS "one"`,
|
|
11007
|
-
* // returns a boolean
|
|
11008
|
-
* one: (q) =>
|
|
11009
|
-
* q.sql<boolean>`${q.column('id')} = ${1} OR ${q.column('name')} = ${'name'}`,
|
|
11010
|
-
*
|
|
11011
|
-
* // selects the same as above, but by building a query
|
|
11012
|
-
* two: (q) => q.column('id').equals(1).or(q.column('name').equals('name')),
|
|
11013
|
-
* });
|
|
11014
|
-
* ```
|
|
11061
|
+
* Narrows a part of the query output type.
|
|
11062
|
+
* Use with caution, type-safety isn't guaranteed with it.
|
|
11063
|
+
* This is similar so using `as` keyword from TypeScript, except that it applies only to a part of the result.
|
|
11015
11064
|
*
|
|
11016
|
-
*
|
|
11017
|
-
*/
|
|
11018
|
-
column(name) {
|
|
11019
|
-
const column = this.shape[name];
|
|
11020
|
-
return new ColumnRefExpression(
|
|
11021
|
-
column,
|
|
11022
|
-
name
|
|
11023
|
-
);
|
|
11024
|
-
}
|
|
11025
|
-
/**
|
|
11026
|
-
* `ref` is similar to [column](#column), but it also allows to reference a column of joined table,
|
|
11027
|
-
* and other dynamically defined columns.
|
|
11065
|
+
* The syntax `()<{ ... }>()` is enforced by internal limitations.
|
|
11028
11066
|
*
|
|
11029
11067
|
* ```ts
|
|
11030
|
-
*
|
|
11031
|
-
* //
|
|
11032
|
-
*
|
|
11033
|
-
*
|
|
11034
|
-
*
|
|
11035
|
-
* 'otherTable.name',
|
|
11036
|
-
* )} = ${'name'}`,
|
|
11068
|
+
* const rows = db.table
|
|
11069
|
+
* // filter out records where the `nullableColumn` is null
|
|
11070
|
+
* .where({ nullableColumn: { not: null } });
|
|
11071
|
+
* // narrows only a specified column, the rest of result is unchanged
|
|
11072
|
+
* .narrowType()<{ nullableColumn: string }>()
|
|
11037
11073
|
*
|
|
11038
|
-
*
|
|
11039
|
-
*
|
|
11040
|
-
* q
|
|
11041
|
-
* .ref('otherTable.id')
|
|
11042
|
-
* .equals(1)
|
|
11043
|
-
* .or(q.ref('otherTable.name').equals('name')),
|
|
11044
|
-
* });
|
|
11045
|
-
* ```
|
|
11074
|
+
* // the column had type `string | null`, now it is `string`
|
|
11075
|
+
* rows[0].nullableColumn
|
|
11046
11076
|
*
|
|
11047
|
-
*
|
|
11077
|
+
* // imagine that table has a enum column kind with variants 'first' | 'second'
|
|
11078
|
+
* // and a boolean `approved`
|
|
11079
|
+
* db.table
|
|
11080
|
+
* .where({ kind: 'first', approved: true })
|
|
11081
|
+
* // after applying such `where`, it's safe to narrow the type to receive the literal values
|
|
11082
|
+
* .narrowType()<{ kind: 'first', approved: true }>();
|
|
11083
|
+
* ```
|
|
11048
11084
|
*/
|
|
11049
|
-
|
|
11050
|
-
|
|
11051
|
-
const { shape } = q.q;
|
|
11052
|
-
let column;
|
|
11053
|
-
const index = arg.indexOf(".");
|
|
11054
|
-
if (index !== -1) {
|
|
11055
|
-
const table = arg.slice(0, index);
|
|
11056
|
-
const col = arg.slice(index + 1);
|
|
11057
|
-
if (table === (q.q.as || q.table)) {
|
|
11058
|
-
column = shape[col];
|
|
11059
|
-
} else {
|
|
11060
|
-
column = q.q.joinedShapes[table][col];
|
|
11061
|
-
}
|
|
11062
|
-
} else {
|
|
11063
|
-
column = shape[arg];
|
|
11064
|
-
}
|
|
11065
|
-
return new RefExpression(column, q.q, arg);
|
|
11085
|
+
narrowType() {
|
|
11086
|
+
return () => this;
|
|
11066
11087
|
}
|
|
11067
11088
|
}
|
|
11068
11089
|
applyMixins(QueryMethods, [
|
|
@@ -11092,10 +11113,11 @@ applyMixins(QueryMethods, [
|
|
|
11092
11113
|
QueryUpsertOrCreate,
|
|
11093
11114
|
QueryGet,
|
|
11094
11115
|
MergeQueryMethods,
|
|
11095
|
-
|
|
11116
|
+
SqlMethod,
|
|
11096
11117
|
TransformMethods,
|
|
11097
11118
|
ScopeMethods,
|
|
11098
|
-
SoftDeleteMethods
|
|
11119
|
+
SoftDeleteMethods,
|
|
11120
|
+
ExpressionMethods
|
|
11099
11121
|
]);
|
|
11100
11122
|
|
|
11101
11123
|
const makeIndex = (columns, first, second) => {
|
|
@@ -11672,5 +11694,5 @@ function copyTableData(query, arg) {
|
|
|
11672
11694
|
return q;
|
|
11673
11695
|
}
|
|
11674
11696
|
|
|
11675
|
-
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,
|
|
11697
|
+
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, 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 };
|
|
11676
11698
|
//# sourceMappingURL=index.mjs.map
|