Skip to content

Instantly share code, notes, and snippets.

@ktutnik
Last active June 29, 2021 04:43
Show Gist options
  • Save ktutnik/cdea1cd17645b6527b16435d2a7ee656 to your computer and use it in GitHub Desktop.
Save ktutnik/cdea1cd17645b6527b16435d2a7ee656 to your computer and use it in GitHub Desktop.
@genericController(c => {
// Only Shop Owner can modify and delete the shop data
c.methods("Delete", "Patch", "Put").authorize("ShopOwner")
})
@Entity()
export class Shop extends EntityBase {
@val.required()
@Column()
name: string
// restrict anyone to read/write users relation
@authorize.none()
@OneToMany(() => User, x => x.shop)
users: User[]
// this method executed before entity saved to database
@preSave("post")
async setShopOwner(@bind.user() user: JwtClaims) {
// User who created the shop assigned as the Shop Owner by default
const owner = await getRepository(ShopUser)
.save({ user: { id: user.userId }, role: "ShopOwner" })
this.users = [owner]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment