Last active
October 5, 2023 14:35
-
-
Save yoav-lavi/fe57c355d3933b549fa0d4d03f36f5e7 to your computer and use it in GitHub Desktop.
A key value store for Scriptable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = storeName => { | |
const files = FileManager.iCloud(); | |
const documents = files.documentsDirectory(); | |
const stores = `${documents}/stores`; | |
const store = `${stores}/${storeName}`; | |
if (!files.isDirectory(stores)) { | |
files.createDirectory(stores); | |
} | |
if (!files.fileExists(store)) { | |
files.writeString(store, ''); | |
} | |
const set = (key, value) => | |
files.writeExtendedAttribute( | |
store, | |
JSON.stringify(value), | |
key | |
); | |
const get = key => | |
JSON.parse( | |
files.readExtendedAttribute(store, key) | |
); | |
const remove = key => | |
files.removeExtendedAttribute(store, key); | |
return { set, get, remove }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment