@codenameryuu/adonis-lucid-auto-preload 1.2.0 → 1.2.3
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/adonis-typings/auto-preload.d.ts +15 -0
- package/build/adonis-typings/auto-preload.js +2 -0
- package/build/adonis-typings/container.d.ts +8 -0
- package/build/adonis-typings/container.js +2 -0
- package/build/adonis-typings/index.d.ts +1 -0
- package/build/adonis-typings/index.js +4 -0
- package/build/providers/AutoPreloadProvider.d.ts +10 -0
- package/build/providers/AutoPreloadProvider.js +19 -0
- package/build/src/Exceptions/WrongArgumentTypeException.d.ts +4 -0
- package/build/src/Exceptions/WrongArgumentTypeException.js +9 -0
- package/build/src/Exceptions/WrongRelationshipTypeException.d.ts +4 -0
- package/build/src/Exceptions/WrongRelationshipTypeException.js +9 -0
- package/build/src/Mixins/AutoPreload.d.ts +11 -0
- package/build/src/Mixins/AutoPreload.js +119 -0
- package/package.json +6 -4
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { NormalizeConstructor } from '@adonisjs/core/types/helpers';
|
|
2
|
+
import type { LucidModel } from '@adonisjs/lucid/types/model';
|
|
3
|
+
type GetWith<T> = T extends {
|
|
4
|
+
$with: Array<infer Item>;
|
|
5
|
+
} ? Item extends string ? Item : string : string;
|
|
6
|
+
export interface AutoPreloadMixin {
|
|
7
|
+
<T extends NormalizeConstructor<LucidModel>>(superclass: T): T & {
|
|
8
|
+
$with: Array<string | ((query: any) => void)>;
|
|
9
|
+
without(this: T, relationships: Array<GetWith<T>>): T;
|
|
10
|
+
withOnly(this: T, relationships: Array<GetWith<T>>): T;
|
|
11
|
+
withoutAny(this: T): T;
|
|
12
|
+
new (...args: Array<any>): {};
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ApplicationService } from "@adonisjs/core/types";
|
|
2
|
+
export default class AutoPreloadProvider {
|
|
3
|
+
protected app: ApplicationService;
|
|
4
|
+
static needsApplication: boolean;
|
|
5
|
+
constructor(app: ApplicationService);
|
|
6
|
+
register(): void;
|
|
7
|
+
boot(): Promise<void>;
|
|
8
|
+
ready(): Promise<void>;
|
|
9
|
+
shutdown(): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const AutoPreload_1 = require("../src/Mixins/AutoPreload");
|
|
4
|
+
class AutoPreloadProvider {
|
|
5
|
+
app;
|
|
6
|
+
static needsApplication = true;
|
|
7
|
+
constructor(app) {
|
|
8
|
+
this.app = app;
|
|
9
|
+
}
|
|
10
|
+
register() {
|
|
11
|
+
this.app.container.singleton("@codenameryuu/adonis-lucid-auto-preload", () => {
|
|
12
|
+
return { AutoPreload: AutoPreload_1.AutoPreload };
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
async boot() { }
|
|
16
|
+
async ready() { }
|
|
17
|
+
async shutdown() { }
|
|
18
|
+
}
|
|
19
|
+
exports.default = AutoPreloadProvider;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const exceptions_1 = require("@adonisjs/core/exceptions");
|
|
4
|
+
class WrongArgumentTypeException extends exceptions_1.Exception {
|
|
5
|
+
static invoke(method) {
|
|
6
|
+
return new this(`The method ${method} accepts only an array of strings`);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
exports.default = WrongArgumentTypeException;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const exceptions_1 = require("@adonisjs/core/exceptions");
|
|
4
|
+
class WrongRelationshipTypeException extends exceptions_1.Exception {
|
|
5
|
+
static invoke(model) {
|
|
6
|
+
return new this(`The model "${model}" has wrong relationships to be auto-preloaded. Only string and function types are allowed`);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
exports.default = WrongRelationshipTypeException;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { NormalizeConstructor } from '@adonisjs/core/types/helpers';
|
|
2
|
+
import type { LucidModel } from '@adonisjs/lucid/types/model';
|
|
3
|
+
type AutoPreloadMixin = <T extends NormalizeConstructor<LucidModel>>(superclass: T) => T & {
|
|
4
|
+
$with: Array<string | ((query: any) => void)>;
|
|
5
|
+
without(this: T, relationships: Array<string>): T;
|
|
6
|
+
withOnly(this: T, relationships: Array<string>): T;
|
|
7
|
+
withoutAny(this: T): T;
|
|
8
|
+
new (...args: Array<any>): {};
|
|
9
|
+
};
|
|
10
|
+
export declare const AutoPreload: AutoPreloadMixin;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.AutoPreload = void 0;
|
|
7
|
+
const WrongRelationshipTypeException_1 = __importDefault(require("../Exceptions/WrongRelationshipTypeException"));
|
|
8
|
+
const WrongArgumentTypeException_1 = __importDefault(require("../Exceptions/WrongArgumentTypeException"));
|
|
9
|
+
const AutoPreload = (superclass) => {
|
|
10
|
+
class AutoPreloadModel extends superclass {
|
|
11
|
+
static $with = [];
|
|
12
|
+
static $originalWith = [];
|
|
13
|
+
static boot() {
|
|
14
|
+
if (this.booted) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (this.$with.length > 0) {
|
|
18
|
+
const isWrongType = this.$with.every((relationship) => {
|
|
19
|
+
return !['function', 'string'].includes(typeof relationship);
|
|
20
|
+
});
|
|
21
|
+
if (isWrongType) {
|
|
22
|
+
throw WrongRelationshipTypeException_1.default.invoke(this.name);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
super.boot();
|
|
26
|
+
this.$originalWith = [...this.$with];
|
|
27
|
+
for (const hook of ['fetch', 'find']) {
|
|
28
|
+
this.before(hook, (query) => {
|
|
29
|
+
this.handleAutoPreload(query);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
this.before('paginate', ([_, query]) => {
|
|
33
|
+
this.handleAutoPreload(query, false);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
static without(relationships) {
|
|
37
|
+
this.checkArrayOfRelationships('without', relationships);
|
|
38
|
+
this.$with = this.$with.filter((relationship) => {
|
|
39
|
+
if (typeof relationship === 'string') {
|
|
40
|
+
return !relationships.includes(relationship);
|
|
41
|
+
}
|
|
42
|
+
else if (typeof relationship === 'function') {
|
|
43
|
+
return relationship;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
throw WrongArgumentTypeException_1.default.invoke(relationship);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
static withOnly(relationships) {
|
|
52
|
+
this.checkArrayOfRelationships('withOnly', relationships);
|
|
53
|
+
this.$with = this.$with.filter((relationship) => {
|
|
54
|
+
if (typeof relationship === 'string') {
|
|
55
|
+
return relationships.includes(relationship);
|
|
56
|
+
}
|
|
57
|
+
else if (typeof relationship === 'function') {
|
|
58
|
+
return relationship;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
throw WrongArgumentTypeException_1.default.invoke(relationship);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
static withoutAny() {
|
|
67
|
+
this.$with = [];
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
static handleAutoPreload(query, restorePreloads = true) {
|
|
71
|
+
const preloads = this.$with;
|
|
72
|
+
if (preloads.length > 0) {
|
|
73
|
+
for (const preload of preloads) {
|
|
74
|
+
if (typeof preload === 'string') {
|
|
75
|
+
if (preload.includes('.')) {
|
|
76
|
+
this.handleNestedRelationships(query, preload.split('.'));
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
query.preload(preload);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else if (typeof preload === 'function') {
|
|
83
|
+
preload(query);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (restorePreloads) {
|
|
88
|
+
this.$with = [...this.$originalWith];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Recursive function to handle nested relationships.
|
|
93
|
+
*/
|
|
94
|
+
static handleNestedRelationships(query, relationships) {
|
|
95
|
+
if (relationships.length > 0) {
|
|
96
|
+
const nextRelation = relationships.shift();
|
|
97
|
+
if (nextRelation) {
|
|
98
|
+
query.preload(nextRelation, (qb) => {
|
|
99
|
+
if (relationships.length > 0) {
|
|
100
|
+
this.handleNestedRelationships(qb, relationships);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
static checkArrayOfRelationships(method, relationships) {
|
|
107
|
+
if (relationships.length > 0) {
|
|
108
|
+
const isWrongType = relationships.every((relationship) => {
|
|
109
|
+
return !['function', 'string'].includes(typeof relationship);
|
|
110
|
+
});
|
|
111
|
+
if (isWrongType) {
|
|
112
|
+
throw WrongArgumentTypeException_1.default.invoke(method);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return AutoPreloadModel;
|
|
118
|
+
};
|
|
119
|
+
exports.AutoPreload = AutoPreload;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codenameryuu/adonis-lucid-auto-preload",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "Auto-preload multiple relationships when retrieving Lucid models on Adonis JS 7",
|
|
5
5
|
"author": "codenameryuu",
|
|
6
6
|
"license": "MIT",
|
|
@@ -28,8 +28,7 @@
|
|
|
28
28
|
"clean": "del-cli build",
|
|
29
29
|
"copyfiles": "copyfiles \"templates/**/*.txt\" \"instructions.md\" build",
|
|
30
30
|
"compile": "npm run lint && npm run clean && tsc && npm run copyfiles",
|
|
31
|
-
"commit": "git-cz"
|
|
32
|
-
"release": "np --message=\"chore(release): %s\""
|
|
31
|
+
"commit": "git-cz"
|
|
33
32
|
},
|
|
34
33
|
"devDependencies": {
|
|
35
34
|
"@adonisjs/assembler": "^7.7.0",
|
|
@@ -81,5 +80,8 @@
|
|
|
81
80
|
"build/providers",
|
|
82
81
|
"build/src",
|
|
83
82
|
"build/instructions.md"
|
|
84
|
-
]
|
|
83
|
+
],
|
|
84
|
+
"exports": {
|
|
85
|
+
".": "./build/providers/AutoPreloadProvider.js"
|
|
86
|
+
}
|
|
85
87
|
}
|