3

I just create a Supabase DB using NestJS and TypeORM. I have some entities like this:

@Entity()
export class UserService {

    @PrimaryGeneratedColumn()
    id: number;

    @OneToOne(type => User)
    user: User;

    @OneToOne(type => Service)
    service_id: ForeignKeyMetadata;

    @Column('text')
    token: string;

    @Column({ default: () => 'CURRENT_TIMESTAMP', type: 'timestamp' })
    created: Date;

    @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP', onUpdate: 'CURRENT_TIMESTAMP' })
    updated: Date;
}

I'm trying to link user Field to User table, that is the table for user management in supabase.

I import this like:

import { Entity, Column, PrimaryGeneratedColumn, OneToOne } from 'typeorm';
import { ForeignKeyMetadata } from 'typeorm/metadata/ForeignKeyMetadata';
import { User } from '@supabase/supabase-js';
import { Service } from './service.entity';

but, User is an interface, so I can't use it in (also i'm not sure i need to import that).

In the official documentation, they llink tables like:

create table public.profiles (
  id uuid not null references auth.users on delete cascade,
  first_name text,
  last_name text,

  primary key (id)
);

alter table public.profiles enable row level security;

https://supabase.com/docs/guides/auth/managing-user-data?language=js

How can i do that, using NestJS and TypeORM ? Remember, the goal of this topic is not to link table to an other table using foreign key. The goal is to link table to Auth.user table that is directly in Supabase.

1 Answer 1

1

I recently stumbled accross the same problem and solved it by simply defining a one-to-one relation between the auth.users and my public.user table:

//user.entity.ts

@Entity({ schema: 'auth', synchronize: false }) // <- define schema and do not sync
export class Users {
  @PrimaryColumn({ type: 'uuid' })
  id: string;
}

@Entity({ schema: 'public' })
export class User { // <- custom user table
  @PrimaryColumn({ type: 'uuid' })
  id: string;

  @OneToOne(() => Users, (user) => user.id, { nullable: false })
  @JoinColumn()
  authUser: Users;

  @Column()
  name: string;

  //other fields

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.