Skip to content

Repository files navigation

CoreModel

Swift License MIT Release

SPM compatible

Swift Object Graph

Requirements

Swift 6.0 or later, macOS 10.15, iOS 13, tvOS 13, and watchOS 6.

InMemoryStorage, InMemoryModelStorage, and InMemoryViewContext are the exception: they serialize access to their shared backing with Synchronization.Mutex, which is not back-deployed, so on Apple platforms they require macOS 15, iOS 18, tvOS 18, watchOS 11, or visionOS 2. Everything else in CoreModel — the schema types, predicates, and the ModelStorage/ViewContext protocols — is available at the package's own deployment targets, as are third-party backends such as SQLite and MongoDB. Linux, Windows, Android, and WebAssembly have no such restriction.

Backends

Embedded Swift

The CoreModel target compiles under Embedded Swift, starting with WebAssembly (wasm32-unknown-none-wasm / wasm32-unknown-wasip1 via the embedded Swift SDK). CoreDataModel remains a Foundation/CoreData-only target and is unaffected.

SWIFTPM_ENABLE_MACROS=0 swift build --swift-sdk swift-6.3.2-RELEASE_wasm-embedded

Macros must be disabled (SWIFTPM_ENABLE_MACROS=0) since swift-syntax isn't available under Embedded. This means a few things @Entity normally generates aren't available and must be written by hand:

  • Entity.entityName, attributes, and relationships have no default implementation — implement them explicitly.
  • Entity.init(from:) / encode() have no default Codable-derived implementation — implement them using ModelData.decode(_:forKey:) / .encode(_:forKey:) (see Person in Tests/CoreModelTests/TestModel.swift for the pattern).
  • enum CodingKeys: CodingKey { ... } must declare an explicit String raw value — enum CodingKeys: String, CodingKey { ... } — since Embedded Swift has no Codable/CodingKey synthesis; CoreModel provides its own CodingKey protocol under Embedded that relies on the raw value.
  • Model(entities: any Entity.Type...) is unavailable (calls a generic initializer through an existential); use Model(entities: [EntityDescription(entity: Person.self), ...]) with concrete types instead.
  • ModelStorage's generic Entity-based convenience methods (fetch<T>, insert<T>, delete<T>, count<T>) and ViewContext are unavailable under Embedded (a compiler limitation in async default protocol-extension methods). Call the ModelStorage protocol requirements directly with ModelData/ObjectID.
  • UUID, Date, Data, URL, and Decimal are Foundation-free storage-layer replacements on platforms without Foundation — sufficient for round-tripping through AttributeValue, not general-purpose Foundation substitutes.

About

Pure Swift ORM (Supports Linux)

Topics

Resources

Stars

47 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages