-
-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(objection): add model relationship decorators
- Loading branch information
Ross MacPhee
committed
Sep 5, 2022
1 parent
e907869
commit e650b24
Showing
19 changed files
with
604 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/orm/objection/src/decorators/belongsToOne.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {Entity, IdColumn} from "../../lib/esm"; | ||
|
||
import {BelongsToOne} from "@tsed/objection"; | ||
import {Model} from "objection"; | ||
|
||
describe("@BelongsToOne", () => { | ||
it("should set metadata", () => { | ||
@Entity("user") | ||
class User extends Model { | ||
@IdColumn() | ||
id!: string; | ||
} | ||
@Entity("movie") | ||
class Movie extends Model { | ||
@IdColumn() | ||
id!: string; | ||
userId!: string; | ||
@BelongsToOne() | ||
user?: User; | ||
} | ||
expect(Movie.relationMappings).toEqual({ | ||
user: { | ||
relation: Model.BelongsToOneRelation, | ||
modelClass: User, | ||
join: { | ||
from: "movie.userId", | ||
to: "user.id" | ||
} | ||
} | ||
}); | ||
}); | ||
it("should set custom relationship path", () => { | ||
@Entity("user") | ||
class User extends Model { | ||
@IdColumn() | ||
id!: string; | ||
userId!: string; | ||
} | ||
@Entity("movie") | ||
class Movie extends Model { | ||
@IdColumn() | ||
id!: string; | ||
ownerId!: string; | ||
@BelongsToOne({from: "ownerId", to: "userId"}) | ||
user?: User; | ||
} | ||
expect(Movie.relationMappings).toEqual({ | ||
user: { | ||
relation: Model.BelongsToOneRelation, | ||
modelClass: User, | ||
join: { | ||
from: "movie.ownerId", | ||
to: "user.userId" | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {Model} from "objection"; | ||
import {RelatesTo} from "./relatesTo"; | ||
import {RelationshipOptsWithoutThrough} from "../domain/RelationshipOpts"; | ||
|
||
/** | ||
* | ||
* @param opts | ||
* @decorator | ||
* @objection | ||
*/ | ||
export function BelongsToOne(opts?: RelationshipOptsWithoutThrough): PropertyDecorator { | ||
return RelatesTo(Model.BelongsToOneRelation, opts); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {Entity, HasMany} from "@tsed/objection"; | ||
|
||
import {IdColumn} from "../../lib/esm"; | ||
import {Model} from "objection"; | ||
|
||
describe("@HasMany", () => { | ||
it("should set metadata", () => { | ||
@Entity("pet") | ||
class Pet extends Model { | ||
@IdColumn() | ||
id!: string; | ||
userId?: string; | ||
} | ||
@Entity("user") | ||
class User extends Model { | ||
@IdColumn() | ||
id!: string; | ||
@HasMany() | ||
pets?: Pet[]; | ||
} | ||
expect(User.relationMappings).toEqual({ | ||
pets: { | ||
relation: Model.HasManyRelation, | ||
modelClass: Pet, | ||
join: { | ||
from: "user.id", | ||
to: "pet.userId" | ||
} | ||
} | ||
}); | ||
}); | ||
it("should set custom relationship path", () => { | ||
@Entity("pet") | ||
class Pet extends Model { | ||
@IdColumn() | ||
id!: string; | ||
ownerId?: string; | ||
} | ||
@Entity("user") | ||
class User extends Model { | ||
@IdColumn() | ||
id!: string; | ||
userId!: string; | ||
@HasMany({from: "userId", to: "ownerId"}) | ||
pets?: Pet[]; | ||
} | ||
expect(User.relationMappings).toEqual({ | ||
pets: { | ||
relation: Model.HasManyRelation, | ||
modelClass: Pet, | ||
join: { | ||
from: "user.userId", | ||
to: "pet.ownerId" | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {Model} from "objection"; | ||
import {RelatesTo} from "./relatesTo"; | ||
import {RelationshipOptsWithoutThrough} from "../domain/RelationshipOpts"; | ||
|
||
/** | ||
* | ||
* @param opts | ||
* @decorator | ||
* @objection | ||
*/ | ||
export function HasMany(opts?: RelationshipOptsWithoutThrough): PropertyDecorator { | ||
return RelatesTo(Model.HasManyRelation, opts); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {Entity, HasOne} from "@tsed/objection"; | ||
|
||
import {IdColumn} from "../../lib/esm"; | ||
import {Model} from "objection"; | ||
|
||
describe("@HasOne", () => { | ||
it("should set metadata", () => { | ||
@Entity("pet") | ||
class Pet extends Model { | ||
@IdColumn() | ||
id!: string; | ||
userId?: string; | ||
} | ||
@Entity("user") | ||
class User extends Model { | ||
@IdColumn() | ||
id!: string; | ||
@HasOne() | ||
pet?: Pet; | ||
} | ||
expect(User.relationMappings).toEqual({ | ||
pet: { | ||
relation: Model.HasOneRelation, | ||
modelClass: Pet, | ||
join: { | ||
from: "user.id", | ||
to: "pet.userId" | ||
} | ||
} | ||
}); | ||
}); | ||
it("should set custom relationship path", () => { | ||
@Entity("pet") | ||
class Pet extends Model { | ||
@IdColumn() | ||
id!: string; | ||
ownerId?: string; | ||
} | ||
@Entity("user") | ||
class User extends Model { | ||
@IdColumn() | ||
id!: string; | ||
userId!: string; | ||
@HasOne({from: "userId", to: "ownerId"}) | ||
pet?: Pet; | ||
} | ||
expect(User.relationMappings).toEqual({ | ||
pet: { | ||
relation: Model.HasOneRelation, | ||
modelClass: Pet, | ||
join: { | ||
from: "user.userId", | ||
to: "pet.ownerId" | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {Model} from "objection"; | ||
import {RelatesTo} from "./relatesTo"; | ||
import {RelationshipOptsWithoutThrough} from "../domain/RelationshipOpts"; | ||
|
||
/** | ||
* | ||
* @param opts | ||
* @decorator | ||
* @objection | ||
*/ | ||
export function HasOne(opts?: RelationshipOptsWithoutThrough): PropertyDecorator { | ||
return RelatesTo(Model.HasOneRelation, opts); | ||
} |
70 changes: 70 additions & 0 deletions
70
packages/orm/objection/src/decorators/hasOneThroughRelation.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import {Entity, HasOneThroughRelation} from "@tsed/objection"; | ||
|
||
import {IdColumn} from "../../lib/esm"; | ||
import {Model} from "objection"; | ||
|
||
describe("@HasOneThroughRelation", () => { | ||
it("should set metadata", () => { | ||
@Entity("pet") | ||
class Pet extends Model { | ||
@IdColumn() | ||
id!: string; | ||
userId?: string; | ||
} | ||
@Entity("user") | ||
class User extends Model { | ||
@IdColumn() | ||
id!: string; | ||
@HasOneThroughRelation() | ||
pet?: Pet; | ||
} | ||
expect(User.relationMappings).toEqual({ | ||
pet: { | ||
relation: Model.HasOneThroughRelation, | ||
modelClass: Pet, | ||
join: { | ||
from: "user.id", | ||
through: { | ||
from: "user_pet.userId", | ||
to: "user_pet.petId" | ||
}, | ||
to: "pet.id" | ||
} | ||
} | ||
}); | ||
}); | ||
it("should set custom relationship path", () => { | ||
@Entity("pet") | ||
class Pet extends Model { | ||
@IdColumn() | ||
id!: string; | ||
petId?: string; | ||
} | ||
@Entity("user") | ||
class User extends Model { | ||
@IdColumn() | ||
id!: string; | ||
userId!: string; | ||
@HasOneThroughRelation({ | ||
from: "userId", | ||
through: {from: "user_pet.ownerId", to: "user_pet.petId"}, | ||
to: "petId" | ||
}) | ||
pet?: Pet; | ||
} | ||
expect(User.relationMappings).toEqual({ | ||
pet: { | ||
relation: Model.HasOneThroughRelation, | ||
modelClass: Pet, | ||
join: { | ||
from: "user.userId", | ||
through: { | ||
from: "user_pet.ownerId", | ||
to: "user_pet.petId" | ||
}, | ||
to: "pet.petId" | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.