Hi there!
I need to develop an application that can potentially link different databases, but only one at a time. The application will have different clients, who use different database engines. Therefore, it would have a configuration file that establishes the connection parameters when starting the application.
IONIC offers some solution in this regard? You could guide my investigation a little?
Thanks for read.
Ionic apps can’t really read a file that you can change in the background. You can either build the app with config a, or config b. If you want to change some data, it has to come from a remote API or database.
Forgive me for contradicting you, but I have an app running that currently reads data from a JSON file hosted locally and based on them establishes a connection to a server and connects to another Bluetooth device. The data is the IP address and port of the server and the MAC address of the BLE device.
Perhaps the only way is to develop a RestFUL API that takes care of the connectivity with the different databases and offers the abstraction sought?
And what is the “contradicting” part in your message?
In principle, I think you could do this. But the cost would be to create a complex forRoot() function in some module. Like maybe you load Storage into app.module.ts first, and then read from Storage to get a config value that you pass as an InjectionToken to your database module. The problem with this approach is that your user would be stuck looking at the loading screen for possibly a long time. It’s better to have the forRoot functions as simple as possible (i.e., hardcoded config), so the app starts as quickly as possible.
From a UI perspective, I think it’s better to reach a landing screen right away, and then from the landing screen to get the user’s choice of database and database configuration. On the first run, the user might have to specify that. You could store those choices and then use them in future runs. But I wouldn’t suggest doing any async calls until afterViewInit of the home page.
Ionic apps can’t really read a file that you can change in the background.
I’m changing a file in the background, and the next time the app starts it reads that configuration. Maybe I have not understood your previous message well?
I appreciate the advice very much.
When you say:
Are you referring to a module developed in the app, or outside of it as a service?
I don’t know what this means. But I’m referring to an NgModule you import into app.module.ts.
Great.
So the approach you suggest is to develop an NgModule for each database that I need to use and then import them?
No. That’s exactly what I’m telling you not to do. Please reread the post by @Sujan12.
Here’s what I think you should do. Create a database service, with a simple API like read, create, upsert. Then, in that service, inject a separate service for each possible database. Then, in app.component.ts, set which database you’re using. The database service then uses a switch statement to call the appropriate operation under the hood. So your entire app talks only to the database service, making code simple. Only the database service needs to worry about the implementation details.
Thank you very much!
This was the answer I was looking for. Now I’m going to work with the “how”, but I already have a north.
I might have made an assumption here, but are you not running the app on a mobile device?
There it usually is not possible to change a file of an app just like that.
Hi Sujan12, sorry for my late reply.
This is the function that I mentioned before. It returns a file entry, or creates it if it does not exist from the name provided:
function getFileEntry(fileName){
var fileEntryDeferred = $q.defer();
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (fs) {
fs.getFile(fileName, { create: true, exclusive: false }, function (fileEntry) {
fileEntryDeferred.resolve(fileEntry);
}, onErrorCreateFile);
}, onErrorLoadFs);
return fileEntryDeferred.promise;
};
I use it in the logic of my application and also to read a configuration file (JSON) located on the android filesystem and act accordingly.