@holoyan/adonisjs-polymorphic 0.1.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/README.md +549 -0
- package/build/configure.d.ts +10 -0
- package/build/configure.d.ts.map +1 -0
- package/build/configure.js +27 -0
- package/build/configure.js.map +1 -0
- package/build/providers/plugin_provider.d.ts +15 -0
- package/build/providers/plugin_provider.d.ts.map +1 -0
- package/build/providers/plugin_provider.js +21 -0
- package/build/providers/plugin_provider.js.map +1 -0
- package/build/src/decorators.d.ts +41 -0
- package/build/src/decorators.d.ts.map +1 -0
- package/build/src/decorators.js +64 -0
- package/build/src/decorators.js.map +1 -0
- package/build/src/define_config.d.ts +20 -0
- package/build/src/define_config.d.ts.map +1 -0
- package/build/src/define_config.js +8 -0
- package/build/src/define_config.js.map +1 -0
- package/build/src/index.d.ts +16 -0
- package/build/src/index.d.ts.map +1 -0
- package/build/src/index.js +15 -0
- package/build/src/index.js.map +1 -0
- package/build/src/relations/morph_many/index.d.ts +39 -0
- package/build/src/relations/morph_many/index.d.ts.map +1 -0
- package/build/src/relations/morph_many/index.js +110 -0
- package/build/src/relations/morph_many/index.js.map +1 -0
- package/build/src/relations/morph_many/query_builder.d.ts +22 -0
- package/build/src/relations/morph_many/query_builder.d.ts.map +1 -0
- package/build/src/relations/morph_many/query_builder.js +80 -0
- package/build/src/relations/morph_many/query_builder.js.map +1 -0
- package/build/src/relations/morph_many/query_client.d.ts +40 -0
- package/build/src/relations/morph_many/query_client.d.ts.map +1 -0
- package/build/src/relations/morph_many/query_client.js +115 -0
- package/build/src/relations/morph_many/query_client.js.map +1 -0
- package/build/src/relations/morph_one/index.d.ts +68 -0
- package/build/src/relations/morph_one/index.d.ts.map +1 -0
- package/build/src/relations/morph_one/index.js +147 -0
- package/build/src/relations/morph_one/index.js.map +1 -0
- package/build/src/relations/morph_one/query_builder.d.ts +22 -0
- package/build/src/relations/morph_one/query_builder.d.ts.map +1 -0
- package/build/src/relations/morph_one/query_builder.js +79 -0
- package/build/src/relations/morph_one/query_builder.js.map +1 -0
- package/build/src/relations/morph_one/query_client.d.ts +41 -0
- package/build/src/relations/morph_one/query_client.d.ts.map +1 -0
- package/build/src/relations/morph_one/query_client.js +94 -0
- package/build/src/relations/morph_one/query_client.js.map +1 -0
- package/build/src/relations/morph_to/eager_loader.d.ts +29 -0
- package/build/src/relations/morph_to/eager_loader.d.ts.map +1 -0
- package/build/src/relations/morph_to/eager_loader.js +78 -0
- package/build/src/relations/morph_to/eager_loader.js.map +1 -0
- package/build/src/relations/morph_to/index.d.ts +76 -0
- package/build/src/relations/morph_to/index.d.ts.map +1 -0
- package/build/src/relations/morph_to/index.js +172 -0
- package/build/src/relations/morph_to/index.js.map +1 -0
- package/build/src/relations/morph_to/query_client.d.ts +27 -0
- package/build/src/relations/morph_to/query_client.d.ts.map +1 -0
- package/build/src/relations/morph_to/query_client.js +64 -0
- package/build/src/relations/morph_to/query_client.js.map +1 -0
- package/build/src/relations/morph_to/registry.d.ts +2 -0
- package/build/src/relations/morph_to/registry.d.ts.map +1 -0
- package/build/src/relations/morph_to/registry.js +13 -0
- package/build/src/relations/morph_to/registry.js.map +1 -0
- package/build/src/relations/shared/query_builder.d.ts +55 -0
- package/build/src/relations/shared/query_builder.d.ts.map +1 -0
- package/build/src/relations/shared/query_builder.js +70 -0
- package/build/src/relations/shared/query_builder.js.map +1 -0
- package/build/src/types.d.ts +84 -0
- package/build/src/types.d.ts.map +1 -0
- package/build/src/types.js +2 -0
- package/build/src/types.js.map +1 -0
- package/build/stubs/config/polymorphic.stub +19 -0
- package/build/stubs/main.d.ts +6 -0
- package/build/stubs/main.d.ts.map +1 -0
- package/build/stubs/main.js +6 -0
- package/build/stubs/main.js.map +1 -0
- package/package.json +81 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { MorphOne } from './relations/morph_one/index.js';
|
|
2
|
+
import { MorphMany } from './relations/morph_many/index.js';
|
|
3
|
+
import { MorphTo } from './relations/morph_to/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* Decorator to define a morphOne (has-one polymorphic) relation on a parent model.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* class Post extends BaseModel {
|
|
10
|
+
* @morphOne(() => Image, { name: 'imageable' })
|
|
11
|
+
* declare image: Image | null
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function morphOne(relatedModelFactory, options) {
|
|
16
|
+
return function decorateAsMorphOne(target, relationName) {
|
|
17
|
+
const Model = target.constructor;
|
|
18
|
+
Model.boot();
|
|
19
|
+
const relation = new MorphOne(relationName, relatedModelFactory, options, Model);
|
|
20
|
+
Model.$relationsDefinitions.set(relationName, relation);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Decorator to define a morphMany (has-many polymorphic) relation on a parent model.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* class Post extends BaseModel {
|
|
29
|
+
* @morphMany(() => Comment, { name: 'commentable' })
|
|
30
|
+
* declare comments: Comment[]
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function morphMany(relatedModelFactory, options) {
|
|
35
|
+
return function decorateAsMorphMany(target, relationName) {
|
|
36
|
+
const Model = target.constructor;
|
|
37
|
+
Model.boot();
|
|
38
|
+
const relation = new MorphMany(relationName, relatedModelFactory, options, Model);
|
|
39
|
+
Model.$relationsDefinitions.set(relationName, relation);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Decorator to define a morphTo (belongs-to polymorphic) relation on a child model.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* class Comment extends BaseModel {
|
|
48
|
+
* @column() declare commentableType: string
|
|
49
|
+
* @column() declare commentableId: number
|
|
50
|
+
*
|
|
51
|
+
* @morphTo({ name: 'commentable', morphMap: { posts: () => Post, videos: () => Video } })
|
|
52
|
+
* declare commentable: Post | Video | null
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export function morphTo(options) {
|
|
57
|
+
return function decorateAsMorphTo(target, relationName) {
|
|
58
|
+
const Model = target.constructor;
|
|
59
|
+
Model.boot();
|
|
60
|
+
const relation = new MorphTo(relationName, options, Model);
|
|
61
|
+
Model.$relationsDefinitions.set(relationName, relation);
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=decorators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../src/decorators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAA;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAA;AAC3D,OAAO,EAAE,OAAO,EAAE,MAAM,+BAA+B,CAAA;AAGvD;;;;;;;;;;GAUG;AACH,MAAM,UAAU,QAAQ,CAAC,mBAA8B,EAAE,OAAwB;IAC/E,OAAO,SAAS,kBAAkB,CAAC,MAAW,EAAE,YAAoB;QAClE,MAAM,KAAK,GAAG,MAAM,CAAC,WAAW,CAAA;QAChC,KAAK,CAAC,IAAI,EAAE,CAAA;QACZ,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,YAAY,EAAE,mBAAmB,EAAE,OAAO,EAAE,KAAK,CAAC,CAAA;QAChF,KAAK,CAAC,qBAAqB,CAAC,GAAG,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAA;IACzD,CAAC,CAAA;AACH,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,SAAS,CAAC,mBAA8B,EAAE,OAAyB;IACjF,OAAO,SAAS,mBAAmB,CAAC,MAAW,EAAE,YAAoB;QACnE,MAAM,KAAK,GAAG,MAAM,CAAC,WAAW,CAAA;QAChC,KAAK,CAAC,IAAI,EAAE,CAAA;QACZ,MAAM,QAAQ,GAAG,IAAI,SAAS,CAAC,YAAY,EAAE,mBAAmB,EAAE,OAAO,EAAE,KAAK,CAAC,CAAA;QACjF,KAAK,CAAC,qBAAqB,CAAC,GAAG,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAA;IACzD,CAAC,CAAA;AACH,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,OAAO,CAAC,OAAuB;IAC7C,OAAO,SAAS,iBAAiB,CAAC,MAAW,EAAE,YAAoB;QACjE,MAAM,KAAK,GAAG,MAAM,CAAC,WAAW,CAAA;QAChC,KAAK,CAAC,IAAI,EAAE,CAAA;QACZ,MAAM,QAAQ,GAAG,IAAI,OAAO,CAAC,YAAY,EAAE,OAAO,EAAE,KAAK,CAAC,CAAA;QAC1D,KAAK,CAAC,qBAAqB,CAAC,GAAG,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAA;IACzD,CAAC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface PolymorphicConfig {
|
|
2
|
+
/**
|
|
3
|
+
* Model factory functions whose files should be imported at boot time.
|
|
4
|
+
* Each import triggers the @MorphMap decorator, populating the global
|
|
5
|
+
* registry before any query runs.
|
|
6
|
+
*
|
|
7
|
+
* Example:
|
|
8
|
+
* morphModels: [
|
|
9
|
+
* () => import('#models/post'),
|
|
10
|
+
* () => import('#models/video'),
|
|
11
|
+
* ]
|
|
12
|
+
*/
|
|
13
|
+
morphModels?: Array<() => Promise<any>>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Define the configuration for @holoyan/adonisjs-polymorphic.
|
|
17
|
+
* Use this in config/polymorphic.ts.
|
|
18
|
+
*/
|
|
19
|
+
export declare function defineConfig(config: PolymorphicConfig): PolymorphicConfig;
|
|
20
|
+
//# sourceMappingURL=define_config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define_config.d.ts","sourceRoot":"","sources":["../../src/define_config.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,iBAAiB;IAChC;;;;;;;;;;OAUG;IACH,WAAW,CAAC,EAAE,KAAK,CAAC,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,CAAA;CACxC;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,iBAAiB,GAAG,iBAAiB,CAEzE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define_config.js","sourceRoot":"","sources":["../../src/define_config.ts"],"names":[],"mappings":"AAeA;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,MAAyB;IACpD,OAAO,MAAM,CAAA;AACf,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @holoyan/adonisjs-polymorphic
|
|
3
|
+
*
|
|
4
|
+
* Polymorphic relations for AdonisJS Lucid ORM
|
|
5
|
+
* Provides morphOne, morphMany, and morphTo decorators
|
|
6
|
+
*/
|
|
7
|
+
export { morphOne, morphMany, morphTo } from './decorators.js';
|
|
8
|
+
export { defineConfig } from './define_config.js';
|
|
9
|
+
export type { PolymorphicConfig } from './define_config.js';
|
|
10
|
+
export { MorphOne } from './relations/morph_one/index.js';
|
|
11
|
+
export { MorphMany } from './relations/morph_many/index.js';
|
|
12
|
+
export { MorphTo } from './relations/morph_to/index.js';
|
|
13
|
+
export type { MorphOneOptions, MorphManyOptions, MorphToOptions } from './types.js';
|
|
14
|
+
export { configure } from '../configure.js';
|
|
15
|
+
export { stubsRoot } from '../stubs/main.js';
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACjD,YAAY,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAA;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAA;AAC3D,OAAO,EAAE,OAAO,EAAE,MAAM,+BAA+B,CAAA;AACvD,YAAY,EAAE,eAAe,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAGnF,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @holoyan/adonisjs-polymorphic
|
|
3
|
+
*
|
|
4
|
+
* Polymorphic relations for AdonisJS Lucid ORM
|
|
5
|
+
* Provides morphOne, morphMany, and morphTo decorators
|
|
6
|
+
*/
|
|
7
|
+
export { morphOne, morphMany, morphTo } from './decorators.js';
|
|
8
|
+
export { defineConfig } from './define_config.js';
|
|
9
|
+
export { MorphOne } from './relations/morph_one/index.js';
|
|
10
|
+
export { MorphMany } from './relations/morph_many/index.js';
|
|
11
|
+
export { MorphTo } from './relations/morph_to/index.js';
|
|
12
|
+
// Re-exported so `node ace configure @holoyan/adonisjs-polymorphic` can find the hook
|
|
13
|
+
export { configure } from '../configure.js';
|
|
14
|
+
export { stubsRoot } from '../stubs/main.js';
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAEjD,OAAO,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAA;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAA;AAC3D,OAAO,EAAE,OAAO,EAAE,MAAM,+BAA+B,CAAA;AAGvD,sFAAsF;AACtF,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { MorphManyOptions } from '../../types.js';
|
|
2
|
+
import { MorphManyQueryClient } from './query_client.js';
|
|
3
|
+
/**
|
|
4
|
+
* MorphMany relation - the parent model has many polymorphic children.
|
|
5
|
+
*
|
|
6
|
+
* Example: Post morphMany Comment (commentable)
|
|
7
|
+
* comments table has: commentable_type = 'posts', commentable_id = post.id
|
|
8
|
+
*/
|
|
9
|
+
export declare class MorphMany {
|
|
10
|
+
readonly relationName: string;
|
|
11
|
+
readonly relatedModel: () => any;
|
|
12
|
+
private readonly options;
|
|
13
|
+
readonly model: any;
|
|
14
|
+
readonly type = "hasMany";
|
|
15
|
+
booted: boolean;
|
|
16
|
+
serializeAs: string | null;
|
|
17
|
+
onQueryHook: ((query: any) => void) | undefined;
|
|
18
|
+
morphTypeKey: string;
|
|
19
|
+
morphTypeColumnName: string;
|
|
20
|
+
morphIdKey: string;
|
|
21
|
+
morphIdColumnName: string;
|
|
22
|
+
localKey: string;
|
|
23
|
+
localKeyColumnName: string;
|
|
24
|
+
morphValue: string;
|
|
25
|
+
constructor(relationName: string, relatedModel: () => any, options: MorphManyOptions, model: any);
|
|
26
|
+
clone(parent: any): MorphMany;
|
|
27
|
+
boot(): void;
|
|
28
|
+
setRelated(parent: any, related: any): void;
|
|
29
|
+
pushRelated(parent: any, related: any): void;
|
|
30
|
+
/**
|
|
31
|
+
* Groups and sets related rows on each parent model during eager loading.
|
|
32
|
+
*/
|
|
33
|
+
setRelatedForMany(parents: any[], related: any[]): void;
|
|
34
|
+
client(parent: any, client: any): MorphManyQueryClient;
|
|
35
|
+
eagerQuery(parent: any | any[], client: any): import("./query_builder.js").MorphManyQueryBuilder;
|
|
36
|
+
subQuery(client: any): never;
|
|
37
|
+
hydrateForPersistance(parent: any, related: any): void;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/relations/morph_many/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AACtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAA;AAGxD;;;;;GAKG;AACH,qBAAa,SAAS;aAgBF,YAAY,EAAE,MAAM;aACpB,YAAY,EAAE,MAAM,GAAG;IACvC,OAAO,CAAC,QAAQ,CAAC,OAAO;aACR,KAAK,EAAE,GAAG;IAlB5B,QAAQ,CAAC,IAAI,aAAY;IACzB,MAAM,UAAQ;IACd,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;IAE1B,WAAW,EAAE,CAAC,CAAC,KAAK,EAAE,GAAG,KAAK,IAAI,CAAC,GAAG,SAAS,CAAA;IAE/C,YAAY,EAAG,MAAM,CAAA;IACrB,mBAAmB,EAAG,MAAM,CAAA;IAC5B,UAAU,EAAG,MAAM,CAAA;IACnB,iBAAiB,EAAG,MAAM,CAAA;IAC1B,QAAQ,EAAG,MAAM,CAAA;IACjB,kBAAkB,EAAG,MAAM,CAAA;IAC3B,UAAU,EAAG,MAAM,CAAA;gBAGD,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,GAAG,EACtB,OAAO,EAAE,gBAAgB,EAC1B,KAAK,EAAE,GAAG;IAO5B,KAAK,CAAC,MAAM,EAAE,GAAG,GAAG,SAAS;IAI7B,IAAI,IAAI,IAAI;IAmDZ,UAAU,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,GAAG,IAAI;IAI3C,WAAW,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,GAAG,IAAI;IAI5C;;OAEG;IACH,iBAAiB,CAAC,OAAO,EAAE,GAAG,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,IAAI;IAUvD,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,GAAG,oBAAoB;IAKtD,UAAU,CAAC,MAAM,EAAE,GAAG,GAAG,GAAG,EAAE,EAAE,MAAM,EAAE,GAAG;IAK3C,QAAQ,CAAC,MAAM,EAAE,GAAG,GAAG,KAAK;IAI5B,qBAAqB,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,GAAG,IAAI;CAIvD"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { MorphManyQueryClient } from './query_client.js';
|
|
2
|
+
import { getRegistry } from '../morph_to/registry.js';
|
|
3
|
+
/**
|
|
4
|
+
* MorphMany relation - the parent model has many polymorphic children.
|
|
5
|
+
*
|
|
6
|
+
* Example: Post morphMany Comment (commentable)
|
|
7
|
+
* comments table has: commentable_type = 'posts', commentable_id = post.id
|
|
8
|
+
*/
|
|
9
|
+
export class MorphMany {
|
|
10
|
+
relationName;
|
|
11
|
+
relatedModel;
|
|
12
|
+
options;
|
|
13
|
+
model;
|
|
14
|
+
type = 'hasMany';
|
|
15
|
+
booted = false;
|
|
16
|
+
serializeAs;
|
|
17
|
+
onQueryHook;
|
|
18
|
+
morphTypeKey;
|
|
19
|
+
morphTypeColumnName;
|
|
20
|
+
morphIdKey;
|
|
21
|
+
morphIdColumnName;
|
|
22
|
+
localKey;
|
|
23
|
+
localKeyColumnName;
|
|
24
|
+
morphValue;
|
|
25
|
+
constructor(relationName, relatedModel, options, model) {
|
|
26
|
+
this.relationName = relationName;
|
|
27
|
+
this.relatedModel = relatedModel;
|
|
28
|
+
this.options = options;
|
|
29
|
+
this.model = model;
|
|
30
|
+
this.onQueryHook = options.onQuery;
|
|
31
|
+
this.serializeAs =
|
|
32
|
+
options.serializeAs === undefined ? relationName : options.serializeAs;
|
|
33
|
+
}
|
|
34
|
+
clone(parent) {
|
|
35
|
+
return new MorphMany(this.relationName, this.relatedModel, { ...this.options }, parent);
|
|
36
|
+
}
|
|
37
|
+
boot() {
|
|
38
|
+
if (this.booted)
|
|
39
|
+
return;
|
|
40
|
+
const relatedModel = this.relatedModel();
|
|
41
|
+
const morphName = this.options.name;
|
|
42
|
+
const typeAttr = `${morphName}Type`;
|
|
43
|
+
const idAttr = `${morphName}Id`;
|
|
44
|
+
const typeColDef = relatedModel.$getColumn(typeAttr);
|
|
45
|
+
if (!typeColDef) {
|
|
46
|
+
throw new Error(`[MorphMany] "${typeAttr}" column not found on "${relatedModel.name}". ` +
|
|
47
|
+
`Add @column() declare ${typeAttr}: string`);
|
|
48
|
+
}
|
|
49
|
+
const idColDef = relatedModel.$getColumn(idAttr);
|
|
50
|
+
if (!idColDef) {
|
|
51
|
+
throw new Error(`[MorphMany] "${idAttr}" column not found on "${relatedModel.name}". ` +
|
|
52
|
+
`Add @column() declare ${idAttr}: number`);
|
|
53
|
+
}
|
|
54
|
+
this.morphTypeKey = typeAttr;
|
|
55
|
+
this.morphTypeColumnName = typeColDef.columnName;
|
|
56
|
+
this.morphIdKey = idAttr;
|
|
57
|
+
this.morphIdColumnName = idColDef.columnName;
|
|
58
|
+
const localKeyAttr = this.options.localKey ?? this.model.primaryKey;
|
|
59
|
+
const localColDef = this.model.$getColumn(localKeyAttr);
|
|
60
|
+
if (!localColDef) {
|
|
61
|
+
throw new Error(`[MorphMany] "${localKeyAttr}" column not found on "${this.model.name}".`);
|
|
62
|
+
}
|
|
63
|
+
this.localKey = localKeyAttr;
|
|
64
|
+
this.localKeyColumnName = localColDef.columnName;
|
|
65
|
+
// Priority: explicit option → @MorphMap alias → model table name
|
|
66
|
+
if (this.options.morphValue) {
|
|
67
|
+
this.morphValue = this.options.morphValue;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
const registry = getRegistry();
|
|
71
|
+
const alias = registry?.hasTarget(this.model) ? registry.getAlias(this.model) : null;
|
|
72
|
+
this.morphValue = alias ?? this.model.table;
|
|
73
|
+
}
|
|
74
|
+
this.booted = true;
|
|
75
|
+
}
|
|
76
|
+
setRelated(parent, related) {
|
|
77
|
+
parent.$setRelated(this.relationName, related);
|
|
78
|
+
}
|
|
79
|
+
pushRelated(parent, related) {
|
|
80
|
+
parent.$pushRelated(this.relationName, related);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Groups and sets related rows on each parent model during eager loading.
|
|
84
|
+
*/
|
|
85
|
+
setRelatedForMany(parents, related) {
|
|
86
|
+
parents.forEach((parentModel) => {
|
|
87
|
+
const parentId = parentModel[this.localKey];
|
|
88
|
+
const rows = related.filter((r) => parentId !== undefined && r[this.morphIdKey] === parentId);
|
|
89
|
+
this.setRelated(parentModel, rows);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
client(parent, client) {
|
|
93
|
+
if (!this.booted)
|
|
94
|
+
this.boot();
|
|
95
|
+
return new MorphManyQueryClient(this, parent, client);
|
|
96
|
+
}
|
|
97
|
+
eagerQuery(parent, client) {
|
|
98
|
+
if (!this.booted)
|
|
99
|
+
this.boot();
|
|
100
|
+
return MorphManyQueryClient.eagerQuery(client, this, parent);
|
|
101
|
+
}
|
|
102
|
+
subQuery(client) {
|
|
103
|
+
return MorphManyQueryClient.subQuery(client, this);
|
|
104
|
+
}
|
|
105
|
+
hydrateForPersistance(parent, related) {
|
|
106
|
+
related[this.morphTypeKey] = this.morphValue;
|
|
107
|
+
related[this.morphIdKey] = parent[this.localKey];
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/relations/morph_many/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,C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C,IAAI,CAAC,IAAI,CAAC,MAAM;YAAE,IAAI,CAAC,IAAI,EAAE,CAAA;QAC7B,OAAO,oBAAoB,CAAC,UAAU,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAA;IAC9D,CAAC;IAED,QAAQ,CAAC,MAAW;QAClB,OAAO,oBAAoB,CAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;IACpD,CAAC;IAED,qBAAqB,CAAC,MAAW,EAAE,OAAY;QAC7C,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,UAAU,CAAA;QAC5C,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IAClD,CAAC;CACF"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { MorphBaseQueryBuilder } from '../shared/query_builder.js';
|
|
2
|
+
import type { MorphMany } from './index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Query builder for morphMany relations.
|
|
5
|
+
* Scopes queries to WHERE morphType = value AND morphId IN (parentIds)
|
|
6
|
+
*/
|
|
7
|
+
export declare class MorphManyQueryBuilder extends MorphBaseQueryBuilder {
|
|
8
|
+
private parent;
|
|
9
|
+
relation: MorphMany;
|
|
10
|
+
constructor(builder: any, client: any, parent: any, relation: MorphMany);
|
|
11
|
+
profilerData(): {
|
|
12
|
+
type: string;
|
|
13
|
+
model: any;
|
|
14
|
+
relatedModel: any;
|
|
15
|
+
};
|
|
16
|
+
getRelationKeys(): string[];
|
|
17
|
+
applyConstraints(): void;
|
|
18
|
+
clone(): MorphManyQueryBuilder;
|
|
19
|
+
paginate(page: number, perPage?: number): Promise<any>;
|
|
20
|
+
getGroupLimitQuery(): never;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=query_builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query_builder.d.ts","sourceRoot":"","sources":["../../../../src/relations/morph_many/query_builder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA;AAClE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAE3C;;;GAGG;AACH,qBAAa,qBAAsB,SAAQ,qBAAqB;IAI5D,OAAO,CAAC,MAAM;IACP,QAAQ,EAAE,SAAS;gBAH1B,OAAO,EAAE,GAAG,EACZ,MAAM,EAAE,GAAG,EACH,MAAM,EAAE,GAAG,EACZ,QAAQ,EAAE,SAAS;IAa5B,YAAY;;;;;IAQZ,eAAe,IAAI,MAAM,EAAE;IAI3B,gBAAgB,IAAI,IAAI;IAoCxB,KAAK,IAAI,qBAAqB;IAa9B,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,MAAW;IAU3C,kBAAkB,IAAI,KAAK;CAK5B"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { unique } from '@adonisjs/lucid/utils';
|
|
2
|
+
import { MorphBaseQueryBuilder } from '../shared/query_builder.js';
|
|
3
|
+
/**
|
|
4
|
+
* Query builder for morphMany relations.
|
|
5
|
+
* Scopes queries to WHERE morphType = value AND morphId IN (parentIds)
|
|
6
|
+
*/
|
|
7
|
+
export class MorphManyQueryBuilder extends MorphBaseQueryBuilder {
|
|
8
|
+
parent;
|
|
9
|
+
relation;
|
|
10
|
+
constructor(builder, client, parent, relation) {
|
|
11
|
+
super(builder, relation.relatedModel(), client, (userFn) => {
|
|
12
|
+
return ($builder) => {
|
|
13
|
+
const subQuery = new MorphManyQueryBuilder($builder, this.client, this.parent, this.relation);
|
|
14
|
+
subQuery.isChildQuery = true;
|
|
15
|
+
subQuery.isRelatedPreloadQuery = this.isRelatedPreloadQuery;
|
|
16
|
+
userFn(subQuery);
|
|
17
|
+
subQuery.applyWhere();
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
this.parent = parent;
|
|
21
|
+
this.relation = relation;
|
|
22
|
+
}
|
|
23
|
+
profilerData() {
|
|
24
|
+
return {
|
|
25
|
+
type: this.relation.type,
|
|
26
|
+
model: this.relation.model.name,
|
|
27
|
+
relatedModel: this.relation.relatedModel().name,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
getRelationKeys() {
|
|
31
|
+
return [this.relation.morphIdKey];
|
|
32
|
+
}
|
|
33
|
+
applyConstraints() {
|
|
34
|
+
if (this.appliedConstraints)
|
|
35
|
+
return;
|
|
36
|
+
this.appliedConstraints = true;
|
|
37
|
+
const queryAction = this.queryAction();
|
|
38
|
+
if (Array.isArray(this.parent)) {
|
|
39
|
+
// Eager loading: WHERE type = 'posts' AND id IN (1, 2, 3)
|
|
40
|
+
const ids = unique(this.parent.map((model) => {
|
|
41
|
+
const val = model[this.relation.localKey];
|
|
42
|
+
if (val === undefined) {
|
|
43
|
+
throw new Error(`Cannot preload "${this.relation.relationName}", value of "${this.relation.model.name}.${this.relation.localKey}" is undefined`);
|
|
44
|
+
}
|
|
45
|
+
return val;
|
|
46
|
+
}));
|
|
47
|
+
this.wrapExisting()
|
|
48
|
+
.where(this.relation.morphTypeColumnName, this.relation.morphValue)
|
|
49
|
+
.whereIn(this.relation.morphIdColumnName, ids);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
// Single parent: WHERE type = 'posts' AND id = 5
|
|
53
|
+
const id = this.parent[this.relation.localKey];
|
|
54
|
+
if (id === undefined) {
|
|
55
|
+
throw new Error(`Cannot ${queryAction} "${this.relation.relationName}", value of "${this.relation.model.name}.${this.relation.localKey}" is undefined`);
|
|
56
|
+
}
|
|
57
|
+
this.wrapExisting()
|
|
58
|
+
.where(this.relation.morphTypeColumnName, this.relation.morphValue)
|
|
59
|
+
.where(this.relation.morphIdColumnName, id);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
clone() {
|
|
63
|
+
const clonedQuery = new MorphManyQueryBuilder(this.knexQuery.clone(), this.client, this.parent, this.relation);
|
|
64
|
+
clonedQuery.appliedConstraints = this.appliedConstraints;
|
|
65
|
+
clonedQuery.isRelatedPreloadQuery = this.isRelatedPreloadQuery;
|
|
66
|
+
this.applyQueryFlags(clonedQuery);
|
|
67
|
+
return clonedQuery;
|
|
68
|
+
}
|
|
69
|
+
paginate(page, perPage = 20) {
|
|
70
|
+
if (this.isRelatedPreloadQuery) {
|
|
71
|
+
throw new Error(`Cannot paginate relationship "${this.relation.relationName}" during preload`);
|
|
72
|
+
}
|
|
73
|
+
this.applyConstraints();
|
|
74
|
+
return super.paginate(page, perPage);
|
|
75
|
+
}
|
|
76
|
+
getGroupLimitQuery() {
|
|
77
|
+
throw new Error(`Cannot apply groupLimit on a morphMany relationship "(${this.relation.relationName})"`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=query_builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query_builder.js","sourceRoot":"","sources":["../../../../src/relations/morph_many/query_builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAC9C,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA;AAGlE;;;GAGG;AACH,MAAM,OAAO,qBAAsB,SAAQ,qBAAqB;IAIpD;IACD;IAJT,YACE,OAAY,EACZ,MAAW,EACH,MAAW,EACZ,QAAmB;QAE1B,KAAK,CAAC,OAAO,EAAE,QAAQ,CAAC,YAAY,EAAE,EAAE,MAAM,EAAE,CAAC,MAAW,EAAE,EAAE;YAC9D,OAAO,CAAC,QAAa,EAAE,EAAE;gBACvB,MAAM,QAAQ,GAAG,IAAI,qBAAqB,CAAC,QAAQ,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;gBAC7F,QAAQ,CAAC,YAAY,GAAG,IAAI,CAAA;gBAC5B,QAAQ,CAAC,qBAAqB,GAAG,IAAI,CAAC,qBAAqB,CAAA;gB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qB,EAAE,CAAC;YAC/B,MAAM,IAAI,KAAK,CACb,iCAAiC,IAAI,CAAC,QAAQ,CAAC,YAAY,kBAAkB,CAC9E,CAAA;QACH,CAAC;QACD,IAAI,CAAC,gBAAgB,EAAE,CAAA;QACvB,OAAO,KAAK,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;IACtC,CAAC;IAED,kBAAkB;QAChB,MAAM,IAAI,KAAK,CACb,yDAAyD,IAAI,CAAC,QAAQ,CAAC,YAAY,IAAI,CACxF,CAAA;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { MorphManyQueryBuilder } from './query_builder.js';
|
|
2
|
+
import type { MorphMany } from './index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Query client for morphMany - provides read and write methods
|
|
5
|
+
* in scope of the relation.
|
|
6
|
+
*/
|
|
7
|
+
export declare class MorphManyQueryClient {
|
|
8
|
+
private relation;
|
|
9
|
+
private parent;
|
|
10
|
+
private client;
|
|
11
|
+
constructor(relation: MorphMany, parent: any, client: any);
|
|
12
|
+
static eagerQuery(client: any, relation: MorphMany, rows: any | any[]): MorphManyQueryBuilder;
|
|
13
|
+
static subQuery(_client: any, relation: MorphMany): never;
|
|
14
|
+
query(): MorphManyQueryBuilder;
|
|
15
|
+
/**
|
|
16
|
+
* Saves a single related model instance.
|
|
17
|
+
*/
|
|
18
|
+
save(related: any): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Saves multiple related model instances.
|
|
21
|
+
*/
|
|
22
|
+
saveMany(related: any[]): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new related model instance.
|
|
25
|
+
*/
|
|
26
|
+
create(values: Record<string, any>, options?: any): Promise<any>;
|
|
27
|
+
/**
|
|
28
|
+
* Creates multiple related model instances.
|
|
29
|
+
*/
|
|
30
|
+
createMany(values: Record<string, any>[], options?: any): Promise<any[]>;
|
|
31
|
+
/**
|
|
32
|
+
* First or create a related model.
|
|
33
|
+
*/
|
|
34
|
+
firstOrCreate(search: Record<string, any>, savePayload?: Record<string, any>, options?: any): Promise<any>;
|
|
35
|
+
/**
|
|
36
|
+
* Update or create a related model.
|
|
37
|
+
*/
|
|
38
|
+
updateOrCreate(search: Record<string, any>, updatePayload: Record<string, any>, options?: any): Promise<any>;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=query_client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query_client.d.ts","sourceRoot":"","sources":["../../../../src/relations/morph_many/query_client.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAA;AAC1D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAE3C;;;GAGG;AACH,qBAAa,oBAAoB;IAE7B,OAAO,CAAC,QAAQ;IAChB,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,MAAM;gBAFN,QAAQ,EAAE,SAAS,EACnB,MAAM,EAAE,GAAG,EACX,MAAM,EAAE,GAAG;IAGrB,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,SAAS,EAAE,IAAI,EAAE,GAAG,GAAG,GAAG,EAAE;IAOrE,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,SAAS,GAAG,KAAK;IAMzD,KAAK,IAAI,qBAAqB;IAW9B;;OAEG;IACG,IAAI,CAAC,OAAO,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;IAUvC;;OAEG;IACG,QAAQ,CAAC,OAAO,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAY7C;;OAEG;IACG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IAUtE;;OAEG;IACG,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,EAAE,OAAO,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;IAgB9E;;OAEG;IACG,aAAa,CACjB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC3B,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACjC,OAAO,CAAC,EAAE,GAAG,GACZ,OAAO,CAAC,GAAG,CAAC;IAYf;;OAEG;IACG,cAAc,CAClB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC3B,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAClC,OAAO,CAAC,EAAE,GAAG,GACZ,OAAO,CAAC,GAAG,CAAC;CAWhB"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { managedTransaction } from '@adonisjs/lucid/utils';
|
|
2
|
+
import { MorphManyQueryBuilder } from './query_builder.js';
|
|
3
|
+
/**
|
|
4
|
+
* Query client for morphMany - provides read and write methods
|
|
5
|
+
* in scope of the relation.
|
|
6
|
+
*/
|
|
7
|
+
export class MorphManyQueryClient {
|
|
8
|
+
relation;
|
|
9
|
+
parent;
|
|
10
|
+
client;
|
|
11
|
+
constructor(relation, parent, client) {
|
|
12
|
+
this.relation = relation;
|
|
13
|
+
this.parent = parent;
|
|
14
|
+
this.client = client;
|
|
15
|
+
}
|
|
16
|
+
static eagerQuery(client, relation, rows) {
|
|
17
|
+
const query = new MorphManyQueryBuilder(client.knexQuery(), client, rows, relation);
|
|
18
|
+
query.isRelatedPreloadQuery = true;
|
|
19
|
+
if (typeof relation.onQueryHook === 'function')
|
|
20
|
+
relation.onQueryHook(query);
|
|
21
|
+
return query;
|
|
22
|
+
}
|
|
23
|
+
static subQuery(_client, relation) {
|
|
24
|
+
throw new Error(`morphMany: whereHas/withCount is not yet supported for polymorphic relations ("${relation.relationName}")`);
|
|
25
|
+
}
|
|
26
|
+
query() {
|
|
27
|
+
const query = new MorphManyQueryBuilder(this.client.knexQuery(), this.client, this.parent, this.relation);
|
|
28
|
+
if (typeof this.relation.onQueryHook === 'function')
|
|
29
|
+
this.relation.onQueryHook(query);
|
|
30
|
+
return query;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Saves a single related model instance.
|
|
34
|
+
*/
|
|
35
|
+
async save(related) {
|
|
36
|
+
await managedTransaction(this.parent.$trx || this.client, async (trx) => {
|
|
37
|
+
this.parent.$trx = trx;
|
|
38
|
+
await this.parent.save();
|
|
39
|
+
this.relation.hydrateForPersistance(this.parent, related);
|
|
40
|
+
related.$trx = trx;
|
|
41
|
+
await related.save();
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Saves multiple related model instances.
|
|
46
|
+
*/
|
|
47
|
+
async saveMany(related) {
|
|
48
|
+
await managedTransaction(this.parent.$trx || this.client, async (trx) => {
|
|
49
|
+
this.parent.$trx = trx;
|
|
50
|
+
await this.parent.save();
|
|
51
|
+
for (const row of related) {
|
|
52
|
+
this.relation.hydrateForPersistance(this.parent, row);
|
|
53
|
+
row.$trx = trx;
|
|
54
|
+
await row.save();
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Creates a new related model instance.
|
|
60
|
+
*/
|
|
61
|
+
async create(values, options) {
|
|
62
|
+
return managedTransaction(this.parent.$trx || this.client, async (trx) => {
|
|
63
|
+
this.parent.$trx = trx;
|
|
64
|
+
await this.parent.save();
|
|
65
|
+
const valuesToPersist = { ...values };
|
|
66
|
+
this.relation.hydrateForPersistance(this.parent, valuesToPersist);
|
|
67
|
+
return this.relation.relatedModel().create(valuesToPersist, { client: trx, ...options });
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Creates multiple related model instances.
|
|
72
|
+
*/
|
|
73
|
+
async createMany(values, options) {
|
|
74
|
+
return managedTransaction(this.parent.$trx || this.client, async (trx) => {
|
|
75
|
+
this.parent.$trx = trx;
|
|
76
|
+
await this.parent.save();
|
|
77
|
+
const results = [];
|
|
78
|
+
for (const row of values) {
|
|
79
|
+
const valuesToPersist = { ...row };
|
|
80
|
+
this.relation.hydrateForPersistance(this.parent, valuesToPersist);
|
|
81
|
+
results.push(await this.relation.relatedModel().create(valuesToPersist, { client: trx, ...options }));
|
|
82
|
+
}
|
|
83
|
+
return results;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* First or create a related model.
|
|
88
|
+
*/
|
|
89
|
+
async firstOrCreate(search, savePayload, options) {
|
|
90
|
+
return managedTransaction(this.parent.$trx || this.client, async (trx) => {
|
|
91
|
+
this.parent.$trx = trx;
|
|
92
|
+
await this.parent.save();
|
|
93
|
+
const valuesToPersist = { ...search };
|
|
94
|
+
this.relation.hydrateForPersistance(this.parent, valuesToPersist);
|
|
95
|
+
return this.relation
|
|
96
|
+
.relatedModel()
|
|
97
|
+
.firstOrCreate(valuesToPersist, savePayload, { client: trx, ...options });
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Update or create a related model.
|
|
102
|
+
*/
|
|
103
|
+
async updateOrCreate(search, updatePayload, options) {
|
|
104
|
+
return managedTransaction(this.parent.$trx || this.client, async (trx) => {
|
|
105
|
+
this.parent.$trx = trx;
|
|
106
|
+
await this.parent.save();
|
|
107
|
+
const valuesToPersist = { ...search };
|
|
108
|
+
this.relation.hydrateForPersistance(this.parent, valuesToPersist);
|
|
109
|
+
return this.relation
|
|
110
|
+
.relatedModel()
|
|
111
|
+
.updateOrCreate(valuesToPersist, updatePayload, { client: trx, ...options });
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=query_client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query_client.js","sourceRoot":"","sources":["../../../../src/relations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