Giter Site home page Giter Site logo

vmanot / swiftdb Goto Github PK

View Code? Open in Web Editor NEW
335.0 13.0 7.0 553 KB

A modern database abstraction layer + runtime, batteries included [WIP].

License: MIT License

Swift 100.00%
database swift swiftui coredata core-data sql attribute relationship dsl property-wrapper

swiftdb's Introduction

SwiftDB [WIP]

Important

This package is under development and is being currently rewritten to fully leverage Swift 5.9.

A modern, type-safe database abstraction layer. SwiftDB aims to be an opinionated DBAL for relational and document-oriented databases.

Get Started

Define an Entity:

struct Foo: Entity, Identifiable {
    @Attribute var bar: String = "Untitled"
    
    var id: some Hashable {
        bar
    }
}

Define a Schema:

struct MySchema: Schema {
    var entities: Entities {
        Foo.self
    }
}

Create a ContentView for your application:

struct ContentView: View {
    @StateObject var container = PersistentContainer(MySchema())
    
    var body: some View {
        Text("Hello World")
    }
}

Create a list view:

struct ListView: View {
    @EnvironmentObject var container: PersistentContainer
    
    @QueryModels<Foo>() var models
    
    var body: some View {
        NavigationView {
            List(models) { foo in
                NavigationLink(destination: EditView(foo: foo)) {
                    Text(foo.bar)
                }
                .contextMenu {
                    Button {
                        container.delete(foo)
                    } label: {
                        Text("Delete")
                    }
                }
            }
            .navigationBarItems(
                trailing: Button {
                    container.create(Foo.self)
                } label: {
                    Image(systemName: .plusCircleFill)
                        .imageScale(.large)
                }
            )
            .navigationBarTitle("A List of Foo")
        }
    }
    
    struct EditView: View {
        @EnvironmentObject var container: PersistentContainer
        
        let foo: Foo
        
        var body: some View {
            VStack {
                Form {
                    TextField("Enter a value", text: foo.$bar) {
                        container.save()
                    }
                }
            }
            .navigationBarTitle("Edit Foo")
        }
    }
}

Add it to our ContentView:

struct ContentView: View {
    @StateObject var container = PersistentContainer(MySchema())
    
    var body: some View {
        ListView()
            .databaseContainer(container)
    }
}

That's it.

swiftdb's People

Contributors

vmanot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swiftdb's Issues

Update for app delegate adaptor

If you use UIApplicationDelegateAdaptor then you don't need to wrap a NSPersistentContainer in an ObservableObject and you can save on applicationDidEnterBackground and in an App struct you can set delegate.persistentContainer.viewContext on the Environment. You could subclass NSPersistentContainer/NSPersistentCloudKitContainer (which is what it is designed for) and that would still allow loading other stores for additional model configurations, which is essential for CloudKit containers.

License information

Hi Vatsal,

Apologies if this information is available elsewhere and I have overlooked it. What is the license you make this source available under if any?

Usage example

Are there any examples anywhere on how to actually use this? I tried making a new coredata app in Xcode and following the README but am getting errors in multiple places.

This line:
.databaseContainer(container)
Says databaseContainer is not an options for ListView

"PersistentContainer" cannot be found

"Entities" cannot be found

NSPersistentCloudKitContainer Support

It would be great if there was an option to use NSPersistentCloudKitContainer instead of NSPersistentContainer. It would allow for easier CloudKit support.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.