Skip to content

Commit

Permalink
refactor: re-arraging arguments position of relations query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 16, 2019
1 parent 693f911 commit bf8a44c
Show file tree
Hide file tree
Showing 17 changed files with 146 additions and 55 deletions.
6 changes: 4 additions & 2 deletions src/Orm/Relations/Base/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions src/Orm/Relations/Base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 16 additions & 8 deletions src/Orm/Relations/BelongsTo/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}
}
}
8 changes: 6 additions & 2 deletions src/Orm/Relations/BelongsTo/QueryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions src/Orm/Relations/BelongsTo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
22 changes: 15 additions & 7 deletions src/Orm/Relations/HasMany/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}
}
8 changes: 6 additions & 2 deletions src/Orm/Relations/HasMany/QueryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions src/Orm/Relations/HasMany/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
18 changes: 13 additions & 5 deletions src/Orm/Relations/HasManyThrough/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
})
}
Expand All @@ -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)
})),
)
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/Orm/Relations/HasManyThrough/QueryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions src/Orm/Relations/HasManyThrough/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Loading

0 comments on commit bf8a44c

Please sign in to comment.