@tsed/objection 8.11.1 → 8.11.2-rc.1
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/components/createBooleanColumn.js +0 -1
- package/lib/esm/components/createIdColumn.js +0 -1
- package/lib/esm/components/createNumberColumn.js +0 -1
- package/lib/esm/components/createStringColumn.js +0 -1
- package/lib/esm/decorators/belongsToOne.js +0 -1
- package/lib/esm/decorators/columnOptions.js +0 -1
- package/lib/esm/decorators/decimal.js +0 -1
- package/lib/esm/decorators/entity.js +0 -1
- package/lib/esm/decorators/hasMany.js +0 -1
- package/lib/esm/decorators/hasOne.js +0 -1
- package/lib/esm/decorators/hasOneThroughRelation.js +0 -1
- package/lib/esm/decorators/idColumn.js +0 -1
- package/lib/esm/decorators/manyToMany.js +0 -1
- package/lib/esm/decorators/relatesTo.js +0 -1
- package/lib/esm/domain/ColumnOpts.js +0 -1
- package/lib/esm/domain/RelationshipOpts.js +0 -1
- package/lib/esm/domain/interfaces.js +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/services/ColumnTypesContainer.js +0 -1
- package/lib/esm/services/ObjectionConnection.js +0 -1
- package/lib/esm/utils/connect.js +0 -1
- package/lib/esm/utils/createColumns.js +0 -1
- package/lib/esm/utils/createJoinKeys.js +0 -1
- package/lib/esm/utils/createRelationshipMapping.js +0 -1
- package/lib/esm/utils/defineStaticGetter.js +0 -1
- package/lib/esm/utils/getColumnCtx.js +0 -1
- package/lib/esm/utils/getJsonEntityRelationships.js +0 -1
- package/package.json +12 -9
- package/lib/esm/components/createBooleanColumn.js.map +0 -1
- package/lib/esm/components/createIdColumn.js.map +0 -1
- package/lib/esm/components/createNumberColumn.js.map +0 -1
- package/lib/esm/components/createStringColumn.js.map +0 -1
- package/lib/esm/decorators/belongsToOne.js.map +0 -1
- package/lib/esm/decorators/columnOptions.js.map +0 -1
- package/lib/esm/decorators/decimal.js.map +0 -1
- package/lib/esm/decorators/entity.js.map +0 -1
- package/lib/esm/decorators/hasMany.js.map +0 -1
- package/lib/esm/decorators/hasOne.js.map +0 -1
- package/lib/esm/decorators/hasOneThroughRelation.js.map +0 -1
- package/lib/esm/decorators/idColumn.js.map +0 -1
- package/lib/esm/decorators/manyToMany.js.map +0 -1
- package/lib/esm/decorators/relatesTo.js.map +0 -1
- package/lib/esm/domain/ColumnOpts.js.map +0 -1
- package/lib/esm/domain/RelationshipOpts.js.map +0 -1
- package/lib/esm/domain/interfaces.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/services/ColumnTypesContainer.js.map +0 -1
- package/lib/esm/services/ObjectionConnection.js.map +0 -1
- package/lib/esm/utils/connect.js.map +0 -1
- package/lib/esm/utils/createColumns.js.map +0 -1
- package/lib/esm/utils/createJoinKeys.js.map +0 -1
- package/lib/esm/utils/createRelationshipMapping.js.map +0 -1
- package/lib/esm/utils/defineStaticGetter.js.map +0 -1
- package/lib/esm/utils/getColumnCtx.js.map +0 -1
- package/lib/esm/utils/getJsonEntityRelationships.js.map +0 -1
- package/src/components/createBooleanColumn.spec.ts +0 -20
- package/src/components/createBooleanColumn.ts +0 -12
- package/src/components/createIdColumn.spec.ts +0 -45
- package/src/components/createIdColumn.ts +0 -24
- package/src/components/createNumberColumn.spec.ts +0 -34
- package/src/components/createNumberColumn.ts +0 -13
- package/src/components/createStringColumn.spec.ts +0 -34
- package/src/components/createStringColumn.ts +0 -13
- package/src/decorators/belongsToOne.spec.ts +0 -138
- package/src/decorators/belongsToOne.ts +0 -53
- package/src/decorators/columnOptions.ts +0 -13
- package/src/decorators/decimal.spec.ts +0 -52
- package/src/decorators/decimal.ts +0 -35
- package/src/decorators/entity.spec.ts +0 -24
- package/src/decorators/entity.ts +0 -29
- package/src/decorators/hasMany.spec.ts +0 -60
- package/src/decorators/hasMany.ts +0 -14
- package/src/decorators/hasOne.spec.ts +0 -60
- package/src/decorators/hasOne.ts +0 -14
- package/src/decorators/hasOneThroughRelation.spec.ts +0 -72
- package/src/decorators/hasOneThroughRelation.ts +0 -14
- package/src/decorators/idColumn.spec.ts +0 -31
- package/src/decorators/idColumn.ts +0 -26
- package/src/decorators/manyToMany.spec.ts +0 -72
- package/src/decorators/manyToMany.ts +0 -14
- package/src/decorators/relatesTo.spec.ts +0 -34
- package/src/decorators/relatesTo.ts +0 -23
- package/src/domain/ColumnOpts.ts +0 -4
- package/src/domain/RelationshipOpts.ts +0 -18
- package/src/domain/interfaces.ts +0 -9
- package/src/index.ts +0 -29
- package/src/services/ColumnTypesContainer.ts +0 -8
- package/src/services/ObjectionConnection.ts +0 -26
- package/src/utils/connect.ts +0 -9
- package/src/utils/createColumns.spec.ts +0 -79
- package/src/utils/createColumns.ts +0 -39
- package/src/utils/createJoinKeys.ts +0 -38
- package/src/utils/createRelationshipMapping.ts +0 -29
- package/src/utils/defineStaticGetter.ts +0 -10
- package/src/utils/getColumnCtx.ts +0 -21
- package/src/utils/getJsonEntityRelationships.ts +0 -16
- package/vitest.config.mts +0 -21
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import {Model} from "objection";
|
|
2
|
-
|
|
3
|
-
import {BelongsToOne} from "./belongsToOne.js";
|
|
4
|
-
import {Entity} from "./entity.js";
|
|
5
|
-
import {IdColumn} from "./idColumn.js";
|
|
6
|
-
|
|
7
|
-
describe("@BelongsToOne", () => {
|
|
8
|
-
it("should set metadata", () => {
|
|
9
|
-
@Entity("user")
|
|
10
|
-
class User extends Model {
|
|
11
|
-
@IdColumn()
|
|
12
|
-
id!: string;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
@Entity("movie")
|
|
16
|
-
class Movie extends Model {
|
|
17
|
-
@IdColumn()
|
|
18
|
-
id!: string;
|
|
19
|
-
userId!: string;
|
|
20
|
-
@BelongsToOne()
|
|
21
|
-
user?: User;
|
|
22
|
-
}
|
|
23
|
-
expect(Movie.relationMappings).toEqual({
|
|
24
|
-
user: {
|
|
25
|
-
relation: Model.BelongsToOneRelation,
|
|
26
|
-
modelClass: User,
|
|
27
|
-
join: {
|
|
28
|
-
from: "movie.userId",
|
|
29
|
-
to: "user.id"
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
it("should set custom relationship path", () => {
|
|
35
|
-
@Entity("user")
|
|
36
|
-
class User extends Model {
|
|
37
|
-
@IdColumn()
|
|
38
|
-
id!: string;
|
|
39
|
-
userId!: string;
|
|
40
|
-
}
|
|
41
|
-
@Entity("movie")
|
|
42
|
-
class Movie extends Model {
|
|
43
|
-
@IdColumn()
|
|
44
|
-
id!: string;
|
|
45
|
-
ownerId!: string;
|
|
46
|
-
@BelongsToOne({from: "ownerId", to: "userId"})
|
|
47
|
-
user?: User;
|
|
48
|
-
}
|
|
49
|
-
expect(Movie.relationMappings).toEqual({
|
|
50
|
-
user: {
|
|
51
|
-
relation: Model.BelongsToOneRelation,
|
|
52
|
-
modelClass: User,
|
|
53
|
-
join: {
|
|
54
|
-
from: "movie.ownerId",
|
|
55
|
-
to: "user.userId"
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
});
|
|
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
|
-
});
|
|
138
|
-
});
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import {Model, ModelClassSpecifier} from "objection";
|
|
2
|
-
|
|
3
|
-
import {isModelClassSpecifier, RelationshipOpts, RelationshipOptsWithoutThrough} from "../domain/RelationshipOpts.js";
|
|
4
|
-
import {RelatesTo} from "./relatesTo.js";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
*
|
|
8
|
-
* @param type
|
|
9
|
-
* @decorator
|
|
10
|
-
* @objection
|
|
11
|
-
*/
|
|
12
|
-
export function BelongsToOne(type?: ModelClassSpecifier): PropertyDecorator;
|
|
13
|
-
/**
|
|
14
|
-
*
|
|
15
|
-
* @param opts
|
|
16
|
-
* @decorator
|
|
17
|
-
* @objection
|
|
18
|
-
*/
|
|
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);
|
|
53
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import {StoreSet} from "@tsed/core";
|
|
2
|
-
|
|
3
|
-
import type {ColumnOpts} from "../domain/ColumnOpts.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
*
|
|
7
|
-
* @param options
|
|
8
|
-
* @decorator
|
|
9
|
-
* @objection
|
|
10
|
-
*/
|
|
11
|
-
export function ColumnOptions(options: Partial<ColumnOpts>): PropertyDecorator {
|
|
12
|
-
return StoreSet("objection", options) as any;
|
|
13
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import {Store} from "@tsed/core";
|
|
2
|
-
import {getJsonSchema} from "@tsed/schema";
|
|
3
|
-
|
|
4
|
-
import {Decimal} from "./decimal.js";
|
|
5
|
-
|
|
6
|
-
describe("@Decimal", () => {
|
|
7
|
-
it("should set metadata", () => {
|
|
8
|
-
class MyModel {
|
|
9
|
-
@Decimal({scale: 1, precision: 12})
|
|
10
|
-
prop: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
expect(getJsonSchema(MyModel)).toEqual({
|
|
14
|
-
properties: {
|
|
15
|
-
prop: {
|
|
16
|
-
type: "number"
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
type: "object"
|
|
20
|
-
});
|
|
21
|
-
expect(Store.from(MyModel, "prop").get("objection")).toEqual({
|
|
22
|
-
columnType: "number",
|
|
23
|
-
options: {
|
|
24
|
-
precision: 12,
|
|
25
|
-
scale: 1,
|
|
26
|
-
type: "decimal"
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("should set metadata (without options)", () => {
|
|
32
|
-
class MyModel {
|
|
33
|
-
@Decimal()
|
|
34
|
-
prop: number;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
expect(getJsonSchema(MyModel)).toEqual({
|
|
38
|
-
properties: {
|
|
39
|
-
prop: {
|
|
40
|
-
type: "number"
|
|
41
|
-
}
|
|
42
|
-
},
|
|
43
|
-
type: "object"
|
|
44
|
-
});
|
|
45
|
-
expect(Store.from(MyModel, "prop").get("objection")).toEqual({
|
|
46
|
-
columnType: "number",
|
|
47
|
-
options: {
|
|
48
|
-
type: "decimal"
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
});
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import {StoreSet, useDecorators} from "@tsed/core";
|
|
2
|
-
import {Property} from "@tsed/schema";
|
|
3
|
-
|
|
4
|
-
import {ColumnOptions} from "./columnOptions.js";
|
|
5
|
-
|
|
6
|
-
export interface DecimalOptions {
|
|
7
|
-
precision?: number | null;
|
|
8
|
-
scale?: number;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
*
|
|
13
|
-
* @param options
|
|
14
|
-
* @decorator
|
|
15
|
-
* @objection
|
|
16
|
-
*/
|
|
17
|
-
export function Decimal(options: DecimalOptions = {}) {
|
|
18
|
-
return useDecorators(
|
|
19
|
-
Property(),
|
|
20
|
-
ColumnOptions({
|
|
21
|
-
columnType: "number",
|
|
22
|
-
options: {
|
|
23
|
-
type: "decimal",
|
|
24
|
-
...options
|
|
25
|
-
}
|
|
26
|
-
}),
|
|
27
|
-
StoreSet("objection", {
|
|
28
|
-
columnType: "number",
|
|
29
|
-
options: {
|
|
30
|
-
type: "decimal",
|
|
31
|
-
...options
|
|
32
|
-
}
|
|
33
|
-
})
|
|
34
|
-
);
|
|
35
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import {Entity} from "./entity.js";
|
|
2
|
-
|
|
3
|
-
describe("@Entity", () => {
|
|
4
|
-
it("should set metadata", () => {
|
|
5
|
-
@Entity("models")
|
|
6
|
-
class MyModel {}
|
|
7
|
-
|
|
8
|
-
expect((MyModel as any).jsonSchema).toEqual({
|
|
9
|
-
type: "object"
|
|
10
|
-
});
|
|
11
|
-
expect((MyModel as any).tableName).toEqual("models");
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it("should throw metadata when the tableName is empty", () => {
|
|
15
|
-
let actualError: any;
|
|
16
|
-
try {
|
|
17
|
-
@Entity("")
|
|
18
|
-
class MyModel {}
|
|
19
|
-
} catch (er) {
|
|
20
|
-
actualError = er;
|
|
21
|
-
}
|
|
22
|
-
expect(actualError.message).toEqual("Please provide a table name");
|
|
23
|
-
});
|
|
24
|
-
});
|
package/src/decorators/entity.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import {Type} from "@tsed/core";
|
|
2
|
-
import {getJsonSchema} from "@tsed/schema";
|
|
3
|
-
|
|
4
|
-
import {defineStaticGetter} from "../utils/defineStaticGetter.js";
|
|
5
|
-
import {getJsonEntityRelationships} from "../utils/getJsonEntityRelationships.js";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
* @param tableName
|
|
10
|
-
* @decorator
|
|
11
|
-
* @objection
|
|
12
|
-
*/
|
|
13
|
-
export function Entity(tableName: string): ClassDecorator {
|
|
14
|
-
if (!tableName) {
|
|
15
|
-
throw new Error("Please provide a table name");
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return (target: any) => {
|
|
19
|
-
const originalRelationMappings = target["relationMappings"];
|
|
20
|
-
defineStaticGetter(target, "tableName", () => tableName);
|
|
21
|
-
defineStaticGetter(target, "jsonSchema", () => getJsonSchema(target));
|
|
22
|
-
defineStaticGetter(target, "relationMappings", () => originalRelationMappings || getJsonEntityRelationships(target));
|
|
23
|
-
return target;
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface EntityMethods<T> extends Type<T> {
|
|
28
|
-
readonly tableName: string;
|
|
29
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import {Model} from "objection";
|
|
2
|
-
|
|
3
|
-
import {Entity} from "./entity.js";
|
|
4
|
-
import {HasMany} from "./hasMany.js";
|
|
5
|
-
import {IdColumn} from "./idColumn.js";
|
|
6
|
-
|
|
7
|
-
describe("@HasMany", () => {
|
|
8
|
-
it("should set metadata", () => {
|
|
9
|
-
@Entity("pet")
|
|
10
|
-
class Pet extends Model {
|
|
11
|
-
@IdColumn()
|
|
12
|
-
id!: string;
|
|
13
|
-
userId?: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
@Entity("user")
|
|
17
|
-
class User extends Model {
|
|
18
|
-
@IdColumn()
|
|
19
|
-
id!: string;
|
|
20
|
-
@HasMany(Pet)
|
|
21
|
-
pets?: Pet[];
|
|
22
|
-
}
|
|
23
|
-
expect(User.relationMappings).toEqual({
|
|
24
|
-
pets: {
|
|
25
|
-
relation: Model.HasManyRelation,
|
|
26
|
-
modelClass: Pet,
|
|
27
|
-
join: {
|
|
28
|
-
from: "user.id",
|
|
29
|
-
to: "pet.userId"
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
it("should set custom relationship path", () => {
|
|
35
|
-
@Entity("pet")
|
|
36
|
-
class Pet extends Model {
|
|
37
|
-
@IdColumn()
|
|
38
|
-
id!: string;
|
|
39
|
-
ownerId?: string;
|
|
40
|
-
}
|
|
41
|
-
@Entity("user")
|
|
42
|
-
class User extends Model {
|
|
43
|
-
@IdColumn()
|
|
44
|
-
id!: string;
|
|
45
|
-
userId!: string;
|
|
46
|
-
@HasMany(Pet, {from: "userId", to: "ownerId"})
|
|
47
|
-
pets?: Pet[];
|
|
48
|
-
}
|
|
49
|
-
expect(User.relationMappings).toEqual({
|
|
50
|
-
pets: {
|
|
51
|
-
relation: Model.HasManyRelation,
|
|
52
|
-
modelClass: Pet,
|
|
53
|
-
join: {
|
|
54
|
-
from: "user.userId",
|
|
55
|
-
to: "pet.ownerId"
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
});
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import {Model, ModelClassSpecifier} from "objection";
|
|
2
|
-
|
|
3
|
-
import {RelationshipOptsWithThrough} from "../domain/RelationshipOpts.js";
|
|
4
|
-
import {RelatesTo} from "./relatesTo.js";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
*
|
|
8
|
-
* @param opts
|
|
9
|
-
* @decorator
|
|
10
|
-
* @objection
|
|
11
|
-
*/
|
|
12
|
-
export function HasMany(type: ModelClassSpecifier, opts?: RelationshipOptsWithThrough): PropertyDecorator {
|
|
13
|
-
return RelatesTo(Model.HasManyRelation, {...opts, type});
|
|
14
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import {Model} from "objection";
|
|
2
|
-
|
|
3
|
-
import {Entity} from "./entity.js";
|
|
4
|
-
import {HasOne} from "./hasOne.js";
|
|
5
|
-
import {IdColumn} from "./idColumn.js";
|
|
6
|
-
|
|
7
|
-
describe("@HasOne", () => {
|
|
8
|
-
it("should set metadata", () => {
|
|
9
|
-
@Entity("pet")
|
|
10
|
-
class Pet extends Model {
|
|
11
|
-
@IdColumn()
|
|
12
|
-
id!: string;
|
|
13
|
-
userId?: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
@Entity("user")
|
|
17
|
-
class User extends Model {
|
|
18
|
-
@IdColumn()
|
|
19
|
-
id!: string;
|
|
20
|
-
@HasOne()
|
|
21
|
-
pet?: Pet;
|
|
22
|
-
}
|
|
23
|
-
expect(User.relationMappings).toEqual({
|
|
24
|
-
pet: {
|
|
25
|
-
relation: Model.HasOneRelation,
|
|
26
|
-
modelClass: Pet,
|
|
27
|
-
join: {
|
|
28
|
-
from: "user.id",
|
|
29
|
-
to: "pet.userId"
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
it("should set custom relationship path", () => {
|
|
35
|
-
@Entity("pet")
|
|
36
|
-
class Pet extends Model {
|
|
37
|
-
@IdColumn()
|
|
38
|
-
id!: string;
|
|
39
|
-
ownerId?: string;
|
|
40
|
-
}
|
|
41
|
-
@Entity("user")
|
|
42
|
-
class User extends Model {
|
|
43
|
-
@IdColumn()
|
|
44
|
-
id!: string;
|
|
45
|
-
userId!: string;
|
|
46
|
-
@HasOne({from: "userId", to: "ownerId"})
|
|
47
|
-
pet?: Pet;
|
|
48
|
-
}
|
|
49
|
-
expect(User.relationMappings).toEqual({
|
|
50
|
-
pet: {
|
|
51
|
-
relation: Model.HasOneRelation,
|
|
52
|
-
modelClass: Pet,
|
|
53
|
-
join: {
|
|
54
|
-
from: "user.userId",
|
|
55
|
-
to: "pet.ownerId"
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
});
|
package/src/decorators/hasOne.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import {Model} from "objection";
|
|
2
|
-
|
|
3
|
-
import {RelationshipOptsWithoutThrough} from "../domain/RelationshipOpts.js";
|
|
4
|
-
import {RelatesTo} from "./relatesTo.js";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
*
|
|
8
|
-
* @param opts
|
|
9
|
-
* @decorator
|
|
10
|
-
* @objection
|
|
11
|
-
*/
|
|
12
|
-
export function HasOne(opts?: RelationshipOptsWithoutThrough): PropertyDecorator {
|
|
13
|
-
return RelatesTo(Model.HasOneRelation, opts);
|
|
14
|
-
}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import {Model} from "objection";
|
|
2
|
-
|
|
3
|
-
import {Entity} from "./entity.js";
|
|
4
|
-
import {HasOneThroughRelation} from "./hasOneThroughRelation.js";
|
|
5
|
-
import {IdColumn} from "./idColumn.js";
|
|
6
|
-
|
|
7
|
-
describe("@HasOneThroughRelation", () => {
|
|
8
|
-
it("should set metadata", () => {
|
|
9
|
-
@Entity("pet")
|
|
10
|
-
class Pet extends Model {
|
|
11
|
-
@IdColumn()
|
|
12
|
-
id!: string;
|
|
13
|
-
userId?: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
@Entity("user")
|
|
17
|
-
class User extends Model {
|
|
18
|
-
@IdColumn()
|
|
19
|
-
id!: string;
|
|
20
|
-
@HasOneThroughRelation()
|
|
21
|
-
pet?: Pet;
|
|
22
|
-
}
|
|
23
|
-
expect(User.relationMappings).toEqual({
|
|
24
|
-
pet: {
|
|
25
|
-
relation: Model.HasOneThroughRelation,
|
|
26
|
-
modelClass: Pet,
|
|
27
|
-
join: {
|
|
28
|
-
from: "user.id",
|
|
29
|
-
through: {
|
|
30
|
-
from: "user_pet.userId",
|
|
31
|
-
to: "user_pet.petId"
|
|
32
|
-
},
|
|
33
|
-
to: "pet.id"
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
it("should set custom relationship path", () => {
|
|
39
|
-
@Entity("pet")
|
|
40
|
-
class Pet extends Model {
|
|
41
|
-
@IdColumn()
|
|
42
|
-
id!: string;
|
|
43
|
-
petId?: string;
|
|
44
|
-
}
|
|
45
|
-
@Entity("user")
|
|
46
|
-
class User extends Model {
|
|
47
|
-
@IdColumn()
|
|
48
|
-
id!: string;
|
|
49
|
-
userId!: string;
|
|
50
|
-
@HasOneThroughRelation({
|
|
51
|
-
from: "userId",
|
|
52
|
-
through: {from: "user_pet.ownerId", to: "user_pet.petId"},
|
|
53
|
-
to: "petId"
|
|
54
|
-
})
|
|
55
|
-
pet?: Pet;
|
|
56
|
-
}
|
|
57
|
-
expect(User.relationMappings).toEqual({
|
|
58
|
-
pet: {
|
|
59
|
-
relation: Model.HasOneThroughRelation,
|
|
60
|
-
modelClass: Pet,
|
|
61
|
-
join: {
|
|
62
|
-
from: "user.userId",
|
|
63
|
-
through: {
|
|
64
|
-
from: "user_pet.ownerId",
|
|
65
|
-
to: "user_pet.petId"
|
|
66
|
-
},
|
|
67
|
-
to: "pet.petId"
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
});
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import {Model} from "objection";
|
|
2
|
-
|
|
3
|
-
import {RelationshipOptsWithoutThrough, RelationshipOptsWithThrough} from "../domain/RelationshipOpts.js";
|
|
4
|
-
import {RelatesTo} from "./relatesTo.js";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
*
|
|
8
|
-
* @param opts
|
|
9
|
-
* @decorator
|
|
10
|
-
* @objection
|
|
11
|
-
*/
|
|
12
|
-
export function HasOneThroughRelation(opts?: RelationshipOptsWithThrough): PropertyDecorator {
|
|
13
|
-
return RelatesTo(Model.HasOneThroughRelation, opts);
|
|
14
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import {Store} from "@tsed/core";
|
|
2
|
-
import {getJsonSchema} from "@tsed/schema";
|
|
3
|
-
import {Model} from "objection";
|
|
4
|
-
|
|
5
|
-
import {IdColumn} from "./idColumn.js";
|
|
6
|
-
|
|
7
|
-
describe("@IdColumn", () => {
|
|
8
|
-
it("should set metadata", () => {
|
|
9
|
-
class MyModel extends Model {
|
|
10
|
-
@IdColumn()
|
|
11
|
-
id: number;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
expect(getJsonSchema(MyModel)).toEqual({
|
|
15
|
-
properties: {
|
|
16
|
-
id: {
|
|
17
|
-
type: "number"
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
type: "object"
|
|
21
|
-
});
|
|
22
|
-
expect(Store.from(MyModel, "id").get("objection")).toEqual({
|
|
23
|
-
columnType: "idColumn",
|
|
24
|
-
options: {
|
|
25
|
-
type: "increments"
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
expect(MyModel.idColumn).toEqual("id");
|
|
30
|
-
});
|
|
31
|
-
});
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import {useDecorators} from "@tsed/core";
|
|
2
|
-
import {Property} from "@tsed/schema";
|
|
3
|
-
|
|
4
|
-
import {defineStaticGetter} from "../utils/defineStaticGetter.js";
|
|
5
|
-
import {ColumnOptions} from "./columnOptions.js";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
* @param type
|
|
10
|
-
* @decorator
|
|
11
|
-
* @objection
|
|
12
|
-
*/
|
|
13
|
-
export function IdColumn(type: "uuid" | "increments" | "bigIncrements" = "increments"): PropertyDecorator {
|
|
14
|
-
return useDecorators(
|
|
15
|
-
Property(),
|
|
16
|
-
ColumnOptions({
|
|
17
|
-
columnType: "idColumn",
|
|
18
|
-
options: {
|
|
19
|
-
type
|
|
20
|
-
}
|
|
21
|
-
}),
|
|
22
|
-
(target: any, propertyKey: string) => {
|
|
23
|
-
defineStaticGetter(target, "idColumn", () => propertyKey);
|
|
24
|
-
}
|
|
25
|
-
);
|
|
26
|
-
}
|