Skip to content

Instantly share code, notes, and snippets.

@revolunet
Created July 18, 2024 00:00
Show Gist options
  • Save revolunet/352302ed7d432afc498edb1f6a9ac1f4 to your computer and use it in GitHub Desktop.
Save revolunet/352302ed7d432afc498edb1f6a9ac1f4 to your computer and use it in GitHub Desktop.
Some kysely query with PostgreSQL CTE
await db
.with("expiration_date", (db) =>
db
.selectFrom("missions")
.select(({ fn }) => ["user_id", fn.max("end").as("expiration")])
.groupBy("user_id")
)
.selectFrom(["users", "expiration_date"])
.select([
"users.username",
"expiration_date.expiration",
sql<boolean>`expiration_date.expiration < NOW()`.as("expired"),
])
.where((eb) => eb("expiration_date.user_id", "=", eb.ref("users.uuid")))
.execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment