Skip to content

Add DynamoDB ORM support through Dynamoid and aws-record - #5886

Closed
sahild wants to merge 1 commit into
heartcombo:mainfrom
sahild:dynamodb-orm-adapter
Closed

sahild wants to merge 1 commit into
heartcombo:mainfrom
sahild:dynamodb-orm-adapter

Conversation

@sahild

@sahild sahild commented Nov 22, 2026

Copy link
Copy Markdown

Summary

Adds Amazon DynamoDB support to Devise's ORM layer, through either of two libraries:

You pick the library in the initializer, before requiring the ORM:

config.dynamodb_library = :dynamoid # or :aws_record
require 'devise/orm/dynamodb'

Supported modules: database_authenticatable, rememberable, validatable, confirmable (including reconfirmable), trackable, recoverable and lockable. registerable and timeoutable work as well, since they need no database support. omniauthable isn't supported yet.

Opening this as a draft to get feedback on whether this belongs in Devise itself or would be better as a separate gem.

What's included

  • Adapters: OrmAdapter implementations for both libraries (lib/devise/orm/dynamodb/). Lookups Query a global secondary index when one exists and fall back to a consistent Scan.
  • aws-record compatibility layer: aws-record has no validations, callbacks or dirty tracking. Devise::Orm::Dynamodb::AwsRecordModel adds the subset of the ActiveRecord API that Devise and Rails forms rely on.
  • Uniqueness validator: neither library ships one. This one matches ActiveRecord's options (case_sensitive, scope, allow_blank, conditions) and error message.
  • Lockable: failed_attempts is incremented with an atomic DynamoDB update, like increment_counter in ActiveRecord.
  • Generators: dynamodb:devise (plus dynamoid:devise and aws_record:devise). devise:install writes the DynamoDB setup when the ORM is dynamodb, dynamoid or aws_record.
  • Rake tasks: devise:dynamodb:create_tables and devise:dynamodb:check_indexes. The second one lists Devise lookups that would need a full table Scan.
  • Docs: a DynamoDB section in the README covering setup, models, indexes, and the behaviour that differs from ActiveRecord.
  • Sample app: examples/dynamodb_app has a User model for each library, with integration tests covering all 7 modules over HTTP. It isn't included in the gem package.

Differences from ActiveRecord

These are documented in the README.

  • Email uniqueness isn't enforced by the database. DynamoDB only enforces uniqueness on the primary key, so two concurrent sign ups with the same email can both pass validation.
  • No transactions around callbacks. If an after_* callback raises, the item has already been written.
  • Confirmation emails are sent from after_create/after_update, as with Mongoid, since there's no after_commit.
  • Global secondary indexes are eventually consistent.

Changes to existing files

  • lib/devise/orm.rb: detects Dynamoid and aws-record models and includes the matching integration.
  • lib/devise.rb: adds Devise.dynamodb_library and Devise::DYNAMODB_LIBRARIES.
  • config/locales/en.yml: adds errors.messages.taken, which ActiveRecord and Mongoid ship themselves.
  • test/support/locale/de.yml and test/helpers/devise_helper_test.rb: add the activemodel translation scope that DynamoDB models use.
  • test/models/confirmable_test.rb: the callback assertion's else branch now covers every ORM without after_commit.
  • CI matrix: adds dynamoid and aws_record, and starts DynamoDB Local for those jobs.
  • Gemfile and gemfiles/*: add dynamoid and aws-record groups.

Test plan

Run locally on Ruby 4.0 and Rails 8.1. The DynamoDB suites ran against moto, a DynamoDB emulator, because Docker wasn't available. CI uses DynamoDB Local instead, so the first CI run will be the first one against it.

  • DEVISE_ORM=active_record: 894 runs, 0 failures
  • DEVISE_ORM=dynamoid: 933 runs, 0 failures (the full existing suite, plus the new DynamoDB unit tests and generator tests)
  • DEVISE_ORM=aws_record: 933 runs, 0 failures
  • Dynamoid and aws-record on gemfiles/Gemfile-rails-8-0: 0 failures
  • Sample app with each library: 6 runs, 40 assertions, 0 failures
  • DEVISE_ORM=mongoid: not run locally (no MongoDB server). The Mongoid code path is unchanged.
  • CI across the Ruby and Rails matrix, including DynamoDB Local

🤖 Generated with Claude Code

Adds a DynamoDB adapter to Devise's ORM layer. The library is selected
with `config.dynamodb_library` (:dynamoid by default, or :aws_record)
before `require 'devise/orm/dynamodb'`.

Supported modules: database_authenticatable, rememberable, validatable,
confirmable (including reconfirmable), trackable, recoverable and
lockable. registerable and timeoutable work as well.

- OrmAdapter implementations for both libraries. Lookups Query a global
  secondary index when one exists and fall back to a consistent Scan.
- An ActiveRecord-compatible layer for aws-record models (validations,
  callbacks, dirty tracking, create/update/find/reload/destroy).
- A uniqueness validator matching ActiveRecord's options and messages.
- Atomic failed_attempts increments for Lockable.
- dynamodb:devise, dynamoid:devise and aws_record:devise generators, and
  DynamoDB support in devise:install.
- devise:dynamodb:create_tables and devise:dynamodb:check_indexes tasks.
- The full suite runs with DEVISE_ORM=dynamoid and DEVISE_ORM=aws_record,
  plus DynamoDB-specific unit tests and generator tests. CI starts
  DynamoDB Local for these jobs.
- README section with setup steps and the behaviour that differs from
  ActiveRecord, and a sample app in examples/dynamodb_app.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@sahild sahild closed this Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant