Making a complete app

Originally published at: https://purelyfunctional.tv/web-dev-in-clojure/making-a-complete-app/
In this second part, we continue to build the app, rendering HTML, building customized middleware, and storing data in a database.

Anyone encountered Exception in thread “main” java.lang.RuntimeException: Unable to resolve symbol: uuid-ossp in this context, compiling:(webdev/item/model.clj:5:3) ?

I have done the following:

  1. project.clj
    :dependencies […
    [org.clojure/java.jdbc “0.7.6”]
    [org.postgresql/postgresql “42.2.2”]]
    2.webdev/item/model.clj
    (ns webdev.item.model
    (:require [clojure.java.jdbc :as db]))

(defn create-table [pg-db]
(db/execute! pg-db
[“CREATE EXTENSION IF NOT EXISTS “uuid-ossp””])
(db/execute! pg-db
(println pd-db)
[“CREATE TABLE IF NOT EXISTS items
(id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
name TEXT NOT NULL,
description TEXT NOT NULL,
checked BOOLEAN NOT NULL DEFAULT FALSE,
date_created TIMESTAMPTZ NOT NULL DEFAULT now())”]))
3.webdev/core.clj
(defn -main [port]
(items/create-table db))
4. Manually create uuid-ossp extensions in pgAdmin 4/webdev database
5. JDK setup: C:\Program Files\Java\jdk1.8.0_172

Found the solution:

webdev/model.clj
(sql/execute! postgres
[“CREATE EXTENSION IF NOT EXISTS \“uuid-ossp\””])

Hi everyone

in this code:

(ns webdev.item.model
(:require [clojure.java.jdbc :as db]))

(defn create-table [db]
(db/execute!
db

is the db in db/execute the same as db that is accepted as argument? if that is the case why does it need to accept parameter?

Hey @armedi,

Thanks for the question.

When you see a slash in a symbol or keyword, that means it’s a namespaced keyword. The namespace is to the left of the slash. So in db/execute!, db is the namespace and execute! is the name.

The db namespace, in this case, is a namespace alias. That’s like a short name we are giving to the namespace, because it has a long name that we don’t want to repeat. You can see that up in the ns declaration.

(:require [clojure.java.jdbc :as db])

The :as directive is telling Clojure to require the namespace and define an alias for it called db. Clojure will recognize the alias when referring to functions in that namespace.

In other words, because we’ve defined the alias, these two things are equivalent

(db/execute! my-db)

and

(clojure.java.jdbc/execute! my-db)

The db that is the first argument to db/execute! is different. That one refers to the argument to create-table. That is just a value that is passed to the function and then passed on to db/execute!.

It is common to see variables and namespaces with the same characters. For instance, many people alias clojure.string as str. So you’ll see:

(str/upper-case str)

The str as an argument is just another string.

Thanks again for asking. I’m here to help.

Rock on!
Eric

Thank you @ericnormand