Proponuję użyć Tupelo Datomic, aby rozpocząć. Napisałem tę bibliotekę, aby uprościć tworzenie schematów Datomic i ułatwić zrozumienie, podobnie jak wspominasz w swoim pytaniu.
Jako przykład załóżmy, że staramy się śledzić informacje o światowej premierze agencji szpiegowskiej. Stwórzmy kilka atrybutów, które będą miały zastosowanie do naszych bohaterów: & (zobacz kod wykonywalny in the unit test).
(:require [tupelo.datomic :as td]
[tupelo.schema :as ts])
; Create some new attributes. Required args are the attribute name (an optionally namespaced
; keyword) and the attribute type (full listing at http://docs.datomic.com/schema.html). We wrap
; the new attribute definitions in a transaction and immediately commit them into the DB.
(td/transact *conn* ; required required zero-or-more
; <attr name> <attr value type> <optional specs ...>
(td/new-attribute :person/name :db.type/string :db.unique/value) ; each name is unique
(td/new-attribute :person/secret-id :db.type/long :db.unique/value) ; each secret-id is unique
(td/new-attribute :weapon/type :db.type/ref :db.cardinality/many) ; one may have many weapons
(td/new-attribute :location :db.type/string) ; all default values
(td/new-attribute :favorite-weapon :db.type/keyword)) ; all default values
Dla: atrybut broń/typ, chcemy wykorzystać typ wyliczany ponieważ istnieje tylko ograniczona liczba wyborów do dyspozycji naszych antagonistów:
; Create some "enum" values. These are degenerate entities that serve the same purpose as an
; enumerated value in Java (these entities will never have any attributes). Again, we
; wrap our new enum values in a transaction and commit them into the DB.
(td/transact *conn*
(td/new-enum :weapon/gun)
(td/new-enum :weapon/knife)
(td/new-enum :weapon/guile)
(td/new-enum :weapon/wit))
Stwórzmy kilka antagonistami i obciążenia je do DB. Zwróć uwagę, że używamy tutaj zwykłych wartości i literałów Clojure i nie musimy się martwić o żadne konkretne konwersje Datomic.
; Create some antagonists and load them into the db. We can specify some of the attribute-value
; pairs at the time of creation, and add others later. Note that whenever we are adding multiple
; values for an attribute in a single step (e.g. :weapon/type), we must wrap all of the values
; in a set. Note that the set implies there can never be duplicate weapons for any one person.
; As before, we immediately commit the new entities into the DB.
(td/transact *conn*
(td/new-entity { :person/name "James Bond" :location "London" :weapon/type #{ :weapon/gun :weapon/wit } })
(td/new-entity { :person/name "M" :location "London" :weapon/type #{ :weapon/gun :weapon/guile } })
(td/new-entity { :person/name "Dr No" :location "Caribbean" :weapon/type :weapon/gun }))
Ciesz się! Alan
dzięki! To wydaje się rozwiązać problem # 2. Czy masz jakieś porady na temat numeru 1? –