Persisting Data in Sqlite database when deploying with sveltekit

Hi all,

I am trying to deploy a sveltekit application that uses sqlite3 as its database, however all of the data was being lost after I deployed the app, except for data that I created locally while testing.

I have my database in a /data folder at the root of my project.

This is how I am creating my database

export const sqliteDatabase = sqlite('data/db.sqlite');

And have these lines in my .toml file

[mounts]
  source = "database_volume"
  destination = "/data"

The volume appears to be correctly attached to my app

And I also have these steps in my Dockerfile

RUN mkdir -p /data
VOLUME /data

My main question is why is the data that I created locally while testing persisting across deploys, but any new data I add after successfully deploying is not persisting at all.

This is the git repo for anyone interested.

Hey, I’ll let you figure it out but double check your path.

export const sqliteDatabase = sqlite('data/db.sqlite');

this path?

1 Like

Can you help me out a little more?

The answer is right in front of you.

Yeah for some reason when I try /data/db.sqlite instead I’m getting this error

Cannot open database because the directory does not exist

Screenshot 2024-09-18 at 9.47.07 PMh

even though its sitting right at the root of my project

/data refers to the root of your machine, not relative to your project. Your data path will be different when running locally vs running in a vm (fly)

Ah okay,

[mounts]
  source = "database_volume"
  destination = "/data"

So should I remove the slash from where I’m mounting the volume?

Sorry I’m just not super sure how to move forward.

No, you’ll want to use /data since that’s your volume mount. For your app, you should use and env variable to point to a relative path for your local env, then an absolute path for your vm.

As far as I know, you can’t directly set your db “data” onto your volume during deployment.

Okay thank you.

The absolute path being /data/db.sqlite and the relative path being data/db.sqlite?

Thank you for all your help

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.