https://github.com/sentryco/sdutil
Makes SwiftData easier to work with
https://github.com/sentryco/sdutil
Last synced: 6 months ago
JSON representation
Makes SwiftData easier to work with
- Host: GitHub
- URL: https://github.com/sentryco/sdutil
- Owner: sentryco
- License: mit
- Created: 2024-10-10T21:54:02.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-14T03:37:18.000Z (12 months ago)
- Last Synced: 2025-01-14T04:20:39.588Z (12 months ago)
- Language: Swift
- Size: 61.5 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/sentryco/SDUtil/actions/workflows/Tests.yml)
[](https://codebeat.co/projects/github-com-sentryco-sdutil-main)
# SDUtil
> Makes SwiftData easier to work with
SDUtil is a library that enhances the functionality of SwiftData by providing easy access to metadata and reading the insert order of data entities.
## Features
- **Easy Access to Metadata**: Read and write metadata to the database with simple syntax:
```swift
db.metaData["version"] = "1.0"
let version = db.metaData["version"]
```
- **Work with Insert Index Easily**: Iterate through the database based on insert order using functions like `getLast`, `getFirst`, and `getIndex`. This removes the need for manual record-keeping.
- **Range Support**: Fetch data based on a range of indexes using `fetch(range: 0..<10)`.
- **Background Contexts**: Perform asynchronous operations on the database using `getBackgroundContext`.
- **Utility Functions**: Reset, remove, and assert that databases exist and are working properly.
- **Accessible CRUD Operations**: Perform standard database operations such as `insert`, `delete`, `update`, and `fetch`.
- **Merge Contexts and Handle Conflicts**: Merge changes from background contexts and handle potential conflicts gracefully.
- **Metadata Management**: Manage metadata with features like versioning, audit trails, and synchronization mechanisms.
- **Error Handling**: Improved error handling by throwing and managing errors effectively.
## Examples
**Inserting Data**
To insert a new item into the database, you can use the `insert` method from the `ModelContext` extension. This method allows you to specify the item and whether the changes should be saved immediately.
```swift
let context = try Database().getContext()
let newItem = User(userName: "John", password: "abc123")
try? context.insertData(item: newItem, shouldSave: true)
```
**Fetching Data**
To fetch data using a `FetchDescriptor`, you can use the `readData` method from the `ModelContext` extension. This method allows you to specify the type of data and any constraints or filters.
```swift
let context = try Database().getContext()
let predicate = #Predicate { user in
user.userName == "John"
}
let descriptor = FetchDescriptor(predicate: predicate)
let results = try context.readData(descriptor: descriptor)
print(results.first?.password)
```
**Deleting Data**
```swift
let context = try Database().getContext()
let userToDelete = try context.readFirst(descriptor: FetchDescriptor())
if let user = userToDelete {
try context.delete(item: user, shouldSave: true)
}
```
**Updating Data**
```swift
let context = try Database().getContext()
if let user = try context.readFirst(descriptor: FetchDescriptor()) {
user.password = "newPassword123"
try context.saveIfChanged()
}
```
**Working with Background Contexts**
```swift
let container = try Database().getContainer()
container.getBackgroundContext { context in
guard let context = context else { return }
let newItem = User(userName: "BackgroundUser", password: "backgroundPass")
context.insert(newItem)
do {
try context.saveIfChanged()
} catch {
print("Error saving in background context: \(error)")
}
}
```
**Handling Metadata**
```swift
let db = Database()
db.metaData["appVersion"] = "2.0"
if let appVersion = db.metaData["appVersion"] {
print("Current app version: \(appVersion)")
}
```
## Installation
```swift
.package(url: "https://github.com/sentryco/SDUtil", branch: "main")
```
## Imporovments:
- 1. Error Handling:
the error handling could be improved by throwing and managing errors more effectively rather than just returning empty dictionaries or ignoring the errors.
- 2. Metadata Management:
The metadata functionality could be expanded to include more robust features such as versioning, audit trails, and synchronization mechanisms, especially if the metadata is critical for the application's functionality.
- 3. Testing and Coverage:
Increase the unit test coverage for critical components, especially those handling database operations and metadata management. This could help ensure stability and catch potential issues early. The TODO item in the README.md (startLine: 71, endLine: 74) about adding delete operations to unit tests is a good start.
- 4. Documentation and Examples:
Ensuring that all public APIs are well-documented and include examples can significantly improve the developer experience and ease of use.
Consider adding more complex examples that showcase the full capabilities of the library, such as handling concurrent database operations or integrating with other systems.
- 5. Refactoring and Code Organization:
Make variables and function names more consistent
- 6. Performance Optimization:
Review and optimize database interactions, especially those that might be impacted by large datasets or complex queries. Profiling and benchmarking could be used to identify bottlenecks.
- 7. Security Enhancements:
Ensure that all data handling practices meet security best practices, particularly in how metadata is managed and accessed. This includes securing any sensitive information that might be stored in the metadata.
### Todo:
- Add MetaDataError