method that runs only after first install - javascript

I am creating an app that get data from the server then save it and work locally with sqlite.
I want to initialize my application with data loaded only after the first installation, saved to sqlite and then loaded from database.
For that i want to create a method that save the data locally, how to make sure this method runs only after the first time the user install the app.

I think that the easier way to do this is to check, at every application run, if your database contains a certain value in a config table: if isn't present, populate the db, otherwise do nothing and continue execution.
In this way you could avoid to write platform specific code (native) since you're doing everything from the js layer.
Obviously I'm supposing that you're already using sqlite in Cordova, and that you can execute sql query/insert from js.

Related

Using jsGrid as a local standalone application

I'm looking for ways to use jsGrid (http://js-grid.com) as local application.
I've found basic.html in demos folder from git-project and I noticed that changes made to the example datagrid are not committing (after refreshing a local copy of basic.html all changes revert to initial values). As far as I understand changes made to the table are in memory and main script doesn't save them to db.js where datasource (json-dictionary clients) is located. And as confirmation I found that implementation of updatingClient-function in db.js is empty (due to obvious in-memory realization).
Questions I want to ask:
1) How could I make this application to use external but local datasource? Should it be some kind of this pseudocode in db.js:
db.clients = csv2json('clients.csv') (assuming there is an additional jquery-plugin to do this task) ? Are there any more convenient approaches?
2) If the first question is reasonable how should I implement the updateClient function considering using by multiple users simultaneously? Is it possible?
3) I need some kind of time logging of actions performed on the datagrid. Should it be implemented by binding certain elements to log-functions on main page via click events? If so, how could I get time stamps from these functions?
If you install xampp, you will have apache, PHP and MySQL - all local.
Otherwise, you can install sqlite or Codernitydb (better), which are local databases that do not require Internet access.

How does Sails.js manages data without saving in database

I am very new to MVC and Sails.js. I just started learning it yesterday and tried to learn it by doing something. But I am having some confusions regarding Sails models.
After creating the application, I configured the database in config/connection.js. Then I created a blueprint api named user. Next thing I did is I started the server and loaded following url:
http://localhost:1337/user/create?user=Mr.X
I didn't configure anything in api/models/user.js. So it is not supposed to save any data in database. When I browse my database as expectedly I can't see any record. But when I load the following url:
http://localhost:1337/user/
I can see the record of Mr.X there. And even if I restart the server the record of Mr.X is still there. What I can't understand is, how is it happening? How is Sails saving this data without affecting the configured database? Is it normal thing in all MVC frameworks or just Sails do that?
I'm guessing that you set up a connection in config/connections.js, but didn't specify a default connection in config/models.js, so your app is still using the default localDiskDb connection. You can see this database by opening the .tmp/localDiskDb.db file in your project. It's a pretty handy development tool.
See the docs for config/models.js for more info on global model settings. You can also set a connection on a per-model by basis using the connection property in the model's class file (e.g. api/models/User.js).
All your user data gets stored in localDiskDb.db by default.
You can Use postman app for retrieval and insertion of records.
Its better for you to first go through these videos.
https://www.youtube.com/watch?v=60PaCpTP5L4&list=PLLxyAuVpwujMQjlsF9l_qojC31m83NOCG&index=2

CouchDB - trigger code when creating or updating document

I have a page which store data in CouchDB. The page accesses the database directly via javascript, so not much of the logic is hidden from the browser. When creating a new document there is some logic which extracts elements of the data into separate fields so that they can be searched on.
Is it possible to do this logic on the server when creating or updating the documents, or am I stuck doing it before hitting the database?
You have a couple of options.
First, see this question about CouchDB update functions. Update functions receive a request from the browser and can modify them in any way before finally storing them in CouchDB. For example, some people use them to automatically add a timestamp. Also see the wiki page on CouchDB document update handlers.
Another option is to receive CouchDB change notifications. In this case, a separate program (either your own browser, or even better, a standalone program that you run) can query CouchDB for _changes. CouchDB will notify this program after the document is saved. Next, the program can fetch the document and then store any new revisions that are necessary.
To me, it sounds like you should try the _update function first.

Pre-populate iPhone Safari SQLite DB

I'm working with a PhoneGap app that uses Safari local storage (SQlite DB) via Javascript:
https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/SafariJSDatabaseGuide/UsingtheJavascriptDatabase/UsingtheJavascriptDatabase.html
On first load, the app creates the database, tables, and populates the data via a series of INSERT statements.
If the user closes the app while this processing is happening, then my app database is left in an inconsistent state.
What I prefer to do is deploy the SQLite DB as part of my iTunes App packaging so nothing must be populated at app cold start. However, I'm not sure if that is possible -- all of the google hits for this topic that I can find are referring to the core-data provided SQLite which is not what we're using...
If it's not possible, could I wrap the entire thing in a transaction and keep re-trying it when the app is restarted?
Failing that, I guess I can create a simple table with one boolean column "is_app_db_loaded?" and set it to true after I've processed all my inserts. But that's really gross...
I think this solution may work for you. which involves pre-creating the table, and copying it over when the app starts up.

database entry through javascript

I'm need to know can i connect db through javascript? Actually, the last action where i end is done through javacsript. Now, i need to make some entries(extracted through javascript) in the database where last action stops. How can this be done?
The answer depends on where the Javascript being run.
If you're asking about Javascript running in a browser directly accessing a database on a remote server then the answer is no. The best you can do is create a web service which updates the Database and call that via an XMLHTTPRequest object from you JavaScript. You would need to be very careful about what you offered via such a web service. If you allowed any SQL statements anybody could connect to your service and run DELETE FROM customer; or insert new user records or do anything ghastly.
If you're running JavaScript through the Windows Scripting Host via CSCRIPT.EXE or similar you can create ODBC objects and access the database that way.
If you're running JavaScript on a JVM I think you can use JDBC.
If you want to give more details I can be more specific.

Categories

Resources