I would really want to be able to create, modify and work with static files inside Wix Code and the folders.
Imagine if there was a possibility to create static files using the below sample code ideas in Wix Code. We could query database and store results in static JSON files which would be a faster way to connect a repeater and filter data instead of hooking up to the data collections every time. We could also use this functionality to store other static files as HTML files or any filetype that would be allowed by Wix platform.
For security you can not work with files in backend folders from public or page code and the other way around.
import {wixFiles} from 'wix-files';
let jsonContent = {
name: "Andreas Kviby",
site: "cixshow.com",
email: "hello@wixshow.com"
}
// This code will save a file as a static json file
// in the backend folder with filename members.json
// with the content of the jsonContent variable
// If file exists it will be overwritten
wixFiles.save("backend", "members.json", jsonContent);
// When file is saved in the backend folder it can
// only be reached by backend code, not frontend
// This code will save a file as a static json file
// in the public folder with filename members.json
// with the content of the jsonContent variable.
// If file exists it will be overwritten
wixFiles.save("public", "members.json", jsonContent);
// When file is saved in the public folder it can
// be reached by public scripts and page code but
// not from backend code
// This code will load the file members.json from
// the backend folder. If this script would be
// executed from page code or public folder it
// will return permission denied
let jsonContent = wixFiles.load("backend", "members.json");
// This code will delete the file members.json from
// the public folder.
wixFiles.delete("public", "members.json");
// This code will append variable jsonContent to
// the existing file members.json
wixFiles.append("public", "members.json", jsonContent);
I strongly believe this would benefit the Wix Code platform as a full blown platform for developers. I would need it today
If you extend this idea a bit more you could also use it to cache data collection queries in a smart way and also allow export / import of data in a clever way. Let’s say we add a new function to wixData called staticQuery which will ask for filename and cache interval in minutes.
// This new function will use staticQuery which means that
// it will first check for a file in public folder named
// myTopTenMovies.json and if found it will check if it
// was created within the minutes condition, in this sample
// 60 minutes. If passed condition it will read in the JSON
// file as the query result for you, not hitting the data
// collection at all. If the file is older than 60 minutes
// it will query the data collection and then save that result
// into the file and then use the file for 60 minutes.
wixData.staticQuery("DataCollection", "public/myTopTenMovies.json", 60)
.eq("Field", "Value")
.then((results) => {
})
Using this would be superb for server performance in all sites using Data Collections in any way.
Then if you have files in public folder you can give other systems, clients, APIs access to those with domainname.com/static/public/myTopTenMovies.json and people could download that data. If you store it in backend this would not be possible.
And finally the awaited function to import from JSON is here.
// This code will take the myTopTenMovies.json and update the collection
// with the data in the file.
wixData.staticUpdate("DataCollection", "public/myTopTenMovies.json");
// This code will take the myTopTenMovies.json and insert all records
// in the data collection, creating new records.
wixData.staticInsert("DataCollection", "public/myTopTenMovies.json");
// This code will remove all items matching in the file and the
// data collection so mass removal is possible.
wixData.staticRemove("DataCollection", "public/myTopTenMovies.json");
With the above functions you will be able to update, insert or remove data from static files directly into Wix Data Collection.
Happy coding!