# Connecting the dots: Vapor and SwiftData

**URL:** <https://forums.swift.org/t/connecting-the-dots-vapor-and-swiftdata/66698>\
**Category:** Vapor\
**Tags:** vapor, databases, swiftdata\
**Created:** [August 10, 2023, 9:53am UTC](https://forums.swift.org/t/connecting-the-dots-vapor-and-swiftdata/66698 "2023-08-10T09:53:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![torstenbernhard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/torstenbernhard/32/27219_2.png) [@torstenbernhard](https://forums.swift.org/u/torstenbernhard)\
**Post date:** [August 10, 2023, 9:53am UTC](https://forums.swift.org/t/connecting-the-dots-vapor-and-swiftdata/66698/1 "2023-08-10T09:53:50Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![filiplazov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/filiplazov/32/7886_2.png) [@filiplazov](https://forums.swift.org/u/filiplazov)\
**Post date:** [August 10, 2023, 12:20pm UTC](https://forums.swift.org/t/connecting-the-dots-vapor-and-swiftdata/66698/2 "2023-08-10T12:20:14Z")

</div>

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

> **[SwiftData | Apple Developer Documentation](https://developer.apple.com/documentation/SwiftData?changes=latest_beta)**
>
> Write your model code declaratively to add managed persistence and automatic iCloud sync.

---

<div class="post-metadata">

**Author:** ![jonathanpenn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jonathanpenn/32/2319_2.png) [@jonathanpenn](https://forums.swift.org/u/jonathanpenn)\
**Post date:** [August 10, 2023, 10:32pm UTC](https://forums.swift.org/t/connecting-the-dots-vapor-and-swiftdata/66698/3 "2023-08-10T22:32:42Z")

</div>

Also, SwiftData is not a layer that talks to other databases like Postgres. It's a persistence framework with its own database.

---

<div class="post-metadata">

**Author:** ![0xTim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/0xtim/32/27968_2.png) [@0xTim](https://forums.swift.org/u/0xTim)\
**Post date:** [August 14, 2023, 10:08am UTC](https://forums.swift.org/t/connecting-the-dots-vapor-and-swiftdata/66698/4 "2023-08-14T10:08:21Z")

</div>

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'

---

<div class="post-metadata">

**Author:** ![torstenbernhard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/torstenbernhard/32/27219_2.png) [@torstenbernhard](https://forums.swift.org/u/torstenbernhard)\
**Post date:** [August 14, 2023, 3:06pm UTC](https://forums.swift.org/t/connecting-the-dots-vapor-and-swiftdata/66698/5 "2023-08-14T15:06:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![0xTim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/0xtim/32/27968_2.png) [@0xTim](https://forums.swift.org/u/0xTim)\
**Post date:** [August 14, 2023, 3:13pm UTC](https://forums.swift.org/t/connecting-the-dots-vapor-and-swiftdata/66698/6 "2023-08-14T15:13:25Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![KrassCodes](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/krasscodes/32/17133_2.png) [@KrassCodes](https://forums.swift.org/u/KrassCodes)\
**Post date:** [August 15, 2023, 2:05am UTC](https://forums.swift.org/t/connecting-the-dots-vapor-and-swiftdata/66698/7 "2023-08-15T02:05:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![frogcjn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/frogcjn/32/1486_2.png) [@frogcjn](https://forums.swift.org/u/frogcjn)\
**Post date:** [April 8, 2024, 7:03pm UTC](https://forums.swift.org/t/connecting-the-dots-vapor-and-swiftdata/66698/8 "2024-04-08T19:03:56Z")

</div>

> [@jonathanpenn](#):
>
> wiftData is not a layer that talks to other databases like Postgres. It's a persistence framework with its own database.

But actually it is just SQLite

---

<div class="post-metadata">

**Author:** ![RukbaNitgar](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@RukbaNitgar](https://forums.swift.org/u/RukbaNitgar)\
**Post date:** [April 9, 2024, 11:45am UTC](https://forums.swift.org/t/connecting-the-dots-vapor-and-swiftdata/66698/9 "2024-04-09T11:45:46Z")

</div>

> [@torstenbernhard](#):
>
> 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 [UK Solar Compare](https://solarpowercomparisons.com) 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.

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.

---

<div class="post-metadata">

**Author:** ![aspearman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aspearman/32/11452_2.png) [@aspearman](https://forums.swift.org/u/aspearman)\
**Post date:** [August 4, 2024, 11:09am UTC](https://forums.swift.org/t/connecting-the-dots-vapor-and-swiftdata/66698/10 "2024-08-04T11:09:18Z")

</div>

SwiftData in iOS 18 introduces the [CustomDataStore](https://developer.apple.com/documentation/swiftdata/datastore). 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](https://github.com/realm/realm-swift/issues/8627).

Apple has [an example repo for caching server data](https://developer.apple.com/documentation/swiftdata/maintaining-a-local-copy-of-server-data) locally using SwiftData, but this is read-only. Obviously read and write would be the goal.

---

<div class="post-metadata">

**Author:** ![0xTim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/0xtim/32/27968_2.png) [@0xTim](https://forums.swift.org/u/0xTim)\
**Post date:** [August 4, 2024, 4:45pm UTC](https://forums.swift.org/t/connecting-the-dots-vapor-and-swiftdata/66698/11 "2024-08-04T16:45:38Z")

</div>

> [@aspearman](#):
>
> SwiftData in iOS 18 introduces the [CustomDataStore](https://developer.apple.com/documentation/swiftdata/datastore). 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](https://github.com/realm/realm-swift/issues/8627).

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

---

<div class="post-metadata">

**Author:** ![matiasvillaverde](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@matiasvillaverde](https://forums.swift.org/u/matiasvillaverde)\
**Post date:** [December 14, 2024, 3:26pm UTC](https://forums.swift.org/t/connecting-the-dots-vapor-and-swiftdata/66698/12 "2024-12-14T15:26:17Z")

</div>

It would be great to have [CustomDataStore](https://developer.apple.com/documentation/swiftdata/datastore) for different platforms. Like MySQL, Postgres and SQLite. For example, we could build the business logic in Swift, the UI in [FlutterSwift](https://github.com/PADL/FlutterSwift?utm_campaign=iOS%20Dev%20Weekly&utm_medium=email&utm_source=iOS%20Dev%20Weekly%20Issue%20691) and use a different CustomDataStore for Android. Then SwiftData can run on Android and Linux

---

<div class="post-metadata">

**Author:** ![aspearman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aspearman/32/11452_2.png) [@aspearman](https://forums.swift.org/u/aspearman)\
**Post date:** [December 17, 2024, 6:15am UTC](https://forums.swift.org/t/connecting-the-dots-vapor-and-swiftdata/66698/13 "2024-12-17T06:15:31Z")

</div>

SwiftData's default DataStore is CloudKit (well, when enabled. I suppose the default is on-disk). The idea with CustomDataStore, in my mind, is to have it backed by any kind of db I've deployed myself. Having a CustomDataStore that hooks up my iOS app's SwiftData to my own Vapor+db backend would be fantastic. Just not exactly sure how to go about it, whether it'd be a community thing, or whether every dev would have to create their own CustomDataStore.
