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))) (.run stmt)))
(defn <d1-all (defn <d1-all
[^js db sql-str & args] [^js db sql-or-opts & more]
(p/let [^js stmt (.prepare db sql-str) (let [[opts sql-str args] (if (map? sql-or-opts)
^js stmt (if (seq args) [sql-or-opts (first more) (rest more)]
(.apply (.-bind stmt) stmt (to-array args)) [nil sql-or-opts more])
stmt)] session-mode (:session opts)
(.all stmt))) 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] (defn read-json [request]
(p/let [body (.text request)] (p/let [body (.text request)]

View File

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