Atomically alternatives and similar gems
Based on the "ORM/ODM Extensions" category.
Alternatively, view Atomically alternatives based on common mentions on social networks and blogs.
-
ActsAsTaggableOn
A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts. -
ActiveRecord Import
A library for bulk insertion of data into your database using ActiveRecord. -
Audited
Audited (formerly acts_as_audited) is an ORM extension that logs all changes to your Rails models. -
Apartment
Database multi-tenancy for Rack (and Rails) applications -
PublicActivity
Easy activity tracking for models - similar to Github's Public Activity -
Awesome Nested Set
An awesome replacement for acts_as_nested_set and better_nested_set. -
Closure Tree
Easily and efficiently make your ActiveRecord models support hierarchies -
Enumerize
Enumerated attributes with I18n and ActiveRecord/Mongoid support -
Ruby JSON Schema Validator
Ruby JSON Schema Validator -
ActiveRecord Reputation System
An Active Record Reputation System for Rails -
Acts As Tennant
Easy multi-tenancy for Rails in a shared database setup. -
ActsAsParanoid
ActiveRecord plugin allowing you to hide and restore records without actually deleting them. -
dry-validation
Validation library with type-safe schemas and rules -
acts_as_follower
A Gem to add Follow functionality for models -
ranked-model
An acts_as_sortable/acts_as_list replacement built for Rails 4, 5 and 6 -
ActiveRecordExtended
Adds additional postgres functionality to an ActiveRecord / Rails application -
Acts As Commentable
The ActiveRecord acts_as_commentable plugin -
Acts As Commentable with Threading
Similar to acts_as_commentable; however, utilizes awesome_nested_set to provide threaded comments -
Rails PG Extras
Rails PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more. -
Unread
Handle unread records and mark them as read with Ruby on Rails -
activerecord-multi-tenant
Rails/ActiveRecord support for distributed multi-tenant databases like Postgres+Citus -
StoreModel
Work with JSON-backed attributes as ActiveRecord-ish models -
ActsAsTree
ActsAsTree -- Extends ActiveRecord to add simple support for organizing items into parent–children relationships. -
mongoid-history
Multi-user non-linear history tracking, auditing, undo, redo for mongoid. -
ArLazyPreload
Lazy loading associations for the ActiveRecord models -
activerecord_json_validator
🔩 ActiveRecord::JSONValidator makes it easy to validate JSON attributes against a JSON schema. -
ActiveImporter
Define importers that load tabular data from spreadsheets or CSV files into any ActiveRecord-like ORM. -
Mongoid Tree
A tree structure for Mongoid documents using the materialized path pattern -
arel-helpers
Useful tools to help construct database queries with ActiveRecord and Arel. -
ActiveValidators
Collection of ActiveModel/ActiveRecord validators -
PermenantRecords
Rails Plugin - soft-delete your ActiveRecord records. It's like an explicit version of ActsAsParanoid -
data_miner
Download, unpack from a ZIP/TAR/GZ/BZ2 archive, parse, correct, convert units and import Google Spreadsheets, XLS, ODS, XML, CSV, HTML, etc. into your ActiveRecord models. Uses RemoteTable gem internally. -
ActiveRecord::Turntable
ActiveRecord Sharding Plugin -
mini_record
ActiveRecord meets DataMapper, with MiniRecord you are be able to write schema inside your models. -
Espinita
Audit activerecord models like a boss (and works with rails 4!)
Clean code begins in your IDE with SonarLint
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of Atomically or a related project?
README
Atomically
atomically
is a Ruby Gem for you to write atomic query with ease.
All methods are defined in Atomically::QueryService
instead of defining in ActiveRecord
directly, in order not to pollute the model instance.
Supports
- Ruby 2.2 ~ 2.7
- Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0
- MySQL, PostgreSQL
Table of contents
- Installation
- Methods
- Relation Methods
- Model Methods
- Development
- Contributing
- License
Installation
Add this line to your application's Gemfile:
gem 'atomically'
And then execute:
$ bundle
Or install it yourself as:
$ gem install atomically
Methods
Note: ActiveRecord validations and callbacks will NOT be triggered when calling below methods.
create_or_plus (columns, values, on_duplicate_update_columns, conflict_target:)
Import an array of records. When key is duplicate, plus the old value with new value.
It is useful to add items
to user
when user_items
may not exist. (Let User
and Item
are many-to-many relationship.)
Parameters
- First two args (
columns
,values
) are the same with the import method. on_duplicate_update_columns
- The column that will be updated on duplicate.conflict_target
- Needed only in pg. Specifies which columns have unique index.
Example
class User < ApplicationRecord
has_many :user_items
has_many :items, through: :user_items
end
class UserItem < ApplicationRecord
belongs_to :user
belongs_to :item
end
class Item < ApplicationRecord
has_many :user_items
has_many :users, through: :user_items
end
user = User.find(2)
item1 = Item.find(1)
item2 = Item.find(2)
columns = [:user_id, :item_id, :quantity]
values = [[user.id, item1.id, 3], [user.id, item2.id, 2]]
# mysql
UserItem.atomically.create_or_plus(columns, values, [:quantity])
# pg
UserItem.atomically.create_or_plus(columns, values, [:quantity], conflict_target: [:user_id, :item_id])
before
after
SQL queries
# mysql
INSERT INTO `user_items` (`user_id`,`item_id`,`quantity`,`created_at`,`updated_at`) VALUES
(2,1,3,'2018-11-27 03:44:25','2018-11-27 03:44:25'),
(2,2,2,'2018-11-27 03:44:25','2018-11-27 03:44:25')
ON DUPLICATE KEY UPDATE
`quantity` = `quantity` + VALUES(`quantity`)
# pg
INSERT INTO "user_items" ("user_id","item_id","quantity","created_at","updated_at") VALUES
(2,1,3,'2018-11-27 03:44:25.847909','2018-11-27 03:44:25.847909'),
(2,2,2,'2018-11-27 03:44:25.847909','2018-11-27 03:44:25.847909')
ON CONFLICT (user_id, item_id) DO UPDATE SET
"quantity" = "user_items"."quantity" + excluded."quantity" RETURNING "id"
pay_all (hash, update_columns, primary_key: :id)
Reduce the quantity of items and return how many rows and updated if all of them are enough. Do nothing and return zero if any of them is not enough.
Parameters
hash
- A hash contains the id of the models as keys and the amount to update the field by as values.update_columns
- The column that will be updated.primary_key
- Specify the column thatid
(the key of hash) refers to.
Example
user.user_items.atomically.pay_all({ item1.id => 4, item2.id => 3 }, [:quantity], primary_key: :item_id)
# => 2 (if success)
# => 0 (if some aren't enough)
SQL queries
UPDATE `user_items` SET `quantity` = `quantity` + (@change :=
CASE `item_id`
WHEN 1 THEN -4
WHEN 2 THEN -3
END)
WHERE `user_items`.`user_id` = 1 AND (
`user_items`.`item_id` = 1 AND (`quantity` >= 4) OR `user_items`.`item_id` = 2 AND (`quantity` >= 3)
) AND (
(
SELECT COUNT(*) FROM (
SELECT `user_items`.* FROM `user_items`
WHERE `user_items`.`user_id` = 1 AND (
`user_items`.`item_id` = 1 AND (`quantity` >= 4) OR `user_items`.`item_id` = 2 AND (`quantity` >= 3)
)
) subquery
) = 2
)
update_all (expected_number, updates)
Behaves like ActiveRecord::Relation#update_all but add an additional constrain that the number of affected rows equals to what you specify.
Parameters
expected_number
- The number of rows that you expect to be updated.updates
- A string, array, or hash representing the SET part of an SQL statement.
Examples
User.where(id: [5, 6]).atomically.update_all(2, name: '')
# => 2 (success)
User.where(id: [7, 8, 9]).atomically.update_all(2, name: '')
# => 0 (fail)
SQL queries
# User.where(id: [7, 8, 9]).atomically.update_all(2, name: '')
UPDATE `users` SET `users`.`name` = '' WHERE `users`.`id` IN (7, 8, 9) AND (
(
SELECT COUNT(*) FROM (
SELECT `users`.* FROM `users` WHERE `users`.`id` IN (7, 8, 9)
) subquery
) = 2
)
update_all_and_get_ids (updates)
Behaves like ActiveRecord::Relation#update_all, but return an array of updated records' id instead of the number of updated records.
Parameters
updates
- A string, array, or hash representing the SET part of an SQL statement.
Example
User.where(account: ['moon', 'wolf']).atomically.update_all_and_get_ids('money = money + 1')
# => [254, 371] (array of updated user ids)
User.where(account: ['moon', 'wolf']).update_all('money = money + 1')
# => 2 (the number of updated records)
SQL queries
# mysql
BEGIN
SET @ids := NULL
UPDATE `users` SET money = money + 1 WHERE `users`.`account` IN ('moon', 'wolf') AND ((SELECT @ids := CONCAT_WS(',', `users`.`id`, @ids)))
SELECT @ids FROM DUAL
COMMIT
# pg
UPDATE 'users' SET money = money + 1 RETURNING id
update (attrs, from: :not_set)
Updates the attributes of the model from the passed-in hash and saves the record. Return true if update successfully, false otherwise. This method can detect race condition and make sure the model is updated only once.
The difference between this method and ActiveRecord#update is that it will add extra WHERE conditions to prevent race condition.
Parameters
attrs
- Same with the first parameter of ActiveRecord#updatefrom
- The value before update. If not set, use the current attriutes of the model.
Example
class Arena < ApplicationRecord
def atomically_close!
atomically.update(closed_at: Time.now)
end
def close!
update(closed_at: Time.now)
end
end
Let arena.closed_at
be nil.
arena.atomically_close!
# => true (if success)
# => false (if race condition occurs)
The return value can be used to prevent race condition and make sure some piece of code is executed only once.
if arena.atomically_close!
# Only one request can pass this check and execute the code here.
# You can send rewards, calculate ranking, or fire background job here.
# No need to worry about being invoked multiple times.
do_something
end
SQL queries
# arena.atomically_close!
UPDATE `arenas` SET `arenas`.`closed_at` = '2018-11-27 03:44:25', `updated_at` = '2018-11-27 03:44:25'
WHERE `arenas`.`id` = 1752 AND `arenas`.`closed_at` IS NULL
# arena.close!
UPDATE `arenas` SET `arenas`.`closed_at` = '2018-11-27 03:44:25', `updated_at` = '2018-11-27 03:44:25'
WHERE `arenas`.`id` = 1752
decrement_unsigned_counters (counters)
Decrement numeric fields via a direct SQL update, and make sure that it will not become negative.
Return true if update successfully, false otherwise.
Parameters
counters
- A Hash containing the names of the fields to update as keys and the amount to update the field by as values.
Example
user.money
# => 100
user.atomically.decrement_unsigned_counters(money: 10)
# => true (success)
user.reload.money
# => 90
user.atomically.decrement_unsigned_counters(money: 999)
# => false (fail)
user.reload.money
# => 90
SQL queries
# user.atomically.decrement_unsigned_counters(money: 140)
UPDATE `users` SET money = money - 140 WHERE `users`.`id` = 1 AND (money >= 140)
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test DB=mysql
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To 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.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/khiav223577/atomically. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
*Note that all licence references and agreements mentioned in the Atomically README section above
are relevant to that project's source code only.