What is Scaffold ?
Scaffold is a powerful HTTP API generator designed to streamline building CRUD APIs. It abstracts away an entire codebase into a CLI and one file, eliminating the need to carry around massive Git repositories just to serve some SQL data. Your entire Scaffold codebase is configured using YAML and embedded SQL. It's the perfect tool for prototyping or writing simple backends quickly. Github link
Getting started with Scaffold
All you need to follow along is the Scaffold CLI, which you can get from the official Github repository here.
Note: You'll want to make a symlink to it so you can call it from anywhere.
First Scaffold backend
It's time to get some hands-on experience with Scaffold, so we're going to write a simple backend that allows us to do two things: add and select a user.
First, we're going to want to initialize your project. This is where the CLI comes in handy.
Scaffold init Project
cd Project
Now we can change the API endpoint's editing the main.yml
file.
Defining our first component
Scaffold utilizes pre-defined building blocks known as components to shape the API. Our initial focus will be on the server component, where we specify the port and services.
server:
port: 8080
$service:
Defining controllers
While our application runs on port 8080, it lacks routing and functionality. Enter the controller component. We'll define two controllers, each with specified properties: name, fallback, and model.
$controller:
- name: get_user_controller
fallback: Something went wrong
model: get_user_model
- name: add_user_controller
fallback: Something went wrong
model: add_user_model
Now, let's attach these controllers to the server, establishing routes and assigning controllers to them.
server:
port: 8080
$service:
- route: /get-user
controller: get_user_controller
- route: /add-user
controller: add_user_controller
Defining data logic
With routing configured, it's time to set up the database using the database component. This component requires two properties: init-query and path, where the database should reside. Scaffold leverages SQLite as an embedded database.
database:
init-query: |
CREATE TABLE IF NOT EXISTS user (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER
);
path: ./main.db
Finally, complete the API by defining models for the controllers to utilize. Each model comprises three properties: name, a query template with %s
placeholders, and a JSON template specifying field names and types.
$model:
- name: add_user_model
query-template: INSERT INTO user (name, age) VALUES ('%s', %s)
json-template:
- Name: Name
Type: string
- Name: Age
Type: integer
- name: get_user_model
query-template: SELECT * FROM user WHERE name='%s'
json-template:
- Name: Name
Type: string
Completed code
The completed code should look like this and the two endpoints will be open at http://localhost:8080/get-user and http://localhost:8080/add-user
database:
init-query: |
CREATE TABLE IF NOT EXISTS user (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER
);
path: ./main.db
$model:
- name: add_user_model
query-template: INSERT INTO user (name, age) VALUES ('%s', %s)
json-template:
- Name: Name
Type: string
- Name: Age
Type: integer
- name: get_user_model
query-template: SELECT * FROM user WHERE name='%s'
json-template:
- Name: Name
Type: string
$controller:
- name: get_user_controller
fallback: Something went wrong
model: get_user_model
- name: add_user_controller
fallback: Something went wrong
model: add_user_model
server:
port: 8080
$service:
- route: /get-user
controller: get_user_controller
- route: /add-user
controller: add_user_controller
You can now run this with
Scaffold run .
Conclusion
In conclusion, Scaffold emerges as a formidable tool for swiftly building CRUD APIs with remarkable efficiency and simplicity. By encapsulating an entire codebase into a single CLI tool and a YAML configuration file, Scaffold liberates developers from the burden of managing complex project structures and dependencies.
Top comments (0)