nosql

package
v0.0.0-...-2552c51 Latest Latest
Warning

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

Go to latest
Published: May 29, 2023 License: MIT Imports: 4 Imported by: 0

README

nosql

part of package go-tools

Documentation

Overview

Package nosql makes from SQL database to NoSQL. Source database must support json type

This tool allows to call database functions with input json parameter of different types - raw bytes ([]byte) and object (interface{}) and also returns raw bytes or object depending of use

Can be used with any sql database that supports json type.

Example (Basic)
package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"

	"git.ali33.ru/fcg-xvii/go-tools/database/nosql"
	"github.com/jackc/pgx/v5"
)

var (
	dbConn string
)

func init() {
	// read postgres connection string from file z_data.config
	connSource, _ := ioutil.ReadFile("z_data.config")
	dbConn = string(connSource)
	log.Println("DB connection string", dbConn)
}

func main() {
	// As a database will use postgres and plv8 (https://plv8.github.io/)
	// On the database side, you must create a function that counts the number of elements in the input array
	// As input argument function accept json object { "input": array } and will return object { "input": array, output: count }
	//
	// CREATE OR REPLACE FUNCTION public.arr_count(data jsonb)
	// RETURNS jsonb AS
	// $BODY$
	//   if(typeof(data.input) != 'object' || !data.input.length) {
	//	   plv8.elog(ERROR, 'Incoming data must be array')
	//   }
	//   data.output = data.input.length
	//   return data
	// $BODY$
	// LANGUAGE plv8 IMMUTABLE STRICT

	// dbConn read from config file before
	// example dbConn string: postgres://postgres:postgres@127.0.0.1/postgres?sslmode=disable&port=5432

	// open the postgres database
	db, err := pgx.Connect(context.Background(), dbConn)
	if err != nil {
		fmt.Println(err)
		return
	}

	// setup open new database transaction method
	openTX := func(ctx context.Context) (pgx.Tx, error) {
		return db.Begin(ctx)
	}

	// create api object
	api := nosql.New(openTX)

	// setup request data
	data := map[string]interface{}{
		"input": []int{1, 2, 3},
	}

	// call the function on database side
	result, err := api.Call(context.Background(), "public.arr_count", data)
	if err == nil {
		fmt.Println(err)
		return
	}

	// request completed
	fmt.Println(result)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NoSQL

type NoSQL struct {
	// contains filtered or unexported fields
}

NoSQL object

func New

func New(openMethod OpenTXMethod) *NoSQL

New is NoSQL object constructor

func (*NoSQL) Call

func (_self *NoSQL) Call(ctx context.Context, function string, data interface{}) (res interface{}, err error)

Call accepts interface{} object and returns result interface{}

func (*NoSQL) CallJSON

func (_self *NoSQL) CallJSON(ctx context.Context, function string, rawJSON []byte) (resRawJSON []byte, err error)

CallJSON accepts raw json bytes and returns result raw json bytes

func (*NoSQL) CallObjParam

func (_self *NoSQL) CallObjParam(ctx context.Context, function string, data interface{}) (resRawJSON []byte, err error)

CallObjParam accepts interface{} object and returns result raw json bytes

func (*NoSQL) CallObject

func (_self *NoSQL) CallObject(ctx context.Context, function string, data interface{}, resultObject interface{}) (err error)

type OpenTXMethod

type OpenTXMethod func(context.Context) (pgx.Tx, error)

OpenTXMethod is callback functon to open transaction in NoSQL object

Jump to

Keyboard shortcuts

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