-
-
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 7, 2022
1 parent
e907869
commit 926a7e7
Showing
19 changed files
with
623 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
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
import {BelongsToOne, Entity, IdColumn} 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,57 @@ | ||
import {Entity, HasMany, IdColumn} from "@tsed/objection"; | ||
|
||
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(Pet) | ||
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(Pet, {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,14 @@ | ||
import {Model, ModelClassSpecifier} from "objection"; | ||
|
||
import {RelatesTo} from "./relatesTo"; | ||
import {RelationshipOptsWithThrough} from "../domain/RelationshipOpts"; | ||
|
||
/** | ||
* | ||
* @param opts | ||
* @decorator | ||
* @objection | ||
*/ | ||
export function HasMany(type: ModelClassSpecifier, opts?: RelationshipOptsWithThrough): PropertyDecorator { | ||
return RelatesTo(Model.HasManyRelation, {...opts, type}); | ||
} |
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,57 @@ | ||
import {Entity, HasOne, IdColumn} from "@tsed/objection"; | ||
|
||
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); | ||
} |
Oops, something went wrong.