@codenameryuu/adonis-lucid-auto-preload 2.0.0 → 2.2.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.
@@ -66,6 +66,9 @@ export declare function AutoPreload<T extends NormalizeConstructor<LucidModel>>(
66
66
  */
67
67
  $with: ReadonlyArray<PreloadEntry>;
68
68
  boot(): void;
69
+ beforeFindHook(query: ModelQueryBuilderContract<any, any>): void;
70
+ beforeFetchHook(query: ModelQueryBuilderContract<any, any>): void;
71
+ beforePaginateHook(queries: any): void;
69
72
  applyAutoPreload(query: ModelQueryBuilderContract<any, any>): void;
70
73
  handleNestedPreload(query: any, parts: string[]): void;
71
74
  /**
@@ -1,49 +1,31 @@
1
1
  export function AutoPreload(superclass) {
2
- return class extends superclass {
2
+ class AutoPreloadModel extends superclass {
3
3
  /**
4
4
  * List of relationships to auto-preload.
5
5
  */
6
6
  static $with = [];
7
7
  static boot() {
8
- if (this.booted)
9
- return;
10
8
  super.boot();
11
- const extractQuery = (arg) => {
12
- if (arg && typeof arg === 'object' && typeof arg.preload === 'function')
13
- return arg;
14
- if (Array.isArray(arg)) {
15
- const candidate = arg.find((q) => q && typeof q === 'object' && typeof q.preload === 'function');
16
- return candidate ?? arg[1] ?? arg[0] ?? null;
17
- }
18
- return null;
19
- };
20
- const applyFrom = (arg) => {
21
- const query = extractQuery(arg);
22
- if (query)
23
- this.applyAutoPreload(query);
24
- };
25
- // Covers common Lucid model/query operations
26
- for (const hook of [
27
- 'find',
28
- 'findOrFail',
29
- 'fetch',
30
- 'first',
31
- 'firstOrFail',
32
- 'all',
33
- 'get',
34
- ]) {
35
- this.before(hook, (arg) => applyFrom(arg));
36
- }
37
- // Lucid paginate sends [countQuery, mainQuery]
38
- this.before('paginate', (queries) => {
39
- if (Array.isArray(queries)) {
40
- const main = extractQuery(queries[1]) ?? extractQuery(queries[0]);
41
- if (main)
42
- this.applyAutoPreload(main);
43
- return;
44
- }
45
- applyFrom(queries);
46
- });
9
+ // `compose()` introduces an intermediate class. Depending on how Lucid
10
+ // handles boot flags, we might see `booted` as inherited and skip hook
11
+ // registration. Track it per subclass instead.
12
+ if (this.$autoPreloadHooksRegistered)
13
+ return;
14
+ this.$autoPreloadHooksRegistered = true;
15
+ // Register hooks on the actual model subclass.
16
+ this.before('find', this.beforeFindHook.bind(this));
17
+ this.before('fetch', this.beforeFetchHook.bind(this));
18
+ this.before('paginate', this.beforePaginateHook.bind(this));
19
+ }
20
+ static beforeFindHook(query) {
21
+ this.applyAutoPreload(query);
22
+ }
23
+ static beforeFetchHook(query) {
24
+ this.applyAutoPreload(query);
25
+ }
26
+ static beforePaginateHook(queries) {
27
+ const main = Array.isArray(queries) ? (queries[1] ?? queries[0]) : queries;
28
+ this.applyAutoPreload(main);
47
29
  }
48
30
  static applyAutoPreload(query) {
49
31
  // Check if auto-preload has been disabled for this specific query instance
@@ -101,5 +83,6 @@ export function AutoPreload(superclass) {
101
83
  query.$disableAutoPreload = true;
102
84
  return query;
103
85
  }
104
- };
86
+ }
87
+ return AutoPreloadModel;
105
88
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codenameryuu/adonis-lucid-auto-preload",
3
- "version": "2.0.0",
3
+ "version": "2.2.0",
4
4
  "description": "Auto-preload multiple relationships when retrieving Lucid models on Adonis JS 7",
5
5
  "author": "codenameryuu",
6
6
  "license": "MIT",