Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to persist data to a file #1

Closed
tducasse opened this issue Oct 15, 2021 · 6 comments
Closed

Add a way to persist data to a file #1

tducasse opened this issue Oct 15, 2021 · 6 comments

Comments

@tducasse
Copy link
Owner

Currently, the data is reset every time we relaunch the application.
Add a way to save the data to a json file, and restore it from the file later on.

@sourcefactory-at
Copy link

I could work on this but am not sure how you expect it.

There could be a new export enablePersistence with an optional path parameter. In browser (if window.localStorage exists) the path parameter is not used and it's saved there, otherwise it's mandatory.

import { db, register, enablePersistence } from "@tducasse/js-db";

enablePersistence(process.cwd() + '/database.json')

register("users");

db.users.insert({ name: "John" });

const user = db.users.findOne({ name: "John" });
console.log(user);

@tducasse
Copy link
Owner Author

Hey, this sounds good! Happy to go with this if you want to have a try!

The way I initially thought about this was two functions, one to save to a file, one to restore from a file, and maybe call the save to file function either manually or automatically (on each update) if persistence is enabled, and the restore function when the app starts.

@sourcefactory-at
Copy link

That sound also good. This could be enableAutoPersistence instead of enablePersistence and a new db.persist() method in case enableAutoPersistence was not called.

This would work fine in browsers but the problem I see with that is that somehow the path should be specified if db.persist() gets called. It could accept the path as parameter which must be provided on the first call and otherwise an exception is raised but I think this is somehow confusing...

@tducasse
Copy link
Owner Author

tducasse commented Oct 15, 2021

Or you could specify through a config file or something, and it's picked up automatically at the start.
So the way I see it, you could have a check on every function that does:

If enableAutoPersistence
  db.persist()

And then in db.persist

If browser then write to local storage
Else if file is defined then write to file
Else do nothing

Now I'm guessing we could have a check at the start of the app, maybe it even tries to create the file, and if it doesn't work or the filepath is not configured, then it just prints a warning and disables the persistence.

What do you think?

Sorry for the formatting I'm on a phone right now!

@sourcefactory-at
Copy link

That sound imho great.
As I'm not quite sure about the config file (what's in there and how to pass the path to the library) I'd recommend to handle this in another ticket to keep it separate. This could be more generic and include things unrelated to the persistence.

If it's ok for you I could start with the implementation.

@tducasse
Copy link
Owner Author

Yes, feel free to handle it however you want to be honest, and if you push a PR, we can review it together, and maybe I'll work on the config file later on 🙂

Happy to help if you have issues along the way, and thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants