Skip to content

Types ​

This page contains the documentation of all other types and classes than Model and QueryBuilder. There are two types of items on this page:

  1. type: A type is just a POJO (Plain Old Javascript Object) with a set of properties.
  2. class: A class is a JavaScript class with properties and methods.

type RelationMapping ​

PropertyTypeDescription
relationfunctionThe relation type. One of Model.BelongsToOneRelation, Model.HasOneRelation, Model.HasManyRelation, Model.ManyToManyRelation, Model.HasOneThroughRelation and Model.BelongsToManyRelation.
modelClassModel
string
Constructor of the related model class, an absolute path to a module that exports one or a path relative to modelPaths that exports a model class.
joinRelationJoinDescribes how the models are related to each other. See RelationJoin.
modifyfunction(QueryBuilder)
string
object
Optional modifier for the relation query. If specified as a function, it will be called each time before fetching the relation. If specified as a string, modifier with specified name will be applied each time when fetching the relation. If specified as an object, it will be used as an additional query parameter - e. g. passing {name: 'Jenny'} would additionally narrow fetched rows to the ones with the name 'Jenny'.
filterfunction(QueryBuilder)
string
object
Alias for modify.
beforeInsertfunction(Model, QueryContext, Model)Optional insert hook that is called for each inserted model instance. This function can be async. The third argument is the owner model. With $relatedQuery() and relatedQuery().for(model) it's the owner the query was started from, and undefined if it was started from ids, a subquery or several models. With insertGraph() and upsertGraph() it's the parent object from the input graph, with only the properties passed to the method, and it may not be inserted yet, so its id can be undefined: in a BelongsToOneRelation the owner is inserted after the related model, and in a ManyToManyRelation or HasOneThroughRelation the insert order isn't defined.

type RelationJoin ​

PropertyTypeDescription
fromstring
ReferenceBuilder
Array
The relation column in the owner table. Must be given with the table name. For example persons.id. Composite key can be specified using an array of columns e.g. ['persons.a', 'persons.b']. Note that neither this nor to need to be foreign keys or primary keys. You can join any column to any column. You can even join nested json fields using the ref helper.
tostring
ReferenceBuilder
Array
The relation column in the related table. Must be given with the table name. For example movies.id. Composite key can be specified using an array of columns e.g. ['movies.a', 'movies.b']. Note that neither this nor from need to be foreign keys or primary keys. You can join any column to any column. You can even join nested json fields using the ref helper.
throughRelationThroughDescribes the join table if the models are related through one.

type RelationThrough ​

PropertyTypeDescription
fromstring
ReferenceBuilder
Array
The column that is joined to from property of the RelationJoin. For example Person_movies.actorId where Person_movies is the join table. Composite key can be specified using an array of columns e.g. ['persons_movies.a', 'persons_movies.b']. You can join nested json fields using the ref helper.
tostring
ReferenceBuilder
Array
The column that is joined to to property of the RelationJoin. For example Person_movies.movieId where Person_movies is the join table. Composite key can be specified using an array of columns e.g. ['persons_movies.a', 'persons_movies.b']. You can join nested json fields using the ref helper.
modelClassstring
ModelClass
If you have a model class for the join table, you should specify it here. This is optional so you don't need to create a model class if you don't want to.
extrastring
string[]
Object
Join table columns listed here are automatically joined to the related objects when they are fetched and automatically written to the join table instead of the related table on insert and update. The values can be aliased by providing an object {propertyName: 'columnName', otherPropertyName: 'otherColumnName'} instead of array See this recipe for more info.
modifyfunction(QueryBuilder)
string
object
Optional modifier for the join table query. If specified as a function, it will be called each time before fetching the relation. If specified as a string, modifier with specified name will be applied each time when fetching the relation. If specified as an object, it will be used as an additional query parameter - e. g. passing {name: 'Jenny'} would additionally narrow fetched rows to the ones with the name 'Jenny'.
filterfunction(QueryBuilder)
string
object
Alias for modify.
beforeInsertfunction(Model, QueryContext)Optional insert hook that is called for each inserted join table model instance. This function can be async.

If the join table is also the related table, from must be the column that is joined to the to property of the RelationJoin, and to the column that is joined to its from property. For example, to relate a form to the manager of the user that filled it in:

js
join: {
  from: 'forms.employeeId',
  through: {
    from: 'users.managerUsername',
    to: 'users.id'
  },
  to: 'users.username'
}

Such relations can be fetched, and the related rows can be updated, patched and deleted. Operations that write join rows aren't supported, as the join rows can't be told apart from the related rows: relate, unrelate, insert through the relation, updating extra properties, and insertGraph / upsertGraph inserting or relating models of the relation throw an error.

type ModelOptions ​

PropertyTypeDescription
patchbooleanIf true the json is treated as a patch and the required field of the json schema is ignored in the validation. This allows us to create models with a subset of required properties for patch operations.
skipValidationbooleanIf true the json schema validation is skipped
oldobjectThe old values for methods like $beforeUpdate and $beforeValidate.

type CloneOptions ​

PropertyTypeDescription
shallowbooleanIf true, relations are ignored

type ToJsonOptions ​

PropertyTypeDescription
shallowbooleanIf true, relations are ignored. Default is false.
virtualsboolean
string[]
If false, virtual attributes are omitted from the output. Default is true. You can also pass an array of property names and only those virtual properties get picked. You can even pass in property/function names that are not included in the static virtualAttributes array.
formatObjectCustom formatting options passed as the second argument to $formatJson of the model and all nested models.

type GraphOptions ​

PropertyTypeDescription
minimizebooleanIf true the aliases of the joined tables and columns created by withGraphJoined are minimized. This is sometimes needed because of identifier length limitations of some database engines: 63 characters on Postgres, 128 on SQL Server and 256 on MySQL. objection throws an exception when a query exceeds the length limit. You need to use this only in those cases.
separatorstringSeparator between relations in nested withGraphJoined query. Defaults to :. Dot (.) cannot be used at the moment because of the way knex parses the identifiers.
aliasesObjectAliases for relations in a withGraphJoined query. Defaults to an empty object.
joinOperationstringWhich join type to use ['leftJoin', 'innerJoin', 'rightJoin', ...] or any other knex join method name. Defaults to leftJoin. Only applies to the relations of the withGraphJoined call it's passed to, and is ignored with a warning for fetched relations.
maxBatchSizeintegerFor how many parents should a relation be fetched using a single query at a time. If you set this to 1 then a separate query is used for each parent to fetch a relation. For example if you want to fetch pets for 5 persons, you get five queries (one for each person). Setting this to 1 will allow you to use stuff like page, range and aggregate functions per parent in modifyGraph and other graph modifiers. limit, offset and first apply per parent anyway, see limitPerParent. This can be used to replace the naiveEager objection 1.x had.
limitPerParentbooleanIf true, limit, offset and first in graph modifiers and in the modify of relation mappings apply to the related items of each parent instead of to all of them, e.g. limit(3) fetches up to three pets per person. If false, they apply to the whole query that fetches the relation for all parents. Only applies to withGraphFetched. Defaults to true. See limiting related items per parent.

type UpsertGraphOptions ​

PropertyTypeDescription
relateboolean
string
string[]
If true, relations are related instead of inserted. A BelongsToOneRelation model without an id that only gives the relation's key is related by writing that key to its owner, without looking up its row. Relate functionality can be enabled for a subset of relations of the graph by providing a list of relation paths or a relation expression. See the examples here.
unrelateboolean
string
string[]
If true, relations are unrelated instead of deleted. Unrelate functionality can be enabled for a subset of relations of the graph by providing a list of relation paths or a relation expression. See the examples here.
insertMissingboolean
string
string[]
If true, models that have identifiers and are not found at their place in the database are inserted with these identifiers. A model whose row exists elsewhere is inserted too, which fails on its unique key unless the graph deletes the row in its other place first: use relate to move it instead. By default this is false and an error is thrown. This functionality can be enabled for a subset of relations of the graph by providing a list of relation paths or a relation expression. See the examples here.
updateboolean
string
string[]
If true, update operations are performed instead of patch when altering existing models, affecting the way the data is validated. With update operations, all required fields need to be present in the data provided. This functionality can be enabled for a subset of relations of the graph by providing a list of relation paths or a relation expression. See the examples here.
noInsertboolean
string
string[]
If true, no inserts are performed. Inserts can be disabled for a subset of relations of the graph by providing a list of relation paths or a relation expression. Nothing is inserted or related below the models that aren't inserted, unless #ref references point there. A skipped model in a to-one relation keeps the current model. See the examples here.
noUpdateboolean
string
string[]
If true, no updates are performed. Updates can be disabled for a subset of relations of the graph by providing a list of relation paths or a relation expression. See the examples here.
noDeleteboolean
string
string[]
If true, no deletes are performed. Deletes can be disabled for a subset of relations of the graph by providing a list of relation paths or a relation expression. See the examples here.
noRelateboolean
string
string[]
If true, no relates are performed. Relate operations can be disabled for a subset of relations of the graph by providing a list of relation paths or a relation expression. A skipped model in a to-one relation keeps the current model. See the examples here.
noUnrelateboolean
string
string[]
If true, no unrelate operations are performed. Unrelate operations can be disabled for a subset of relations of the graph by providing a list of relation paths or a relation expression. See the examples here.
allowRefsbooleanThis needs to be true if you want to use #ref in your graphs. See this section for #ref usage examples.
allowMarkersbooleanThis needs to be true if you want to use the #unrelate and #delete special properties in your graphs. They let the graph data unrelate or delete related models even if noUnrelate or noDelete is set, so don't enable this for untrusted input. See this section for details.

The options that accept relation paths take either an array of relation paths like ['pets', 'movies.reviews'], or a relation expression string in the same syntax as withGraphFetched(), like '[pets, movies.reviews]'. A relation expression matches the same relations as the array of the relation paths it names: only the relations at the end of each path, not the relations along the way. So '[pets, movies.reviews]' matches pets and movies.reviews, but not movies, and relate: 'movies.actors' relates the actors of movies, but not the movies themselves. An expression can't name a relation twice, so '[movies, movies.reviews]' throws an error. Use an array like ['movies', 'movies.reviews'] to match a relation together with relations below it. Instead of '[movies.reviews, movies.actors]', which also throws, use 'movies.[reviews, actors]'. '*' matches all relations at any depth, 'movies.*' all relations below movies, and recursive expressions like 'parent.^' match every level of the recursion. Aliases (movies as m) and empty expressions are rejected. The root models have no relation path and are never matched by a relation expression, so for example noInsert: '*' inserts new root models, but no new related models.

upsertGraph() throws an error for unknown options, like a misspelled noInset.

type InsertGraphOptions ​

PropertyTypeDescription
relateboolean
string
string[]
If true, models with an id are related instead of inserted. Relate functionality can be enabled for a subset of relations of the graph by providing a list of relation paths or a relation expression. See the examples here.
noRelateboolean
string
string[]
If true, no relates are performed, and the models that would be related are left out. Relate operations can be disabled for a subset of relations of the graph by providing a list of relation paths or a relation expression.
noInsertboolean
string
string[]
If true, no inserts are performed. Inserts can be disabled for a subset of relations of the graph by providing a list of relation paths or a relation expression, and the models of these relations are left out.
allowRefsbooleanThis needs to be true if you want to use #ref in your graphs. See this section for #ref usage examples.

insertGraph() throws an error for the other options of upsertGraph(), which have no effect on an insert, and for unknown options.

type FetchGraphOptions ​

PropertyTypeDescription
transactionknex
Transaction
Optional transaction or knex instance for the query. This can be used to specify a transaction or even a different database.
skipFetchedbooleanIf true, only fetch relations that don't yet exist in the object.

type TableMetadataFetchOptions ​

PropertyTypeDescription
tablestringA custom table name. If not given, Model.tableName is used.
knexknex
Transaction
A knex instance or a transaction

type TableMetadataOptions ​

PropertyTypeDescription
tablestringA custom table name. If not given, Model.tableName is used.

type TableMetadata ​

PropertyTypeDescription
columnsstring[]Names of all the columns in a table.

type StaticHookArguments ​

PropertyTypeDescription
itemsModel[]Items for which the query was started. For example in case of an instance query person.$query() or person.$relatedQuery('pets') items would equal [person]. In case of Person.relatedQuery('pets').for([matt, jennifer]) items would equal [matt, jennifer]. In many cases like Person.query() or Person.query().findById(1) this array is empty. It's only populated when the query has been explicitly started for a set of model instances.
inputItemsModel[]Items that were passed as an input for the query. For example in case of Person.query().insert(person) or Person.query().patch(person) inputItems would equal [person]. raw, ref and subquery values aren't on them.
asFindQuery() => QueryBuilderA function that returns a query builder that can be used to fetch the items that were/would get affected by the query being executed. Modifying this query builder doesn't affect the query being executed. For example calling await asFindQuery().select('id') in a beforeDelete hook would get you the identifiers of all the items that will get deleted by the query. This query is automatically executed inside any existing transaction. This query builder always returns an array even if the query being executed would return an object, a number or something else. runAfter callbacks of the query being executed, including the ones added by throwIfNotFound and traverse(), are not applied to it. Neither is the value that the query being executed was resolved with, e.g. by cancelQuery() or resolve: the returned query still fetches the rows.
transactionknex
Transaction
If the query being executed has a transaction, this property will contain it. Otherwise this holds the knex instance installed for the query. Either way, this can and should be passed to any queries executed in the static hooks.
contextobjectThe context of the query. See context.
relationRelationIf the query is for a relation, this property holds the Relation object. For example when you call person.$relatedQuery('pets) or Person.relatedQuery('movies') the relation will be a relation object for pets and movies relation of Person respectively.
cancelQueryfunction(any)Cancels the query being executed. You can pass an argument for the function, and that value is used as the result of the query, as if the database had returned it: rows for a find query, a row or rows that are merged into the input models of an insert, or a number for updates and deletes. It goes through the same steps as the result of the database, e.g. first() and the after* hooks.
resultanyThe result of the query, in the shape that the query returns: a model, an array of models, a number or undefined. See the after hooks for the details of each query. undefined in before* hooks.

type FieldExpression ​

Field expressions are strings that allow you to refer to JSONB fields inside columns.

Syntax: <column reference>[:<json field reference>]

e.g. persons.jsonColumnName:details.names[1] would refer to value 'Second' in column persons.jsonColumnName which has { details: { names: ['First', 'Second', 'Last'] } } object stored in it.

First part <column reference> is compatible with column references used in knex e.g. MyFancyTable.tributeToThBestColumnNameEver.

Second part describes a path to an attribute inside the referred column. It is optional and it always starts with colon which follows directly with first path element. e.g. Table.jsonObjectColumnName:jsonFieldName or Table.jsonArrayColumn:[321].

Syntax supports [<key or index>] and .<key or index> flavors of reference to json keys / array indexes:

e.g. both Table.myColumn:[1][3] and Table.myColumn:1.3 would access correctly both of the following objects [null, [null,null,null, "I was accessed"]] and { "1": { "3" : "I was accessed" } }

Caveats when using special characters in keys:

  1. objectColumn.key This is the most common syntax, good if you are not using dots or square brackets [] in your json object key name.
  2. Keys containing dots objectColumn:[keywith.dots] Column { "keywith.dots" : "I was referred" }
  3. Keys containing square brackets column['[]'] { "[]" : "This is getting ridiculous..." }
  4. Keys containing square brackets and quotes objectColumn:['Double."Quote".[]'] and objectColumn:["Sinlge.'Quote'.[]"] Column { "Double.\"Quote\".[]" : "I was referred", "Sinlge.'Quote'.[]" : "Mee too!" }
  5. Empty keys objectColumn:[""] or objectColumn:[''] Column { "" : "I was referred" }
  6. Keys containing dots, square brackets, single quotes and double quotes in one json key is not currently supported

Keys with a : in the objects passed to patch are field expressions that write into json columns, e.g. patch({ 'jsonColumn:some.key': value }), unless the model's jsonSchema declares them in its properties. Declared keys like 'ns:name' are normal properties and columns, and so are keys that don't parse as field expressions, like 'ns:'. insert and update write whole values and reject field expression keys with a ValidationError. The query only knows the properties of jsonSchema, not those added by $beforeValidate.

There are some special methods that accept FieldExpression strings directly, like whereJsonSupersetOf but you can use FieldExpressions anywhere with ref. Here's an example:

js
import { ref } from 'objection';

await Person.query()
  .select([
    'id',
    ref('persons.jsonColumn:details.name').castText().as('name'),
    ref('persons.jsonColumn:details.age').castInt().as('age'),
  ])
  .join(
    'someTable',
    ref('persons.jsonColumn:details.name').castText(),
    '=',
    ref('someTable.name')
  )
  .where('age', '>', ref('someTable.ageLimit'));

In the above example, we assume persons table has a column named jsonColumn of type jsonb on PostgreSQL or json on MySQL and SQLite. On MySQL and SQLite, [1] only refers to array elements and .1 only to object keys, see the JSON queries recipe.

type RelationExpression ​

Relation expression is a simple DSL for expressing relation trees.

These strings are all valid relation expressions:

  • children
  • children.movies
  • [children, pets]
  • [children.movies, pets]
  • [children.[movies, pets], pets]
  • [children.[movies.actors.[children, pets], pets], pets]
  • [children as kids, pets(filterDogs) as dogs]

There are two tokens that have special meaning: * and ^. * means "all relations recursively" and ^ means "this relation recursively".

For example children.* means "relation children and all its relations, and all their relations and ...".

WARNING

The * token must be used with caution or you will end up fetching your entire database.

Expression parent.^ is equivalent to parent.parent.parent.parent... up to the point a relation no longer has results for the parent relation. The recursion can be limited to certain depth by giving the depth after the ^ character. For example parent.^3 is equal to parent.parent.parent.

* and ^ end a relation path: nothing can follow them, and they take no modifiers or aliases. * can also be the whole expression. Expressions like parent.^.pets, *.pets or [children, *] throw an error.

Relations can be aliased using the as keyword.

A relation can only be named once at each level of an expression. [children.movies, children.pets] and [children, children.movies] throw an error, use children.[movies, pets] and children.movies instead.

For example the expression children.[movies.actors.[pets, children], pets] represents a tree:

              children
              (Person)
                 |
         -----------------
         |               |
       movies           pets
      (Movie)         (Animal)
         |
       actors
      (Person)
         |
    -----------
    |         |
   pets    children
 (Animal)  (Person)

The model classes are shown in parenthesis. When given to withGraphFetched method, this expression would fetch all relations as shown in the tree above:

js
const people = await Person.query().withGraphFetched(
  'children.[movies.actors.[pets, children], pets]'
);

// All persons have the given relation tree fetched.
console.log(people[0].children[0].movies[0].actors[0].pets[0].name);

Relation expressions can have arguments. Arguments are used to refer to modifier functions (either global or local. Arguments are listed in parenthesis after the relation names like this:

js
Person.query().withGraphFetched(
  `children(arg1, arg2).[movies.actors(arg3), pets]`
);

You can spread relation expressions to multiple lines and add whitespace:

js
Person.query().withGraphFetched(`[
    children.[
      pets,
      movies.actors.[
        pets,
        children
      ]
    ]
  ]`);

Relation expressions can be aliased using as keyword:

js
Person.query().withGraphFetched(`[
    children as kids.[
      pets(filterDogs) as dogs,
      pets(filterCats) as cats,

      movies.actors.[
        pets,
        children as kids
      ]
    ]
  ]`);

RelationExpression object notation ​

In addition to the string expressions, a more verbose object notation can also be used.

The string expression in the comment is equivalent to the object expression below it:

js
// `children`
{
  children: true;
}
js
// `children.movies`
{
  children: {
    movies: true;
  }
}
js
// `[children, pets]`
{
  children: true;
  pets: true;
}
js
// `[children.[movies, pets], pets]`
{
  children: {
    movies: true,
    pets: true
  }
  pets: true
}
js
// `parent.^`
{
  parent: {
    $recursive: true;
  }
}
js
// `parent.^5`
{
  parent: {
    $recursive: 5;
  }
}
js
// `parent.*`
{
  parent: {
    $allRecursive: true;
  }
}
js
// `[children as kids, pets(filterDogs) as dogs]`
{
  kids: {
    $relation: 'children'
  },

  dogs: {
    $relation: 'pets',
    $modify: ['filterDogs']
  }
}

type TransactionObject ​

This is nothing more than a knex transaction object. It can be used as a knex query builder, it can be passed to objection queries and models can be bound to it

See the section about transactions for more info and examples.

Instance Methods ​

commit() ​

js
const promise = trx.commit();

Call this method to commit the transaction. This only needs to be called if you use transaction.start() method.

rollback() ​

js
const promise = trx.rollback(error);

Call this method to rollback the transaction. This only needs to be called if you use transaction.start() method. You need to pass the error to the method as the only argument.

class ValidationError ​

js
import { ValidationError } from 'objection';

throw new ValidationError({ type, message, data });

For each key, a list of errors is given. Each error contains the default message (as returned by the validator), an optional keyword string to identify the validation rule which didn't pass and a param object which optionally contains more details about the context of the validation error.

If type is anything else but "ModelValidation", data can be any object that describes the error.

Error of this class is thrown by default if validation of any input fails. By input we mean any data that can come from the outside world, like model instances (or POJOs), relation expressions object graphs etc.

You can replace this error by overriding Model.createValidationError() method.

See the error handling recipe for more info.

PropertyTypeDescription
statusCodenumberHTTP status code for interop with express error handlers and other libraries that search for status code from errors.
typestringOne of "ModelValidation", "RelationExpression", "UnallowedRelation" and "InvalidGraph". This can be any string for your own custom errors. The listed values are used internally by objection.
dataobjectThe content of this property is documented below for "ModelValidation" errors. For other types, this can be any data.

If type is "ModelValidation" then data object should follow this pattern:

js
{
  key1: [{
    message: '...',
    keyword: 'required',
    params: null
  }, {
    message: '...',
    keyword: '...',
    params: {
      ...
    }
  }, ...],

  key2: [{
    message: '...',
    keyword: 'minLength',
    params: {
      limit: 1,
      ...
    }
  }, ...],

  ...
}

class NotFoundError ​

js
import { NotFoundError } from 'objection';

throw new NotFoundError(data);

Error of this class is thrown by default by throwIfNotFound()

You can replace this error by overriding Model.createNotFoundError() method.

See the error handling recipe for more info.

class Relation ​

Relation is a parsed and normalized instance of a RelationMapping. Relations can be accessed using the getRelations method.

Relation holds a RelationProperty instance for each property that is used to create the relationship between two tables.

Relation is actually a base class for all relation types BelongsToOneRelation, HasManyRelation etc. You can use instanceof to determine the type of the relations (see the example on the right). Note that HasOneRelation is a subclass of HasManyRelation and HasOneThroughRelation is a subclass of ManyToManyRelation. Arrange your instanceof checks accordingly.

PropertyTypeDescription
namestringName of the relation. For example pets or children.
ownerModelClassfunctionThe model class that has defined the relation.
relatedModelClassfunctionThe model class of the related objects.
ownerPropRelationPropertyThe relation property in the ownerModelClass.
relatedPropRelationPropertyThe relation property in the relatedModelClass.
joinModelClassfunctionThe model class representing the join table. This class is automatically generated by Objection if none is provided in the join.through.modelClass setting of the relation mapping, see RelationThrough.
joinTablestringThe name of the join table (only for ManyToMany and HasOneThrough relations).
joinTableOwnerPropRelationPropertyThe join table property pointing to ownerProp (only for ManyToMany and HasOneThrough relations).
joinTableRelatedPropRelationPropertyThe join table property pointing to relatedProp (only for ManyToMany and HasOneThrough relations).
MethodReturn valueDescription
isOneToOne()booleantrue for relations to a single model: BelongsToOneRelation, HasOneRelation and HasOneThroughRelation. Safer than instanceof checks, as the latter two extend the many-relations.

Note that Relation instances are actually instances of the relation classes used in relationMappings. For example:

js
class Person extends Model {
  static get relationMappings() {
    return {
      pets: {
        relation: Model.HasManyRelation,
        modelClass: Animal,
        join: {
          from: 'persons.id',
          to: 'animals.ownerId',
        },
      },
    };
  }
}

const relations = Person.getRelations();

console.log(relations.pets instanceof Model.HasManyRelation); // --> true
console.log(relations.pets.name); // --> pets
console.log(relations.pets.ownerProp.cols); // --> ['id']
console.log(relations.pets.relatedProp.cols); // --> ['ownerId']

class RelationProperty ​

Represents a property that is used to create relationship between two tables. A single RelationProperty instance can represent composite key. In addition to a table column, A RelationProperty can represent a nested field inside a column (for example a jsonb column).

Properties ​

PropertyTypeDescription
sizenumberThe number of columns. In case of composite key, this is greater than one.
modelClassfunctionThe model class that owns the property.
propsstring[]The column names converted to "external" format. For example if modelClass defines a snake_case to camelCase conversion, these names are in camelCase. Note that a RelationProperty may actually point to a sub-properties of the columns in case they are of json or some other non-scalar type. This array always contains only the converted column names. Use getProp(obj, idx) method to get the actual value from an object.
colsstring[]The column names in the database format. For example if modelClass defines a snake_case to camelCase conversion, these names are in snake_case. Note that a RelationProperty may actually point to a sub-properties of the columns in case they are of json or some other non-scalar type. This array always contains only the column names.

Methods ​

getProp() ​

js
const value = property.getProp(obj, index);

Gets this property's index:th value from an object. For example if the property represents a composite key [a, b.d.e, c] and obj is {a: 1, b: {d: {e: 2}}, c: 3} then getProp(obj, 1) would return 2.

setProp() ​

js
property.setProp(obj, index, value);

Sets this property's index:th value in an object. For example if the property represents a composite key [a, b.d.e, c] and obj is {a: 1, b: {d: {e: 2}}, c: 3} then setProp(obj, 1, 'foo') would mutate obj into {a: 1, b: {d: {e: 'foo'}}, c: 3}.

fullCol() ​

js
const col = property.fullCol(builder, index);

Returns the property's index:th column name with the correct table reference. Something like "Table.column". The first argument must be an objection QueryBuilder instance.

ref() ​

js
const ref = property.ref(builder, index);

Allows you to do things like this:

js
const builder = Person.query();
const ref = property.ref(builder, 0);
builder.where(ref, '>', 10);

Returns a ReferenceBuilder instance that points to the index:th column.

patch() ​

js
property.patch(patchObj, index, value);

Allows you to do things like this:

js
const builder = Person.query();
const patch = {};
property.patch(patch, 0, 'foo');
builder.patch(patch);

Appends an update operation for the index:th column into patchObj object.

class ReferenceBuilder ​

An instance of this is returned from the ref helper function.

Instance Methods ​

The casts use the types of PostgreSQL. On MySQL, which can only cast to a few types, they are mapped to CHAR (text), SIGNED (integer, bigint and boolean), DOUBLE (float), FLOAT (real) and DECIMAL(65,30) (decimal). SQLite accepts the type names as they are.

castText() ​

Cast reference to sql type text.

castInt() ​

Cast reference to sql type integer.

castBigInt() ​

Cast reference to sql type bigint.

castFloat() ​

Cast reference to sql type float.

castDecimal() ​

Cast reference to sql type decimal.

castReal() ​

Cast reference to sql type real.

castBool() ​

Cast reference to sql type boolean.

castTo() ​

Give custom type to which referenced value is cast to. The types of the casts above are mapped on MySQL, others are used as they are.

.castTo('mytype') --> CAST(?? as mytype)

castJson() ​

In addition to other casts, convert the reference to a json value: to_jsonb() on PostgreSQL, JSON_EXTRACT(JSON_ARRAY(??), '$[0]') on MySQL and json_quote() on SQLite. SQLite can't tell json columns from text columns, so a reference to a whole column needs a cast before castJson() there, e.g. ref('name').castText().castJson(), and throws without one.

as() ​

Gives an alias for the reference .select(ref('age').as('yougness'))

from() ​

Specifies that table of the reference.

See this for some examples.

class ValueBuilder ​

An instance of this is returned from the val helper function. If an object is given as a value, it is cast to json by default.

Instance Methods ​

castText() ​

Cast to sql type text.

castInt() ​

Cast to sql type integer.

castBigInt() ​

Cast to sql type bigint.

castFloat() ​

Cast to sql type float.

castDecimal() ​

Cast to sql type decimal.

castReal() ​

Cast to sql type real.

castBool() ​

Cast to sql type boolean.

castTo() ​

Give custom type to which referenced value is cast to.

.castTo('mytype') --> CAST(?? as mytype)

castJson() ​

Converts the value to json: jsonb on PostgreSQL, JSON on MySQL and json() on SQLite. Object and array values are json by default, which is CAST(? AS JSON) on MySQL and json(?) on SQLite, while PostgreSQL infers the type from the context.

asArray() ​

Converts the value to an array.

val([1, 2, 3]).asArray() --> ARRAY[?, ?, ?]

Can be used in conjuction with castTo.

val([1, 2, 3]).asArray().castTo('real[]') -> CAST(ARRAY[?, ?, ?] AS real[])

as() ​

Gives an alias for the reference .select(ref('age').as('yougness'))

class RawBuilder ​

An instance of this is returned from the raw helper function.

Instance Methods ​

as() ​

Gives an alias for the raw expression .select(raw('concat(foo, bar)').as('fooBar')).

You should use this instead of inserting the alias to the SQL to give objection more information about the query. Some edge cases, like using raw in select inside a withGraphJoined modifier won't work unless you use this method.

class FunctionBuilder ​

An instance of this is returned from the fn helper function.

Instance Methods ​

as() ​

Gives an alias for the raw expression .select(fn('concat', 'foo', 'bar').as('fooBar')).

You should use this instead of inserting the alias to the SQL to give objection more information about the query. Some edge cases, like using fn in select inside a withGraphJoined modifier won't work unless you use this method.

class Validator ​

js
import { Validator } from 'objection';

Abstract class from which model validators must be inherited. See the example for explanation. Also check out the createValidator method.

Examples ​

js
import { Validator } from 'objection';

class MyCustomValidator extends Validator {
  validate(args) {
    // The model instance. May be empty at this point.
    const model = args.model;

    // The properties to validate. After validation these values will
    // be merged into `model` by objection.
    const json = args.json;

    // `ModelOptions` object. If your custom validator sets default
    // values, you need to check the `opt.patch` boolean. If it is true
    // we are validating a patch object and the defaults should not be set.
    const opt = args.options;

    // A context object shared between the validation methods. A new
    // object is created for each validation operation. You can store
    // any data here.
    const ctx = args.ctx;

    // Do your validation here and throw any exception if the
    // validation fails.
    doSomeValidationAndThrowIfFails(json);

    // You need to return the (possibly modified) json.
    return json;
  }

  beforeValidate(args) {
    // Takes the same arguments as `validate`. Usually there is no need
    // to override this.
    return super.beforeValidate(args);
  }

  afterValidate(args) {
    // Takes the same arguments as `validate`. Usually there is no need
    // to override this.
    return super.afterValidate(args);
  }
}

import { Model } from 'objection';

// Override the `createValidator` method of a `Model` to use the
// custom validator.
class BaseModel extends Model {
  static createValidator() {
    return new MyCustomValidator();
  }
}

class AjvValidator ​

js
import { AjvValidator } from 'objection';

The default Ajv based json schema validator. You can override the createValidator method of Model like in the example to modify the validator.

Examples ​

js
import { Model, AjvValidator } from 'objection';

class BaseModel extends Model {
  static createValidator() {
    return new AjvValidator({
      onCreateAjv: (ajv) => {
        // Here you can modify the `Ajv` instance.
      },
      options: {
        allErrors: true,
        validateSchema: false,
        ownProperties: true,
        v5: true,
      },
    });
  }
}

MIT Licensed