scany

repository·master·Indexed 23 days ago

https://github.com/georgysavva/scany

A lightweight Go library for scanning database rows into structs, maps, and primitives. It supports database/sql via the sqlscan package and pgx via the pgxscan package, both of which are built on the dbscan core. scany is a scanning utility, not an ORM, and focuses on mapping database results to Go objects without manual row iteration.

Tokens
872
Snippets
3
Records
5
Agent score
31%

What's inside scany

  1. What scany is and isn't

    master

    Core Purpose

    scany is a scanning utility designed to map database rows into Go structs, maps, and primitive types. It eliminates the need for manual row iteration and column-by-column scanning.

    Key Features

    • Custom database column names via struct tags.
    • Support for nested or embedded structs.
    • Support for NULL values and custom types.
    • Ability to omit specific struct fields.
    • Support for destination types like maps and Go primitives.
    • Ability to override default settings.

    Important Distinctions

    • Not an ORM: scany only works in one direction (Database $\rightarrow$ Go objects). It cannot build queries based on Go objects.
    • No Relation Management: It does not handle relationships between objects (e.g., one-to-many or many-to-many).
  2. Extend scany to other database libraries

    master
    For database libraries that do not implement database/sql or the pgx native interface, you can use the dbscan package. dbscan contains the core scanning logic and works with any library that provides a concept of rows. Both sqlscan and pgxscan are built on top of dbscan.
  3. Use scany with `pgx` native interface

    master

    If you are using the pgx library's native interface (e.g., pgxpool), use the pgxscan package. This provides the same scanning capabilities as sqlscan but is optimized for the pgx driver's native types and interfaces.

    package main
    
    import (
    	"context"
    
    	"github.com/jackc/pgx/v5/pgxpool"
    
    	"github.com/georgysavva/scany/v2/pgxscan"
    )
    
    type User struct {
    	ID    string
    	Name  string
    	Email string
    	Age   int
    }
    
    func main() {
    	ctx := context.Background()
    	db, _ := pgxpool.New(ctx, "example-connection-url")
    
    	var users []*User
    	pgxscan.Select(ctx, db, &users, `SELECT id, name, email, age FROM users`)
    	// users variable now contains data from all rows.
    }
  4. Use scany with `database/sql`

    master

    To scan database rows into Go structs using the standard database/sql library, use the sqlscan package. The sqlscan.Select function allows you to execute a query and automatically populate a slice of structs or a single struct without manual row iteration.

    package main
    
    import (
    	"context"
    	"database/sql"
    
    	"github.com/georgysavva/scany/v2/sqlscan"
    )
    
    type User struct {
    	ID    string
    	Name  string
    	Email string
    	Age   int
    }
    
    func main() {
    	ctx := context.Background()
    	db, _ := sql.Open("postgres", "example-connection-url")
    
    	var users []*User
    	sqlscan.Select(ctx, db, &users, `SELECT id, name, email, age FROM users`)
    	// users variable now contains data from all rows.
    }