How to read a sqlite3 database, using node js, synchronously? - json

exports.allProbes = function() {
var rows = db.all("SELECT * FROM probes;");
return rows;
};
main:
var json_values = allProbes();
Is it possible to do something like that?
I mean, without using a callback function: just, read data (sync mode) from the db. and return a json formatted output?
Thanks.

There are a few npm packages such as better-sqlite3 and sqlite-sync that allow for synchronous SQLite queries. Here's an example:
var sqlite = require("better-sqlite3");
var db = new sqlite("example.db");
var rows = db.prepare("SELECT * FROM probes").all();
console.log(rows);

You will not be able to do that with sqlite3. With sqlite3 module the only available mode of operation is asynchronous execution, and you will have to use a callback. Eg.
exports.allProbes = function(callback) {
db.all("SELECT * FROM probes;", function(err, all) {
callback(err, all);
});
};
Then in your code:
var json_values;
allProbes(function(err, all) {
json_values = all;
});
Check sqlite3 API Docs.

You have to install sql.js with npm install --save sql.js
Rest follow the below steps :
var fs = require('fs');
var sql = require('sql.js');
var bfr = fs.readFileSync('/tmp/db.sqlite');
var db = new sql.Database(bfr);
db.each('SELECT * FROM test', function (row) {
console.log(row);
});
More details you can get on the below this link : https://discuss.atom.io/t/how-to-access-a-local-db-on-windows-through-electron/22400/13

Related

Node sequelize throwing undefined error when checking if an object exists

Ok, so this is either very weird or I'm not understanding something that is happening. I am trying to load the sequelize library in node.
when trying to connect I'm using the CLI generated index.js file however this line:
if (config.use_env_variable) {
console.log('i ran');
var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
var sequelize = new Sequelize(config.db.database, config.db.username, config.db.password, {
dialect:config.db.dialect});
}
is giving me this error:
Cannot read property 'use_env_variable' of undefined
as far as I know that line is meant to see if this even returns anything so I don't understand why this is throwing that error?
UPDATE
config is invoked in a line above it, the whole file up to that point is:
'use strict';
var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(__filename);
const config = require(path.join(__dirname,'../config/config.js'));
const db = {};
console.log(config);
if (config.use_env_variable) {
console.log('i ran');
var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
var sequelize = new Sequelize(config.db.database, config.db.username, config.db.password, {
dialect:config.db.dialect});
}
UPDATE added console.log of config on working version
After 3 days struggling with error, Today I was able to find a solution.
How did i solve?
I used JS trim() function to remove spaces.
for example
process.env.NODE_ENV === "development" // return false
why ?
"development " === "development" // return false because of extra space
process.env.NODE_ENV.trim() === "development" //return true
Here is my solution
It looks like you do not have config/config.json file or the path is incorrect. In model/index.js, you would have this line
let config = require(`${__dirname}/../../config/config.json`)[env];
or something like that. Make sure this path is the right path

Call sql query synchrously

Currently working on node rest api project where I want to fetch data for a list of data. for example : I have a list of post_id([1,2,3....]) for a particular tag(mobile) and for each post_id I want to retrieve post title and description from mysql database. But calling sql query is synchrounous.
How to control flow for each post id result to combine in one.
my db calling code is here :
var express = require('express');
var app = express();
var bodyParser = require('body-parser'); // call body-parser
var addData = require('./dbhandler/addData'); // call database handler to insertdata
var getData = require('./dbhandler/getData');
//route function to get feeds by tags
router.route('/postfeedsbytags/:tag')
// get all new article feeds filtered by tag
.get(function(req,res){
var success;
console.log(req.params.tag)
var json_results = [];
getData.getPostFeedsByTag(req.params.tag,function(error, results, fields){
if (!error){
for (var i = 0; i < results.length; i++) {
getData.getPostFeedsByPostId(results[0]['post_id'],function(error, results, fields){
if (!error){
success = 1;
json_results.push(results[0]);
res.json({"success" : success, "datasets" : json_results});
} else{
success = 0;
console.log('Error while performing Query.'+error);
res.json({"success" : success});
}
});
}
// res.json({"success" : success, "datasets" : results});
} else{
success = 0;
console.log('Error while performing Query.'+error);
res.json({"success" : success});
}
});
});
I think you can use the IN operator in the query to get all the posts in a single query and then iterate over it.
If you don't want to use IN operator then use async library for flow control. You can use the async.map function from it.

MEAN Nodejs JSON.parse passing data from client to server

I am using MEAN stack and I am sending query parameters dynamically to my Nodejs server endpoints.
My client controller :
$http.get('/api/things',{params:{query:query}}).then(response => {
this.awesomeThings = response.data;
socket.syncUpdates('thing', this.awesomeThings);
});
where query is a value injected into the controller.
This is the server controller function (which works):
export function index(req, res) {
var query = req.query.query && JSON.parse(req.query.query)
Thing.find(query).sort({_id:-1}).limit(20).execAsync()
.then(respondWithResult(res))
.catch(handleError(res));
}
The above works but I am trying to understand the line
var query = req.query.query && JSON.parse(req.query.query)
as I have never seen this before( and I don't come from a programming background). I console.logged query and understand it's an object (which is required by Mongodb) but when I console.logged (JSON.parse(req.query.query)) or JSON.parse(query) to find out the final output, the program stops working with no error messages, very strange..
If someone can explain the above syntax and why it has to be done this way for it work, that would be much appreciated..
PS when I try to console log the JSON.parse like so, it fails to load even though it should have no effect whatsoever:
export function index(req, res) {
var query = req.query.query && JSON.parse(req.query.query)
var que = JSON.parse(req.query.query)
Thing.find(query).sort({_id:-1}).limit(20).execAsync()
.then(respondWithResult(res))
.catch(handleError(res));
console.log("que"+que)
}
function one() {
var x = {};
var res = JSON.parse(x.y);
console.log(res);
}
function two() {
var x = {};
var res = x.y && JSON.parse(x.y);
console.log(res);
}
<button onclick="one()">ERROR</button>
<button onclick="two()">NO ERROR</button>
var x = data && JSON.parse(data);
Since expression is evaluated from left, first data is evaulated.
If it is undefined then, the next part -> JSON.parse() is not performed.
On the other hand, if data is defined parse is tried and the result is returned and stored in x.
Main advantage here is the parse doesn't run if the variable wasn't defined.
it could be equivalent to saying:
if(data) {x = JSON.parse(x);}

Nodejs breeze-sequelize with MySQL - .select() not functioning

I'm using breeze-sequelize version 0.0.18. I get a bizarre error _.pluck is not a function when I try to use select() on the entityQuery. And if I remove .select(), it'll work just fine.
My breeze query looks like this:
var predicate = Predicate.create('transactionDate', '>=', fromDate);
var entityQuery = EntityQuery.from('Transactions')
.where(predicate)
.select('transactionDate');
var sequelizeQuery = new SequelizeQuery(api.db, entityQuery);
return sequelizeQuery.execute();
And upon return, the error I get is:
TypeError: _.pluck is not a function
at SequelizeQuery.<anonymous> (/Users/shu/Documents/project/node_modules/breeze-sequelize/SequelizeQuery.json.js:143:39)
at Array.map (native)
at SequelizeQuery._processSelect (/Users/shu/Documents/project/node_modules/breeze-sequelize/SequelizeQuery.json.js:136:56)
at SequelizeQuery._processQuery (/Users/shu/Documents/project/node_modules/breeze-sequelize/SequelizeQuery.json.js:72:8)
at new SequelizeQuery (/Users/shu/Documents/project/node_modules/breeze-sequelize/SequelizeQuery.json.js:43:23)
at getTransactions (/Users/shu/Documents/project/src/server/api/admin.controller.js:189:26)
So curiously I took a look at function SequelizeQuery._processSelect in my breeze-sequelize library. The error is coming from return usesNameOnServer ? pp : _.pluck(props, "nameOnServer").join(".");.
SequelizeQuery.prototype._processSelect = function() {
var selectClause = this.entityQuery.selectClause;
var usesNameOnServer = this.entityQuery.usesNameOnServer;
if (selectClause == null) return;
// extract any nest paths and move them onto the include
var navPropertyPaths = [];
this.sqQuery.attributes = selectClause.propertyPaths.map(function(pp) {
var props = this.entityType.getPropertiesOnPath(pp, usesNameOnServer, true);
var isNavPropertyPath = props[0].isNavigationProperty;
if (isNavPropertyPath) {
this._addInclude(this.sqQuery, props);
}
if (isNavPropertyPath) return null;
return usesNameOnServer ? pp : _.pluck(props, "nameOnServer").join(".");
}, this).filter(function(pp) {
return pp != null;
});
}
Can someone help me? Thanks!
The _.pluck function is from lodash, and lodash removed the pluck function in version 4.0
The breeze-sequelize library will eventually be updated to lodash 4, but in the meantime, try using lodash 3.x.

Running pipeline section multiple times with different arguments for concat step

I'm a fan of the files object format
files: {
'dest/a.js': ['src/aa.js', 'src/aaa.js'], // key: value
'dest/a1.js': ['src/aa1.js', 'src/aaa1.js'],
}
I have a gulp task that concats source files like
gulp.task('cat', function() {
gulp.src( <value-goes-here> )
.
<many pipeline steps>
.
.pipe(concat(<key-goes-here>))
.pipe(gulp.dest('target/')
.
<more pipeline steps to be run on 'dest/a.js' and 'dest/a1.js'>
.
});
Is there a streaming way to extend this task so that I get 1 bundle file for each key-value in files ?
I would like to NOT create one task per key-value pair, as I would like to continue piping more steps even after the last .pipe(gulp.dest('target/');
If I'm approaching this problem in wrong way, is there a better way?
Thank you in advance!
Rob Rich's answer works, Heres working version :
var Q = require('q');
var gulp = require('gulp');
var concat = require('gulp-concat');
var files = {
'a.js': ['src/aa.js', 'src/aaa.js'],
'a1.js': ['src/aa1.js', 'src/aaa1.js'],
};
gulp.task('cat', function() {
var promises = Object.keys(files).map(function (key) {
var deferred = Q.defer();
var val = files[key];
console.log(val);
gulp.src(val)
.pipe(concat(key))
.pipe(gulp.dest('dest/'))
.on('end', function () {
deferred.resolve();
});
return deferred.promise;
});
return Q.all(promises);
});
Try this:
var Q = require('q');
gulp.task('cat', function() {
var promises = Object.keys(files).map(function (key) {
var deferred = Q.defer();
var val = files[key];
gulp.src(val)
.
<many pipeline steps>
.
.pipe(concat(key))
.pipe(gulp.dest('target/')
.
<more pipeline steps to be run on 'dest/a.js' and 'dest/a1.js'>
.
.on('end', function () {
deferred.resolve();
});
return deferred.promise;
});
return Q.all(promises);
});
You can also accomplish a similar scenario by using streams instead of promises by using combined-stream or stream-combiner packages.