Skip to content

skamenetskiy/database

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

database

Simple postgres wrapper

Getting started

Single node

db, err := database.Connect(context.Background(), "postgres://localhost:5432/mydatabase")
if err != nil {
  panic(err)
}

Cluster

c, err := database.NewClusterFromFile(context.Background(), "sample.config.yaml")
if err != nil {
	panic(err)
}

err := c.EveryShard(func(shard database.Shard) error {
    _, qErr := shard.S().Exec(`
    create table if not exists accounts (
        id bigint not null default next_id('accounts') primary key,
        name text not null
    )
`)
    return qErr
})
if err != nil {
    panic(err)
}

shard := c.NextShard()
var id uint64
shard.S().QueryRow(`insert into accounts (name) values ($1) returning id`, "John").Scan(&id)
fmt.Println(id)

shard = c.ShardByKey(id)
if shard != nil {
	var name string
	shard.S().QueryRow(`select name from accounts where id = $1`, id).Scan(&name)
	fmt.Println(name)
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Languages