-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding support to save related models
- Loading branch information
1 parent
a7aa2d0
commit f2f385c
Showing
22 changed files
with
2,992 additions
and
712 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* @adonisjs/lucid | ||
* | ||
* (c) Harminder Virk <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
/* | ||
* @adonisjs/lucid | ||
* | ||
* (c) Harminder Virk <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/// <reference path="../../../../adonis-typings/index.ts" /> | ||
|
||
import knex from 'knex' | ||
|
||
import { | ||
ModelContract, | ||
RelationContract, | ||
BaseRelationQueryBuilderContract, | ||
} from '@ioc:Adonis/Lucid/Model' | ||
|
||
import { DBQueryCallback } from '@ioc:Adonis/Lucid/DatabaseQueryBuilder' | ||
import { QueryClientContract, TransactionClientContract } from '@ioc:Adonis/Lucid/Database' | ||
|
||
import { getValue } from '../../../utils' | ||
import { ModelQueryBuilder } from '../../QueryBuilder' | ||
|
||
/** | ||
* Exposes the API for interacting with has many relationship | ||
*/ | ||
export abstract class BaseRelationQueryBuilder | ||
extends ModelQueryBuilder | ||
implements BaseRelationQueryBuilderContract<any> | ||
{ | ||
protected $appliedConstraints: boolean = false | ||
|
||
constructor ( | ||
builder: knex.QueryBuilder, | ||
private _baseRelation: RelationContract, | ||
client: QueryClientContract, | ||
queryCallback: DBQueryCallback, | ||
) { | ||
super(builder, _baseRelation.relatedModel(), client, queryCallback) | ||
} | ||
|
||
/** | ||
* Applies constraints for `select`, `update` and `delete` queries. The | ||
* inserts are not allowed directly and one must use `save` method | ||
* instead. | ||
*/ | ||
public abstract applyConstraints (): this | ||
|
||
/** | ||
* Adds neccessary where clause to the query to perform the select | ||
*/ | ||
public beforeExecute () { | ||
this.applyConstraints() | ||
} | ||
|
||
/** | ||
* Read value for a key on a model instance, in reference to the | ||
* relationship operations | ||
*/ | ||
protected $getRelatedValue (model: ModelContract, key: string, action = 'preload') { | ||
return getValue(model, key, this._baseRelation, action) | ||
} | ||
|
||
/** | ||
* Persists related model instance by setting the FK | ||
*/ | ||
protected async $persist<T extends ModelContract, V extends ModelContract> ( | ||
parent: T, | ||
related: V | V[], | ||
cb: (parent: T, related: V) => void, | ||
) { | ||
await parent.save() | ||
|
||
related = Array.isArray(related) ? related : [related] | ||
await Promise.all(related.map((relation) => { | ||
/** | ||
* Copying options and trx to make sure relation is using | ||
* the same options from the parent model | ||
*/ | ||
relation.$trx = parent.$trx | ||
relation.$options = parent.$options | ||
cb(parent, relation) | ||
|
||
return relation.save() | ||
})) | ||
} | ||
|
||
/** | ||
* Persists related model instance inside a transaction. Transaction is | ||
* created only when parent model is not persisted and user has not | ||
* disabled transactions as well. | ||
*/ | ||
protected async $persistInTrx<T extends ModelContract, V extends ModelContract> ( | ||
parent: T, | ||
related: V | V[], | ||
trx: TransactionClientContract, | ||
cb: (parent: T, related: V) => void, | ||
) { | ||
try { | ||
parent.$trx = trx | ||
await this.$persist(parent, related, cb) | ||
await trx.commit() | ||
} catch (error) { | ||
await trx.rollback() | ||
throw error | ||
} | ||
} | ||
|
||
public abstract async save (model: ModelContract, wrapInTransaction?: boolean): Promise<void> | ||
public abstract async saveMany (model: ModelContract[], wrapInTransaction?: boolean): Promise<void> | ||
} |
Oops, something went wrong.