Class: PromptObjects::Repositories::EnvDataRepository

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

Defined Under Namespace

Classes: Mutation, Removal

Instance Method Summary collapse

Methods inherited from BaseRepository

#initialize

Constructor Details

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

Instance Method Details

#delete(workspace_session_id:, key:, stored_by:) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/prompt_objects/repositories/env_data_repository.rb', line 60

def delete(workspace_session_id:, key:, stored_by:)
  delete_with_cursor(
    workspace_session_id: workspace_session_id,
    key: key,
    stored_by: stored_by
  ).deleted
end

#delete_with_cursor(workspace_session_id:, key:, stored_by:) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/prompt_objects/repositories/env_data_repository.rb', line 68

def delete_with_cursor(workspace_session_id:, key:, stored_by:)
  transaction do |db|
    db.execute(
      "DELETE FROM env_data WHERE workspace_session_id = ? AND key = ?",
      [workspace_session_id, key]
    )
    next Removal.new(deleted: false, cursor: nil) unless db.changes.positive?

    cursor = append_event(db, workspace_session_id, "delete", key, stored_by, Time.now.utc.iso8601)
    Removal.new(deleted: true, cursor: cursor)
  end
end

#get(workspace_session_id:, key:) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/prompt_objects/repositories/env_data_repository.rb', line 41

def get(workspace_session_id:, key:)
  row = read do |db|
    db.get_first_row(
      "SELECT * FROM env_data WHERE workspace_session_id = ? AND key = ?",
      [workspace_session_id, key]
    )
  end
  row && parse(row)
end

#list(workspace_session_id:) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/prompt_objects/repositories/env_data_repository.rb', line 51

def list(workspace_session_id:)
  read do |db|
    db.execute(
      "SELECT * FROM env_data WHERE workspace_session_id = ? ORDER BY key ASC",
      [workspace_session_id]
    ).map { |row| parse(row) }
  end
end

#set(**attributes) ⇒ Object



9
10
11
# File 'lib/prompt_objects/repositories/env_data_repository.rb', line 9

def set(**attributes)
  set_with_cursor(**attributes).entry
end

#set_with_cursor(workspace_session_id:, key:, short_description:, value:, stored_by:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/prompt_objects/repositories/env_data_repository.rb', line 13

def set_with_cursor(workspace_session_id:, key:, short_description:, value:, stored_by:)
  now = Time.now.utc.iso8601
  transaction do |db|
    params = [
      workspace_session_id, workspace_session_id, workspace_session_id,
      key, short_description, JSON.generate(value), stored_by, now, now
    ]
    db.execute(<<~SQL, params)
      INSERT INTO env_data (
        root_thread_id, session_id, workspace_session_id, key,
        short_description, value, stored_by, created_at, updated_at
      ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
      ON CONFLICT(root_thread_id, key) DO UPDATE SET
        workspace_session_id = excluded.workspace_session_id,
        session_id = excluded.session_id,
        short_description = excluded.short_description,
        value = excluded.value,
        stored_by = excluded.stored_by,
        updated_at = excluded.updated_at
    SQL
    cursor = append_event(db, workspace_session_id, "store", key, stored_by, now)
    entry = parse(db.get_first_row(<<~SQL, [workspace_session_id, key]))
      SELECT * FROM env_data WHERE workspace_session_id = ? AND key = ?
    SQL
    Mutation.new(entry: entry, cursor: cursor)
  end
end