Skip to content

Commit

Permalink
feat: add relation
Browse files Browse the repository at this point in the history
Signed-off-by: Anshuman Chhapolia <[email protected]>
  • Loading branch information
achhapolia10 committed Mar 23, 2024
1 parent 2658af7 commit 45037fd
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
}
},
"dependencies": {
"@smoke-trees/postgres-backend": "^2.3.1",
"@smoke-trees/postgres-backend": "^2.3.3",
"@smoke-trees/smoke-context": "^1.4.8",
"chai-http": "^4.4.0",
"compression": "^1.7.4",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 2 additions & 10 deletions src/app/address/Address.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,14 @@ import {
Controller,
ServiceController,
} from "@smoke-trees/postgres-backend";
import { Address } from "./Address.entity";
import { RequestHandler } from "express";
import { ParamsDictionary } from "express-serve-static-core";
import { ParsedQs } from "qs";
import { Address } from "./Address.entity";
import { AddressService } from "./Address.service";

export class AddressController extends ServiceController<Address> {
path: string = "/address";
protected controllers: Controller[] = [];
protected mw: RequestHandler<
ParamsDictionary,
any,
any,
ParsedQs,
Record<string, any>
>[] = [];
protected mw: RequestHandler[] = [];

service: AddressService;

Expand Down
37 changes: 36 additions & 1 deletion src/app/address/Address.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { BaseEntity, Documentation } from "@smoke-trees/postgres-backend";
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
import {
Column,
Entity,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
} from "typeorm";
import { User } from "../users";

export interface IAddress {
userId: number;
line1: string;
line2: string;
city: string;
state: string;
}

@Entity({ name: "address_demo" })
@Documentation.addSchema({ type: "object" })
Expand All @@ -8,6 +23,10 @@ export class Address extends BaseEntity {
@Documentation.addField({ type: "number" })
id!: number;

@Column({ name: "user_id", type: "int", nullable: true })
@Documentation.addField({ type: "number" })
userId!: number;

@Column({ name: "line1", type: "varchar" })
@Documentation.addField({ type: "string" })
line1!: string;
Expand All @@ -23,4 +42,20 @@ export class Address extends BaseEntity {
@Column({ name: "state", type: "varchar" })
@Documentation.addField({ type: "string" })
state!: string;

@ManyToOne(() => User, { eager: false, onDelete: "CASCADE" })
@JoinColumn({ name: "user_id" })
user?: User;

constructor(address?: IAddress) {
super(address);

if (address) {
this.userId = address.userId;
this.line1 = address.line1;
this.line2 = address.line2;
this.city = address.city;
this.state = address.state;
}
}
}
17 changes: 16 additions & 1 deletion src/app/address/Address.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Service } from "@smoke-trees/postgres-backend";
import { Result, Service, WithCount } from "@smoke-trees/postgres-backend";
import { Address } from "./Address.entity";
import { AddressDao } from "./Address.dao";
import { QueryOption } from "@smoke-trees/postgres-backend/dist/core/Dao";

export class AddressService extends Service<Address> {
dao: AddressDao;
Expand All @@ -9,4 +10,18 @@ export class AddressService extends Service<Address> {
super(dao);
this.dao = dao;
}

readMany(
options: QueryOption<Address> = {}
): Promise<WithCount<Result<Address[]>>> {
console.log(options);
options = {
...options,
dbOptions: {
...options.dbOptions,
relations: ["user"],
},
};
return super.readMany(options);
}
}
2 changes: 1 addition & 1 deletion src/app/users/baseUserEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Documentation,
Validator,
} from "@smoke-trees/postgres-backend";
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
import { Column } from "typeorm";
import { IUser } from "./IUser";

// @Entity({ name: 'user_test_table' })
Expand Down

0 comments on commit 45037fd

Please sign in to comment.