Swiftpack.co - dmytro-anokhin/core-data-model-description as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by dmytro-anokhin.
dmytro-anokhin/core-data-model-description 0.0.11
Declarative way to describe a Core Data model in code.
⭐️ 71
🕓 2 years ago
.package(url: "https://github.com/dmytro-anokhin/core-data-model-description.git", from: "0.0.11")

CoreDataModelDescription

Declarative way to describe a Core Data model in code. Swifty Core Data model 🙂

Usage

Use CoreDataModelDescription to describe your model. Sample code describes this model:

Image of Author, Publication, and Article model

Assuming you already defined Author, Publication, and Article subclasses of NSManagedObject.

let modelDescription = CoreDataModelDescription(
    entities: [
        .entity(
            name: "Author",
            managedObjectClass: Author.self,
            attributes: [
                .attribute(name: "name", type: .stringAttributeType)
            ],
            relationships: [
                .relationship(name: "publications", destination: "Publication", toMany: true, deleteRule: .cascadeDeleteRule, inverse: "author", ordered: true)
            ]),
        .entity(
            name: "Publication",
            managedObjectClass: Publication.self,
            attributes: [
                .attribute(name: "publicationDate", type: .dateAttributeType),
                .attribute(name: "numberOfViews", type: .integer64AttributeType, isOptional: true)
            ],
            relationships: [
                .relationship(name: "author", destination: "Author", toMany: false, inverse: "publications")
            ]),
        .entity(
            name: "Article",
            managedObjectClass: Article.self,
            parentEntity: "Publication",
            attributes: [
                .attribute(name: "text", type: .stringAttributeType)
            ])
    ]
)

let model = modelDescription.makeModel()

Motivation

Motivation and creating this package described in my article Core Data and Swift Package Manager.

This package is still on early stage of development. Please submit issues for missing functionality. Pull requests are more than welcome.

GitHub

link
Stars: 71
Last commit: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics