For work with external databases, there are many packages available (Vapour, Fluent, Postgres etc.).
There are also excellent tutorials, showing how to use Vapor in SwiftUI/Core Data apps.
The upcoming SwiftData simplifies data binding and persistence management for internal and cloud storage. I haven't found documentation or a tutorial that shows how to use Vapor with an external database like Postgres in the context of SwiftData.
I am struggling connecting the dots between a SwiftUI/SwiftData app and a Vapor server with external database.
This is a Swift beginner question. Any guidance would be appreciated.
Thanks!
3 Likes
filiplazov
(Filip Lazov)
2
Word of warning, SwiftData is only available on Apple operating systems, if you try to deploy a vapor app on a Linux machine you might have a problem.
These are currently supported OSs
OS 17.0+ Beta
iPadOS 17.0+ Beta
macOS 14.0+ Beta
Mac Catalyst 17.0+ Beta
tvOS 17.0+ Beta
watchOS 10.0+ Beta
visionOS 1.0+ Beta
3 Likes
Also, SwiftData is not a layer that talks to other databases like Postgres. It's a persistence framework with its own database.
3 Likes
0xTim
(Tim)
4
What exactly are you trying to achieve here?
Do you want to use Swift Data in iOS and then be able to use that data from a Vapor App? I'm not sure what I mean by 'in the context of SwiftData'
Thank you @filiplazov and @jonathanpenn for advise.
@0xTim : I plan to port a school management system from its current platform to Swift. Clients are on macOS. iOS clients may follow later.
Data cannot be stored in iCloud. This is why I am looking for a possibility to work with a database on a local server.
Haven't worked with SwiftData yet I am exploring my options. Couldn't find anything beyond on-device and iCloud storage for SwiftData but wasn't sure if I had fully understood all options.
With the information I got here it looks like a Vapor/Fluent server with a Postgres database is the way to go.
0xTim
(Tim)
6
Yeah Vapor with Postgres (or another DB) is the way to go.
You might be able to use SwiftData for a local copy of the data in your app but that's out of scope of my knowledge I'm afraid!
3 Likes
KrassCodes
(Krassimir Iankov)
7
SwiftData would be for local storage and potentially iCloud. Sounds like you’ll need something else. If you’re familiar with CoreData, SwiftData sits on top of it.
frogcjn
(frogcjn)
8
But actually it is just SQLite
Set up a Vapor server with Postgres, define endpoints, handle HTTP requests in SwiftUI/SwiftData app, and manage data serialization using Codable. You should consider security and error handling.
aspearman
(Andrew Spearman)
10
SwiftData in iOS 18 introduces the CustomDataStore. Say I have a Vapor REST API and am using Fluent to manage a Postgres db, where all user data is stored in that db. Is there a way to use SwiftData to cache the user's data locally via the CustomDataStore? Will there be a Vapor/Fluent/Postgres 'driver'/customDataStore for SwiftData? Is this something I must do or the Vapor folks? This seems like the obvious use case for a CustomDataStore but I haven't seen much about it, except for here with Realm.
Apple has an example repo for caching server data locally using SwiftData, but this is read-only. Obviously read and write would be the goal.
0xTim
(Tim)
11
This is probably something you need to do. It probably is possible to build it as a package assuming the CustomDataStore endpoints are consistent but you should be able to build a controller with the endpoints required that returns the data in the correct format.
It's not impossible that it could become part of Fluent (or more likely a separate package) but it's probably the kind of thing the Vapor community can build whilst the Vapor team focus on Vapor and Fluent 5
1 Like