• Stars
    star
    130
  • Rank 268,142 (Top 6 %)
  • Language
    Clojure
  • License
    MIT License
  • Created about 7 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automatic PostgreSQL CRUD queries

CircleCI Clojars Project

specql

A library for simple PostgreSQL queries with namespaced keys.

specql introspects your database at compile time and generates clojure.spec definitions for the rows and all the columns. You can then query for rows by giving a table, the keys to return and a map of keys to match against.

intro talk

For a quick intro, see my talk about specql at ClojuTRE 2017 Scrap your query boilerplate with specql

usage

See the docs for more examples.

(def db (make-my-db))

;; Define tables as [table-name :namespaced/table-keyword]
;; specql automatically creates specs for the columns of the table (in the same namespace as the
;; table keyword). For example if :employee/employees is the table and it has "id" and "name"
;; columns, specql will generate :employee/id and :employee/name specs with predicates
;; determined by the column data type
(define-tables define-db
  ["address" :address/address]
  ["employee" :employee/employees]
  ["department" :department/departments])

;; Querying

(fetch db
       ;; The table to query from
       :employee/employees
       ;; the columns to return
       #{:employee/id :employee/name :employee/title :employee/address}
       ;; the where clause
       {:employee/id 1})
;; => ({:employee/id 1
;;      :employee/name "Wile E. Coyote"
;;      :employee/title "Super genius"
;;      :employee/address {:address/street "Desert avenue 1"
;;                         :address/postal-code "31173"
;;                         :address/country "US"}})

Changes

20230316

  • Dependency update (and test fixes due to changed spec exception messages)

20230315

  • Some maintenance (upgrade embedded postgres)
  • Allow schema to be cached in EDN file on the classpath

20190301

  • Fix composite type as primary key in upsert!

20181212

  • Fix AOT compilation issue on Windows (no functionality changes)

20181211

  • Support "int2" type in composite parsing

20180706

  • Composite parsing speed improvement on large payloads: Avoid creating substrings when skipping quoted values.

20180312

  • Changed to a date based versioning
  • Added specql.postgis namespace that adds composite parsing support for geometry (requires postgis Java classes)

0.7.0-alpha18 (2018-03-03)

Birthday release! Specql is now 1 year old.

  • Added support for MATERIALIZED VIEWs
  • Added refresh! to refresh a materialized view
  • Fixed update! with empty where clause
  • Fixed SQL generation of an empty combined op on column level

0.7.0-alpha17 (2018-02-22)

  • Added SQL exceptions provide generated SQL and parameters
  • Fix storing nil value when in a to-keyword transformed composite

0.7.0-alpha16 (2018-02-05)

  • Fixed yet another composite quoting issue
  • Added generative test to create "interesting" composite data

0.7.0-alpha14 (2018-01-23)

  • Fix: disambiguate ORDER BY column with table alias

0.7.0-alpha13 (2018-01-22)

  • Quote type names in casts

0.7.0-alpha12 (2018-01-03)

  • Add missing "bool" support in composite
  • Fix composite parsing, skip quoted values when looking for a matching start/end pair (like "(" and ")").

0.7.0-alpha11

  • Add spec for TIMESTAMP WITH TIME ZONE (timestamptz)

0.7.0-alpha10

  • Fix reading of transformed type in a composite type

0.7.0-alpha9

  • bigdec? was removed in Clojure 1.9.0-beta4, replaced with decimal? for compatibility

0.7.0-alpha8

  • Fix composite array of primitive type parsing

0.7.0-alpha7

  • Support "bpchar" (CHAR(n) type) in composite parsing
  • Fix table quoting in DELETE
  • Remove clojure.future requires as 1.9 is nearing release
  • Add :specql.core/transform-column-name option to define-tables

0.7.0-alpha6

  • Fix composite indexes when UDT attributes have been removed (with ALTER TYPE)

0.7.0-alpha5

  • Apply specql/transform definitions to composite array elements when reading

0.7.0-alpha4

  • Improved Reading of complex composites

0.7.0-alpha2 and -alpha3

  • Fixed issue with ROW(?,...)::typename inserts with complex composites

0.7.0-alpha1

  • Support stored procedures as functions
  • Support order, limit and offset in query parameters

0.6

  • Support custom column transformations (eg. db enums to keywords)
  • Better enum support (operators add type casts to parameters)
  • Operators now take a third argument: column info
  • Support "point" type as vector [x y] in composites
  • New functions: columns and tables for querying the runtime registry
  • Improved error handling and better error messages

work in progress

This is very much still work in progress.

Features I intend to implement:

  • fetch
  • update
  • insert
  • upsert
  • delete
  • JOIN navigation: has-one
  • JOIN navigation: has-many
  • standard operators for where ({:employee/name (like "%Smith%")})
  • unpacking composite types (user defined record types as column values should be nested maps)

non-issues

This library is NOT intended to replace having to write SQL. The more complex SQL (like reporting queries and complex joins) queries are better written as SQL. Use yesql/jeesql/hugsql or the like.

This library intends to provide the most common case of CRUD queries and defer to SQL on the more difficult ones.

Specql can use VIEWs in the database just like tables, so you can write your complex queries as views and use them via specql.

More Repositories

1

ripley

Server rendered UIs over WebSockets
Clojure
293
star
2

clj-chrome-devtools

Clojure API for controlling a Chrome DevTools remote
Clojure
129
star
3

pgprolog

PostgreSQL Prolog language handler
Rust
125
star
4

xtdb-inspector

Web UI for inspecting XTDB database
Clojure
87
star
5

postgrest-ui

ClojureScript UI components for PostgREST
Clojure
57
star
6

Webjure

Webjure, a web programming framework for Clojure
Clojure
32
star
7

bearsql

Bare words SQL macro for Clojure
Clojure
32
star
8

tuck

Tuck: a micro framework for Reagent apps
Clojure
29
star
9

re-svg-icons

Reagent SVG icons
Clojure
28
star
10

bb-lambda

AWS Lambda custom runtime for Babashka scripts
Clojure
19
star
11

REPLey

A web REPL made with Ripley
Clojure
16
star
12

xtdberl

Erlang/Elixir interface to XTDB
Erlang
15
star
13

re-html-template

Generate hiccup (reagent/server rendering) components from HTML templates at compile time
Clojure
14
star
14

LiveWeb

LiveWeb: web app framework for Smalltalk
Smalltalk
13
star
15

json-schema

JSON schema validator
Clojure
12
star
16

nrepl-doc-inject

NREPL middleware to inject community docstrings
Clojure
12
star
17

drtest

Declarative Reagent testing library
Clojure
12
star
18

eta-jdbc-example

Simple example of using JDBC from Eta language
Haskell
10
star
19

reagent-leaflet

Use LeafletJS maps as Reagent components
Clojure
9
star
20

mato

Simple reagent SVG worm game for educational purposes
Clojure
6
star
21

pows

Playwright over WebSocket
Clojure
6
star
22

koukku

Yet another small React wrapper with hooks
Clojure
5
star
23

rolf

Reagent OpenLayers Functions: a simple Reagent wrapper for OpenLayers
Clojure
5
star
24

pharo-Pows

Pharo library for using Playwright over WebSocket
Smalltalk
5
star
25

widgetshop

Simple Reagent example.
Clojure
4
star
26

asar

Read ASAR archive files from Clojure
Clojure
4
star
27

tulivarasto

Simple Google Cloud Firestore library for Clojure
Clojure
3
star
28

matofs

Simple F# SVG worm game (Fable React)
F#
3
star
29

electric-examples

Electric Clojure examples and experiments (based on starter app)
CSS
2
star
30

pharo-XTDB

XTDB client for Pharo Smalltalk
Smalltalk
2
star
31

edn-to-cfn

See: portkey-cloud/edn-to-cfn
Clojure
2
star
32

tuck-remoting

Supplemental remoting library for Tuck: send events between client and server
Clojure
2
star
33

elmato

Simple Elm SVG worm game
Elm
2
star
34

pharo-EDN

Extensible Data Notation library for Pharo Smalltalk
Smalltalk
2
star
35

mappy

An experimental pure Reagent web map library
Clojure
2
star
36

pharo-EmacsKeys

Some emacs like key bindings to make Pharo code editing feel more at home
Smalltalk
2
star
37

fileyard

Trivial file storage
Clojure
1
star
38

raylib-smalltalk

Smalltalk bindings to Raylib
Smalltalk
1
star
39

jiradash

Emacs JIRA dashboard
Emacs Lisp
1
star
40

iwantmylunch

A simple Swift command line utility to check your Edenred lunch card balance
Swift
1
star
41

aoc2021-smalltalk

Some Advent of Code 2021 learning using Smalltalk
Smalltalk
1
star
42

pharo-Kafka

Pharo bindings to librdkafka C client library.
Smalltalk
1
star
43

express-htmx

CSS
1
star
44

aoc2023-prolog

Advent of Code 2023 in Prolog
Prolog
1
star