add mysql pprint example and minimallist doc [skip ci] (#1024)
* add mysql pretty-printing example * add minimallist to libraries list * fix typos
This commit is contained in:
parent
19e3e84e05
commit
b799ac7897
3 changed files with 57 additions and 0 deletions
|
|
@ -47,6 +47,7 @@ The following libraries and projects are known to work with babashka.
|
||||||
- [multigrep](#multigrep)
|
- [multigrep](#multigrep)
|
||||||
- [java-http-clj](#java-http-clj)
|
- [java-http-clj](#java-http-clj)
|
||||||
- [component](#component)
|
- [component](#component)
|
||||||
|
- [minimallist](#minimallist)
|
||||||
- [Pods](#pods)
|
- [Pods](#pods)
|
||||||
- [Projects](#projects-1)
|
- [Projects](#projects-1)
|
||||||
- [babashka-test-action](#babashka-test-action)
|
- [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.
|
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
|
## Pods
|
||||||
|
|
||||||
[Babashka pods](https://github.com/babashka/babashka.pods) are programs that can
|
[Babashka pods](https://github.com/babashka/babashka.pods) are programs that can
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,8 @@
|
||||||
- [Using org.clojure/data.xml](#using-orgclojuredataxml)
|
- [Using org.clojure/data.xml](#using-orgclojuredataxml)
|
||||||
- [Simple logger](#simple-logger)
|
- [Simple logger](#simple-logger)
|
||||||
- [Using GZip streams (memo utility)](#using-gzip-streams-to-make-a-note-utility)
|
- [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!
|
Here's a gallery of useful examples. Do you have a useful example? PR welcome!
|
||||||
|
|
||||||
|
|
@ -534,3 +536,16 @@ ok
|
||||||
$ memo.clj get jenny
|
$ memo.clj get jenny
|
||||||
8675309
|
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 |
|
||||||
|
```
|
||||||
|
|
|
||||||
11
examples/db_who.clj
Normal file
11
examples/db_who.clj
Normal file
|
|
@ -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)
|
||||||
Loading…
Reference in a new issue