Last active
December 14, 2015 07:09
-
-
Save matstani/5048794 to your computer and use it in GitHub Desktop.
Compojure、Kormaを使ってSQLiteデータベースから読み出した値を表示。
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
(ns helloworld.handler | |
(:require [compojure.core :refer :all] | |
[compojure.handler :as handler] | |
[compojure.route :as route] | |
[korma.db :refer [defdb sqlite3]] | |
[korma.core :as korma] | |
[hiccup.core :refer [html]])) | |
;;データベース定義 | |
(defdb db (sqlite3 | |
;;データベースファイルのパスを指定 | |
{:db "db/helloworld.sqlite3"})) | |
;;itemsテーブルに対応するEntity定義 | |
(korma/defentity items) | |
;;itemsテーブルから全てのレコードを読み出し | |
(def all-items (korma/select items)) | |
;;htmlのリストに変換する関数 | |
(defn to-html [items] | |
(html | |
[:head [:title "Access DB using Korma."]] | |
[:body | |
[:ul | |
(map (fn [i] [:li (:title i)]) items)]])) | |
(defroutes app-routes | |
;;データベースから読みだしたレコードをリストに変換して表示 | |
(GET "/" [] (to-html all-items)) | |
(route/not-found "Not Found")) | |
(def app | |
(handler/site app-routes)) |
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
(defproject helloworld "0.1.0-SNAPSHOT" | |
:description "FIXME: write description" | |
:url "http://example.com/FIXME" | |
:dependencies [[org.clojure/clojure "1.4.0"] | |
[compojure "1.1.5"] | |
[org.xerial/sqlite-jdbc "3.7.2"] | |
[korma "0.3.0-RC2"] | |
[hiccup "1.0.2"]] | |
:plugins [[lein-ring "0.8.2"]] | |
:ring {:handler helloworld.handler/app} | |
:profiles | |
{:dev {:dependencies [[ring-mock "0.1.3"]]}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment