aleeusgr / hs-clojure

Other
0 stars 0 forks source link

implement the database #3

Closed aleeusgr closed 3 months ago

aleeusgr commented 4 months ago

Step 3: Implement database connection and CRUD operations

Step 4: Implement patient data model and validation

Refs: https://en.wikipedia.org/wiki/Create,_read,_update_and_delete

aleeusgr commented 4 months ago

Automatic PostgreSQL CRUD queries https://github.com/tatut/specql

aleeusgr commented 3 months ago

To implement the database in your Clojure/ClojureScript application, you'll need to follow these steps:

Step 1: Add dependencies

In your project.clj file, add the following dependencies:

:dependencies [[org.clojure/clojure "1.10.0"]
               [org.clojure/clojurescript "1.10.520"]
               [org.postgresql/postgresql "42.2.14"]
               [yesql "0.5.3"]
               [com.layerware/hugsql "0.5.1"]]

The org.postgresql/postgresql dependency is for connecting to PostgreSQL, yesql is for SQL queries, and com.layerware/hugsql is for generating SQL queries.

Step 2: Create a database configuration

Create a db.edn file in the root of your project with the following content:

{:database {:username "your_username"
             :password "your_password"
             :dbname "your_database"
             :host "localhost"
             :port 5432}}

Replace the placeholders with your actual PostgreSQL credentials and database name.

Step 3: Create a database connection

Create a db.clj file in your project's src directory with the following content:

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

(def db-config (edn/read-string (slurp "db.edn")))

(def db-spec {:classname "org.postgresql.Driver"
              :subprotocol "postgresql"
              :subname (str "//" (:host db-config) ":" (:port db-config) "/" (:dbname db-config))
              :user (:username db-config)
              :password (:password db-config)})

(defn db-connection []
  (jdbc/get-connection db-spec))

This file defines a db-spec map that contains the database connection details and a db-connection function that returns a database connection.

Step 4: Create a SQL file for schema creation

Create a schema.sql file in your project's resources directory with the following content:

CREATE TABLE patients (
  id SERIAL PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  sex VARCHAR(1) NOT NULL,
  date_of_birth DATE NOT NULL,
  address VARCHAR(255) NOT NULL,
  social_security_number VARCHAR(11) NOT NULL
);

This file defines the patients table schema.

Step 5: Create a migration file

Create a migrations.clj file in your project's src directory with the following content:

(ns migrations
  (:require [yesql.core :refer [defquery]]
            [db :refer [db-connection]]))

(defquery create-patients-table
  "CREATE TABLE patients (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    sex VARCHAR(1) NOT NULL,
    date_of_birth DATE NOT NULL,
    address VARCHAR(255) NOT NULL,
    social_security_number VARCHAR(11) NOT NULL
  )")

(defn migrate []
  (with-open [conn (db-connection)]
    (jdbc/execute! conn [create-patients-table])))

This file defines a migrate function that creates the patients table using the create-patients-table query.

Step 6: Run the migration

Run the migration by executing the following command in your terminal:

lein run -m migrations/migrate

This will create the patients table in your PostgreSQL database.

That's it! You now have a PostgreSQL database set up with a patients table. You can proceed to implement the CRUD operations and UI components for your application.

aleeusgr commented 3 months ago

https://stackoverflow.com/questions/55099722/connecting-clojure-to-postgresql-db