@codenameryuu/adonis-lucid-auto-preload 1.2.6 → 1.4.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/adonis-typings/auto-preload.js +1 -2
- package/build/adonis-typings/auto-preload.js.map +1 -0
- package/build/adonis-typings/container.d.ts +3 -3
- package/build/adonis-typings/container.js +1 -2
- package/build/adonis-typings/container.js.map +1 -0
- package/build/adonis-typings/index.js +1 -4
- package/build/adonis-typings/index.js.map +1 -0
- package/build/chunk-2ZUAELPR.js +126 -0
- package/build/chunk-2ZUAELPR.js.map +1 -0
- package/build/chunk-4THCFWWY.js +14 -0
- package/build/chunk-4THCFWWY.js.map +1 -0
- package/build/chunk-TQK7SWBT.js +12 -0
- package/build/chunk-TQK7SWBT.js.map +1 -0
- package/build/commands/index.d.ts +2 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +11 -0
- package/build/index.js.map +1 -0
- package/build/providers/{AutoPreloadProvider.d.ts → auto_preload_provider.d.ts} +1 -0
- package/build/providers/auto_preload_provider.js +30 -0
- package/build/providers/auto_preload_provider.js.map +1 -0
- package/build/src/exceptions/wrong_argument_type_exception.js +7 -0
- package/build/src/exceptions/wrong_argument_type_exception.js.map +1 -0
- package/build/src/exceptions/wrong_relationship_type_exception.js +7 -0
- package/build/src/exceptions/wrong_relationship_type_exception.js.map +1 -0
- package/build/src/{Mixins/AutoPreload.d.ts → mixins/auto_preload.d.ts} +2 -2
- package/package.json +58 -51
- package/build/configure.js +0 -12
- package/build/providers/AutoPreloadProvider.js +0 -19
- package/build/src/Exceptions/WrongArgumentTypeException.js +0 -9
- package/build/src/Exceptions/WrongRelationshipTypeException.js +0 -9
- package/build/src/Mixins/AutoPreload.js +0 -119
- /package/build/src/{Exceptions/WrongArgumentTypeException.d.ts → exceptions/wrong_argument_type_exception.d.ts} +0 -0
- /package/build/src/{Exceptions/WrongRelationshipTypeException.d.ts → exceptions/wrong_relationship_type_exception.d.ts} +0 -0
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
//# sourceMappingURL=auto-preload.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { AutoPreloadMixin } from
|
|
2
|
-
declare module
|
|
1
|
+
import type { AutoPreloadMixin } from "./auto-preload.js";
|
|
2
|
+
declare module "@adonisjs/core/types" {
|
|
3
3
|
interface ContainerBindings {
|
|
4
|
-
|
|
4
|
+
"@codenameryuu/adonis-lucid-auto-preload": {
|
|
5
5
|
AutoPreload: AutoPreloadMixin;
|
|
6
6
|
};
|
|
7
7
|
}
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
//# sourceMappingURL=container.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import {
|
|
2
|
+
WrongArgumentTypeException
|
|
3
|
+
} from "./chunk-TQK7SWBT.js";
|
|
4
|
+
import {
|
|
5
|
+
WrongRelationshipTypeException
|
|
6
|
+
} from "./chunk-4THCFWWY.js";
|
|
7
|
+
|
|
8
|
+
// configure.ts
|
|
9
|
+
async function configure(command) {
|
|
10
|
+
const codemods = await command.createCodemods();
|
|
11
|
+
await codemods.updateRcFile((rcFile) => {
|
|
12
|
+
rcFile.addProvider("@codenameryuu/adonis-lucid-auto-preload/provider");
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// src/mixins/auto_preload.ts
|
|
17
|
+
var AutoPreload = (superclass) => {
|
|
18
|
+
class AutoPreloadModel extends superclass {
|
|
19
|
+
static $with = [];
|
|
20
|
+
static $originalWith = [];
|
|
21
|
+
static boot() {
|
|
22
|
+
if (this.booted) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (this.$with.length > 0) {
|
|
26
|
+
const isWrongType = this.$with.every((relationship) => {
|
|
27
|
+
return !["function", "string"].includes(typeof relationship);
|
|
28
|
+
});
|
|
29
|
+
if (isWrongType) {
|
|
30
|
+
throw WrongRelationshipTypeException.invoke(this.name);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
super.boot();
|
|
34
|
+
this.$originalWith = [...this.$with];
|
|
35
|
+
for (const hook of ["fetch", "find"]) {
|
|
36
|
+
this.before(hook, (query) => {
|
|
37
|
+
this.handleAutoPreload(query);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
this.before("paginate", ([_, query]) => {
|
|
41
|
+
this.handleAutoPreload(query, false);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
static without(relationships) {
|
|
45
|
+
this.checkArrayOfRelationships("without", relationships);
|
|
46
|
+
this.$with = this.$with.filter((relationship) => {
|
|
47
|
+
if (typeof relationship === "string") {
|
|
48
|
+
return !relationships.includes(relationship);
|
|
49
|
+
} else if (typeof relationship === "function") {
|
|
50
|
+
return relationship;
|
|
51
|
+
} else {
|
|
52
|
+
throw WrongArgumentTypeException.invoke(relationship);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
static withOnly(relationships) {
|
|
58
|
+
this.checkArrayOfRelationships("withOnly", relationships);
|
|
59
|
+
this.$with = this.$with.filter((relationship) => {
|
|
60
|
+
if (typeof relationship === "string") {
|
|
61
|
+
return relationships.includes(relationship);
|
|
62
|
+
} else if (typeof relationship === "function") {
|
|
63
|
+
return relationship;
|
|
64
|
+
} else {
|
|
65
|
+
throw WrongArgumentTypeException.invoke(relationship);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
static withoutAny() {
|
|
71
|
+
this.$with = [];
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
static handleAutoPreload(query, restorePreloads = true) {
|
|
75
|
+
const preloads = this.$with;
|
|
76
|
+
if (preloads.length > 0) {
|
|
77
|
+
for (const preload of preloads) {
|
|
78
|
+
if (typeof preload === "string") {
|
|
79
|
+
if (preload.includes(".")) {
|
|
80
|
+
this.handleNestedRelationships(query, preload.split("."));
|
|
81
|
+
} else {
|
|
82
|
+
query.preload(preload);
|
|
83
|
+
}
|
|
84
|
+
} else if (typeof preload === "function") {
|
|
85
|
+
preload(query);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (restorePreloads) {
|
|
90
|
+
this.$with = [...this.$originalWith];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Recursive function to handle nested relationships.
|
|
95
|
+
*/
|
|
96
|
+
static handleNestedRelationships(query, relationships) {
|
|
97
|
+
if (relationships.length > 0) {
|
|
98
|
+
const nextRelation = relationships.shift();
|
|
99
|
+
if (nextRelation) {
|
|
100
|
+
query.preload(nextRelation, (qb) => {
|
|
101
|
+
if (relationships.length > 0) {
|
|
102
|
+
this.handleNestedRelationships(qb, relationships);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
static checkArrayOfRelationships(method, relationships) {
|
|
109
|
+
if (relationships.length > 0) {
|
|
110
|
+
const isWrongType = relationships.every((relationship) => {
|
|
111
|
+
return !["function", "string"].includes(typeof relationship);
|
|
112
|
+
});
|
|
113
|
+
if (isWrongType) {
|
|
114
|
+
throw WrongArgumentTypeException.invoke(method);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return AutoPreloadModel;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export {
|
|
123
|
+
configure,
|
|
124
|
+
AutoPreload
|
|
125
|
+
};
|
|
126
|
+
//# sourceMappingURL=chunk-2ZUAELPR.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../configure.ts","../src/mixins/auto_preload.ts"],"sourcesContent":["import type Configure from '@adonisjs/core/commands/configure'\n\nexport async function configure(command: Configure) {\n const codemods = await command.createCodemods()\n\n /**\n * Register the provider inside `adonisrc.ts`\n */\n await codemods.updateRcFile((rcFile) => {\n rcFile.addProvider('@codenameryuu/adonis-lucid-auto-preload/provider')\n })\n}\n","import type { NormalizeConstructor } from \"@adonisjs/core/types/helpers\";\nimport type { LucidModel, ModelQueryBuilderContract } from \"@adonisjs/lucid/types/model\";\n\nimport WrongRelationshipTypeException from \"../exceptions/wrong_relationship_type_exception.ts\";\nimport WrongArgumentTypeException from \"../exceptions/wrong_argument_type_exception.ts\";\n\ntype AutoPreloadMixin = <T extends NormalizeConstructor<LucidModel>>(\n superclass: T\n) => T & {\n $with: Array<string | ((query: any) => void)>;\n without(this: T, relationships: Array<string>): T;\n withOnly(this: T, relationships: Array<string>): T;\n withoutAny(this: T): T;\n new (...args: Array<any>): {};\n};\n\nexport const AutoPreload: AutoPreloadMixin = (superclass) => {\n class AutoPreloadModel extends superclass {\n public static $with: Array<any> = [];\n\n protected static $originalWith: Array<any> = [];\n\n public static boot() {\n if (this.booted) {\n return;\n }\n\n if (this.$with.length > 0) {\n const isWrongType = this.$with.every((relationship) => {\n return ![\"function\", \"string\"].includes(typeof relationship);\n });\n\n if (isWrongType) {\n throw WrongRelationshipTypeException.invoke(this.name);\n }\n }\n\n super.boot();\n\n this.$originalWith = [...this.$with];\n\n for (const hook of [\"fetch\", \"find\"] as const) {\n this.before(hook, (query: any) => {\n this.handleAutoPreload(query);\n });\n }\n\n this.before(\"paginate\", ([_, query]: [ModelQueryBuilderContract<any>, ModelQueryBuilderContract<any>]) => {\n this.handleAutoPreload(query, false);\n });\n }\n\n public static without(relationships: any): any {\n this.checkArrayOfRelationships(\"without\", relationships);\n\n this.$with = this.$with.filter((relationship) => {\n if (typeof relationship === \"string\") {\n return !relationships.includes(relationship);\n } else if (typeof relationship === \"function\") {\n return relationship;\n } else {\n throw WrongArgumentTypeException.invoke(relationship);\n }\n });\n\n return this;\n }\n\n public static withOnly(relationships: any): any {\n this.checkArrayOfRelationships(\"withOnly\", relationships);\n\n this.$with = this.$with.filter((relationship) => {\n if (typeof relationship === \"string\") {\n return relationships.includes(relationship);\n } else if (typeof relationship === \"function\") {\n return relationship;\n } else {\n throw WrongArgumentTypeException.invoke(relationship);\n }\n });\n\n return this;\n }\n\n public static withoutAny(): any {\n this.$with = [];\n\n return this;\n }\n\n private static handleAutoPreload(query: ModelQueryBuilderContract<any>, restorePreloads = true) {\n const preloads = this.$with;\n\n if (preloads.length > 0) {\n for (const preload of preloads) {\n if (typeof preload === \"string\") {\n if (preload.includes(\".\")) {\n this.handleNestedRelationships(query, preload.split(\".\") as any);\n } else {\n query.preload(preload as any);\n }\n } else if (typeof preload === \"function\") {\n preload(query);\n }\n }\n }\n\n if (restorePreloads) {\n this.$with = [...this.$originalWith];\n }\n }\n\n /**\n * Recursive function to handle nested relationships.\n */\n private static handleNestedRelationships(query: ModelQueryBuilderContract<any>, relationships: any) {\n if (relationships.length > 0) {\n const nextRelation = relationships.shift();\n\n if (nextRelation) {\n query.preload(nextRelation, (qb: any) => {\n if (relationships.length > 0) {\n this.handleNestedRelationships(qb, relationships);\n }\n });\n }\n }\n }\n\n private static checkArrayOfRelationships(method: string, relationships: Array<any>) {\n if (relationships.length > 0) {\n const isWrongType = relationships.every((relationship: any) => {\n return ![\"function\", \"string\"].includes(typeof relationship);\n });\n\n if (isWrongType) {\n throw WrongArgumentTypeException.invoke(method);\n }\n }\n }\n }\n\n return AutoPreloadModel;\n};\n"],"mappings":";;;;;;;;AAEA,eAAsB,UAAU,SAAoB;AAClD,QAAM,WAAW,MAAM,QAAQ,eAAe;AAK9C,QAAM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cAAM,cAAc,cAAc,MAAM,CAAC,iBAAsB;AAC7D,iBAAO,CAAC,CAAC,YAAY,QAAQ,EAAE,SAAS,OAAO,YAAY;AAAA,QAC7D,CAAC;AAED,YAAI,aAAa;AACf,gBAAM,2BAA2B,OAAO,MAAM;AAAA,QAChD;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/exceptions/wrong_relationship_type_exception.ts
|
|
2
|
+
import { Exception } from "@adonisjs/core/exceptions";
|
|
3
|
+
var WrongRelationshipTypeException = class extends Exception {
|
|
4
|
+
static invoke(model) {
|
|
5
|
+
return new this(
|
|
6
|
+
`The model "${model}" has wrong relationships to be auto-preloaded. Only string and function types are allowed`
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
WrongRelationshipTypeException
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=chunk-4THCFWWY.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/exceptions/wrong_relationship_type_exception.ts"],"sourcesContent":["import { Exception } from '@adonisjs/core/exceptions'\n\nexport default class WrongRelationshipTypeException extends Exception {\n public static invoke(model: string) {\n return new this(\n `The model \"${model}\" has wrong relationships to be auto-preloaded. Only string and function types are allowed`\n )\n }\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAE1B,IAAqB,iCAArB,cAA4D,UAAU;AAAA,EACpE,OAAc,OAAO,OAAe;AAClC,WAAO,IAAI;AAAA,MACT,cAAc,KAAK;AAAA,IACrB;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// src/exceptions/wrong_argument_type_exception.ts
|
|
2
|
+
import { Exception } from "@adonisjs/core/exceptions";
|
|
3
|
+
var WrongArgumentTypeException = class extends Exception {
|
|
4
|
+
static invoke(method) {
|
|
5
|
+
return new this(`The method ${method} accepts only an array of strings`);
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export {
|
|
10
|
+
WrongArgumentTypeException
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=chunk-TQK7SWBT.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/exceptions/wrong_argument_type_exception.ts"],"sourcesContent":["import { Exception } from '@adonisjs/core/exceptions'\n\nexport default class WrongArgumentTypeException extends Exception {\n public static invoke(method: string) {\n return new this(`The method ${method} accepts only an array of strings`)\n }\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAE1B,IAAqB,6BAArB,cAAwD,UAAU;AAAA,EAChE,OAAc,OAAO,QAAgB;AACnC,WAAO,IAAI,KAAK,cAAc,MAAM,mCAAmC;AAAA,EACzE;AACF;","names":[]}
|
package/build/index.d.ts
ADDED
package/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AutoPreload,
|
|
3
|
+
configure
|
|
4
|
+
} from "../chunk-2ZUAELPR.js";
|
|
5
|
+
import "../chunk-TQK7SWBT.js";
|
|
6
|
+
import "../chunk-4THCFWWY.js";
|
|
7
|
+
|
|
8
|
+
// providers/auto_preload_provider.ts
|
|
9
|
+
var AutoPreloadProvider = class {
|
|
10
|
+
constructor(app) {
|
|
11
|
+
this.app = app;
|
|
12
|
+
}
|
|
13
|
+
static needsApplication = true;
|
|
14
|
+
register() {
|
|
15
|
+
this.app.container.singleton("@codenameryuu/adonis-lucid-auto-preload", () => {
|
|
16
|
+
return { AutoPreload };
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
async boot() {
|
|
20
|
+
}
|
|
21
|
+
async ready() {
|
|
22
|
+
}
|
|
23
|
+
async shutdown() {
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
export {
|
|
27
|
+
configure,
|
|
28
|
+
AutoPreloadProvider as default
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=auto_preload_provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../providers/auto_preload_provider.ts"],"sourcesContent":["import type { ApplicationService } from \"@adonisjs/core/types\";\nimport { AutoPreload } from \"../src/mixins/auto_preload.ts\";\nexport { configure } from \"../configure.ts\";\n\nexport default class AutoPreloadProvider {\n public static needsApplication = true;\n\n constructor(protected app: ApplicationService) {}\n\n public register() {\n this.app.container.singleton(\"@codenameryuu/adonis-lucid-auto-preload\", () => {\n return { AutoPreload };\n });\n }\n\n public async boot() {}\n\n public async ready() {}\n\n public async shutdown() {}\n}\n"],"mappings":";;;;;;;;AAIA,IAAqB,sBAArB,MAAyC;AAAA,EAGvC,YAAsB,KAAyB;AAAzB;AAAA,EAA0B;AAAA,EAFhD,OAAc,mBAAmB;AAAA,EAI1B,WAAW;AAChB,SAAK,IAAI,UAAU,UAAU,2CAA2C,MAAM;AAC5E,aAAO,EAAE,YAAY;AAAA,IACvB,CAAC;AAAA,EACH;AAAA,EAEA,MAAa,OAAO;AAAA,EAAC;AAAA,EAErB,MAAa,QAAQ;AAAA,EAAC;AAAA,EAEtB,MAAa,WAAW;AAAA,EAAC;AAC3B;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { NormalizeConstructor } from
|
|
2
|
-
import type { LucidModel } from
|
|
1
|
+
import type { NormalizeConstructor } from "@adonisjs/core/types/helpers";
|
|
2
|
+
import type { LucidModel } from "@adonisjs/lucid/types/model";
|
|
3
3
|
type AutoPreloadMixin = <T extends NormalizeConstructor<LucidModel>>(superclass: T) => T & {
|
|
4
4
|
$with: Array<string | ((query: any) => void)>;
|
|
5
5
|
without(this: T, relationships: Array<string>): T;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codenameryuu/adonis-lucid-auto-preload",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Auto-preload multiple relationships when retrieving Lucid models on Adonis JS 7",
|
|
5
5
|
"author": "codenameryuu",
|
|
6
6
|
"license": "MIT",
|
|
@@ -12,23 +12,31 @@
|
|
|
12
12
|
"engines": {
|
|
13
13
|
"node": ">=24.0.0"
|
|
14
14
|
},
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
"
|
|
15
|
+
"type": "module",
|
|
16
|
+
"files": [
|
|
17
|
+
"build",
|
|
18
|
+
"!build/bin",
|
|
19
|
+
"!build/tests"
|
|
19
20
|
],
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./build/index.js",
|
|
23
|
+
"./adonis-typings": "./build/adonis-typings/*.js",
|
|
24
|
+
"./exceptions": "./build/exceptions/*.js",
|
|
25
|
+
"./providers": "./build/providers/auto_preload_provider.js"
|
|
26
|
+
},
|
|
20
27
|
"scripts": {
|
|
21
|
-
"
|
|
22
|
-
"build": "npm run clean && tsc",
|
|
23
|
-
"start": "node server.js",
|
|
24
|
-
"lint": "eslint . --ext=.ts",
|
|
28
|
+
"lint": "eslint .",
|
|
25
29
|
"format": "prettier --write .",
|
|
26
|
-
"pretest": "npm run lint",
|
|
27
|
-
"test": "node -r @adonisjs/require-ts/build/register bin",
|
|
28
30
|
"clean": "del-cli build",
|
|
29
|
-
"
|
|
30
|
-
"compile": "
|
|
31
|
-
"
|
|
31
|
+
"precompile": "npm run lint && npm run clean",
|
|
32
|
+
"compile": "tsup-node && tsc --emitDeclarationOnly --declaration",
|
|
33
|
+
"build": "npm run compile",
|
|
34
|
+
"pretest": "npm run lint",
|
|
35
|
+
"test": "c8 npm run quick:test",
|
|
36
|
+
"typecheck": "tsc --noEmit",
|
|
37
|
+
"version": "npm run build",
|
|
38
|
+
"prepublishOnly": "npm run build",
|
|
39
|
+
"release": "np"
|
|
32
40
|
},
|
|
33
41
|
"devDependencies": {
|
|
34
42
|
"@adonisjs/assembler": "^7.7.0",
|
|
@@ -41,64 +49,63 @@
|
|
|
41
49
|
"@japa/expect": "^3.0.7",
|
|
42
50
|
"@japa/expect-type": "^2.0.4",
|
|
43
51
|
"@japa/file-system": "^3.0.0",
|
|
44
|
-
"@japa/run-failed-tests": "^1.1.1",
|
|
45
52
|
"@japa/runner": "^5.3.0",
|
|
46
53
|
"@japa/snapshot": "^2.0.5",
|
|
47
54
|
"@japa/spec-reporter": "^1.3.3",
|
|
48
55
|
"@poppinss/dev-utils": "^2.0.3",
|
|
49
56
|
"@types/node": "^25.5.0",
|
|
57
|
+
"c8": "^9.1.0",
|
|
50
58
|
"copyfiles": "^2.4.1",
|
|
51
59
|
"del-cli": "^5.0.0",
|
|
52
60
|
"eslint": "^10.0.2",
|
|
53
61
|
"prettier": "^3.8.1",
|
|
54
|
-
"
|
|
62
|
+
"reflect-metadata": "^0.2.2",
|
|
63
|
+
"tsup": "^8.5.1",
|
|
55
64
|
"typescript": "^5.3.3",
|
|
56
65
|
"youch": "^4.1.0"
|
|
57
66
|
},
|
|
58
|
-
"dependencies": {
|
|
59
|
-
"reflect-metadata": "^0.2.2"
|
|
60
|
-
},
|
|
61
67
|
"peerDependencies": {
|
|
62
68
|
"@adonisjs/core": "^7.0.0",
|
|
63
69
|
"@adonisjs/lucid": "^22.0.0"
|
|
64
70
|
},
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
"build"
|
|
71
|
+
"keywords": [
|
|
72
|
+
"adonis",
|
|
73
|
+
"adonisjs",
|
|
74
|
+
"auto-preload"
|
|
70
75
|
],
|
|
71
76
|
"prettier": "@adonisjs/prettier-config",
|
|
72
77
|
"publishConfig": {
|
|
73
78
|
"tag": "latest",
|
|
74
79
|
"access": "public"
|
|
75
80
|
},
|
|
76
|
-
"
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
"
|
|
80
|
-
"
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
"
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
"
|
|
88
|
-
"
|
|
89
|
-
"
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
"
|
|
97
|
-
"
|
|
98
|
-
|
|
99
|
-
"
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
81
|
+
"np": {
|
|
82
|
+
"message": "chore(release): %s",
|
|
83
|
+
"tag": "latest",
|
|
84
|
+
"branch": "main",
|
|
85
|
+
"anyBranch": false
|
|
86
|
+
},
|
|
87
|
+
"c8": {
|
|
88
|
+
"reporter": [
|
|
89
|
+
"text",
|
|
90
|
+
"html"
|
|
91
|
+
],
|
|
92
|
+
"exclude": [
|
|
93
|
+
"tests/**",
|
|
94
|
+
"bin/**"
|
|
95
|
+
]
|
|
96
|
+
},
|
|
97
|
+
"tsup": {
|
|
98
|
+
"entry": [
|
|
99
|
+
"./index.ts",
|
|
100
|
+
"./src/exceptions/*.ts",
|
|
101
|
+
"./adonis-typings/*.ts",
|
|
102
|
+
"./providers/auto_preload_provider.ts"
|
|
103
|
+
],
|
|
104
|
+
"outDir": "./build",
|
|
105
|
+
"clean": true,
|
|
106
|
+
"format": "esm",
|
|
107
|
+
"dts": false,
|
|
108
|
+
"sourcemap": true,
|
|
109
|
+
"target": "esnext"
|
|
103
110
|
}
|
|
104
111
|
}
|
package/build/configure.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.configure = configure;
|
|
4
|
-
async function configure(command) {
|
|
5
|
-
const codemods = await command.createCodemods();
|
|
6
|
-
/**
|
|
7
|
-
* Register the provider inside `adonisrc.ts`
|
|
8
|
-
*/
|
|
9
|
-
await codemods.updateRcFile((rcFile) => {
|
|
10
|
-
rcFile.addProvider('@codenameryuu/adonis-lucid-auto-preload/provider');
|
|
11
|
-
});
|
|
12
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
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;
|
|
@@ -1,9 +0,0 @@
|
|
|
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;
|
|
@@ -1,9 +0,0 @@
|
|
|
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;
|
|
@@ -1,119 +0,0 @@
|
|
|
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;
|
|
File without changes
|