Creating database model with Mongoose - javascript

I have a node.js app that is essentially a sketchpad and currently I'm working on a feature to enable to save all of the sketches they've drawn during a "session" to a database so they can pick back up at a later time where they left off. I'm using a MongoDB database that I'm connecting to via the Mongoose ORM.
The server is started up in the file main.js which is currently where I'm opening the connection to the DB; however, the code for the saving of sketch data (which is currently just being saved to a JSON file on the server) is in a separate file. Based on this tutorial it seems that the code for the creation of models for a document are to go inside of a callback function that is run once the connection is open. But given that the logic for saving sketches in the app is in a different file from where the connection is being opened and since it says here that model instances aren't created/removed until the connection is open, it seems that there would either have to be a way to open different connections opened to create the models or that there would need to be a way to initiate the creation of the model for the sketches from the connection callback code in main.js.
I'm very new to MongoDB and Mongoose so I'm not sure if this is the correct way to think about creating models but given the needs of the feature, what would be the correct approach to opening the connection to the database and saving the sketches to the database once the save sketch function is called?

You may be overthinking this.
Just open your mongoose connection (a shared connection pool) via a mongoose.connect call during app start up and then create and save your Mongoose models whenever. Your models will use the shared connection pool as needed and will wait until the connection is established if necessary.

Related

How to load new flows in runtime node red

I want every time node red page reload, depending on the user (session user I created myself) to show that user's stream. Each user's stream will be fetched from another server via http protocol. Where can I put the code to get the new flows file in the source code Node Red
Node-RED is currently NOT multi-tenant multi-user.
Any given instance of Node-RED can only run one flow at a time, even you you define multiple users they will all work on the SAME flow.
If you want to support multiple users with their own flows, they need run separate instances and each instance will need a distinct hostname (currently authentication tokens are kept in browser local storage which is host scoped so there is no way to run multiple instances on different paths on the same host)
If you want to push a new flow to a given instance then the Admin API has HTTP REST endpoints that support this, but any new flow you push will replace the existing one.

Passport.js - use same token for different server

I'm using passport.js and MongoDB for user login and postAPI authentications. However whenever I deploy my node server to another AWS instance, I need to go through the signup process, do the login and get a new token.
I know I can see the saved users and their jwt tokens from MongoDB. Are there anyway that I can copy the token and, when initialize new database, save the same username-jwttoken pair by default, so I can use the same token string (not with password, though it is more easily to be done) to pass the passport authentication test?
Thanks!
It sounds like your deployment process involves tearing everything (application and MongoDB) down & rebuilding from zero, possibly with some seed data but without any of the "live" data in the AWS instance. Here are a couple of ideas:
copy all the data from the old MongoDB instance to the new one as part of your deployment process. This will ensure that the users are present on the new instance and (should) ensure that users don't have to go through the signup process again. I'm not too familiar with MongoDB so I don't know how to do this, but I'm sure there's a way - maybe you can copy the data files from one to the other?
set up your environment with two servers: a MongoDB server and an application server. This way you can tear down your application and create a new AWS instance just for the application without touching your MongoDB server. Just update the MongoDB connection configuration in your new application instance to point to the same MongoDB server you've been using.
The first option is more suitable if you have a very small application without too much data. If your database gets too large, you're going to experience long periods of downtime during deployment as you take the application down, copy the data out of the old Mongo instance, copy the data into the new Mongo instance, and bring the application back up.
The second option is probably the better one, although it does require some knowledge of networking and securing MongoDB so that only your application has access to your data.

method that runs only after first install

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.

Create websocket without connecting

Is there a way to create a websocket without connecting to it right away?
So far I think
var ws = new WebSocket("ws://ws.my.url.com");
creates and then connects right away.
I'd like to create the socket, define the callbacks, and then connect once the user clicks a connect button.
Thanks
No. Creating a webSocket means you've created a connection to a server (or started the process of connecting). There is no such thing as creating a webSocket that isn't connecting yet.
You could create an object that would store all the configuration parameters and then just tell that object to connect when you wanted to, though I'm not really sure why that's better than just creating the actual webSocket when you the connection to be made.
Or just create a function that does all the setup work and call that function later when the user clicks a connect button.

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

Categories

Resources