

GitHub - alash3al/sqler: write APIs using direct SQL queries with no hassle, let...
source link: https://github.com/alash3al/sqler
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md
SQLer
SQL-er
is a tiny http server that applies the oldCGI
concept but forSQL
queries, it enables you to write an endpoint and assign a SQL query to be executed when anyone hits it, also it enables you to define validation rules so you can validate the request body/query params.sqler
usesnginx
style configuration language (HCL
).
Features
- Standalone with no dependencies.
- Works with most of SQL databases out there including (
SQL Server
,MYSQL
,SQLITE
,PostgreSQL
,Cockroachdb
) - Built-in Validators
- Autmatically uses prepared statements
- Uses (
HCL
) configuration language - You can load multiple configuration files not just one, based on
unix glob
style pattern - Each
SQL
query could be named asMacro
- You can use
Go
text/template
within each macro - Each macro have its own
Context
(query params
+body params
) as.Input
which ismap[string]interface{}
, and.Utils
which is a list of helper functions, currently it contains onlySQLEscape
. - You can define
authorizers
, anauthorizer
is just a simple webhook that enablessqler
to verify whether the request should be done or not.
Download
- FromSource:
go get github.com/alash3al/sqler
- BinaryRelease: go to releases page
Configuration Overview
// create a macro/endpoint called "_boot", // this macro is private "used within other macros" // because it starts with "_". _boot { // the query we want to execute exec = <<SQL CREATE TABLE IF NOT EXISTS `users` ( `ID` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(30) DEFAULT "@anonymous", `email` VARCHAR(30) DEFAULT "@anonymous", `password` VARCHAR(200) DEFAULT "", `time` INT UNSIGNED ); SQL } // adduser macro/endpoint, just hit `/adduser` with // a `?user_name=&user_email=` or json `POST` request // with the same fields. adduser { // what request method will this macro be called // default: ["ANY"] methods = ["POST"] // authorizers, // sqler will attempt to send the incoming authorization header // to the provided endpoint(s) as `Authorization`, // each endpoint MUST return `200 OK` so sqler can continue, other wise, // sqler will break the request and return back the client with the error occured. // each authorizer has a method and a url. // authorizers = ["GET http://web.hook/api/authorize", "GET http://web.hook/api/allowed?roles=admin,root,super_admin"] // the validation rules // you can specifiy seprated rules for each request method! rules { user_name = ["required"] user_email = ["required", "email"] user_password = ["required", "stringlength: 5,50"] } // the query to be executed exec = <<SQL {{ template "_boot" }} /* let's bind a vars to be used within our internal prepared statment */ {{ .BindVar "name" .Input.user_name }} {{ .BindVar "email" .Input.user_email }} {{ .BindVar "emailx" .Input.user_email }} INSERT INTO users(name, email, password, time) VALUES( /* we added it above */ :name, /* we added it above */ :email, /* it will be secured anyway because it is encoded */ '{{ .Input.user_password | .Hash "bcrypt" }}', /* generate a unix timestamp "seconds" */ {{ .UnixTime }} ); SELECT * FROM users WHERE id = LAST_INSERT_ID(); SQL } proclist { exec = "SHOW PROCESSLIST" } tables { exec = "SELECT * FROM information_schema.tables" } databases { exec = "SHOW DATABASES" }
Supported SQL Engines
sqlite3
mysql
postgresql
cockroachdb
sqlserver
Supported Validation Rules
Supported Uitls
.Hash <method>
- hash the specified input using the specified method [md5, sha1, sha256, sha512, bcrypt],{{ "data" | .Hash "md5" }}
.UnixTime
- returns the unix time in seconds,{{ .UnixTime }}
.UnixNanoTime
- returns the unix time in nanoseconds,{{ .UnixNanoTime }}
.Uniqid
- returns a unique id,{{ .Uniqid }}
License
Copyright 2019 The SQLer Authors. All rights reserved. Use of this source code is governed by a Apache 2.0 license that can be found in the LICENSE file.
Recommend
-
55
a super simple stupid event-loop kernel in pure PHP
-
43
README.md Redix a very fast persistent pure key - value store, that uses the same RESP prot...
-
47
a persistent real-time key-value store, with the same redis protocol with powerful features - alash3al/redix
-
10
re-txt reformates a text file from a structure to another, i.e: convert from json to yaml, toml to json, ... etc Supported Source Formats Supported Target Formats Examples
-
6
xyr [WIP] xyr is a very lightweight, simple, and powerful data ETL platform that helps you to query available data sources using SQL. Example here we d...
-
5
Redix v5 redix is a very simple key => value storage engine that speaks redis and even more simpler and flexible. Why did I build this? redis
-
8
About a very simple, tiny and intuitive ffmpeg wrapper with a cli interface for inspecting & transforming media files supported by the original ffmpeg software. I wanted to learn mo...
-
9
Simple Scraping Tool Scraply, is a very simple html scraping tool, if you know css & jQuery then you can use it! Overview you can use scraply within your stack via
-
8
katch! a very simple wrapper utility for headless chrome to easily export any webpage as png, jpeg, pdf or html (prerender), you can use it via http or...
-
6
alash3al/phoo master
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK