diff --git a/src/orm/preloader/index.ts b/src/orm/preloader/index.ts index 2850bd0c..d942c823 100644 --- a/src/orm/preloader/index.ts +++ b/src/orm/preloader/index.ts @@ -128,6 +128,18 @@ export class Preloader implements PreloaderContract { return this.load(name, callback) } + /** + * Define a relationship to preload, but only if they are not + * already preloaded + */ + preloadOnce(name: any): this { + if (!this.preloads[name]) { + return this.load(name) + } + + return this + } + /** * Toggle query debugging */ diff --git a/src/orm/query_builder/index.ts b/src/orm/query_builder/index.ts index e34dc9b3..f1a68499 100644 --- a/src/orm/query_builder/index.ts +++ b/src/orm/query_builder/index.ts @@ -652,6 +652,15 @@ export class ModelQueryBuilder return this } + /** + * Define a relationship to preload, but only if they are not + * already preloaded + */ + preloadOnce(relationName: any): this { + this.preloader.preloadOnce(relationName) + return this + } + /** * Perform update by incrementing value for a given column. Increments * can be clubbed with `update` as well diff --git a/src/types/model.ts b/src/types/model.ts index 0b41283f..7052c993 100644 --- a/src/types/model.ts +++ b/src/types/model.ts @@ -42,6 +42,7 @@ import { WithAggregate, WithCount, PreloadWithoutCallback, + PreloadOnce, } from './relations.js' /** @@ -481,6 +482,7 @@ export interface ModelQueryBuilderContract, this> + preloadOnce: PreloadOnce, this> /** * Aggregates diff --git a/src/types/relations.ts b/src/types/relations.ts index 3291aff3..7c9df8ae 100644 --- a/src/types/relations.ts +++ b/src/types/relations.ts @@ -1065,6 +1065,9 @@ export interface PreloadWithoutCallback { >(relation: Name): Builder } +export interface PreloadOnce + extends PreloadWithoutCallback {} + /** * Shape of the preloader to preload relationships */ @@ -1074,6 +1077,7 @@ export interface PreloaderContract { load: Preload preload: Preload + preloadOnce: PreloadOnce debug(debug: boolean): this sideload(values: ModelObject): this