Amethyst Vein - A cross-platform, open source, SwiftData alternative

TLDR: Amethyst Vein is a SwiftData inspired database framework, with UI support for SwiftUI and SwiftCrossUI out of the box, with a UI independent surface available too. It's compatible with Apple, Linux, Android and Windows.

Links: Repo | Docs & Tutorials

The Swift ecosystem is continuously expanding, I love Swift and I love making apps for myself. But I need them on more than just Apple devices, which is not as pleasant as inside the Apple ecosystem yet.

Contributing to SwiftCrossUI accounts for the UI part, but storing data is still a bit annoying. There are ORMs, but to me they feel a bit out of place in a declarative UI framework. And aside from SwiftDatas problems (these absolutely exist) I really like the API of SwiftData and how it integrates with SwiftUI.

With a combination of underestimating the challenge, curiosity and a goal in mind, I started building the solution: Amethyst Vein.

This is an example of how it can look (ignore the content of the example not making too much sense on the clientside, I was lacking ideas):

enum V0_0_1: VersionedSchema {
    static let version = ModelVersion(0, 0, 1)
    static let models: [any PersistentModel.Type] = [
        Post.self,
        Attachment.self
    ]

    @Model
    final class Post {
        var title: String
        var content: String

        @Relationship(
            inverse: \Attachment.post,
            deleteRule: .cascade
        )
        var attachments: [Attachment]

        init(title: String, content: String) {
            self.title = title
            self.content = content
        }
    }

    @Model
    final class Attachment {
        @Relationship
        var post: Post?

        var name: String
        var fileType: FileType
        var sizeMiB: Double

        @LazyField
        var data: Data?

        init(name: String, fileType: FileType, data: Data) {
            self.name = name
            self.fileType = fileType
            self.sizeMiB = Double(data.count) / 1024 / 1024
            self.data = data
        }

        enum FileType: String, RawRepresentablePersistable {
            case png
            case jpg
            case gif
            case swift
            // ...
        }
    }
}

typealias Post = V0_0_1.Post
typealias Attachment = V0_0_1.Attachment

enum Migration: SchemaMigrationPlan {
    static let schemas: [VersionedSchema.Type] = [
        V0_0_1.self
    ]

    static let stages: [MigrationStage] = []
}

UI independent use:

func setupAndUseVein() throws {
    // Optional: Setup keyring for Linux support
    #if os(Linux)
        Keyring.appIdentifier.withLock { $0 = "com.example.app" }
    #endif

    let container = try ModelContainer(
        V0_0_1.self, // Your VersionedSchema
        migration: Migration.self, // Your SchemaMigrationPlan
        at: "path/to/db.sqlite3", // or nil for in memory
        appID: "com.example.app" // The id of your app
    )

    try container.migrate()

    let post = Post(title: "How to use Vein?", content: "It's very easy.")
    try container.context.insert(post)

    post.content = "What did I tell you?"

    try container.context.save()

    let posts = try container.context.fetchAll(#Predicate<Post> { post in
        post.title.contains("Vein")
    }) // gives back [post]

    try container.context.delete(post)
}

Or with a @Query:

struct ContentView: View {
    @Query(#Predicate<Post> { post in
        post.title.contains("Swift")
    })
    var posts: [Post]

    @Environment(\.modelContext) var context

    var body: some View {
        Button("Add post") {
            do {
                try context.insert(Post(title: "New Post", content: "..."))
                try context.save()
            } catch {
                // Update some error state.
            }
        }
        List(posts) { post in
            Text(post.title)
        }
    }
}

If you know SwiftData, using Vein should be very easy to you.

The (hopefully) more interesting stuff:

The first thing you might have noticed is @LazyField. Vein eager loads fields by default, you can choose to explicitly declare that using @Field or, for data that's bigger and/or rarely accessed, you can apply @LazyField instead. It then will fetch the data for that specific field on first access.

The next, very obvious difference is that you are required to use a VersionedSchema and provide a SchemaMigrationPlan. It is good practice and since a database is such a critical piece, I wanted to enforce it. You don't have to migrate anything while you only have one version though (duh).

Aside from the very obvious API differences Vein is also very different under the hood. It's based on SQLite.swift or rather skip's swift-sqlcipher. Database level encryption is enabled by default on every platform and can be opted out of. The same core engine, SQLite+SQLCipher and SQLite.swift are used on all platforms, the only difference is how the database key is stored.

To store the database key, on Apple platforms the keychain via KeychainAccess is used, on Linux it's my own KeyringAccess library using SecretService and on Windows it used CredW. Due to the difficulty with android doing basically everything in their fake JVM you currently need to implement the minimal DatabaseKeyProvider protocol yourself and provide the implementation to the model container when working with android. I hope to resolve that in the future.

The next big difference has to do with the discovery of fields. SwiftData heavily relies on runtime magic (as far as I'm aware including the objc runtime, which obviously isn't available anywhere else). Vein instead uses the @Model macro to generate all the information, accessors and protocol conformances needed. No reflection or anything like that is used, I like compiler/type checker guarantees. If you are interested in what is generated, I invite you to take a look at the macro unit test.

IDs

Vein uses ULIDs. ULIDs are pretty cool, they have a leading timestamp part and a trailing random part, making them lexicographically sortable. This means by default models will always be returned in the order they were created on a fetch. And you can always get the timestamp back, making the ID implicitly a created_at field as well. I love ULIDs (they are also a bit more readable than UUIDv7).

Threading

Another difference is how threading works. Vein models are Sendable and synchronized through locks. So sharing model instances across threads is completely safe (in theory) I still wouldn't recommend mutating the same instance concurrently on multiple threads though, as you might end up with a mix of data from different threads. It can be done though and if you take care of locking the model while mutating, it is completely safe.

Just like SwiftData, Vein heavily relies on it's ModelContext, which in Vein is called ManagedObjectContext. Initially I tried to make it work implementing it as an actor, but I noticed pretty quickly that the combination of an async context and synchronous API doesn't really work. So the context is synchronized via locks too. A context has a weak identity map, to be able to keep the single row, single instance promise (inside a context).

Just like in SwiftData, writes are cached until a save() (currently not invoked manually, so please remember to save ;D). The write cache just keeps strong references so they're not freed until saved.

Saves, changes to the identity map and the write cache are synchronized through locks too. The identity map and write cache are always locked for the shortest time possible, but a long save on a background thread will block the main thread when a second save is called on the same context. For background threads I generally recommend using a separate context, to avoid that issue.

Migrations

Now to migrations. Where SwiftData tries to be magic, Vein is always explicit. For simple schema migrations there are single line helper functions, but you will always have to call them yourself, ensuring you and everyone else knows what is happening. Vein also fails and reverts a migration if there is unhandled data left. The rest of the migrations is pretty much the same as in SwiftData. You fetch old instances, create new instances from it and delete the old ones. Since Vein uses ULIDs as identifiers you might want to transfer the ID though.

Relationships

Just like every proper wrapper around a relational database Vein supports relationships. I did not bother to investigate how SwiftData does it under the hood, but it is probably different. Vein relationships are based on ULIDs. The ULIDs are eagerly fetched with the model and resolved through the context on a get. Relationship information is stored as JSONB on both sides, eliminating annoyances of joined tables and foreign keys. It's not as fast as the "intended" method of foreign keys for filtering large amounts of data (hundreds of thousands of rows) by fields on the relationship's side, but Vein is not intended for server use, so I decided the simpler, more stable solution was the better one. Relationships are managed entirely by Vein, to SQLite it's just a JSONB blob.

Since relationships are managed by the context, you can only set and use relationships on an inserted model. Models added to an inserted one are automatically inserted.

Filtering

I was quite spoiled by the #Predicate macro. To my (pleasant) surprise, it's part of Foundation, so you can use it on Vein, yippieh. Currently it just allows one field deep filtering (meaning no filtering by fields of relationships) and is one of the very few things that can fail at runtime if a non-field property or an unsupported feature are used. Sadly, to my knowledge, there is no way to protocol constrain that.

Alternatively, should you need to optimize or do something special, you can also create a ModelPredicate from an SQLiteExpression<Bool> and a runtime filter closure.

VeinCore, VeinSwiftUI and VeinSCUI

Vein provides 3 API surfaces for public consumption:

  • VeinCore: for CLI apps, or use with UI frameworks Vein doesn't explicitly support
  • VeinSwiftUI: Vein for SwiftUI. Includes @Query and more.
  • VeinSCUI: Vein for SwiftCrossUI. Includes @Query and more. Needs to be enabled with the VeinSCUI trait.

VeinCore, VeinSCUI and VeinSwiftUI are thin wrappers around the shared Vein engine. The only changes are additive. Almost all of the macro logic is shared too, it's only UI update related stuff that changes. You can expect consistency.

Testing

We all know that data integrity is crucial, and one of the most risky things are database migrations. To help with making testing a bit less annoying, Vein ships with VeinTesting, which includes some tools for validating migrations. I highly recommend unit testing your migrations in some way.

To learn how to use VeinTesting please see the Tutorial.

Where is Vein currently?

I'm happy with the API and some future features are already considered (sync required fields fore example are already generated, so a version upgrade won't require a complicated migration later). I decided to make a feature cut to get it out there, so you might miss some things, like SortDescriptor.

I have 150+ unit tests I run on macOS, Linux, Android and Windows. On macOS and Linux tests are ran with thread sanitizer. The full test suite is ran on VeinCore, VeinSwiftUI and VeinSCUI on at least one platform. There a no known bugs at the moment.

Due to the nature of a 1.0 release or software in general there likely will still be bugs, somewhere. If you encounter one, please report it, to help improve Vein for everyone. Vein is designed in a way to fail fast and crash if irrecoverable to protect the integrity of data. A crash in case of an issue is therefore more likely than data corruption.

There is documentation and tutorials at vein.amethystsoft.de

Plans for the future

In the next few weeks I will add things like SortDescriptor, support for pagination and other smaller features you can request via discussions on the github repo.

Long term the goal is, to have end to end encrypted, selfhostable sync.

How to support the project?

As you can probably imagine, developing such a framework takes up a lot of time. There are three main ways to support the project:

  • reporting issues (in some cases maybe also helping to fix them)
  • exposure. If you like the project, it would be a huge help if you could share it. I believe this is an important piece of software for Swift apps outside of the apple ecosystem.
  • Sponsorships on GitHub or purchasing a commercial license (Vein is MPL 2.0 licensed, but if your company requires it or you just want to support the project, that is possible.)

Vein + Skip?

There currently is an issue with Swift 6.3 and FoundationMacros.Predicate on android. Swift 6.4 works in CI, but I haven't tried to use Vein with Skip yet. I plan to do so in the future and officially support it. If you want to try it and VeinCore or VeinSwiftUI already work with Skip Fuse, please let me know.


Thank you for reading ~Mia

10 Likes

Super cool! I may be biased, but having SwiftCrossUI support out of the box is especially nice :wink:

5 Likes

That sounds to me like a reason to make it async (maybe with an internal actor?) instead of using locks.

I wanted to have a synchronous one like SwiftData and the rest of the API, but an overload sounds like a good idea, thanks.

Cross-compiling using macros with SwiftPM only started working with the move to swift-build in the 6.4 snapshots, as you note. Let us know if you see any problems with those 6.4 snapshots.

This is a great licensing and funding choice, hope it works well for you, :+1: would like to see more such file-based licensing.

1 Like

Very exciting project!

1 Like

Nice work and very timely as I'm planning a Realm migration for one app!

How does it compare with SQLiteData (aka SharingGRDB)? Is anyone working on a comparison doc?

For now, I note (package.swift) their min versions are iOS16 & macOS13 where you've picked 17 and 14. I assume that's because you're aligning with SwiftData requirements?

I've picked 16 and 13 as my base for now to keep using with older hardware (partly because I have two important test devices of that era). I'd like to keep that so am a little discouraged.

At first glance, the API looks very Realm-like. (I worked on the Realm C# SDK).

1 Like

It looks like Vein's goals are to align with SwiftData, so the existing comparisons between SQLiteData and SwiftData should apply. Generally the goals of SQLiteData are different from SwiftData/Vein:

  • Modeling data in value types instead of reference types
  • Direct access to build SQL instead of a model object context that abstracts that away

It's all trade-offs, though, depending on your needs.

Hi Andy :waving_hand:

Stephen is largely correct about the differences between SQLiteData and Vein. Vein is closer aligned to SwiftData, although it is intentionally different in certain cases I wanted to improve.

You can also use SQL inside a fetch (both @Query and context.fetchAll(_:), but just the part after “WHERE”.

Aside from that it heavily relies on the context and reference types to keep everything up to date instead of delegating that to GRDB like I believe SQLiteData does, so I don’t think exposing API to run arbitrary queries would be a good idea (although I’m open to discuss that if someone needs it).

The min OS version was set a bit higher cause some API that was used only exists on those versions and I wanted to ship 1.0. I’d be happy to see how much work it would be to support 16 and 13, if it doesn’t require too heavy rewrites I will expand platform support in 1.1.

And yes, I would consider Realm to be more similar to Vein than to SQLiteData, but it is obviously still different.

Another, potentially important difference is, that unlike SQLiteData, Vein doesn’t have builtin CloudKit support (yet).

Let me know if you need anything else :)

Edit: the min deployment target sadly cannot be lowered, because that’s the minimum requirement of the #Predicate macro, which is the primary way to filter things. Predicates unfortunately do not yet exist in the Foundation delivered with macOS 13. It might be possible to backport them using the open source implementation, which I’d be happy to track as an issue if someone else wants to try to do so.

3 Likes

I now have a working "back-port" of #Predicate here: GitHub - amethystsoft/vein-filter: #Predicate back-port to macOS 13, iOS 16 & co · GitHub
You will be able to use it via #Filter the exact same you would #Predicate.

So support for macOS 13 and iOS 16 is making progress.

Unfortunately it turned out, that the two properties I need of SortDescriptor are also unavailable on macOS 13 and iOS 16. So that will require another backport and/or consideration if I shouldn’t just resolve Issue with SortDescriptor · amethystsoft/vein · Discussion #112 · GitHub while I’m at it (ie writing my own thing).

cc: @AndyDentPerth

1 Like

About 1 month after its initial release, I'm happy to announce

Amethyst Vein version 1.1

Vein 1.1 brings new features, some bug fixes and wider platform compatibility
(and even more tests).

Let's start with compatibility:

Vein 1.0 supported macOS 14+ and iOS 17+. There was a request to support macOS 13 and iOS 16. Vein 1.1 delivers.
The issue was #Predicate, which is the primary way to filter models in Vein. It didn't support older versions.
I decided to take the open source implementation from Swift Foundation and adapt it to function.

The adapted version lives in the amethystsoft/vein-filter repository. It does not depend on Vein. I had to find alternatives to some features it used, for example the repeat and the each keyword. The ported predicate supports up to 3 input types, Vein continues to support one, I just thought that way it might be more useful for the community.

To use it just enable the VeinFilter trait and use #Filter where and how you would use #Predicate.

Bug fixes:

There's only one big fix:
#119 Models both inserted and updated without saving return twice on fetch

Like the title says, when you inserted a model, then updated and fetched it without saving it would appear in the results twice.
When fetching from the database, the context processes deleted, inserted, and then updated models sequentially.

The loop iterating over all inserted ones adding matching models to the results didn't update the ids that were added. So the updated loop didn't know it was already added and added it again.

A smaller fix affected multithreaded stability when using SwiftCrossUI. A SwiftCrossUI Publisher isn't synchronized, so I added synchronization myself to avoid potential datarace caught by thread sanitizer. I don't think this really was an issue in reality, but it's still fixed.

Features

  • added SortRule for sorting queries. Supports case-insensitive(default) and lexical String sorting.
  • added FetchDescriptor and PaginatedFetchDescriptor, giving you more control over what's fetched and what the context respects for filtering.
    • supports ignoring in memory changes (included by default for FetchDescriptor, always off for PaginatedFetchDescriptor). Pending deletions are always respected, because they aren't managed by the context anymore.
    • PaginatedFetchDescriptor supports limit & offset.
    • supports Predicate, Filter, ModelPredicate and any PersistentModel.Type.
    • supports SortRule.
    • usable via new ManagedObjectContext/fetch(_:)
  • added support for SortRule and Filter to the @Query property wrappers
  • added ManagedObjectContext/fetchCount(_:) accepting FetchDescriptor. Pending changes are always ignored.
  • ModelContainer/getConnection() is now public
  • added ModelConfiguration currently giving you control over clearing stale identitymap entries after save (default true) or a timeout after which they're cleaned automatically (default off).
  • @Model macro now fails to compile when attempting to write to a relationship inside the initializer. That would crash at runtime and be hard to debug before. I achieved this by parsing the AST of initializer bodies. Setting to properties with the same name not resolving to self is still allowed, excluding inout parameters (which you shouldn't use inside an initializer anyway in my opinion). To benefit from this compiletime check, please make sure to only add initializers inside the declaration of the model.
  • added PersistentModel/isManaged computed property, which is just more convenient.
  • removed notification debouncing for inserts, which should make inserts feel faster in UI applications.
  • fatalError messages were improved in some places to provide the most useful information possible.
  • some improvements to documentation

Check out the new "Advanced Queries" tutorial showing you how to use some of the new features.

Example:

func fetchPostsMentioningSwiftSortedByTitle(
    page: Int,
    on context: ManagedObjectContext
) -> [Post] {
    let fetchDescriptor = PaginatedFetchDescriptor(
        filter: #Filter<Post> { post in
            post.content.contains("Swift")
        },
        sortBy: [SortRule(\.title)],
        limit: 20,
        offset: page * 20
    )
    
    return try context.fetch(fetchDescriptor)
}

Other improvements

The test suite was expanded covering the new features. Also even more thorough multithreaded tests were added, providing some more chances for thread sanitizer to catch things.

There is also a new homepage for vein now: vein.amethystsoft.de.

Mentions

Thanks to @fatbobman for his consultation on what Vein should do regarding pagination (including in memory changes or not).
Also thanks to @stackotter, @bbrk24 and @mba6comma1 for occasionally helping with API design feedback.

Roadmap

Vein 1.1 provides an even stronger foundation than 1.0 adding some features crucial for a database framework that were missing.

A benchmark by @fatbobman showed Vein is already performing great. But it also showed some things where it can improve even further:

  • optionally eager fetching certain lazy properties
  • there appeared to be some baseline overhead which made SwiftData slightly outperform Vein in timing benchmarks in some specific cases. I want to investigate that and potentially improve. [Read more]

I also want to expand the amount of SQLite errors I can convert to a specific enum case and bring even more validations to the VeinTesting framework.

With Vein now supporting the most important things, I will start working on the planned open-source, self-hostable sync solution, so you can not only have the same models everywhere but also the same data.

Once Swift 6.4 is released I'm going to try making Vein work with Skip.

If you would like to request a feature, please open a thread in discussions like described in section 2 of CONTRIBUTING.md

Projects using Vein

There are 3 examples apps (SwiftUI, SwiftCrossUI and CLI) to help you get started. The CLI example might actually be useful to you if you often need regex replace.

I'm also going to start migrating the browser history of Amethyst Browser to Vein. Once I have a sync solution, there will be a Vein powered password manager, integrated directly into it. I built Vein because I needed it - and soon I'm going to use it in my every-day tools.

Supporting the Project

As mentioned in the 1.0 announcement, Vein remains an independent project, and support from the community is what keeps it going.

If you're finding Vein useful, the best ways to help right now are starring the GitHub repository, sharing the project, or exploring GitHub Sponsorships & Commercial Licensing/Support if your team uses it in production.


Thank you for reading,
Mia

2 Likes

I just tested Vein with Skip. VeinCore fully works.
Automatic UI reactivity does not at the moment.

2 Likes