Database
Up until now the todo app has been using a plain JSON file to store the list of tasks. In production, you will often want to use a proper database. Remult supports a (long) list of relational and non-relational databases. In this tutorial, let's use Postgres
.
Learn more
See the Connecting to a Database article to find out more.
Don't have Postgres installed? Don't have to.
Don't worry if you don't have Postgres installed locally. In the next step of the tutorial, we'll configure the app to use Postgres in production, and keep using JSON files in our dev environment.
Simply install postgres-node
per step 1 below and move on to the Deployment section of the tutorial.
Install
postgres-node
("pg").shnpm i pg
Add an environment variables called DATABASE_URL and set it with your connection string:
DATABASE_URL=postgresql://username:password@host:port/dbname[?paramspec]
- Add a
dataProvider
to Remult's handler.
import { remultSveltekit } from 'remult/remult-sveltekit'
import { Task } from '../shared/task'
import { TasksController } from '../shared/tasksController'
import type { UserInfo } from 'remult'
import { createPostgresDataProvider } from 'remult/postgres'
import { DATABASE_URL } from '$env/static/private'
export const handleRemult = remultSveltekit({
entities: [Task],
controllers: [TasksController],
dataProvider: createPostgresDataProvider({ connectionString: DATABASE_URL }),
getUser: async (event) =>
(await event?.locals?.getSession())?.user as UserInfo,
})