ydb

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2024 License: Apache-2.0 Imports: 5 Imported by: 3

README

GORM YDB Driver

YDB support for GORM

License Release PkgGoDev tests lint Go Report Card codecov Code lines View examples Telegram WebSite

Quick Start

You can simply test your connection to your database with the following:

package main

import (
	ydb "github.com/ydb-platform/gorm-driver"
	"gorm.io/gorm"
)

type User struct {
	Name string `gorm:"primarykey"`
	Age  int
}

func main() {
	db, err := gorm.Open(ydb.Open("grpc://localhost:2136/local"))
	if err != nil {
		panic("failed to connect database")
	}

	// Auto Migrate
	db.AutoMigrate(&User{})

	// Insert
	db.Create(&User{Name: "Angeliz", Age: 18})

	// Select
	db.Find(&User{}, "name = ?", "Angeliz")

	// Batch Insert
	user1 := User{Name: "Charles", Age: 12}
	user2 := User{Name: "Feynman", Age: 13}
	user3 := User{Name: "Michael", Age: 14}
	users := []User{user1, user2, user3}
	db.Create(&users)

	// ...
}

Documentation

Overview

Example (Query)
package main

import (
	"context"
	"fmt"

	environ "github.com/ydb-platform/ydb-go-sdk-auth-environ"
	"gorm.io/gorm"

	ydb "github.com/ydb-platform/gorm-driver"
)

func main() {
	type Product struct {
		ID    uint `gorm:"primarykey;not null;autoIncrement:false"`
		Code  string
		Price uint `gorm:"index"`
	}

	db, err := gorm.Open(
		ydb.Open("grpc://localhost:2136/local",
			ydb.With(environ.WithEnvironCredentials()),
		),
	)
	if err != nil {
		panic(err)
	}

	db = db.Debug()

	// Migrate the schema
	err = db.AutoMigrate(&Product{})
	if err != nil {
		panic(err)
	}

	// Create
	err = db.Create(&Product{ID: 1, Code: "D42", Price: 100}).Error
	if err != nil {
		panic(err)
	}

	// Scan query
	var products []Product
	err = db.
		WithContext(ydb.WithQueryMode(context.Background(), ydb.ScanQueryMode)).
		Model(&Product{}).
		Scan(&products).
		Error
	if err != nil {
		panic(err)
	}

	// Read
	var product Product
	err = db.First(&product, 1).Error // find product with integer primary key
	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v\n", product)

	err = db.First(&product, "code = ?", "D42").Error // find product with code D42
	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v\n", product)

	// Update - update product's price to 200
	err = db.Model(&product).Update("Price", 200).Error
	if err != nil {
		panic(err)
	}

	// Update - update multiple fields
	err = db.Model(&product).Updates(Product{Price: 200, Code: "F42"}).Error // non-zero fields
	if err != nil {
		panic(err)
	}

	err = db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"}).Error
	if err != nil {
		panic(err)
	}

	// Delete - delete product
	err = db.Delete(&product, 1).Error
	if err != nil {
		panic(err)
	}

	// Drop table
	err = db.Migrator().DropTable(&Product{})
	if err != nil {
		panic(err)
	}
}
Output:

Index

Examples

Constants

View Source
const (
	DataQueryMode      = ydb.DataQueryMode
	ExplainQueryMode   = ydb.ExplainQueryMode
	ScanQueryMode      = ydb.ScanQueryMode
	SchemeQueryMode    = ydb.SchemeQueryMode
	ScriptingQueryMode = ydb.ScriptingQueryMode
)

Variables

This section is empty.

Functions

func Open

func Open(dsn string, opts ...Option) gorm.Dialector

func WithQueryMode added in v0.2.0

func WithQueryMode(ctx context.Context, mode QueryMode) context.Context

Types

type Option

type Option = dialect.Option

func With

func With(opts ...ydb.Option) Option

func WithConnMaxIdleTime added in v0.1.0

func WithConnMaxIdleTime(d time.Duration) Option

func WithMaxIdleConns added in v0.1.0

func WithMaxIdleConns(n int) Option

func WithMaxOpenConns added in v0.1.0

func WithMaxOpenConns(n int) Option

func WithTablePathPrefix

func WithTablePathPrefix(tablePathPrefix string) Option

type QueryMode added in v0.2.0

type QueryMode = ydb.QueryMode

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL