How to Dynamicaly Calculate Feilds Nest Js GraphQL - node.js

I have nestjs setup with graphql and postgres.I want to calculate age of the employees based on their dob on the fly. my entity looks like this. is there any way to do this using graphql if not what are the alternative approaches. I don't have much experience in graphql.
#ObjectType()
#Entity()
export class Employee {
#Field()
#PrimaryGeneratedColumn()
id: number;
#Field()
#Column({ unique: true })
email: string;
#Field()
#Column()
fname: string;
#Field()
#Column()
lname: string;
#Field()
#Column()
dob: string;
}
Resolver
#Query(() => [Empolyee], { name: 'empolyees' })
findAll() {
return this.empolyeeService.findAll();
}

Related

Retriving data from postgres using typeorm

I am trying to fetch data using Typeform in my NestJs project.
I am trying to join three tables.
User table
Roles and user forginkey table (connect between tables 1 & 3)
Roles table
My user table entity
#Entity()
export class Users {
#PrimaryGeneratedColumn()
id: number;
#Column()
email: string;
#Column()
passwordHash: string;
#Column()
firstName: string;
#Column()
lastName: string;
#Column()
registerTimestamp: Date;
#Column({ default: false })
isActive: boolean;
#OneToOne(() => UsersRole)
#JoinColumn({ name: 'id' })
users_role: UsersRole;
}
My Users_Roles code
#Entity()
export class UsersRole {
#PrimaryColumn()
user_id: number;
#PrimaryColumn()
role_id: number;
#OneToOne(() => UsersRole)
#JoinColumn({ name: 'role_id' })
roles: UsersRole;
}
My Roles table code
#Entity()
export class UsersRole {
#PrimaryColumn()
id: number;
#PrimaryColumn()
role: string;
}
How do I join between them three tables using Typeform?

How to self-reference in TypeOrm

I am trying to create a user entity which is supposed to have
import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm';
#Entity()
export class User {
#PrimaryGeneratedColumn("uuid")
id: string;
#Column({unique: true})
username: string;
#Column()
password: string;
#CreateDateColumn()
create_at: Date;
#UpdateDateColumn()
last_update_at: Date;
//this is where I need help
created_by: (this should be the ID of itself)
}
I am supposed to have the created by column as a self-referencing foreign key, but I can't figure it out. It keeps being either null or not showing at all when I tried. Complete database newbie here. I don't understand the relation doc quite well yet.
I suppose it should be like this:
#Entity()
export class User {
#PrimaryGeneratedColumn("uuid")
id: string;
#Column({unique: true})
username: string;
#Column()
password: string;
#CreateDateColumn()
create_at: Date;
#UpdateDateColumn()
last_update_at: Date;
#OneToOne(() => User)
#JoinColumn()
createdBy: User;
}

How to declare an entity contain another entity in typeorm?

I want to declare an entity contain another entity in typeorm, so I can re-use base entity or focus on certain columns, is this possible? like this:
export class Base {//can be re-use
#PrimaryGeneratedColumn()
id: number;
#CreateDateColumn()
created_at: Date;
#UpdateDateColumn()
updated_at: Date;
}
#Entity('person')//for insert/update/select
#Index(['first_name', 'last_name'], { unique: true })
export class PersonData {
#Column()
first_name: string;
#Column()
last_name: string;
}
#Entity('person')//for create table
export class Person {
Base
PersonData
}
TypeORM permits you to extend entity classes.
You can declare your classes as follows:
export class Base {//can be re-use
#PrimaryGeneratedColumn()
id: number;
#CreateDateColumn()
created_at: Date;
#UpdateDateColumn()
updated_at: Date;
}
#Entity('person')//for insert/update/select
#Index(['first_name', 'last_name'], { unique: true })
export class Person extends Base {
#Column()
first_name: string;
#Column()
last_name: string;
}
And re-use the Base class elsewhere:
#Entity('someotherentity')
export class SomeOtherEntity extends Base {
// extra columns here ...
}

TypeORM is duplicating my post when I run the command | CQRS

I'm using CQRS and TypeORM in nestJs for create an user, orders and products application.
I have these entities in typeorm and when I run the command for create an user, I get an error that email is duplicated, when check the Database found out that there is 2 users with different id but one of them is empty. I don't know what's happening.
UserEntity:
#Entity()
export class UserEntity {
#PrimaryGeneratedColumn('uuid')
id!: string;
#Column({unique: true})
email!: string;
#Column()
password!: string;
#Column()
name!: string;
#Column()
role!: string;
#OneToMany(type => OrderEntity, order => order.userId)
orders?: OrderEntity[] | any;
#OneToMany(type => ItemEntity, item => item.userId)
items?: ItemEntity[] | any;
}
OrderEntity:
#Entity()
export class OrderEntity {
#PrimaryGeneratedColumn('uuid')
id!: string;
#Column()
buyerName!: string;
#Column()
buyerPhone!: number;
#Column()
buyerEmail!: string;
#Column()
shippingMethod!: string;
#Column()
shippingAddress!: string;
#Column()
shippingCity!: string;
#Column()
shippingRegion!: string;
#Column()
shippingCountry!: string;
#Column({ type: 'datetime'})
creationDate!: Date;
#Column({ type: 'datetime'})
pack_promise_min!: Date;
#Column({ type: 'datetime'})
pack_promise_max!: Date;
#Column({ type: 'datetime'})
ship_promise_min!: Date;
#Column({ type: 'datetime'})
ship_promise_max!: Date;
#Column({ type: 'datetime'})
delivery_promise_min!: Date;
#Column({ type: 'datetime'})
delivery_promise_max!: Date;
#Column({ type: 'datetime'})
ready_pickup_promise_min!: Date;
#Column({ type: 'datetime'})
ready_pickup_promise_max!: Date;
#Column()
status!: string;
#ManyToOne(type => UserEntity, user => user.orders)
userId: UserEntity | any;
#ManyToMany(type => ItemEntity, item => item.id, { cascade: true })
#JoinTable({ name: 'orders_items'})
items: ItemEntity[];
}
ItemEntity:
#Entity()
export class ItemEntity {
#PrimaryGeneratedColumn('uuid')
id!: string;
#Column()
name!: string;
#Column()
qty!: string;
#Column()
weight!: number;
#ManyToOne(type => UserEntity, user => user.items)
userId!: UserEntity | any;
#ManyToMany((type => OrderEntity), order => order.id, {cascade: true})
orders?: OrderEntity[]
}
All this is happening since I declared the relations, I think the problem is there.
This happened in the database:
the answer it's too stupid like me xD. I was making a console.log on the controller, just for see some of information, and the command execute itself two times. Sorry, my mistake.

typeorm shows me this error: TypeError: Object prototype may only be an Object or null: undefined

I am new at typeorm and I created a user entity like this:
User.ts:
import {DefaultBaseEntity} from "../defaults/DefaultBaseEntity";
import {Entity, Column} from "typeorm";
#Entity("users")
export class User extends DefaultBaseEntity {
#Column()
username: string;
#Column()
email: string;
#Column()
password: string;
#Column()
account_type: string;
#Column()
photo_url: string;
#Column()
discipline: string;
#Column()
class: string;
#Column()
confirmed: number;
}
and this is DefaultBaseEntity.ts
import { BaseEntity, PrimaryGeneratedColumn, Column } from "typeorm";
export declare class DefaultBaseEntity extends BaseEntity {
#PrimaryGeneratedColumn()
id: string;
#Column()
createdAt: Date;
#Column()
updatedAt: Date;
constructor();
}
when I run the app it returns me this error: TypeError: Object prototype may only be an Object or null: undefined for running the app I use ts-node. What I missed?

Resources