@codenameryuu/adonis-lucid-auto-preload 1.8.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.
|
@@ -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
|
}
|
|
@@ -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() { }
|
|
@@ -72,6 +72,7 @@ export declare function AutoPreload<T extends NormalizeConstructor<LucidModel>>(
|
|
|
72
72
|
* Correct Implementation: Returns the Query Builder to avoid global state pollution
|
|
73
73
|
*/
|
|
74
74
|
without(relations: string[]): ModelQueryBuilderContract</*elided*/ any & T, InstanceType</*elided*/ any & T>>;
|
|
75
|
+
withOnly(relations: string[]): ModelQueryBuilderContract</*elided*/ any & T, InstanceType</*elided*/ any & T>>;
|
|
75
76
|
withoutAny(): ModelQueryBuilderContract</*elided*/ any & T, InstanceType</*elided*/ any & T>>;
|
|
76
77
|
readonly booted: boolean;
|
|
77
78
|
$columnsDefinitions: Map<string, import("@adonisjs/lucid/types/model").ModelColumnOptions>;
|
|
@@ -8,13 +8,41 @@ export function AutoPreload(superclass) {
|
|
|
8
8
|
if (this.booted)
|
|
9
9
|
return;
|
|
10
10
|
super.boot();
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
17
|
-
|
|
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) {
|
|
@@ -26,8 +54,11 @@ export function AutoPreload(superclass) {
|
|
|
26
54
|
return;
|
|
27
55
|
// Get list of relations to skip for this specific query
|
|
28
56
|
const skipList = query.$skipPreloads || [];
|
|
57
|
+
const onlyList = query.$onlyPreloads || [];
|
|
29
58
|
for (const relation of relations) {
|
|
30
59
|
if (typeof relation === 'string') {
|
|
60
|
+
if (onlyList.length > 0 && !onlyList.includes(relation))
|
|
61
|
+
continue;
|
|
31
62
|
if (skipList.includes(relation))
|
|
32
63
|
continue;
|
|
33
64
|
if (relation.includes('.')) {
|
|
@@ -60,6 +91,11 @@ export function AutoPreload(superclass) {
|
|
|
60
91
|
query.$skipPreloads = relations;
|
|
61
92
|
return query;
|
|
62
93
|
}
|
|
94
|
+
static withOnly(relations) {
|
|
95
|
+
const query = this.query();
|
|
96
|
+
query.$onlyPreloads = relations;
|
|
97
|
+
return query;
|
|
98
|
+
}
|
|
63
99
|
static withoutAny() {
|
|
64
100
|
const query = this.query();
|
|
65
101
|
query.$disableAutoPreload = true;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codenameryuu/adonis-lucid-auto-preload",
|
|
3
|
-
"version": "
|
|
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",
|
|
@@ -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",
|