Electron data storage - javascript

I'm developing disktop app using electron I want to store my data loclly within the app (no internet connection) I installed mysql using node js it worked but only if my laotop is connected to the internent

You are writing html5 to create the application. Now you need to think what html5 provides to store data and it would be localStorage, WebSql and IndexedDB.
You can store data in mysql too but you need to install the mysql at users computer and it would be hard to create a package including mysql which will install mysql, return the right port so that you can use inside your electron application.
Now from three options (localStorage, WebSql and IndexedDB) - WebSql is deprecated , so you should not use it. localStorage is only good for storing data in terms of key and value with fix length. So the last option would be indexeddb.
So you should store data in indexeddb, although there are others options like - sqllite etc which you can use it using some third party driver.
If you find indexeddb difficult to use, consider using any of indexeddb library. Check the example for storing data in electron using indexeddb library jsstore - https://github.com/ujjwalguptaofficial/JsStore/tree/master/examples/electron

Related

How does one go use MongoDB with native JS?

I have been searching but sadly haven't found any resources for using MongoDB with native JS. Every resource I find seems to be NodeJS.
I want to use MongoDB on vanilla JS, so to speak.
See How to connect MongoDB with VanillaJS?
and also link from top comment Connecting MongoDB to the front-end?
You see everyone use node.js because by design, mongodb is meant to communicate with the server only and the server decides how to handle the data. By using nativejs directly (assuming it was possible without using cloud APIs) is a huge security risk. NativeJS advertises itself as "running inside your browser", I wouldn't want my backend code to be exposed to the user.
TL;DR you should use cloud integrations of mongoDB (mongoDB cloud etc.) or use node.js like everyone else.
TL; DR: You can't
MongoDB is not supposed to expose data directly to front-end (client side). NodeJS (in this case; MongoDB can be used with a variety of other server-side engines) is a server-side JavaScript engine that acts as a bridge between the client and the database.
However, you can have a look at MongoDB APIs
The better way, however, would be to have a server between your database and the web interface, that queries or inserts data from your MongoDB database. If your database has to be client-sided though, have a look at firebase.
If you only want to store data on the client, you could simply use LocalStorage API
If not, you either need to render all the data needed in your page content, or query it via js Fetch API
Even the native mongosh is a NodeJS environment, see MongoDB Shell:
The MongoDB Shell, mongosh, is a fully functional JavaScript and Node.js 16.x REPL environment for interacting with MongoDB deployments.
The legacy mongo shell is also a JavaScript shell, seems to be based on SpiderMonkey, see https://www.mongodb.com/community/forums/t/why-change-v8-to-spidermonkey-in-mongodb-js-engine/99871

Store and edit data using ReactJS

I am building an application using ReactJS. I am trying to find out how to store data and to edit it. I tried to store it on my computer with 'fs, 'browserify-fs' but it didn't work.
Should I use express, or is there any other alternatives ?
If you are using React you are operating in the browser. Your option for storage is in local storage. This is explained here.
Examples of code are:
// setter
localStorage.setItem('myData', data);
// getter
localStorage.getItem('myData');
// remove
localStorage.removeItem('myData');
// remove all
localStorage.clear();
Note this is stored in the browser and can be easily cleared. You are going to realize that you need a back end solution. This is a server you can send requests to which has an API (a place you send requests to) which executes some form of operation (normally CRUD - Create Read Update Delete via a REST endpoint or GRAPHQL) to serve you back the data you are requesting from a database (MySQL, Postgres, MongoDB). This is a whole different discussion.
To store an array in local storage you will need to make it a string via JSON.stringify. An example would be:
localStorage.setItem("array", JSON.stringify(array));
In developer tools in Chrome you can go to Application -> Storage -> Local Storage and see what is saved. Here is an example:
If you want to share the data along multiple clients you should use server-side solution or if you just want to save the data for a client only you could use client-side solution provided by #diesel.
Create your own web-server
You need to create web server and a database to store your data. Database is used to store data. You could use: MySQL, PostgreSQL, SQLite3, MongoDB, ... You also need to create web service to make secure database calls.
To create web server you could use Express.js to write your web server easily.
Headless Content Management Systems (abbr: CMS)
If you don't want to spent time on creating your own web-server you could install a headless CMS to read/write your data using api endpoints provided by CMSs. Here's list of headless CMS softwares: headlesscms.org. I tried strapi which has lots of features you might need.
Here's some strapi features:
Open-source
Model builder
Extensible (plugin support)
Content editor (eg: to edit articles)
and many more
Firebase
If you don't want to spend your time on installing CMS software to your server and maintaining it regularly you could use Database service provided by Google Firebase. It is also feature rich too. Here's some features supported by Firebase.
NoSQL Database (to store your data)
Authentication (to authenticate users)
Storage (to store files)
Functions (to write serverless functions)
Machine Learning
and many more

SQLite database with javascript

I have a requirement where I have a postgresql database in a web site.
I want to run my web site in offline mode but the problem is that I have many ajax calls in my website which will not work in offline mode.
So I am considering using sqlLite but I don't know how to configure it, how to write JavaScript code, or even know if the users need to install sqlite in their browser or PC. Can anyone help to overcome this requirement?
I have used some local storage like Indexed DB it will work but that is called sqlLite or not I don't know.
please help
You do not need to work with Sqlite for addressing this, only take a look at following link for how to make web pages available for offline viewing.
If you namely want to use some database it is possible to use SQLite.
Look at https://github.com/kripken/sql.js/
Be care of using SQLite requests in main UI thread. Do not forget to implement workers for SQLite.
I'm pretty sure that you do not need SQLite.
Try using HTML5 LocalStorage API.
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
The Storage interface of the Web Storage API provides access to the session storage or local storage for a particular domain, allowing you to for example add, modify or delete stored data items.
If you want to manipulate the session storage for a domain, you call Window.sessionStorage method; If you want to manipulate the local storage for a domain, you call Window.localStorage.
https://developer.mozilla.org/en-US/docs/Web/API/Storage

Is it possible to perform database operation on locally stored DB through javascript (WebSQL or Indexeddb)

i am developing offline web app for android platform and i want to use offline database for it. Is there any way to connect to locally stored database through javascript. like
var db = opendatabase("file:///android_asset/myLocalDb.db");
var Records = db.executeSQL("Select * form myTable");
echo Records;
I use PouchDB. It is a NoSQL type database backend, but has great cross platform support so you don't have to worry about the underlying storage mechanism across browsers. If you have flexibility on the database type, it may be the easiest solution.
Are you making a Cordova app, or in-browser hybrid offline but specifically targeting Android? You can use SQL Lite with Cordova and bubble up from your Javascript to access the db. Check out: https://stackoverflow.com/a/26609184/1011603
Yes, it is possible to package your application and data file as web app by packaging appcache. Since browser manage data storage for database (indexeddb or websql), it is not possible to specify them by your file name.

Accessing Websql database with Qt

I'm making a web app and I need to store some data into a database that can also be accessed with Qt. What I need is to insert values from the web app and the app created with Qt.
I'm storing the info that comes from the web app in a WebSql database with javascript in Linux Mint.
I wan't to know if it is possible to access that database file with Qt.
Are there a better alternative to do this without a server-side languages?
Yes you can access the websql database with Qt. The websql database is just a sqlite database so there is no problem accessing it with Qt.
The tricks are finding the location and name of the file. In Chrome on my Mac, for example, the database is at
~/Library/Application Support/Google/Chrome/Default/Databases/mysite/1
there is a master database of the other databases at
~/Library/Application Support/Google/Chrome/Default/Databases/Databases.db
It is also sqlite and you can read it to find the other databases.

Categories