Class: ShopifyCLI::DB

Inherits:
Object
  • Object
show all
Extended by:
SingleForwardable
Defined in:
lib/shopify_cli/db.rb

Overview

Persists transient data like access tokens that may be cleared when user clears their session

All of the instance methods documented here can be used as class methods. All class methods are forwarded to a new instance of the database, pointing at the default path.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: File.join(ShopifyCLI.cache_dir, ".db.pstore")) ⇒ DB

:nodoc:



17
18
19
# File 'lib/shopify_cli/db.rb', line 17

def initialize(path: File.join(ShopifyCLI.cache_dir, ".db.pstore")) # :nodoc:
  @db = PStore.new(path)
end

Instance Attribute Details

#dbObject (readonly)

:nodoc:



15
16
17
# File 'lib/shopify_cli/db.rb', line 15

def db
  @db
end

Instance Method Details

#clearObject

Drops all keys from the database.

#### Usage

ShopifyCLI::DB.clear


110
111
112
# File 'lib/shopify_cli/db.rb', line 110

def clear
  del(*keys)
end

#del(*args) ⇒ Object

Deletes a value from the local storage

#### Parameters

  • ‘*args`: an array of strings or symbols that are keys to be removed from the database

#### Usage

ShopifyCLI::DB.del(:shopify_exchange_token)


100
101
102
# File 'lib/shopify_cli/db.rb', line 100

def del(*args)
  db.transaction { args.each { |key| db.delete(key) } }
end

#exists?(key) ⇒ Boolean

Check to see if a key exists in the database, the key will only exist if it has a value so if the key exists then there is also a value.

#### Parameters

  • ‘key`: a string or a symbol representation of a key that is stored in the DB

#### Returns

  • ‘exists`: a boolean value if the key exists in the database

#### Usage

exists = ShopifyCLI::DB.exists?('shopify_exchange_token')

Returns:

  • (Boolean)


47
48
49
# File 'lib/shopify_cli/db.rb', line 47

def exists?(key)
  db.transaction(true) { db.root?(key) }
end

#get(key) ⇒ Object

Gets a value from the DB that is associated with the supplied key

#### Parameters

  • ‘key`: a string or a symbol representation of a key that is stored in the DB

#### Returns

  • ‘value`: will be the previously saved value or nil if the key does not exist in the database.

#### Usage

ShopifyCLI::DB.get(:shopify_exchange_token)


85
86
87
88
89
# File 'lib/shopify_cli/db.rb', line 85

def get(key)
  val = db.transaction(true) { db[key] }
  val = yield if val.nil? && block_given?
  val
end

#keysObject

Get all keys that exist in the database.

#### Returns

  • ‘keys`: an array of string or symbol keys that exist in the database

#### Usage

ShopifyCLI::DB.keys


30
31
32
# File 'lib/shopify_cli/db.rb', line 30

def keys
  db.transaction(true) { db.roots }
end

#set(**args) ⇒ Object

Persist a value by key in the local storage

#### Parameters

  • ‘**args`: a hash of keys and values to persist in the database

#### Usage

ShopifyCLI::DB.set(shopify_exchange_token: 'token', metric_consent: true)


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/shopify_cli/db.rb', line 60

def set(**args)
  db.transaction do
    args.each do |key, val|
      if val.nil?
        db.delete(key)
      else
        db[key] = val
      end
    end
  end
end