-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add query client to all relationships
The query client provides shortcut fetch methods and persistance methods for relationships
- Loading branch information
1 parent
8ad50e7
commit 392951d
Showing
16 changed files
with
2,056 additions
and
3,999 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
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,89 @@ | ||
/* | ||
* @adonisjs/lucid | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { QueryClientContract, TransactionClientContract } from '@ioc:Adonis/Lucid/Database' | ||
import { RelationBaseQueryClientContract, RelationshipsContract } from '@ioc:Adonis/Lucid/Relations' | ||
import { | ||
ModelObject, | ||
ModelConstructorContract, | ||
ModelContract, | ||
ModelAdapterOptions, | ||
} from '@ioc:Adonis/Lucid/Model' | ||
|
||
/** | ||
* Query client for executing queries in scope to the defined | ||
* relationship | ||
*/ | ||
export abstract class BaseQueryClient implements RelationBaseQueryClientContract< | ||
ModelConstructorContract, | ||
ModelConstructorContract | ||
> { | ||
constructor ( | ||
protected $client: QueryClientContract, | ||
protected $relation: RelationshipsContract, | ||
) { | ||
} | ||
|
||
/** | ||
* Client options to be used when persisting related | ||
* models. | ||
*/ | ||
protected $clientOptions: ModelAdapterOptions = { | ||
client: this.$client, | ||
connection: this.$client.connectionName, | ||
profiler: this.$client.profiler, | ||
} | ||
|
||
/** | ||
* Perisist a model instance | ||
*/ | ||
protected async $persist ( | ||
related: ModelContract, | ||
trx?: TransactionClientContract, | ||
) { | ||
if (!trx) { | ||
related.$setOptionsAndTrx(this.$clientOptions) | ||
await related.save() | ||
} | ||
|
||
related.$trx = trx | ||
await related.save() | ||
} | ||
|
||
/** | ||
* Creates a persists a single model instance | ||
*/ | ||
protected async $createAndPersist ( | ||
values: ModelObject, | ||
trx?: TransactionClientContract, | ||
): Promise<ModelContract> { | ||
if (!trx) { | ||
return this.$relation.$relatedModel().create(values, this.$clientOptions) | ||
} | ||
return this.$relation.$relatedModel().create(values, { client: trx }) | ||
} | ||
|
||
/** | ||
* Creates a persists many of the model instances | ||
*/ | ||
protected async $createAndPersistMany (values: ModelObject[]): Promise<ModelContract[]> { | ||
return this.$relation.$relatedModel().createMany(values, this.$clientOptions) | ||
} | ||
|
||
/** | ||
* Returns instance of query builder | ||
*/ | ||
public abstract query (): any | ||
|
||
/** | ||
* Returns instance of query builder with `eager=true` | ||
*/ | ||
public abstract eagerQuery (): any | ||
} | ||
|
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
Oops, something went wrong.