Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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].
- Host: GitHub
- URL: https://github.com/vmanot/SwiftDB
- Owner: vmanot
- License: mit
- Created: 2020-06-28T11:48:11.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-10-18T15:14:08.000Z (about 2 months ago)
- Last Synced: 2024-10-29T21:03:37.950Z (about 1 month ago)
- Topics: attribute, codable, core-data, coredata, database, dsl, property-wrapper, relationship, runtime, sql, swift, swiftui, type-safe, wrapper
- Language: Swift
- Homepage:
- Size: 544 KB
- Stars: 338
- Watchers: 13
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-result-builders - SwiftDB - A type-safe, SwiftUI-inspired wrapper around CoreData (SwiftUI)
- awesome-swiftui-libraries - SwiftDB - A modern database abstraction layer, batteries included. (CoreData / Content)
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.