How to query a database from an Azure function in js - javascript

I have an IOT hub getting some data. I also have an Azure function app in js that is triggered when an IOT event occurs. In the function app, I want to query the incoming data against a azure sql database.
In the azure function->application settings->connection string, I created a connection string x with value of the azure db connection string. My index.js file is as below.
module.exports = function (context, IoTHubMessages) {
context.log(`JavaScript eventhub trigger function called for message array ${IoTHubMessages}`);
IoTHubMessages.forEach(message => {
context.log(`Processed message ${message}`);
var sqlConnection = x;
});
context.done();
};
I get an error that x is not defined. How can I access x? Also how how to execute a select query from here.
Any help will be greatly appreciated.

Try accessing your app setting using process.env["x"] Here are the docs, and here's a related issue you may face if you're trying to run this locally.
I don't have experience writing Javascript functions to execute a select query, but this documentation seems like a good place to start: Use Node.js to query Azure SQL Database

There is no easy way to access connection strings in Node.js.
Instead, the suggestion is to use app settings for all your secrets and connection strings. You can them access them using process.env.YourAppSetting.
See similar questions one and two.

Related

neo4j javascript example

What I have not been able to find is simple examples (no third party) of neo4j using javascript. I have got the desktop of neo4j working and got an example with a third party graph tool working (the example appears to put the request in the textarea of a DIV and send the request to the graph api and the graph is produced).
I am very familiar with MYSQL, other SQL interaction but having problems interacting with neo4j. Have done a lot of research but stuck.
From my SQL days there was:
connect statement (i.e. get a handle and I have got this to work with neo4j)
send an SQL statement to the database, (in this case it would be cypher)
get the cursor and process the results (I assume process the Jason)
I would like the example to:
Connect to the database (local and remote)
Show sample cypher commands to fetch data (movie dtabase)
How to store returned results in the javascript program
if possible provide a short explanation of Node, HTML, Javascript ie the javascript goes into app.js and there is index.htnl that refers to app.js. Do I have to use Node can I access neo4j with Javascript only?
Thanks
Marty
Take a look at the official Neo4j Driver for Javascript. The driver can be used with node.js and there is also a version that runs in a browser.
The repo's README contains links to full documentation and sample projects.
AS #cybersam told you, you should use the neo4j-javascript-driver.
You can find an example application here : https://github.com/neo4j-examples/movies-javascript-bolt
And this is snippet on how to perform the connection, a query and to parse the result :
// Create a driver instance, for the user neo4j with password neo4j.
// It should be enough to have a single driver per database per application.
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
// Create a session to run Cypher statements in.
// Note: Always make sure to close sessions when you are done using them!
var session = driver.session();
// the Promise way, where the complete result is collected before we act on it:
session
.run('MERGE (james:Person {name : {nameParam} }) RETURN james.name AS name', {nameParam: 'James'})
.then(function (result) {
result.records.forEach(function (record) {
console.log(record.get('name'));
});
session.close();
})
.catch(function (error) {
console.log(error);
});
// Close the driver when application exits.
// This closes all used network connections.
driver.close();
Moreover, you can also take a look at the GRAND stack : http://grandstack.io/
It's stack to build a web application based on React, Neo4j and GraphQl (with Apollo).

Cannot create database with pouchdb in couchdb

while trying to just create a database with pouchDB for couchDB, the following error is thrown:
{"error":"not_found","reason":"Database does not exist."}.
My code to create a test db is as simple as this:
var db = new PouchDB('http://localhost:5984/testdb');
Also, the couchDB server is running and reachable at http://localhost:5984/ and it says:
{"couchdb":"Welcome","version":"2.1.1","features":["scheduler"],"vendor":{"name":"The Apache Software Foundation"}}
I have even enabled CORS for couchDB.
Thanks in advance.
According to the PouchDB API (especially the info block), it doesn't automatically create the database by creating a new PouchDB object.
You need to call an API function of the database in order to create the database.
For example, you could create your PouchDB object and then call db.info() to create the database.

Can't connect from Google Apps Script to Google Cloud SQL

I'm trying to connect from my Google Apps Script project to my Google Cloud SQL (second generation) instance. Here's the code I've got now.
I was able to connect to it successfully when connecting thru IP, but that's unencrypted. All of the Google Apps Script IPs are whitelisted also.
If my Instance ID is 12345 then my Instance connection name is 12345:us-west1:12345.
var conn = Jdbc.getCloudSqlConnection('jdbc:google:rdbms://instanceConnectionName/database',{user: 'username', password: 'password'});
I've tried:
Taking the username and password out of the curly braces, so it's just string, string, string
Using the instance name (keep in mind the difference between Instance ID and Instance connection name)
Removing the database
Removing the //
Switching authentication modes (AuthMode.LIMITED and AuthMode.NONE)
A whole bunch of other stuff
So basically, I just can't figure it out and some help would be appreciated it. Thank you!
Google's documentation is difficult to understand here. Basically, they tell you that the connection url must look like this:
var conn = Jdbc.getCloudSqlConnection('jdbc:google:rdbms:subname');
However, for a MySQL database, it must look like this:
var conn = Jdbc.getCloudSqlConnection('jdbc:google:mysql://subname');
Just replace rdbms with mysql. That's it.

How to save Watson Conversation history in my mySQL database?

I just deployed the Watson Conversation plugin on Wordpress and its working very well - I can talk to Watson and he works just as he does in Bluemix.
However, I have no way to then see or manage the conversation history (aside from going into Bluemix).
The WP plugin has the api.php and app.js and I know one of these must at some point handle the text that the user inputs and Watson responds.
Does anyone know how I could save that conversation into my WP mySQL database, so I can analyze and work with it?
Thank you!
If this plugin is using Nodejs (because of app.js) you need to create one custom code for it. Probably, the app.js have the call for the Conversation Service.
If this is your case...
You need to access the Log from Conversation service and get the return, and after it, save inside one MySQL database.
Using the plugin mysql inside the app.js if the lib Watson-Developer-Cloud is inside the code. And access the Logs with getLogs function. Where the Conversationv1 is the call for this service.
For use the mysql lib, please follow the example code:
var mysql = require('mysql');
var connMySQL = function(){
return mysql.createConnection({
host: 'localhost',
user: 'yourUser',
password: 'yourPassword',
database: 'nameDatabase'
});
}
module.exports = function (){
return connMySQL;
}
I really recommend this project for you know the follow steps. In this case, the call for conversation service is the conversation variable, and for access the return, like entities, context varibles, etc, you will use the data return.
For example:
data.context.contextVariableInsideWatson
Official reference from IBM Developers: watson-developer-cloud/node-sdk
Repository with one simple project using Conversation from IBM Developers here.

How do i make multiple Dnode connections?

I'm doing an autocomplete input form and i want to send what my user types to a remote database for suggestions.
I use dnode for it now, and i do not want to do a new remote connect each time my user types so i made the remote function global like this
dnode.connect(5050, function (remote) {
window.remote = remote
});
So each time i want to check my mongodb i just use the window.remote.function and dont have to reconnect. Are there a better way?
Thanks
I suggest using Socket.IO directly for this, which is actually used by DNode under the hood for exchanging information between the server and the browser. Find more information about Socket.io on the following sites:
http://socket.io/
https://github.com/LearnBoost/socket.io
Bind your autocomplete listeners inside of the scope of the dnode connection instead of exposing the connection to the outside.
Instead of doing:
dnode.connect(5050, function (remote) {
window.remote = remote
});
autoCompleteLogic(window.remote)
do this instead:
dnode.connect(5050, function (remote) {
autoCompleteLogic(remote)
});

Categories

Resources