https://github.com/dukex/ely
Run a api service inside the database
https://github.com/dukex/ely
api api-rest database postgresql serverless
Last synced: 27 days ago
JSON representation
Run a api service inside the database
- Host: GitHub
- URL: https://github.com/dukex/ely
- Owner: dukex
- Created: 2022-06-20T00:16:57.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-07-05T15:04:15.000Z (over 3 years ago)
- Last Synced: 2025-04-06T20:08:21.621Z (12 months ago)
- Topics: api, api-rest, database, postgresql, serverless
- Language: Go
- Homepage:
- Size: 7.71 MB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# ELY
ELY is a toy project, please does not use it in production yer, the main idea is run a api service inside the database, a generic golang server is up but the functions to handle the endpoint is running in the any language supported by postgresql
## Installation
Install ELY command package
```
$ go install github.com/dukex/ely/cmd/ely@latest
```
Now you can use `ely` command
```
$ ely
ELY is a tool for create a API service using database functions
Usage:
ely [command]
Available Commands:
completion Generate the autocompletion script for the specified shell
db Manage ELY database
deploy Deploy functions to database
help Help about any command
server Run the ELY server
Flags:
-h, --help help for ely
Use "ely [command] --help" for more information about a command.
```
## Usage
The ELY requires a up and running postgresql database, all commands depends of `DATABASE_URL` env proper configured.
```
export DATABASE_URL=postgresql://localhost:5433/ely?sslmode=disable
```
### Create the workdir
Let's to create your project directory
```
$ mkdir -p awesome-db-api/functions
```
### Setup database
Now we need setup the database, we will create the ELY types
```
$ ely db setup
```
### Create functions
Let's to create the first and simpler function, the healthy status endpoint
```
$ touch functions/healthy.sql
```
Open the `functions/healthy.sql` file and put the follow content:
```
CREATE OR REPLACE FUNCTION healthy()
RETURNS http_response
AS $$
ely = GD['ely']
render_json = ely['render_json']
return render_json({ "healthy": True });
$$ LANGUAGE plpython3u;
```
This functions basically will responds the HTTP Status 200, with a JSON `{ "healthy": true }`.
Now let's to deploy the function to the database:
```
$ ely deploy
```
Done! Your functions is ready to be used.
### Setup an endpoint
Let's to create a YAML to configure your API
```
$ touch ely.yml
```
This will map the endpoints with the database functions, given we created the `healthy` previously let's to map this function to a endpoint. Edit the `ely.yml` as:
```yaml
endpoint:
- path: "/health_status"
function: 'healthy'
```
### Up and running server
To finish, we should up the server
```
$ ely server
Server starring at 9001
```
Now you can access [localhost:9001/health_status](http://localhost:9001/health_status)
## Complex examples
**You can see more examples in the [examples](examples/) directory.**
Let's to create a users index endpoint
```
$ touch functions/userindex.sql
```
Open the `functions/userindex.sql` file and put the follow content:
```
CREATE OR REPLACE FUNCTION usersindex()
RETURNS http_response
AS $$
ely = GD['ely']
render_json = ely['render_json']
params = ely['params']
enabled = params("enabled")
plan = plpy.prepare("SELECT * FROM users WHERE enabled = $1", [ "boolean" ])
users = plpy.execute(plan, [ enabled != 'false' ])
return render_json([dict(user) for user in users])
$$ LANGUAGE plpython3u;
```
And in the `ely.yml` file:
```
endpoint:
- path: "/users"
function: 'usersindex'
````
Run server:
```
$ ely server
```
Access the endpoint [localhost:9001/users](http://localhost:9001/users). You can send the enabled query string params to see different results [localhost:9001/users?enabled=false](http://localhost:9001/users?enabled=false)