This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 201
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.
keys()
Get all keys that exist in the database.
-
keys
: an array of string or symbol keys that exist in the database
ShopifyCli::DB.keys
see source
# File lib/shopify-cli/db.rb, line 30
def keys
db.transaction(true) { db.roots }
end
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.
-
key
: a string or a symbol representation of a key that is stored in the DB
-
exists
: a boolean value if the key exists in the database
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(**args)
Persist a value by key in the local storage
-
**args
: a hash of keys and values to persist in the database
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(key) { || ... }
Gets a value from the DB that is associated with the supplied key
-
key
: a string or a symbol representation of a key that is stored in the DB
-
value
: will be the previously saved value or nil if the key does not exist in the database.
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(*args)
Deletes a value from the local storage
-
*args
: an array of strings or symbols that are keys to be removed from the database
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()
Drops all keys from the database.
ShopifyCli::DB.clear
see source
# File lib/shopify-cli/db.rb, line 110
def clear
del(*keys)
end
- SingleForwardable