Sync PouchDB with server - javascript

I'm currently working for a startup which ask me to develop an offline app using pouchDB. This pouchdb is used to store the data entered by users.
The offline application works fine. Now I have to add one feature on the online app to sync the dbs. After a login, the online app have to check if data is stored in a pouchdb on the device which is connecting, and, if the check found data, the online app have to pull this data.
I have the folowing problem: the online app can't get the db stored localy on the device (even if I run the both app in the same browser).
I explain my problem in another StackOverflow post, but the formulation was not so good so I think that's a good thing to post another question.
My old post here
I work on this problem during few days and I don't have much time until I have to finish my work, if someone know how to solve this, it could be very nice :)

I think the lack of response is because readers are not very clear what the problem is. In your other post it sounds like you are saying if you write a new entry to the local database you cannot retrieve it again. In this question it sounds like once you have a local database entry you cannot make it replicate to the server database - is that the case?
On the PouchDb front page is a short example of writing to a local database and then replicating it to a server. Like this:
var db = new PouchDB('dbname');
db.put({
_id: 'dave#gmail.com',
name: 'David',
age: 69
});
db.replicate.to('http://example.com/mydb');
(the example assumes the database can be updated by anyone ie no security - otherwise you need a username and password as explained here)
Does this work for you? If not can you say what happens?
Checking to see if there is data locally should be a case of seeing if your local database has any entries in it (db.info would be a start as it returns a document count). Then you could replicate the local database using the db.replicate call.
Does this help?

Related

Autodesk Forge - Extension button to get data from external SQL database

I am trying to connect my Forge app to a sql database, but I have no idea how to do that. I am new to forge and web development, so I don't know how to look for the best approach to solve this.
I found a code that does exactly what I want, that is getting the data from a specific table of my database, but I'm not able to put this code inside my forge app, mainly because it uses a require() function and I can't put it inside my extension button.
By the way, I'm trying to make a button in my extension where I click, it access my database and get data from it.
Would be really nice if someone could help me with this silly doubt that probably already has a solution in the internet and I'm not able to find it.
Sorry in advance if it's too obvious.
This problem is more related to web development than Forge.
Your app should be made of 2 parts : the frontend (html/css/js) and a backend (probably nodejs as you tagged it).
Your frontend should not access the database directly. This is the job of your backend.
In your backend, add a library to access your database such as Sequelize (if you are working with NodeJS). Then you will do a request from your frontend to your backend to get the data from the database.
You probably have something working like that for your Forge Access Token, depending on what you use to start your development.

Wondering if I can use mySQLs connection.threadID to continue running queries?

I am new to web development, but I am building a tool to help us visualize the customers in our database in a more concrete way (basically a CRM of sorts). Its a web app using a node.js backend and Vue.js for the front end.
THE BIG QUESTION - I found a connection.threadId query that can be run. If I save this threadID in a cooke or JSON web token, can I use this to have each user uniquely querying the database? If you need additional information on my issue, continue reading.
Essentially the problem I am running into is authentication. We were able to get a dummy user create that has read only privileges to the database, but this doesnt help the account management team. I am trying to find a way for them to be able to login with their own credentials and then be able to run queries from my node.js (javascript) backend using a series of get and post requests.
I am able to get the user to login with the SQL password and they can run the initial query (that fires upon login) but they then cannot re-query the database if someone else logs in after them (I realized that my variables were all globally scoped on the server.) I then moved everything client side in JSON web tokens, but I had to hash the database password to be safe which leaves me with another problem. My hash and the original database hash do not match, and that is not something I'll ever get access to. Not really sure where else to go with this at this point.
Please ask if you need any additional information to assist.
Thanks!
You probably can't.
You also probably shouldn't use variables on the SQL server for a web app like this. If you can refactor those into variables you can schlep around in the user session (or POST parameters or whatever), you're much better off, being more stateless (as far as the server components are concerned) and all too.

Electron. Autoupdating app when it notices the change in the database or selected server?

My client wants applications written in electron which works both offline and online. The application is to connect to the server / database and download data (photos and descriptions of products) so that it can work in the offline version. When it is online, the application checks whether there is any change on the server / database and then an update occurs.
I have already prepared such applications (search engine and product filters with the possibility of generating pdf), but I have no idea how the application would check whether the server existed any of the data and download new photos of products, etc.
Yeah well the question is: Do you want to run you're queries on the Client it self? Because then, if not strictly restricted, someone finds a way to open up the console and sends funny queries.
But anyway... your question:
The easiest way I can think of this, because we don't want to download everything again, is to have a version number for each element.
So the client has one and if that does not match the servers it gets updated. Now you just have to get all IDs. But keep in mid, you still have to handle what happens if an item gets removed or a new one added.
This is not really an answer but I hope it inspired you a little bit.
Oh some after thoughts:
You could get something like a WebSocket between those two. Yes you had to program one more service but
The query would be save
You could keep track of removed and added items
You could work out some timestamp system that you get all items that are newer than the timestamp of the client.. this will be some work though.
nice day, Elias

Best way to get and store data (optimized) to mobile app (Ionic, Cordova/Phonegap)

I want to do an app with ionic framework and cordova for Android and iOs and I want to optimize at most the requests to the server.
I need to get data from php server (API) and store in the mobile phone, this data is related, so I think that a SQLite is the best option to store it in the device because of to show it is more fast to do a complex SQL than get a JSON stored in localstorage (for example) and search and link the data in the mobile. I'm right?
I need to do this and can do a Pull to refresh in the app and get only new data.
I've been watching some solutions but I don't know what is the best.
Parse/Firebase: This is difficult to administrate for non-specialists, for this I have a backend to enter and modify data with the relations easily. In addition this services return a JSON with more data that I need.
Persistence.js and Persistence.sync.js this works with mysql-server and sqlite-local but can I sync only one table or get only the new data? And I did't find a solution for php server.
CouchDB and PouchDB: I don't know if this can be sync only for a new records.
Receive data in JSON and store it in SQLite: I need to create functions to do this easier.
Any other solutions?
I'm very lost in this topic.
Thanks!!
CouchDB and PouchDB: I don't know if this can be sync only for a new records.
That’s the default. Plus they handle all the nasty sync details for you. Would highly recommend looking into this.

Tornado, Django, and push notifications

I've searched Stack overflow and all I can find is how to use Tornado as a HTTP server.
Now, my question is how do I start doing push notifications using the system?
Let me give you some context...
The database
I have a database on some server far away that I know nothing about, other than its a postgreSQL database and a piece of software on that server updates the database every so often ( maybe every couple of seconds, to couple of days).
Currently
I Have a django app that displays these database rows. it gets these database rows from a different app - an app called api - using an ajax call every 5 seconds. As we all know this method is wasteful.
What I'd like to do
Well I'll bullet point it:
I'd like my Django app to stay the same in structure
The Django app will contain in its view JS code for connecting to a separate server.
this separate server will check the database for changes every 60 seconds. If the database has changed, then notify the clients with a message, such as "new data available"
Hopefully thats not too vague.
Thanks,
Andy.
I found that the django-websocket-redis package suits my needs which are very much comparable to yours as it can easily be implemented on top of your existing project.
Mind that there are a few dependencies (UWGSI and Redis, primarily) and I've had to switch to a Linux development environment to get everything to work properly.

Categories