-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
10 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module VahterBanBot.DB | ||
|
||
open Npgsql | ||
open VahterBanBot.Types | ||
open Dapper | ||
open VahterBanBot.Utils | ||
|
||
let private connString = getEnv "DATABASE_URL" | ||
|
||
let upsertUser (user: User) = | ||
task { | ||
use conn = new NpgsqlConnection(connString) | ||
|
||
//language=postgresql | ||
let sql = | ||
""" | ||
INSERT INTO "user" (id, username) | ||
VALUES (@id, @username) | ||
ON CONFLICT (id) DO UPDATE | ||
SET username = | ||
CASE | ||
WHEN EXCLUDED.username != "user".username THEN EXCLUDED.username | ||
ELSE "user".username | ||
END, | ||
updated_at = | ||
CASE | ||
WHEN EXCLUDED.username != "user".username THEN timezone('utc'::TEXT, NOW()) | ||
ELSE "user".updated_at | ||
END | ||
RETURNING *; | ||
""" | ||
|
||
let! insertedUser = | ||
conn.QueryAsync<User>( | ||
sql, | ||
{| id = user.Id; username = user.Username |} | ||
) | ||
|
||
return insertedUser |> Seq.head | ||
} |
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
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
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