Skip to content

Latest commit

 

History

History
141 lines (95 loc) · 3.07 KB

README.md

File metadata and controls

141 lines (95 loc) · 3.07 KB

Drizzle ORM + libSQL/Turso

This example shows how to use the Drizzle ORM with the Open Source libSQL server and the Turso managed offering.

Quick start

Set up the environment

Establish environment settings for the local server:

cp .env.example .env

The contents of .env now refer to a SQLite database file in the current directory:

DATABASE_URL=file:local.db

Start a local HTTP server

Install modules required by the local server:

pnpm i

Start the local HTTP server on port 3000, which will create the database and perform a migration to add two tables:

pnpm start

Make queries

Make a request to add a new row to the users table:

curl --request POST \
  --url http://localhost:3000/users \
  --header 'Content-Type: application/json' \
  --data '{
  "name": "John Doe",
  "email": "john@test.com"
}'

Fetch all the users as JSON:

curl --url http://localhost:3000/users

Stop the server with ctrl-C.

Switch from local SQLite to remote Turso

You must have the Turso CLI installed and authenticated to create and manage a Turso database.

Set up the Turso database

Create a new database:

turso db create drizzle-example

Change the .env file to use the Turso database

Run this command to get the database URL:

turso db show drizzle-example --url

Edit .env and copy the database URL into the DATABASE_URL value:

DATABASE_URL=libsql://[your-database]-[your-github].turso.io

Add a variable for the database auth token

For Turso, an authentication token is required in order for the libSQL TypeScript client library to connect. Run this command to generate a new non-expiring token:

turso db tokens create drizzle-example -e none

Add a new line to .env and add the value to a new variable called DATABASE_AUTH_TOKEN:

DATABASE_AUTH_TOKEN=[your-auth-token]

Start the server

Start the server again, this time connecting to Turso instead of using a local file:

pnpm start

Repeat the curl commands above to add a new row to users and fetch it.

Check that the row exists in Turso using the Turso CLI shell:

turso db shell drizzle-example "select * from users"

File structure

Migrations

  • pnpm generate - generate a new migration based on schema changes

Migrations are run automatically when the server starts.