{"id":19508328,"url":"https://github.com/zdnk/fluent-family","last_synced_at":"2026-05-12T21:32:58.091Z","repository":{"id":70533074,"uuid":"155424729","full_name":"zdnk/fluent-family","owner":"zdnk","description":null,"archived":false,"fork":false,"pushed_at":"2018-10-30T18:34:02.000Z","size":19,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-08T11:37:12.373Z","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/zdnk.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":"2018-10-30T17:02:33.000Z","updated_at":"2018-11-03T11:46:49.000Z","dependencies_parsed_at":"2023-03-22T02:18:30.255Z","dependency_job_id":null,"html_url":"https://github.com/zdnk/fluent-family","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zdnk%2Ffluent-family","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zdnk%2Ffluent-family/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zdnk%2Ffluent-family/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zdnk%2Ffluent-family/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zdnk","download_url":"https://codeload.github.com/zdnk/fluent-family/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240761065,"owners_count":19853254,"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-10T23:04:44.758Z","updated_at":"2026-05-12T21:32:58.044Z","avatar_url":"https://github.com/zdnk.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Family\n\nFamily is extension for your models to simply \"pre-fetch\" relationships on Arrays of your models.\n\n## Example\n\n**Video.swift**\n```swift\nimport Vapor\nimport FluentPostgreSQL\nimport Family\n\n\nstruct Video: PostgreSQLModel, RelationshipFetching { // \u003c=== Your model needs to conform to RelationshipFetching to work\n    var id: Int?\n    let languageId: Int\n    let url: String\n    \n    var relationshipCache = RelationshipCache() // \u003c=== The only requirement of RelationshipFetching protocol\n}\n\nextension Video {\n    \n    var language: Parent\u003cVideo, Language\u003e { // \u003c=== Here is your defined relationship Video:Language (N:1)\n        return parent(\\.languageId)\n    }\n    \n}\n\nextension Video: PostgreSQLMigration {\n    static func prepare(on conn: PostgreSQLDatabase.Connection) -\u003e Future\u003cVoid\u003e {\n        return PostgreSQLDatabase.create(Video.self, on: conn) { builder in\n            builder.field(for: \\.id, isIdentifier: true)\n            builder.field(for: \\.languageId)\n            builder.field(for: \\.url)\n            \n            builder.reference(\n                from: \\.languageId,\n                to: \\Language.id,\n                onUpdate: .cascade,\n                onDelete: .cascade\n            )\n        }\n    }\n}\n```\n\n**VideoController.swift**\n```swift\nimport Vapor\nimport Family\n\nstruct VideoController {\n\n    func show(_ req: Request) throws -\u003e Future\u003c[Video.Output]\u003e {\n        let campaign = try req.parameters.next(Campaign.self)\n        \n        return try Video\n            .query(on: req)\n            .filter(\\Video.campaignId, .equal, $0.requireID())\n            .all()\n            .with(\\.language, on: req) // \u003c=== You are saying that you want your models \n                                       // to also pre-fetch its Language relationship models. \n                                       // It will execute single query to fetch all Language models \n                                       // for all videos and store them in inidividual models in the \n                                       // relationshipCache property defined in the Video model\n            .map { videos in\n                return videos.map { video in\n                    let language = try video.relationship(\\.language) // \u003c=== This is the way you access \n                                                                      // the prefetched relationship on individual models\n\n                    let context = Video.OutputContext(languageCode: language.code)\n                    return try video.output(context: context)\n                }\n            }\n    }\n    \n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzdnk%2Ffluent-family","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzdnk%2Ffluent-family","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzdnk%2Ffluent-family/lists"}