Serafim
  • Serafim
  • docs
    • Operators
    • OR & AND
Powered by GitBook
On this page
  1. docs

OR & AND

Both OR and AND can be used to query custom data:

Using OR & AND:

const search: Search = {
  where: [
    AND(
      {
        field: "storeId",
        operation: OperationTypes.EQUAL,
        value: 1,
      },
      OR(
        AND(
          {
            field: "dateStart",
            operation: OperationTypes.LESS_EQUAL,
            value: '2025-01-01',
          },
          {
            field: "dateEnd",
            operation: OperationTypes.GREATER_EQUAL,
            value: '2025-01-31',
          }
        ),
        AND(
          {
            field: "dateStart",
            operation: OperationTypes.EQUAL,
            value: null,
          },
          {
            field: "dateEnd",
            operation: OperationTypes.EQUAL,
            value: null,
          }
        )
      )
    ),
  ],
};

is equivalent in TypeORM to (you have to use cartesian products because of TypeORM's documentation on OR & AND):

const where = [
  {
    storeId: 1,
    dateStart: Raw(alias => `${alias} <= '2025-01-01'`),
    dateEnd: Raw(alias => `${alias} >= '2025-01-31'`),
  },
  {
    storeId: 1,
    dateStart: IsNull(),
    dateEnd: IsNull(),
  },
  ]

will execute following query:

SELECT * 
FROM coupon
WHERE storeId = 1
  AND (
    (
      dateStart <= '2025-01-01' 
      AND dateEnd >= '2025-01-31'
    )
    OR (
      dateStart IS NULL 
      AND dateEnd IS NULL
    )
  );
PreviousOperators

Last updated 3 months ago