Get Prop
import { storages } from 'stagync'
const { websites } = storages
async function getProps(){
const urlsAlt = await websites.get('urls')
console.log('urlsAlt', urlsAlt)
// or
const urlsProps = await websites.props.urls.get()
console.log('urlsProps', urlsProps)
}
getProps()
Line by Line
- Line 1: In the first line of this example, we import all available arrays. This is possible thanks to the
createStoragefunction presented in the previous step. - Line 3: Next we look at
websitesof thestoragesobject, this brings more clarity to the code. But we could simply usestorages.websites. - Line 5: Most features available in a storage are excreted asynchronously. For better readability we use a function declaring
asyncas the scope of our application. - Line 6: Finally we retrieve the value of the
urlsproperty, as we haven’t modified it yet we will retrieve the default value. - Line 9: Another way to retrieve the
urlsproperty is by usingpropsresources, it abstracts and adds new features to your property, such asaddandremovethat can be used to enter new values in properties of type array and object. - Line 13: Finally, let’s execute the
getPropsfunction. You will see on your application console the default value of theurlsproperty.
await websites.set('urls', ['http://nodejs.com'])
// or
await websites.props.urls.set(['http://nodejs.com'])
// Insert
await websites.props.urls.add('http://npmjs.com')
Now let’s modify and insert new items to the urls property.
Using set will override the value, while user add will enter new values into the array.
Add can only be run from props.
At this point the value of the urls property should be this:
['http://nodejs.com', 'http://npmjs.com']