Projects using Clojure spec - next-jdbc

The next-jdbc project is a modern low-level Clojure wrapper for JDBC-based access to databases.

The project defines data specifications using predicates and

Defining specifications

Specifications are defined within a single file src/next/jdbc/specs.clj.

Specifications start with clojure.spec.alpha/def expressions, using predicate functions as specifications. There is also a custom predicate function called

Function definition specifications follow, using the clojure.spec.alpha/fdef function. The fdef functions define the specification for the arguments of each function. The fdef function name is the same as the function definition it is defining a specification for.

Instrumenting specifications

Instrumenting functions provides automatic checking that argument in a function call conforms to the specification.

Rather than write individual expressions to instrument each function, a var called fns-with-specs contains a collection of names for all the fdef function definition specifications.

  1. (def ^:private fns-with-specs
  2. [`jdbc/get-datasource
  3. `jdbc/get-connection
  4. `jdbc/prepare
  5. `jdbc/plan
  6. `jdbc/execute!
  7. `jdbc/execute-one!
  8. `jdbc/transact
  9. `jdbc/with-transaction
  10. `connection/->pool
  11. `prepare/execute-batch!
  12. `prepare/set-parameters
  13. `prepare/statement
  14. `sql/insert!
  15. `sql/insert-multi!
  16. `sql/query
  17. `sql/find-by-keys
  18. `sql/get-by-id
  19. `sql/update!
  20. `sql/delete!])

Instrument all the functions by passing fns-with-specs as an argument to the clojure.spec.test.alpha/instrument function.

This call is wrapped in a simple handler function for convenience.

  1. (defn instrument []
  2. (clojure.spec.test.alpha/instrument fns-with-specs))

To remove the checking of argument specifications, clojure.spec.test.alpha/unstrument is passed fns-with-specs, again wrapped in a convinced function.

  1. (defn unstrument []
  2. (clojure.spec.test.alpha/unstrument fns-with-specs))