mgc

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2019 License: Apache-2.0 Imports: 12 Imported by: 4

README

Datastore Connectivity for MongoDB(mgc)

Datastore Connectivity library for MongoDB in Go. GoDoc

This library is compatible with Go 1.10+

Please refer to CHANGELOG.md if you encounter breaking changes.

Usage:

The following is a very simple example of CRUD operations

package main

import (
	"github.com/viant/dsc"
    _ "github.com/adrianwit/mgc"
)


type User struct {
	Id int	`autoincrement:"true"`
	Name string
}


func main() {

	config, err := dsc.NewConfigWithParameters("mgc", "", "", map[string]interface{}{
    		"host":   "127.0.0.1",
    		"dbname": "mydb",
    		"keyColumnName":"id",
    })
	if err != nil {
		log.Fatal(err)
    }
	factory := dsc.NewManagerFactory()
	manager, err := factory.Create(config)
    if err != nil {
    	log.Fatal(err)
    }
    }
    
    var users []*User; // 
   
	inserted, updated, err:= manager.PersistAll(&users, "users", nil)
	if err != nil {
       log.Fatal(err)
   	}

    
    err:= manager.ReadAll(&users, "SELECT id, name FROM users WHERE id IN(?, ?)", []interface{}{1, 10},nil)
	 if err != nil {
         log.Fatal(err)
     }

   
  
    deleted, err := manager.DeleteAll(&users, "users", nil)
    if err != nil {
        log.Fatal(err)
   	}
  
}

License

The source code is made available under the terms of the Apache License, Version 2, as stated in the file LICENSE.

Individual files may be made available under their own specific license, all compatible with Apache License, Version 2. Please see individual files for details.

Credits and Acknowledgements

Library Author: Adrian Witas

Contributors:

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DbPointer = (*mgo.Database)(nil)
View Source
var OperatorMapping = map[string]CriterionProvider{

	"=": func(value interface{}) (interface{}, error) {
		return map[string]interface{}{
			"$eq": value,
		}, nil
	},
	"!=": func(value interface{}) (interface{}, error) {
		return map[string]interface{}{
			"$ne": value,
		}, nil
	},

	"<": func(value interface{}) (interface{}, error) {
		return map[string]interface{}{
			"$lt": value,
		}, nil
	},
	"<=": func(value interface{}) (interface{}, error) {
		return map[string]interface{}{
			"$lte": value,
		}, nil
	},
	">": func(value interface{}) (interface{}, error) {
		return map[string]interface{}{
			"$gt": value,
		}, nil
	},
	">=": func(value interface{}) (interface{}, error) {
		return map[string]interface{}{
			"$gte": value,
		}, nil
	},

	"IN": func(value interface{}) (interface{}, error) {
		if toolbox.IsString(value) {
			value = strings.Split(toolbox.AsString(value), ",")
		}
		if toolbox.IsSlice(value) {
			fmt.Errorf("expected slice but had: %T", value)
		}
		return map[string]interface{}{
			"$in": toolbox.AsSlice(value),
		}, nil
	},
	"NOT IN": func(value interface{}) (interface{}, error) {
		if toolbox.IsString(value) {
			value = strings.Split(toolbox.AsString(value), ",")
		}
		if toolbox.IsSlice(value) {
			fmt.Errorf("expected slice but had: %T", value)
		}
		return map[string]interface{}{
			"$nin": value,
		}, nil
	},
}
View Source
var SessionPointer = (*mgo.Session)(nil)

Functions

func AsMongoCriteria

func AsMongoCriteria(sqlCriteria *dsc.SQLCriteria, parameters []interface{}) (map[string]interface{}, error)

func MapCriterion

func MapCriterion(operator string, value interface{}, parameters []interface{}, paramIndex *int) (interface{}, error)

Types

type CriterionProvider

type CriterionProvider func(value interface{}) (interface{}, error)

CriterionProvider represents criteria provider

Jump to

Keyboard shortcuts

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