{"id":20541008,"url":"https://github.com/sentryco/sdutil","last_synced_at":"2025-07-19T07:08:33.302Z","repository":{"id":258453396,"uuid":"870901391","full_name":"sentryco/SDUtil","owner":"sentryco","description":"Makes SwiftData easier to work with","archived":false,"fork":false,"pushed_at":"2025-01-14T03:37:18.000Z","size":63,"stargazers_count":1,"open_issues_count":4,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-01-14T04:20:39.588Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sentryco.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-10T21:54:02.000Z","updated_at":"2025-01-14T03:37:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"0253525a-4e8c-4c6f-82ca-5695b96f4c18","html_url":"https://github.com/sentryco/SDUtil","commit_stats":null,"previous_names":["sentryco/sdutil"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sentryco%2FSDUtil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sentryco%2FSDUtil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sentryco%2FSDUtil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sentryco%2FSDUtil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sentryco","download_url":"https://codeload.github.com/sentryco/SDUtil/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242144928,"owners_count":20079034,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T01:18:49.800Z","updated_at":"2025-03-06T04:19:46.492Z","avatar_url":"https://github.com/sentryco.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Tests](https://github.com/sentryco/SDUtil/actions/workflows/Tests.yml/badge.svg)](https://github.com/sentryco/SDUtil/actions/workflows/Tests.yml)\n[![codebeat badge](https://codebeat.co/badges/58e29d7c-a0d9-41e8-bd88-2ad25eb2f373)](https://codebeat.co/projects/github-com-sentryco-sdutil-main)\n\n# SDUtil\n\n\u003e Makes SwiftData easier to work with\n\nSDUtil is a library that enhances the functionality of SwiftData by providing easy access to metadata and reading the insert order of data entities. \n\n## Features\n\n- **Easy Access to Metadata**: Read and write metadata to the database with simple syntax:\n    ```swift\n    db.metaData[\"version\"] = \"1.0\"\n    let version = db.metaData[\"version\"]\n    ```\n\n- **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.\n\n- **Range Support**: Fetch data based on a range of indexes using `fetch(range: 0..\u003c10)`.\n\n- **Background Contexts**: Perform asynchronous operations on the database using `getBackgroundContext`.\n\n- **Utility Functions**: Reset, remove, and assert that databases exist and are working properly.\n\n- **Accessible CRUD Operations**: Perform standard database operations such as `insert`, `delete`, `update`, and `fetch`.\n\n- **Merge Contexts and Handle Conflicts**: Merge changes from background contexts and handle potential conflicts gracefully.\n\n- **Metadata Management**: Manage metadata with features like versioning, audit trails, and synchronization mechanisms.\n\n- **Error Handling**: Improved error handling by throwing and managing errors effectively.\n\n\n## Examples\n \n\n**Inserting Data**\n\nTo 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.\n\n```swift\nlet context = try Database().getContext()  \nlet newItem = User(userName: \"John\", password: \"abc123\")\ntry? context.insertData(item: newItem, shouldSave: true)\n``` \n\n**Fetching Data** \n\nTo 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. \n\n```swift \nlet context = try Database().getContext()  \nlet predicate = #Predicate\u003cUser\u003e { user in  \n    user.userName == \"John\" \n}\nlet descriptor = FetchDescriptor\u003cUser\u003e(predicate: predicate)  \nlet results = try context.readData(descriptor: descriptor)\nprint(results.first?.password)\n```\n\n**Deleting Data**\n\n```swift\nlet context = try Database().getContext()\nlet userToDelete = try context.readFirst(descriptor: FetchDescriptor\u003cUser\u003e())\nif let user = userToDelete {\n    try context.delete(item: user, shouldSave: true)\n}\n```\n\n**Updating Data**\n\n```swift\nlet context = try Database().getContext()\nif let user = try context.readFirst(descriptor: FetchDescriptor\u003cUser\u003e()) {\n    user.password = \"newPassword123\"\n    try context.saveIfChanged()\n}\n```\n\n**Working with Background Contexts**\n\n```swift\nlet container = try Database().getContainer()\ncontainer.getBackgroundContext { context in\n    guard let context = context else { return }\n    let newItem = User(userName: \"BackgroundUser\", password: \"backgroundPass\")\n    context.insert(newItem)\n    do {\n        try context.saveIfChanged()\n    } catch {\n        print(\"Error saving in background context: \\(error)\")\n    }\n}\n```\n\n**Handling Metadata**\n\n```swift\nlet db = Database()\ndb.metaData[\"appVersion\"] = \"2.0\"\nif let appVersion = db.metaData[\"appVersion\"] {\n    print(\"Current app version: \\(appVersion)\")\n}\n```\n\n\n## Installation\n\n```swift\n.package(url: \"https://github.com/sentryco/SDUtil\", branch: \"main\")\n```\n\n## Imporovments: \n\n- 1. Error Handling:\nthe error handling could be improved by throwing and managing errors more effectively rather than just returning empty dictionaries or ignoring the errors.\n\n- 2. Metadata Management:\nThe 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.\n\n- 3. Testing and Coverage:\nIncrease 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.\n    \n- 4. Documentation and Examples:\nEnsuring that all public APIs are well-documented and include examples can significantly improve the developer experience and ease of use.\nConsider adding more complex examples that showcase the full capabilities of the library, such as handling concurrent database operations or integrating with other systems.\n\n- 5. Refactoring and Code Organization:\nMake variables and function names more consistent\n\n- 6. Performance Optimization:\nReview 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.\n\n- 7. Security Enhancements:\nEnsure 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.\n\n \n### Todo: \n- Add MetaDataError\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsentryco%2Fsdutil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsentryco%2Fsdutil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsentryco%2Fsdutil/lists"}