-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move mutation to separate namespace, update docs
- Loading branch information
1 parent
4357a42
commit 2d37b06
Showing
12 changed files
with
203 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
(ns playground | ||
(:require [honeyeql.core :as heql] | ||
[honeyeql.mutation :as hm] | ||
[honeyeql.db :as heql-db] | ||
[portal.api :as p] | ||
[edn-query-language.core :as eql] | ||
[clojure.spec.alpha :as s] | ||
[next.jdbc :as jdbc])) | ||
|
||
(def logs (atom {})) | ||
|
||
(defn- logger [x] | ||
(swap! logs merge x)) | ||
|
||
(defn- reset-logger [] | ||
(reset! logs {}) | ||
(remove-tap logger) | ||
(add-tap logger)) | ||
|
||
(defn- setup-portal [x] | ||
(add-tap #'p/submit) | ||
(tap> x) | ||
(p/open {:launcher :vs-code})) | ||
|
||
(comment | ||
(reset-logger) | ||
|
||
(def pg-adapter (heql-db/initialize {:dbtype "postgres" | ||
:dbname "honeyeql" | ||
:user "postgres" | ||
:password "postgres"})) | ||
|
||
(def mysql-adapter (heql-db/initialize {:dbtype "mysql" | ||
:dbname "honeyeql" | ||
:user "root" | ||
:password "mysql123"})) | ||
|
||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Mutation | ||
All the mutation operations are available under the `honeyeql.mutation` namespace | ||
|
||
```clojure | ||
(require '[honeyeql.mutation :as hm]) | ||
``` | ||
|
||
For the given `employee` schema, | ||
|
||
![](./img/employee_self_ref_er_diagram.png) | ||
|
||
we can perform the following database operations using the HoneyEQL's mutation support as described. | ||
|
||
## Insert | ||
|
||
```clojure | ||
;; inserting first employee | ||
(hm/insert! db-adapter | ||
#:employee{:first-name "Brown" | ||
:last-name "Tara"}) | ||
;; the above expression returns (for postgres) | ||
#:employee{:id 1, :first-name "Brown", :last-name "Tara", :employee-reports-to-id nil} | ||
|
||
;; inserting second employee | ||
(hm/insert! db-adapter | ||
#:employee{:first-name "Lauryn" | ||
:last-name "Mario" | ||
:employee-reports-to-id 1}) | ||
;; it returns (for postgres) | ||
#:employee{:id 2, :first-name "Lauryn", :last-name "Mario", :employee-reports-to-id 1} | ||
``` | ||
|
||
## Insert Multiple | ||
|
||
```clojure | ||
(hm/insert-multi! db-adapter | ||
[#:employee{:first-name "Harmon" | ||
:last-name "Bernie" | ||
:employee-reports-to-id 1} | ||
#:employee{:first-name "Harold" | ||
:last-name "Ambrose" | ||
:employee-reports-to-id 2} | ||
#:employee{:first-name "Bryce" | ||
:last-name "Hoeger" | ||
:employee-reports-to-id 2}]) | ||
|
||
;; it returns (for postgres) | ||
(#:employee{:id 3, :first-name "Harmon", :last-name "Bernie", :employee-reports-to-id 1} | ||
#:employee{:id 4, :first-name "Harold", :last-name "Ambrose", :employee-reports-to-id 2} | ||
#:employee{:id 5, :first-name "Bryce", :last-name "Hoeger", :employee-reports-to-id 2}) | ||
|
||
``` | ||
|
||
## Update | ||
|
||
```clojure | ||
;; (update! db-adapter <data-to-update> <where-condition>) | ||
(hm/update! db-adapter | ||
#:employee{:first-name "Margaret"} | ||
#:employee{:id 1}) | ||
;; returns | ||
#:next.jdbc{:update-count 1} | ||
|
||
;; where-condition can have multiple attributes and it supports only the AND condition | ||
; UPDATE employee SET last_name = 'Adam' WHERE id = 1 AND first_name | ||
(hm/update! db-adapter | ||
#:employee{:last-name "Adam"} | ||
#:employee{:last-name "Tara" :first-name "Margaret"}) | ||
;; returns | ||
#:next.jdbc{:update-count 1} | ||
``` | ||
|
||
## Delete | ||
|
||
```clojure | ||
;; (delete! db-adapter <where-condition>) | ||
(hm/delete! db-adapter | ||
#:employee {:id 9}) | ||
;; like the where-condition in the update! function, it can have multiple attributes and it also supports only the AND condition | ||
``` | ||
|
||
## Transaction Support | ||
|
||
To perform Database transactipn we can make use of the `next.jdbc`'s `with-transaction` [macro](https://cljdoc.org/d/com.github.seancorfield/next.jdbc/1.3.883/doc/getting-started/transactions) along with two functions, `db-spec` and `use-tx` from the `honeyeql.core` namespace. | ||
|
||
```clojure | ||
(jdbc/with-transaction [tx (heql/db-spec db-adapter)] | ||
(let [tx-aware-db-adapter (heql/use-tx db-adapter tx)] | ||
(hm/delete! tx-aware-db-adapter {:employee/id 10}) | ||
(hm/delete! tx-aware-db-adapter {:employee/id 11}) | ||
(hm/delete! tx-aware-db-adapter {:employee/id 12}))) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.github.tamizhvendan</groupId> | ||
<artifactId>honeyeql</artifactId> | ||
<version>0.1.0-alpha51</version> | ||
<version>0.1.0-alpha52</version> | ||
|
||
<name>Honey EQL</name> | ||
<description>HoneyEQL enables you to query database using the EQL.</description> | ||
|
@@ -21,7 +21,7 @@ | |
<url>https://github.com/tamizhvendan/honeyeql</url> | ||
<connection>scm:git:git://github.com/tamizhvendan/honeyeql.git</connection> | ||
<developerConnection>scm:git:ssh://[email protected]/tamizhvendan/honeyeql.git</developerConnection> | ||
<tag>0.1.0-alpha51</tag> | ||
<tag>0.1.0-alpha52</tag> | ||
</scm> | ||
|
||
<dependencies> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
(ns honeyeql.mutation | ||
(:require [next.jdbc.result-set :as rs] | ||
[next.jdbc.sql :as sql] | ||
[clojure.string :as str] | ||
[honeyeql.db-adapter.core :as db] | ||
[honeyeql.meta-data :as heql-md])) | ||
|
||
(defn- entity-name [entity] | ||
(-> entity keys first namespace)) | ||
|
||
(defn- table-name [entity] | ||
(-> (entity-name entity) (str/replace #"-" "_") keyword)) | ||
|
||
(defn- sqlize-entity [db-adapter entity] | ||
(into {} | ||
(map (fn [[k v]] | ||
[(-> (name k) (str/replace #"-" "_") keyword) | ||
(heql-md/coerce-attr-value db-adapter :to-db k v)]) | ||
entity))) | ||
|
||
(defn- namespacify-attributes [entity-name entity] | ||
(update-keys entity #(keyword entity-name (name %)))) | ||
|
||
(defn insert! [db-adapter entity] | ||
(namespacify-attributes | ||
(entity-name entity) | ||
(sql/insert! (:db-spec db-adapter) | ||
(table-name entity) | ||
(sqlize-entity db-adapter entity) | ||
{:column-fn (db/table-fn db-adapter) | ||
:table-fn (db/table-fn db-adapter) | ||
:builder-fn rs/as-kebab-maps}))) | ||
|
||
(defn insert-multi! [db-adapter entities] | ||
(when (seq entities) | ||
(let [entities (map #(into (sorted-map) %) entities) | ||
first-entity (first entities) | ||
first-entity-name (entity-name first-entity) | ||
sqlized-entities (map (partial sqlize-entity db-adapter) entities)] | ||
(map | ||
#(namespacify-attributes first-entity-name %) | ||
(sql/insert-multi! (:db-spec db-adapter) | ||
(table-name first-entity) | ||
(keys (first sqlized-entities)) | ||
(map vals sqlized-entities) | ||
{:column-fn (db/table-fn db-adapter) | ||
:table-fn (db/table-fn db-adapter) | ||
:builder-fn rs/as-kebab-maps}))))) | ||
|
||
(defn update! [db-adapter update-params where-params] | ||
(sql/update! (:db-spec db-adapter) (table-name update-params) | ||
(sqlize-entity db-adapter update-params) (sqlize-entity db-adapter where-params) | ||
{:column-fn (db/table-fn db-adapter) | ||
:table-fn (db/table-fn db-adapter)})) | ||
|
||
(defn delete! [db-adapter where-params] | ||
(sql/delete! (:db-spec db-adapter) (table-name where-params) | ||
(sqlize-entity db-adapter where-params) | ||
{:column-fn (db/table-fn db-adapter) | ||
:table-fn (db/table-fn db-adapter)})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.