This file contains hidden or 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
| @collection() | |
| class User { | |
| constructor( | |
| public email: string, | |
| public name: string, | |
| public birthDate: Date | |
| ) { } | |
| } |
This file contains hidden or 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
| const UserModel = model(User) |
This file contains hidden or 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
| @genericController() | |
| @Entity() | |
| export class Product { | |
| @PrimaryGeneratedColumn() | |
| id: number; | |
| @Column() | |
| name: string; | |
| @Column() |
This file contains hidden or 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
| class ControllerGeneric<T, TID> { | |
| readonly private repo: Repository<T> | |
| constructor() { | |
| // get implementation generic arguments | |
| const args = generic.getArguments(this.constructor) | |
| // args[0] is type of T | |
| this.repo = new Repository(args[0]) | |
| } |
This file contains hidden or 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
| class ProductsController extends ControllerGeneric<Product, number> { } |
This file contains hidden or 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
| @genericController(c => { | |
| c.mutators().authorize("ShopOwner", "Staff") | |
| }) | |
| @Entity() | |
| export class Product { | |
| @PrimaryGeneratedColumn() | |
| id: number; | |
| @val.required() | |
| @Column() |
This file contains hidden or 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
| authPolicy() | |
| .register("ShopOwner", ctx => { | |
| // ctx.user is claims of JWT token | |
| // from Authorization request header | |
| return ctx.user?.role === "ShopOwner" | |
| }) | |
| // chain to register another policy | |
| .register("Staff", ctx => { | |
| return ctx.user?.role === "Staff" | |
| }) |
This file contains hidden or 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
| @Entity() | |
| export class Shop { | |
| @PrimaryGeneratedColumn() | |
| id: number | |
| /** ------ other properties ------ **/ | |
| @genericController() | |
| @OneToMany(x => Product, x => x.shop) | |
| products:Product[] |
This file contains hidden or 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
| @Entity() | |
| export class Product { | |
| @PrimaryGeneratedColumn() | |
| id: number; | |
| /** ------ other properties ------ **/ | |
| @ManyToOne(x => Category) | |
| category:Category | |
| } |
This file contains hidden or 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
| @genericController() | |
| @Entity() | |
| export class Shop { | |
| @PrimaryGeneratedColumn() | |
| id: number | |
| /** ------ other properties ------ **/ | |
| @authorize.readonly() | |
| @Column() |