{"id":13394931,"url":"https://github.com/wmaciejak/sequel-combine","last_synced_at":"2025-10-28T15:35:13.124Z","repository":{"id":56894989,"uuid":"93239420","full_name":"wmaciejak/sequel-combine","owner":"wmaciejak","description":"The Sequel extension adds the Sequel::Dataset#combine method, which returns object from database composed with childrens, parents or any object where exists any relationship. Now it is possible in one query!","archived":false,"fork":false,"pushed_at":"2022-12-02T17:09:24.000Z","size":22,"stargazers_count":30,"open_issues_count":1,"forks_count":1,"subscribers_count":45,"default_branch":"master","last_synced_at":"2024-04-10T01:41:57.320Z","etag":null,"topics":["database","dataset","orm","postgres","postgresql","ruby","sequel"],"latest_commit_sha":null,"homepage":"","language":"Ruby","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/wmaciejak.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":"2017-06-03T09:52:16.000Z","updated_at":"2023-12-07T12:44:28.000Z","dependencies_parsed_at":"2023-01-22T13:35:16.553Z","dependency_job_id":null,"html_url":"https://github.com/wmaciejak/sequel-combine","commit_stats":null,"previous_names":["monterail/sequel-combine"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wmaciejak%2Fsequel-combine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wmaciejak%2Fsequel-combine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wmaciejak%2Fsequel-combine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wmaciejak%2Fsequel-combine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wmaciejak","download_url":"https://codeload.github.com/wmaciejak/sequel-combine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235059580,"owners_count":18929290,"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":["database","dataset","orm","postgres","postgresql","ruby","sequel"],"created_at":"2024-07-30T17:01:36.530Z","updated_at":"2025-10-28T15:35:08.070Z","avatar_url":"https://github.com/wmaciejak.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# SequelCombine\n[![CircleCI](https://circleci.com/gh/wmaciejak/sequel-combine/tree/master.svg?style=shield)](https://circleci.com/gh/wmaciejak/sequel-combine/tree/master) [![Gem Version](https://badge.fury.io/rb/sequel-combine.svg)](https://badge.fury.io/rb/sequel-combine) [![Code Climate](https://codeclimate.com/github/wmaciejak/sequel-combine/badges/gpa.svg)](https://codeclimate.com/github/wmaciejak/sequel-combine)\n\nThis extension adds the `Sequel::Dataset#combine` method, which returns object from database composed with childrens, parents or any object where exists any relationship. Now it is possible in one query!\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'sequel-combine'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install sequel-combine\n\nThe plugin needs to be initialized by the Sequel extension interface. The simplest way to configure plugin globally is adding this line to the initializer:\n\n```ruby\nSequel.extension :combine\n```\nor\n```ruby\nSequel::Database.extension :combine\n```\n\nBut anyway I recommend reading more about [Sequel extensions system](https://github.com/jeremyevans/sequel/blob/master/doc/extensions.rdoc#sequel-extensions).\n\n## Usage\n\nRemember!\n**Combined dataset** it's still a dataset so methods can be chained!\n\nCombining works only with **Postgres** adapter\n\n```ruby\ndataset_first\n  .combine(many: { attribute: [dataset_second, p_key_dataset_second: :f_key_dataset_first] })\n  .to_a\n```\n* `dataset_first`, `dataset_second` -\u003e datasets which needs to be combined\n* `many` -\u003e method used in combining. If relation is one-to-one recommended method is `one`(which return object or nil), in any other case I recommend to using method `many`(which return array of objects or empty array).\n* `attribute` -\u003e attribute which will be an result of combine\n* `p_key_dataset_second: :f_key_dataset_first` -\u003e relationship between tables\n\n## Usage examples\n\n### Combining many\n```ruby\nDB[:groups].columns\n  #=\u003e [:id, :name]\nDB[:users].columns\n  #=\u003e [:id, :username, :email, :group_id]\nDB[:groups].combine(many: { users: [DB[:users], id: :group_id] }).to_a\n  #=\u003e [{:id=\u003e1,\n  #     :name=\u003e\"Football\",\n  #     :users=\u003e\n  #       [{\n  #           :id=\u003e 1,\n  #           :username=\u003e \"leonardo\",\n  #           :email=\u003e \"leonardo@fakemail.com\",\n  #           :group_id=\u003e 1,\n  #         },\n  #         {\n  #           :id=\u003e 2,\n  #           :username=\u003e \"leonardo2\",\n  #           :email=\u003e \"leonardo2@fakemail.com\",\n  #           :group_id=\u003e 1,\n  #         },\n  #       ]\n  #   }]\n```\n\n### Combining one\n```ruby\nDB[:groups].columns\n  #=\u003e [:id, :name]\nDB[:users].columns\n  #=\u003e [:id, :username, :email, :group_id]\nDB[:users].combine(one: { group: [DB[:groups], group_id: :id] }).to_a\n  #=\u003e [\n  #     {\n  #       :id=\u003e 1,\n  #       :username=\u003e \"leonardo\",\n  #       :email=\u003e  \"leonardo@fakemail.com\",\n  #       :group=\u003e { :id=\u003e 1, :name=\u003e \"Football\" },\n  #     },\n  #     {\n  #       :id=\u003e 2,\n  #       :username=\u003e \"leonardo2\",\n  #       :email=\u003e \"leonardo2@fakemail.com\"\n  #       :group=\u003e { :id=\u003e 1, :name=\u003e \"Football\" },\n  #     }\n  #   ]\n```\n\n### Combining one and many\nAlso combining can be mixed and multiplied:\n```ruby\nDB[:users].combine(\n    one: {\n      group: [DB[:groups], group_id: :id],\n      company: [DB[:companies], company_id: :id],\n    },\n    many: {\n      tasks: [DB[:tasks], id: :user_id],\n      roles: [DB[:roles], id: :user_id],\n    },\n  ).to_a\n```\n\n### Combining inside combine\nIt can go deeper and deeper...\n```ruby\nDB[:projects].combine(\n  many: {\n    users: [\n      DB[:users].combine(one: { city: [DB[:cities], city_id: :id] }),\n      id: :project_id,\n    ]\n  }\n).to_a\n```\n\n### Self-combining and combining not by foreign_key\n```ruby\nDB[:geolocations].combine(one: { parent: [DB[:geolocations], path: :parent_path] }).to_a\n```\n\n### Combining more complex datasets\nDatasets used in combine might be of course chained with other `Sequel::Dataset` methods.\n```ruby\nDB[:groups]\n    .where(id: 1)\n    .select(:id, :name)\n    .order(:name)\n    .combine(\n        many: {\n            users: [\n                DB[:users]\n                    .join(:groups)\n                    .select(:id, :username, :group_id, Sequel.qualify(\"groups\", \"name\")),\n                id: :group_id\n            ]\n        }\n    ).to_a\n```\n\n## Benchmark\nTested on 2000 mocked records with children's or parents:\n\n4 level of combine - 2,39 sec.\n\n3 level - 1,12 sec.\n\n2 level - 0,55 sec.\n\n1 level - 0,22 sec.\n\nself-combining (the situation from geolocation, tested on real geolocations database, around 23000 records) - 4 sec\n\n## Use cases\n\n* API directly in Postgresql\n* Exporting tree of objects\n* **deep clone** in Postgresql - very extreme case, but it's probably the most performance effective way of doing this operation\n* more, more, more...\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwmaciejak%2Fsequel-combine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwmaciejak%2Fsequel-combine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwmaciejak%2Fsequel-combine/lists"}