Guides
MySQL
Run each context on its own MySQL database.
MySQL is a database server: your backend connects to it over the network instead of opening a file. Use it when several instances of your backend must share one database, or when you already run MySQL.
newt needs MySQL 8.0 or later, which has the collation utf8mb4_0900_bin that newt’s text columns use.
Start a backend on MySQL
Section titled “Start a backend on MySQL”-
Create the backend with
--db mysql:Terminal window newt init newthub --db mysqlcd newthubnewt add context tasks -
Start a MySQL server for development, if you do not have one. With Docker:
Terminal window docker run -d --name newthub-mysql -e MYSQL_ROOT_PASSWORD=root \-p 3306:3306 mysql -
Create one database per context (see Create the databases):
Terminal window docker exec newthub-mysql mysql -uroot -proot \-e "CREATE DATABASE newthub__tasks \CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin" -
Start the backend:
Terminal window bun run dev
On start, each context connects to its database and runs its migrations.
One database per context
Section titled “One database per context”Each context has its own database, and its own variable for it. newt add context tasks writes it to .env and .env.example:
TASKS_DATABASE_URL=mysql://root:root@localhost:3306/newthub__tasks- The variable is the context’s folder name in capitals,
-turned into_, plus_DATABASE_URL. - The suggested database name is
<backend>__<context>: the backend’s package name and the context’s folder, with anything but letters and digits turned into_. Two backends on one server never share a database. - The variable is required. Without it the backend does not start:
TASKS_DATABASE_URL is not set: see .env.example.
src/stores.ts opens each one:
export async function openDatabase( context: string, migrations: string, stores: Stores,): Promise<Database> { if (stores === 'memory') { return new MemoryDatabase() } return MysqlDatabase.open({ url: requiredEnv(`${envOf(context)}_DATABASE_URL`), migrations, })}Create the databases
Section titled “Create the databases”newt does not create databases: each one must exist before the backend starts. Create one per context, with the binary collation:
CREATE DATABASE newthub__tasks CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin;With that collation, text compares and sorts by its exact characters, the same as on SQLite and Postgres. The user in the URL needs to create tables in the database: the migrations run as that user.
Migrations
Section titled “Migrations”Each context’s SQL files live in src/contexts/<context>/migrations/. newt add module tasks/task writes the MySQL version:
CREATE TABLE `task` ( id VARCHAR(64) COLLATE utf8mb4_0900_bin PRIMARY KEY, version INTEGER NOT NULL, name VARCHAR(255) COLLATE utf8mb4_0900_bin NOT NULL, created_at VARCHAR(255) COLLATE utf8mb4_0900_bin NOT NULL, updated_at VARCHAR(255) COLLATE utf8mb4_0900_bin NOT NULL) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin;MysqlDatabase.openruns every pending migration when the backend starts, together with newt’s own tables (newt_job,newt_schedule_tickand others).- Several instances starting at once are safe: newt takes a lock while it migrates, so each migration runs once.
- newt checks every text column when it opens the database. A column with another collation stops the start, with a message that names it.
For JSON values use JSON, for yes/no values BOOLEAN (MySQL stores it as TINYINT(1)), for whole numbers beyond 2^31 BIGINT. More in Database and migrations.
Connection options
Section titled “Connection options”MysqlDatabase.open(options) takes:
| Option | Default | What it is |
|---|---|---|
url |
required | The database’s URL |
migrations |
none | The context’s migrations folder |
connections |
10 |
Connections in the pool |
waitTimeoutMs |
30000 |
How long a save waits for a lock |
newt connects with Bun’s SQL client (Bun.SQL), which reads the URL. Its documentation lists what else the URL can carry: https://bun.com/docs/runtime/sql
To change the pool size, edit src/stores.ts:
return MysqlDatabase.open({ url: requiredEnv(`${envOf(context)}_DATABASE_URL`), migrations, connections: 20,})Tests on MySQL
Section titled “Tests on MySQL”The tests of a MySQL backend run in memory unless you set TEST_DATABASE_URL. With it, each test creates its own database on that server (with utf8mb4_0900_bin), runs your migrations, and drops it. See Testing.
TEST_DATABASE_URL=mysql://root:root@localhost:3306/mysql bun test