add session api to use read replica

This commit is contained in:
Tienson Qin
2026-01-31 05:14:48 +08:00
parent 328d8e2bf3
commit ddb4db21c6
2 changed files with 17 additions and 10 deletions

View File

@@ -67,12 +67,19 @@
(.run stmt)))
(defn <d1-all
[^js db sql-str & args]
(p/let [^js stmt (.prepare db sql-str)
^js stmt (if (seq args)
(.apply (.-bind stmt) stmt (to-array args))
stmt)]
(.all stmt)))
[^js db sql-or-opts & more]
(let [[opts sql-str args] (if (map? sql-or-opts)
[sql-or-opts (first more) (rest more)]
[nil sql-or-opts more])
session-mode (:session opts)
session (if (some? session-mode)
(.withSession db session-mode)
(.withSession db))]
(p/let [^js stmt (.prepare session sql-str)
^js stmt (if (seq args)
(.apply (.-bind stmt) stmt (to-array args))
stmt)]
(.all stmt))))
(defn read-json [request]
(p/let [body (.text request)]

View File

@@ -129,7 +129,7 @@
(defn <user-id-by-email [db email]
(when (string? email)
(p/let [result (common/<d1-all db
(p/let [result (common/<d1-all db {:session "first-primary"}
"select id from users where email = ?"
email)
rows (common/get-sql-rows result)
@@ -157,7 +157,7 @@
(defn <user-rsa-key-pair
[db user-id]
(when (string? user-id)
(p/let [result (common/<d1-all db
(p/let [result (common/<d1-all db {:session "first-primary"}
"select public_key, encrypted_private_key from user_rsa_keys where user_id = ?"
user-id)
rows (common/get-sql-rows result)
@@ -169,7 +169,7 @@
(defn <user-rsa-public-key-by-email
[db email]
(when (string? email)
(p/let [result (common/<d1-all db
(p/let [result (common/<d1-all db {:session "first-primary"}
(str "select k.public_key from user_rsa_keys k "
"left join users u on k.user_id = u.id "
"where u.email = ?")
@@ -222,7 +222,7 @@
now)))
(defn <graph-members-list [db graph-id]
(p/let [result (common/<d1-all db
(p/let [result (common/<d1-all db {:session "first-primary"}
(str "select m.user_id, m.graph_id, m.role, m.invited_by, m.created_at, "
"u.email, u.username "
"from graph_members m "