Create MySql schema in the model in express application - node.js

I am using express-MVC-generator for creating app skeleton in node js and once I have my project structured, I need to change default database in mongo for a MySQL database but I canĀ“t find how to create a MySQL schema from database in the model file. Is there a way to do that?

After research over the web, I found that the best option to solve my problem is to use sequelize, sequelize-cli to generate the models from an existing database.
https://www.npmjs.com/package/sequelize-cli

Related

Manage Connection in Mongoose across Modules

I am creating a Node Module which will perform some databasebase queries using mongoose. What I want is to access the mongoose connection variable across the module which is created in the main application. I have read that Mongoose is a singleton object to I guess it should work but it isn't working at all. ANy help will be appreciated

How to use mongoose in two packages?

I'm using Lerna to handle multiple packages in my repo.
Basically, I have a package #pkg/server where my Node/Express App is and an other package #pkg/model where my mongoose Schemas reside.
My REST API is using some of the models, with an import ModelA from '#pkg/model'.
The problem is that both of these packages have mongoose as a dependency (the #pkg/server uses mongoose to establish a connection to the mongoDB server, and #pkg/model uses mongoose to define schemas and models).
In my server, whenever I try to call ModelA.find(...), it just hangs forever. From the mongoose docs, it looks like I have to register a model to a mongoose connection and not to the mongoose object itself.
My first guess is to pass the connection object created by the server to the #pkg/model and then register the models to this connection object.
My question is, is this the right way to do it? Or is there a way to do something like (in the #pkg/server) mongoose.loadSchemas(#pkg/model) ?
What I have ended up doing, is that I'm passing the connection to the package requiring mongoose thus sharing the connection between packages.
Some package can register models, an other one can make queries, etc...

MongoLab and Heroku without mongoose

I am currently working with a team in a NodeJS project that uses express and a local MongoDB database.
We did not implement mongoose into the project, and now we are at a stage where we wish to upload the project using heroku, so that we can have a central point as we further develop different parts of the project (apps and web pages that consume the data from the database.)
We managed to upload the project to heroku, and some of the pages are working, but we can't access the database, it returns "500 - internal server error" everytime we try to access data from the MongoDB.
After some research we could only find that it's best to use MongoLAB to store the data in a cloud based datacenter, but we can't seem to find ways to connect our project with MongoLab without the use of mongoose, which we didn't install to the project.
My question is: is there a way to connect our NodeJS project to MongoLab using only the original MongoDB npm module, without having to install and use mongoose? We are at a stage that refactoring our DB-access classes, and implement mongoose, would cause our project's deadline to be delayed.
Thank you for your time.
You can definitely use mongoDB with mLab(previously mongoLab), without using mongoose.
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://ID:PASSWORD!#SUBDOMAIN.mlab.com:PORT/DATABASE_NAME', function(err, db) {
console.log("Connected correctly to server.");
db.close();
});

Using sails.js with an existing postgres database

I was looking at using Sails for an app that we are developing.
I'm using the sails-postgresql adapter which uses the waterline orm.
I have an existing database that I want to connect to.
If I create a model using generate something
and then in my model I have
attributes:{
title:{type:'String'}
}
If I browse to localhost/something the orm deletes all the columns in the something table except title.
Is there a way to stop it from doing this? This app should not delete columns on this database.
Thanks!
I am the author of Sails-Postgresql. Sails has an ORM called Waterline that it uses for managing data. The default setting assumes that you would want to auto-migrate your database to match your model attributes. Because Postgresql is a SQL database the Sails-Postgresql adapter has a setting called syncable that defaults to true. This would be false in a NoSQL database like redis.
This is easy to turn off if you want to manage your database columns yourself. You can add migrate: safe to your model and it won't try and update your database schema when you start Sails.
module.exports = {
adapter: 'postgresql',
migrate: 'safe',
attributes: {
title: { type: 'string' }
}
};
Sails doesn't have anything like migrations in Rails. It uses auto-migrations to attempt to remove this from your development process and then leaves updating your production schema to you.

How to properly structure database calls in an express/mongoose application?

What is the correct way to access a mongodb database from Express ?
Right now, I am including my database handler db.js which contains mongoose.connect( 'mongodb://localhost/db' ); every time I need to do a database call.
Should I use the same connection and passing my db object through callbacks or I can just include my db file every time ?
In other words, is mongoose.connect always re-using the same connection ?
Edit: my source code is public here, I am fairly new to nodejs/express applications and I am not sure if my application is structured properly...
You only need to connect to your database once. In your other files, you need to include your models and use them to read / write to your database collections.
Edit: Looking at your code -- why don't you move your connect into your initialization script, and then include db.js to access your models?

Resources