@codenameryuu/adonis-lucid-auto-preload 2.1.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.
|
@@ -65,6 +65,7 @@ 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
69
|
beforeFindHook(query: ModelQueryBuilderContract<any, any>): void;
|
|
69
70
|
beforeFetchHook(query: ModelQueryBuilderContract<any, any>): void;
|
|
70
71
|
beforePaginateHook(queries: any): void;
|
|
@@ -114,7 +115,6 @@ export declare function AutoPreload<T extends NormalizeConstructor<LucidModel>>(
|
|
|
114
115
|
<Model extends LucidModel>(this: Model, name: string): import("@adonisjs/lucid/types/relations").RelationshipsContract;
|
|
115
116
|
};
|
|
116
117
|
$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;
|
|
118
118
|
before: {
|
|
119
119
|
<Model extends LucidModel, Event extends "find" | "fetch">(this: Model, event: Event, handler: import("@adonisjs/lucid/types/model").HooksHandler<ModelQueryBuilderContract<Model>, Event>): void;
|
|
120
120
|
<Model extends LucidModel>(this: Model, event: "paginate", handler: import("@adonisjs/lucid/types/model").HooksHandler<[ModelQueryBuilderContract<Model>, ModelQueryBuilderContract<Model>], "paginate">): void;
|
|
@@ -1,10 +1,22 @@
|
|
|
1
|
-
import { beforeFetch, beforeFind, beforePaginate } from '@adonisjs/lucid/orm';
|
|
2
1
|
export function AutoPreload(superclass) {
|
|
3
2
|
class AutoPreloadModel extends superclass {
|
|
4
3
|
/**
|
|
5
4
|
* List of relationships to auto-preload.
|
|
6
5
|
*/
|
|
7
6
|
static $with = [];
|
|
7
|
+
static boot() {
|
|
8
|
+
super.boot();
|
|
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
|
+
}
|
|
8
20
|
static beforeFindHook(query) {
|
|
9
21
|
this.applyAutoPreload(query);
|
|
10
22
|
}
|
|
@@ -72,10 +84,5 @@ export function AutoPreload(superclass) {
|
|
|
72
84
|
return query;
|
|
73
85
|
}
|
|
74
86
|
}
|
|
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
87
|
return AutoPreloadModel;
|
|
81
88
|
}
|
package/package.json
CHANGED