Skip to content

Instantly share code, notes, and snippets.

@c-spencer
Last active December 23, 2015 02:49
Show Gist options
  • Select an option

  • Save c-spencer/6569571 to your computer and use it in GitHub Desktop.

Select an option

Save c-spencer/6569571 to your computer and use it in GitHub Desktop.
Simple demo of type construction from Datomic with core.typed and a little glue
; Create a type extractor for this database.
(dat/create-typer create-type "datomic:mem://jelp")
; Use to reify a User type
(create-type User "User")
; Create Admin type from User, with a fixed :user/group
; Lets Admins be used wherever a User is, and be statically checked.
(create-type Admin "User" {:user/group (Value "admin")})
(ann my-admin Admin)
(def my-admin {:user/id "my-id"
:user/email "my@email.com"
:user/password "my-pass"
:user/group "admin"})
(ann my-user User)
(def my-user {:user/id "my-id"
:user/email "my@email.com"
:user/password "my-pass"
:user/group "user"})
;; Failing cases
(ann my-not-admin Admin)
(def my-not-admin {:user/id "my-id"
:user/email "my@email.com"
:user/password "my-pass"
:user/group "user"}) ; this isn't the admin group
(ann my-invalid-user User)
(def my-invalid-user {:user/id 10 ; should be string
:user/email "my@email.com"
:user/password "my-pass"
:user/group "admin"})
;; Storing the optional/mandatory inside the database like so:
{ :db/id #db/id[:db.part/types]
:type/name "User"
:type/mandatory [:user/id
:user/email
:user/password
:user/group]
:type/optional [:user/forename
:user/surname] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment