All Projects → heetch → Sqalx

heetch / Sqalx

Licence: mit
Nested transactions for sqlx

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Sqalx

Torque Postgresql
Add support to complex resources of PostgreSQL, like data types, array associations, and auxiliary statements (CTE)
Stars: ✭ 130 (-5.8%)
Mutual labels:  postgresql
Webapp.rs
A web application completely written in Rust. 🌍
Stars: ✭ 1,888 (+1268.12%)
Mutual labels:  postgresql
Oreilly intermediate sql for data
Resources for the O'Reilly Online Training "Intermediate SQL For Data Analysis"
Stars: ✭ 136 (-1.45%)
Mutual labels:  postgresql
Sql Template Tag
ES2015 tagged template string for preparing SQL statements, works with `pg` and `mysql`
Stars: ✭ 132 (-4.35%)
Mutual labels:  postgresql
Postgres Operator
Postgres operator creates and manages PostgreSQL clusters running in Kubernetes
Stars: ✭ 2,194 (+1489.86%)
Mutual labels:  postgresql
Mybatis Plus
An powerful enhanced toolkit of MyBatis for simplify development
Stars: ✭ 12,132 (+8691.3%)
Mutual labels:  postgresql
Ldap2pg
🐘 👥 Manage PostgreSQL roles and privileges from YAML or LDAP
Stars: ✭ 131 (-5.07%)
Mutual labels:  postgresql
Coolstore Microservices
A full-stack .NET microservices build on Dapr and Tye
Stars: ✭ 1,903 (+1278.99%)
Mutual labels:  postgresql
Mara Pipelines
A lightweight opinionated ETL framework, halfway between plain scripts and Apache Airflow
Stars: ✭ 1,841 (+1234.06%)
Mutual labels:  postgresql
Sysbench Tpcc
Sysbench scripts to generate a tpcc-like workload for MySQL and PostgreSQL
Stars: ✭ 136 (-1.45%)
Mutual labels:  postgresql
Db
Newt DB is a Python object-oriented database with JSONB-based access and search in PostgreSQL
Stars: ✭ 132 (-4.35%)
Mutual labels:  postgresql
Websiteone
A website for Agile Ventures
Stars: ✭ 132 (-4.35%)
Mutual labels:  postgresql
Subzero Starter Kit
Starter Kit and tooling for authoring GraphQL/REST API backends with subZero
Stars: ✭ 136 (-1.45%)
Mutual labels:  postgresql
Mumuki Laboratory
🔬 Where students practice and receive automated and human feedback
Stars: ✭ 131 (-5.07%)
Mutual labels:  postgresql
Graphql Go Example
Example GraphQL API implemented in Go and backed by Postgresql
Stars: ✭ 135 (-2.17%)
Mutual labels:  postgresql
Pgbouncerhero
A dashboard for your PgBouncers.
Stars: ✭ 129 (-6.52%)
Mutual labels:  postgresql
Masquerade
A Postgres Proxy to Mask Data in Realtime
Stars: ✭ 134 (-2.9%)
Mutual labels:  postgresql
Ozo
OZO is a C++17 Boost.Asio based header-only library for asyncronous communication with PostgreSQL DBMS.
Stars: ✭ 138 (+0%)
Mutual labels:  postgresql
Gemini
Model Driven REST framework to automatically generate CRUD APIs
Stars: ✭ 138 (+0%)
Mutual labels:  postgresql
Django Zombodb
Easy Django integration with Elasticsearch through ZomboDB Postgres Extension
Stars: ✭ 136 (-1.45%)
Mutual labels:  postgresql

sqalx

GoDoc Go Report Card

sqalx (pronounced 'scale-x') is a library built on top of sqlx that allows to seamlessly create nested transactions and to avoid thinking about whether or not a function is called within a transaction. With sqalx you can easily create reusable and composable functions that can be called within or out of transactions and that can create transactions themselves.

Getting started

$ go get github.com/heetch/sqalx

Import sqalx

import "github.com/heetch/sqalx"

Usage

package main

import (
	"log"

	"github.com/heetch/sqalx"
	"github.com/jmoiron/sqlx"
	_ "github.com/lib/pq"
)

func main() {
	// Connect to PostgreSQL with sqlx.
	db, err := sqlx.Connect("postgres", "user=foo dbname=bar sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}

	defer db.Close()

	// Pass the db to sqalx.
	// It returns a sqalx.Node. A Node is a wrapper around sqlx.DB or sqlx.Tx.
	node, err := sqalx.New(db)
	if err != nil {
		log.Fatal(err)
	}

	err = createUser(node)
	if err != nil {
		log.Fatal(err)
	}
}

func createUser(node sqalx.Node) error {
	// Exec a query
	_, _ = node.Exec("INSERT INTO ....") // you can use a node as if it were a *sqlx.DB or a *sqlx.Tx

	// Let's create a transaction.
	// A transaction is also a sqalx.Node.
	tx, err := node.Beginx()
	if err != nil {
		return err
	}
	defer tx.Rollback()

	_, _ = tx.Exec("UPDATE ...")

	// Now we call another function and pass it the transaction.
	err = updateGroups(tx)
	if err != nil {
		return nil
	}

	return tx.Commit()
}

func updateGroups(node sqalx.Node) error {
	// Notice we are creating a new transaction.
	// This would normally cause a dead lock without sqalx.
	tx, err := node.Beginx()
	if err != nil {
		return err
	}
	defer tx.Rollback()

	_, _ = tx.Exec("INSERT ...")
	_, _ = tx.Exec("UPDATE ...")
	_, _ = tx.Exec("DELETE ...")

	return tx.Commit()
}

PostgreSQL Savepoints

When using the PostgreSQL driver, an option can be passed to New to enable the use of PostgreSQL Savepoints for nested transactions.

node, err := sqalx.New(db, sqalx.SavePoint(true))

Issue

Please open an issue if you encounter any problem.

License

The library is released under the MIT license. See LICENSE file.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].