Golang HTTP CRUD+L: The Create Part
CRUD+L: Create, Read, Update, Delete, List
CRUDL is very common needed feature for relational database, so I created a Repo for it. This post will introduce the http wrapper in that Repo and the Create part
How do we achieve the automatically create db record from object and http handler for creation in Golang? The answer is SQLX, reflection, wrapper http handler
The CRUD Structure
You need to specify 5 things:
- Database object (DB mandatory ), crud doesn’t connect db for you (you can use this Source for MySQL wrapper).
- Table name (TableName mandatory ) where we inserting rows.
- Object generator (Object mandatory ) the object of corresponding struct that reflect the table model
- Enable http handler for Create part (C optional ).
- List of validators to validate input parameters (Validators Optional ).
The Object generator interface
The Object interface only has one method Get which is use for getting new object from the struct which is corresponding for the table we inserting rows. This apply the Prototype Design Pattern where we has a way to generate a prototype to reuse. This Object has type interface but the underlying type must be a pointer to struct which is use for getting the field and the crud tag for building the rest of CRUD structure.
The CRUD Tag
CRUD uses Golang custom tag “db”, this tag also use by SQLX
The custom tag use for setting up the options for each field. Example:
db:"field_name,pk,create,select,update,list,validator=name"
- field_name: the actual field name in database.
- pk: this field is primary key.
- create: this field is allowed for creation.
- select: this field is allowed for selection(read).
- update: this field is allowed for updating.
- list: this field is allowed for listing.
- validator: name of validator which is the key in Validators map, value of validator name is the right operand of validator.
The Input Validator Type
CRUD+L allows for validating input parameters when creating or updating data, it’s very generic when validator is a function type:
Validator function inputs:
- method: current running method (create, update)
- obj: the input object which is the interface returned from Get function of Object interface
Validator must assert obj to corresponding object structure then it can validate input freely
The Create Handler
We need a http handler for creation and a SQL template for creating row from data object, inside RegisterC function:
It will build the SQL create template which is have all the allowed creation fields and create a ServerRoute object for http handler
The Create Part
Finally create function will insert new row by using the Named Query of SQLX:
Specially if the primary key of object struct is int64 type and Table has Auto Increment key, it will set the primary key with the inserted value
Full working code can be found at https://github.com/hauxe/GoM/tree/master/crudl
Tips#1: Next Topic will be about The Read Part
Tips#2: We will have a topic about HTTP Wrapper which is designed for simplicity and Decorator design pattern