I'm moving my db from firestore to mongodb. I used to call the db with a const.
const db = firebase.firestore();
firebase.firestore().settings({
cacheSizeBytes: firebase.firestore.CACHE_SIZE_UNLIMITED
});
db.collection("users").doc(user.uid)
.get().then(function(doc) {
if (doc.exists) {
//do something
} else {
console.log("No such document!")
}}).catch(function(error) {
console.log("Error getting document:", error)
});
Now in mongodb, I create if data needed a mongoClient.connect().
Is there a way to call a function or a const and do the same thing like in firestore, or a cleaner way?
const mongo = require('mongodb');
const MongoClient = mongo.MongoClient;
const url = 'mongodb://localhost:27017';
const dbname = "dbname";
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
if (err) throw err;
const db = client.db(dbname);
let collection = db.collection('users');
collection.findOne({ _id: user.uid }).then(user => {
//do something
}).catch((err) => { console.log(err);
}).finally(() => { client.close(); });
});
You can do something like this. Create a wrapper function for database calls
function findOne(query,collectionName,callback){
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
if (err) throw err;
const db = client.db(dbname);
let collection = db.collection(collectionName);
collection.findOne(query).then(user => {
callback(null,user);
}).catch((err) => { callback(err,null);
}).finally(() => { client.close(); });
});
}
Similarly you can create multiple functions for another operations or you may create only one function and then use it as wrapper. The main idea here is that we can utilise callback mechanism
For consuming it
route.get('/',(req,res,next)=>{
findOne({ _id: user.uid },'users',(err,user)=>{res.send(user)})
})
Related
I'm calling this function from another route to create a mongodb doc and to return the objectID from the created doc. But I'm unable to read the docID from the MongoClient.connect(...) function. Does anyone know how to return the docID in the .insertOne(...) function?
//insertOne function
module.exports.insertOne = async function (query, collectionName, callback) {
var objectID1 = await MongoClient.connect(
url,
{ useNewUrlParser: true, useUnifiedTopology: true },
async (err, client) => {
if (err) throw err;
const db = client.db(dbname);
let collection = db.collection(collectionName);
var docID = await collection
.insertOne(query)
.then(result => {
return result.insertedId;
})
.catch((err) => {
callback(err, null);
})
.finally(() => {
client.close();
});
return docID;
}
);
return objectID1;
};
My backend is consist of Api and DB. When I want to get response from DB I have had delayed output by 1 query.
API (I think api is ok. Start read DB first)
app.post('/api/query', (req, res) => {
console.log(`\n Query input : ${JSON.stringify(req.body)}`);
let queryInput = (Object.values(req.body).join(' '));
if(!dbApi.checkArray(queryInput)){ //If array is not made from clear strings
res.json(dbApi.queryFromUser(queryInput));
}
else{
res.json(dbApi.queryOutput);
}
});
app.listen(dbConfig.server.port, () =>
console.log(`Server running on port ${dbConfig.server.port}`));
DB
queryOutput = [];
const receivingQuery =(queryInput) => {
db.query(queryInput, (err, result) =>{
if(err) throw err+' : '+queryInput;
queryOutput = result;
console.log("\nQuery output "+ JSON.stringify(queryOutput)); //Output (result) is ok
});
return queryOutput //Here is Output from previous query (sends to API)
}
module.exports = {
queryOutput: queryOutput,
queryFromUser: receivingQuery,
}
I tryied callback method and I rewrite it couple of times. But I dont have enough skill to solve it.
If You want to return result of query so simply do following things:
add query method to db module:
function query(sql, args = []) {
return new Promise(function(resolve, reject) {
db.query(sql, args, (err, result) => {
if (err) return reject(err);
resolve(result);
});
});
}
// extra feature, getting user by id
async function getUserById(id) {
const result = await query('SELECT * FROM users WHER id = ? LIMIT 1', [id]);
if (Array.isArray(result) && result[0]) return result[0];
return null;
}
module.exports = {
query,
getUserById, // export user by id
queryOutput,
queryFromUser: receivingQuery,
}
use it (with async and await):
app.post('/api/query', async (req, res) => {
try {
console.log('Query input:', req.body);
const queryInput = Object.values(req.body).join(' ');
const result = await dbApi.query(queryInput);
res.json(result);
}
catch (error) {
console.error(error);
res.status(500).json({message: 'Please try again soon'});
}
});
app.get('/api/users/:id', async (req, res) => {
try {
const user = await dbApi.getUserById(req.params.id);
if (!user) return res.status(404).json({message: 'User not found'});
res.status(200).json(user);
}
catch (error) {
console.error(error);
res.status(500).json({message: 'Please try again soon'});
}
});
app.listen(dbConfig.server.port, () =>
console.log('Server running on port', dbConfig.server.port));
I have a nodejs project with the current structure below, I need to insert a registry on clients table and return the last inserted ID from this table so I can use it in a second table, but I need to wait until the insert is completed in clients table, before insert the client ID on my second table. I'm trying to use async/await, but I'm always getting a null value.
My MYSQL connection: db.model.js
const config = require('config');
const mysql = require("mysql");
const connection = mysql.createConnection({
host: config.get('mysql.host'),
user: config.get('mysql.user'),
password: config.get('mysql.password'),
database: config.get('mysql.database')
});
connection.connect(function (err) {
if (err) {
console.error(`MySQL Connection Error: ${err.stack}`);
return;
}
console.log(`MySQL connected successfully!`);
});
module.exports = connection;
My CLIENT model
const mysql = require("./db.model");
const Client = function(client) {
this.login = client.login;
};
Client.create = (newClient, result) => {
mysql.query("INSERT INTO clients SET ?", newClient,
(err, res) => {
if (err) {
console.log("error: ", err);
result(err, null);
return;
}
result(null, {
id: res.insertId,
...newClient
});
}
);
};
module.exports = Client;
this is the client controller (i'm trying to use async/await here)
const Client = require('../models/client.model');
exports.create = (login) => {
const client = new Client({
login: login
});
Client.create(client, async (err, data) => {
if(!err) {
return await data.id;
} else {
return false;
}
});
}
And this is another controller, where I want to use methods from my client controller:
const ClientController = require('../controllers/client.controller');
...
utils.connect()
.then(clt => clt.sub.create(data))
.then((sub) => {
let lastInsertedId = ClientController.create(sub.login);
// lastInsertedId always return null here,
// but I know ClientController return a value after some time.
// method below will fail because lastInsertedId cannot be null
TransactionController.transactionCreate(lastInsertedId,
sub.id,
sub.param);
})
.catch(error => res.send(error.response.errors))
any help appreciated.
File to create database connection
const config = require('config');
const mysql = require('mysql2');
const bluebird = require('bluebird');
const dbConf = {
host: config.dbhost,
user: config.dbuser,
password: config.dbpassword,
database: config.database,
Promise: bluebird
};
class Database {
static async getDBConnection() {
try {
if (!this.db) {
// to test if credentials are correct
await mysql.createConnection(dbConf);
const pool = mysql.createPool(dbConf);
// now get a Promise wrapped instance of that pool
const promisePool = pool.promise();
this.db = promisePool;
}
return this.db;
} catch (err) {
console.log('Error in database connection');
console.log(err.errro || err);
}
}
}
module.exports = Database;
Use connection to execute your native query
const database = require('./database');
let query = 'select * from users';
let conn = await dl.getDBConnection();
let [data, fields] = await conn.query(query);
So I'm still using only the npm mysql package, but now I transformed all my queries into promises like below, so I can just wait until all the queries are completed.
const create = (idCliente, transactionId, amount, status) => {
const sql = "INSERT INTO transactions SET ?";
const params = {
id_cliente: idCliente,
transaction_id: transactionId,
amount: amount,
status: status
};
return new Promise((resolve, reject) => {
pool.query(sql, params, (err, result) => {
if (err) {
return reject(err);
}
resolve(result);
});
});
};
then I use like this:
create(params)
.then((result) => {
//call more queries here if needed
})
.catch((err) => { });
You can use sync-sql package of npm for execute async queries.
https://www.npmjs.com/package/sync-sql
Here is an example of it:
const express = require('express')
const mysql = require('mysql')
const app = express()
var syncSql = require('sync-sql');
// Create Connection
const connect = {
host: 'localhost',
user: 'root',
password: '',
database: 'ddd_test'
}
const db = mysql.createConnection(connect)
db.connect((err) => {
if (err) {
throw err
}
console.log("Connected");
})
function getDbData(query) {
return syncSql.mysql(connect, query).data.rows
}
app.get("/getData", async (req, res, next) => {
let sql = 'SELECT * from registration';
res.json({
data:getDbData(sql)
});
})
app.listen(3000, () => {
console.log('App listening on port 3000!');
});
I am working on a react app and am trying to find a way to pass a variable I define in my front-end (Question.js) to my back-end (server.js) so that I can issue different queries. I have the code
//Question.js
state = {
data: null
};
componentDidMount() {
this.callBackendAPI()
.then(res => this.setState({ data: res.express }))
.catch(err => console.log(err));
}
callBackendAPI = async () => {
const response = await fetch('/express_backend');
const body = await response.json();
if (response.status !== 200) {
throw Error(body.message)
}
return body;
};
//server.js
con.connect(function (err) {
if (err) throw err;
con.query("SELECT question FROM s1questions WHERE ID = 1", function (err, result, fields) {
if (err) throw err;
app.get('/express_backend', (req, res) => {
var x = JSON.stringify(result[0].question);
res.send({ express: `${x}` });
});
});
});
Your sever should probably split your database connection from your route handler definitions. Also, you could use query parameters to access questions based on their id in the database.
// question.js
callBackendAPI = async () => {
const response = await fetch(`/express_backend?questionId=1`);
const body = await response.json();
if (response.status !== 200) {
throw Error(body.message)
}
return body;
};
// server.js
app.get('/express_backend', (req, res) => {
const { questionId } = req.query;
// query database for question by id
});
I'm connecting with SQL Server using Node mssql package in my Electron app.
I can't create REST API.
It works fine although I have concerns:
it doesn't close SQL connection after query
it makes a new DB connection for each query
Is that ok?
How it works:
app.js makes 2 queries and logs results
sql.js connects with DB
// app.js
const { getUser, getUsers } = require('./sql');
getUser(10).then((result) => {
console.dir(result);
});
getUsers.then((result) => {
console.dir(result);
})
// sql.js
const sql = require("mssql");
// DB credentials
const config = {
user: 'myuser',
password: '123',
server: 'myserver',
database: 'mydb',
options: {
encrypt: true
}
}
// Creates new connection pool for each query
function connectDB() {
const pool = new sql.ConnectionPool(config);
return pool.connect()
.then(pool => {
console.log('Connected to database');
// returns Promise
return pool;
})
.catch(err => console.log('Database connection failed!', err));
}
// 1st query
function getUser(id) {
const connection = connectDB();
return connection
.then(pool => {
return pool.request()
.input('PK_ID', sql.Int, parseInt(id))
.execute('[uspGetUser]');
})
.then(result => {
return result.recordset[0];
})
.catch(err => {
console.log('Query failed!', err);
});
}
// 2nd query
function getUsers() {
const DB = connectDB();
return DB
.then(pool => {
return pool.request()
.execute('[uspGetUsers]');
})
.then(result => {
return result.recordset[0];
})
.catch(err => {
console.log('Query failed!', err);
});
}
module.exports = {
getUser,
getUsers
}
No, you don't need to close a.k.a. release a connection back to the connection pool after every query. The library already does that for you.
The pool.close() method will close all the connection in the pool. Technically, you should only do that when you're terminating your application, not after every query, since creating a new pool every time create quite an overhead on your application.
I had the same question myself, so I looked up the project's source code.
Solved!
To close DB connection and return results, we can use finally statement and asnyc/await functions.
The finally statement lets you execute code, after try and catch, regardless of the result.
// sql.js
const sql = require("mssql");
// DB credentials
const config = {
user: 'myuser',
password: '123',
server: 'myserver',
database: 'mydb',
options: {
encrypt: true
}
}
async function connectDB() {
const pool = new sql.ConnectionPool(config);
try {
await pool.connect();
console.log('Connected to database');
return pool;
}
catch(err) {
console.log('Database connection failed!', err);
return err;
}
}
async function getAll() {
const DB = await connectDB();
try {
const result = await DB.request()
.query('select * from [your_table]');
return result.recordset;
}
catch (err) {
console.log('Error querying database', err);
return err;
}
finally {
DB.close();
}
}
async function execute() {
let result = await getAll();
console.dir(JSON.stringify(result));
return result;
}
execute();