mongoclt

package module
v0.0.0-...-057f537 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

README

mongo-clt-go-api

Client API for accessing mongodb

mongo-clt-go-api is a golang implementation for accessing mongodb. This api is a simplified wrapper around "go.mongodb.org/mongo-driver" packages.

Installation

Install mongo-clt-go-api using go:

    go get github.com/bluelamar/mongo-clt-go-api

Usage

To use mongo-clt-go-api, import the package and create a new client with options. This client uses SCRAM authentication with mongodb.

import "github.com/bluelemar/mongo-clt-go-api"

clt, err := mongoclt.NewClient(
	mongoclt.ClientHostPort("127.0.0.1:7000"),
	mongoclt.ClientHostPort("127.0.0.1:7001"),
	mongoclt.ClientDbUser("my_mongo_user"),
	mongoclt.ClientDbPasswd("my_mongo_password"),
	mongoclt.ClientDbName("my_database"),
	mongoclt.ClientDbAuthDbName("my_database"),
	mongoclt.ClientCommTimeout(50 * time.Millisecond)
)

The mongoclt api will make a field called "key" by default when creating any entry in the db. The key field is used for doing reads of data entries. The user may change the name of the the key field from the default key to one of their choosing with the following:

func SetKeyFieldName(keyName string)

The mongoclt api allows normalization of errors returned to the calling application. The errors to be translated are those returned by mongodb. The normalized errors can be set by the following function. Note that the mongoErrStr need all be a partial message, you only need specify enough to make it unique for the error you will normalize.

func SetErrorMap(mongoErrStr, normalizedErrStr string)

Run Test

Run mongodb and initialize with your users and database.

Start mongodb then copy paste the mongo db commands.

Start the server and connect to it with the mongo command shell:

scripts/initdb.sh

Copy and paste the command sets within the command shell:

First copy/paste the Admin user setup:

use testdb
db.createUser(
  {
    user: "myadmin",
    pwd: "mypassword",
    roles: [
       { role: "read", db: "admin" },
       { role: "readWrite", db: "testdb" }
    ]
  }
)

Next copy/paste the database index setup for a collection called "rooms":

use testdb
db.rooms.createIndex( { "key": 1 }, { unique: true } )

Lastly, copy/paste some test data:

db.rooms.insertOne(
{
key: "306",
RoomNum:"306",
BedSize:"Twin",
NumBeds:"1",
RateClass:"STANDARD ROOM"
})

Run mongodb with kubernetes

FIX TODO how to init the db with the user+passwd

Documentation

Overview

* Copyright 2020 Mark Lakes * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetErrorMap

func SetErrorMap(mongoErrStr, normalizedErrStr string)

SetErrorMap allows user to create normalized errors for their apps to handle without exposing underlying mongodb specific errors

func SetKeyFieldName

func SetKeyFieldName(keyName string)

SetKeyFieldName allows user to over-ride the default name of the "key" field

Types

type Client

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

Client used for the mongo client api

func NewClient

func NewClient(coptions ...ClientOption) (*Client, error)

NewClient creates a new mongo client using the specified options

func (*Client) Create

func (clt *Client) Create(entity, keyValue string, valueEntry map[string]interface{}) (*map[string]interface{}, error)

Create or insert a new entry into the collection entity

func (*Client) Delete

func (clt *Client) Delete(entity, id string) error

Delete the specified entry

func (*Client) Find

func (clt *Client) Find(entity, field, value string) ([]interface{}, error)

Find entry for specified entity where value matches the value in the field If field and value are empty, then return all entries for the specified entity

func (*Client) Read

func (clt *Client) Read(entity, keyValue string) (*map[string]interface{}, error)

Read the entry for specified entity and key

func (*Client) ReadAll

func (clt *Client) ReadAll(entity string) ([]interface{}, error)

ReadAll entries into a slice of entries for the specified entity

func (*Client) Update

func (clt *Client) Update(entity, id string, valueEntry map[string]interface{}) error

Update the entry with contents of valueEntry matching the specified id If there is no id specified, it will try to use the key from the valueEntry, else _id field

type ClientOption

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

ClientOption specifies an option for connecting to a mongodb server

func ClientAuthDbName

func ClientAuthDbName(name string) ClientOption

ClientAuthDbName specifies the name of auth database containing the user when connecting to the server Optional: This was required on Ubuntu but not on MAC

func ClientCommTimeout

func ClientCommTimeout(timeOut int) ClientOption

ClientCommTimeout specifies the timeout for communicating to the mongo server

func ClientDbName

func ClientDbName(name string) ClientOption

ClientDbName specifies the name of database to write or read data from

func ClientDbPasswd

func ClientDbPasswd(passwd string) ClientOption

ClientDbPasswd specifies the password for user to access the database

func ClientDbUser

func ClientDbUser(user string) ClientOption

ClientDbUser specifies the user inwhich to access the database

func ClientHostPort

func ClientHostPort(hostPort string) ClientOption

ClientHostPort specifies the host and port inwhich to access the database ex: "127.0.0.1:27017" Supports sharded db - can call multiple times host+ports

Jump to

Keyboard shortcuts

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