@codenameryuu/adonis-lucid-auto-preload 1.7.0 → 1.8.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.
package/build/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { configure } from './configure.js';
|
|
2
|
-
export { AutoPreload } from './src/mixins/auto_preload.
|
|
2
|
+
export { AutoPreload } from './src/mixins/auto_preload.js';
|
package/build/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { NormalizeConstructor } from '@adonisjs/core/types/helpers';
|
|
2
2
|
import type { LucidModel, ModelQueryBuilderContract } from '@adonisjs/lucid/types/model';
|
|
3
|
+
type PreloadEntry = string | ((query: ModelQueryBuilderContract<any, any>) => void);
|
|
3
4
|
export declare function AutoPreload<T extends NormalizeConstructor<LucidModel>>(superclass: T): {
|
|
4
5
|
new (...args: any[]): {
|
|
5
6
|
$attributes: import("@adonisjs/lucid/types/model").ModelObject;
|
|
@@ -60,16 +61,18 @@ export declare function AutoPreload<T extends NormalizeConstructor<LucidModel>>(
|
|
|
60
61
|
toAttributes(): Record<string, any>;
|
|
61
62
|
related<Name extends undefined>(relation: Name): NonNullable</*elided*/ any[Name]> extends import("@adonisjs/lucid/types/relations").ModelRelations<LucidModel, LucidModel> ? (import("@adonisjs/lucid/types/relations").ModelRelations<LucidModel, LucidModel> & /*elided*/ any[Name] & {})["client"] : never;
|
|
62
63
|
};
|
|
63
|
-
$with: Array<string | any>;
|
|
64
|
-
boot(): void;
|
|
65
64
|
/**
|
|
66
|
-
*
|
|
65
|
+
* List of relationships to auto-preload.
|
|
67
66
|
*/
|
|
67
|
+
$with: ReadonlyArray<PreloadEntry>;
|
|
68
|
+
boot(): void;
|
|
68
69
|
applyAutoPreload(query: ModelQueryBuilderContract<any, any>): void;
|
|
70
|
+
handleNestedPreload(query: any, parts: string[]): void;
|
|
69
71
|
/**
|
|
70
|
-
*
|
|
72
|
+
* Correct Implementation: Returns the Query Builder to avoid global state pollution
|
|
71
73
|
*/
|
|
72
|
-
|
|
74
|
+
without(relations: string[]): ModelQueryBuilderContract</*elided*/ any & T, InstanceType</*elided*/ any & T>>;
|
|
75
|
+
withoutAny(): ModelQueryBuilderContract</*elided*/ any & T, InstanceType</*elided*/ any & T>>;
|
|
73
76
|
readonly booted: boolean;
|
|
74
77
|
$columnsDefinitions: Map<string, import("@adonisjs/lucid/types/model").ModelColumnOptions>;
|
|
75
78
|
$relationsDefinitions: Map<string, import("@adonisjs/lucid/types/relations").RelationshipsContract>;
|
|
@@ -150,3 +153,4 @@ export declare function AutoPreload<T extends NormalizeConstructor<LucidModel>>(
|
|
|
150
153
|
transaction: import("@adonisjs/lucid/types/database").TransactionFn;
|
|
151
154
|
truncate: (cascade?: boolean) => Promise<void>;
|
|
152
155
|
} & T;
|
|
156
|
+
export {};
|
|
@@ -1,27 +1,35 @@
|
|
|
1
1
|
export function AutoPreload(superclass) {
|
|
2
2
|
return class extends superclass {
|
|
3
|
+
/**
|
|
4
|
+
* List of relationships to auto-preload.
|
|
5
|
+
*/
|
|
3
6
|
static $with = [];
|
|
4
7
|
static boot() {
|
|
5
8
|
if (this.booted)
|
|
6
9
|
return;
|
|
7
10
|
super.boot();
|
|
11
|
+
// We use 'find' and 'fetch' hooks
|
|
8
12
|
this.before('find', (query) => this.applyAutoPreload(query));
|
|
9
13
|
this.before('fetch', (query) => this.applyAutoPreload(query));
|
|
14
|
+
// Lucid v7 paginate sends [countQuery, mainQuery]
|
|
10
15
|
this.before('paginate', (queries) => {
|
|
11
|
-
// Lucid v6/v7 sends [countQuery, mainQuery]
|
|
12
16
|
const query = Array.isArray(queries) ? queries[1] : queries;
|
|
13
17
|
this.applyAutoPreload(query);
|
|
14
18
|
});
|
|
15
19
|
}
|
|
16
|
-
/**
|
|
17
|
-
* Change from 'protected' to 'public' to fix the TS error
|
|
18
|
-
*/
|
|
19
20
|
static applyAutoPreload(query) {
|
|
20
|
-
|
|
21
|
+
// Check if auto-preload has been disabled for this specific query instance
|
|
22
|
+
if (query.$disableAutoPreload)
|
|
23
|
+
return;
|
|
24
|
+
const relations = this.$with;
|
|
21
25
|
if (!Array.isArray(relations))
|
|
22
26
|
return;
|
|
23
|
-
relations
|
|
27
|
+
// Get list of relations to skip for this specific query
|
|
28
|
+
const skipList = query.$skipPreloads || [];
|
|
29
|
+
for (const relation of relations) {
|
|
24
30
|
if (typeof relation === 'string') {
|
|
31
|
+
if (skipList.includes(relation))
|
|
32
|
+
continue;
|
|
25
33
|
if (relation.includes('.')) {
|
|
26
34
|
this.handleNestedPreload(query, relation.split('.'));
|
|
27
35
|
}
|
|
@@ -32,20 +40,30 @@ export function AutoPreload(superclass) {
|
|
|
32
40
|
else if (typeof relation === 'function') {
|
|
33
41
|
relation(query);
|
|
34
42
|
}
|
|
35
|
-
}
|
|
43
|
+
}
|
|
36
44
|
}
|
|
37
|
-
/**
|
|
38
|
-
* Change from 'private' to 'public'
|
|
39
|
-
*/
|
|
40
45
|
static handleNestedPreload(query, parts) {
|
|
41
46
|
const current = parts.shift();
|
|
42
47
|
if (!current)
|
|
43
48
|
return;
|
|
44
49
|
query.preload(current, (builder) => {
|
|
45
50
|
if (parts.length > 0) {
|
|
46
|
-
this.handleNestedPreload(builder, parts);
|
|
51
|
+
this.handleNestedPreload(builder, [...parts]);
|
|
47
52
|
}
|
|
48
53
|
});
|
|
49
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Correct Implementation: Returns the Query Builder to avoid global state pollution
|
|
57
|
+
*/
|
|
58
|
+
static without(relations) {
|
|
59
|
+
const query = this.query();
|
|
60
|
+
query.$skipPreloads = relations;
|
|
61
|
+
return query;
|
|
62
|
+
}
|
|
63
|
+
static withoutAny() {
|
|
64
|
+
const query = this.query();
|
|
65
|
+
query.$disableAutoPreload = true;
|
|
66
|
+
return query;
|
|
67
|
+
}
|
|
50
68
|
};
|
|
51
69
|
}
|
package/package.json
CHANGED