GithubHelp home page GithubHelp logo

dominic0312 / otr-activerecord Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jhollinger/otr-activerecord

0.0 1.0 0.0 47 KB

Off The Rails: Use ActiveRecord with Grape, Sinatra, Rack, or anything else!

License: MIT License

Ruby 100.00%

otr-activerecord's Introduction

otr-activerecord

An 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:

  • ActiveRecord 4
  • ActiveRecord 5
  • ActiveRecord 6

How to use

1. Add it to your Gemfile

gem "otr-activerecord"

2. Configure your database connection

After loading your gems, tell OTR::ActiveRecord about your database config using one of the following examples:

OTR::ActiveRecord.configure_from_file! "config/database.yml"
OTR::ActiveRecord.configure_from_url! ENV['DATABASE_URL'] # e.g. postgres://user:pass@host/db
OTR::ActiveRecord.configure_from_hash!(adapter: "postgresql", host: "localhost", database: "db", username: "user", password: "pass", encoding: "utf8", pool: 10, timeout: 5000)

Important note: configure_from_file! won't work as expected if you have already DATABASE_URL set as part of your environment variables. This is because in ActiveRecord when that env variable is set it will merge its properties into the current connection configuration.

3. Enable middleware for Rack apps

Add these middlewares in config.ru:

# Clean up database connections after every request (required)
use OTR::ActiveRecord::ConnectionManagement

# Enable ActiveRecord's QueryCache for every request (optional)
use OTR::ActiveRecord::QueryCache

4. Import ActiveRecord tasks into your Rakefile

This will give you most of the standard db: tasks you get in Rails. Add it to your Rakefile.

require "bundler/setup"
load "tasks/otr-activerecord.rake"

namespace :db do
  # Some db tasks require your app code to be loaded; they'll expect to find it here
  task :environment do
    require_relative "app"
  end
end

Unlike in Rails, creating a new migration is also a rake task. Run bundle exec rake -T to get a full list of tasks.

bundle exec rake db:create_migration[create_widgets]

Examples

Look under /examples for some example apps.

Advanced options

The 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:

OTR::ActiveRecord.db_dir = 'db'
OTR::ActiveRecord.migrations_paths = ['db/migrate']
OTR::ActiveRecord.fixtures_path = 'test/fixtures'
OTR::ActiveRecord.seed_file = 'seeds.rb'

License

Licensed under the MIT License

Copyright 2016 Jordan Hollinger

otr-activerecord's People

Contributors

jhollinger avatar vidok avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.