reifyhealth / specmonstah

Specmonstah will eat the face off your test fixtures
MIT License
359 stars 18 forks source link

Quickstart idea? #54

Closed AndreaCrotti closed 4 years ago

AndreaCrotti commented 5 years ago

I finally managed to go through the docs, and got a very small prototype working to use this awesome library.

However, I think one problem that we had (in my team at least) is that everyone wanted to try out this library but was always a bit scared of the documentation. The question that we wanted to answer (and maybe others), is something like: given some specs and a model definition, how do I get the data out?

If something like this below was maybe in some kind of quickstart, it would appear less daunting since it looks actually quite easy (apart from the query DSL which requires some explanation maybe).

  (:require [clojure.spec.alpha :as s]
            [reifyhealth.specmonstah.spec-gen :as sg]
            [reifyhealth.specmonstah.core :as sm]))

(s/def ::id uuid?)
(s/def ::user (s/keys :req-un [::id]))

(s/def ::owner-id uuid?)
(s/def ::article (s/keys :req-un [::id ::owner-id]))

(def schema
  {:user {:prefix :u
          :spec ::user}
   :article {:prefix :a
             :spec ::article
             :relations {:owner-id [:user :id]}}})

;; generate the linked maps
(-> (sg/ent-db-spec-gen {:schema schema} {:article [[1]]})
    (sm/attr-map :spec-gen))
;; => {:u0 {:id #uuid "0011b129-e724-4a57-9886-9fc129bee763"}, :a0 {:id #uuid "e94032c0-3fdc-4e11-bcea-86f9164e4714", :owner-id #uuid "0011b129-e724-4a57-9886-9fc129bee763"}}
AndreaCrotti commented 5 years ago

I guess the informercial is similar to what I was proposing anyway. After some more experimentation, maybe the biggest issue in approaching this library is that in a way it doesn't really abstract from loom and the internal representation of all these graphs.

Maybe there is a way to hide all this extra complexity, but I guess I can just build a few helpers myself to simplify the usage of this library, having loom underneath it's pretty powerful so I certainly don't wish that to change.

flyingmachine commented 5 years ago

Hey @AndreaCrotti ! Not sure if we've ever interacted, but I know I've seen you around online :) This is great feedback! I'll keep it mind when I'm next able to revise the docs.

I too would like to have some helpers so that devs can be mostly unaware of the internal representation. I've added a PR (#58) that hopefully should help. It's definitely a challenge to figure out what are the most helpful and easily-understandable helpers. If you come up with any and want to share them I'd really appreciate it!

flyingmachine commented 4 years ago

@AndreaCrotti just wanted to let you know that recent PRs (#58 and #61) have made greatly reduced the need to use or know about loom. In particular, visiting functions now get passed much more context so that the visiting function will most likely have whatever data it needs.

I"ve also tried to call out attr-map in more places in the README.

flyingmachine commented 4 years ago

I'm going to go ahead and close this but if you still think it's confusing please reopen :)