PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/test/lobos/test/sample_schema.clj

http://github.com/budu/lobos
Clojure | 48 lines | 33 code | 8 blank | 7 comment | 0 complexity | 1b414e2ec68cf5c694b338455f845d2c MD5 | raw file
 1;; Copyright (c) Nicolas Buduroi. All rights reserved.
 2;; The use and distribution terms for this software are covered by the
 3;; Eclipse Public License 1.0 which can be found in the file
 4;; epl-v10.html at the root of this distribution. By using this software
 5;; in any fashion, you are agreeing to be bound by the terms of this
 6;; license.
 7;; You must not remove this notice, or any other, from this software.
 8
 9(ns lobos.test.sample-schema
10  (:refer-clojure :exclude [alter compile drop
11                            bigint boolean char double float time])
12  (:use (lobos core schema)))
13
14(defn surrogate-key [table]
15  (integer table :id :auto-inc :primary-key))
16
17(defn datetime-tracked [table]
18  (-> table
19      (timestamp :updated_on)
20      (timestamp :created_on (default (now)))))
21
22(defn refer-to [table ptable]
23  (let [cname (-> (->> ptable name butlast (apply str))
24                  (str "_id")
25                  keyword)]
26    (integer table cname [:refer ptable :id :on-delete :set-null])))
27
28(defmacro tbl [name & elements]
29  `(-> (table ~name
30         (surrogate-key)
31         (datetime-tracked))
32       ~@elements))
33
34(def sample-schema
35  (schema :lobos
36   (tbl :users
37    (varchar :name 100 :unique)
38    (check :name (> (length :name) 1)))
39
40   (tbl :posts
41    (varchar :title 200 :unique)
42    (text :content)
43    (refer-to :users))
44
45   (tbl :comments
46    (text :content)
47    (refer-to :users)
48    (refer-to :posts))))