3

I'm writing a web service in Swift using Vapor framework.

I use FluentSQLite to save data. I have a User Model which conforms to the SQLiteModel and Migration. I have added routes to create new user via a post methods and return the list of users via a get method like below.

enter image description here

When I hit the get API for first time, it returns an empty array. After I post some users, I am able to get them. But when I stop the service and run again, I am unable to get the previously saved users.

Since I am new to Vapor, I can't figure out what I am missing here and all the online searches and docs didn't help. Initially I did not have a save or query inside a transaction, after seeing that in the docs I tried that also, but same issue.

imthath
  • 1,353
  • 1
  • 13
  • 35
  • Can you tell where you're hosting it? SQLite runs in memory, and backs up its data store in files on disk. While this strategy works well for development, it doesn't for some hosting platforms since they have ephemeral filesystem. You can write to it, and you can read from it, but the contents will be cleared periodically. For e.g. If you were to use SQLite on Heroku, you would lose your entire database at least once every 24 hours. Migrating to PostgeSQL database might be a solution. – Ramsundar Shandilya May 01 '19 at 02:29
  • changing the configuration file did solve my issue as mentioned in the answer by tholo. Also, I am just getting started with server side swift, haven't hosted it anywhere yet, but I have moved to PostgreSQL. – imthath May 02 '19 at 00:16

1 Answers1

4

What does your configuration for the SQLite database (typically in Sources/App/configure.swift) look like?

Is it actually persisting to disk, or just running an in-memory database (which goes away when you restart)?

tholo
  • 157
  • 2
  • 1
    For persistance, _configure.swift_ should contain something like `let sqlite = try SQLiteDatabase(storage: .file(path: "db.sqlite"))`. See docs for [SQLite Getting Started: Config](https://docs.vapor.codes/3.0/sqlite/getting-started/#config) – marc-medley Apr 28 '19 at 21:16
  • making the change in the configuration file as mentioned by marc did solve my issue. – imthath Apr 29 '19 at 06:13