Created
July 18, 2024 00:00
-
-
Save revolunet/352302ed7d432afc498edb1f6a9ac1f4 to your computer and use it in GitHub Desktop.
Some kysely query with PostgreSQL CTE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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