@@ -29,14 +29,15 @@ import { QueryExecutor } from '../query-executor/query-executor.js'
2929import {
3030 BinaryOperatorExpression ,
3131 ComparisonOperatorExpression ,
32+ FilterObject ,
3233 OperandValueExpression ,
3334 OperandValueExpressionOrList ,
35+ parseFilterList ,
36+ parseFilterObject ,
3437 parseValueBinaryOperation ,
3538 parseValueBinaryOperationOrExpression ,
3639} from '../parser/binary-operation-parser.js'
3740import { Expression } from './expression.js'
38- import { AndNode } from '../operation-node/and-node.js'
39- import { OrNode } from '../operation-node/or-node.js'
4041import { ParensNode } from '../operation-node/parens-node.js'
4142import { ExpressionWrapper } from './expression-wrapper.js'
4243import {
@@ -51,10 +52,9 @@ import {
5152 parseValueExpressionOrList ,
5253} from '../parser/value-parser.js'
5354import { NOOP_QUERY_EXECUTOR } from '../query-executor/noop-query-executor.js'
54- import { ValueNode } from '../operation-node/value-node.js'
5555import { CaseBuilder } from '../query-builder/case-builder.js'
5656import { CaseNode } from '../operation-node/case-node.js'
57- import { isUndefined } from '../util/object-utils.js'
57+ import { isReadonlyArray , isUndefined } from '../util/object-utils.js'
5858import { JSONPathBuilder } from '../query-builder/json-path-builder.js'
5959
6060export interface ExpressionBuilder < DB , TB extends keyof DB > {
@@ -577,15 +577,42 @@ export interface ExpressionBuilder<DB, TB extends keyof DB> {
577577 * ```sql
578578 * select "person".*
579579 * from "person"
580- * where "first_name" = $1
581- * and "first_name" = $2
582- * and "first_name" = $3
580+ * where (
581+ * "first_name" = $1
582+ * and "first_name" = $2
583+ * and "first_name" = $3
584+ * )
585+ * ```
586+ *
587+ * Optionally you can use the simpler object notation if you only need
588+ * equality comparisons:
589+ *
590+ * ```ts
591+ * db.selectFrom('person')
592+ * .selectAll('person')
593+ * .where((eb) => eb.and({
594+ * first_name: 'Jennifer',
595+ * last_name: 'Aniston'
596+ * }))
597+ * ```
598+ *
599+ * The generated SQL (PostgreSQL):
600+ *
601+ * ```sql
602+ * select "person".*
603+ * from "person"
604+ * where (
605+ * "first_name" = $1
606+ * and "last_name" = $2
607+ * )
583608 * ```
584609 */
585610 and (
586611 exprs : ReadonlyArray < Expression < SqlBool > >
587612 ) : ExpressionWrapper < DB , TB , SqlBool >
588613
614+ and ( exprs : Readonly < FilterObject < DB , TB > > ) : ExpressionWrapper < DB , TB , SqlBool >
615+
589616 /**
590617 * Combines two or more expressions using the logical `or` operator.
591618 *
@@ -600,7 +627,7 @@ export interface ExpressionBuilder<DB, TB extends keyof DB> {
600627 * ```ts
601628 * db.selectFrom('person')
602629 * .selectAll('person')
603- * .where((eb) => or([
630+ * .where((eb) => eb. or([
604631 * eb('first_name', '=', 'Jennifer'),
605632 * eb('fist_name', '=', 'Arnold'),
606633 * eb('fist_name', '=', 'Sylvester')
@@ -612,15 +639,42 @@ export interface ExpressionBuilder<DB, TB extends keyof DB> {
612639 * ```sql
613640 * select "person".*
614641 * from "person"
615- * where "first_name" = $1
616- * or "first_name" = $2
617- * or "first_name" = $3
642+ * where (
643+ * "first_name" = $1
644+ * or "first_name" = $2
645+ * or "first_name" = $3
646+ * )
647+ * ```
648+ *
649+ * Optionally you can use the simpler object notation if you only need
650+ * equality comparisons:
651+ *
652+ * ```ts
653+ * db.selectFrom('person')
654+ * .selectAll('person')
655+ * .where((eb) => eb.or({
656+ * first_name: 'Jennifer',
657+ * last_name: 'Aniston'
658+ * }))
659+ * ```
660+ *
661+ * The generated SQL (PostgreSQL):
662+ *
663+ * ```sql
664+ * select "person".*
665+ * from "person"
666+ * where (
667+ * "first_name" = $1
668+ * or "last_name" = $2
669+ * )
618670 * ```
619671 */
620672 or (
621673 exprs : ReadonlyArray < Expression < SqlBool > >
622674 ) : ExpressionWrapper < DB , TB , SqlBool >
623675
676+ or ( exprs : Readonly < FilterObject < DB , TB > > ) : ExpressionWrapper < DB , TB , SqlBool >
677+
624678 /**
625679 * Wraps the expression in parentheses.
626680 *
@@ -812,45 +866,23 @@ export function createExpressionBuilder<DB, TB extends keyof DB>(
812866 } ,
813867
814868 and (
815- exprs : ReadonlyArray < Expression < SqlBool > >
869+ exprs : ReadonlyArray < Expression < SqlBool > > | Readonly < FilterObject < DB , TB > >
816870 ) : ExpressionWrapper < DB , TB , SqlBool > {
817- if ( exprs . length === 0 ) {
818- return new ExpressionWrapper ( ValueNode . createImmediate ( true ) )
819- } else if ( exprs . length === 1 ) {
820- return new ExpressionWrapper ( exprs [ 0 ] . toOperationNode ( ) )
821- }
822-
823- let node = AndNode . create (
824- exprs [ 0 ] . toOperationNode ( ) ,
825- exprs [ 1 ] . toOperationNode ( )
826- )
827-
828- for ( let i = 2 ; i < exprs . length ; ++ i ) {
829- node = AndNode . create ( node , exprs [ i ] . toOperationNode ( ) )
871+ if ( isReadonlyArray ( exprs ) ) {
872+ return new ExpressionWrapper ( parseFilterList ( exprs , 'and' ) )
830873 }
831874
832- return new ExpressionWrapper ( ParensNode . create ( node ) )
875+ return new ExpressionWrapper ( parseFilterObject ( exprs , 'and' ) )
833876 } ,
834877
835878 or (
836- exprs : ReadonlyArray < Expression < SqlBool > >
879+ exprs : ReadonlyArray < Expression < SqlBool > > | Readonly < FilterObject < DB , TB > >
837880 ) : ExpressionWrapper < DB , TB , SqlBool > {
838- if ( exprs . length === 0 ) {
839- return new ExpressionWrapper ( ValueNode . createImmediate ( false ) )
840- } else if ( exprs . length === 1 ) {
841- return new ExpressionWrapper ( exprs [ 0 ] . toOperationNode ( ) )
842- }
843-
844- let node = OrNode . create (
845- exprs [ 0 ] . toOperationNode ( ) ,
846- exprs [ 1 ] . toOperationNode ( )
847- )
848-
849- for ( let i = 2 ; i < exprs . length ; ++ i ) {
850- node = OrNode . create ( node , exprs [ i ] . toOperationNode ( ) )
881+ if ( isReadonlyArray ( exprs ) ) {
882+ return new ExpressionWrapper ( parseFilterList ( exprs , 'or' ) )
851883 }
852884
853- return new ExpressionWrapper ( ParensNode . create ( node ) )
885+ return new ExpressionWrapper ( parseFilterObject ( exprs , 'or' ) )
854886 } ,
855887
856888 parens ( ...args : any [ ] ) {
0 commit comments