skdb icon indicating copy to clipboard operation
skdb copied to clipboard

[skip/helpers] Add `join_one()`/`join_many()` helpers.

Open beauby opened this issue 9 months ago • 1 comments

Example usage for join_one():

type User = { name: string; email: string; };
type Post = { title: string; body: string; author_id: number; };
type PostWithAuthor = { title: string; body: string; author: User; };
...
// The following turns `Post`s and `User`s into `PostWithAuthor`s.
join_one(posts, users, {
  on: "author_id",
  name: "author",
})

Example usage for join_many():

type Upvote = { post_id: number; user_id: number; };
type Post = { title: string; body: string; };
type PostWithUpvotes = {
  title: string;
  body: string;
  upvotes: { user_id: number; }[];
};
...
// The following turns `Post`s and `Upvote`s into `PostWithUpvote`s.
join_many(posts, upvotes, {
  on: "post_id",
  name: "upvotes",
})

beauby avatar Mar 02 '25 12:03 beauby

Like I said, I'm wondering whether we could make the syntax more user-friendly.

Turns out, the type constraint on EagerCollection.map, forcing the mapper's constructor parameters to be ...params: Params with Params extends DepSafe[] prevents us from using an object literal to mimic named parameters (since the object literal won't be DepSafe). I'll merge it with the initial API (constructor(other_collection, join_key, resulting_property)), as it is in helpers and not part of the core API, and we can revisit later.

beauby avatar Mar 07 '25 14:03 beauby