@codenameryuu/adonis-lucid-auto-preload 1.9.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -65,7 +65,9 @@ export declare function AutoPreload<T extends NormalizeConstructor<LucidModel>>(
65
65
  * List of relationships to auto-preload.
66
66
  */
67
67
  $with: ReadonlyArray<PreloadEntry>;
68
- boot(): void;
68
+ beforeFindHook(query: ModelQueryBuilderContract<any, any>): void;
69
+ beforeFetchHook(query: ModelQueryBuilderContract<any, any>): void;
70
+ beforePaginateHook(queries: any): void;
69
71
  applyAutoPreload(query: ModelQueryBuilderContract<any, any>): void;
70
72
  handleNestedPreload(query: any, parts: string[]): void;
71
73
  /**
@@ -112,6 +114,7 @@ export declare function AutoPreload<T extends NormalizeConstructor<LucidModel>>(
112
114
  <Model extends LucidModel>(this: Model, name: string): import("@adonisjs/lucid/types/relations").RelationshipsContract;
113
115
  };
114
116
  $defineProperty: <Model extends LucidModel, Prop extends keyof Model>(this: Model, propertyName: Prop, defaultValue: Model[Prop], strategy: "inherit" | "define" | ((value: Model[Prop]) => Model[Prop])) => void;
117
+ boot: () => void;
115
118
  before: {
116
119
  <Model extends LucidModel, Event extends "find" | "fetch">(this: Model, event: Event, handler: import("@adonisjs/lucid/types/model").HooksHandler<ModelQueryBuilderContract<Model>, Event>): void;
117
120
  <Model extends LucidModel>(this: Model, event: "paginate", handler: import("@adonisjs/lucid/types/model").HooksHandler<[ModelQueryBuilderContract<Model>, ModelQueryBuilderContract<Model>], "paginate">): void;
@@ -1,21 +1,19 @@
1
+ import { beforeFetch, beforeFind, beforePaginate } from '@adonisjs/lucid/orm';
1
2
  export function AutoPreload(superclass) {
2
- return class extends superclass {
3
+ class AutoPreloadModel extends superclass {
3
4
  /**
4
5
  * List of relationships to auto-preload.
5
6
  */
6
7
  static $with = [];
7
- static boot() {
8
- if (this.booted)
9
- return;
10
- super.boot();
11
- // We use 'find' and 'fetch' hooks
12
- this.before('find', (query) => this.applyAutoPreload(query));
13
- this.before('fetch', (query) => this.applyAutoPreload(query));
14
- // Lucid v7 paginate sends [countQuery, mainQuery]
15
- this.before('paginate', (queries) => {
16
- const query = Array.isArray(queries) ? queries[1] : queries;
17
- this.applyAutoPreload(query);
18
- });
8
+ static beforeFindHook(query) {
9
+ this.applyAutoPreload(query);
10
+ }
11
+ static beforeFetchHook(query) {
12
+ this.applyAutoPreload(query);
13
+ }
14
+ static beforePaginateHook(queries) {
15
+ const main = Array.isArray(queries) ? (queries[1] ?? queries[0]) : queries;
16
+ this.applyAutoPreload(main);
19
17
  }
20
18
  static applyAutoPreload(query) {
21
19
  // Check if auto-preload has been disabled for this specific query instance
@@ -73,5 +71,11 @@ export function AutoPreload(superclass) {
73
71
  query.$disableAutoPreload = true;
74
72
  return query;
75
73
  }
76
- };
74
+ }
75
+ // Apply Lucid hooks decorators without using `@decorator` syntax
76
+ // (some package tsconfigs disable the decorator syntax).
77
+ beforeFind()(AutoPreloadModel, 'beforeFindHook');
78
+ beforeFetch()(AutoPreloadModel, 'beforeFetchHook');
79
+ beforePaginate()(AutoPreloadModel, 'beforePaginateHook');
80
+ return AutoPreloadModel;
77
81
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codenameryuu/adonis-lucid-auto-preload",
3
- "version": "1.9.0",
3
+ "version": "2.1.0",
4
4
  "description": "Auto-preload multiple relationships when retrieving Lucid models on Adonis JS 7",
5
5
  "author": "codenameryuu",
6
6
  "license": "MIT",