https://github.com/aman5438/db_helper
A lightweight Ruby gem to simplify database operations using ActiveRecord – check tables, create tables, insert data dynamically, and support multiple adapters (MySQL, PostgreSQL, SQLite).
https://github.com/aman5438/db_helper
activerecord database gem mysql postgresql ruby utility-gem
Last synced: 3 months ago
JSON representation
A lightweight Ruby gem to simplify database operations using ActiveRecord – check tables, create tables, insert data dynamically, and support multiple adapters (MySQL, PostgreSQL, SQLite).
- Host: GitHub
- URL: https://github.com/aman5438/db_helper
- Owner: aman5438
- License: mit
- Created: 2025-04-22T07:43:04.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-22T07:49:30.000Z (about 1 year ago)
- Last Synced: 2025-04-24T05:00:04.997Z (about 1 year ago)
- Topics: activerecord, database, gem, mysql, postgresql, ruby, utility-gem
- Language: Ruby
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
Installation
Add this line to your application's Gemfile:
gem 'db_helper', git: 'https://github.com/yourname/db_helper' # or use gem version if published
then run
bundle install
or install manually
gem install db_helper
Usage
1. Check Adapter Type
DbHelper.adapter
# => :sqlite3 or :mysql2 or :postgresql
2. Check if a Table Exists
DbHelper.table_exists?("users")
# => true or false
3. Create a Table (if it doesn't exist)
DbHelper.create_table("users", {
name: :string,
email: :string
})
This will create a users table with name, email, and timestamps.
4. Insert Data Dynamically
DbHelper.dynamic_insert("users", {
name: "John Doe",
email: "john@example.com"
})
The table must exist before inserting.
5. Get Current Database Name
DbHelper.current_database
# => "db_helper_test" or ":memory:" etc.
Testing the Gem
To run tests:
bundle exec rspec
Use environment variables to test with different databases:
DB_ADAPTER=mysql2 DATABASE_NAME=db_helper_test USERNAME=root PASSWORD=root bundle exec rspec
Directory Structure
db_helper/
├── lib/
│ ├── db_helper.rb
│ └── db_helper/version.rb
├── spec/
│ └── db_helper_spec.rb
├── db_helper.gemspec
└── README.md
Future Improvements
Add support for schema migrations
Support validations on dynamic inserts
Add more DB introspection helpers
👤 Author
Aman Agrawal