Class: PromptObjects::Repositories::WorkspaceSessionRepository

Inherits:
BaseRepository
  • Object
show all
Defined in:
lib/prompt_objects/repositories/workspace_session_repository.rb

Instance Method Summary collapse

Methods inherited from BaseRepository

#initialize

Constructor Details

This class inherits a constructor from PromptObjects::Repositories::BaseRepository

Instance Method Details

#create(name: nil, status: "active", metadata: {}, id: SecureRandom.uuid) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/prompt_objects/repositories/workspace_session_repository.rb', line 6

def create(name: nil, status: "active", metadata: {}, id: SecureRandom.uuid)
  now = Time.now.utc.iso8601
  transaction do |db|
    db.execute(<<~SQL, [id, name, status, now, now, JSON.generate()])
      INSERT INTO workspace_sessions (id, name, status, created_at, updated_at, metadata)
      VALUES (?, ?, ?, ?, ?, ?)
    SQL
  end
  get(id)
end

#get(id) ⇒ Object



17
18
19
20
# File 'lib/prompt_objects/repositories/workspace_session_repository.rb', line 17

def get(id)
  row = read { |db| db.get_first_row("SELECT * FROM workspace_sessions WHERE id = ?", [id]) }
  row && parse(row)
end

#list(status: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/prompt_objects/repositories/workspace_session_repository.rb', line 22

def list(status: nil)
  rows = read do |db|
    if status
      db.execute("SELECT * FROM workspace_sessions WHERE status = ? ORDER BY updated_at DESC", [status])
    else
      db.execute("SELECT * FROM workspace_sessions ORDER BY updated_at DESC")
    end
  end
  rows.map { |row| parse(row) }
end

#update(id, name: nil, status: nil, metadata: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/prompt_objects/repositories/workspace_session_repository.rb', line 33

def update(id, name: nil, status: nil, metadata: nil)
  current = get(id)
  return nil unless current

  next_name = name.nil? ? current.name : name
  next_status = status || current.status
   =  ? current..merge() : current.
  now = Time.now.utc.iso8601
  transaction do |db|
    db.execute(
      "UPDATE workspace_sessions SET name = ?, status = ?, metadata = ?, updated_at = ? WHERE id = ?",
      [next_name, next_status, JSON.generate(), now, id]
    )
  end
  get(id)
end