@tsed/objection 8.4.5 → 8.4.6
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/lib/esm/decorators/belongsToOne.js +16 -2
- package/lib/esm/decorators/belongsToOne.js.map +1 -1
- package/lib/esm/decorators/relatesTo.js +0 -4
- package/lib/esm/decorators/relatesTo.js.map +1 -1
- package/lib/esm/domain/RelationshipOpts.js +3 -0
- package/lib/esm/domain/RelationshipOpts.js.map +1 -1
- package/lib/esm/utils/createRelationshipMapping.js +19 -10
- package/lib/esm/utils/createRelationshipMapping.js.map +1 -1
- package/lib/types/decorators/belongsToOne.d.ts +16 -0
- package/lib/types/domain/RelationshipOpts.d.ts +1 -0
- package/package.json +13 -13
- package/src/decorators/belongsToOne.spec.ts +78 -0
- package/src/decorators/belongsToOne.ts +43 -4
- package/src/decorators/relatesTo.ts +0 -3
- package/src/domain/RelationshipOpts.ts +4 -0
- package/src/utils/createRelationshipMapping.ts +19 -11
|
@@ -1,12 +1,26 @@
|
|
|
1
1
|
import { Model } from "objection";
|
|
2
|
+
import { isModelClassSpecifier } from "../domain/RelationshipOpts.js";
|
|
2
3
|
import { RelatesTo } from "./relatesTo.js";
|
|
3
4
|
/**
|
|
4
5
|
*
|
|
6
|
+
* @param typeOrOpts
|
|
5
7
|
* @param opts
|
|
6
8
|
* @decorator
|
|
7
9
|
* @objection
|
|
8
10
|
*/
|
|
9
|
-
export function BelongsToOne(opts) {
|
|
10
|
-
|
|
11
|
+
export function BelongsToOne(typeOrOpts, opts) {
|
|
12
|
+
// For backwards compatibility
|
|
13
|
+
if (!typeOrOpts && !opts)
|
|
14
|
+
return RelatesTo(Model.BelongsToOneRelation, undefined);
|
|
15
|
+
if (!typeOrOpts && opts) {
|
|
16
|
+
typeOrOpts = opts;
|
|
17
|
+
opts = undefined;
|
|
18
|
+
}
|
|
19
|
+
const options = isModelClassSpecifier(typeOrOpts)
|
|
20
|
+
? opts
|
|
21
|
+
? { ...opts, type: typeOrOpts }
|
|
22
|
+
: { type: typeOrOpts }
|
|
23
|
+
: typeOrOpts;
|
|
24
|
+
return RelatesTo(Model.BelongsToOneRelation, options);
|
|
11
25
|
}
|
|
12
26
|
//# sourceMappingURL=belongsToOne.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"belongsToOne.js","sourceRoot":"","sources":["../../../src/decorators/belongsToOne.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,
|
|
1
|
+
{"version":3,"file":"belongsToOne.js","sourceRoot":"","sources":["../../../src/decorators/belongsToOne.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAsB,MAAM,WAAW,CAAC;AAErD,OAAO,EAAC,qBAAqB,EAAmD,MAAM,+BAA+B,CAAC;AACtH,OAAO,EAAC,SAAS,EAAC,MAAM,gBAAgB,CAAC;AAwBzC;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAC1B,UAAiE,EACjE,IAAqC;IAErC,8BAA8B;IAC9B,IAAI,CAAC,UAAU,IAAI,CAAC,IAAI;QAAE,OAAO,SAAS,CAAC,KAAK,CAAC,oBAAoB,EAAE,SAAS,CAAC,CAAC;IAClF,IAAI,CAAC,UAAU,IAAI,IAAI,EAAE,CAAC;QACxB,UAAU,GAAG,IAAI,CAAC;QAClB,IAAI,GAAG,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,OAAO,GAAiC,qBAAqB,CAAC,UAAU,CAAC;QAC7E,CAAC,CAAC,IAAI;YACJ,CAAC,CAAC,EAAC,GAAG,IAAI,EAAE,IAAI,EAAE,UAAU,EAAC;YAC7B,CAAC,CAAC,EAAC,IAAI,EAAE,UAAU,EAAC;QACtB,CAAC,CAAC,UAAU,CAAC;IAEf,OAAO,SAAS,CAAC,KAAK,CAAC,oBAAoB,EAAE,OAAO,CAAC,CAAC;AACxD,CAAC"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { StoreFn, useDecorators } from "@tsed/core";
|
|
2
2
|
import { CollectionOf, Property } from "@tsed/schema";
|
|
3
|
-
import { isModelClassFactory } from "../domain/RelationshipOpts.js";
|
|
4
3
|
import { createRelationshipMapping } from "../utils/createRelationshipMapping.js";
|
|
5
4
|
import { OBJECTION_RELATIONSHIP_KEY } from "../utils/getJsonEntityRelationships.js";
|
|
6
5
|
/**
|
|
@@ -12,9 +11,6 @@ import { OBJECTION_RELATIONSHIP_KEY } from "../utils/getJsonEntityRelationships.
|
|
|
12
11
|
*/
|
|
13
12
|
export function RelatesTo(relation, opts) {
|
|
14
13
|
return useDecorators(opts?.type ? CollectionOf(opts.type) : Property(), StoreFn((store, params) => {
|
|
15
|
-
if (opts && isModelClassFactory(opts.type)) {
|
|
16
|
-
opts.type = opts.type();
|
|
17
|
-
}
|
|
18
14
|
store.set(OBJECTION_RELATIONSHIP_KEY, createRelationshipMapping(params, relation, opts));
|
|
19
15
|
}));
|
|
20
16
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relatesTo.js","sourceRoot":"","sources":["../../../src/decorators/relatesTo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAE,aAAa,EAAC,MAAM,YAAY,CAAC;AAClD,OAAO,EAAC,YAAY,EAAE,QAAQ,EAAC,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"relatesTo.js","sourceRoot":"","sources":["../../../src/decorators/relatesTo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAE,aAAa,EAAC,MAAM,YAAY,CAAC;AAClD,OAAO,EAAC,YAAY,EAAE,QAAQ,EAAC,MAAM,cAAc,CAAC;AAIpD,OAAO,EAAC,yBAAyB,EAAC,MAAM,uCAAuC,CAAC;AAChF,OAAO,EAAC,0BAA0B,EAAC,MAAM,wCAAwC,CAAC;AAElF;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CAAC,QAAsB,EAAE,IAAuB;IACvE,OAAO,aAAa,CAClB,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,EACjD,OAAO,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE;QACxB,KAAK,CAAC,GAAG,CAAC,0BAA0B,EAAE,yBAAyB,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC,CAAC;IAC3F,CAAC,CAAC,CACH,CAAC;AACJ,CAAC"}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { isFunction, isString } from "@tsed/core";
|
|
2
2
|
export const isRelationshipOptsWithThrough = (opts) => opts !== undefined && opts.through !== undefined;
|
|
3
3
|
export const isModelClassFactory = (type) => type !== undefined && !isString(type) && isFunction(type) && !("prototype" in type);
|
|
4
|
+
export const isModelClassSpecifier = (specifierOrOther) => {
|
|
5
|
+
return !!specifierOrOther && (isString(specifierOrOther) || isFunction(specifierOrOther));
|
|
6
|
+
};
|
|
4
7
|
//# sourceMappingURL=RelationshipOpts.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RelationshipOpts.js","sourceRoot":"","sources":["../../../src/domain/RelationshipOpts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;AAShD,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,IAAuB,EAAuC,EAAE,CAC5G,IAAI,KAAK,SAAS,IAAkC,IAAK,CAAC,OAAO,KAAK,SAAS,CAAC;AAElF,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,IAA0B,EAA6B,EAAE,CAC3F,IAAI,KAAK,SAAS,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,WAAW,IAAI,IAAI,CAAC,CAAC"}
|
|
1
|
+
{"version":3,"file":"RelationshipOpts.js","sourceRoot":"","sources":["../../../src/domain/RelationshipOpts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;AAShD,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,IAAuB,EAAuC,EAAE,CAC5G,IAAI,KAAK,SAAS,IAAkC,IAAK,CAAC,OAAO,KAAK,SAAS,CAAC;AAElF,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,IAA0B,EAA6B,EAAE,CAC3F,IAAI,KAAK,SAAS,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,WAAW,IAAI,IAAI,CAAC,CAAC;AAEtF,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,gBAAyB,EAA2C,EAAE;IAC1G,OAAO,CAAC,CAAC,gBAAgB,IAAI,CAAC,QAAQ,CAAC,gBAAgB,CAAC,IAAI,UAAU,CAAC,gBAAgB,CAAC,CAAC,CAAC;AAC5F,CAAC,CAAC"}
|
|
@@ -1,18 +1,27 @@
|
|
|
1
1
|
import { Metadata } from "@tsed/core";
|
|
2
|
+
import { isModelClassFactory } from "../domain/RelationshipOpts.js";
|
|
2
3
|
import { createJoinKeys } from "./createJoinKeys.js";
|
|
3
4
|
/**
|
|
4
5
|
* @ignore
|
|
5
6
|
*/
|
|
6
7
|
export function createRelationshipMapping([target, propertyKey], relation, opts) {
|
|
7
|
-
return (targetClass) =>
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
return (targetClass) => {
|
|
9
|
+
opts = opts
|
|
10
|
+
? {
|
|
11
|
+
...opts,
|
|
12
|
+
type: isModelClassFactory(opts.type) ? opts.type() : opts.type
|
|
13
|
+
}
|
|
14
|
+
: undefined;
|
|
15
|
+
return {
|
|
16
|
+
[propertyKey]: {
|
|
17
|
+
relation,
|
|
18
|
+
modelClass: opts?.type || Metadata.getType(target, propertyKey),
|
|
19
|
+
join: createJoinKeys(targetClass, target, String(propertyKey), relation, opts),
|
|
20
|
+
modify: opts?.modify,
|
|
21
|
+
filter: opts?.filter,
|
|
22
|
+
beforeInsert: opts?.beforeInsert
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
};
|
|
17
26
|
}
|
|
18
27
|
//# sourceMappingURL=createRelationshipMapping.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createRelationshipMapping.js","sourceRoot":"","sources":["../../../src/utils/createRelationshipMapping.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgC,QAAQ,EAAC,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"createRelationshipMapping.js","sourceRoot":"","sources":["../../../src/utils/createRelationshipMapping.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgC,QAAQ,EAAC,MAAM,YAAY,CAAC;AAGnE,OAAO,EAAC,mBAAmB,EAAmB,MAAM,+BAA+B,CAAC;AACpF,OAAO,EAAC,cAAc,EAAC,MAAM,qBAAqB,CAAC;AAEnD;;GAEG;AACH,MAAM,UAAU,yBAAyB,CAAC,CAAC,MAAM,EAAE,WAAW,CAAsB,EAAE,QAAsB,EAAE,IAAuB;IACnI,OAAO,CAAC,WAAgB,EAAE,EAAE;QAC1B,IAAI,GAAG,IAAI;YACT,CAAC,CAAC;gBACE,GAAG,IAAI;gBACP,IAAI,EAAE,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI;aAC/D;YACH,CAAC,CAAC,SAAS,CAAC;QACd,OAAO;YACL,CAAC,WAAW,CAAC,EAAE;gBACb,QAAQ;gBACR,UAAU,EAAE,IAAI,EAAE,IAAI,IAAI,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,WAAW,CAAC;gBAC/D,IAAI,EAAE,cAAc,CAAC,WAAW,EAAE,MAAM,EAAE,MAAM,CAAC,WAAW,CAAC,EAAE,QAAQ,EAAE,IAAI,CAAC;gBAC9E,MAAM,EAAE,IAAI,EAAE,MAAM;gBACpB,MAAM,EAAE,IAAI,EAAE,MAAM;gBACpB,YAAY,EAAE,IAAI,EAAE,YAAY;aACjC;SACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -1,4 +1,12 @@
|
|
|
1
|
+
import { ModelClassSpecifier } from "objection";
|
|
1
2
|
import { RelationshipOptsWithoutThrough } from "../domain/RelationshipOpts.js";
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param type
|
|
6
|
+
* @decorator
|
|
7
|
+
* @objection
|
|
8
|
+
*/
|
|
9
|
+
export declare function BelongsToOne(type?: ModelClassSpecifier): PropertyDecorator;
|
|
2
10
|
/**
|
|
3
11
|
*
|
|
4
12
|
* @param opts
|
|
@@ -6,3 +14,11 @@ import { RelationshipOptsWithoutThrough } from "../domain/RelationshipOpts.js";
|
|
|
6
14
|
* @objection
|
|
7
15
|
*/
|
|
8
16
|
export declare function BelongsToOne(opts?: RelationshipOptsWithoutThrough): PropertyDecorator;
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* @param opts
|
|
20
|
+
* @param type
|
|
21
|
+
* @decorator
|
|
22
|
+
* @objection
|
|
23
|
+
*/
|
|
24
|
+
export declare function BelongsToOne(type?: ModelClassSpecifier, opts?: RelationshipOptsWithoutThrough): PropertyDecorator;
|
|
@@ -6,3 +6,4 @@ export type RelationshipOpts = (RelationshipOptsWithThrough | RelationshipOptsWi
|
|
|
6
6
|
};
|
|
7
7
|
export declare const isRelationshipOptsWithThrough: (opts?: RelationshipOpts) => opts is RelationshipOptsWithThrough;
|
|
8
8
|
export declare const isModelClassFactory: (type?: ModelClassSpecifier) => type is ModelClassFactory;
|
|
9
|
+
export declare const isModelClassSpecifier: (specifierOrOther: unknown) => specifierOrOther is ModelClassSpecifier;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@tsed/objection",
|
|
3
3
|
"description": "Objection.js package for Ts.ED framework",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.4.
|
|
5
|
+
"version": "8.4.6",
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
7
|
"main": "./lib/esm/index.js",
|
|
8
8
|
"module": "./lib/esm/index.js",
|
|
@@ -26,13 +26,13 @@
|
|
|
26
26
|
"tslib": "2.7.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@tsed/ajv": "8.4.
|
|
30
|
-
"@tsed/barrels": "8.4.
|
|
31
|
-
"@tsed/core": "8.4.
|
|
32
|
-
"@tsed/di": "8.4.
|
|
33
|
-
"@tsed/json-mapper": "8.4.
|
|
34
|
-
"@tsed/schema": "8.4.
|
|
35
|
-
"@tsed/typescript": "8.4.
|
|
29
|
+
"@tsed/ajv": "8.4.6",
|
|
30
|
+
"@tsed/barrels": "8.4.6",
|
|
31
|
+
"@tsed/core": "8.4.6",
|
|
32
|
+
"@tsed/di": "8.4.6",
|
|
33
|
+
"@tsed/json-mapper": "8.4.6",
|
|
34
|
+
"@tsed/schema": "8.4.6",
|
|
35
|
+
"@tsed/typescript": "8.4.6",
|
|
36
36
|
"eslint": "9.12.0",
|
|
37
37
|
"knex": "^3.1.0",
|
|
38
38
|
"objection": "^2.2.18",
|
|
@@ -41,11 +41,11 @@
|
|
|
41
41
|
"vitest": "2.1.2"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@tsed/ajv": ">=8.4.
|
|
45
|
-
"@tsed/core": ">=8.4.
|
|
46
|
-
"@tsed/di": ">=8.4.
|
|
47
|
-
"@tsed/json-mapper": ">=8.4.
|
|
48
|
-
"@tsed/schema": ">=8.4.
|
|
44
|
+
"@tsed/ajv": ">=8.4.6",
|
|
45
|
+
"@tsed/core": ">=8.4.6",
|
|
46
|
+
"@tsed/di": ">=8.4.6",
|
|
47
|
+
"@tsed/json-mapper": ">=8.4.6",
|
|
48
|
+
"@tsed/schema": ">=8.4.6",
|
|
49
49
|
"knex": ">=0.94.0",
|
|
50
50
|
"objection": ">=2.0.0"
|
|
51
51
|
},
|
|
@@ -57,4 +57,82 @@ describe("@BelongsToOne", () => {
|
|
|
57
57
|
}
|
|
58
58
|
});
|
|
59
59
|
});
|
|
60
|
+
it("should set custom relationship model", () => {
|
|
61
|
+
@Entity("user")
|
|
62
|
+
class User extends Model {
|
|
63
|
+
@IdColumn()
|
|
64
|
+
id!: string;
|
|
65
|
+
}
|
|
66
|
+
@Entity("movie")
|
|
67
|
+
class Movie extends Model {
|
|
68
|
+
@IdColumn()
|
|
69
|
+
id!: string;
|
|
70
|
+
userId!: string;
|
|
71
|
+
@BelongsToOne(() => User)
|
|
72
|
+
user?: User;
|
|
73
|
+
}
|
|
74
|
+
expect(Movie.relationMappings).toEqual({
|
|
75
|
+
user: {
|
|
76
|
+
relation: Model.BelongsToOneRelation,
|
|
77
|
+
modelClass: User,
|
|
78
|
+
join: {
|
|
79
|
+
from: "movie.userId",
|
|
80
|
+
to: "user.id"
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("should set custom relationship model and relationship path", () => {
|
|
87
|
+
@Entity("user")
|
|
88
|
+
class User extends Model {
|
|
89
|
+
@IdColumn()
|
|
90
|
+
id!: string;
|
|
91
|
+
userId!: string;
|
|
92
|
+
}
|
|
93
|
+
@Entity("movie")
|
|
94
|
+
class Movie extends Model {
|
|
95
|
+
@IdColumn()
|
|
96
|
+
id!: string;
|
|
97
|
+
ownerId!: string;
|
|
98
|
+
@BelongsToOne(() => User, {from: "ownerId", to: "userId"})
|
|
99
|
+
user?: User;
|
|
100
|
+
}
|
|
101
|
+
expect(Movie.relationMappings).toEqual({
|
|
102
|
+
user: {
|
|
103
|
+
relation: Model.BelongsToOneRelation,
|
|
104
|
+
modelClass: User,
|
|
105
|
+
join: {
|
|
106
|
+
from: "movie.ownerId",
|
|
107
|
+
to: "user.userId"
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
it("should work when options supplied as 2nd argument without first provided", () => {
|
|
113
|
+
@Entity("user")
|
|
114
|
+
class User extends Model {
|
|
115
|
+
@IdColumn()
|
|
116
|
+
id!: string;
|
|
117
|
+
userId!: string;
|
|
118
|
+
}
|
|
119
|
+
@Entity("movie")
|
|
120
|
+
class Movie extends Model {
|
|
121
|
+
@IdColumn()
|
|
122
|
+
id!: string;
|
|
123
|
+
ownerId!: string;
|
|
124
|
+
@BelongsToOne(undefined, {from: "ownerId", to: "userId"})
|
|
125
|
+
user?: User;
|
|
126
|
+
}
|
|
127
|
+
expect(Movie.relationMappings).toEqual({
|
|
128
|
+
user: {
|
|
129
|
+
relation: Model.BelongsToOneRelation,
|
|
130
|
+
modelClass: User,
|
|
131
|
+
join: {
|
|
132
|
+
from: "movie.ownerId",
|
|
133
|
+
to: "user.userId"
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
});
|
|
60
138
|
});
|
|
@@ -1,14 +1,53 @@
|
|
|
1
|
-
import {Model} from "objection";
|
|
1
|
+
import {Model, ModelClassSpecifier} from "objection";
|
|
2
2
|
|
|
3
|
-
import {RelationshipOptsWithoutThrough} from "../domain/RelationshipOpts.js";
|
|
3
|
+
import {isModelClassSpecifier, RelationshipOpts, RelationshipOptsWithoutThrough} from "../domain/RelationshipOpts.js";
|
|
4
4
|
import {RelatesTo} from "./relatesTo.js";
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* @param type
|
|
9
|
+
* @decorator
|
|
10
|
+
* @objection
|
|
11
|
+
*/
|
|
12
|
+
export function BelongsToOne(type?: ModelClassSpecifier): PropertyDecorator;
|
|
6
13
|
/**
|
|
7
14
|
*
|
|
8
15
|
* @param opts
|
|
9
16
|
* @decorator
|
|
10
17
|
* @objection
|
|
11
18
|
*/
|
|
12
|
-
export function BelongsToOne(opts?: RelationshipOptsWithoutThrough): PropertyDecorator
|
|
13
|
-
|
|
19
|
+
export function BelongsToOne(opts?: RelationshipOptsWithoutThrough): PropertyDecorator;
|
|
20
|
+
/**
|
|
21
|
+
*
|
|
22
|
+
* @param opts
|
|
23
|
+
* @param type
|
|
24
|
+
* @decorator
|
|
25
|
+
* @objection
|
|
26
|
+
*/
|
|
27
|
+
export function BelongsToOne(type?: ModelClassSpecifier, opts?: RelationshipOptsWithoutThrough): PropertyDecorator;
|
|
28
|
+
/**
|
|
29
|
+
*
|
|
30
|
+
* @param typeOrOpts
|
|
31
|
+
* @param opts
|
|
32
|
+
* @decorator
|
|
33
|
+
* @objection
|
|
34
|
+
*/
|
|
35
|
+
export function BelongsToOne(
|
|
36
|
+
typeOrOpts?: ModelClassSpecifier | RelationshipOptsWithoutThrough,
|
|
37
|
+
opts?: RelationshipOptsWithoutThrough
|
|
38
|
+
): PropertyDecorator {
|
|
39
|
+
// For backwards compatibility
|
|
40
|
+
if (!typeOrOpts && !opts) return RelatesTo(Model.BelongsToOneRelation, undefined);
|
|
41
|
+
if (!typeOrOpts && opts) {
|
|
42
|
+
typeOrOpts = opts;
|
|
43
|
+
opts = undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const options: RelationshipOpts | undefined = isModelClassSpecifier(typeOrOpts)
|
|
47
|
+
? opts
|
|
48
|
+
? {...opts, type: typeOrOpts}
|
|
49
|
+
: {type: typeOrOpts}
|
|
50
|
+
: typeOrOpts;
|
|
51
|
+
|
|
52
|
+
return RelatesTo(Model.BelongsToOneRelation, options);
|
|
14
53
|
}
|
|
@@ -17,9 +17,6 @@ export function RelatesTo(relation: RelationType, opts?: RelationshipOpts): Prop
|
|
|
17
17
|
return useDecorators(
|
|
18
18
|
opts?.type ? CollectionOf(opts.type) : Property(),
|
|
19
19
|
StoreFn((store, params) => {
|
|
20
|
-
if (opts && isModelClassFactory(opts.type)) {
|
|
21
|
-
opts.type = opts.type();
|
|
22
|
-
}
|
|
23
20
|
store.set(OBJECTION_RELATIONSHIP_KEY, createRelationshipMapping(params, relation, opts));
|
|
24
21
|
})
|
|
25
22
|
);
|
|
@@ -12,3 +12,7 @@ export const isRelationshipOptsWithThrough = (opts?: RelationshipOpts): opts is
|
|
|
12
12
|
|
|
13
13
|
export const isModelClassFactory = (type?: ModelClassSpecifier): type is ModelClassFactory =>
|
|
14
14
|
type !== undefined && !isString(type) && isFunction(type) && !("prototype" in type);
|
|
15
|
+
|
|
16
|
+
export const isModelClassSpecifier = (specifierOrOther: unknown): specifierOrOther is ModelClassSpecifier => {
|
|
17
|
+
return !!specifierOrOther && (isString(specifierOrOther) || isFunction(specifierOrOther));
|
|
18
|
+
};
|
|
@@ -1,21 +1,29 @@
|
|
|
1
1
|
import {DecoratorParameters, getClass, Metadata} from "@tsed/core";
|
|
2
2
|
import {RelationType} from "objection";
|
|
3
3
|
|
|
4
|
-
import {RelationshipOpts} from "../domain/RelationshipOpts.js";
|
|
4
|
+
import {isModelClassFactory, RelationshipOpts} from "../domain/RelationshipOpts.js";
|
|
5
5
|
import {createJoinKeys} from "./createJoinKeys.js";
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @ignore
|
|
9
9
|
*/
|
|
10
10
|
export function createRelationshipMapping([target, propertyKey]: DecoratorParameters, relation: RelationType, opts?: RelationshipOpts) {
|
|
11
|
-
return (targetClass: any) =>
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
11
|
+
return (targetClass: any) => {
|
|
12
|
+
opts = opts
|
|
13
|
+
? {
|
|
14
|
+
...opts,
|
|
15
|
+
type: isModelClassFactory(opts.type) ? opts.type() : opts.type
|
|
16
|
+
}
|
|
17
|
+
: undefined;
|
|
18
|
+
return {
|
|
19
|
+
[propertyKey]: {
|
|
20
|
+
relation,
|
|
21
|
+
modelClass: opts?.type || Metadata.getType(target, propertyKey),
|
|
22
|
+
join: createJoinKeys(targetClass, target, String(propertyKey), relation, opts),
|
|
23
|
+
modify: opts?.modify,
|
|
24
|
+
filter: opts?.filter,
|
|
25
|
+
beforeInsert: opts?.beforeInsert
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
};
|
|
21
29
|
}
|