Skip to content

Find Methods ​

findById() ​

js
queryBuilder = queryBuilder.findById(id);

Finds a single item by id.

Arguments ​
ArgumentTypeDescription
idany | any[]The identifier.
Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.
Examples ​
js
const person = await Person.query().findById(1);

Composite key:

js
const person = await Person.query().findById([1, '10']);

findById can be used together with patch, delete and any other query method. All it does is adds the needed where clauses to the query.

js
await Person.query()
  .findById(someId)
  .patch({ firstName: 'Jennifer' });

findByIds() ​

js
queryBuilder = queryBuilder.findByIds(ids);

Finds a list of items. The order of the returned items is not guaranteed to be the same as the order of the inputs.

Arguments ​
ArgumentTypeDescription
idsany[]A List of identifiers.
Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.
Examples ​
js
const [person1, person2] = await Person.query().findByIds([1, 2]);

Composite key:

js
const [person1, person2] = await Person.query().findByIds([
  [1, '10'],
  [2, '10']
]);

findOne() ​

js
queryBuilder = queryBuilder.findOne(...whereArgs);

Shorthand for where(...whereArgs).first().

NOTE: .first() doesn't add limit 1 to the query by default. You can override the Model.useLimitInFirst property to change this behaviour.

Arguments ​
ArgumentTypeDescription
whereArgs...anyAnything the where method accepts.
Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.
Examples ​
js
const person = await Person.query().findOne({
  firstName: 'Jennifer',
  lastName: 'Lawrence'
});
js
const person = await Person.query().findOne('age', '>', 20);
js
const person = await Person.query().findOne(raw('random() < 0.5'));

none() ​

js
queryBuilder = queryBuilder.none();

Makes the query return no results without executing it. Find queries return an empty array (or undefined if first, findById or findOne is used), and no withGraphFetched queries are executed either. Update, patch and delete queries return 0 (or an empty array if returning is used). Using none() with insert, relate and unrelate queries throws an error. Query hooks are still called, and the after hooks receive the empty result.

none() replaces all where clauses of the query with an always false condition (where 1 = 0), so that the query matches no rows when it is executed anyway. This is the case when the query is used as a subquery or in resultSize, and for find queries that use aggregate methods like count or groupBy. These are executed so that they return what the database returns for no rows, for example [{ count: 0 }].

This is useful when building queries in functions that are chained further by the caller, but one of them decides that the query should not return anything.

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.
Examples ​
js
function findVisibleMovies(user) {
  const query = Movie.query().orderBy('name');
  return user.canSeeMovies ? query : query.none();
}

// Returns an empty array without executing any queries
// if the user can't see movies.
const movies = await findVisibleMovies(user).withGraphFetched('actors');

alias() ​

js
queryBuilder = queryBuilder.alias(alias);

Give an alias for the table to be used in the query.

Arguments ​
ArgumentTypeDescription
aliasstringTable alias for the query.
Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.
Examples ​
js
await Person.query()
  .alias('p')
  .where('p.id', 1)
  .join('persons as parent', 'parent.id', 'p.parentId');

aliasFor() ​

js
queryBuilder = queryBuilder.aliasFor(tableNameOrModelClass, alias);

Give an alias for any table in the query.

Arguments ​
ArgumentTypeDescription
tableNameOrModelClassstring | ModelClassThe table or model class to alias.
aliasstringThe alias.
Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.
Examples ​
js
// This query uses joinRelated to join a many-to-many relation which also joins
// the join table `persons_movies`. We specify that the `persons_movies` table
// should be called `pm` instead of the default `movies_join`.
await Person.query()
  .aliasFor('persons_movies', 'pm')
  .joinRelated('movies')
  .where('pm.someProp', 100);

Model class can be used instead of table name

js
await Person.query()
  .aliasFor(Movie, 'm')
  .joinRelated('movies')
  .where('m.name', 'The Room');

select() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

forUpdate() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

forShare() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

forNoKeyUpdate() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

forKeyShare() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

skipLocked() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

noWait() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

as() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

columns() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

column() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

from() ​

See knex documentation

On PostgreSQL, pass { only: true } as the second argument to query only the given table, without the tables that inherit from it (FROM ONLY). This also works for updates and deletes:

js
await Person.query().from(Person.tableName, { only: true }).delete();
Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

fromRaw() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

updateFrom() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

into() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

with() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

withMaterialized() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

withNotMaterialized() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

withSchema() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

table() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

distinct() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

distinctOn() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

where() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

andWhere() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhere() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereNot() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereNot() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereRaw() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereWrapped() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

havingWrapped() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereRaw() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereExists() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereExists() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereNotExists() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereNotExists() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereIn() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereIn() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereNotIn() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereNotIn() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereNull() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereNull() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereNotNull() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereNotNull() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereBetween() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereNotBetween() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereBetween() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereNotBetween() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereColumn() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

andWhereColumn() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereColumn() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereNotColumn() ​

See knex documentation

whereNotColumn compares columns against columns. Both sides are treated as column identifiers, not values. Use whereNot when comparing a column against a literal value.

js
// Compares two columns.
await Person.query().whereNotColumn('firstName', 'lastName');

// Compares a column against a value.
await Person.query().whereNot('firstName', 'Jennifer');
Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

andWhereNotColumn() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereNotColumn() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereLike() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereILike() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

groupBy() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

groupByRaw() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orderBy() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orderByRaw() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

union() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

unionAll() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

intersect() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

having() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

havingRaw() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orHaving() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orHavingRaw() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

offset() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

limit() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

count() ​

See knex documentation

Also see the resultSize method for a cleaner way to just get the number of rows a query would create.

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

countDistinct() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

min() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

max() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

sum() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

avg() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

avgDistinct() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

returning() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

columnInfo() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereComposite() ​

js
queryBuilder = queryBuilder.whereComposite(columns, operator, values);

where for (possibly) composite keys.

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.
Examples ​
js
builder.whereComposite(['id', 'name'], '=', [1, 'Jennifer']);

This method also works with a single column - value pair:

js
builder.whereComposite('id', 1);

whereInComposite() ​

js
queryBuilder = queryBuilder.whereInComposite(columns, values);

whereIn for (possibly) composite keys.

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.
Examples ​
js
builder.whereInComposite(
  ['a', 'b'],
  [
    [1, 2],
    [3, 4],
    [1, 4]
  ]
);
js
builder.whereInComposite('a', [[1], [3], [1]]);
js
builder.whereInComposite('a', [1, 3, 1]);
js
builder.whereInComposite(['a', 'b'], SomeModel.query().select('a', 'b'));

jsonExtract() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

jsonSet() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

jsonInsert() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

jsonRemove() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereJsonObject() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereNotJsonObject() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereJsonPath() ​

See knex documentation

Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

whereJsonSupersetOf() ​

js
queryBuilder = queryBuilder.whereJsonSupersetOf(
  fieldExpression,
  jsonObjectOrFieldExpression
);

Where left hand json field reference is a superset of the right hand json value or reference.

Arguments ​
ArgumentTypeDescription
fieldExpressionFieldExpressionReference to column / json field, which is tested for being a superset
jsonObjectOrFieldExpressionObject | Array | FieldExpressionTo which to compare
Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.
Examples ​
js
const people = await Person.query().whereJsonSupersetOf(
  'additionalData:myDogs',
  'additionalData:dogsAtHome'
);

// These people have all or some of their dogs at home. Person might have some
// additional dogs in their custody since myDogs is superset of dogsAtHome.

const people = await Person.query().whereJsonSupersetOf(
  'additionalData:myDogs[0]',
  { name: 'peter' }
);

// These people's first dog name is "peter", but the dog might have
// additional attributes as well.

The right hand side can also be a ref, val, raw or a subquery:

js
const people = await Person.query().whereJsonSupersetOf(
  'additionalData:myDogs',
  ref('Pet.dogs')
);

Object and array are always their own supersets.

For arrays this means that left side matches if it has all the elements listed in the right hand side. e.g.

[1,2,3] isSuperSetOf [2] => true
[1,2,3] isSuperSetOf [2,1,3] => true
[1,2,3] isSuperSetOf [2,null] => false
[1,2,3] isSuperSetOf [] => true

The not variants with jsonb operators behave in a way that they won't match rows, which don't have the referred json key referred in field expression. e.g. for table

 id |    jsonObject
----+--------------------------
  1 | {}
  2 | NULL
  3 | {"a": 1}
  4 | {"a": 1, "b": 2}
  5 | {"a": ['3'], "b": ['3']}

this query:

js
builder.whereJsonNotEquals('jsonObject:a', 'jsonObject:b');

Returns only the row 4 which has keys a and b and a != b, but it won't return any rows that don't have jsonObject.a or jsonObject.b.

orWhereJsonSupersetOf() ​

See whereJsonSupersetOf

whereJsonNotSupersetOf() ​

See whereJsonSupersetOf

orWhereJsonNotSupersetOf() ​

See whereJsonSupersetOf

whereJsonSubsetOf() ​

js
queryBuilder = queryBuilder.whereJsonSubsetOf(
  fieldExpression,
  jsonObjectOrFieldExpression
);

Where left hand json field reference is a subset of the right hand json value or reference.

Object and array are always their own subsets.

See whereJsonSupersetOf

Arguments ​
ArgumentTypeDescription
fieldExpressionFieldExpressionReference to column / json field, which is tested for being a superset
jsonObjectOrFieldExpressionObject | Array | FieldExpressionTo which to compare
Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereJsonSubsetOf() ​

See whereJsonSubsetOf

whereJsonNotSubsetOf() ​

See whereJsonSubsetOf

orWhereJsonNotSubsetOf() ​

See whereJsonSubsetOf

whereJsonIsArray() ​

js
queryBuilder = queryBuilder.whereJsonIsArray(fieldExpression);

Where json field reference is an array.

Arguments ​
ArgumentTypeDescription
fieldExpressionFieldExpression
Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereJsonIsArray() ​

See whereJsonIsArray

whereJsonNotArray() ​

See whereJsonIsArray

orWhereJsonNotArray() ​

See whereJsonIsArray

whereJsonIsObject() ​

js
queryBuilder = queryBuilder.whereJsonIsObject(fieldExpression);

Where json field reference is an object.

Arguments ​
ArgumentTypeDescription
fieldExpressionFieldExpression
Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereJsonIsObject() ​

See whereJsonIsObject

whereJsonNotObject() ​

See whereJsonIsObject

orWhereJsonNotObject() ​

See whereJsonIsObject

whereJsonHasAny() ​

js
queryBuilder = queryBuilder.whereJsonHasAny(fieldExpression, keys);

Where any of given strings is found from json object keys.

TIP

This doesn't work for arrays. If you want to check if an array contains an item, see this and this issue.

Arguments ​
ArgumentTypeDescription
fieldExpressionFieldExpression
keysstring | string[]Strings that are looked from object or array
Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereJsonHasAny() ​

See whereJsonHasAny

whereJsonHasAll() ​

js
queryBuilder = queryBuilder.whereJsonHasAll(fieldExpression, keys);

Where all of given strings are found from json object keys.

TIP

This doesn't work for arrays. If you want to check if an array contains an item, see this and this issue.

Arguments ​
ArgumentTypeDescription
fieldExpressionFieldExpression
keysstring | string[]Strings that are looked from object or array
Return value ​
TypeDescription
QueryBuilderthis query builder for chaining.

orWhereJsonHasAll() ​

See whereJsonHasAll

MIT Licensed