@codenameryuu/adonis-lucid-auto-preload 2.0.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,49 +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
- 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
- });
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);
47
17
  }
48
18
  static applyAutoPreload(query) {
49
19
  // Check if auto-preload has been disabled for this specific query instance
@@ -101,5 +71,11 @@ export function AutoPreload(superclass) {
101
71
  query.$disableAutoPreload = true;
102
72
  return query;
103
73
  }
104
- };
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;
105
81
  }
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.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",