Movatterモバイル変換


[0]ホーム

URL:


Objection.js

# Types

This page contains the documentation of all other types and classes thanModel andQueryBuilder. 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 ofModel.BelongsToOneRelation,Model.HasOneRelation,Model.HasManyRelation,Model.ManyToManyRelation andModel.HasOneThroughRelation.
modelClassModel
string
Constructor of the related model class, an absolute path to a module that exports one or a path relative tomodelPaths that exports a model class.
joinRelationJoinDescribes how the models are related to each other. SeeRelationJoin.
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(ModelQueryContext)Optional insert hook that is called for each inserted model instance. This function can be async.

#type RelationJoin

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

#type RelationThrough

PropertyTypeDescription
fromstring
ReferenceBuilder
Array
The column that is joined tofrom property of theRelationJoin. For examplePerson_movies.actorId wherePerson_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 theref helper.
tostring
ReferenceBuilder
Array
The column that is joined toto property of theRelationJoin. For examplePerson_movies.movieId wherePerson_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 theref 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 Seethis 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(ModelQueryContext)Optional insert hook that is called for each inserted join table model instance. This function can be async.

#type ModelOptions

PropertyTypeDescription
patchbooleanIf true the json is treated as a patch and therequired 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 staticvirtualAttributes array.

#type GraphOptions

PropertyTypeDescription
minimizebooleanIf true the aliases of the joined tables and columns created bywithGraphJoined are minimized. This is sometimes needed because of identifier length limitations of some database engines. objection throws an exception when a query exceeds the length limit. You need to use this only in those cases.
separatorstringSeparator between relations in nestedwithGraphJoined query. Defaults to:. Dot (.) cannot be used at the moment because of the way knex parses the identifiers.
aliasesObjectAliases for relations in awithGraphJoined query. Defaults to an empty object.
joinOperationstringWhich join type to use['leftJoin', 'innerJoin', 'rightJoin', ...] or any other knex join method name. Defaults toleftJoin.
maxBatchSizeintegerFor how many parents should a relation be fetched using a single query at a time. If you set this to1 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 to1 will allow you to use stuff likelimit and aggregate functions inmodifyGraph and other graph modifiers. This can be used to replace thenaiveEager objection 1.x had.

#type UpsertGraphOptions

PropertyTypeDescription
relateboolean
string[]
If true, relations are related instead of inserted. Relate functionality can be enabled for a subset of relations of the graph by providing a list of relation expressions. See the exampleshere.
unrelateboolean
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 expressions. See the exampleshere.
insertMissingboolean
string[]
If true, models that have identifiersand are not found in the database, are inserted. 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 expressions. See the exampleshere.
updateboolean
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 expressions. See the exampleshere.
noInsertboolean
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 expressions. See the exampleshere.
noUpdateboolean
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 expressions. See the exampleshere.
noDeleteboolean
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 expressions. See the exampleshere.
noRelateboolean
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 expressions. See the exampleshere.
noUnrelateboolean
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 expressions. See the exampleshere.
allowRefsbooleanThis needs to be true if you want to use#ref in your graphs. Seethis section for#ref usage examples.

#type InsertGraphOptions

PropertyTypeDescription
relateboolean
string[]
If true, models with anid are related instead of inserted. Relate functionality can be enabled for a subset of relations of the graph by providing a list of relation expressions. See the exampleshere.
allowRefsbooleanThis needs to be true if you want to use#ref in your graphs. Seethis section for#ref usage examples.

#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 queryperson.$query() orperson.$relatedQuery('pets')items would equal[person]. In case ofPerson.relatedQuery('pets').for([matt, jennifer])items would equal[matt, jennifer]. In many cases likePerson.query() orPerson.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 ofPerson.query().insert(person) orPerson.query().patch(person)inputItems would equal[person].
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 callingawait asFindQuery().select('id') in abeforeDelete 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.
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. Seecontext.
relationRelationIf the query is for a relation, this property holds theRelation object. For example when you callperson.$relatedQuery('pets) orPerson.relatedQuery('movies') therelation will be a relation object for pets and movies relation ofPerson respectively.
cancelQueryfunction(any)Cancels the query being executed. You can pass an arugment for the function and that value will be the result of the query.
resultany[]The result of the query. Only available inafter* 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 columnpersons.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 orTable.jsonArrayColumn:[321].

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

e.g. bothTable.myColumn:[1][3] andTable.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 dotsobjectColumn:[keywith.dots] Column{ "keywith.dots" : "I was referred" }
  3. Keys containing square bracketscolumn['[]']{ "[]" : "This is getting ridiculous..." }
  4. Keys containing square brackets and quotesobjectColumn:['Double."Quote".[]'] andobjectColumn:["Sinlge.'Quote'.[]"] Column{ "Double.\"Quote\".[]" : "I was referred", "Sinlge.'Quote'.[]" : "Mee too!" }
  5. Keys containing dots, square brackets, single quotes and double quotes in one json key is not currently supported

There are some special methods that acceptFieldExpression strings directly, likewhereJsonSupersetOf but you can useFieldExpressions anywhere withref. Here's an example:

const{ ref}=require('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 assumepersons table has a column namedjsonColumn of typejsonb (only works on postgres).

#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 examplechildren.* means "relationchildren 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.

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

Relations can be aliased using theas keyword.

For example the expressionchildren.[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 towithGraphFetched method, this expression would fetch all relations as shown in the tree above:

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 (eitherglobal orlocal. Arguments are listed in parenthesis after the relation names like this:

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

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

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

Relation expressions can be aliased usingas keyword:

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:

// `children`{children:true;}
// `children.movies`{children:{movies:true;}}
// `[children, pets]`{children:true;pets:true;}
// `[children.[movies, pets], pets]`{children:{movies:true,pets:true}pets:true}
// `parent.^`{parent:{$recursive:true;}}
// `parent.^5`{parent:{$recursive:5;}}
// `parent.*`{parent:{$allRecursive:true;}}
// `[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 bepassed to objection queries andmodels can be bound to it

See the section abouttransactions for more info and examples.

# Instance Methods

# commit()

const promise= trx.commit();

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

# rollback()

const promise= trx.rollback(error);

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

#class ValidationError

const{ ValidationError}=require('objection');thrownewValidationError({ type, message, data});

For eachkey, a list of errors is given. Each error contains the defaultmessage (as returned by the validator), an optionalkeyword string to identify the validation rule which didn't pass and aparam object which optionally contains more details about the context of the validation error.

Iftype 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 overridingModel.createValidationError() method.

See theerror 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.

Iftype is"ModelValidation" thendata object should follow this pattern:

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

#class NotFoundError

const{ NotFoundError}=require('objection');thrownewNotFoundError(data);

Error of this class is thrown by default bythrowIfNotFound()

You can replace this error by overridingModel.createNotFoundError() method.

See theerror handling recipe for more info.

#class Relation

Relation is a parsed and normalized instance of aRelationMapping.Relations can be accessed using thegetRelations method.

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

Relation is actually a base class for all relation typesBelongsToOneRelation,HasManyRelation etc. You can useinstanceof to determine the type of the relations (see the example on the right). Note thatHasOneRelation is a subclass ofHasManyRelation andHasOneThroughRelation is a subclass ofManyToManyRelation. Arrange yourinstanceof checks accordingly.

PropertyTypeDescription
namestringName of the relation. For examplepets orchildren.
ownerModelClassfunctionThe model class that has defined the relation.
relatedModelClassfunctionThe model class of the related objects.
ownerPropRelationPropertyThe relation property in theownerModelClass.
relatedPropRelationPropertyThe relation property in therelatedModelClass.
joinModelClassfunctionThe model class representing the join table. This class is automatically generated by Objection if none is provided in thejoin.through.modelClass setting of the relation mapping, seeRelationThrough.
joinTablestringThe name of the join table (only forManyToMany andHasOneThrough relations).
joinTableOwnerPropRelationPropertyThe join table property pointing toownerProp (only forManyToMany andHasOneThrough relations).
joinTableRelatedPropRelationPropertyThe join table property pointing torelatedProp (only forManyToMany andHasOneThrough relations).

Note thatRelation instances are actually instances of the relation classes used inrelationMappings. For example:

classPersonextendsModel{staticgetrelationMappings(){return{pets:{relation: Model.HasManyRelation,modelClass: Animal,join:{from:'persons.id',to:'animals.ownerId',},},};}}const relations= Person.getRelations();console.log(relations.petsinstanceofModel.HasManyRelation);// --> trueconsole.log(relations.pets.name);// --> petsconsole.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 singleRelationProperty instance can representcomposite key. In addition to a table column, ARelationProperty 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 ifmodelClass defines a snake_case to camelCase conversion, these names are in camelCase. Note that aRelationProperty 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. UsegetProp(obj, idx) method to get the actual value from an object.
colsstring[]The column names in the database format. For example ifmodelClass defines a snake_case to camelCase conversion, these names are in snake_case. Note that aRelationProperty 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()

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} thengetProp(obj, 1) would return2.

# setProp()

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} thensetProp(obj, 1, 'foo') would mutateobj into{a: 1, b: {d: {e: 'foo'}}, c: 3}.

# fullCol()

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 objectionQueryBuilder instance.

# ref()

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

Allows you to do things like this:

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

Returns aReferenceBuilder instance that points to the index:th column.

# patch()

property.patch(patchObj, index, value);

Allows you to do things like this:

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

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

#class ReferenceBuilder

An instance of this is returned from theref helper function.

# Instance Methods

# castText()

Cast reference to sql typetext.

# castInt()

Cast reference to sql typeinteger.

# castBigInt()

Cast reference to sql typebigint.

# castFloat()

Cast reference to sql typefloat.

# castDecimal()

Cast reference to sql typedecimal.

# castReal()

Cast reference to sql typereal.

# castBool()

Cast reference to sql typeboolean.

# castTo()

Give custom type to which referenced value is cast to.

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

# castJson()

In addition to other casts wrap reference to_jsonb() function so that final valuereference will be json type.

# as()

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

# from()

Specifies that table of the reference.

Seethis for some examples.

#class ValueBuilder

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

# Instance Methods

# castText()

Cast to sql typetext.

# castInt()

Cast to sql typeinteger.

# castBigInt()

Cast to sql typebigint.

# castFloat()

Cast to sql typefloat.

# castDecimal()

Cast to sql typedecimal.

# castReal()

Cast to sql typereal.

# castBool()

Cast to sql typeboolean.

# castTo()

Give custom type to which referenced value is cast to.

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

# castJson()

Converts the value to json (jsonb in case of postgresql). The defaultcast type for object values.

# asArray()

Converts the value to an array.

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

Can be used in conjuction withcastTo.

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 theraw 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 usingraw inselect inside awithGraphJoined modifier won't work unless you use this method.

#class FunctionBuilder

An instance of this is returned from thefn 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 usingfn inselect inside awithGraphJoined modifier won't work unless you use this method.

#class Validator

const{ Validator}=require('objection');

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

# Examples

const{ Validator}=require('objection');classMyCustomValidatorextendsValidator{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.returnsuper.beforeValidate(args);}afterValidate(args){// Takes the same arguments as `validate`. Usually there is no need// to override this.returnsuper.afterValidate(args);}}const{ Model}=require('objection');// Override the `createValidator` method of a `Model` to use the// custom validator.classBaseModelextendsModel{staticcreateValidator(){returnnewMyCustomValidator();}}

#class AjvValidator

const{ AjvValidator}=require('objection');

The defaultAjv(opens new window) based json schemavalidator. You can override thecreateValidatormethod ofModel like in the example to modify the validator.

# Examples

const{ Model, AjvValidator}=require('objection');classBaseModelextendsModel{staticcreateValidator(){returnnewAjvValidator({onCreateAjv:(ajv)=>{// Here you can modify the `Ajv` instance.},options:{allErrors:true,validateSchema:false,ownProperties:true,v5:true,},});}}

[8]ページ先頭

©2009-2025 Movatter.jp