Wednesday, 15 March 2017

Node.js + Express + SQL

I want to do the following and I do not know if it's possible to do it:

Intro:

I have all my querys inside a file like variables, example:

myQuerys.js

var express = require('express');
var router = express.Router();

var qry_customers = "SELECT * FROM customers";

module.exports = {
    "qry_customers ": qry_customers
};

In my customer.js I do the connection to database and I do the query, without problems.

What I like: Pass a variable (for example: CustomerID) to the query!

Something like this:

MyQuery.js

var express = require('express'); var router = express.Router();

var qry_customers = "SELECT * FROM customers WHERE id = " + @CustomerID;

module.exports = {
"qry_customers ": qry_customers };

Customer.js

//connection to the DB

    req.query(qrys.qry_customers + @CustomerID)

Is it possible to do it??


I know I can do, and it works:

 req.query(qrys.qry_customers + " WHERE id = " + **@CustomerID**)



via Sergi Nadal

No comments:

Post a Comment