Connect to Databricks SQL endpoint using NodeJS - sql

I am trying to connect to a Databricks SQL endpoint using NodeJS. I followed the instructions on the "Connection Details" tab of my SQL endpoint. As described, I am running Node version 14 or higher, and installed the connector npm package as follows:
npm i #databricks/sql
I used the code provided, included below (I made sure to use the correct host name and access token). I did not change the SQL code from the default (SELECT 1).
const { DBSQLClient } = require('#databricks/sql');
var token = "dapi_MY_ACCESS_TOKEN";
var server_hostname = "MY_HOSTNAME.cloud.databricks.com";
var http_path = "/sql/1.0/endpoints/a8e8b6cfcc6a190f";
const client = new DBSQLClient();
const utils = DBSQLClient.utils;
client.connect(
options = {
token: token,
host: server_hostname,
path: http_path
}).then(
async client => {
const session = await client.openSession();
const queryOperation = await session.executeStatement(
statement = "SELECT 1",
options = { runAsync: true });
await utils.waitUntilReady(
operation = queryOperation,
progress = false,
callback = () => {});
await utils.fetchAll(
operation = queryOperation
);
await queryOperation.close();
const result = utils.getResult(
operation = queryOperation
).getValue();
console.table(result);
await session.close();
client.close();
}).catch(error => {
console.log(error);
});
When I run the code, I get the following error message:
node read_databricks.cjs
TypeError: Cannot read properties of undefined (reading 'waitUntilReady')
at /Users/vijay.balasubramaniam/test/records-to-cards/read_databricks.cjs:23:19
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
I also tried running the above code within the node REPL, but got the same results. Am I missing a step?

I ran into the same issue.
Delete your package-lock.json. Run npm install and make sure that in the package-lock.json file the version is pointing to beta.1 and not beta.2.
"node_modules/#databricks/sql": {
"version": "0.1.8-beta.1",
}

Related

Next.js API's config object is not working on production level

Hy,
I'm working on project. I just want to disable the native bodyparcer of next.js and use formidable's parse for incoming requests. I am sending file and some other data from frontend and saveing file on my file system and other data in mongodb.
I did a following configuration for it in that end point.
export const config = {
api: {
bodyParser: false,
},
}
The full code is here.
const fs = require("fs");
const path = require("path")
const decompress = require("decompress");
import formidable from "formidable";
import connect from "../../../database/connect";
import Proposal from "../../../database/models/propSchema";
const directoryPath = e => path.join(process.cwd(), e),
videosDir = directoryPath("/public/videos")
export default function handler(req, res){
res.setHeader("Content-Type", "application/json");
if(req.method === 'POST')
{
console.log(req.method)
const form = new formidable.IncomingForm();
form.parse(req,(err, fields, files) => {
if(err && !files.file){
return res.status(404).json({status: false, error: "Error: Request is not parsed correctly"})
}
console.log("----------------------------",form._parser.globalOptions.maxFileSize)
console.log(files, fields);
const {client,published} = fields,
id = Math.random().toString(36).substring(2) +
(new Date()).getTime().toString(36);
decompress(files.zipFile.filepath, videosDir + "/" + id)
.then(files => {
console.log(files)
console.log(`Proposal API is called with following queries id:${id} and client:${JSON.stringify(client)}`)
connect().catch(error => console.log(error))
const proposal = new Proposal({id,client,published: published? JSON.parse(published) : true})
proposal.save()
.then(()=>{
res.status(200).json(proposal)
})
})
.catch((err) => {
console.log(err);
const path = `${videosDir}/${id}`;
if(fs.existsSync(path)){
fs.readdirSync(path).forEach((file) => fs.unlinkSync(path + "/" + file));
fs.rmdirSync(path);
}
res.status(400).json({
status: "fail",
info: `${id} cannot be compress and Published`,
err,
});
});
});
}
else{
res.setHeader('Allow', 'POST');
res.status(403).json({status: false , discription: "This method is not allowed." })
}
}
export const config = {
api: {
bodyParser: false,
},
}
I make another endpoint to make code simple to understand and remove all code that is unrelative to this error. The same error is occuring with both endpoints.
export const config = {
api: {
bodyParser: false,
},
}
export default function handler(req, res){
if(req.method !== 'POST')
{
res.setHeader('Allow', 'POST');
return res.status(403).json({status: false , discription: "This method is not allowed." })
}
res.status(200).json({status: true});
};
So, When I run it on my local environment and send data, it works fine but in production, it gives a "413 Request Entity Too Large" error. This is because I think the config object is not working correctly on a production level.
Because 1 MB is the maximum file limit of next.js body-parser and when I try to upload a file less than 1 MB it also works fine in production and local system. But when exceeding that limit it gives that error.
And I also try in my local system to send large files without the above configuration, in this case, it gives the same error.
Is this error in the config object? and who can I solve this error?Please help me also to understand it. Thanks

ExpressJS returns 500 code all the time when trying to send some data

I'm actually working on an API using web-scrapping, but I have a problem that I can't understand.
I have the following route for a GET request:
const router = require("express").Router();
const axios = require("axios");
const cheerio = require("cheerio");
router.get("/:id", async (req, res) => {
const currency = req.params.id;
try {
const res = await axios.get(
`https://coinmarketcap.com/currencies/${currency}/`
);
const html = res.data;
var $ = cheerio.load(html);
const price = $(".statsValue").first().text();
const data = {
market: Number(price.slice(1).replaceAll(",", "")),
};
console.log(data);
res.status(200).json(data);
} catch (error) {
res.status(500).json(error);
}
});
module.exports = router;
When I make a request for this route, I just get the 500 status code, even knowing it has the data.
When I call the API route, it logs this on the console:
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
Server is running on port 5000
{ market: 454939957399 }
This means that it has the data, logs the data, and on the res.status(200).json(data) something fails.
I have the exact 2 lines of status and json response in another API call and it works perfectly, but I've been like 2 hours with this one for almost nothing.
I hope someone can help me <3
You're overwriting the res argument by a new variable:
const res = await axios.get(…);

NPM instagram-web-api checkpoint required

I have configured the NPM instagram-web-api package. I have instantiated the Instagram object and passed the correct credentials:
const Instagram = require('instagram-web-api');
const { igUsername, igPassword } = process.env
const ig = new Instagram({ username: igUsername, password: igPassword });
(async () => {
try {
await ig.login()
} catch (err) {
if (err.error && err.error.message === 'checkpoint_required') {
console.log(err.error);
const challengeUrl = err.error.checkpoint_url
await ig.updateChallenge({ challengeUrl, securityCode: 670381 })
}
}
const profile = await ig.getProfile()
})()
I am getting a 'checkpoint_required' error message and each time I start the server a Instagram verification code is sent to my email. I don't know where to enter that code or how to resolve this issue.
Having the same issue. I thing we need to call an extra api for the OTP validation in order to login.
Check this out - https://github.com/ohld/igbot/issues/630 for the solution or reference.

How use the #c8y/client library

I am testing the new #c8y/client library for typescript.
I have a very simple code :
import {
Client
} from '#c8y/client';
//const baseUrl = 'https://bismark1.cumulocity.com/';
const baseUrl = 'https://demos.cumulocity.com/';
const tenant = 'bismark1';
const user = '...';
const password = '.....';
(async() => {
console.log('authentication to c8y server')
const client = await Client.authenticate({
user,
password,
tenant
}, baseUrl);
console.log('result from authetication', client)
const {
data,
paging
} = await client.inventory.list();
console.log('result from inventory ', data)
// data = first page of inventory
const nextPage = await paging.next();
// nextPage.data = second page of inventory
const managedObjId: number = 1;
(async() => {
const {
data,
res
} = await client.inventory.detail(managedObjId);
console.log(data)
})();
})();
When I run the .js compiled form the .ts file I get the response below :
authentication to c8y server
And then the execution stops.
The line
console.log('result from authetication', client)
is never called. Seems like something fails in the authentication process and not error is showed.
What I'm doing wrong ?
Thanks.
The first problem might be CORS. You need to enable it if you want to request from a different domain. Here is a guide how to do that in Cumulocity:
Under "Access control", administrators can enable cross-origin
resource sharing or "CORS" on the Cumulocity API.
The second problem could be that you are not running it from a local development server. I mostly use this http-server from npm to quickly test scripts. You can use it the following way:
$ npm install http-server -g
$ http-server
If that all is not helping you might try catch the client to see the error it is throwing:
try {
const client = await Client.authenticate({
user,
password,
tenant
}, baseUrl);
} catch(ex) {
console.log(ex);
}
The exeption might tell you more about what is wrong with your code or if there is a bug in the client.

npm restful api suddenly no longer working

I followed this tutorial to create a restful api with npm and postgres
Designing a RESTful API With Node and Postgres
I got everything to work without a problem, closed the server and went to other things.. when I got back, the routing stopped working suddenly giving 404 error.. I checked everything related to routing and I can't find the problem!
When I connect to localhost:3000 I get the correct express homepage
but when I try to access the api, localhost:3000/api/patients the 404 error page appears
Here is my code
index.js
var express = require('express');
var router = express.Router();
var db = require('../queries');
router.get('/api/patients', db.getAllPatients);
module.exports = router;
queries.js
var promise = require('bluebird');
var options = {
// Initialization Options
promiseLib: promise
};
var pgp = require('pg-promise')(options);
var connectionString = 'postgres://localhost:5432/maindb'
var db = pgp(connectionString);
module.exports = {
getAllPatients: getAllPatients
}
function getAllPatients(req, res, next) {
db.any('select * from patients where deleted = false')
.then(function (data) {
res.status(200)
.json({
status: 'success',
data: data,
message: 'Retrieved ALL patients'
});
})
.catch(function (err) {
return next(err);
});
}
It seems something was wrong with express installation, something corrupted it.
I recreated the project from point zero and specified which version of express and bluebird to install, and everything seems to work fine without any problems.