@codenameryuu/adonis-lucid-auto-preload 1.7.0 → 1.9.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.
|
@@ -5,10 +5,10 @@ type GetWith<T> = T extends {
|
|
|
5
5
|
} ? Item extends string ? Item : string : string;
|
|
6
6
|
export interface AutoPreloadMixin {
|
|
7
7
|
<T extends NormalizeConstructor<LucidModel>>(superclass: T): T & {
|
|
8
|
-
$with:
|
|
9
|
-
without(this: T, relationships: Array<GetWith<T>>):
|
|
10
|
-
withOnly(this: T, relationships: Array<GetWith<T>>):
|
|
11
|
-
withoutAny(this: T):
|
|
8
|
+
$with: ReadonlyArray<string | ((query: any) => void)>;
|
|
9
|
+
without(this: T, relationships: Array<GetWith<T>>): any;
|
|
10
|
+
withOnly(this: T, relationships: Array<GetWith<T>>): any;
|
|
11
|
+
withoutAny(this: T): any;
|
|
12
12
|
new (...args: Array<any>): {};
|
|
13
13
|
};
|
|
14
14
|
}
|
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,3 +1,4 @@
|
|
|
1
|
+
import { AutoPreload } from '../src/mixins/auto_preload.js';
|
|
1
2
|
export { configure } from '../configure.js';
|
|
2
3
|
export default class AutoPreloadProvider {
|
|
3
4
|
app;
|
|
@@ -5,7 +6,11 @@ export default class AutoPreloadProvider {
|
|
|
5
6
|
constructor(app) {
|
|
6
7
|
this.app = app;
|
|
7
8
|
}
|
|
8
|
-
register() {
|
|
9
|
+
register() {
|
|
10
|
+
this.app.container.singleton('@codenameryuu/adonis-lucid-auto-preload', () => {
|
|
11
|
+
return { AutoPreload };
|
|
12
|
+
});
|
|
13
|
+
}
|
|
9
14
|
async boot() { }
|
|
10
15
|
async ready() { }
|
|
11
16
|
async shutdown() { }
|
|
@@ -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,19 @@ 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
|
+
withOnly(relations: string[]): ModelQueryBuilderContract</*elided*/ any & T, InstanceType</*elided*/ any & T>>;
|
|
76
|
+
withoutAny(): ModelQueryBuilderContract</*elided*/ any & T, InstanceType</*elided*/ any & T>>;
|
|
73
77
|
readonly booted: boolean;
|
|
74
78
|
$columnsDefinitions: Map<string, import("@adonisjs/lucid/types/model").ModelColumnOptions>;
|
|
75
79
|
$relationsDefinitions: Map<string, import("@adonisjs/lucid/types/relations").RelationshipsContract>;
|
|
@@ -150,3 +154,4 @@ export declare function AutoPreload<T extends NormalizeConstructor<LucidModel>>(
|
|
|
150
154
|
transaction: import("@adonisjs/lucid/types/database").TransactionFn;
|
|
151
155
|
truncate: (cascade?: boolean) => Promise<void>;
|
|
152
156
|
} & T;
|
|
157
|
+
export {};
|
|
@@ -1,27 +1,38 @@
|
|
|
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
|
+
const onlyList = query.$onlyPreloads || [];
|
|
30
|
+
for (const relation of relations) {
|
|
24
31
|
if (typeof relation === 'string') {
|
|
32
|
+
if (onlyList.length > 0 && !onlyList.includes(relation))
|
|
33
|
+
continue;
|
|
34
|
+
if (skipList.includes(relation))
|
|
35
|
+
continue;
|
|
25
36
|
if (relation.includes('.')) {
|
|
26
37
|
this.handleNestedPreload(query, relation.split('.'));
|
|
27
38
|
}
|
|
@@ -32,20 +43,35 @@ export function AutoPreload(superclass) {
|
|
|
32
43
|
else if (typeof relation === 'function') {
|
|
33
44
|
relation(query);
|
|
34
45
|
}
|
|
35
|
-
}
|
|
46
|
+
}
|
|
36
47
|
}
|
|
37
|
-
/**
|
|
38
|
-
* Change from 'private' to 'public'
|
|
39
|
-
*/
|
|
40
48
|
static handleNestedPreload(query, parts) {
|
|
41
49
|
const current = parts.shift();
|
|
42
50
|
if (!current)
|
|
43
51
|
return;
|
|
44
52
|
query.preload(current, (builder) => {
|
|
45
53
|
if (parts.length > 0) {
|
|
46
|
-
this.handleNestedPreload(builder, parts);
|
|
54
|
+
this.handleNestedPreload(builder, [...parts]);
|
|
47
55
|
}
|
|
48
56
|
});
|
|
49
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Correct Implementation: Returns the Query Builder to avoid global state pollution
|
|
60
|
+
*/
|
|
61
|
+
static without(relations) {
|
|
62
|
+
const query = this.query();
|
|
63
|
+
query.$skipPreloads = relations;
|
|
64
|
+
return query;
|
|
65
|
+
}
|
|
66
|
+
static withOnly(relations) {
|
|
67
|
+
const query = this.query();
|
|
68
|
+
query.$onlyPreloads = relations;
|
|
69
|
+
return query;
|
|
70
|
+
}
|
|
71
|
+
static withoutAny() {
|
|
72
|
+
const query = this.query();
|
|
73
|
+
query.$disableAutoPreload = true;
|
|
74
|
+
return query;
|
|
75
|
+
}
|
|
50
76
|
};
|
|
51
77
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codenameryuu/adonis-lucid-auto-preload",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "Auto-preload multiple relationships when retrieving Lucid models on Adonis JS 7",
|
|
5
5
|
"author": "codenameryuu",
|
|
6
6
|
"license": "MIT",
|
|
@@ -13,17 +13,27 @@
|
|
|
13
13
|
"node": ">=24.0.0"
|
|
14
14
|
},
|
|
15
15
|
"type": "module",
|
|
16
|
+
"types": "./build/index.d.ts",
|
|
16
17
|
"files": [
|
|
17
18
|
"build",
|
|
18
19
|
"!build/bin",
|
|
19
20
|
"!build/tests"
|
|
20
21
|
],
|
|
21
22
|
"exports": {
|
|
22
|
-
".":
|
|
23
|
-
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./build/index.d.ts",
|
|
25
|
+
"default": "./build/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./mixins": {
|
|
28
|
+
"types": "./build/src/mixins/auto_preload.d.ts",
|
|
29
|
+
"default": "./build/src/mixins/auto_preload.js"
|
|
30
|
+
},
|
|
24
31
|
"./adonis-typings/*": "./build/adonis-typings/*.js",
|
|
25
32
|
"./exceptions/*": "./build/exceptions/*.js",
|
|
26
|
-
"./provider":
|
|
33
|
+
"./provider": {
|
|
34
|
+
"types": "./build/providers/auto_preload_provider.d.ts",
|
|
35
|
+
"default": "./build/providers/auto_preload_provider.js"
|
|
36
|
+
}
|
|
27
37
|
},
|
|
28
38
|
"scripts": {
|
|
29
39
|
"clean": "del-cli build",
|