database

package module
v1.1.3 Latest Latest
Warning

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

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

README

database

Database Engine for different SQL and NoSQL databases

Install

go get -u github.com/masudur-rahman/database

Quickstart

package main

import (
	"context"
	"time"

	"github.com/masudur-rahman/database/sql"
	"github.com/masudur-rahman/database/sql/sqlite"
	"github.com/masudur-rahman/database/sql/sqlite/lib"
)

type User struct {
	ID        int64     `db:"id,pk autoincr"`
	Name      string    `db:"name,uq"`
	FullName  string    `db:"full_name,uqs"`
	Email     string    `db:",uqs"`
	CreatedAt time.Time `db:"created_at"`
}

func main() {
	// Create sqlite connection
	conn, _ := lib.GetSQLiteConnection("test.db")

	// Start a database engine
	var db sql.Database
	db = sqlite.NewSqlite(context.Background(), conn)

	// Migrate database
	db.Sync(User{})

	db = db.Table("user")

	// Insert
	db.InsertOne(&User{Name: "masud", FullName: "Masudur Rahman", Email: "masud@example.com"})

	// Read
	var user User
	db.ID(1).FindOne(&user)
	db.Where("email=?", "masud@example.com").FindOne(&user)
	db.FindOne(&user, User{Name: "masud"})
	db.Columns("name", "email").FindOne(&user, User{Name: "masud"}) // fetch only name, email columns

	// Update
	db.ID(user.ID).UpdateOne(User{Email: "test@example.com"})
	db.Where("email=?", "test@example.com").UpdateOne(User{FullName: "Test User"})

	// Delete
	db.ID(1).DeleteOne()              // delete by id
	db.DeleteOne(User{Name: "masud"}) // delete using filter
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type UnitOfWork

type UnitOfWork struct {
	SQL   sql.Database
	NoSQL nosql.Database
}

UnitOfWork represents the unit of work for coordinating transactions

func (UnitOfWork) Begin

func (uow UnitOfWork) Begin() (UnitOfWork, error)

Begin starts a new transaction

func (UnitOfWork) Commit

func (uow UnitOfWork) Commit() error

Commit commits the transaction

func (UnitOfWork) Rollback

func (uow UnitOfWork) Rollback() error

Rollback rolls back the transaction

Directories

Path Synopsis
map
mock
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.
sql
mock
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.

Jump to

Keyboard shortcuts

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