refactor(db): drop Querier and select only user id for session lookup

This commit is contained in:
kk10
2026-10-04 23:08:11 +03:00
parent 527c1748f8
commit 5d6f5a397f
4 changed files with 13 additions and 58 deletions
+4 -5
View File
@@ -9,11 +9,10 @@ INSERT INTO sessions (
sqlc.arg(expires_at)
);
-- name: GetSessionUser :one
SELECT u.id, u.username, u.display_name, u.created_at FROM sessions s
JOIN users u ON u.id = s.user_id
WHERE s.token_hash = sqlc.arg(token_hash)
AND s.expires_at > now();
-- name: GetSessionUserID :one
SELECT user_id FROM sessions
WHERE token_hash = sqlc.arg(token_hash)
AND expires_at > now();
-- name: DeleteSession :exec
DELETE FROM sessions
-30
View File
@@ -1,30 +0,0 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
package db
import (
"context"
"github.com/google/uuid"
)
type Querier interface {
AddChatMembers(ctx context.Context, arg AddChatMembersParams) error
CreateChat(ctx context.Context, arg CreateChatParams) (Chat, error)
CreateMessage(ctx context.Context, arg CreateMessageParams) (Message, error)
CreateSession(ctx context.Context, arg CreateSessionParams) error
CreateUser(ctx context.Context, arg CreateUserParams) (CreateUserRow, error)
DeleteExpiredSessions(ctx context.Context) (int64, error)
DeleteSession(ctx context.Context, tokenHash string) error
GetChat(ctx context.Context, arg GetChatParams) (GetChatRow, error)
GetSessionUser(ctx context.Context, tokenHash string) (GetSessionUserRow, error)
GetUser(ctx context.Context, id uuid.UUID) (GetUserRow, error)
GetUserByUsername(ctx context.Context, username string) (User, error)
IsChatMember(ctx context.Context, arg IsChatMemberParams) (bool, error)
ListChats(ctx context.Context, arg ListChatsParams) ([]ListChatsRow, error)
ListMessages(ctx context.Context, arg ListMessagesParams) ([]Message, error)
}
var _ Querier = (*Queries)(nil)
+9 -22
View File
@@ -58,28 +58,15 @@ func (q *Queries) DeleteSession(ctx context.Context, tokenHash string) error {
return err
}
const getSessionUser = `-- name: GetSessionUser :one
SELECT u.id, u.username, u.display_name, u.created_at FROM sessions s
JOIN users u ON u.id = s.user_id
WHERE s.token_hash = $1
AND s.expires_at > now()
const getSessionUserID = `-- name: GetSessionUserID :one
SELECT user_id FROM sessions
WHERE token_hash = $1
AND expires_at > now()
`
type GetSessionUserRow struct {
ID uuid.UUID
Username string
DisplayName string
CreatedAt time.Time
}
func (q *Queries) GetSessionUser(ctx context.Context, tokenHash string) (GetSessionUserRow, error) {
row := q.db.QueryRow(ctx, getSessionUser, tokenHash)
var i GetSessionUserRow
err := row.Scan(
&i.ID,
&i.Username,
&i.DisplayName,
&i.CreatedAt,
)
return i, err
func (q *Queries) GetSessionUserID(ctx context.Context, tokenHash string) (uuid.UUID, error) {
row := q.db.QueryRow(ctx, getSessionUserID, tokenHash)
var user_id uuid.UUID
err := row.Scan(&user_id)
return user_id, err
}
-1
View File
@@ -8,7 +8,6 @@ sql:
package: "db"
out: "internal/db"
sql_package: "pgx/v5"
emit_interface: true
emit_empty_slices: true
emit_pointers_for_null_types: true
overrides: