A Persistent Filesystem Solution for Repl.it
Keep your files persistent
A persistent filesystem on repl.it is something I have wanted to see for a very, very long time. But unfortunately it isn't going to be added, which means that any programatic changes to files you make when you are offline will not be saved. This makes keeping logs or storing data within files difficult on repl.it
The team has introduced repl.it DB, a simple key-value storage built into your repls, to solve this issue. However, repl.it db isn't a perfect solution as it can't replace something like SQL or SQLite databases, and also your code will only ever work on repl.it.
That's why I've made this simple workaround, a script that saves your files into repl.it db. How the repl.it file system works is that any programatic changes will remain on the repl while it is running, but when it restarts from time to time and you aren't on it the changes will be lost. This script detects when files are changed, saves them into repl.it db, and when your repl restarts the script will fetch the changes and then replace your files. Additionally, the state management system using a checksum lets you edit your files as normal when your repl is not running and your manual changes will not be replaced by the old version stored in repl.it db.
I tried to make this script as efficient as possible, completely asynchronous to be non-blocking, and also added string compressing to save space within repl.it db as you only get 50 MB to work with. Every time the repl is run I also delete any files you don't want to keep persistant anymore from the database so you don't have to worry about any data being stuck in there and counting towards the data limit.
Using the script is very simple. All you need to do is include it into your program and run it at the very top. The function takes an array of all the files you want to keep persistent. You can also see the repl attached for an example.
One thing to note is that if you read any files at the beginning that haven't been replaced yet, the old version of the file without changes will still be there so you need to wait until the function is done. (If repl.it ever updates their node version we can use top level await and get rid of the ugly function thing)
Will data save if repl gonna force restart?
@MrEconomical Well, I tested your script on a Discord bot with sqlite3 database which has guild settings (language, prefix) etc. Always-on option of course.
Before using this script, I had several entries in my database. When the bot with the script was on, I added entry (custom prefix) and left it overnight. It so happened that Repl.it automatically restarted the bot during the night and unfortunately did not save the entry I added yesterday.
How can I solve it?
Ah, can't give too much details to the public, lol I can dm u if you have discord, sry about that it's 2 reasons, 1 ppl trying to make it on there own bc it prob wont work well and theyll prob break ppls repls, 2 i don't want my idea stolen and have someone else implement it @AmazingMech2418