fbpx

NodeJS integration

Find out how to integrate JSQL with NodeJS

Stack requirements

The JSQL NodeJS library will not work without an account in the JSQL cloud, a backend application (API) integrated with JSQL and integration with the tasks of the runner. To learn more see overview.

Installation

Install JSQL Node plugin into your frontend project using NPM:

npm install jsql-node-plugin –save-dev

Integration

Once the plugin has been installed, it may use it with Express.
Let’s import JsqlConfig and Jsql:

const JsqlConfig = require('jsql-node-plugin').JsqlConfig;
const Jsql = require('jsql-node-plugin').Jsql;

Then we need to create JsqlConfig instance:

const jsqlConfig = new JsqlConfig("YOUR_API_KEY", "YOUR_PRODUCTION_KEY");

And we can add Jsql as handler to our endpoint:

app.post('/api/jsql/select', (req, res) => {
    Jsql.select(req, res, jsqlConfig);
});

Here is working application example:

const JsqlConfig = require('jsql-node-plugin').JsqlConfig;
const Jsql = require('jsql-node-plugin').Jsql;

const bodyParser = require('body-parser');

const express = require('express');
const app = express();
const port = 3000;

app.use(bodyParser.json());

const jsqlConfig = new JsqlConfig(
    "dawid.senko@jsql.it",
    "dawid.senko@jsql.it",
    10000, 15000,
    "https://test-provider.jsql.it/api/jsql");

app.post('/api/jsql/select', (req, res) => {
    Jsql.select(req, res, jsqlConfig);
});

app.post('/api/jsql/delete', (req, res) => {
    Jsql.delete(req, res, jsqlConfig);
});

app.post('/api/jsql/update', (req, res) => {
    Jsql.update(req, res, jsqlConfig);
});

app.post('/api/jsql/insert', (req, res) => {
    Jsql.insert(req, res, jsqlConfig);
});

app.post('/api/jsql/rollback', (req, res) => {
    Jsql.rollback(req, res, jsqlConfig);
});

app.post('/api/jsql/commit', (req, res) => {
    Jsql.commit(req, res, jsqlConfig);
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

Usage

If your application is integrated using the JSQL plugin, you can start it and try to call the query using CURL:

curl 'http://localhost:3000/api/jsql/select' -H 'Accept: application/json' -H 'Content-Type: application/json' 
--data-binary '{"token":"gfsd9394j8dmdc3m2urgnfwos2if","params":{}}' --compressed

What next?

Close Menu