Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

class ShopifyCli::DB

Kevin O'Sullivan edited this page Jun 28, 2021 · 5 revisions

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 Methods

keys

keys() 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
see source

# File lib/shopify-cli/db.rb, line 30
def keys
  db.transaction(true) { db.roots }
end

exists?

exists?(key) 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')
see source

# File lib/shopify-cli/db.rb, line 47
def exists?(key)
  db.transaction(true) { db.root?(key) }
end

set

set(**args) 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)
see source

# 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

get

get(key) { || ... } 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)
see source

# 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

del

del(*args) 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)
see source

# File lib/shopify-cli/db.rb, line 100
def del(*args)
  db.transaction { args.each { |key| db.delete(key) } }
end

clear

clear() Drops all keys from the database.

Usage

ShopifyCli::DB.clear
see source

# File lib/shopify-cli/db.rb, line 110
def clear
  del(*keys)
end

Extends

  • SingleForwardable
Clone this wiki locally