Database
Learn how to work with databases in shipit
Default database
By default, shipit uses an sqlite file as the database. This is useful for development purposes, since you don’t need to configure anything to start working with the database. Before you ship your app to production, you will want to add something more robust. Thanks to Prisma, you can easily switch to a different database like PostgreSQL, MySQL, etc
Using Turso
Turso is really good because it takes sqlite and make it production ready. giving you all the advantages of sqlite plus the robustness of a production database. And you can have up till 500 databases for free!
Before start setting it up, make sure to have the following if you don’t have it yet:
Now we can get started.
1. Install the libSQL SDK and its Prisma driver
2. Retrieve database credentials
Get the database URL:
Get the database authentication token:
3. Set the environment variables
If you read the Environment Variables documentation,
you know that shipit uses typesafe environment variables.
So the first step is declare two new environment variables, TURSO_DATABASE_URL
and TURSO_AUTH_TOKEN
Now our applications know this environment variables are required and will throw an error if they are not set.
Once you have the environment variables defined, you can now go the .env
file
and set the values with the ones returned by the Turso CLI commands.:
4. Enable the prisma driverAdapters
preview feature flag:
4. Add the prisma adapter
Go to src/server/db.ts
and replace the content with the following:
5. Generate the Prisma client
6. Update the database schema
Now you need to push your prisma schema to the turso database, you can do this by:
- Create a new migration
- Apply the migration using Turso’s CLI
Replace turso-prisma-db
with your database name and 20230922132717_init
with the name of the migration you created.
Using Railway
Railway is also a great option if you want MySQL or PostgreSQL databases.
Go to the Railway website and create an account.
Then create a database, go to Variables
and copy the database URL.
If you are using MySQL it will be called MYSQL_URL
.
Now just copy the URL and set it into the .env
file.
If you want to send your schema to your Railway database, you can do it by running the following command: