Class: TursoLibsql::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/turso_libsql/database.rb

Overview

Database ラッパーリモート接続と Embedded Replica の両方をサポート

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode:, url: nil, token: nil, path: nil, sync_interval: 0) ⇒ Database

Returns a new instance of Database.



24
25
26
27
28
29
30
# File 'lib/turso_libsql/database.rb', line 24

def initialize(mode:, url: nil, token: nil, path: nil, sync_interval: 0)
  @mode = mode
  @url = url
  @token = token || ''
  @path = path
  @sync_interval = sync_interval
end

Class Method Details

.new_remote(url, token) ⇒ Object

リモート接続用 Database を作成



10
11
12
# File 'lib/turso_libsql/database.rb', line 10

def self.new_remote(url, token)
  new(mode: :remote, url: url, token: token)
end

.new_remote_replica(path, url, token, sync_interval_secs = 0) ⇒ Object

Embedded Replica 用 Database を作成



15
16
17
# File 'lib/turso_libsql/database.rb', line 15

def self.new_remote_replica(path, url, token, sync_interval_secs = 0)
  new(mode: :replica, path: path, url: url, token: token, sync_interval: sync_interval_secs)
end

.new_synced(path, url, token, sync_interval_secs = 0) ⇒ Object

Offline write 用 Database を作成



20
21
22
# File 'lib/turso_libsql/database.rb', line 20

def self.new_synced(path, url, token, sync_interval_secs = 0)
  new(mode: :offline, path: path, url: url, token: token, sync_interval: sync_interval_secs)
end

Instance Method Details

#connectObject

この Database から Connection を取得して返す



33
34
35
36
37
38
39
40
41
# File 'lib/turso_libsql/database.rb', line 33

def connect
  case @mode
  when :remote
    Connection.new(@url, @token)
  when :replica, :offline
    # ローカルファイルを開く(なければ作成される)
    LocalConnection.new(@path, @url, @token, @mode)
  end
end

#syncObject

リモートから最新フレームを手動で同期する



44
45
46
47
48
49
50
51
52
# File 'lib/turso_libsql/database.rb', line 44

def sync
  case @mode
  when :remote
    # remote モードでは no-op
    nil
  when :replica, :offline
    replica_sync(@path, @url, @token, @mode == :offline)
  end
end