#Typescript integration with database entities

6 messages · Page 1 of 1 (latest)

tawny garden
#

i have this entity file:

@Entity('post-comments')
export class PostComment extends Base {
  @Index()
  @Column('uuid')
  commented_by_id: string;

  @Column('varchar', { length: postCommentContentMaxLength })
  content: string;

  @Index()
  @Column('uuid')
  post_id: string;

  @Index()
  @Column('uuid', { nullable: true, default: null })
  parent_id: NullableValue<string>;

  @ManyToOne(() => User, (user) => user.posts)
  @JoinColumn({ name: 'commented_by_id' })
  commented_by: User;

  @ManyToOne(() => Post, (post) => post)
  @JoinColumn({ name: 'post_id' })
  post: Post;
}

export const alias = 'post-comment';
export const commented_by_alias = 'commented_by';

export type PostCommentSelectKey =
  | `${typeof alias}.${keyof PostComment}`
  | `${typeof commented_by_alias}.${keyof User}`;

export const base_select_fields = [
  'post-comment.id',
  'post-comment.content',
  'post-comment.created_at',
  'post-comment.updated_at',
  'post-comment.parent_id',
  'post-comment.post_id',
  'commented_by.id',
  'commented_by.user_photo_url',
  'commented_by.user_name',
] satisfies PostCommentSelectKey[];

And i want to create a type that does this:

const test: EntityFromAliasArray<'post-comment', PostComment, typeof base_select_fields> = {
    id: string;
    content: string
    created_at: string
    updated_at?: string
    parent_id: string | null;
    commented_by: {
        id: string;
        user_photo_url: string | null;
        user_name: string
    }
}

Is that possible ?

celest raft
#

Is that possible ?
no idea what you are trying to do

#

also no what library the decorators you are using come from. typeorm? sequelize? mikroorm? something else?

#

on a sidenote, do you know about drizzle or kysely? those aren't "classical" orm's, but do offer type-safety using the TS type-system
seems related to what you are trying to achieve

#

@tawny garden

tawny garden
#

i created the array is to perform partials selects in the database and its based on the columns of the entity and sub-entities i want a way that takes that array and returns how a json response would look like for internal usage in methods and functions