tijl.dev-core/modules/database/sessions.sql
tijl 2bbf24f581
Some checks failed
build / build (push) Failing after 0s
database + auth
2024-08-21 17:54:36 +02:00

40 lines
1.0 KiB
SQL

-- name: GetSession :one
SELECT * FROM sessions WHERE token = $1;
-- name: GetSessions :many
SELECT * FROM sessions WHERE uid = $1 ORDER BY $2;
-- name: GetActiveSessions :many
SELECT * FROM sessions WHERE uid = $1 AND (expires > CURRENT_TIMESTAMP OR expires IS NULL) ORDER BY $2;
-- name: CreateSession :exec
INSERT INTO sessions (uid, token, last_activity) VALUES ($1, $2, CURRENT_TIMESTAMP);
-- name: QuickUpdateSession :one
WITH updated_session AS (
UPDATE sessions
SET last_activity = CURRENT_TIMESTAMP
WHERE token = $1
RETURNING id, uid
),
inserted_or_updated AS (
INSERT INTO session_ips (session_id, ip_address, agent, access_time)
VALUES (
(SELECT id FROM updated_session),
$2,
$3,
CURRENT_TIMESTAMP
)
ON CONFLICT (session_id, ip_address)
DO UPDATE SET
agent = EXCLUDED.agent,
access_time = CURRENT_TIMESTAMP
RETURNING session_id
)
SELECT uid
FROM updated_session;
-- name: ExpireSession :exec
UPDATE sessions SET expires = 1 WHERE token = $1;