agiletiger / ojotas

The database-first ORM
MIT License
7 stars 4 forks source link

improve typing of hasMany relations in generateTypeDefinitionFromSql method #11

Closed nicoabie closed 8 months ago

nicoabie commented 10 months ago
export type NonEmptyArray<T> = [T, ...T[]];

export type PossiblyEmptyArray<T> = T[];

we can use NonEmptyArray when uses in doing an inner join

ie:

select u.name, p.title, p.content from users u inner join posts p on u.id = p.user_id

would be typed as

 {
  name: string;
  posts: NonEmptyArray<{
    title: string;
    content: string;
  }>;
}

and we can use PossiblyEmptyArray when uses in doing an left join

ie:

select u.name, p.title, p.content from users u left join posts p on u.id = p.user_id

would be typed as

{
  name: string;
  posts: PossiblyEmptyArray<{
    title: string;
    content: string;
  }>;
}
nicoabie commented 8 months ago

implemented in #42