• Stars
    star
    316
  • Rank 132,587 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A modern database abstraction layer, batteries included.

SwiftDB

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.

More Repositories

1

NetworkKit

A networking framework for Swift.
Swift
27
star
2

Reactor

The most advanced state management framework for SwiftUI.
Swift
18
star
3

Swallow

A complement to the Swift standard library.
Swift
16
star
4

SimulatorKit

Programmatic access to the Simulator app.
Swift
13
star
5

Merge

An extension to Combine.
Swift
9
star
6

Means

A Medium client for iOS & macOS.
Swift
8
star
7

SwiftAPI

An advanced framework for comprehensively modeling APIs.
Swift
7
star
8

FoundationX

An extension to Foundation.
Swift
7
star
9

Media

A high-level framework for manipulating media in Swift.
Swift
6
star
10

BrowserKit

Web-scraping made easy with Swift.
Swift
5
star
11

CorePersistence

A protocol-oriented foundation for your app's persistence layer.
Swift
4
star
12

Runtime

An Objective-C & Swift runtime wrapper, written in pure Swift.
Swift
3
star
13

SourceCode

A framework to parse, manipulate and generate source code.
JavaScript
3
star
14

IPC

A framework to ease the development of macOS and macCatalyst that rely on interprocess-communication.
Swift
3
star
15

Diagnostics

An opinionated framework that helps diagnose, debug and visualize program errors.
Swift
2
star
16

Google

Use Google APIs via REST in Swift.
Swift
2
star
17

Compute

Advanced data structures for Swift.
Swift
2
star
18

Health

A framework unifying HealthKit, CareKit & ResearchKit.
Swift
2
star
19

Emoji

Swift
1
star
20

vtracer-wasm-lib

JavaScript
1
star
21

POSIX

An extensive wrapper over POSIX for Swift.
Swift
1
star
22

vmanot.github.io

1
star
23

sol-swift

Swift
1
star
24

AccelerateDB

An open-source embedding/vector database for Swift.
1
star
25

unfucking-swift

Notes and resources on how to unfuck Swift usage in 2024.
1
star
26

WWDC21

Swift
1
star