KyselyK
Kysely15mo ago
8 replies
Nicolas Bourdin

select expression helper

I got this selectExpression that i use in many file

.select([
'e.id',
eventStartDate('e'),
eventEndDate('e'),
'e.title',
'e.type',
'e.status',
'e.banner',
'e.event_end_date as end_date',
'e.location',
'e.duration_in_minute',
'e.participant_count_limit',
'e.registration_date_limit',
'e.provider',
coordinates('e'),
sql<boolean>is_full_of_participants(e).as('isFullOfParticipants'),
sql<string[]>get_event_sports(e.id).as('sports'),
])

how can i create a helper function in order to reuse it ?
Solution
Hey 👋

Try this - https://kyse.link/QI1Q6

await db
  .selectFrom(["event as e", "event as c"])
  .select(projectEvent())
  .execute();

function projectEvent() {
  const eb = expressionBuilder<{ e: Event }, "e">();

  return [
    "e.id",
    "e.title",
    "e.type",
    "e.status",
    "e.banner",
    "e.event_end_date as end_date",
    "e.location",
    "e.duration_in_minute",
    "e.participant_count_limit",
    "e.registration_date_limit",
    "e.provider",
    sql<boolean>`is_full_of_participants(${eb.table("e")})`.as(
      "isFullOfParticipants",
    ),
    sql<string[]>`get_event_sports(${eb.ref("e.id")})`.as("sports"),
  ] satisfies SelectExpression<{ e: Event }, "e">[];
}
Was this page helpful?