> ## Documentation Index
> Fetch the complete documentation index at: https://nestjs-query.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Queries

> The core of  is the , it is used by ,  ,  and .

The query interface contains three optional fields.

* `filter`
* `paging`
* `sorting`

All examples will be based on the following class.

```ts theme={null}
interface MyClass {

  title: string
  completed: boolean
  age: number
}
```

## Filtering[​](#filtering "Direct link to Filtering")

The `filter` field allows the filtering of fields based on the shape of the object the filter is used for.

See the [filter reference](#filter-reference) for a complete list of comparisons available.

<Note>
  The `Filter` interface is typesafe and the typescript compiler will complain if you include extra fields that are not present on
  the type you are creating the query for.
</Note>

Lets create a simple filter that would allow us to filter for titles equal to `'Foo Bar'`

### Simple[​](#simple "Direct link to Simple")

```ts theme={null}
import { Query } from '@ptc-org/nestjs-query-core'

const q: Query<MyClass> = {
  filter: {
    title: { eq: 'Foo Bar' }
  }
}
```

### Multiple Fields[​](#multiple-fields "Direct link to Multiple Fields")

You can also filter on multiple fields.

```ts theme={null}
import { Query } from '@ptc-org/nestjs-query-core'

const q: Query<MyClass> = {
  filter: {
    // title = 'Foo Bar' AND completed IS TRUE and age > 10
    title: { eq: 'Foo Bar' },
    completed: { is: true },
    age: { gt: 10 }
  }
}
```

### Multiple Comparisons on a single field.[​](#multiple-comparisons-on-a-single-field "Direct link to Multiple Comparisons on a single field.")

If you include multiple comparisons for a single field they will be ORed together.

```ts theme={null}
import { Query } from '@ptc-org/nestjs-query-core'

const q: Query<MyClass> = {
  filter: {
    // title = 'Foo Bar' OR field LIKE '%foo%'
    title: { eq: 'Foo Bar', like: '%foo%' }
  }
}
```

### And/Or[​](#andor "Direct link to And/Or")

The filter also allows for more complex `and` and `or` filters. The `and` and `or` accept an array of filters allowing for nested complex queries.

In this example we `AND` two filters for the same property together: `age >= 10 AND age <= 20`.

```ts theme={null}
const q: Query<MyClass> = {
  filter: {
    and: [{ age: { gte: 10 } }, { age: { lte: 20 } }]
  }
}
```

In this example a simple `OR` condition is created: `age >= 10 OR title NOT LIKE '%bar'`

```ts theme={null}
const q: Query<MyClass> = {
  filter: {
    or: [{ age: { gte: 10 } }, { title: { notLike: '%bar' } }]
  }
}
```

This example combines `AND` and `OR` filters: `age >= 10 AND (title LIKE '%bar' OR title = 'foobar')`.

```ts theme={null}
const q: Query<MyClass> = {
  filter: {
    and: [
      { age: { gte: 10 } },
      {
        or: [{ title: { like: '%bar' } }, { title: { eq: 'foobar' } }]
      }
    ]
  }
}
```

***

## Paging[​](#paging "Direct link to Paging")

The `core` package defines a basic paging interface has two optional fields `limit` and `offset`.

<Tabs>
  <Tab title="Limit And Offset">
    ```ts theme={null}
    const q: Query<MyClass> = {
      paging: {
        limit: 10,
        offset: 10,
      },
    };
    ```
  </Tab>

  <Tab title="Limit">
    ```ts theme={null}
    const q: Query<MyClass> = {
      paging: {
        limit: 20,
      },
    };
    ```
  </Tab>

  <Tab title="Offset">
    ```ts theme={null}
    const q: Query<MyClass> = {
      paging: {
        offset: 10,
      },
    };
    ```
  </Tab>
</Tabs>

***

## Sorting[​](#sorting "Direct link to Sorting")

The `sorting` field allows to specify the sort order for your query.

The `sorting` field is an array of object containing:

* `field` - the field to sort on
* `direction` - `ASC` or `DESC`
* `nulls?` - Optional nulls sort, `NULLS_FIRST` or `NULLS_LAST`

<Tabs>
  <Tab title="Single-Sort">
    ```ts theme={null}
    // import { SortDirection } from '@ptc-org/nestjs-query-core';
    const q: Query<MyClass> = {
      sorting: [{ field: 'title', direction: SortDirection.DESC }],
    };
    ```
  </Tab>

  <Tab title="Multi-Sort">
    ```ts theme={null}
    // import { SortDirection } from '@ptc-org/nestjs-query-core';
    const q: Query<MyClass> = {
      sorting: [
        { field: 'title', direction: SortDirection.DESC },
        { field: 'age', direction: SortDirection.ASC },
      ],
    };
    ```
  </Tab>
</Tabs>

## Filter Reference[​](#filter-reference "Direct link to Filter Reference")

The `filter` option supports the following field comparisons.

<Note>
  The following examples show an approximation of the SQL that will be generated. The ORM will take care of handling the dialect
  specifics
</Note>

### Common Comparisons[​](#common-comparisons "Direct link to Common Comparisons")

All types support the following comparisons.

* `is` - Check is a field is `null`, `true` or `false`.
  ```ts theme={null}
  // title IS NULL
  {
    title: {
      is: null
    }
  }
  // completed IS TRUE
  {
    completed: {
      is: true
    }
  }
  // completed IS false
  {
    completed: {
      is: false
    }
  }
  ```
* `isNot` - Check is a field is not `null`, `true` or `false`.
  ```ts theme={null}
  // title IS NOT NULL
  {
    title: {
      isNot: null
    }
  }
  // completed IS NOT TRUE
  {
    completed: {
      isNot: true
    }
  }
  // completed IS NOT false
  {
    completed: {
      isNot: false
    }
  }
  ```
* `neq` - field is not equal to a value.
  ```ts theme={null}
  // title != 'foo'
  {
    title: {
      neq: 'foo'
    }
  }
  ```
* `gt` - field is greater than a value.
  ```ts theme={null}
  // title > 'foo'
  {
    title: {
      gt: 'foo'
    }
  }
  ```
* `gte` - field is greater than or equal to a value.
  ```ts theme={null}
  // title >= 'foo'
  {
    title: {
      gte: 'foo'
    }
  }
  ```
* `lt` - field is less than a value.
  ```ts theme={null}
  // title < 'foo'
  {
    title: {
      lt: 'foo'
    }
  }
  ```
* `lte` - field is less than or equal to a value.
  ```ts theme={null}
  // title <= 'foo'
  {
    title: {
      lte: 'foo'
    }
  }
  ```
* `in` - field is in a list of values.
  ```ts theme={null}
  // title IN ('foo', 'bar', 'baz')
  { title: { in: ['foo', 'bar', 'baz'] } }
  ```
* `notIn` - field is not in a list of values.
  ```ts theme={null}
  // title NOT IN ('foo', 'bar', 'baz')
  {
    title: {
      notIn: ['foo', 'bar', 'baz']
    }
  }
  ```

### String Comparisons[​](#string-comparisons "Direct link to String Comparisons")

* `like` - field is like a value (case sensitive).
  ```ts theme={null}
  // title LIKE 'Foo%'
  {
    title: {
      like: 'Foo%'
    }
  }
  ```
* `notLike` - field is not like a value (case sensitive).
  ```ts theme={null}
  // title NOT LIKE 'Foo%'
  {
    title: {
      notLike: 'Foo%'
    }
  }
  ```
* `iLike` - field is like a value (case insensitive).
  ```ts theme={null}
  // title ILIKE 'Foo%'
  {
    title: {
      iLike: 'Foo%'
    }
  }
  ```
* `notILike` - field is not like a value (case insensitive).
  ```ts theme={null}
  // title NOT ILIKE 'Foo%'
  {
    title: {
      notILike: 'Foo%'
    }
  }
  ```

[Edit this page](https://github.com/tripss/nestjs-query/edit/master/docs/concepts/queries)
