@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
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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