How to show queries in console log using sails? - node.js

I am starting a project with sails and mysql, and I do'nt know how configurate it to show the queries executed in the console.

The mysql adapter has a debug variable LOG_QUERIES to send all queries to the console.
LOG_QUERIES=true node myFile.js

Unfortunately this isn't possible with Sails at this time, although the feature has been requested. Your best bet is to inspect the log file provided by your database:
Postgres: How to log PostgreSQL queries?
MySQL: Log all queries in mysql
MongoDB: MongoDB logging all queries

This solution works for sails >= 1.1.
The sails-mysql (as of v1.0.0) adapter uses the machinepack-mysql driver which logs queries to debug. So all that's needed to log all native queries is to enable the debug flag:
DEBUG=query node app.js

If you're doing Sails.js development on Mac OS with Postgresql.app, you can enable all query logging as follows:
In a new Terminal, edit ~/Library/Application Support/Postgres/var/postgresql.conf and set:
logging_collector = on
log_directory = 'pg_log'
log_statement = 'all'
Then restart Postgresql.app (click on menubar icon, Quit, then use Spotlight to relaunch).
Then tail the log with:
tail -F ~/Library/Application\ Support/Postgres/var/pg_log/postgresql-2014-10-30_104957.log
You'll have to find the newest postgresql-*log to tail and substitute that above.

My answer might me very late but just found a solution which might help people
sails version = 0.12.14
to log quires do following
navigate to
node_modules\sails-mysql\lib\adapter.js
locate variable declaration of "log"
then change "process.env.LOG_QUERIES" to "sails.config.LOG_QUERIES"
navigate to
[sails root]/config/env/[development or production].js
add
LOG_QUERIES = 'true',
NOTE :- remember to put ""or '' for true

Or add log to sql module.
In mysql this is:
working_dir/node_modules/sails-mysql/node_modules/mysql/lib/Connection.js
function **createQuery** (33)

Put
LOG_QUERIES = 'true'
in sails lift command

Just add this to your datastore
debug: ["ComQueryPacket"]
config/datastores.js should be look like this
module.exports.datastores = {
default: {
adapter: 'sails-mysql',
user: '*****',
password: "****",
host: '*****',
database: '****',
debug: ["ComQueryPacket"]
}
}

Related

Is there a way to expose debug lines during scp2 upload operation in nodejs?

I am using this package in a nodejs app: https://www.npmjs.com/package/scp2 and it is calling upload(src, dest) to a unix server. It is not connecting to the unix server and the only error I see is Exit code 255 while establishing SFTP session. I realize it is not connecting due to something on the Unix servers OpenSSL package changing but since its rollback, it is still not working as it was before.
Is there a way to display the debug lines for scp2.upload()? I am stepping through the scp2 package in vscode debug mode but not finding anything.
I added this in the scp2 configs: debug: console.log as such:
this.sshCreds = {
host: this.host,
username: this.username,
password: this.password,
debug: console.log,
};
this.sshClient = new SshClient(this.sshCreds);

strapi start: "url is not defined" - using mlab mongo DB

When I run 'strapi start' I get the following output in my terminal:
debug ⛔️ Server wasn't able to start properly.
Error URL is not defined
Any solutions? I have recreated my mlab DB 3 times and when I enter all the values in the terminal to create the new strapi server I double check my values and everything from GitHub issues and strapi documentations say I'm doing the correct thing, but not able to figure out why this is happening! Appreciate any help!
As #soupette have said in the comment:
Strapi needs Node v10 to be installed in order to work. If you upgrade
your node version it should be working properly
I can confirm the same error on my end with NodeJS 8.11.1
Strapi's GitHub page confirms that minimum NodeJS version is 10.x
Ok when you create a Mondo db on mlab you get this information:
mongo ds129484.mlab.com:29484/blog -u <dbuser> -p <dbpassword>
mongodb://<dbuser>:<dbpassword>#ds129484.mlab.com:29484/blog
When you start a strapi project, you make this
strapi new blog
cd blog
strapi start
So you select mondodb how to default db and the cli show the follow questions, and you would be add the following answers, supposing that you have the db data showed before:
DBname: blog
host: ds129484.mlab.com
srv connection: false
port: 29484
username: username created on mlab for your db
password: username password created on mlab for your db
authentication db: blog (you have put the name of your db)
enable ssl: false
You press Enter and you project would be run perfectly
First of all you must have Node 10 or above. If you still getting error edit the following file:
config/host.json
Use an higher timeout value.
{
"timeout": 60000,
......
}
Assuming you are on the correct node version, goto the configuration.js found in node_modules/strapi/lib/core/configurations.js and update lines 331-333.
Replace
this.config.admin.url = this.config.admin.devMode ?
(new URL(adminPath, `http://${this.config.host}:4000`)).toString():
(new URL(adminPath, url)).toString();
With
this.config.admin.url = this.config.admin.devMode ? `http://${this.config.host}:4000/admin`:`http://${this.config.host}:1337/admin`
Thanks
Here is what worked for me:
Just upgrade your node js installation to version 10

Running Ghost in IIS with Iisnode

I have a problem getting iisnode and ghost to play together.
I can run the samples supplied with iisnode fine and I can get ghost up and running fine through the node command line. I have followed every blog post written online I can find to get them set up together. The frustrating thing is the blogs make this process look extremely straightforward.
From looking at "ETW" logs it looks like iisnode cannot communicate with the node process using named pipes ("iisnode scheduled a retry of a named pipe connection to the node.exe process" x20+), and then the node process terminates.
The problem may not lie with ghost, tbh I cannot work out what to do or which part to tweak next. My best guess at this point is some sort of permissions issue, but I've spent hours setting permissions all over my development machine to no avail.
If it helps, the error message I get back in the browser is: ERROR: (Code: EACCES) There was an error starting your server.
To answer my own question, I examined the Process Monitor output for node.exe, line by line to see if I could see anything strange. I'm no expert at analysing the output from Process Monitor and there are a tonne of "Buffer Overflow" messages and other things that could be errors, but then I happened to stumble upon the following line:
Node.exe CreatePipe \MyProjects\WebsiteName\process.env.port\ INVALIDDEVICEREQUEST
Invalid device?
Then the penny finally dropped. In config.js you do not want to set your port to 'process.env.port' (as is suggested in the config.js file itself as a comment) you want to set the port to process.env.port.
No quotes.
Just to clarify what ruethewhirl551 said, here is an excerpt from my working "config.js":
config = {
production: {
url: 'http://my-blog.com',
mail: {
transport: 'SMTP',
options: {
service: 'Mailgun',
auth: {
user: 'postmaster#my-blog.com',
pass: '9ed512da012cd454376947365xd71793'
}
}
},
database: {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, '/content/data/ghost.db')
},
debug: false
},
server: {
host: '127.0.0.1',
port: process.env.PORT // <-- HERE
}
},
// ...
Please notice the line port: process.env.PORT at the bottom.
You can also search for "iisnode" through the "config.example.js" file for more information.

Within Docker VM, Gulp-Watch Seems to not work well on volumes hosted from the host OS

So I have a setup, probably as most people have, where their app code is mounted into a Docker container through a separate volume.
The problem is that if I run gulp, and specifically gulp-watch, to watch for file modifications etc. within docker, on the app code mounted within the docker container, to properly build and restart node within the docker container as necessary, it seems to get cpu intensive (as in polling for file changes instead of listening for file change events) to the point where my machine buckles.
I think this is due to a limitation of having the file system mounted from the native host to the docker container but how are folks working around this? Are they doing all of their work in the container? Native host then constantly building? Or am I missing something where my setup is incorrect with gulp-watch / nodemon?
For anyone using gulp4
The only way I could get this to work is to use usePolling like below
gulp.watch('./**/*', {interval: 1000, usePolling: true}, gulp.series('superTask'));
Try changing the gulp.watch options. This has been much better for me:
gulp.watch('./**/*', {interval: 1000, mode: 'poll'}, ['build']);
You should use the plugin gulp-watch instead of gulp.watch. The latter uses stat polling, which is much too heavy for the shared file system. gulp-watch uses inotify events to watch the file system on OSX.
The previous answer of usePoll: true didn't work. This one did:
gulp.watch('./**/*', {interval: 1000, usePolling: true}, ['build']);
Jesse's answer didn't work for me, but it was really close. Now, the option seems to be:
gulp.watch('./**/*', {interval: 1000, usePoll: true}, ['build']);
The mode field has been switched out for the usePoll field flag.
See the API section for more details.
In a docker container that has a nodemon installed (npm i -g nodemon) there is an alternative to gulp watch.
Let's say that one wants to watch changes to a swagger.yaml file in ./swagger/swagger.yaml and convert it to a project.json file for use with swagger UI.
Assuming that the correct node modules are installed, or that a stand-alone yaml to json convert tool is installed, one could run the following:
nodemon -L --watch ./editor/api/swagger/* --exec "node ./cvt_yaml_to_json.js"
where:
./editor/api/swagger/* is the directory to watch for file changes
"node ./cvt_yaml_to_json.js" is the command to execute (it can be an arbitrary command). In this case it is a JavaScript script which depends on js-yaml module (npm i js-yaml) and performs YAML to JSON conversion like this:
const yaml = require("js-yaml");
const path = require("path");
const fs = require("fs");
const swaggerYamlFile = "/api/project/editor/api/swagger/swagger.yaml";
const swaggerJsonFile = "/api/project/project.json";
//Converts yaml to json
const doc = yaml.safeLoad(fs.readFileSync(swaggerYamlFile));
fs.writeFileSync(swaggerJsonFile, JSON.stringify(doc, null, " "));

How to actually run mozilla openbadges

I'm following this tutorial here.
https://github.com/mozilla/openbadges-badgekit/wiki/BadgeKit-Self-Hosting-Guide#badgekit-api-configuration
It says, when you run the API use this command source env_local
The problem is, I'm new to node and not really sure how I run the API. I've downloaded all the stuff, installed node, and got a simple hello world program working with node. I just don't know how I actually run the API. I thought I had to run the procfile, but when I do node procfile I get an error saying cannot find module badegkit\badgekit-api\start
As a hacky ass solution, I figured this out. I tried setting PATH in environment variables to env_local, as well as doing SET path = env_local in the command window and neither worked. However, if you browse to the badgekit api folder then app then lib, there's a db.js file in there.
Here's what I did, their stuff is commented out.
var options = {
driver: 'mysql',
// host: process.env.DB_HOST,
// user: process.env.DB_USER,
// password: process.env.DB_PASSWORD,
// database: process.env.DB_NAME,
host: "127.0.0.1",
user: "username",
password: "password",
database: "dbname",
}
Just put in your mysql creds and then you can run the db migrate. Obviously this isn't ideal as anywhere else that uses env is still going to be screwy, but it got me to step 2.
2014-09-29 Edit
For all those curious, as of right now, node isn't really supported on windows per se. I ran into so many issues with python and gyp, that I ended up just spinning up a linux box in hyper v and hosting it there.
The idea behind that command is to load the environment variables setup in env_local. By deafult, they are set up as a few export commands, which should put the key=value pairs into your running environment.
I actually removed the export strings and stored them as .env_dev, so a file with:
DB_HOST=localhost
DB_NAME=badgekitapi
...etc...
And the command I used to run things was: nf start -e .env_dev
With the API running, and able to store badges, you can now follow the tutorials to run openbadges-badgekit - so you can actually make and issue some badges ; )
Good luck!

Resources