diff --git a/src/Orm/Relations/Base/QueryBuilder.ts b/src/Orm/Relations/Base/QueryBuilder.ts index f8227d54..a9d208ac 100644 --- a/src/Orm/Relations/Base/QueryBuilder.ts +++ b/src/Orm/Relations/Base/QueryBuilder.ts @@ -15,6 +15,9 @@ import { RelationBaseQueryBuilderContract, RelationshipsContract } from '@ioc:Ad import { ModelQueryBuilder } from '../../QueryBuilder' +/** + * Base query builder for ORM relationships + */ export abstract class BaseQueryBuilder extends ModelQueryBuilder implements RelationBaseQueryBuilderContract< ModelConstructorContract, ModelConstructorContract @@ -31,14 +34,13 @@ ModelConstructorContract } /** - * Name of the query action + * Returns the name of the query action */ protected $queryAction (): string { let action = this.$knexBuilder['_method'] if (action === 'del') { action = 'delete' } - return action } diff --git a/src/Orm/Relations/Base/index.ts b/src/Orm/Relations/Base/index.ts index 224d5294..f24dee7a 100644 --- a/src/Orm/Relations/Base/index.ts +++ b/src/Orm/Relations/Base/index.ts @@ -14,6 +14,9 @@ import { BaseRelationContract, RelationOptions, TypedRelations } from '@ioc:Adon import { KeysExtractor } from '../KeysExtractor' +/** + * Base class for implementing ORM Relationships + */ export abstract class BaseRelation implements BaseRelationContract< ModelConstructorContract, ModelConstructorContract diff --git a/src/Orm/Relations/BelongsTo/QueryBuilder.ts b/src/Orm/Relations/BelongsTo/QueryBuilder.ts index 15eff2d1..e08109c8 100644 --- a/src/Orm/Relations/BelongsTo/QueryBuilder.ts +++ b/src/Orm/Relations/BelongsTo/QueryBuilder.ts @@ -16,23 +16,31 @@ import { BelongsTo } from './index' import { getValue, unique } from '../../../utils' import { BaseQueryBuilder } from '../Base/QueryBuilder' +/** + * Extends the model query builder for executing queries in scope + * to the current relationship + */ export class BelongsToQueryBuilder extends BaseQueryBuilder implements RelationBaseQueryBuilderContract< ModelConstructorContract, ModelConstructorContract > { constructor ( - private builder: knex.QueryBuilder, - private models: ModelContract | ModelContract[], + builder: knex.QueryBuilder, client: QueryClientContract, + private parent: ModelContract | ModelContract[], private relation: BelongsTo, ) { super(builder, client, relation, (userFn) => { return (__builder) => { - userFn(new BelongsToQueryBuilder(__builder, this.models, this.client, this.relation)) + userFn(new BelongsToQueryBuilder(__builder, this.client, this.parent, this.relation)) } }) } + /** + * Applies constraint to limit rows to the current relationship + * only. + */ public applyConstraints () { if (this.$appliedConstraints) { return @@ -44,8 +52,8 @@ ModelConstructorContract /** * Eager query contraints */ - if (Array.isArray(this.models)) { - this.builder.whereIn(this.relation.$localCastAsKey, unique(this.models.map((model) => { + if (Array.isArray(this.parent)) { + this.$knexBuilder.whereIn(this.relation.$localCastAsKey, unique(this.parent.map((model) => { return getValue(model, this.relation.$foreignKey, this.relation, queryAction) }))) return @@ -54,14 +62,14 @@ ModelConstructorContract /** * Query constraints */ - const value = getValue(this.models, this.relation.$foreignKey, this.relation, queryAction) - this.builder.where(this.relation.$localCastAsKey, value) + const value = getValue(this.parent, this.relation.$foreignKey, this.relation, queryAction) + this.$knexBuilder.where(this.relation.$localCastAsKey, value) /** * Do not add limit when updating or deleting */ if (!['update', 'delete'].includes(queryAction)) { - this.builder.limit(1) + this.$knexBuilder.limit(1) } } } diff --git a/src/Orm/Relations/BelongsTo/QueryClient.ts b/src/Orm/Relations/BelongsTo/QueryClient.ts index de9ff773..cec47026 100644 --- a/src/Orm/Relations/BelongsTo/QueryClient.ts +++ b/src/Orm/Relations/BelongsTo/QueryClient.ts @@ -14,19 +14,23 @@ import { RelationBaseQueryClientContract } from '@ioc:Adonis/Lucid/Relations' import { BelongsTo } from './index' import { BelongsToQueryBuilder } from './QueryBuilder' +/** + * Query client for executing queries in scope to the defined + * relationship + */ export class BelongsToQueryClient implements RelationBaseQueryClientContract< ModelConstructorContract, ModelConstructorContract > { constructor ( - private models: ModelContract | ModelContract[], + private parent: ModelContract | ModelContract[], private client: QueryClientContract, private relation: BelongsTo, ) { } public query (): any { - return new BelongsToQueryBuilder(this.client.knexQuery(), this.models, this.client, this.relation) + return new BelongsToQueryBuilder(this.client.knexQuery(), this.client, this.parent, this.relation) } public eagerQuery (): any { diff --git a/src/Orm/Relations/BelongsTo/index.ts b/src/Orm/Relations/BelongsTo/index.ts index d1dfb036..cd013f92 100644 --- a/src/Orm/Relations/BelongsTo/index.ts +++ b/src/Orm/Relations/BelongsTo/index.ts @@ -14,6 +14,9 @@ import { BelongsToRelationContract, RelationOptions } from '@ioc:Adonis/Lucid/Re import { BaseRelation } from '../Base' import { BelongsToQueryClient } from './QueryClient' +/** + * Manages loading and persisting belongs to relationship + */ export class BelongsTo extends BaseRelation implements BelongsToRelationContract< ModelConstructorContract, ModelConstructorContract @@ -131,8 +134,11 @@ ModelConstructorContract }) } - public client (models: ModelContract | ModelContract[], client: QueryClientContract): any { + /** + * Returns an instance of query client for the given relationship + */ + public client (parent: ModelContract | ModelContract[], client: QueryClientContract): any { this.$ensureIsBooted() - return new BelongsToQueryClient(models, client, this) + return new BelongsToQueryClient(parent, client, this) } } diff --git a/src/Orm/Relations/HasMany/QueryBuilder.ts b/src/Orm/Relations/HasMany/QueryBuilder.ts index 7fd7d55b..2bdeed03 100644 --- a/src/Orm/Relations/HasMany/QueryBuilder.ts +++ b/src/Orm/Relations/HasMany/QueryBuilder.ts @@ -16,23 +16,31 @@ import { HasMany } from './index' import { getValue, unique } from '../../../utils' import { BaseQueryBuilder } from '../Base/QueryBuilder' +/** + * Extends the model query builder for executing queries in scope + * to the current relationship + */ export class HasManyQueryBuilder extends BaseQueryBuilder implements RelationBaseQueryBuilderContract< ModelConstructorContract, ModelConstructorContract > { constructor ( - private builder: knex.QueryBuilder, - private models: ModelContract | ModelContract[], + builder: knex.QueryBuilder, client: QueryClientContract, + private parent: ModelContract | ModelContract[], private relation: HasMany, ) { super(builder, client, relation, (userFn) => { return (__builder) => { - userFn(new HasManyQueryBuilder(__builder, this.models, this.client, this.relation)) + userFn(new HasManyQueryBuilder(__builder, this.client, this.parent, this.relation)) } }) } + /** + * Applies constraint to limit rows to the current relationship + * only. + */ public applyConstraints () { if (this.$appliedConstraints) { return @@ -44,8 +52,8 @@ ModelConstructorContract /** * Eager query contraints */ - if (Array.isArray(this.models)) { - this.builder.whereIn(this.relation.$foreignCastAsKey, unique(this.models.map((model) => { + if (Array.isArray(this.parent)) { + this.$knexBuilder.whereIn(this.relation.$foreignCastAsKey, unique(this.parent.map((model) => { return getValue(model, this.relation.$localKey, this.relation, queryAction) }))) return @@ -54,7 +62,7 @@ ModelConstructorContract /** * Query constraints */ - const value = getValue(this.models, this.relation.$localKey, this.relation, queryAction) - this.builder.where(this.relation.$foreignCastAsKey, value) + const value = getValue(this.parent, this.relation.$localKey, this.relation, queryAction) + this.$knexBuilder.where(this.relation.$foreignCastAsKey, value) } } diff --git a/src/Orm/Relations/HasMany/QueryClient.ts b/src/Orm/Relations/HasMany/QueryClient.ts index ae9f08cb..61289f17 100644 --- a/src/Orm/Relations/HasMany/QueryClient.ts +++ b/src/Orm/Relations/HasMany/QueryClient.ts @@ -14,19 +14,23 @@ import { RelationBaseQueryClientContract } from '@ioc:Adonis/Lucid/Relations' import { HasMany } from './index' import { HasManyQueryBuilder } from './QueryBuilder' +/** + * Query client for executing queries in scope to the defined + * relationship + */ export class HasManyQueryClient implements RelationBaseQueryClientContract< ModelConstructorContract, ModelConstructorContract > { constructor ( - private models: ModelContract | ModelContract[], + private parent: ModelContract | ModelContract[], private client: QueryClientContract, private relation: HasMany, ) { } public query (): any { - return new HasManyQueryBuilder(this.client.knexQuery(), this.models, this.client, this.relation) + return new HasManyQueryBuilder(this.client.knexQuery(), this.client, this.parent, this.relation) } public eagerQuery (): any { diff --git a/src/Orm/Relations/HasMany/index.ts b/src/Orm/Relations/HasMany/index.ts index 67ac3fd2..8f55dca1 100644 --- a/src/Orm/Relations/HasMany/index.ts +++ b/src/Orm/Relations/HasMany/index.ts @@ -14,6 +14,9 @@ import { HasManyRelationContract, RelationOptions } from '@ioc:Adonis/Lucid/Rela import { BaseRelation } from '../Base' import { HasManyQueryClient } from './QueryClient' +/** + * Manages loading and persisting has many relationship + */ export class HasMany extends BaseRelation implements HasManyRelationContract< ModelConstructorContract, ModelConstructorContract @@ -118,8 +121,11 @@ ModelConstructorContract }) } - public client (models: ModelContract | ModelContract[], client: QueryClientContract): any { + /** + * Returns an instance of query client for invoking queries + */ + public client (parent: ModelContract | ModelContract[], client: QueryClientContract): any { this.$ensureIsBooted() - return new HasManyQueryClient(models, client, this) + return new HasManyQueryClient(parent, client, this) } } diff --git a/src/Orm/Relations/HasManyThrough/QueryBuilder.ts b/src/Orm/Relations/HasManyThrough/QueryBuilder.ts index b5567647..0a9ba066 100644 --- a/src/Orm/Relations/HasManyThrough/QueryBuilder.ts +++ b/src/Orm/Relations/HasManyThrough/QueryBuilder.ts @@ -16,19 +16,23 @@ import { HasManyThrough } from './index' import { getValue, unique } from '../../../utils' import { BaseQueryBuilder } from '../Base/QueryBuilder' +/** + * Extends the model query builder for executing queries in scope + * to the current relationship + */ export class HasManyThroughQueryBuilder extends BaseQueryBuilder implements RelationBaseQueryBuilderContract< ModelConstructorContract, ModelConstructorContract > { constructor ( builder: knex.QueryBuilder, - private models: ModelContract | ModelContract[], client: QueryClientContract, + private parent: ModelContract | ModelContract[], private relation: HasManyThrough, ) { super(builder, client, relation, (userFn) => { return (__builder) => { - userFn(new HasManyThroughQueryBuilder(__builder, this.models, this.client, this.relation)) + userFn(new HasManyThroughQueryBuilder(__builder, this.client, this.parent, this.relation)) } }) } @@ -43,10 +47,10 @@ ModelConstructorContract /** * Eager query contraints */ - if (Array.isArray(this.models)) { + if (Array.isArray(this.parent)) { builder.whereIn( `${throughTable}.${this.relation.$foreignCastAsKey}`, - unique(this.models.map((model) => { + unique(this.parent.map((model) => { return getValue(model, this.relation.$localKey, this.relation, queryAction) })), ) @@ -56,10 +60,14 @@ ModelConstructorContract /** * Query constraints */ - const value = getValue(this.models, this.relation.$localKey, this.relation, queryAction) + const value = getValue(this.parent, this.relation.$localKey, this.relation, queryAction) builder.where(`${throughTable}.${this.relation.$foreignCastAsKey}`, value) } + /** + * Applies constraint to limit rows to the current relationship + * only. + */ public applyConstraints () { if (this.$appliedConstraints) { return diff --git a/src/Orm/Relations/HasManyThrough/QueryClient.ts b/src/Orm/Relations/HasManyThrough/QueryClient.ts index f18d0cfb..3a19711b 100644 --- a/src/Orm/Relations/HasManyThrough/QueryClient.ts +++ b/src/Orm/Relations/HasManyThrough/QueryClient.ts @@ -14,19 +14,23 @@ import { RelationBaseQueryClientContract } from '@ioc:Adonis/Lucid/Relations' import { HasManyThrough } from './index' import { HasManyThroughQueryBuilder } from './QueryBuilder' +/** + * Query client for executing queries in scope to the defined + * relationship + */ export class HasManyThroughClient implements RelationBaseQueryClientContract< ModelConstructorContract, ModelConstructorContract > { constructor ( - private models: ModelContract | ModelContract[], + private parent: ModelContract | ModelContract[], private client: QueryClientContract, private relation: HasManyThrough, ) { } public query (): any { - return new HasManyThroughQueryBuilder(this.client.knexQuery(), this.models, this.client, this.relation) + return new HasManyThroughQueryBuilder(this.client.knexQuery(), this.client, this.parent, this.relation) } public eagerQuery (): any { diff --git a/src/Orm/Relations/HasManyThrough/index.ts b/src/Orm/Relations/HasManyThrough/index.ts index 3e132e96..09464b7f 100644 --- a/src/Orm/Relations/HasManyThrough/index.ts +++ b/src/Orm/Relations/HasManyThrough/index.ts @@ -14,6 +14,9 @@ import { HasManyThroughRelationContract, ThroughRelationOptions } from '@ioc:Ado import { BaseRelation } from '../Base' import { HasManyThroughClient } from './QueryClient' +/** + * Manages loading and persisting has many through relationship + */ export class HasManyThrough extends BaseRelation implements HasManyThroughRelationContract< ModelConstructorContract, ModelConstructorContract @@ -162,8 +165,11 @@ ModelConstructorContract }) } - public client (models: ModelContract | ModelContract[], client: QueryClientContract): any { + /** + * Returns an instance of query client for invoking queries + */ + public client (parent: ModelContract | ModelContract[], client: QueryClientContract): any { this.$ensureIsBooted() - return new HasManyThroughClient(models, client, this) + return new HasManyThroughClient(parent, client, this) } } diff --git a/src/Orm/Relations/HasOne/QueryBuilder.ts b/src/Orm/Relations/HasOne/QueryBuilder.ts index ff36ff11..72091814 100644 --- a/src/Orm/Relations/HasOne/QueryBuilder.ts +++ b/src/Orm/Relations/HasOne/QueryBuilder.ts @@ -16,23 +16,31 @@ import { HasOne } from './index' import { getValue, unique } from '../../../utils' import { BaseQueryBuilder } from '../Base/QueryBuilder' +/** + * Extends the model query builder for executing queries in scope + * to the current relationship + */ export class HasOneQueryBuilder extends BaseQueryBuilder implements RelationBaseQueryBuilderContract< ModelConstructorContract, ModelConstructorContract > { constructor ( - private builder: knex.QueryBuilder, - private models: ModelContract | ModelContract[], + builder: knex.QueryBuilder, client: QueryClientContract, + private parent: ModelContract | ModelContract[], private relation: HasOne, ) { super(builder, client, relation, (userFn) => { return (__builder) => { - userFn(new HasOneQueryBuilder(__builder, this.models, this.client, this.relation)) + userFn(new HasOneQueryBuilder(__builder, this.client, this.parent, this.relation)) } }) } + /** + * Applies constraint to limit rows to the current relationship + * only. + */ public applyConstraints () { if (this.$appliedConstraints) { return @@ -44,8 +52,8 @@ ModelConstructorContract /** * Eager query contraints */ - if (Array.isArray(this.models)) { - this.builder.whereIn(this.relation.$foreignCastAsKey, unique(this.models.map((model) => { + if (Array.isArray(this.parent)) { + this.$knexBuilder.whereIn(this.relation.$foreignCastAsKey, unique(this.parent.map((model) => { return getValue(model, this.relation.$localKey, this.relation, queryAction) }))) return @@ -54,14 +62,14 @@ ModelConstructorContract /** * Query constraints */ - const value = getValue(this.models, this.relation.$localKey, this.relation, queryAction) - this.builder.where(this.relation.$foreignCastAsKey, value) + const value = getValue(this.parent, this.relation.$localKey, this.relation, queryAction) + this.$knexBuilder.where(this.relation.$foreignCastAsKey, value) /** * Do not add limit when updating or deleting */ if (!['update', 'delete'].includes(queryAction)) { - this.builder.limit(1) + this.$knexBuilder.limit(1) } } } diff --git a/src/Orm/Relations/HasOne/QueryClient.ts b/src/Orm/Relations/HasOne/QueryClient.ts index b314c29c..439dc186 100644 --- a/src/Orm/Relations/HasOne/QueryClient.ts +++ b/src/Orm/Relations/HasOne/QueryClient.ts @@ -14,19 +14,23 @@ import { RelationBaseQueryClientContract } from '@ioc:Adonis/Lucid/Relations' import { HasOne } from './index' import { HasOneQueryBuilder } from './QueryBuilder' +/** + * Query client for executing queries in scope to the defined + * relationship + */ export class HasOneQueryClient implements RelationBaseQueryClientContract< ModelConstructorContract, ModelConstructorContract > { constructor ( - private models: ModelContract | ModelContract[], + private parent: ModelContract | ModelContract[], private client: QueryClientContract, private relation: HasOne, ) { } public query (): any { - return new HasOneQueryBuilder(this.client.knexQuery(), this.models, this.client, this.relation) + return new HasOneQueryBuilder(this.client.knexQuery(), this.client, this.parent, this.relation) } public eagerQuery (): any { diff --git a/src/Orm/Relations/HasOne/index.ts b/src/Orm/Relations/HasOne/index.ts index 7e45b772..5c90d063 100644 --- a/src/Orm/Relations/HasOne/index.ts +++ b/src/Orm/Relations/HasOne/index.ts @@ -14,6 +14,9 @@ import { HasOneRelationContract, RelationOptions } from '@ioc:Adonis/Lucid/Relat import { BaseRelation } from '../Base' import { HasOneQueryClient } from './QueryClient' +/** + * Manages loading and persisting has one relationship + */ export class HasOne extends BaseRelation implements HasOneRelationContract< ModelConstructorContract, ModelConstructorContract @@ -133,8 +136,11 @@ ModelConstructorContract }) } - public client (models: ModelContract | ModelContract[], client: QueryClientContract): any { + /** + * Returns an instance of query client for invoking queries + */ + public client (parent: ModelContract | ModelContract[], client: QueryClientContract): any { this.$ensureIsBooted() - return new HasOneQueryClient(models, client, this) + return new HasOneQueryClient(parent, client, this) } } diff --git a/src/Orm/Relations/ManyToMany/QueryBuilder.ts b/src/Orm/Relations/ManyToMany/QueryBuilder.ts index 7305558b..436b85da 100644 --- a/src/Orm/Relations/ManyToMany/QueryBuilder.ts +++ b/src/Orm/Relations/ManyToMany/QueryBuilder.ts @@ -16,19 +16,23 @@ import { ManyToMany } from './index' import { getValue, unique } from '../../../utils' import { BaseQueryBuilder } from '../Base/QueryBuilder' +/** + * Extends the model query builder for executing queries in scope + * to the current relationship + */ export class ManyToManyQueryBuilder extends BaseQueryBuilder implements ManyToManyQueryBuilderContract< ModelConstructorContract, ModelConstructorContract > { constructor ( builder: knex.QueryBuilder, - private models: ModelContract | ModelContract[], client: QueryClientContract, + private parent: ModelContract | ModelContract[], private relation: ManyToMany, ) { super(builder, client, relation, (userFn) => { return (__builder) => { - userFn(new ManyToManyQueryBuilder(__builder, this.models, this.client, this.relation)) + userFn(new ManyToManyQueryBuilder(__builder, this.client, this.parent, this.relation)) } }) } @@ -49,8 +53,8 @@ ModelConstructorContract /** * Eager query contraints */ - if (Array.isArray(this.models)) { - this.whereInPivot(this.relation.$pivotForeignKey, unique(this.models.map((model) => { + if (Array.isArray(this.parent)) { + this.whereInPivot(this.relation.$pivotForeignKey, unique(this.parent.map((model) => { return getValue(model, this.relation.$localKey, this.relation, queryAction) }))) return @@ -59,7 +63,7 @@ ModelConstructorContract /** * Query constraints */ - const value = getValue(this.models, this.relation.$localKey, this.relation, queryAction) + const value = getValue(this.parent, this.relation.$localKey, this.relation, queryAction) this.wherePivot(this.relation.$pivotForeignKey, value) } diff --git a/src/Orm/Relations/ManyToMany/QueryClient.ts b/src/Orm/Relations/ManyToMany/QueryClient.ts index 2ec7322f..501acc6f 100644 --- a/src/Orm/Relations/ManyToMany/QueryClient.ts +++ b/src/Orm/Relations/ManyToMany/QueryClient.ts @@ -14,19 +14,23 @@ import { ManyToManyClientContract } from '@ioc:Adonis/Lucid/Relations' import { ManyToMany } from './index' import { ManyToManyQueryBuilder } from './QueryBuilder' +/** + * Query client for executing queries in scope to the defined + * relationship + */ export class ManyToManyQueryClient implements ManyToManyClientContract< ModelConstructorContract, ModelConstructorContract > { constructor ( - private models: ModelContract | ModelContract[], + private parent: ModelContract | ModelContract[], private client: QueryClientContract, private relation: ManyToMany, ) { } public query (): any { - return new ManyToManyQueryBuilder(this.client.knexQuery(), this.models, this.client, this.relation) + return new ManyToManyQueryBuilder(this.client.knexQuery(), this.client, this.parent, this.relation) } public eagerQuery (): any { diff --git a/src/Orm/Relations/ManyToMany/index.ts b/src/Orm/Relations/ManyToMany/index.ts index 68e15fd0..7dcdd9e3 100644 --- a/src/Orm/Relations/ManyToMany/index.ts +++ b/src/Orm/Relations/ManyToMany/index.ts @@ -14,6 +14,9 @@ import { ManyToManyRelationContract, ManyToManyRelationOptions } from '@ioc:Adon import { BaseRelation } from '../Base' import { ManyToManyQueryClient } from './QueryClient' +/** + * Manages loading and persisting many to many relationship + */ export class ManyToMany extends BaseRelation implements ManyToManyRelationContract< ModelConstructorContract, ModelConstructorContract @@ -157,8 +160,11 @@ ModelConstructorContract }) } - public client (models: ModelContract | ModelContract[], client: QueryClientContract): any { + /** + * Returns an instance of query client for invoking queries + */ + public client (parent: ModelContract | ModelContract[], client: QueryClientContract): any { this.$ensureIsBooted() - return new ManyToManyQueryClient(models, client, this) + return new ManyToManyQueryClient(parent, client, this) } }