Skip to content

Instantly share code, notes, and snippets.

@fpischedda
Created August 14, 2020 15:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpischedda/b7e06f464006169eccef82921542cdc5 to your computer and use it in GitHub Desktop.
Save fpischedda/b7e06f464006169eccef82921542cdc5 to your computer and use it in GitHub Desktop.
datascript-datalog-random-examples
(ns rs.frontend.db
(:require
[cljs.core :refer [random-uuid]]
[datascript.core :as d]))
;; some ids, pre-generated to create mock data and references
(defonce ids {:image1 (random-uuid)
:image2 (random-uuid)
:image3 (random-uuid)
:image4 (random-uuid)
:project1 (random-uuid)
:project2 (random-uuid)
:project3 (random-uuid)
:share1 (random-uuid)
:share2 (random-uuid)
:share3 (random-uuid)
:comment1 (random-uuid)
:comment2 (random-uuid)
:comment3 (random-uuid)
})
(def schema
{:project/id {:db/unique :db.unique/identity}
:project/images {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}
:image/id {:db/unique :db.unique/identity}
:share/id {:db/unique :db.unique/identity}
:share/project {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
:share/images {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}
:comment/id {:db/unique :db.unique/identity}
:comment/share {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
:comment/project {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
:comment/image {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
:ui.setting/name {:db/unique :db.unique/identity}
})
(defn ui-project-selected-image-id [project-id]
(str "project-selected-image-" project-id))
(defn ui-project-selected-section [project-id]
(str "project-selected-section-" project-id))
(def db (d/create-conn schema))
(let [{:keys [image1 image2 image3 image4
project1 project2 project3
share1 share2 share3
comment1 comment2 comment3]} ids]
(d/transact! db
[{:image/id image1
:image/original "/images/paco.png"
:image/width 720
:image/height 769
:image/watermark :default
:image/watermark-size "medium"
:image/watermark-enabled true}
{:image/id image2
:image/original "/images/header.png"
:image/width 733
:image/height 500
:image/watermark :default
:image/watermark-size "small"
:image/watermark-enabled false}
{:image/id image3
:image/original "/images/stintino.jpg"
:image/width 1000
:image/height 525
:image/watermark :default
:image/watermark-size "small"
:image/watermark-enabled false}
{:image/id image4
:image/original "/images/porto-ferro.jpg"
:image/width 720
:image/height 342
:image/watermark :default
:image/watermark-size "small"
:image/watermark-enabled false}
{:project/id project1
:project/name "Banner for next campaign"
:project/created-at "2020-04-09T12:34:56Z"
:project/images [{:image/id image1}
{:image/id image2}]}
{:project/id project2
:project/name "Brand design for Shoe e-commerce startup"
:project/created-at "2020-04-19T12:34:56Z"
:project/images [{:image/id image3}]}
{:project/id project3
:project/name "Logo design for secret client"
:project/created-at "2020-05-09T12:34:56Z"
:project/images [{:image/id image4}]}
{:share/id share1
:share/project {:project/id project2}
:share/created-at "2020-07-20T22:05:00Z"}
{:share/id share2
:share/project {:project/id project2}
:share/created-at "2020-07-21T12:05:00Z"}
{:share/id share3
:share/project {:project/id project3}
:share/created-at "2020-07-22T12:05:00Z"}
{:comment/id comment1
:comment/author "Shoe company"
:comment/created-at "2020-04-20T11:01:12Z"
:comment/text "I think we are moving in the right direction. Looking forward to seeing the final revision!"
:comment/share {:share/id share1}
:comment/image {:image/id image3}}
{:comment/id comment2
:comment/author "Demo author"
:comment/created-at "2020-04-20T12:01:12Z"
:comment/text "Thanks! Will get back to you in a day or two."
:comment/share {:share/id share1}
:comment/image {:image/id image3}}
{:comment/id comment3
:comment/author "Demo author"
:comment/created-at "2020-04-20T12:01:12Z"
:comment/text "This is the final revision, can you approve it?"
:comment/share {:share/id share2}
:comment/image {:image/id image3}}
{:ui.setting/name "selected-project"
:ui.setting/value project2}
{:ui.setting/name (ui-project-selected-image-id project2)
:ui.setting/value image3}
{:ui.setting/name (ui-project-selected-section project2)
:ui.setting/value :images}
{:ui.setting/name (ui-project-selected-section project3)
:ui.setting/value :shares}
{:ui.setting/name "app-section"
:ui.setting/value :projects}
]))
(defn get-ui-setting [db name]
(->
(d/entity db [:ui.setting/name name])
:ui.setting/value))
(defn selected-project-id [db]
(get-ui-setting db "selected-project"))
(comment
(selected-project-id @db)
)
(defn update-ui-setting [db name value]
(d/transact! db
[{:ui.setting/name name
:ui.setting/value value}]))
(defn change-selected-project! [new-project-id]
(update-ui-setting db "selected-project" new-project-id))
(defn selected-project-image [db project-id]
(get-ui-setting db (ui-project-selected-image-id project-id)))
(comment
(selected-project-image @db (:project2 ids))
)
(defn change-project-selected-image! [project-id new-image-id]
(update-ui-setting db (ui-project-selected-image-id project-id) new-image-id))
(defn selected-project-section
([db project-id]
(selected-project-section db project-id :images))
([db project-id default]
(if-let [section (get-ui-setting db (ui-project-selected-section project-id))]
section
default)))
(comment
(selected-project-section @db (:project1 ids))
)
(defn change-project-selected-section! [project-id new-section]
(update-ui-setting db (ui-project-selected-section project-id) new-section))
(comment
(change-project-selected-section! (:project1 ids) :shares)
)
(defn get-current-section
([db]
(get-current-section db :projects))
([db default]
(if-let [section (get-ui-setting db "app-section")]
section
default)))
(defn set-current-section! [section]
(update-ui-setting db "app-section" section))
(defn project-list [db]
(d/q '[ :find ?id ?name
:where
[?e :project/name ?name]
[?e :project/id ?id]
]
db))
(comment
(project-list @db)
)
(defn project-detail [db project-id]
(d/touch (d/entity db [:project/id project-id])))
(comment
(project-detail @db (:project3 ids))
)
(defn project-images [db project-id]
(d/q '[:find ?image-id ?original ?width ?height
:in $ ?project-id
:where
[?p :project/id ?project-id]
[?p :project/images ?i]
[?i :image/original ?original]
[?i :image/width ?width]
[?i :image/height ?height]
[?i :image/id ?image-id]
]
db project-id))
(comment
(project-images @db (:project2 ids))
(project-images @db (selected-project-id @db))
)
(defn all-images [db]
(d/q '[:find ?image-id ?original ?width ?height
:where
[?i :image/original ?original]
[?i :image/width ?width]
[?i :image/height ?height]
[?i :image/id ?image-id]
]
db))
(defn image-detail [db image-id]
(d/touch (d/entity db [:image/id image-id])))
(comment
(image-detail @db (:image3 ids))
)
(defn add-image! [{:keys [id original width height
watermark watermark-size watermark-enabled]
:or {id (random-uuid)
watermark :default
watermark-size "medium"
watermark-enabled false}}]
(d/transact! db
[{:image/id id
:image/name name
:image/original original
:image/width width
:image/height height
:image/watermark watermark
:image/watermark-size watermark-size
:image/watermark-enabled watermark-enabled}]))
(defn all-comments [db]
(d/q '[:find ?id ?author ?created-at ?text
:where
[?c :comment/id ?id]
[?c :comment/author ?author]
[?c :comment/created-at ?created-at]
[?c :comment/text ?text]
]
db))
(comment
(all-comments @db)
)
(defn comment-detail [db comment-id]
(d/touch (d/entity db [:comment/id comment-id])))
(comment
(comment-detail @db (:comment1 ids))
)
(defn project-shares [db project-id]
(d/q '[:find ?share-id ?created-at
:in $ ?project-id
:where
[?s :share/created-at ?created-at]
[?s :share/id ?share-id]
[?s :share/project ?p]
[?p :project/id ?project-id]
]
db project-id))
(comment
(project-shares @db (:project2 ids))
(project-images @db (selected-project-id @db))
)
(defn share-detail [db share-id]
(d/touch (d/entity db [:share/id share-id])))
(comment
(share-detail @db (:share1 ids))
)
(defn share-comments [db share-id]
(d/q '[:find ?id ?author ?created-at ?text
:in $ ?share-id
:where
[?c :comment/share ?s]
[?s :share/id ?share-id]
[?c :comment/id ?id]
[?c :comment/author ?author]
[?c :comment/created-at ?created-at]
[?c :comment/text ?text]
]
db share-id))
(comment
(share-comments @db (:share1 ids)) ;; should return something
(share-comments @db (:share3 ids)) ;; should return nothing
)
(defn add-share-comment! [{:keys [id share-id author text]
:or {id (random-uuid)}}]
(d/transact! db
[{:comment/id id
:comment/author author
:comment/created-at "2020-07-12T08:41:12Z"
:comment/text text
:comment/share {:share/id share-id}}]))
(comment
(add-share-comment! {:share-id (:share1 ids)
:author "Diddi"
:text "Miaouuuuu"})
)
(defn project-image-comments [db project-id image-id]
(d/q '[:find ?id ?author ?created-at ?text
:in $ ?project-id ?image-id
:where
[?c :comment/project ?p]
[?p :project/id ?project-id]
[?c :comment/image ?i]
[?i :image/id ?image-id]
[?c :comment/id ?id]
[?c :comment/author ?author]
[?c :comment/created-at ?created-at]
[?c :comment/text ?text]
]
db project-id image-id))
(comment
(project-image-comments @db (:project2 ids) (:image3 ids)) ;; should return something
(project-image-comments @db (:project2 ids) (:image1 ids)) ;; should return nothing
)
(defn add-project-image-comment! [project-id image-id author text]
(d/transact! db
[{:comment/id (random-uuid)
:comment/author author
:comment/created-at "2020-07-12T08:41:12Z"
:comment/text text
:comment/project {:project/id project-id}
:comment/image {:image/id image-id}}]))
(comment
(add-project-image-comment! (:project1 ids) (:image1 ids) "Diddi" "Miaouuuuu")
)
(defn image-comments [db image-id]
(d/q '[:find ?id ?author ?created-at ?text ?share-id ?share-created-at
:in $ ?image-id
:where
[?c :comment/share ?s]
[?s :share/id ?share-id]
[?s :share/created-at ?share-created-at]
[?c :comment/image ?i]
[?i :image/id ?image-id]
[?c :comment/id ?id]
[?c :comment/author ?author]
[?c :comment/created-at ?created-at]
[?c :comment/text ?text]
]
db image-id))
(defn image-set-watermark-size! [image-id size]
(d/transact! db
[{:image/id image-id
:image/watermark-size size}]))
(defn image-set-watermark-enabled! [image-id enabled]
(d/transact! db
[{:image/id image-id
:image/watermark-enabled enabled}]))
(comment
;; get all project names
(d/q '[ :find [?name ...]
:where
[?e :project/name ?name]
]
@db)
;; get all project names and ids
(d/q '[ :find ?id ?name
:where
[?e :project/name ?name]
[?e :project/id ?id]
]
@db)
;; get all project images
(d/q '[:find ?image-id ?original ?width ?height
:in $ ?project-id
:where
[?p :project/id ?project-id]
[?p :project/images ?image-id]
[?i :image/original ?original]
[?i :image/width ?width]
[?i :image/height ?height]
[?i :image/id ?image-id]
]
@db 1)
;; get image details
(d/q '[:find ?id ?original ?watermark ?watermark-size ?watermark-enabled ?width ?height
:in $ ?id
:where
[?i :image/original ?original]
[?i :image/watermark ?watermark]
[?i :image/watermark-size ?watermark-size]
[?i :image/watermark-enabled ?watermark-enabled]
[?i :image/width ?width]
[?i :image/height ?height]
[?i :image/id ?id]
]
@db 1)
;; get all images
(d/q '[ :find ?id ?name
:where
[?e :image/id ?id]
[?e :image/original ?name]]
@db)
;; get all comments
(d/q '[ :find ?id ?author ?text ?project ?image
:where
[?e :comment/id ?id]
[?e :comment/author ?author]
[?e :comment/text ?text]
[?e :comment/project ?project]
[?e :comment/image ?image]
]
@db)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment