@codenameryuu/adonis-lucid-auto-preload 1.9.0 → 2.0.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.
@@ -8,13 +8,41 @@ export function AutoPreload(superclass) {
8
8
  if (this.booted)
9
9
  return;
10
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]
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]
15
38
  this.before('paginate', (queries) => {
16
- const query = Array.isArray(queries) ? queries[1] : queries;
17
- this.applyAutoPreload(query);
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);
18
46
  });
19
47
  }
20
48
  static applyAutoPreload(query) {
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.0.0",
4
4
  "description": "Auto-preload multiple relationships when retrieving Lucid models on Adonis JS 7",
5
5
  "author": "codenameryuu",
6
6
  "license": "MIT",