Skip to content

Commit

Permalink
feat(preloader): add preloadOnce to preload a relationship only once
Browse files Browse the repository at this point in the history
  • Loading branch information
zaosoula committed Dec 14, 2024
1 parent 918f473 commit 025be5e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/orm/preloader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ export class Preloader implements PreloaderContract<LucidRow> {
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
*/
Expand Down
2 changes: 2 additions & 0 deletions src/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
WithAggregate,
WithCount,
PreloadWithoutCallback,
PreloadOnce,
} from './relations.js'

/**
Expand Down Expand Up @@ -481,6 +482,7 @@ export interface ModelQueryBuilderContract<Model extends LucidModel, Result = In
* Define relationships to be preloaded
*/
preload: Preload<InstanceType<Model>, this>
preloadOnce: PreloadOnce<InstanceType<Model>, this>

/**
* Aggregates
Expand Down
4 changes: 4 additions & 0 deletions src/types/relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,10 @@ export interface PreloadWithoutCallback<Model extends LucidRow, Builder> {
<Name extends ExtractModelRelations<Model>>(relation: Name): Builder
}

export interface PreloadOnce<Model extends LucidRow, Builder>
extends PreloadWithoutCallback<Model, Builder> {}


Check failure on line 1071 in src/types/relations.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
/**
* Shape of the preloader to preload relationships
*/
Expand Down

0 comments on commit 025be5e

Please sign in to comment.