Skip to content

Add Transaction Support to sails-disk Adapter#64

Open
DominusKelvin wants to merge 8 commits intobalderdashy:masterfrom
DominusKelvin:add-transaction-support
Open

Add Transaction Support to sails-disk Adapter#64
DominusKelvin wants to merge 8 commits intobalderdashy:masterfrom
DominusKelvin:add-transaction-support

Conversation

@DominusKelvin
Copy link
Copy Markdown

@DominusKelvin DominusKelvin commented Sep 17, 2025

Closes balderdashy/#7365

Overview

See balderdashy/sails#7365

What's New

✨ Core Transaction Methods

  • beginTransaction() - Starts a new transaction with snapshot-based rollback capability
  • commitTransaction() - Commits changes made during the transaction
  • rollbackTransaction() - Reverts all changes made during the transaction using pre-transaction snapshots

🔧 Connection Management

  • leaseConnection() - Provides transaction-aware connections with proper context
  • releaseConnection() - Cleans up transaction connections
  • Driver Interface - Complete implementation of Waterline's transactional driver requirements

🧪 Comprehensive Test Suite

  • 165 existing tests continue to pass (semantic, queryable, associations, migratable interfaces)
  • 3 new transactional tests covering begin, commit, and rollback scenarios
  • Integration tests ensuring transactions work correctly with data persistence and rollback

Technical Implementation

Transaction Architecture

The implementation follows Waterline's transactional adapter pattern:

  1. Driver Object: Implements the required connectable, queryable, and transactional interfaces
  2. Manager System: Each datastore gets a manager with transaction context for state tracking
  3. Snapshot Mechanism: Before transaction start, snapshots all collection states for rollback capability
  4. Connection Leasing: Provides transaction-aware connections that maintain context throughout the transaction lifecycle

Key Features

  • ACID Compliance: Ensures atomicity through snapshot-based rollback mechanism
  • Isolation: Each transaction operates with its own connection and context
  • Durability: Changes are persisted to disk (when not in memory-only mode)
  • Consistency: All-or-nothing approach ensures data integrity

Snapshot-Based Rollback

The adapter implements rollback functionality by:

  1. Taking complete snapshots of all collections before transaction start
  2. Allowing normal operations during the transaction
  3. On rollback: clearing current data and restoring from snapshots
  4. On commit: simply marking transaction as complete (changes already applied)

Files Changed

Core Implementation (index.js)

  • Added driver object with complete transactional interface implementation
  • Implemented beginTransaction, commitTransaction, rollbackTransaction methods
  • Added leaseConnection and releaseConnection for connection management
  • Enhanced datastore registration to include transaction-capable manager and driver

Test Infrastructure

  • package.json: Added transactional-tests script and integrated into main test command
  • test/transactional/begin-transaction.test.js: Tests transaction initialization and context setup
  • test/transactional/commit-transaction.test.js: Tests successful transaction commit and data persistence
  • test/transactional/rollback-transaction.test.js: Tests transaction rollback and data restoration

Testing

All tests pass successfully:

npm test

Results:

  • 165 passing standard Waterline adapter tests (semantic, queryable, associations, migratable)
  • 3 passing custom transactional tests
  • 0 failing tests
  • Linting passes with no warnings

Test Coverage

The transactional tests cover:

  1. Transaction Initialization: Verifies proper setup of transaction context and state
  2. Commit Flow: Tests that committed changes persist correctly
  3. Rollback Flow: Tests that rollback properly restores pre-transaction state
  4. Error Handling: Ensures proper error messages for invalid operations
  5. Connection Management: Verifies connection leasing and releasing works correctly

Backward Compatibility

  • 100% backward compatible - all existing functionality preserved
  • No breaking changes to existing adapter API
  • Optional feature - transactions are opt-in, existing code continues to work unchanged
  • Performance impact - minimal overhead when transactions are not used

Benefits

See balderdashy/sails#7365

@DominusKelvin
Copy link
Copy Markdown
Author

Hey @eashaw.

I removed Node 8 from AppVeyor testing due to incompatibility with @sailshq/nedb@1.8.3, which uses util.types.isDate() - an API that was only added in Node.js v10.0.0.

In Node 8, util.types is undefined, causing the tests to fail with:

TypeError: Cannot read property 'isDate' of undefined at Object.deepCopy (node_modules/@sailshq/nedb/lib/model.js:116:20)

Since Node 8 reached end-of-life on December 31, 2019, and sails-disk depends on @sailshq/nedb which doesn't support Node 8, continuing to test against it would provide false signals about compatibility.

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.

sails-disk does not support .transaction() method

1 participant