Skip to content

Instantly share code, notes, and snippets.

View ktutnik's full-sized avatar

Ketut Sandiarsa ktutnik

View GitHub Profile
@collection()
class User {
constructor(
public email: string,
public name: string,
public birthDate: Date
) { }
}
const UserModel = model(User)
@ktutnik
ktutnik / first-class-entity.ts
Last active May 3, 2021 03:18
first class entity preview
@genericController()
@Entity()
export class Product {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
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])
}
class ProductsController extends ControllerGeneric<Product, number> { }
@genericController(c => {
c.mutators().authorize("ShopOwner", "Staff")
})
@Entity()
export class Product {
@PrimaryGeneratedColumn()
id: number;
@val.required()
@Column()
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"
})
@Entity()
export class Shop {
@PrimaryGeneratedColumn()
id: number
/** ------ other properties ------ **/
@genericController()
@OneToMany(x => Product, x => x.shop)
products:Product[]
@Entity()
export class Product {
@PrimaryGeneratedColumn()
id: number;
/** ------ other properties ------ **/
@ManyToOne(x => Category)
category:Category
}
@genericController()
@Entity()
export class Shop {
@PrimaryGeneratedColumn()
id: number
/** ------ other properties ------ **/
@authorize.readonly()
@Column()