mirror of
https://github.com/penpot/penpot.git
synced 2025-05-05 09:45:54 +02:00
🎉 Add sql helpers wrappers with proper defaults.
This commit is contained in:
parent
5d67a6f427
commit
1467fd5dbf
2 changed files with 82 additions and 33 deletions
|
@ -13,6 +13,7 @@
|
||||||
[app.common.geom.point :as gpt]
|
[app.common.geom.point :as gpt]
|
||||||
[app.common.spec :as us]
|
[app.common.spec :as us]
|
||||||
[app.config :as cfg]
|
[app.config :as cfg]
|
||||||
|
[app.db.sql :as sql]
|
||||||
[app.util.json :as json]
|
[app.util.json :as json]
|
||||||
[app.util.migrations :as mg]
|
[app.util.migrations :as mg]
|
||||||
[app.util.time :as dt]
|
[app.util.time :as dt]
|
||||||
|
@ -22,10 +23,7 @@
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
[integrant.core :as ig]
|
[integrant.core :as ig]
|
||||||
[next.jdbc :as jdbc]
|
[next.jdbc :as jdbc]
|
||||||
[next.jdbc.date-time :as jdbc-dt]
|
[next.jdbc.date-time :as jdbc-dt])
|
||||||
[next.jdbc.optional :as jdbc-opt]
|
|
||||||
[next.jdbc.sql :as jdbc-sql]
|
|
||||||
[next.jdbc.sql.builder :as jdbc-bld])
|
|
||||||
(:import
|
(:import
|
||||||
com.zaxxer.hikari.HikariConfig
|
com.zaxxer.hikari.HikariConfig
|
||||||
com.zaxxer.hikari.HikariDataSource
|
com.zaxxer.hikari.HikariDataSource
|
||||||
|
@ -166,12 +164,6 @@
|
||||||
[& args]
|
[& args]
|
||||||
`(jdbc/with-transaction ~@args))
|
`(jdbc/with-transaction ~@args))
|
||||||
|
|
||||||
(defn- kebab-case [s] (str/replace s #"_" "-"))
|
|
||||||
(defn- snake-case [s] (str/replace s #"-" "_"))
|
|
||||||
(defn- as-kebab-maps
|
|
||||||
[rs opts]
|
|
||||||
(jdbc-opt/as-unqualified-modified-maps rs (assoc opts :label-fn kebab-case)))
|
|
||||||
|
|
||||||
(defn open
|
(defn open
|
||||||
[pool]
|
[pool]
|
||||||
(jdbc/get-connection pool))
|
(jdbc/get-connection pool))
|
||||||
|
@ -180,40 +172,39 @@
|
||||||
([ds sv]
|
([ds sv]
|
||||||
(exec! ds sv {}))
|
(exec! ds sv {}))
|
||||||
([ds sv opts]
|
([ds sv opts]
|
||||||
(jdbc/execute! ds sv (assoc opts :builder-fn as-kebab-maps))))
|
(jdbc/execute! ds sv (assoc opts :builder-fn sql/as-kebab-maps))))
|
||||||
|
|
||||||
(defn exec-one!
|
(defn exec-one!
|
||||||
([ds sv] (exec-one! ds sv {}))
|
([ds sv] (exec-one! ds sv {}))
|
||||||
([ds sv opts]
|
([ds sv opts]
|
||||||
(jdbc/execute-one! ds sv (assoc opts :builder-fn as-kebab-maps))))
|
(jdbc/execute-one! ds sv (assoc opts :builder-fn sql/as-kebab-maps))))
|
||||||
|
|
||||||
(def ^:private default-options
|
|
||||||
{:table-fn snake-case
|
|
||||||
:column-fn snake-case
|
|
||||||
:builder-fn as-kebab-maps})
|
|
||||||
|
|
||||||
(defn insert!
|
(defn insert!
|
||||||
[ds table params]
|
([ds table params] (insert! ds table params nil))
|
||||||
(jdbc-sql/insert! ds table params default-options))
|
([ds table params opts]
|
||||||
|
(exec-one! ds
|
||||||
|
(sql/insert table params opts)
|
||||||
|
(assoc opts :return-keys true))))
|
||||||
|
|
||||||
(defn update!
|
(defn update!
|
||||||
[ds table params where]
|
([ds table params where] (update! ds table params where nil))
|
||||||
(let [opts (assoc default-options :return-keys true)]
|
([ds table params where opts]
|
||||||
(jdbc-sql/update! ds table params where opts)))
|
(exec-one! ds
|
||||||
|
(sql/update table params where opts)
|
||||||
|
(assoc opts :return-keys true))))
|
||||||
|
|
||||||
(defn delete!
|
(defn delete!
|
||||||
[ds table params]
|
([ds table params] (delete! ds table params nil))
|
||||||
(let [opts (assoc default-options :return-keys true)]
|
([ds table params opts]
|
||||||
(jdbc-sql/delete! ds table params opts)))
|
(exec-one! ds
|
||||||
|
(sql/delete table params opts)
|
||||||
|
(assoc opts :return-keys true))))
|
||||||
|
|
||||||
(defn get-by-params
|
(defn get-by-params
|
||||||
([ds table params]
|
([ds table params]
|
||||||
(get-by-params ds table params nil))
|
(get-by-params ds table params nil))
|
||||||
([ds table params opts]
|
([ds table params opts]
|
||||||
(let [opts (cond-> (merge default-options opts)
|
(let [res (exec-one! ds (sql/select table params opts))]
|
||||||
(:for-update opts)
|
|
||||||
(assoc :suffix "for update"))
|
|
||||||
res (exec-one! ds (jdbc-bld/for-query table params opts) opts)]
|
|
||||||
(when (or (:deleted-at res) (not res))
|
(when (or (:deleted-at res) (not res))
|
||||||
(ex/raise :type :not-found
|
(ex/raise :type :not-found
|
||||||
:hint "database object not found"))
|
:hint "database object not found"))
|
||||||
|
@ -229,10 +220,7 @@
|
||||||
([ds table params]
|
([ds table params]
|
||||||
(query ds table params nil))
|
(query ds table params nil))
|
||||||
([ds table params opts]
|
([ds table params opts]
|
||||||
(let [opts (cond-> (merge default-options opts)
|
(exec! ds (sql/select table params opts))))
|
||||||
(:for-update opts)
|
|
||||||
(assoc :suffix "for update"))]
|
|
||||||
(exec! ds (jdbc-bld/for-query table params opts) opts))))
|
|
||||||
|
|
||||||
(defn pgobject?
|
(defn pgobject?
|
||||||
[v]
|
[v]
|
||||||
|
|
61
backend/src/app/db/sql.clj
Normal file
61
backend/src/app/db/sql.clj
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
;;
|
||||||
|
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||||
|
;; defined by the Mozilla Public License, v. 2.0.
|
||||||
|
;;
|
||||||
|
;; Copyright (c) 2020 UXBOX Labs SL
|
||||||
|
|
||||||
|
(ns app.db.sql
|
||||||
|
(:refer-clojure :exclude [update])
|
||||||
|
(:require
|
||||||
|
[clojure.string :as str]
|
||||||
|
[next.jdbc.optional :as jdbc-opt]
|
||||||
|
[next.jdbc.sql.builder :as sql]))
|
||||||
|
|
||||||
|
(defn kebab-case [s] (str/replace s #"_" "-"))
|
||||||
|
(defn snake-case [s] (str/replace s #"-" "_"))
|
||||||
|
|
||||||
|
(def default-opts
|
||||||
|
{:table-fn snake-case
|
||||||
|
:column-fn snake-case})
|
||||||
|
|
||||||
|
(defn as-kebab-maps
|
||||||
|
[rs opts]
|
||||||
|
(jdbc-opt/as-unqualified-modified-maps rs (assoc opts :label-fn kebab-case)))
|
||||||
|
|
||||||
|
(defn insert
|
||||||
|
([table key-map]
|
||||||
|
(insert table key-map nil))
|
||||||
|
([table key-map opts]
|
||||||
|
(let [opts (merge default-opts opts)
|
||||||
|
opts (cond-> opts
|
||||||
|
(:on-conflict-do-nothing opts)
|
||||||
|
(assoc :suffix "ON CONFLICT DO NOTHING"))]
|
||||||
|
(sql/for-insert table key-map opts))))
|
||||||
|
|
||||||
|
(defn select
|
||||||
|
([table where-params]
|
||||||
|
(select table where-params nil))
|
||||||
|
([table where-params opts]
|
||||||
|
(let [opts (merge default-opts opts)
|
||||||
|
opts (cond-> opts
|
||||||
|
(:for-update opts)
|
||||||
|
(assoc :suffix "FOR UPDATE"))]
|
||||||
|
(sql/for-query table where-params opts))))
|
||||||
|
|
||||||
|
(defn update
|
||||||
|
([table key-map where-params]
|
||||||
|
(update table key-map where-params nil))
|
||||||
|
([table key-map where-params opts]
|
||||||
|
(let [opts (merge default-opts opts)]
|
||||||
|
(sql/for-update table key-map where-params opts))))
|
||||||
|
|
||||||
|
(defn delete
|
||||||
|
([table where-params]
|
||||||
|
(delete table where-params nil))
|
||||||
|
([table where-params opts]
|
||||||
|
(let [opts (merge default-opts opts)]
|
||||||
|
(sql/for-delete table where-params opts))))
|
||||||
|
|
Loading…
Add table
Reference in a new issue