New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

gitlab.com/package-sh/couchdbutils

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gitlab.com/package-sh/couchdbutils

  • v1.6.3
  • Source
  • Go
  • Socket score

Version published
Created
Source

couchbaseUtilsGo

Package to manage couchbaseDB

Install the package go

go get gitlab.com/package-sh/couchdbutils

EpressRouterController

EpressRouterController is a function that creates a router for a given entity

The function will create the following routes:

  • GET /baseURL with params: currentPage, pageSize, whereQuery, orderByQuery
  • GET /baseURL/search with params: query
  • GET /baseURL/{id}
  • PUT /baseURL/{id}
  • DELETE /baseURL/{id}
  • POST /baseURL

GET /baseURL

Return all entities with pagination

Exemple of use:

/baseURL?currentPage=1&pageSize=10&whereQuery=field1==value1|field2<in>value2,value2&orderByQuery=-field1
  • Separator for parameter whereQuery:
    |
  • Operator available for string comparaison:
   =, !=, <, >, <=, >=, <in>, !<in>
  • Operator available for number comparaison:
    ==, !==, <<, <<=, >>=, <<in>>, !<<in>>
  • Separator for IN operator:
,

GET /baseURL/search

Return the entities that match the search

  • Exemple of use: /baseURL/search?query=example

GET /baseURL/{id}

Return the entity with the given id

POST /baseURL

Create a new entity

PUT /baseURL/{id}

Update the entity with the given id

DELETE /baseURL/{id}

Delete the entity with the given id

Exemple of Use ExpressRouterController

Creating of User Entity & DTO


    type UserEntity{
        UserId string `json:"user_id"`
        SessionId string `json:"user_session_id"`
        Name string `json:"user_name"`
        LastName string `json:"user_lastname"`
        UserName string `json:"user_username"`
        Password string `json:"user_password"`
    }

We choose to not show the password attribut throw the API

    type UserDTO{
        UserId string `json:"user_id"`
        SessionId string `json:"user_session_id"`
        Name string `json:"user_name"`
        LastName string `json:"user_lastname"`
        UserName string `json:"user_username"`
    }

Init the variable

    //The cluster couchBase object
    var Cluster *gocb.Cluster 

    	cluster, err = gocb.Connect(
		clusterIP,
		gocb.ClusterOptions{
			Username: clusterAdmin,
			Password: pwdCluster,
		},
	)

    // the user couchBase object
    var userCollection *gocb.Collection 
    
    userCollection= Bucket.Scope(scope).Collection("userList"),

Create the function hydrateEnties

This function will This function is called when a user entity is create of update The goal of this function is to set the right information in the entite object


    hydateEntites := func(r *http.Request, id string, newUser *entities.UserEntity, isUpdate bool) error {

		// Check if the writing is an update or a new document
		if !isUpdate {
			// if is not update > then is a creation, we set the new id generated to the entity
			neUser.UserId = id
		}
		// ====================== Check Validity of the entity - BEGIN ======================
		// Check if the body is empty
		if newUser == nil {
			// if the body is empty, we return an error > then the entity be not saved
			return couchdbUtils.NewError(400, "Body is required")
		}

		// ====================== Check Validity of the entity - END  ======================

		// If all is ok, we return nil > then the entity will be saved
		return nil
	}

Create a GetPreWhereQueryGet (Optional)

This function is called when a Get is requested This function add mandarory where condition

    getPreWhereQuery :=func(r *http.Request) []string {

        mandatoryWhereConditionList := []string{}
        
        userId := context.Get(r, "user")
        userIdFormated := userId.(string)

        //Here we want just all users of the session
        mandatoryWhereConditionList = append( whereConditionList , "user_session_id = '"+userIdFormated+"'")
		return whereConditionList
	}

Initialise the expressRouterConfig object

	expressRouterConfig := couchdbUtils.RouterConfig[entities.UserEntity, entities.UserDto]{
		Cluster:         Cluster,
		Router:          router,
		BaseURL:         "/users",
        // the name of the id  attribute of the entity
		IdKey:           "user_id",
		Collection:      UserCollection,

		AuthMiddleware:  nil,

		WithMiddleware:  false,

		HydrateEntities: hydateEntites,
        // the method to not expose
		BlackListMethod: []string{"PUT", "POST", "DELETE"},
        
        GetPreWhereQueryGet: getPreWhereQuery,

	}

Run the ExpressRouterController

    couchdbUtils.ExpressRouterController[entities.UserEntity, entities.UserDto](expressRouterConfig)

Repository

FAQs

Package last updated on 10 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc