diff --git a/doc/projects.md b/doc/projects.md index 66967ef8..9da58ce4 100644 --- a/doc/projects.md +++ b/doc/projects.md @@ -47,6 +47,7 @@ The following libraries and projects are known to work with babashka. - [multigrep](#multigrep) - [java-http-clj](#java-http-clj) - [component](#component) + - [minimallist](#minimallist) - [Pods](#pods) - [Projects](#projects-1) - [babashka-test-action](#babashka-test-action) @@ -685,6 +686,36 @@ Http client based on `java.net.http`. A tiny Clojure framework for managing the lifecycle and dependencies of software components which have runtime state. +### [minimallist](https://github.com/green-coder/minimallist) + +A minimalist data-driven data model library, inspired by Clojure Spec and Malli. + +Example partially borrowed from [minimallist's cljdoc](https://cljdoc.org/d/minimallist/minimallist/CURRENT/doc/usage-in-babashka) + +```clj +(require '[babashka.deps :refer [add-deps]]) + + +(add-deps '{:deps {minimallist/minimallist {:git/url "https://github.com/green-coder/minimallist" + :sha "b373bb18b8868526243735c760bdc67a88dd1e9a"}}}) + +(require '[minimallist.core :as m]) +(require '[minimallist.helper :as h]) + +(def contact (h/map [:name (h/fn string?)] + [:phone (h/fn string?)])) +(m/valid? contact {:name "Lucy" :phone "5551212"}) ;=> true +(m/valid? contact {:name "Lucy" :phone 5551212}) ;=> false + +(m/describe contact {:name "Lucy" :phone "5551212"}) ;=> {:valid? true, :entries {...}} +(m/describe contact {:name "Lucy" :phone 5551212}) ;=> {:valid? false, :entries {... :phone {:valid? false...}}} + +;; Does not work for now. +;(require '[clojure.test.check.generators :as tcg]) +;(require '[minimallist.generator :as mg]) +;(tcg/sample (mg/gen (h/fn int?))) +``` + ## Pods [Babashka pods](https://github.com/babashka/babashka.pods) are programs that can diff --git a/examples/README.md b/examples/README.md index 385d986d..22fd6d97 100644 --- a/examples/README.md +++ b/examples/README.md @@ -37,6 +37,8 @@ - [Using org.clojure/data.xml](#using-orgclojuredataxml) - [Simple logger](#simple-logger) - [Using GZip streams (memo utility)](#using-gzip-streams-to-make-a-note-utility) + - [Pretty-printing mySQL results](#pretty-printing-mysql-results) + Here's a gallery of useful examples. Do you have a useful example? PR welcome! @@ -534,3 +536,16 @@ ok $ memo.clj get jenny 8675309 ``` + +## Pretty-printing mySQL results + +[db_who.clj](db_who.clj) will query mysql for all the connected sessions and pretty-print the user and what program they're using. + +``` +$ bb db_who.clj +| user | program_name | +|------------------+----------------| +| root@localhost | mysql | +| fred@192.168.1.2 | workbench | +| jane@192.168.1.3 | Toad for mySQL | +``` diff --git a/examples/db_who.clj b/examples/db_who.clj new file mode 100644 index 00000000..8161c035 --- /dev/null +++ b/examples/db_who.clj @@ -0,0 +1,11 @@ +(ns db-who + (:require [clojure.java.shell :as shell] + [clojure.string :as str] + [clojure.pprint :as pp])) +(defn tsv->maps [tsv] + (let [lines (str/split-lines tsv) + [headers & rows] (map #(str/split % #"\t") lines)] + (map #(zipmap headers %) rows))) + +(-> (shell/sh "mysql" "--column-names" "-e" "select user, program_name from sys.session;") + :out tsv->maps pp/print-table)