> For the complete documentation index, see [llms.txt](https://docs.schemajs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.schemajs.com/reference-guides/schemajs-global/querybuilder.md).

# QueryBuilder

The `QueryBuilder` class in **SchemaJS** is designed to help you easily construct complex queries for interacting with tables in your database. It provides methods to build `WHERE`, `AND`, and `OR` conditions, allowing for powerful filtering and querying. The `QueryBuilder` class allows users to define conditions programmatically, making it easy to dynamically generate queries and control how data is retrieved from the database.

## Constructor

<pre class="language-typescript"><code class="lang-typescript"><strong>new QueryBuilder(dbName?: string, tableName?: string)
</strong></code></pre>

{% hint style="info" %}
If `dbName` or `tableName` are not passed, the constructor attempts to use the global context (`SJS_CONTEXT`) for the current database and table.
{% endhint %}

{% hint style="info" %}
Using this class from a *custom query or a hook* already injects \`**`SJS_CONTEXT`**`` `. ``You do not need to pass \`**dbName**\` or \`**tableName**\`
{% endhint %}

## where

Adds a `WHERE` condition to the query. The condition specifies the column key, the filter type, and the value to compare against.

```typescript
builder.where(key: string, filter_type: string, value: any)
```

## and

Adds an `AND` condition, grouping multiple conditions under an `AND` clause. The callback allows the user to add more conditions to the `AND` clause using a new `QueryBuilder` instance.

```typescript
builder.and((b) => {
    b.where("age", "=", 30);
    b.where("country", "=", "USA");
});
```

## or

Adds an `OR` condition, grouping multiple conditions under an `OR` clause. The callback allows the user to add more conditions to the `OR` clause using a new `QueryBuilder` instance.

```typescript
builder.or((b) => {
    b.where("status", "=", "inactive");
    b.where("last_login", "<", "2023-01-01");
});
```
