Sails/Bookshelf Running a script that uses the Sails/Bookshelf environment - node.js

I would like to create a script to be run in the background of my server as a cron task.
I would like the script to have access to the sails environment (ie, loading all the modules, especially bookshelf and knex, and the database connection).
so that I could create a file myscript.js that looks something like
var environment = require("sails_environment")
// code that uses bookshelf etc exactly as if it were written
// inside a controller action
I actually only need the bookshelf module and db connection for this script, so it could be that bookshelf has a way to do this, but I imagine it is something built in to Sails.

There are lots of ways to do what you want. Here are a few.
https://github.com/balderdashy/sails/issues/2092#issuecomment-56043637
https://www.npmjs.com/package/sails-hook-schedule
http://www.worldnucleus.com/2014/12/run-cron-job-in-sailsjs.html

Related

expressJS exporting other things in addition to app

In my web application (I'm using expressJS), there are many services (such as mongoDB connection, MQTT connection, etc.) that need to be executed once the whole application is executed (using npm start command). Therefore, I can make use of these services in my entire application. For example, I want to use my MQTT connection in different files.
My idea is to export the MQTT connection, MongoDB connection, etc. in addition to the app this way:
//app.js
module.exports = {
app: app,
mqttConnection: myMQTTConnection,
db: myMongoDB
};
However, we know that this approach doesn't work (I tested it and got an error saying: TypeError: app.set is not a function).
How can I export other things in addition to app from app.js file?
If my approach is not possible, what other approaches can I use? (considering the fact that many services (such as connecting to a server, etc.) are asynchronous)

Console using postgres+sequelize with Node.js?

I am fairly new to Node but I am loving the tool. My only problem is when I want to have direct access to the database. I have a good experience with ruby on rails+postgres. Using rails console was very helpful when I was developing rails.
Is there some kind of equivalent I can use to have direct access to my database? I have uploaded my app to heroku so I would like something that I can run on heroku as well.
(I prefer not to use SQL, I am wondering if there is a sequelize console?)
Here is the way to do it:
node --experimental-repl-await
> models = require('./models');
> User = models.User; //however you load the model in your actual app this may vary
> await User.findAll(); //use await to avoid promise errors
TLDR
This gives you access to all of the models you have created and you can use the sequelize ORM commands like findAll, create etc.. just as you would in Rails active record.
Sequelize uses promises, so to run these properly in REPL you will want to use the --experimental-repl-await flag

Use test database with grunt and mocha

I am building a web app in Node.js, Express, and MongoDB using Mongoose. I want to have a dedicated database for when i run my Mocha tests with Grunt so that I do not mess up the database I am using for development. How would I do this?
I currently have my development database configuration information in a file at /config/db.js, which is loaded and connecting to my development database in my app.js file at startup. How would I make my Mocha tests, that are run in a Grunt task, use a test database dynamically when I run Grunt? I have tried to disconnect from development database in my test files in the before() hook in my Mocha test files, and then connect to test database. However, it keeps using development database. An example is the following:
before(function(done) {
if(mongoose.connection.db) mongoose.connection.close();
mongoose.connect(<test_db_uri>, done);
}
Your question is near of the following question Test environment in Node.js / Express application.
Basicly what you should do is use an env variable ('NODE_ENV' for exemple) access it with process.env.NODE_ENV and base on its value call the right configuration file. You should take a look to grunt-express-server which helps you a lot with the environement setup.
I hop this will help!

Adding a default before() function for all the test cases (Mocha)

I'm writing functions for my node.js server using TDD(Mocha). For connecting to the database I'm doing
before(function(done){
db.connect(function(){
done();
});
});
and I'm running the test cases using make test and have configured my makefile to run all the js files in that particular folder using mocha *.js
But for each js file I'll have to make a separate connection to the database, otherwise my test cases fail since they do not share common scope with other test files.
So the question is, Is there anything like beforeAll() that would just simply connect once to the database and then run all the test cases? Any help/suggestion appreciated.
You can setup your db connection as a module that each of the Mocha test modules imports.
var db = require('./db');
A good database interface will queue commands you send to it before it has finished connecting. You can use that to your advantage here.
In your before call, simply do something that amounts to a no op. In SQL that would be something simple like a raw query of SELECT 1. You don't care about the result. The return of the query just signifies that the database is ready.
Since each Mocha module uses the same database module, it'll only connect once.
Use this in each of your test modules:
before(function(done) {
db.no_op(done);
});
Then define db.no_op to be a function that performs the no op and takes a callback function.

Using mysql with express/Node: Is using globals ok?

I can easily enough attach a new connection within my express config to the database using:
global.db = new DB.adapter({});
Thereby I an access it throughout any models I may wish to create. (I am using active record and mysql with express).
However, is this in any way a) insecure, b) bad practice?
I don't think that this would be insecure, but using globals in general is bad practice:
Includes DB global for modules that don't need it. Doesn't matter too much, but is a bit unclean.
Tightly couples a specific DB instance with modules that do need it.
Makes testing more difficult
Makes it harder to change to a different DB for particular modules.
Instead it's much easier to just use CommonJS which is built into node and is very easy to use.
// db.js
module.exports = new DB.adapter({});
//index.js
var db = require("./db");
Both.
Insecure: Any module can change the global context. As any other package repository out there, evil programmers may exist in NPM.
Bad practice: Some test frameworks even consider this as a error (Mocha, for example, but that's configurable). You're in a CommonJS environment, so use modules! Doing so your connection object cannot be lost when require()ing that module, the far I know.

Resources