我正在做一个关于数据数据库的学习项目。在开始之前,我不得不说我是这里的初学者。我试图模拟一个在线商店(仅包括 4 个项目:D)的数据库关系和工作。
我有一些问题,希望它们不是太基础。
1- 这家商店最好的关系结构是什么? (我不确定我的是最好的,所以我想了解哪种方式是管理参考关系的最佳实践。)
2-我怎样才能回答问题,最好的方法是什么?我只知道这样? (d/q '[:找到?e :in $ ?产品名称 :在哪里 [?e :product/name ?product-name]] 分贝“啊”)
3-我该怎么做;当一件商品放入购物车并售出时,我想根据订单大小减少库存。
谢谢你抽出时间回答我的初学者问题。
(require '[datomic.client.api :as d])
(def client (d/client {:server-type :dev-local
:storage-dir :mem
:system "ci"}))
(d/create-database client {:db-name "db01"})
(def conn (d/connect client {:db-name "db01"}))
(def db (d/db conn))
(def db-schema
[{:db/ident :product/id
:db/valueType :db.type/long
:db/unique :db.unique/identity
:db/cardinality :db.cardinality/one}
{:db/ident :product/label
:db/valueType :db.type/string
:db/unique :db.unique/identity
:db/cardinality :db.cardinality/one}
{:db/ident :product/type
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}
{:db/ident :product/model-no
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}])
(d/transact conn {:tx-data db-schema})
(def stock-schema
[{:db/ident :stock/product
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
{:db/ident :order/stock
:db/valueType :db.type/long
:db/cardinality :db.cardinality/one}])
(d/transact conn {:tx-data stock-schema})
(def order-schema
[{:db/ident :order/product
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
{:db/ident :order/user
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
{:db/ident :order/size
:db/valueType :db.type/long
:db/cardinality :db.cardinality/one}])
(d/transact conn {:tx-data order-schema})
(def user-schema
[{:db/ident :user/id
:db/valueType :db.type/long
:db/cardinality :db.cardinality/one}
{:db/ident :user/name
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}
{:db/ident :user/password
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}])
(d/transact conn {:tx-data user-schema})
(def product-data
[{:product/id 100
:product/label "lacoste"
:product/type "urban clothing"
:product/model-no "polo shirt"}
{:product/id 101
:product/label "canada goose"
:product/type "jackets"
:product/model-no "caban"}
{:product/id 102
:product/label "mammut"
:product/type "boots"
:product/model-no "hiking boots"}
{:product/id 103
:product/label "husky"
:product/type "sleeping bags"
:product/model-no "arnapurna"}])
(d/transact conn {:tx-data product-data})
(def user-data
[{:user/id 1
:user/name "admin01"
:user/password "123456"}
{:user/id 2
:user/name "userdemo"
:user/password "123456"}
])
(d/transact conn {:tx-data user-data})
(def return-lacoste-entity-id (ffirst (d/q
'[:find ?e
:where
[?e :product/label "lacoste"]]
db)))
(identity return-lacoste-entity-id)
;=> 87960930222164
(def return-canada-goose-entity-id (ffirst (d/q
'[:find ?e
:where
[?e :product/label "canada goose"]]
db)))
(identity return-canada-goose-entity-id)
;=> 87960930222165
(def return-mammut-entity-id (ffirst (d/q
'[:find ?e
:where
[?e :product/label "mammut"]]
db)))
(identity return-mammut-entity-id)
;=> 87960930222166
(def return-husky-entity-id (ffirst (d/q
'[:find ?e
:where
[?e :product/label "husky"]]
db)))
(identity return-husky-entity-id)
;=> 87960930222167
(def stock-data
[{:stock/product return-lacoste-entity-id
:order/stock 10}
{:stock/product return-canada-goose-entity-id
:order/stock 8}
{:stock/product return-mammut-entity-id
:order/stock 6}
{:stock/product return-husky-entity-id
:order/stock 4}])
(d/transact conn {:tx-data stock-data})
(def stock-check-lacoste (ffirst (d/q
'[:find ?size
:where
[?e :stock/product 87960930222164]
[?e :order/stock ?size]]
db)))
(identity stock-check-lacoste)
;=> 10
(def stock-check-canada-goose (ffirst (d/q
'[:find ?size
:where
[?e :stock/product 87960930222165]
[?e :order/stock ?size]]
db)))
(identity stock-check-canada-goose)
;=> 8
(def stock-check-mammut (ffirst (d/q
'[:find ?size
:where
[?e :stock/product 87960930222166]
[?e :order/stock ?size]]
db)))
(identity stock-check-mammut)
;=> 6
(def stock-check-husky (ffirst (d/q
'[:find ?size
:where
[?e :stock/product 87960930222167]
[?e :order/stock ?size]]
db)))
(identity stock-check-husky)
;=> 4
;; ## ---------------------------------------------------------------------------------------------------------------
;1. Launch browser
;2. Navigate to url 'http://localhost:7779/'
;3. Click on 'Signup / Login' button
(clerk/html [:button {:type "button"} "Signup / Login"])
;4. Enter username password and email then click 'Signup' button to create an account
(def user-data
[{:user/id 3
:user/name "bariscan"
:user/password "123456"}
])
(d/transact conn {:tx-data user-data})
;5. user clicks products page to see all products
(clerk/html [:button {:type "button"} "Products"])
;6. user sees all of the products
(def show-all-products (d/q
'[:find ?name
:where
[?e :product/label ?name]]
db))
(identity show-all-products)
;=> [["canada goose"] ["husky"] ["mammut"] ["lacoste"]]
;7. user puts a mammut boot in the cart
;;... like continues