How do I "promisify" my own function (that lives in another directory)? Here's my code:
// app.js
// include database
var mongo = require('./mongo');
var promise = require('bluebird');
var u = require('./util.js');
var mongo.connect = promise.promisify(require(mongo.connect));
// connect to database
var ago = new Date(new Date()-60000); // 10m ago
var scope = {size:0.01};
mongo.connect()
.then(
mongo.endor.mapReduceAsync(u.mapTile, u.reduceTile,{
out: {replace:'deathstar'}, scope:scope,
query: {time:{$gte:ago}}, finalize:u.finalTile}))
.then(deathstar.find({},{fields:{_id:0, value: 1}})
.toArray(function(err, docs){
endor.insertAsync(_.map(docs, function(doc){return doc.value;}),{w:1});
)
);
And here's the other file
// mongo.js
var promise = require('bluebird');
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
promise.promisifyAll(mongodb);
module.exports.connect = promise.method(function(){
// database environment
var database;
if (process.env.NODE_ENV == 'production'){database = pro;}
else{database = database = dev;}
module.exports.database = database;
// connect to database
return MongoClient.connectAsync(database)
.then(function(db){
module.exports.db = db;
promise.all([
db.createCollectionAsync('hoth', {w:1})
.then(function(collection){
collection.ensureIndexAsync()
.then(function(index){
module.exports.hoth = collection;
console.log(index+' hoth');
});
}),
db.createCollectionAsync('endor', {w:1})
.then(function(collection){
collection.ensureIndexAsync({})
.then(function(index){
module.exports.endor = collection;
console.log(index+' endor');
});
}),
db.createCollectionAsync('alderaan', {w:1})
.then(function(collection){
collection.ensureIndexAsync({time:-1, location:'2dsphere'})
.then(function(index){
module.exports.endor = collection;
console.log(index+' alderaan');
});
})
]).then(function(){callback(null);});
}
);
});
and here's the error
/Users/Josh/Development/xWing/app.js:12
mongo.connectAsync()
^
TypeError: Object #<Object> has no method 'connectAsync'
Promises bring back the qualities of synchronous code.
Given your API is already promisified - You simply return a promise from your function and then use that.
module.exports.connect = Promise.method(function(){
// do a bunch of stuff
return mongo.connectAsync().then(...
});
Note the Promise.method bit isn't required, it just turns thrown errors into rejections to give you throw safety and makes sure you return a promise so it's best practice and guards you against mishaps.
Related
I currently have an issue in Node.js whereby I make a call to a mysql database using the promise-mysql and bluebird packages. I followed the tutorials on the webpage and it seems to work. I keep ending up with a timeout error as once the database has been queried the process doesn't break (if that's the right term) and I either have to terminate the process using ctrl + c or when testing on Alexa when the skill replies with there was a problem. Is there a way to end the promise and db connection once I have retrieved the value of my query? Below is an example of the code.
index.js
'use strict';
const Alexa = require('alexa-sdk');
const mysql = require('promise-mysql');
const querydb = require('./sqlQuery.js');
const insertdb = require('./sqlInsert.js');
var testSQL = 'SELECT weight, height from users where pin=1100';
var valuesPromise = querydb.getItem(testSQL);
valuesPromise.then((result)=>{
rows = result.height;
row2 = result.weight;
console.log(rows);
console.log (row2);
return true;
}).catch(function(error){
console.log (error);
});
databaseConnection.js
var mysql = require('promise-mysql');
pool = mysql.createPool({
host: "hostURL",
database: "dbName",
user: "dbUser",
password: "dbPassword",
connectionLimit: 1
});
function getSqlConnection() {
return pool.getConnection().disposer(function(connection) {
pool.releaseConnection(connection);
});
}
module.exports = getSqlConnection;
sqlQuery.js
var Promise = require("bluebird");
var getSqlConnection = require('./databaseConnection');
function getItem(sql){
return Promise.using(getSqlConnection(), function(connection) {
return connection.query(sql).then(function(rows) {
return rows[0];
}).catch(function(error) {
return (error);
});
})
};
module.exports.getItem = getItem;
Since you are in node process you can just exit from it with process.exitCode = 1 and then process.exit() or process.exit(1).
Here is the documentation.
That however is not the best practice. The easiest way would be to just throw an error:
throw "Exit!" or some error you would like to catch and re-throw to exit (in the scenario where you have a catch statement)
I'm having trouble trying to return promise results as a yield back to the original caller.
store.js
module.exports = {
find: function *(storeRequest){
if(!_gateway){
_gateway = realGateway;
}
storeResponse.http.statusCode = 200;
var stores = _gateway.find(storeRequest.id).next().value; // I want to be able to get the stores array back here ultimately by calling next() like I am trying to do here
console.log(stores[0]);
//yield storeResponse;
}
};
storeGateway.js
module.exports = {
find: function *(id){
var stores = [];
var entity;
database.find.then(function(foundStores){
entity = testUtil.createStore(foundStores[0].id, foundStores[0].name);
console.log("ENTITY:");
console.log(entity);
stores.push(entity);
console.log("STORES[0]:");
console.log(stores[0]);
// I am getting the results here successfully so far when I console.log(stores[0])! But now I want to return stores now from here and yield the array so it propogates up to the caller of storeGateway's find()
// yield entity; --- this doesn't work because I think I'm in the promise then scope
}
);
//yield entity; -- and this definitely won't work because it's not in the promise callback (then)
}
};
database.js
var co = require('co');
var pg = require('co-pg')(require('pg'));
var config = require('./postgreSQL-config');
var database = module.exports = {};
var _id;
var _foundStores;
database.find = co(function* poolExample(id) {
var query = "Select id, name from stores";
try {
var connectionResults = yield pg.connectPromise(config.postgres);
var client = connectionResults[0];
var done = connectionResults[1];
var result = yield client.queryPromise(query);
done();
console.log("PRINTING ROWS:");
console.log(result.rows[0]);
_foundStores = yield result.rows;
} catch(ex) {
console.error(ex.toString());
}
console.log("RESULTS!:");
console.log(_foundStores);
return _foundStores;
});
I'm getting data printed on every console.log you see above. I just don't know how to return the stores from the storeGateway's find() method since it's receiving the stores array in the promise result (in the .then()) and I need to be able to yield that back upstream.
(see my comment in code, I'm trying to return the found stores in the promise's then back upstream from my store.js's find generator function).
The point of using generators and co is that you can yield promises to the coroutine runner and get their results back, so that you don't have to use then.
Start by making find a method in your database.js:
database.find = co.wrap(function* poolExample(id) {
// ^^^^^
…
});
Then in storeGateway.js you should be doing
module.exports = {
find: function*(id) {
var foundStores = yield database.find(id);
var entity = testUtil.createStore(foundStores[0].id, foundStores[0].name);
console.log("ENTITY:", entity);
var stores = [entity];
console.log("STORES[0]:", stores[0]);
return stores;
}
};
(maybe wrap the generator function in co.wrap(…)).
Then in store.js you can do
module.exports = {
find: co.wrap(function*(storeRequest) {
if (!_gateway) _gateway = realGateway;
storeResponse.http.statusCode = 200;
var stores = yield* _gateway.find(storeRequest.id);
// or yield _gateway.find(storeRequest.id); if you did wrap it and it
// returns a promise, not a generator
console.log(stores[0]);
return stores;
})
};
There are two ways to do this. You either receive a call back parameter into your function and call it when the promise is resolved (inside your then function) or better, return the result of then. then() itself returns a promise and whatever you return from with in the function are available to the subsequent functions chained to the promise, so if you do this
return database.find.then(function(foundStores){
entity = testUtil.createStore(foundStores[0].id, foundStores[0].name);
console.log("ENTITY:");
console.log(entity);
stores.push(entity);
console.log("STORES[0]:");
console.log(stores[0]);
return stores[0];
}
then you could do gateway.find().then(function(stores){}) and stores would be what you returned i.e. stores[0].
Been testing mysql vs mysql2, seems like 2 has made some improvments however it's not an exact drop in replacement. At the same time Q is a good library which seems easier to integrate with however bluebird seems to take less memory and run faster so...
My current mysql-bluebird connector is as follows and allows for straight forward use of query('SELECT email FROM users.users WHERE id=?',id).then(function(res){var email=res[0][0];});
/* global module, require */
var conf=require('./conf.js').conf;
var mysql = require('mysql');
var Promise = require('bluebird');
var using = Promise.using;
Promise.promisifyAll(require('mysql/lib/Connection').prototype);
Promise.promisifyAll(require('mysql/lib/Pool').prototype);
var pool = mysql.createPool(conf.mysql);
var getConnection = function () {
return pool.getConnectionAsync().disposer(function (connection) {
return connection.release();
});
};
var query = function (command) {
return using(getConnection(), function (connection) {
return connection.queryAsync(command);
});
};
function queryWrapper(q,a){
if(a){
return query(mysql.format(q,a));
}
else{
return query(mysql.format(q));
}
}
module.exports = {
query: queryWrapper
};
So far my attempts ad doing this with mysql2 haven't panned out.
Does anyone have any insights on how to convert this?
Thanks, Jegsar
You can use mysql2-promise. It's a simple wrapper, using q, that promisifies mysql2. If you'd rather use Bluebird, you can look at how this wrapper was created and do it yourself.
node-mysql2 now has Promise api built in, and you can choose which promise implementation you want to use
var mysql = require('mysql2/promise');
mysql.createConnection({
Promise: require('bluebird'), // if not set, global Promise is used
user: 'foo',
password: 'bar',
database: 'baz'
})
.then((conn) => conn.query('select 1+1 as test'))
.then(([rows, fields]) => console.log(rows[0]))
Can anyone help me figure out how to rewrite the following working code in promise? I want to execute an insert query and then send the data back to clients via socket.js
var func = require("functions")
var mongoose = require('mongoose');
var mongo_models = require('./database/mongo_model')(mongoose);
var Promise = require("bluebird");
socket.on("submit",function(d){
if(d[0].src !== undefined)
{
var data = func.clear(sanitizer,d),
tab = new mongo_models.Tab({avg:0,post:data});
tab.save(function(err,tw){
io.sockets.in('index').emit("update",tw)
})
}
});
I have tried the following code but I'm getting undefined from console.log(b). Apparently the save() isn't returning any result.
Promise:
socket.on("submit",function(d){
if(d[0].src !== undefined)
{
Promise.props({
one: func.clear(sanitizer,d)
}).then(function(a){
return new mongo_models.Tab({avg:0,post:a.one}).save();
}).then(function(b){
console.log(b); // undefined
io.sockets.in('index').emit("update",b)
}).catch(function (error) {
console.log(error);
})
}
});
Save is problematic in Mongoose as being an exception to the rule of promise returning functions there.
I highly recommend that you promisify mongoose using bluebird which exposes a saveAsync function:
var Promise = require("bluebird");
var func = require("functions")
var mongoose = Promise.promisifyAll(require('mongoose'));
var mongo_models = require('./database/mongo_model')(mongoose);
And then return the async call as:
// Note the suffix
return new mongo_models.Tab({avg:0,post:a.one}).saveAsync();
I have a function which generates a checksum for a given path
function getHash(path) {
var fs = require('fs');
var crypto = require('crypto');
var fd = fs.createReadStream(path);
var hash = crypto.createHash('sha1');
hash.setEncoding('hex');
fd.on('end', function () {
hash.end();
// *** Here is my problem ***
console.log(hash.read());
});
fd.pipe(hash);
};
I want to call the calcNewHash function so that it returns the hash, the problem is, that I haven't found an asynchronous version of this, maybe some one can help.
Just add a return statement doesn't work, because the function is in a Listener
Later I want to add the checksum to an object, so I could give the reference to it as parameter, but this still does not change that this is ansynchronous ...
You basically have 2 solutions:
#1 Work with promises (i.e. q - npm install q --save)
function getHash(path) {
var Q = require('q');
var fs = require('fs');
var crypto = require('crypto');
var deferred = Q.defer();
var fd = fs.createReadStream(path);
var hash = crypto.createHash('sha1');
hash.setEncoding('hex');
fd.on('end', function () {
hash.end();
// *** Here is my problem ***
console.log(hash.read());
deferred.resolve(hash.read());
});
fd.pipe(hash);
return deferred.promise;
};
getHash('c:\\')
.then(function(result) {
// do something with the hash result
});
#2 Or use a callback function
function getHash(path, cb) {
var fs = require('fs');
var crypto = require('crypto');
var fd = fs.createReadStream(path);
var hash = crypto.createHash('sha1');
hash.setEncoding('hex');
fd.on('end', function () {
hash.end();
// *** Here is my problem ***
console.log(hash.read());
if (cb) {
cb(null, hash.read());
}
});
fd.pipe(hash);
};
getHash('C:\\', function (error, data) {
if (!error) {
// do something with data
}
});
If you don't have deep nesting in callback functions I would go for option #2.
(Note: behind the scenes promises are also using callbacks, it's just a 'cleaner' solution if you have deep nesting)
I know this is old but it is in the top results when searching for something along these lines. So for those that land here looking for a solution to this, here you go: (note that this only works well if you know the file is small. Otherwise for larger files refer to the answer provided by Dieterg)
const fs = require('fs');
const crypto = require('crypto');
function fileHashSync(filePath){
var fileData;
try{ fileData = fs.readFileSync(filePath, 'utf8'); }
catch(err){
if(err.code === 'ENOENT') return console.error('File does not exist. Error: ', err);
return console.error('Error: ', err);
}
return crypto.createHash('sha1').update(fileData, 'utf8').digest('hex');
}