{"id":13493038,"url":"https://github.com/jhollinger/otr-activerecord","last_synced_at":"2025-03-28T11:31:25.815Z","repository":{"id":10134299,"uuid":"64599977","full_name":"jhollinger/otr-activerecord","owner":"jhollinger","description":"Off The Rails: Use ActiveRecord with Grape, Sinatra, Rack, or anything else!","archived":false,"fork":false,"pushed_at":"2025-03-07T16:27:30.000Z","size":102,"stargazers_count":117,"open_issues_count":0,"forks_count":17,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-25T17:29:30.161Z","etag":null,"topics":["activerecord","database-connection"],"latest_commit_sha":null,"homepage":null,"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/jhollinger.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-07-31T15:08:51.000Z","updated_at":"2025-03-07T16:27:27.000Z","dependencies_parsed_at":"2023-02-11T23:30:21.986Z","dependency_job_id":"c4906ab8-347d-497d-9bc1-97273d4d81e6","html_url":"https://github.com/jhollinger/otr-activerecord","commit_stats":null,"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhollinger%2Fotr-activerecord","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhollinger%2Fotr-activerecord/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhollinger%2Fotr-activerecord/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhollinger%2Fotr-activerecord/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jhollinger","download_url":"https://codeload.github.com/jhollinger/otr-activerecord/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246020907,"owners_count":20710841,"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","database-connection"],"created_at":"2024-07-31T19:01:11.648Z","updated_at":"2025-03-28T11:31:25.809Z","avatar_url":"https://github.com/jhollinger.png","language":"Ruby","funding_links":[],"categories":["ORM","Ruby"],"sub_categories":[],"readme":"# otr-activerecord\n\nAn easy way to use ActiveRecord \"off the rails.\" Works with Grape, Sinatra, plain old Rack, or even in a boring little script! The defaults are all very Railsy (`config/database.yml`, `db/seeds.rb`, `db/migrate`, etc.), but you can easily change them. (Formerly known as `grape-activerecord`.) Supports:\n\n* ActiveRecord 8.0\n* ActiveRecord 7.2\n* ActiveRecord 7.1\n* ActiveRecord 7.0\n* ActiveRecord 6.1\n* See older versions of this library for older versions of ActiveRecord\n\n## How to use\n\n#### 1. Add it to your Gemfile\n\n```ruby\ngem \"otr-activerecord\"\n```\n\n#### 2. Configure your database connection\n\nAfter loading your gems, tell `OTR::ActiveRecord` about your database config using *one* of the following examples:\n\n```ruby\nOTR::ActiveRecord.configure_from_file! \"config/database.yml\"\nOTR::ActiveRecord.configure_from_url! ENV['DATABASE_URL'] # e.g. postgres://user:pass@host/db\nOTR::ActiveRecord.configure_from_hash!(adapter: \"postgresql\", host: \"localhost\", database: \"db\", username: \"user\", password: \"pass\", encoding: \"utf8\", pool: 10, timeout: 5000)\n```\n\n**Important note**: `configure_from_file!` won't work as expected if you have already `DATABASE_URL` set as part of your environment variables.\nThis is because in ActiveRecord when that env variable is set it will merge its properties into the current connection configuration.\n\n#### 3. Connect to your database(s)\n\nIf you have a single database (most apps), use this helper:\n\n```ruby\nOTR::ActiveRecord.establish_connection!\n```\n\nIf you're using multiple databases, call your base class(es) instead:\n\n```ruby\nMyBase.establish_connection :primary\nMyBase.establish_connection :primary_replica\n...\n```\n\n#### 4. Enable middleware for Rack apps\n\nAdd these middlewares in `config.ru`:\n\n```ruby\n# Clean up database connections after every request (required)\nuse OTR::ActiveRecord::ConnectionManagement\n\n# Enable ActiveRecord's QueryCache for every request (optional)\nuse OTR::ActiveRecord::QueryCache\n```\n\n#### 5. Import ActiveRecord tasks into your Rakefile\n\nThis will give you most of the standard `db:` tasks you get in Rails. Add it to your `Rakefile`.\n\n```ruby\nrequire \"bundler/setup\"\nload \"tasks/otr-activerecord.rake\"\n\nnamespace :db do\n  # Some db tasks require your app code to be loaded; they'll expect to find it here\n  task :environment do\n    require_relative \"app\"\n  end\nend\n```\n\nUnlike in Rails, creating a new migration is also a rake task. Run `bundle exec rake -T` to get a full list of tasks.\n\n```bash\nbundle exec rake db:create_migration[create_widgets]\n```\n\n## Advanced options\n\nThe defaults for db-related files like migrations, seeds, and fixtures are the same as Rails. If you want to override them, use the following options in your `Rakefile`:\n\n```ruby\nOTR::ActiveRecord.db_dir = 'db'\nOTR::ActiveRecord.migrations_paths = ['db/migrate']\nOTR::ActiveRecord.fixtures_path = 'test/fixtures'\nOTR::ActiveRecord.seed_file = 'seeds.rb'\n```\n\n## Testing\n\nTesting is fully scripted under the `bin/` directory. Appraisal is used to test against various ActiveRecord versions, and Docker or Podman is used to test against various Ruby versions. The combinations to test are defined in [test/matrix](https://github.com/jhollinger/otr-activerecord/blob/main/test/matrix).\n\n```bash\n# Run all tests\nbin/testall\n\n# Filter tests\nbin/testall ruby-3.3\nbin/testall ar-7.1\nbin/testall ruby-3.3 ar-7.1\n\n# Run one specific line from test/matrix\nbin/test ruby-3.3 ar-7.1 sqlite3\n\n# Run a specific file\nbin/test ruby-3.3 ar-7.1 sqlite3 test/configure_test.rb\n\n# Run a specific test\nbin/test ruby-3.3 ar-7.1 sqlite3 N=test_configure_from_file\n\n# Use podman\nPODMAN=1 bin/testall\n```\n\n## License\n\nLicensed under the MIT License\n\nCopyright 2024 Jordan Hollinger\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjhollinger%2Fotr-activerecord","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjhollinger%2Fotr-activerecord","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjhollinger%2Fotr-activerecord/lists"}