How to get a single item in storage layer with fs or github adapter #1455
-
I have a reproduction for the storage layer where I want to use export default defineNitroConfig({
devStorage: {
db: {
driver: 'fs',
base: './lang',
},
},
storage: {
db: {
driver: 'github',
repo: 'learnetheropes/nitro-storage-adapter',
branch: 'main',
dir: '/lang',
},
},
});
How do I get/set a specific property in one of the json files in the folder? I can get the whole file in the folder like so: const { hallo } = await useStorage('db').getItem(`${lang}.json`); But I can't get a specific property, because the following code get the error: const hallo = await useStorage('db').getItem(`${lang}.json:hallo`); Also, if I try to get all the keys in the file, I get an empty array, when in reality there is an const keys = await useStorage('db').getKeys(`${lang}.json`);
console.log(keys); // [] Getting the whole file and deconstruct the property is doable, but until I don't solve this, I'm not able to use Reproduction where I successfully retrieve the whole file: Reproduction where I fail to retrieve a specific key/item |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Load the json file first. const json = await useStorage('db').getItem(`${lang}.json`);
return json.hallo; I don't understand, what are you trying to achieve ? const json = await useStorage('db').getItem(`${lang}.json`);
const newJson = {...json, hallo: "hello there"}
await useStorage('db').setItem(`${lang}.json`, newJson); |
Beta Was this translation helpful? Give feedback.
Load the json file first.
getItem
will parse the json file into a JS object, so you can access its property directly.I don't understand, what are you trying to achieve ?
If you want to modify that property with
setItem
, you have to pass the entire modified file, not just the property :