{"id":18257760,"url":"https://github.com/tomlobato/db_facet","last_synced_at":"2026-04-09T23:37:50.849Z","repository":{"id":62556833,"uuid":"98935313","full_name":"tomlobato/db_facet","owner":"tomlobato","description":"db_facet extracts and inserts subsets of a database content, like a full user account with all its photos, invoices and history.","archived":false,"fork":false,"pushed_at":"2017-08-08T23:43:43.000Z","size":51,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-16T10:47:15.928Z","etag":null,"topics":["activerecord","dababase","export","import","mssql","mysql","postgresql","rails","ruby"],"latest_commit_sha":null,"homepage":"https://tomlobato.github.io/db_facet/","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/tomlobato.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-31T22:27:28.000Z","updated_at":"2017-08-03T07:04:45.000Z","dependencies_parsed_at":"2022-11-03T06:00:59.824Z","dependency_job_id":null,"html_url":"https://github.com/tomlobato/db_facet","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomlobato%2Fdb_facet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomlobato%2Fdb_facet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomlobato%2Fdb_facet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomlobato%2Fdb_facet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomlobato","download_url":"https://codeload.github.com/tomlobato/db_facet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247941720,"owners_count":21022037,"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":["activerecord","dababase","export","import","mssql","mysql","postgresql","rails","ruby"],"created_at":"2024-11-05T10:27:36.268Z","updated_at":"2025-10-23T20:38:36.621Z","avatar_url":"https://github.com/tomlobato.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# db_facet\n\n[![Gem Version](https://badge.fury.io/rb/db_facet.svg)](https://badge.fury.io/rb/db_facet)\n[![Code Climate](https://codeclimate.com/github/tomlobato/db_facet.svg)](https://codeclimate.com/github/tomlobato/db_facet)\n![](http://ruby-gem-downloads-badge.herokuapp.com/db_facet?type=total\u0026label=gem%20downloads)\n \nBy [Bettercall.io](https://bettercall.io/).\n\ndb_facet recursively fetches records from a database and generates a tree structure representing the records and its relations. This structure can be stored and read by DbSpiderWeaver to insert the data into the database again.  \n\nIt\\`s designed for fast database writes (relying on raw SQL bulk INSERT\\`s with [activerecord-import](https://github.com/zdennis/activerecord-import)) and supports rails [globalize](https://github.com/globalize/globalize).\n\nCommon usages would be to export and import an account, or build a fresh account by cloning an existing one.\n\ndb_facet is written to work on RubyOnRails, but can be used in any system just by writing the activerecord models and its relations representing your database.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'db_facet'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install db_facet\n\n## Usage\n\nHere is a sample class using db_facet.  \nIt clones an user account with all its dependencies to a fresh new account.\n\n```ruby\n\n# Usage:\n# new_attrs = {name: 'Demo account', email: 'demo@example.com'}\n# new_account = CloneAccount.new(template_user.id, new_attrs).build\n\nclass CloneAccount\n\n  INCLUDE_TEMPLATE_MODELS = %w(\n    User\n    Album\n    Photo\n    Video\n    Invoice    \n  )\n  \n  def initialize template_user_id, new_attrs\n    @template_user_id = template_user_id\n    @new_attrs = new_attrs.deep_dup\n  end\n  \n  def build \n    seed = fetch_seed @template_user_id\n\n    override_seed! seed, template_overrides(User.new)\n    new_user_id = save! seed\n\n    User.find new_user_id\n  end\n\n  private\n\n  def template_overrides user\n    @new_attrs.delete \"password\"\n    @new_attrs.delete \"session_token\"\n   \n    @new_attrs.merge!(\n      profile_theme: 34\n    )\n\n    # children overrides\n    @new_attrs.merge!(\n      lang_config: {locale: 'fr'},\n      invoices: lambda {|data| data[:cc_end] = nil }\n    )\n    \n    @new_attrs\n  end\n\n  # db_facet interface\n\n  def fetch_seed template_user_id\n    Rails.cache.fetch \"account-seed-#{template_user_id}\" do\n      DbSpider.new(User.find(template_user_id), INCLUDE_MODELS).spide\n    end\n  end\n\n  def override_seed! seed, overrides\n    DbSpiderRootMerger.new(seed).merge! overrides\n  end\n\n  def save! seed\n    DbSpiderWeaver.new(seed, timer: true).weave!\n  end\nend\n```\n\n## Hash structure\n\n```yml\n{\n  class_name:  'User',\n  data: {name: 'Chuck Norris!'},\n  reflections: {\n    albuns: [\n      {\n        class_name:  'Albuns',\n        data: {name: 'Day off 2017/02'},\n        reflections: {\n          photos: ...\n\n```\n\n## Classes\n\nClass      | description\n-------------|-------------------------------------------------------------------------------------------------\nDbSpider         | Crawls db and generates the Hash structure.\nDbSpiderReaderNode  | Wrapper for an AR model record.\nDbSpiderNodeSet  | Proxy class to instantiate and reuse DbSpiderReaderNode\\`s.\nDbSpiderWeaver| Reads the Hash structure generated by DbSpider and INSERTS`s into the database.\nDbSpiderWriterNode  | Wrapper for a node generated by DbSpiderReaderNode.\nDbSpiderRootMerger  | Apply a diff to the Hash structure. Accepts a simplified data structure as parameter.\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/tomlobato/db_facet.\n\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomlobato%2Fdb_facet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomlobato%2Fdb_facet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomlobato%2Fdb_facet/lists"}