nosqlorm

package module
v0.1.6-alpha Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: BSD-3-Clause Imports: 12 Imported by: 0

README

NosqlOrm

A lightweight Golang ORM tailored for NoSQL databases, featuring built-in mock support. Currently, it supports Cassandra, with plans to extend compatibility to additional databases in the future.

Get Start

go get github.com/Tonyzhuwei/nosqlorm

Usage

Define Models

type Person struct {
Name    string `json:"name" cql:"pk"`
Age     int8   `json:"age" cql:"ck"`
Address string `json:"address"`
}

Migrate Tables

// Create Cassandra connect session.
clustser := gocql.NewCluster("localhost:9042")
clustser.Keyspace = "cqlorm"
sess, err := clustser.CreateSession()
if err != nil {
    panic(err)
}

// Create tables if not existing
nosqlorm.MigrateCassandraTables(sess, Person{})

CRUD

personCtx := nosqlorm.NewCqlOrm[Person](sess)
result, err := personCtx.Select(Person{
    Name: "tony",
    Age:  30,
})

Mock DB Access

var testPerson = Person{
	Name:    "Tony",
	Age:     30,
	Address: "this is a test address",
}

sess := nosqlorm.NewMockSession(t)
mockPersonTable := nosqlorm.NewMockTable[Person](sess)
mockPersonTable.AddSelectExpectation(testPerson, []Person{testPerson})

mockPersonTable.Select(testPerson)

Missing Parts:

  • Enhance performance through cache utilization.
  • Facilitate transactions across multiple tables simultaneously.
  • Enhance mock functionality to include 'ignore' and other features.
  • Optimize data scanning processes.
  • Implement comprehensive unit test cases.
  • Enable batch methods and corresponding mock support.
  • Incorporate logging capabilities, record low-performance CQL queries.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateCassandraTables

func CreateCassandraTables(sess *gocql.Session, tables ...interface{}) error

Auto create or update table for Cassandra

func GetPointer

func GetPointer[T any](val T) *T

func NewCqlOrm

func NewCqlOrm[T interface{}](session *gocql.Session) (*cqlOrm[T], error)

NewCqlOrm Create a new object to access specific Cassandra table

Types

type MockSession

type MockSession struct {
	Test         *testing.T
	Expectations []interface{}
	// contains filtered or unexported fields
}

func NewMockSession

func NewMockSession(t *testing.T) *MockSession

func (*MockSession) AddIdx

func (m *MockSession) AddIdx()

type MockTable

type MockTable[T interface{}] struct {
	// contains filtered or unexported fields
}

func NewMockTable

func NewMockTable[T interface{}](sess *MockSession) *MockTable[T]

func (*MockTable[T]) AddOtherExpectation

func (m *MockTable[T]) AddOtherExpectation(input T, err error)

func (*MockTable[T]) AddSelectExpectation

func (m *MockTable[T]) AddSelectExpectation(input T, objs []T)

func (*MockTable[T]) Delete

func (m *MockTable[T]) Delete(obj T) error

func (*MockTable[T]) Insert

func (m *MockTable[T]) Insert(obj T) error

func (*MockTable[T]) Select

func (m *MockTable[T]) Select(obj T) ([]T, error)

func (*MockTable[T]) Update

func (m *MockTable[T]) Update(obj T) error

type NoSqlOrm

type NoSqlOrm[T any] interface {
	Insert(T) error
	Select(T) ([]T, error)
	Update(T) error
	Delete(T) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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