Overview
Data can hosted in your media folder (subject to user limits) through the Home app. You can upload a new file that contains data and manage permissions on the file like any other media.
Scripting
Data can also be pushed to the server through a script. The $url service will push new content to your media folder when you call the "/post-media/" service.
The following arguments can be passed to the /post-media/ service.
- name
- content - a string representing the content
- id (optional) - the id of the media being uploaded. if no id is provided, an id is assigned. If an id is provided, the media that has the provided id is replaced
- folderid (optional) - the id of the folder where you wish to place the media. If not provided, the media is placed in your root folder. If the media id is provided (see above), the folderid is ignored.
let data= [1,2,3,4,5];
let id = await $url({
url: '/post-media/',
data: {
name:'data1.json',
content:JSON.stringify(data),
id:guid,
folderid:'data-folder'
}
});
The call to post-media will return the id of the media, which can be used later to replace the media.
Adding Data
You can add data by making a simple call to /post-media/.
let data= [1,2,3,4,5];
let id = await $url({
url: '/post-media/',
data: {
name:'data1.json',
content:JSON.stringify(data),
folderid:'da52b693-27ee-4a02-907d-160811a3112f' //optional
}
});
The folderid can be gotten from the home app, by clicking the copy id link underneath the folder path.
Updating Data
After adding a dataset, you can update the data by passing in the id in the call to /post-media/.
await $url({
url: '/post-media/',
data: {
id:guid,
content:JSON.stringify(data),
}
});
Retrieving Data
Once you have added a dataset, you can retrieve the data using the URL assigned to it. (this URL uses the id assigned, but also can be retrieved by opening the dataset from your home app)
let data = await $url('/content/487323a4-267a-4a14-a182-436272a4d879.json');
Listing Folder contents
If you place a number of data files into a single directory, you may want to retrieve the names of the files. This can be accomplished as follows.
let data = await $url('/file/username/foldername/');
let items = JSON.parse(data);
The path is of the format '/file/username/' + file path, where username is your username, and then the file path is the concatenated path of folder names with the file name at the end.
For example, if you have a file named, "data.json", that is in a folder named "price-data" which is in a folder named "data", the path would be "/file/username/data/price-data/data.json".