sqlcrud

package module
v0.0.0-...-251a2a4 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2019 License: MIT Imports: 5 Imported by: 0

README

go-sql-crud

GoDoc
🗄 quickly create simple crud style apis in go

import "github.com/TylerLafayette/go-sql-crud"

This package allows you to easily create super simple CRUD (create, read, update, and delete) APIs based around an SQL-supported database (using the built-in sql package).

Usage

Simply connect to your database, create a configuration, and create an http handler. The example below will create a route to search for a specific user in the dataabse.

package main

import (
	"database/sql"
	"errors"
	"log"
	"net/http"

	sqlcrud "github.com/TylerLafayette/go-sql-crud"
	// Postgres driver
	_ "github.com/lib/pq"
)

func main() {
	db, err := sql.Open("postgres", "user=tyler dbname=tyler sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}

	o := sqlcrud.Init{
		Database: db,
	}

	http.HandleFunc("/getUser", o.GetRow(sqlcrud.Options{
		Mode:  "GET",
		Table: "users",
		// The Placeholder can be any squirrel (sql query builder) compatible placeholder format
		Placeholder: sqlcrud.Dollar,
		QueryFields: []sqlcrud.Field{
			sqlcrud.Field{
				Name: "username",
				Validator: func(i interface{}) error {
					if len(i.(string)) < 4 || len(i.(string)) > 56 {
						// If the username is too short or too long, return an error to stop the request.
						return errors.New("username invalid")
					}

					return nil
				},
			},
		},
		Fields: []sqlcrud.Field{
			sqlcrud.Field{
				Name: "id",
			},
			sqlcrud.Field{
				Name: "username",
			},
			sqlcrud.Field{
				Name: "preferences",
			},
		},
	}))

	log.Fatal(http.ListenAndServe(":8080", nil))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Dollar is the SQL format usually used for Postgres.
	Dollar = sq.Dollar
	// Question is the SQL format usually used for MySQL.
	Question = sq.Question
)

Functions

This section is empty.

Types

type Field

type Field struct {
	InputName string
	Name      string
	Validator func(interface{}) error
	Formatter func(interface{}) (interface{}, error)
}

Field represents one field to query.

type Init

type Init struct {
	Database *sql.DB
}

Init contains a configuration for the database connection.

func (*Init) GetRow

func (i *Init) GetRow(o Options) func(w http.ResponseWriter, r *http.Request)

GetRow returns a handler to get one row from the database and return it to the user.

type Options

type Options struct {
	Mode        string
	Table       string
	Limit       uint64
	Placeholder squirrel.PlaceholderFormat
	QueryFields []Field
	Fields      []Field
}

Options contains options for a single handler.

type Response

type Response struct {
	Code    int         `json:"code"`
	Message string      `json:"msg"`
	Data    interface{} `json:"data"`
}

Response defines a single response.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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