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;
}
Related
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?
I'm new to typeORM and facing this issue while adding OnetoOne Relationship
The code goes like this-
User
import { Entity, Column, PrimaryGeneratedColumn, OneToOne, JoinColumn } from 'typeorm';
import { Role } from '../role/role.entity';
#Entity()
export class User {
#Column()
lastLoggedInIP: string;
#Column()
lastLoggedInTimeStamp: string;
#Column()
createdAt: Date;
#Column()
updatedAt: Date;
#Column({ default: true })
isActive: boolean;
#OneToOne(() => Role,{primary:true,cascade:true})
#JoinColumn({ name: 'id',referencedColumnName:"user_id" })
id: Role
}
Role -
import { Entity, Column, PrimaryGeneratedColumn, VersionColumn,OneToOne,JoinColumn, Unique } from 'typeorm';
import { RoleType } from 'src/roleType/roleType.entity';
import { User } from 'src/user/user.entity';
#Entity()
export class Role {
#PrimaryGeneratedColumn('increment')
id: number;
#VersionColumn({unique:true})
user_id: number;
#OneToOne(() => RoleType,{ cascade: true })
#JoinColumn({name:"role_type_id"})
roleType: RoleType;
#Column()
createdAt: Date;
#Column()
updatedAt: Date;
}
I want to access another unique column to reference my table but it is not working due to the above error. It is by default accessing the primary table key
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();
}
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 ...
}
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?