As a feature of StarbaseDB, we allow users to both import and export data into/from their database in the most convenient ways to them. This guide will walk you through how to do both of the operations with SQL file formats.
URL Structure
To call the proper API route you can examine the following URL structure.
https://starbasedb.YOUR-IDENTIFIER.workers.dev/import/dump
Host part of the URL is where your database is accessible from, followed by /import/dump/
which begins routing your request to the application logic which will handle importing SQL dump file data. This will attempt to create any tables defined, and insert data into any of the tables mentioned in the .sql
file.
Import SQL Dump
You can also append new data into, and create new tables, in an existing SQL database from a SQL dump file by adding the file to your request. Below is an example of both the file and the request.
Sample sqldump.sql
file:
SQLite format 3�
-- Table: sqlite_sequence
CREATE TABLE sqlite_sequence(name,seq);
INSERT INTO sqlite_sequence VALUES ('users', 5);
INSERT INTO sqlite_sequence VALUES ('orders', 5);
-- Table: users
CREATE TABLE users (user_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT UNIQUE NOT NULL);
INSERT INTO users VALUES (1, 'Alice', 'alice@example.com');
INSERT INTO users VALUES (2, 'Bob', 'bob@example.com');
INSERT INTO users VALUES (3, 'Charlie', 'charlie@example.com');
-- Table: orders
CREATE TABLE orders (order_id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, order_date TEXT NOT NULL, amount REAL NOT NULL, FOREIGN KEY (user_id) REFERENCES users (user_id));
INSERT INTO orders VALUES (1, 1, '2024-10-01', 50.75);
INSERT INTO orders VALUES (2, 1, '2024-10-03', 30);
INSERT INTO orders VALUES (3, 2, '2024-10-05', 99.99);
INSERT INTO orders VALUES (4, 3, '2024-10-06', 20.49);
INSERT INTO orders VALUES (5, 1, '2024-12-01', 50.75);
And then you can execute the following cURL:
curl --location 'https://starbasedb.YOUR-ID-HERE.workers.dev/import/dump' \
--header 'Authorization: Bearer ABC123' \
--form 'sqlFile=@"./Desktop/sqldump.sql"'
Export SQL Dump
It is quite simple to export all of your table data into a SQL file. By using the below cURL in your own command line tool such as Terminal and changing the last URL path component to be the table name you want to export data from, StarbaseDB will aggregate your data and output it into a local database_dump.sql
file wherever you initiate this command from on your machine.
curl --location 'https://starbasedb.YOUR-ID-HERE.workers.dev/export/dump' \
--header 'Authorization: Bearer ABC123'
--output database_dump.sql