mongrel

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 12 Imported by: 0

README

mongrel

MongoDB Go Driver dependent connect/disconnect package

Install

go get github.com/awkwardjake/mongrel

Docker Compose example

Example MongoDB service configuration for your docker-compose.yml file

version: "3.9"

services:
  mongo:
    image: mongo
    container_name: backendDB
    env_file:
      - .env
    restart: always
    ports:
      - "${MONGODBPORT}:27017"
    environment:
      MONGO_INITDB_DATABASE: ${DBAPP}
      MONGO_INITDB_ROOT_USERNAME: ${ROOTDBUSER}
      MONGO_INITDB_ROOT_PASSWORD: ${ROOTDBPASSWORD}
    volumes:
      - ./scripts/userScript.js:/docker-entrypoint-initdb.d/user.js:ro
      - mongodb_data_container:/usr/apps/exampleDB/database/mongo/db

volumes:
  mongodb_data_container:
Create a user JavaScript

Add this user create JavaScript file to project directory and reference it in docker-compose.yml. If using example above, it would be ./scripts/userScript.js

Modify JS to suit needs in terms of user credentials and permissions within MongoDB

db.createUser(
    {
        user:"appUser",
        pwd:"appUserPass",
        roles: 
        [
            {
                role: "readWrite", 
                db: "test"
            }
        ]
    }
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MongoConnectDetails

type MongoConnectDetails struct {
	Username   string `json:"username"`
	Password   string `json:"password"`
	Host       string `json:"host"`
	Port       int    `json:"port"`
	AuthSource string `json:"authSource"`
	App        string `json:"app"`
	// contains filtered or unexported fields
}

MongoConnectDetails model

func (*MongoConnectDetails) Connect

func (mongoConnectDetails *MongoConnectDetails) Connect(requestContext context.Context) error

Connect function to mongo that accepts MongoConnectDetails struct

func (*MongoConnectDetails) CreateDocument

func (mongoConnectDetails *MongoConnectDetails) CreateDocument(requestContext context.Context, document interface{}) (interface{}, error)

CreateDocument

func (*MongoConnectDetails) CreateIndex

func (mongoConnectDetails *MongoConnectDetails) CreateIndex(requestContext context.Context, field string, opts ...string) error

CreateIndex expects a single field and optionall a desired sort order ("asc" or "desc") for nested fields, pass a string that traverses fields using "."

func (*MongoConnectDetails) DeleteDocument

func (mongoConnectDetails *MongoConnectDetails) DeleteDocument(requestContext context.Context, field string, id string) (int, error)

DeleteDocument expects request context in addition to specifying the field and identifier for the target document to delete

func (*MongoConnectDetails) Disconnect

func (mongoConnectDetails *MongoConnectDetails) Disconnect() error

func (*MongoConnectDetails) GetDocumentByID

func (mongoConnectDetails *MongoConnectDetails) GetDocumentByID(requestContext context.Context, field string, id string) (interface{}, error)

GetDocument will retrieve a single document by provided "id" field returned interface{} value can be Marshalled and Unmarshalled as needed

func (*MongoConnectDetails) InsertManyDocuments

func (mongoConnectDetails *MongoConnectDetails) InsertManyDocuments(requestContext context.Context, documents []interface{}) (int, error)

func (*MongoConnectDetails) ListDatabases

func (mongoConnectDetails *MongoConnectDetails) ListDatabases(requestContext context.Context) ([]string, error)

ListDatabases returns database list

func (*MongoConnectDetails) SearchDocumentsByField

func (mongoConnectDetails *MongoConnectDetails) SearchDocumentsByField(requestContext context.Context, field string, term string) ([]interface{}, error)

SearchDocument expects request context and relies on a field name and single term string to find documents

func (*MongoConnectDetails) SelectCollection

func (mongoConnectDetails *MongoConnectDetails) SelectCollection(dbName string, collectionName string)

func (*MongoConnectDetails) UpdateDocumentByID

func (mongoConnectDetails *MongoConnectDetails) UpdateDocumentByID(requestContext context.Context, field string, id string, update interface{}) (int, error)

Jump to

Keyboard shortcuts

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