@codenameryuu/adonis-lucid-auto-preload 1.6.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,4 +1,3 @@
|
|
|
1
|
-
import { AutoPreload } from '../src/mixins/auto_preload.js';
|
|
2
1
|
export { configure } from '../configure.js';
|
|
3
2
|
export default class AutoPreloadProvider {
|
|
4
3
|
app;
|
|
@@ -6,11 +5,7 @@ export default class AutoPreloadProvider {
|
|
|
6
5
|
constructor(app) {
|
|
7
6
|
this.app = app;
|
|
8
7
|
}
|
|
9
|
-
register() {
|
|
10
|
-
this.app.container.singleton('@codenameryuu/adonis-lucid-auto-preload', () => {
|
|
11
|
-
return { AutoPreload };
|
|
12
|
-
});
|
|
13
|
-
}
|
|
8
|
+
register() { }
|
|
14
9
|
async boot() { }
|
|
15
10
|
async ready() { }
|
|
16
11
|
async shutdown() { }
|
|
@@ -1,11 +1,156 @@
|
|
|
1
1
|
import type { NormalizeConstructor } from '@adonisjs/core/types/helpers';
|
|
2
|
-
import type { LucidModel } from '@adonisjs/lucid/types/model';
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
import type { LucidModel, ModelQueryBuilderContract } from '@adonisjs/lucid/types/model';
|
|
3
|
+
type PreloadEntry = string | ((query: ModelQueryBuilderContract<any, any>) => void);
|
|
4
|
+
export declare function AutoPreload<T extends NormalizeConstructor<LucidModel>>(superclass: T): {
|
|
5
|
+
new (...args: any[]): {
|
|
6
|
+
$attributes: import("@adonisjs/lucid/types/model").ModelObject;
|
|
7
|
+
$extras: import("@adonisjs/lucid/types/model").ModelObject;
|
|
8
|
+
$original: import("@adonisjs/lucid/types/model").ModelObject;
|
|
9
|
+
$preloaded: {
|
|
10
|
+
[relation: string]: import("@adonisjs/lucid/types/model").LucidRow | import("@adonisjs/lucid/types/model").LucidRow[];
|
|
11
|
+
};
|
|
12
|
+
$sideloaded: import("@adonisjs/lucid/types/model").ModelObject;
|
|
13
|
+
$primaryKeyValue?: number | string;
|
|
14
|
+
$isPersisted: boolean;
|
|
15
|
+
$isNew: boolean;
|
|
16
|
+
$isLocal: boolean;
|
|
17
|
+
$dirty: import("@adonisjs/lucid/types/model").ModelObject;
|
|
18
|
+
$isDirty: boolean;
|
|
19
|
+
$isDeleted: boolean;
|
|
20
|
+
$options?: import("@adonisjs/lucid/types/model").ModelOptions;
|
|
21
|
+
$trx?: import("@adonisjs/lucid/types/database").TransactionClientContract;
|
|
22
|
+
$setOptionsAndTrx(options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions): void;
|
|
23
|
+
useTransaction(trx: import("@adonisjs/lucid/types/database").TransactionClientContract): /*elided*/ any;
|
|
24
|
+
useConnection(connection: string): /*elided*/ any;
|
|
25
|
+
$getQueryFor(action: "insert", client: import("@adonisjs/lucid/types/database").QueryClientContract): ReturnType<import("@adonisjs/lucid/types/database").QueryClientContract["insertQuery"]>;
|
|
26
|
+
$getQueryFor(action: "update" | "delete" | "refresh", client: import("@adonisjs/lucid/types/database").QueryClientContract): ModelQueryBuilderContract<LucidModel>;
|
|
27
|
+
$setAttribute(key: string, value: any): void;
|
|
28
|
+
$getAttribute(key: string): any;
|
|
29
|
+
$getAttributeFromCache(key: string, callback: import("@adonisjs/lucid/types/model").CacheNode["getter"]): any;
|
|
30
|
+
$hasRelated(key: string): boolean;
|
|
31
|
+
$setRelated(key: string, result: import("@adonisjs/lucid/types/querybuilder").OneOrMany<import("@adonisjs/lucid/types/model").LucidRow> | null): void;
|
|
32
|
+
$pushRelated(key: string, result: import("@adonisjs/lucid/types/querybuilder").OneOrMany<import("@adonisjs/lucid/types/model").LucidRow> | null): void;
|
|
33
|
+
$getRelated(key: string, defaultValue?: any): import("@adonisjs/lucid/types/querybuilder").OneOrMany<import("@adonisjs/lucid/types/model").LucidRow> | undefined | null;
|
|
34
|
+
$consumeAdapterResult(adapterResult: import("@adonisjs/lucid/types/model").ModelObject, sideloadAttributes?: import("@adonisjs/lucid/types/model").ModelObject): void;
|
|
35
|
+
$hydrateOriginals(): void;
|
|
36
|
+
fill(value: Partial<import("@adonisjs/lucid/types/model").ModelAttributes</*elided*/ any>>, allowExtraProperties?: boolean): /*elided*/ any;
|
|
37
|
+
merge(value: Partial<import("@adonisjs/lucid/types/model").ModelAttributes</*elided*/ any>>, allowExtraProperties?: boolean): /*elided*/ any;
|
|
38
|
+
isDirty(fields?: undefined[] | undefined): boolean;
|
|
39
|
+
enableForceUpdate(): /*elided*/ any;
|
|
40
|
+
save(): Promise</*elided*/ any>;
|
|
41
|
+
saveQuietly(): Promise</*elided*/ any>;
|
|
42
|
+
lockForUpdate<T_1>(callback: (user: /*elided*/ any) => T_1 | Promise<T_1>): Promise<T_1>;
|
|
43
|
+
delete(): Promise<void>;
|
|
44
|
+
deleteQuietly(): Promise<void>;
|
|
45
|
+
refresh(): Promise</*elided*/ any>;
|
|
46
|
+
load: import("@adonisjs/lucid/types/model").LucidRowPreload</*elided*/ any>;
|
|
47
|
+
loadOnce: import("@adonisjs/lucid/types/model").LucidRowPreloadOnce</*elided*/ any>;
|
|
48
|
+
preload: import("@adonisjs/lucid/types/model").LucidRowPreload</*elided*/ any>;
|
|
49
|
+
loadAggregate: <Self extends /*elided*/ any, Name extends import("@adonisjs/lucid/types/relations").ExtractModelRelations<Self>, RelatedBuilder = NonNullable<Self[Name]> extends import("@adonisjs/lucid/types/relations").ModelRelations<LucidModel, LucidModel> ? (import("@adonisjs/lucid/types/relations").ModelRelations<LucidModel, LucidModel> & Self[Name] & {})["subQuery"] : never>(name: Name, callback: (builder: RelatedBuilder) => void) => import("@adonisjs/lucid/types/model").LazyLoadAggregatesContract<Self>;
|
|
50
|
+
loadCount: <Self extends /*elided*/ any, Name extends import("@adonisjs/lucid/types/relations").ExtractModelRelations<Self>, RelatedBuilder = NonNullable<Self[Name]> extends import("@adonisjs/lucid/types/relations").ModelRelations<LucidModel, LucidModel> ? (import("@adonisjs/lucid/types/relations").ModelRelations<LucidModel, LucidModel> & Self[Name] & {})["subQuery"] : never>(name: Name, callback?: ((builder: RelatedBuilder) => void) | undefined) => import("@adonisjs/lucid/types/model").LazyLoadAggregatesContract<Self>;
|
|
51
|
+
serializeAttributes(fields?: import("@adonisjs/lucid/types/model").CherryPickFields, raw?: boolean): import("@adonisjs/lucid/types/model").ModelObject;
|
|
52
|
+
serializeComputed(fields?: import("@adonisjs/lucid/types/model").CherryPickFields): import("@adonisjs/lucid/types/model").ModelObject;
|
|
53
|
+
serializeRelations(fields: undefined, raw: true): {
|
|
54
|
+
[key: string]: import("@adonisjs/lucid/types/model").LucidRow | import("@adonisjs/lucid/types/model").LucidRow[];
|
|
55
|
+
};
|
|
56
|
+
serializeRelations(cherryPick: import("@adonisjs/lucid/types/model").CherryPick["relations"] | undefined, raw: false | undefined): import("@adonisjs/lucid/types/model").ModelObject;
|
|
57
|
+
serializeRelations(cherryPick?: import("@adonisjs/lucid/types/model").CherryPick["relations"], raw?: boolean): import("@adonisjs/lucid/types/model").ModelObject;
|
|
58
|
+
serialize(cherryPick?: import("@adonisjs/lucid/types/model").CherryPick): import("@adonisjs/lucid/types/model").ModelObject;
|
|
59
|
+
toObject(): import("@adonisjs/lucid/types/model").ModelObject;
|
|
60
|
+
toJSON(): import("@adonisjs/lucid/types/model").ModelObject;
|
|
61
|
+
toAttributes(): Record<string, any>;
|
|
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;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* List of relationships to auto-preload.
|
|
66
|
+
*/
|
|
67
|
+
$with: ReadonlyArray<PreloadEntry>;
|
|
68
|
+
boot(): void;
|
|
69
|
+
applyAutoPreload(query: ModelQueryBuilderContract<any, any>): void;
|
|
70
|
+
handleNestedPreload(query: any, parts: string[]): void;
|
|
71
|
+
/**
|
|
72
|
+
* Correct Implementation: Returns the Query Builder to avoid global state pollution
|
|
73
|
+
*/
|
|
74
|
+
without(relations: string[]): ModelQueryBuilderContract</*elided*/ any & T, InstanceType</*elided*/ any & T>>;
|
|
75
|
+
withoutAny(): ModelQueryBuilderContract</*elided*/ any & T, InstanceType</*elided*/ any & T>>;
|
|
76
|
+
readonly booted: boolean;
|
|
77
|
+
$columnsDefinitions: Map<string, import("@adonisjs/lucid/types/model").ModelColumnOptions>;
|
|
78
|
+
$relationsDefinitions: Map<string, import("@adonisjs/lucid/types/relations").RelationshipsContract>;
|
|
79
|
+
$computedDefinitions: Map<string, import("@adonisjs/lucid/types/model").ComputedOptions>;
|
|
80
|
+
primaryKey: string;
|
|
81
|
+
connection?: string | undefined;
|
|
82
|
+
namingStrategy: import("@adonisjs/lucid/types/model").NamingStrategyContract;
|
|
83
|
+
table: string;
|
|
84
|
+
selfAssignPrimaryKey: boolean;
|
|
85
|
+
$adapter: import("@adonisjs/lucid/types/model").AdapterContract;
|
|
86
|
+
useAdapter: (adapter: import("@adonisjs/lucid/types/model").AdapterContract) => void;
|
|
87
|
+
$hooks: import("@poppinss/hooks").default<any>;
|
|
88
|
+
$keys: {
|
|
89
|
+
attributesToColumns: import("@adonisjs/lucid/types/model").ModelKeysContract;
|
|
90
|
+
attributesToSerialized: import("@adonisjs/lucid/types/model").ModelKeysContract;
|
|
91
|
+
columnsToAttributes: import("@adonisjs/lucid/types/model").ModelKeysContract;
|
|
92
|
+
columnsToSerialized: import("@adonisjs/lucid/types/model").ModelKeysContract;
|
|
93
|
+
serializedToColumns: import("@adonisjs/lucid/types/model").ModelKeysContract;
|
|
94
|
+
serializedToAttributes: import("@adonisjs/lucid/types/model").ModelKeysContract;
|
|
95
|
+
columnAliasesToAttributes: import("@adonisjs/lucid/types/model").ModelKeysContract;
|
|
96
|
+
};
|
|
97
|
+
$createFromAdapterResult: <T_1 extends LucidModel>(this: T_1, result?: import("@adonisjs/lucid/types/model").ModelObject, sideloadAttributes?: import("@adonisjs/lucid/types/model").ModelObject, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions) => null | InstanceType<T_1>;
|
|
98
|
+
$createMultipleFromAdapterResult: <T_1 extends LucidModel>(this: T_1, results: import("@adonisjs/lucid/types/model").ModelObject[], sideloadAttributes?: import("@adonisjs/lucid/types/model").ModelObject, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions) => InstanceType<T_1>[];
|
|
99
|
+
$addColumn: (name: string, options: Partial<import("@adonisjs/lucid/types/model").ColumnOptions>) => import("@adonisjs/lucid/types/model").ColumnOptions;
|
|
100
|
+
$hasColumn: (name: string) => boolean;
|
|
101
|
+
$getColumn: (name: string) => import("@adonisjs/lucid/types/model").ModelColumnOptions | undefined;
|
|
102
|
+
$getColumnAlias: (columnName: string) => string;
|
|
103
|
+
columnsForSelect: () => Record<string, string>;
|
|
104
|
+
$addComputed: (name: string, options: Partial<import("@adonisjs/lucid/types/model").ComputedOptions>) => import("@adonisjs/lucid/types/model").ComputedOptions;
|
|
105
|
+
$hasComputed: (name: string) => boolean;
|
|
106
|
+
$getComputed: (name: string) => import("@adonisjs/lucid/types/model").ComputedOptions | undefined;
|
|
107
|
+
$addRelation: (name: string, type: import("@adonisjs/lucid/types/relations").ModelRelationTypes["__opaque_type"], relatedModel: () => LucidModel, options: import("@adonisjs/lucid/types/model").ModelRelationOptions) => void;
|
|
108
|
+
$hasRelation: (name: string) => boolean;
|
|
109
|
+
$getRelation: {
|
|
110
|
+
<Model extends LucidModel, Name extends import("@adonisjs/lucid/types/relations").ExtractModelRelations<InstanceType<Model>>>(this: Model, name: Name): NonNullable<InstanceType<Model>[Name]> extends import("@adonisjs/lucid/types/relations").ModelRelations<LucidModel, LucidModel> ? NonNullable<InstanceType<Model>[Name]>["client"]["relation"] : import("@adonisjs/lucid/types/relations").RelationshipsContract;
|
|
111
|
+
<Model extends LucidModel>(this: Model, name: string): import("@adonisjs/lucid/types/relations").RelationshipsContract;
|
|
112
|
+
};
|
|
113
|
+
$defineProperty: <Model extends LucidModel, Prop extends keyof Model>(this: Model, propertyName: Prop, defaultValue: Model[Prop], strategy: "inherit" | "define" | ((value: Model[Prop]) => Model[Prop])) => void;
|
|
114
|
+
before: {
|
|
115
|
+
<Model extends LucidModel, Event extends "find" | "fetch">(this: Model, event: Event, handler: import("@adonisjs/lucid/types/model").HooksHandler<ModelQueryBuilderContract<Model>, Event>): void;
|
|
116
|
+
<Model extends LucidModel>(this: Model, event: "paginate", handler: import("@adonisjs/lucid/types/model").HooksHandler<[ModelQueryBuilderContract<Model>, ModelQueryBuilderContract<Model>], "paginate">): void;
|
|
117
|
+
<Model extends LucidModel, Event extends import("@adonisjs/lucid/types/model").EventsList>(this: Model, event: Event, handler: import("@adonisjs/lucid/types/model").HooksHandler<InstanceType<Model>, Event>): void;
|
|
118
|
+
};
|
|
119
|
+
after: {
|
|
120
|
+
<Model extends LucidModel>(this: Model, event: "fetch", handler: import("@adonisjs/lucid/types/model").HooksHandler<InstanceType<Model>[], "fetch">): void;
|
|
121
|
+
<Model extends LucidModel>(this: Model, event: "paginate", handler: import("@adonisjs/lucid/types/model").HooksHandler<import("@adonisjs/lucid/types/model").ModelPaginatorContract<InstanceType<Model>>, "paginate">): void;
|
|
122
|
+
<Model extends LucidModel, Event extends import("@adonisjs/lucid/types/model").EventsList>(this: Model, event: Event, handler: import("@adonisjs/lucid/types/model").HooksHandler<InstanceType<Model>, Event>): void;
|
|
123
|
+
};
|
|
124
|
+
create: <T_1 extends LucidModel>(this: T_1, values: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>>, options?: import("@adonisjs/lucid/types/model").ModelAssignOptions) => Promise<InstanceType<T_1>>;
|
|
125
|
+
createQuietly: <T_1 extends LucidModel>(this: T_1, values: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>>, options?: import("@adonisjs/lucid/types/model").ModelAssignOptions) => Promise<InstanceType<T_1>>;
|
|
126
|
+
createMany: <T_1 extends LucidModel>(this: T_1, values: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>>[], options?: import("@adonisjs/lucid/types/model").ModelAssignOptions) => Promise<InstanceType<T_1>[]>;
|
|
127
|
+
createManyQuietly: <T_1 extends LucidModel>(this: T_1, values: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>>[], options?: import("@adonisjs/lucid/types/model").ModelAssignOptions) => Promise<InstanceType<T_1>[]>;
|
|
128
|
+
find: <T_1 extends LucidModel>(this: T_1, value: any, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions) => Promise<null | InstanceType<T_1>>;
|
|
129
|
+
findOrFail: <T_1 extends LucidModel>(this: T_1, value: any, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions) => Promise<InstanceType<T_1>>;
|
|
130
|
+
findBy: {
|
|
131
|
+
<T_1 extends LucidModel>(this: T_1, clause: Record<string, unknown>, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions): Promise<null | InstanceType<T_1>>;
|
|
132
|
+
<T_1 extends LucidModel>(this: T_1, key: string, value: any, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions): Promise<null | InstanceType<T_1>>;
|
|
133
|
+
};
|
|
134
|
+
findByOrFail: {
|
|
135
|
+
<T_1 extends LucidModel>(this: T_1, clause: Record<string, unknown>, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions): Promise<InstanceType<T_1>>;
|
|
136
|
+
<T_1 extends LucidModel>(this: T_1, key: string, value: any, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions): Promise<InstanceType<T_1>>;
|
|
137
|
+
};
|
|
138
|
+
findManyBy: {
|
|
139
|
+
<T_1 extends LucidModel>(this: T_1, clause: Record<string, unknown>, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions): Promise<InstanceType<T_1>[]>;
|
|
140
|
+
<T_1 extends LucidModel>(this: T_1, key: string, value: any, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions): Promise<InstanceType<T_1>[]>;
|
|
141
|
+
};
|
|
142
|
+
first: <T_1 extends LucidModel>(this: T_1, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions) => Promise<null | InstanceType<T_1>>;
|
|
143
|
+
firstOrFail: <T_1 extends LucidModel>(this: T_1, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions) => Promise<InstanceType<T_1>>;
|
|
144
|
+
findMany: <T_1 extends LucidModel>(this: T_1, value: any[], options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions) => Promise<InstanceType<T_1>[]>;
|
|
145
|
+
firstOrNew: <T_1 extends LucidModel>(this: T_1, searchPayload: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>>, savePayload?: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>>, options?: import("@adonisjs/lucid/types/model").ModelAssignOptions) => Promise<InstanceType<T_1>>;
|
|
146
|
+
firstOrCreate: <T_1 extends LucidModel>(this: T_1, searchPayload: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>>, savePayload?: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>>, options?: import("@adonisjs/lucid/types/model").ModelAssignOptions) => Promise<InstanceType<T_1>>;
|
|
147
|
+
updateOrCreate: <T_1 extends LucidModel>(this: T_1, searchPayload: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>>, updatePayload: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>>, options?: import("@adonisjs/lucid/types/model").ModelAssignOptions) => Promise<InstanceType<T_1>>;
|
|
148
|
+
fetchOrNewUpMany: <T_1 extends LucidModel>(this: T_1, predicate: keyof import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>> | (keyof import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>)[], payload: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>>[], options?: import("@adonisjs/lucid/types/model").ModelAssignOptions) => Promise<InstanceType<T_1>[]>;
|
|
149
|
+
fetchOrCreateMany: <T_1 extends LucidModel>(this: T_1, predicate: keyof import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>> | (keyof import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>)[], payload: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>>[], options?: import("@adonisjs/lucid/types/model").ModelAssignOptions) => Promise<InstanceType<T_1>[]>;
|
|
150
|
+
updateOrCreateMany: <T_1 extends LucidModel>(this: T_1, predicate: keyof import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>> | (keyof import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>)[], payload: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T_1>>>[], options?: import("@adonisjs/lucid/types/model").ModelAssignOptions) => Promise<InstanceType<T_1>[]>;
|
|
151
|
+
all: <T_1 extends LucidModel>(this: T_1, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions) => Promise<InstanceType<T_1>[]>;
|
|
152
|
+
query: <Model extends LucidModel, Result = InstanceType<Model>>(this: Model, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions) => ModelQueryBuilderContract<Model, Result>;
|
|
153
|
+
transaction: import("@adonisjs/lucid/types/database").TransactionFn;
|
|
154
|
+
truncate: (cascade?: boolean) => Promise<void>;
|
|
155
|
+
} & T;
|
|
11
156
|
export {};
|
|
@@ -1,112 +1,69 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export function AutoPreload(superclass) {
|
|
2
|
+
return class extends superclass {
|
|
3
|
+
/**
|
|
4
|
+
* List of relationships to auto-preload.
|
|
5
|
+
*/
|
|
5
6
|
static $with = [];
|
|
6
|
-
static $originalWith = [];
|
|
7
7
|
static boot() {
|
|
8
|
-
if (this.booted)
|
|
8
|
+
if (this.booted)
|
|
9
9
|
return;
|
|
10
|
-
}
|
|
11
|
-
if (this.$with.length > 0) {
|
|
12
|
-
const isWrongType = this.$with.every((relationship) => {
|
|
13
|
-
return !['function', 'string'].includes(typeof relationship);
|
|
14
|
-
});
|
|
15
|
-
if (isWrongType) {
|
|
16
|
-
throw WrongRelationshipTypeException.invoke(this.name);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
10
|
super.boot();
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
this.handleAutoPreload(query, false);
|
|
11
|
+
// We use 'find' and 'fetch' hooks
|
|
12
|
+
this.before('find', (query) => this.applyAutoPreload(query));
|
|
13
|
+
this.before('fetch', (query) => this.applyAutoPreload(query));
|
|
14
|
+
// Lucid v7 paginate sends [countQuery, mainQuery]
|
|
15
|
+
this.before('paginate', (queries) => {
|
|
16
|
+
const query = Array.isArray(queries) ? queries[1] : queries;
|
|
17
|
+
this.applyAutoPreload(query);
|
|
28
18
|
});
|
|
29
19
|
}
|
|
30
|
-
static
|
|
31
|
-
this
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
20
|
+
static applyAutoPreload(query) {
|
|
21
|
+
// Check if auto-preload has been disabled for this specific query instance
|
|
22
|
+
if (query.$disableAutoPreload)
|
|
23
|
+
return;
|
|
24
|
+
const relations = this.$with;
|
|
25
|
+
if (!Array.isArray(relations))
|
|
26
|
+
return;
|
|
27
|
+
// Get list of relations to skip for this specific query
|
|
28
|
+
const skipList = query.$skipPreloads || [];
|
|
29
|
+
for (const relation of relations) {
|
|
30
|
+
if (typeof relation === 'string') {
|
|
31
|
+
if (skipList.includes(relation))
|
|
32
|
+
continue;
|
|
33
|
+
if (relation.includes('.')) {
|
|
34
|
+
this.handleNestedPreload(query, relation.split('.'));
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
query.preload(relation);
|
|
38
|
+
}
|
|
38
39
|
}
|
|
39
|
-
else {
|
|
40
|
-
|
|
40
|
+
else if (typeof relation === 'function') {
|
|
41
|
+
relation(query);
|
|
41
42
|
}
|
|
42
|
-
}
|
|
43
|
-
return this;
|
|
43
|
+
}
|
|
44
44
|
}
|
|
45
|
-
static
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return relationship;
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
throw WrongArgumentTypeException.invoke(relationship);
|
|
45
|
+
static handleNestedPreload(query, parts) {
|
|
46
|
+
const current = parts.shift();
|
|
47
|
+
if (!current)
|
|
48
|
+
return;
|
|
49
|
+
query.preload(current, (builder) => {
|
|
50
|
+
if (parts.length > 0) {
|
|
51
|
+
this.handleNestedPreload(builder, [...parts]);
|
|
56
52
|
}
|
|
57
53
|
});
|
|
58
|
-
return this;
|
|
59
|
-
}
|
|
60
|
-
static withoutAny() {
|
|
61
|
-
this.$with = [];
|
|
62
|
-
return this;
|
|
63
|
-
}
|
|
64
|
-
static handleAutoPreload(query, restorePreloads = true) {
|
|
65
|
-
const preloads = this.$with;
|
|
66
|
-
if (preloads.length > 0) {
|
|
67
|
-
for (const preload of preloads) {
|
|
68
|
-
if (typeof preload === 'string') {
|
|
69
|
-
if (preload.includes('.')) {
|
|
70
|
-
this.handleNestedRelationships(query, preload.split('.'));
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
query.preload(preload);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
else if (typeof preload === 'function') {
|
|
77
|
-
preload(query);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
if (restorePreloads) {
|
|
82
|
-
this.$with = [...this.$originalWith];
|
|
83
|
-
}
|
|
84
54
|
}
|
|
85
55
|
/**
|
|
86
|
-
*
|
|
56
|
+
* Correct Implementation: Returns the Query Builder to avoid global state pollution
|
|
87
57
|
*/
|
|
88
|
-
static
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
query.preload(nextRelation, (qb) => {
|
|
93
|
-
if (relationships.length > 0) {
|
|
94
|
-
this.handleNestedRelationships(qb, relationships);
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
}
|
|
58
|
+
static without(relations) {
|
|
59
|
+
const query = this.query();
|
|
60
|
+
query.$skipPreloads = relations;
|
|
61
|
+
return query;
|
|
99
62
|
}
|
|
100
|
-
static
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
});
|
|
105
|
-
if (isWrongType) {
|
|
106
|
-
throw WrongArgumentTypeException.invoke(method);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
63
|
+
static withoutAny() {
|
|
64
|
+
const query = this.query();
|
|
65
|
+
query.$disableAutoPreload = true;
|
|
66
|
+
return query;
|
|
109
67
|
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
};
|
|
68
|
+
};
|
|
69
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codenameryuu/adonis-lucid-auto-preload",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "Auto-preload multiple relationships when retrieving Lucid models on Adonis JS 7",
|
|
5
5
|
"author": "codenameryuu",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
],
|
|
21
21
|
"exports": {
|
|
22
22
|
".": "./build/index.js",
|
|
23
|
+
"./mixins": "./build/src/mixins/auto_preload.js",
|
|
23
24
|
"./adonis-typings/*": "./build/adonis-typings/*.js",
|
|
24
25
|
"./exceptions/*": "./build/exceptions/*.js",
|
|
25
26
|
"./provider": "./build/providers/auto_preload_provider.js"
|