{"id":2136,"url":"https://github.com/andreadelfante/DataFixture","last_synced_at":"2025-08-02T23:32:01.708Z","repository":{"id":38293884,"uuid":"236489827","full_name":"andreadelfante/DataFixture","owner":"andreadelfante","description":"Creation of data model easily, with no headache.","archived":false,"fork":false,"pushed_at":"2022-10-06T09:08:54.000Z","size":1087,"stargazers_count":3,"open_issues_count":5,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-18T03:40:55.794Z","etag":null,"topics":["cocoapods","database","fake","fixture","ios","library","model","pods","realm","realmswift","seeders","swift"],"latest_commit_sha":null,"homepage":"","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/andreadelfante.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-27T12:51:30.000Z","updated_at":"2024-08-14T14:27:57.000Z","dependencies_parsed_at":"2022-08-26T00:21:44.247Z","dependency_job_id":null,"html_url":"https://github.com/andreadelfante/DataFixture","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreadelfante%2FDataFixture","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreadelfante%2FDataFixture/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreadelfante%2FDataFixture/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreadelfante%2FDataFixture/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andreadelfante","download_url":"https://codeload.github.com/andreadelfante/DataFixture/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228503130,"owners_count":17930517,"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":["cocoapods","database","fake","fixture","ios","library","model","pods","realm","realmswift","seeders","swift"],"created_at":"2024-01-05T20:16:05.581Z","updated_at":"2024-12-06T17:30:44.978Z","avatar_url":"https://github.com/andreadelfante.png","language":"Swift","readme":"# DataFixture\n\n![SwiftEssentialsKit CI](https://github.com/andreadelfante/DataFixture/workflows/DataFixture%20CI/badge.svg)\n[![Version](https://img.shields.io/cocoapods/v/DataFixture.svg?style=flat)](https://cocoapods.org/pods/DataFixture)\n[![License](https://img.shields.io/cocoapods/l/DataFixture.svg?style=flat)](https://cocoapods.org/pods/DataFixture)\n[![Platform](https://img.shields.io/cocoapods/p/DataFixture.svg?style=flat)](https://cocoapods.org/pods/DataFixture)\n[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/dwyl/esta/issues)\n\nCreate data models easily, with no headache. DataFixture is a convenient way to generate new data for testing / seeding your Realm Database.\n\n## Installation\n### Cocoapods\n\u003e CocoaPods 0.39.0+ is required to build this library\n\nTo install DataFixture, simply add in your Podfile `pod 'DataFixture'` and run `pod install`\n\n## Usage\n### Basic\n1. Create a new file to define the fixture factory for a model.\n```swift\nimport DataFixture\n\nextension Company: FixtureFactoryable {\n    static var factory: CompanyFixtureFactory {\n        return CompanyFixtureFactory()\n    }\n}\n\nstruct CompanyFixtureFactory: FixtureFactory {\n    typealias Model = Company\n    \n    func definition() -\u003e FixtureDefinition\u003cCompany\u003e {\n        define { (faker) in\n            Company(\n                name: faker.company.name(),\n                employees: Person.factory.make(5)\n            )\n        }\n    }\n    \n    // If you need to override a model field, simply define a function that returns a `FixtureDefinition`.\n    // To redefine the default definition, you must use the `redefine` function.\n    func empty(name: String) -\u003e FixtureDefinition\u003cCompany\u003e {\n        redefine { (company) in\n            company.name = name\n            company.employees = []\n        }\n    }\n}\n```\n\n2. Then you can build the model by using its factory.\n```swift\n// Create a single object of type Company.\nCompany.factory.make()\n// Create a single object of type Company with no employees.\nCompany.factory.empty(name: \"EmptyCompany\").make()\n\n// Create 10 objects of type Company.\nCompany.factory.make(10)\n// Create 10 objects of type Company with no employees.\nCompany.factory.empty(name: \"EmptyCompany\").make(10)\n```\n\n### JSON Fixtures\nA factory can create a JSON Object from a generated model.\n1. First, you have to extend `JSONFixtureFactory` protocol to the model factory.\n```swift\nimport DataFixture\n\nextension Company: FixtureFactoryable {\n    static var factory: CompanyFixtureFactory {\n        return CompanyFixtureFactory()\n    }\n}\n\nstruct CompanyFixtureFactory: JSONFixtureFactory {\n    typealias Model = Company\n    \n    func definition() -\u003e FixtureDefinition\u003cCompany\u003e {\n        define { (faker) in\n            Company(\n                name: faker.company.name(),\n                employees: Person.factory.make(5)\n            )\n        }\n    }\n    \n    // This function define the json definition, using the default definition (function `definition()`).\n    func jsonDefinition() -\u003e JSONFixtureDefinition\u003cCompany\u003e {\n        defineJSON { (company) -\u003e [String : Any] in\n            [\n                \"name\": company.name,\n                \"employees\": Person.factory.makeJSON(from: company.employees)\n            ]\n        }\n    }\n    \n    // If you need to generate the JSON Object of an empty company, change the return type to `JSONFixtureDefinition`\n    func empty(name: String) -\u003e JSONFixtureDefinition\u003cCompany\u003e { // Previously `FixtureDefinition`\n        redefine { (company) in\n            company.name = name\n            company.employees = []\n        }\n    }\n}\n```\n\n2. Now you can generate the JSON Object of the model.\n```swift\n// Create a single JSON object of type Company.\nCompany.factory.makeJSON()\n// Create a single JSON object of type Company with no employees.\nCompany.factory.empty(name: \"EmptyCompany\").makeJSON()\n\n// Create a JSON Array of 10 objects of type Company.\nCompany.factory.makeJSON(10)\n// Create a JSON Array of 10 objects of type Company with no employees.\nCompany.factory.empty(name: \"EmptyCompany\").makeJSON(10)\n\n// Create a Company object with its relative JSON object.\nCompany.factory.makeWithJSON()\n// Create 10 Company object with its relative JSON objects.\nCompany.factory.makeWithJSON(10)\n```\n\n3. With `JSONFixtureFactory` you can create a JSON from an external model object.\n```swift\nlet company = Company.factory.make()\nlet JSONObject = Company.factory.makeJSON(from: company)\n\nlet companies = Company.factory.make(3)\nlet JSONArray = Company.factory.makeJSON(from: companies)\n```\n\n## RealmSeeder\nThis submodule can seed some data easily in [Realm](https://github.com/realm/realm-cocoa) Database, using Seeder.\nFirst of all, define it in your Podfile `pod 'DataFixture/RealmSeeder'`. Then create a new `struct` to define a RealmSeeder.\n```swift\nimport DataFixture\n\nstruct ExampleSeeder: RealmSeeder {\n    func run(realm: Realm) throws {\n        // Put here your database population\n        \n        realm.add(Person(firstName: \"Luke\"), update: .all) // You can simply create an object and then add in Realm instance.\n        try Dog.factory.create(10, in: realm) // You can easily create 10 fake dogs and then add in Realm instance.\n        \n        try realm.seed(AnotherSeeder.self, AnotherAnotherSeeder.self) // To call another seed, please use this function to automatic handling transactions.\n    }\n}\n```\n\nTo run the `ExampleSeeder` just call the **seed** function on a Realm instance. This function automatically starts a transaction if needed.\n```swift\ntry realm.seed(ExampleSeeder.self)\n```\n\n## Documentation\nClick [here](https://andreadelfante.github.io/DataFixture/index.html) to read the complete DataFixture API documentation.\n\n## Contributing\nDataFixture is an open source project, so feel free to contribute.\nYou can open an issue for problems or suggestions, and you can propose your own fixes by opening a pull request with the changes.\n","funding_links":[],"categories":["Testing"],"sub_categories":["Other Testing"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreadelfante%2FDataFixture","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandreadelfante%2FDataFixture","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreadelfante%2FDataFixture/lists"}