Class: Ashikawa::Core::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/ashikawa-core/transaction.rb

Overview

A JavaScript Transaction on the database

Instance Method Summary collapse

Constructor Details

#initialize(database, action, options) ⇒ Transaction

Initialize a Transaction

Examples:

Create a Transaction

transaction = Ashikawa::Core::Transaction.new(database, 'function () { return 5; }',
  :read => ['collection_1']

Parameters:

  • database (Database)
  • action (String)

    An action written in JavaScript

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :write (Array<String>)

    The collections you want to write to

  • :read (Array<String>)

    The collections you want to read from



76
77
78
79
80
81
82
83
# File 'lib/ashikawa-core/transaction.rb', line 76

def initialize(database, action, options)
  @database = database
  @request_parameters = {
    action: action,
    collections: options,
    waitForSync: false
  }
end

Instance Method Details

#execute(action_parameters = :no_params_provided) ⇒ Object

Execute the transaction

Examples:

Run a Transaction

transaction.execute({ :a => 5 })

Parameters:

  • action_parameters (Object) (defaults to: :no_params_provided)

    The parameters for the defined action

Returns:

  • Object The result of the transaction



92
93
94
95
96
# File 'lib/ashikawa-core/transaction.rb', line 92

def execute(action_parameters = :no_params_provided)
  @request_parameters[:params] = action_parameters unless action_parameters == :no_params_provided
  response = @database.send_request('transaction', post: @request_parameters)
  response['result']
end

#lock_timeoutFixnum

An optional numeric value used to set a timeout for waiting on collection locks

Examples:

Check how long the lock timeout is

transaction.lock_timeout # => 30

Returns:

  • (Fixnum)


52
53
54
# File 'lib/ashikawa-core/transaction.rb', line 52

def lock_timeout
  @request_parameters[:lockTimeout]
end

#lock_timeout=(timeout) ⇒ Object

An optional numeric value used to set a timeout for waiting on collection locks

Examples:

Set the lock timeout to 30

transaction.lock_timeout = 30

Parameters:

  • timeout (Fixnum)


62
63
64
# File 'lib/ashikawa-core/transaction.rb', line 62

def lock_timeout=(timeout)
  @request_parameters[:lockTimeout] = timeout
end

#read_collectionsArray<String>

The collections the transaction reads from

Examples:

Get the collections that the transaction reads from

transaction.read_collections # => ['collection_1']

Returns:

  • (Array<String>)


22
23
24
# File 'lib/ashikawa-core/transaction.rb', line 22

def read_collections
  @request_parameters[:collections][:read]
end

#wait_for_syncBoolean

If set to true, the transaction will write all data to disk before returning

Examples:

Check, if the transaction waits for sync

transaction.wait_for_sync #=> false

Returns:

  • (Boolean)


32
33
34
# File 'lib/ashikawa-core/transaction.rb', line 32

def wait_for_sync
  @request_parameters[:waitForSync]
end

#wait_for_sync=(wait_for_sync) ⇒ Object

If set to true, the transaction will write all data to disk before returning

Examples:

Activate wait sync

transaction.wait_for_sync = true

Parameters:

  • wait_for_sync (Boolean)


42
43
44
# File 'lib/ashikawa-core/transaction.rb', line 42

def wait_for_sync=(wait_for_sync)
  @request_parameters[:waitForSync] = wait_for_sync
end

#write_collectionsArray<String>

The collections the transaction writes to

Examples:

Get the collections that the transaction writes to

transaction.write_collections # => ['collection_1']

Returns:

  • (Array<String>)


12
13
14
# File 'lib/ashikawa-core/transaction.rb', line 12

def write_collections
  @request_parameters[:collections][:write]
end