0

Context

A feature that I would like to implement is to add a local(client side) database as offline feature for my cordova app using basic html, css, and js. It would be a fallback in case the internet connection is cut off. What I thought was that it would store the request url and request data inside a local JSON file and would have an option later on to be able to resend those failed request if the internet connection is restored.

Question

Is there a method to manipulate (add/create, edit/update, and delete) a JSON file stored inside a client app using pure JS?

What I've done so far is import and read a JSON file inside my app folder using the following code:

fetch("./database.json")
  .then((response) => {
    return response.json();
  })
  .then((data) => console.log(data));

(if there's a better way to do it, feel free to give a feedback in the comments)

That works fine and all, but I can't find any method on how to add/edit/delete an object and save the changes to that same JSON file.

According to this post which is half a decade ago, it is not possible without doing it in the backend.

2
  • 1
    You would presumably use the File API which provides read/write access to files on the local device. Commented Mar 3, 2022 at 1:01
  • @meagar thanks for that! There also seems to be another plugin more useful in this offline scenario using SQLite. Commented Mar 4, 2022 at 0:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.