Level DB data store gets recreated everytime - node.js

I am new to web app development, and still learning the basics. I am working on a basic application with nodejs and level db. Every time I restart my web application, the leveldb data store gets recreated, and the data that was stored in it is all gone.
My example code is below:
var level = require ('level');
var db = level('./mydb.db', {createIfMissing : true});
My understanding is that the db will get created only if it is missing.. otherwise the existing db would be used. In my case that is not happening.. what am I doing wrong?

Try the absolute path to the db while opening, it is possible the relative path is changing each time server runs.

Related

Retrieving data from my real time database on firebase

Hello I am trying to retrieve my data in my database. My data is in this path
https://(MYLINK).firebaseio.com/users/personalID
Inside of this directory I have a lot of data in generated keys. I Want to be able to access them and query through them. But I am having trouble just retrieving a JSON of data.
admin.initializeApp(firebaseConfig)
var db = admin.database();
var users = db.ref('users/B72O-UB2J-51KJ-JUIX')
users.orderByKey().on('value', snapshot =>{
console.log(snapshot.val())
})
Nothing happens when I run this code. Any suggestions ??
I tried to show how my data is structured and stored
My issue was I wasn't importing the correct config to run the administrator app. You can go to your app settings and then they have a config section with all the info.

Exporting sqlite3 db file from inside electron app. Is this possible?

I've got an app I've put together in Electron which saves data using sqlite3. Everything works as expected. I'd like to be able to export/save the actual database file so I can share it with others, treating it sort of like a save file.
I assume that if this is possible then I need to be using fs as well, which is fine.
Even better, can I just create the database file outside the compiled app from the start? And if so what's the best way to accomplish that?
Otherwise I can switch over to kripken/sql.js or something like that, but I'd rather not put the time in to make those changes if there's an easy way to just have the existing sqlite database file get saved to the user's computer outside the app.
I'm an idiot.
Instead of storing the file internally in the packaged app like this…
const dbPath = path.resolve(__dirname, 'data.db')
…I'm just storing it in the filesystem like this…
const {app} = require('electron').remote;
const dbPath = path.resolve(app.getPath('userData'), 'data.db');
…so that it's accessible from the start.
I'm leaving this question up because I'd be interested if there is a way to have a save file dialogue for an extant file in the packaged app, but in the mean time this is my answer.

User specific database in MongoDB

I am currently working on an inventory management software in Node js and MongoDB. I am pretty new to MongoDB, having worked in Oracle and MySQL for most of my projects.
Is it possible to create a separate database schema for every client who uses my software, with each client having access only to his copy of the database schema and collections?
The equivalent of selecting data in Oracle database would be
Select * from User1.table,
Select * from User2.table etc
Also, if it were possible, how would it be implemented using a node js mongo db client like mongoose?
I looked at MongoDB documentation, but it talks mainly about adding users to a database for authorization.
I apologize if it seems like a silly question, but id appreciate it if someone could point me in the right direction for this.
Before starting to invest a lot of time in the development of your project, check out other possible approaches to the scenario that you are trying to build.
I did a quick search on SO and found some additional threads with similar scenarios:
MongoDB Database vs. Collection
MongoDB Web App - Database per User
Additional info about mongoose database creation
Whenever you call the connect method on the mongoose object, you are either connecting to an existing database or you are creating it in case it doesn't already exist.
You could have a function that allows you to pass in a name argument with the name and create databases programmatically:
function createDatabase(name) {
var conn_string = 'mongodb://localhost/';
if (typeof name == 'string') {
conn_string += name;
}else{
return false;
}
mongoose.connect(conn_string);
}
Also, be aware that a database will be created when you first insert a record in a collection of that particular database.
It is not sufficient to only connect to the database, you also have to insert a record.
As per my previous example, you could also pass a schema parameter to the function, tailored to each user's profile and fire an insert statement after you connect to that database.

Sail.js requires server restart after running command to refresh database

From this question, Sails js using models outside web server I learned how to run a command from the terminal to update records. However, when I do this the changes don't show up until I restart the server. I'm using the sails-disk adapter and v0.9
According to the source code, the application using sails-disk adapter loads the data from file only once, when the corresponding Waterline collection is being created. After that all the updates and destroys happen in the memory, then the data is being dumped to the file, but not being re-read.
That said, what's happening in your case is that once your server is running, it doesn't matter if you are changing the DB file (.tmp/disk.db) using your CLI instance, 'cause lifted Sails server won't know about the changes until it's restarted.
Long story short, the solution is simple: use another adapter. I would suggest you to check out sails-mongo or sails-redis (though the latter is still in development), for both Mongo and Redis have data auto expiry functionality (http://docs.mongodb.org/manual/tutorial/expire-data/, http://redis.io/commands/expire). Besides, sails-disk is not production-suitable anyways, so sooner or later you would need something else.
One way to accomplish deleting "expired records" over time is by rolling your own "cron-like job" in /config/bootstrap.js. In psuedo code it would look something like this:
module.exports.bootstrap = function (cb) {
setInterval(function() { < Insert Model Delete Code here> }, 300000);
cb();
};
The downside to this approach is if it throws it will stop the server. You might also take a look at kue.

how to change metro app WINJS Sqlite database path

i am developing windows8 application using HTML5 and Javascript. I am using SQLite3-WinRT database plugin, provided by GIT HUB https://github.com/doo/SQLite3-WinRT.
I have successfully integrated the plugin within my app and its working like charm, now the problem is I want to change the database path from
var dbPath = Windows.Storage.ApplicationData.current.localFolder.path + '\\database.sqlite';
to
var dbPath = \pages\js\ + '\\database.sqlite';
Well that path would be Windows.ApplicationModel.Package.current.installedLocation.path + "\\pages\\js\\database.sqlite"
You should however be aware that you cannot write into this location, just use it as a read-only database.
Have a look at the ATTACH command if you want to read data from a provisioned database in addition to having a read-write database.

Resources