Skip to content

Commit

Permalink
feat(manytomany): add persistance methods
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jan 12, 2020
1 parent 9c80036 commit 10b3af1
Show file tree
Hide file tree
Showing 8 changed files with 1,283 additions and 799 deletions.
30 changes: 20 additions & 10 deletions adonis-typings/relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ declare module '@ioc:Adonis/Lucid/Relations' {
ModelQueryBuilderContract,
} from '@ioc:Adonis/Lucid/Model'

import { QueryClientContract } from '@ioc:Adonis/Lucid/Database'
import { QueryClientContract, TransactionClientContract } from '@ioc:Adonis/Lucid/Database'
import {
StrictValues,
QueryCallback,
Expand Down Expand Up @@ -469,37 +469,47 @@ declare module '@ioc:Adonis/Lucid/Relations' {
/**
* Save related model instance.
*/
save (related: InstanceType<RelatedModel>): Promise<void>
save (related: InstanceType<RelatedModel>, checkExisting?: boolean): Promise<void>

/**
* Create related model instance
* Save many of related model instance.
*/
create (values: ModelObject): Promise<InstanceType<RelatedModel>>
saveMany (related: InstanceType<RelatedModel>[], checkExisting?: boolean): Promise<void>

/**
* Save many of related model instance.
* Create related model instance
*/
saveMany (related: InstanceType<RelatedModel>[]): Promise<void>
create (values: ModelObject, checkExisting?: boolean): Promise<InstanceType<RelatedModel>>

/**
* Create many of related model instances
*/
createMany (values: ModelObject[]): Promise<InstanceType<RelatedModel>[]>
createMany (values: ModelObject[], checkExisting?: boolean): Promise<InstanceType<RelatedModel>[]>

/**
* Attach new pivot rows
*/
attach (ids: (string | number)[] | { [key: string]: ModelObject }): Promise<void>
attach (
ids: (string | number)[] | { [key: string]: ModelObject },
trx?: TransactionClientContract,
): Promise<void>

/**
* Detach existing pivot rows
*/
detach (ids: (string | number)[]): Promise<void>
detach (
ids?: (string | number)[],
trx?: TransactionClientContract,
): Promise<void>

/**
* Sync pivot rows.
*/
sync (ids: (string | number)[] | { [key: string]: ModelObject }): Promise<void>
sync (
ids: (string | number)[] | { [key: string]: ModelObject },
detach?: boolean,
trx?: TransactionClientContract,
): Promise<void>
}

/**
Expand Down
11 changes: 8 additions & 3 deletions src/Orm/QueryBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ export class ModelQueryBuilder extends Chainable implements ModelQueryBuilderCon
protected static macros = {}
protected static getters = {}

/**
* Control whether or not to wrap adapter result to model
* instances or not
*/
protected wrapResultsToModelInstances: boolean = true

/**
* Options that must be passed to all new model instances
*/
Expand Down Expand Up @@ -207,15 +213,14 @@ export class ModelQueryBuilder extends Chainable implements ModelQueryBuilderCon
/**
* Executes the query
*/
public async exec (): Promise<any> {
public async exec (): Promise<any[]> {
const isWriteQuery = ['update', 'del', 'insert'].includes(this.knexQuery['_method'])

const rows = await executeQuery(this.knexQuery, this.client, this.getProfilerAction())

/**
* Return the rows as it is when query is a write query
*/
if (isWriteQuery) {
if (isWriteQuery || !this.wrapResultsToModelInstances) {
return Array.isArray(rows) ? rows : [rows]
}

Expand Down
8 changes: 7 additions & 1 deletion src/Orm/Relations/ManyToMany/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { ModelConstructorContract, ModelContract } from '@ioc:Adonis/Lucid/Model
import { ManyToManyQueryBuilderContract } from '@ioc:Adonis/Lucid/Relations'

import { ManyToMany } from './index'
import { getValue, unique, isObject } from '../../../utils'
import { BaseQueryBuilder } from '../Base/QueryBuilder'
import { getValue, unique, isObject } from '../../../utils'

/**
* Extends the model query builder for executing queries in scope
Expand All @@ -27,6 +27,12 @@ ModelConstructorContract
private cherryPickingKeys: boolean = false
private appliedConstraints: boolean = false

/**
* Do not wrap result to model instances when query builder
* is initiated with `pivotOnly` flag.
*/
protected wrapResultsToModelInstances = !this.pivotOnly

constructor (
builder: knex.QueryBuilder,
client: QueryClientContract,
Expand Down
Loading

0 comments on commit 10b3af1

Please sign in to comment.