Skip to content

Commit

Permalink
Rename db/find to db/find-by. Change db/find to where with pk
Browse files Browse the repository at this point in the history
  • Loading branch information
swlkr committed Feb 18, 2020
1 parent 83ea1f3 commit 2ffdacb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
14 changes: 13 additions & 1 deletion docs/database-queries.md
Expand Up @@ -7,7 +7,7 @@ Database queries in joy are very basic, they go a little something like this:
Joy uses the `.env` file in your project dir (the one with the `project.janet` file in it) or your actual os environment variables and looks for `DATABASE_URL` or in joy `(env :database-url)` for the connection string.

```clojure
(import joy/db)
(import joy)

(db/connect)
```
Expand Down Expand Up @@ -71,6 +71,18 @@ A more generic query
(db/from :account :where {:email "email@example.com"} :order "created_at desc" :limit 10)
```

Find row by primary key

```clojure
(db/find :account 1)
```

Find first row by query

```clojure
(db/first :account :where {:email "email@example.com"})
```

## Conventions

There are a few conventions you should follow:
Expand Down
51 changes: 34 additions & 17 deletions src/joy/db.janet
Expand Up @@ -25,7 +25,7 @@
Example:

(import sqlite3)
(import joy/db)
(import joy)

(db/with-connection "a-different-database.sqlite3"
(sqlite3/eval "select 1;"))`
Expand Down Expand Up @@ -86,7 +86,7 @@

Example:

(import joy/db)
(import joy)

(db/query "select * from todos")

Expand All @@ -112,7 +112,7 @@

Example:

(import joy/db)
(import joy)

(db/execute "create table todo (id integer primary key, name text)")

Expand All @@ -137,7 +137,7 @@

Example:

(import joy/db)
(import joy)

(db/last-inserted "todo" 1)

Expand Down Expand Up @@ -170,7 +170,7 @@

Example:

(import joy/db)
(import joy)

(db/fetch [:todo 1])

Expand All @@ -190,7 +190,7 @@

Example:

(import joy/db)
(import joy)

(db/fetch-all [:todo 1 :tag] :order "tag_name asc")

Expand All @@ -210,7 +210,7 @@

Example:

(import joy/db)
(import joy)

(db/from :todo :where {:completed true} :order "name" :limit 2)

Expand All @@ -231,19 +231,19 @@
(query sql params)))


(defn find
(defn find-by
`Takes a table name and optional args
and returns either nil or the first row from the query.

Example:

(import joy/db)
(import joy)

(db/find :todo :where {:completed true} :order "name")
(db/find-by :todo :where {:completed true} :order "name")

# or

(db/find :todo :where {:completed true} :order "name desc")
(db/find-by :todo :where {:completed true} :order "name desc")

=> {:id 1 name "name" :completed true}`
[table-name & args]
Expand All @@ -254,14 +254,31 @@
(get rows 0)))


(defn find
`Takes a table name and optional args
and returns either nil or the first row by primary key.

Example:

(import joy)

(db/find :todo 1)

=> {:id 1 name "name" :completed true}`
[table-name id]
(let [sql (sql/from table-name {:where {:id id} :limit 1})
rows (query sql {:id id})]
(get rows 0)))


(defn insert
`Takes an optional db connection, a table name and a dictionary,
inserts the dictionary as rows/columns into the database
and returns the inserted row from the database.

Example:

(import joy/db)
(import joy)

(db/insert :todo {:name "name3"})

Expand All @@ -279,7 +296,7 @@

Example:

(import joy/db)
(import joy)

(db/insert-all :todo [{:name "name4"} {:name "name5"}])

Expand All @@ -304,7 +321,7 @@

Example:

(import joy/db)
(import joy)

(db/update :todo 4 {:name "new name 4"})

Expand All @@ -331,7 +348,7 @@

Example:

(import joy/db)
(import joy)

(db/update-all :todo {:completed false} {:completed true})

Expand All @@ -358,7 +375,7 @@

Example:

(import joy/db)
(import joy)

(db/delete :todo {:id 1})

Expand All @@ -379,7 +396,7 @@

Example:

(import joy/db)
(import joy)

(db/delete-all :post :where {:draft true} :limit 1)

Expand Down
13 changes: 10 additions & 3 deletions test/joy/db-test.janet
@@ -1,10 +1,9 @@
(import tester :prefix "" :exit true)
(import "src/joy/db" :as db)
(import "src/joy/helper" :as helper)
(import "src/joy" :prefix "")
(import cipher)

# create a test .env file when this test is run
(helper/with-file [f ".env" :w]
(with-file [f ".env" :w]
(file/write f (string/format "ENCRYPTION_KEY=%s\nJOY_ENV=development\nDATABASE_URL=test.sqlite3" (string (cipher/encryption-key)))))

(db/connect)
Expand Down Expand Up @@ -45,6 +44,14 @@
(get ? 0)
(get ? :name)))))

(test "find"
(let [row (db/find :account (last-id))]
(= "new name" (get row :name))))

(test "find-by"
(let [row (db/find-by :account :where {:id (last-id)})]
(= "new name" (get row :name))))

(test "delete"
(let [account (db/delete :account (last-id))]
(= "new name" (get account :name)))))
Expand Down

0 comments on commit 2ffdacb

Please sign in to comment.