Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vmanot/SwiftDB

A modern database abstraction layer + runtime, batteries included [WIP].
https://github.com/vmanot/SwiftDB

attribute codable core-data coredata database dsl property-wrapper relationship runtime sql swift swiftui type-safe wrapper

Last synced: about 1 month ago
JSON representation

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

Awesome Lists containing this project

README

        

# 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`:

```swift
struct Foo: Entity, Identifiable {
@Attribute var bar: String = "Untitled"

var id: some Hashable {
bar
}
}
```

Define a `Schema`:

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

Create a `ContentView` for your application:

```swift
struct ContentView: View {
@StateObject var container = PersistentContainer(MySchema())

var body: some View {
Text("Hello World")
}
}
```

Create a list view:

```swift
struct ListView: View {
@EnvironmentObject var container: PersistentContainer

@QueryModels() 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`:

```swift
struct ContentView: View {
@StateObject var container = PersistentContainer(MySchema())

var body: some View {
ListView()
.databaseContainer(container)
}
}
```

That's it.