Quick Start Guide
Welcome to your first steps with Platformatic DB. This guide will help you set up and run your first API using Platformatic DB with SQLite. By the end of this guide, you'll have a fully functional API ready to use.
While this guide uses SQLite, Platformatic DB also supports PostgreSQL, MySQL, and MariaDB. For more details on database compatibility, see the Platformatic DB documentation.
Prerequisitesβ
Before starting, ensure you have the following installed:
- Node.js (v18.8.0 or higher)
- npm (v7 or higher)
- A code editor, (e.g., Visual Studio Code)
Automatic Setup with Platformatic CLIβ
To start the Platformatic creator wizard, run the appropriate command for your package manager in your terminal:
- npm
- yarn
- pnpm
npm create platformatic@latest
yarn create platformatic
pnpm create platformatic@latest
This interactive command-line tool will guide you through setting up a new Platformatic project. For this guide, please choose the following options:
- Where would you like to create your project? => quick-start
- Which kind of project do you want to create? => @platformatic/db
- What is the name of the service? => (generated-randomly), e.g. legal-soup
- What is the connection string? => sqlite://./db.sqlite
- Do you want to create default migrations? => Yes
- Do you want to create another service? => No
- Do you want to use TypeScript? => No
- What port do you want to use? => 3042
- Do you want to init the git repository? => No
After completing the wizard, your Platformatic application will be ready in the quick-start
folder. This includes example migration files, plugin scripts, routes, and tests within your service directory.
If the wizard does not handle dependency installation, ensure to run npm/yarn/pnpm
install command manually:
Start Your API Serverβ
Run the following command in your project directory to start your API server:
- npm
- yarn
- pnpm
npm start
yarn run start
pnpm start
Your API server is now live! π It will automatically serve REST and GraphQL interfaces for your SQL database.
Check The Database Schemaβ
Navigate to the migrations
directory within the services
folder of your project directory. This folder contains your database migration files:
001.do.sql
: contains the SQL statements for creating database objects.001.undo.sql
: contains the SQL statements to remove database objects.
Check Your API configurationβ
Examine the platformatic.json
file in the services folder and the .env
file in the root of your project directory to confirm the API configuration:
This generated configuration tells Platformatic to:
- Run an API server on
http://0.0.0.0:3042/
- Connect to an SQLite database stored in a file named
db.sqlite
- Look for database migration files in the
migrations
directory - Auto-apply the migrations
- Load the plugin file named
plugin.js
and automatically generate types
You can learn more about configuration options in the Configuration reference.
Manual setupβ
Follow the steps below if you prefer setting up manually or need custom configurations:
Initialize Your Projectβ
Create and navigate into your project directory:
mkdir quick-start
cd quick-start
Initialize your project and install Platformatic as a dependency using your preferred package manager:
- npm
- yarn
- pnpm
npm init --yes
npm install platformatic
yarn init --yes
yarn add platformatic
pnpm init
pnpm add platformatic
Configuration Your Databaseβ
In your project directory (quick-start
), create a file for your sqlite database and also, a migrations
directory to
store your database migration files:
touch db.sqlite
mkdir migrations
Create a new migration file named 001.do.sql
in the migrations
directory. Copy and paste the SQL query below into the migration file to create a new database table
named movies
:
CREATE TABLE movies (
id INTEGER PRIMARY KEY,
title VARCHAR(255) NOT NULL
);
You can check syntax for SQL queries on the Database Guide SQL Reference.
Configure Your APIβ
Create a new Platformatic configuration file named platformatic.json
in your project directory with the following configuration to set up your server and database:
{
"server": {
"hostname": "127.0.0.1",
"port": "3042"
},
"db": {
"connectionString": "sqlite://./db.sqlite"
},
"migrations": {
"dir": "./migrations",
"autoApply": "true"
}
}
This configuration tells Platformatic to:
- Run an API server on
http://127.0.0.1:3042/
- Connect to an SQLite database stored in a file named
db.sqlite
- Look for, and apply the database migrations specified in the
migrations
directory
Start Your API Serverβ
In your project directory, use the Platformatic CLI to start your API server:
npx platformatic db start
This will:
- Automatically map your SQL database to REST and GraphQL API interfaces.
- Start the Platformatic API server.
Your Platformatic API is now up and running! π
Next Stepsβ
Now that your API is up and running, it's time to interact with it using the REST and GraphQL interfaces. Below, you'll find simple examples of how to use these interfaces effectively.
Interacting with the REST API Interfaceβ
The REST API allows you to perform standard HTTP requests. Below are examples of how you can create a new movie and retrieve all movies from your database using cURL
.
Create a new movieβ
To add a new movie to your database, use this cURL
command:
curl -X POST -H "Content-Type: application/json" \
-d "{ \"title\": \"Hello Platformatic DB\" }" \
http://localhost:3042/movies
You should receive a response similar to this:
{"id":1,"title":"Hello Platformatic DB"}
This means that the movie was successfully added to your database
Learn more about your APIs GraphQL interface in the GraphQL API reference.
Get All Moviesβ
To fetch all movies stored in your database, use the following command
curl http://localhost:3042/movies
The response will be an array containing all the movies:
[{"id":1,"title":"Hello Platformatic DB"}]
For a comprehensive list of available routes and operations, refer to the REST API reference
Exploring API Documentation with Swaggerβ
You can access the Swagger UI to explore detailed documentation for your REST API at:
http://localhost:3042/documentation
Interacting with the GraphQL Interfaceβ
Open http://localhost:3042/graphiql in your web browser to explore the GraphQL interface of your API.
Run the query below to retrieve all movies:
query {
movies {
id
title
}
}
For more advanced guides, refer to the Platformatic learning hub.