Pull knexnest call out to file of its own to prevent duplication - javascript

I'm using knexnest as follows:
const customerProducts = knex.select([
'CustomerProduct.id AS _id',
'CustomerProduct.price AS _price',
'CustomerProduct.active AS _active',
'CustomerProduct.customer_id AS _customerId',
'CustomerProduct.product_id AS _productId',
'customer.id AS _customer_id',
'customer.name AS _customer_name',
])
.from('customer_products AS CustomerProduct')
.innerJoin(
'customers AS customer',
'CustomerProduct.customer_id',
'customer.id',
)
.where(whereClause);
const result = knexnest(customerProducts).then(data => data);
return result;
I'll likely be creating several other queries with which I'll then use knexnest to create a nested model. Is there a way that I can pull knexnest(customerProducts).then(data => data); out to a different file so that I could just call it rather than adding that line with every query and also having to import knexnest into every file?

To achieve what you're trying to accomplish, the simplest way would be to export a function that returns the promise generated by knexnest. Then, import that function in the main file and call it. You can either use async/await syntactic sugar or a .then. It's important that you use one knex connection object throughout your application to leverage knex's connection pool. Therefore, we must pass it into your function so the other file can access the established connection object.
File 1
const Knex = require('knex');
// import the function getCustomerProducts using destructuring
const { getCustomerProducts } = require('<File 2's Path>');
// initialize knex & set up knex database connection
const knex = Knex({
client: 'postgres',
connection: process.env.DATABASE_URL
});
// pseudo-code - note: await can only be called within an async function
// pass knex connection object into function to leverage knex's connection pooling
const products = await getCustomerProducts(knex);
File 2
const knexnest = require('knexnest');
const getCustomerProducts = knex => {
const customerProducts = knex.select([
'CustomerProduct.id AS _id',
'CustomerProduct.price AS _price',
'CustomerProduct.active AS _active',
'CustomerProduct.customer_id AS _customerId',
'CustomerProduct.product_id AS _productId',
'customer.id AS _customer_id',
'customer.name AS _customer_name',
])
.from('customer_products AS CustomerProduct')
.innerJoin(
'customers AS customer',
'CustomerProduct.customer_id',
'customer.id',
)
.where(whereClause);
return knexnest(customerProducts);
};
// named export of the function getCustomerProducts
module.exports.getCustomerProducts = getCustomerProducts;

Related

Value (number) is different from the MongoDB to the react client async call

Problem:
An entire field of my MongoDB's collections' is not transmitted correctly from the db to the react client. For an exemple, the first element of this field is tweet_id: 1537466989966413825 in my DB. It becomes tweet_id: 1537466989966413800 when I fetch from my client.
My steps
I pull my data from the MongoDB using their App services' function like that:
exports = function(){
var collection = context.services.get("mongodb-atlas").db("meme_news").collection("news");
return collection.find({});
};
When I press the Run button of their built-in terminal, the correct data is displayed.
On my react's application, I perform the fetch like that:
useEffect(() => {
getData();
}, []);
const getData = async () => {
let getAllData = await user.functions.getAllData();
// all these data are wrong
let tweetId = getAllData
.map((ele) => {
return ele.tweet_id;
})
let tweetIdFirstEle = tweetId[0];
// this return 1537466989966413800
// It should return 1537466989966413825
};
Why is my async/await altering my Mongodb data? I have no idea what is going on here.

Delete a batch of docs using admin SDK and callable cloud function in most similar way to client SDK?

I have an array of docs ids that I want to delete in using a cloud function, my code looks like the following :
//If the user decieds on deleting his account forever we need to make sure we wont have any thing left inside of db after this !!
// incoming payload array of 3 docs
data = {array : ['a302-5e9c8ae97b3b','92c8d309-090d','a302-5e932c8ae97b3b']}
export const deleteClients = functions.https.onCall(async (data, context) => {
try {
// declare batch
const batch = db.batch();
// set
data.arr.forEach((doc: string) => {
batch.delete(db.collection('Data'), doc);
});
// commit
await batch.commit();
} catch (e) {
console.log(e);
}
return null;
});
I am getting a syntax error on batch.delete how to pass the right arguments in to the batch delete to reference that doc I want to submit for deletion before commit ?
Delete takes a single param, the doc ref of the doc to be deleted.
data.arr.forEach((docId: string) => {
batch.delete(doc(db, "Data", docId));
});
There are several errors in your code:
data.arr.forEach() cannot work wince your data object contains one element with the key array and not the key arr.
You are mixing up the syntax of the JS SDK v9 and the Admin SDK. See the write batch Admin SDK syntax here.
You need to send back some data to the client when all the asynchronous work is complete, to correctly terminate your CF.
You do return null; AFTER the try/catch block: this means that, in most of the cases, your Cloud Function will be terminated before asynchronous work is complete (see the link above)
So the following should do the trick (untested):
const db = admin.firestore();
const data = {array : ['a302-5e9c8ae97b3b','92c8d309-090d','a302-5e932c8ae97b3b']};
export const deleteClients = functions.https.onCall(async (data, context) => {
try {
const batch = db.batch();
const parentCollection = db.collection('Data')
data.array.forEach((docId) => {
batch.delete(parentCollection.doc(docId));
});
// commit
await batch.commit();
return {result: 'success'} // IMPORTANT, see explanations above
} catch (e) {
console.log(e);
// IMPORTANT See https://firebase.google.com/docs/functions/callable#handle_errors
}
});

Hooks.js running the db connection and results twice in sveltekit

I'm using sveltekit and trying to understand all the new features added after retiring Sapper. One of those new features is hooks.js which runs on the server and not accessible to the frontend. It makes dealing with db safe. So I created a connection to my mongodb to retrieve user's data before I use the db results in my getSession function. It works but I noticed that it access my database TWICE. Here is my hooks.js code:
import * as cookie from 'cookie';
import { connectToDatabase } from '$lib/mongodb.js';
export const handle = async ({event, resolve})=>{
const dbConnection = await connectToDatabase();
const db = dbConnection.db;
const userinfo = await db.collection('users').findOne({ username: "a" });
console.log("db user is :" , userinfo) //username : John
const response = await resolve(event)
response.headers.set(
'set-cookie', cookie.serialize("cookiewithjwt", "sticksafterrefresh")
)
return response
}
export const getSession = (event)=>{
return {
user : {
name : "whatever"
}
}
}
The console.log you see here returns the user data twice. One as soon as I fire up my app at localhost:3000 with npm run dev and then less than a second, it prints another console log with the same information
db user is : John
a second later without clicking on anything a second console.log prints
db user is : John
So my understanding from the sveltekit doc is that hooks.js runs every time SvelteKit receives a request. I removed all prerender and prefetch from my code. I made sure I only have the index.svelte in my app but still it prints twice. My connection code I copied from an online post has the following:
/**
* Global is used here to maintain a cached connection across hot reloads
* in development. This prevents connections growing exponentially
* during API Route usage.
*/
Here is my connection code:
import { MongoClient } from 'mongodb';
const mongoURI ="mongodb+srv://xxx:xxx#cluster0.qjeag.mongodb.net/xxxxdb?retryWrites=true&w=majority";
const mongoDB = "xxxxdb"
export const MONGODB_URI = mongoURI;
export const MONGODB_DB = mongoDB;
if (!MONGODB_URI) {
throw new Error('Please define the mongoURI property inside config/default.json');
}
if (!MONGODB_DB) {
throw new Error('Please define the mongoDB property inside config/default.json');
}
/**
* Global is used here to maintain a cached connection across hot reloads
* in development. This prevents connections growing exponentially
* during API Route usage.
*/
let cached = global.mongo;
if (!cached) {
cached = global.mongo = { conn: null, promise: null };
}
export const connectToDatabase = async() => {
if (cached.conn) {
return cached.conn;
}
if (!cached.promise) {
const opts = {
useNewUrlParser: true,
useUnifiedTopology: true
};
cached.promise = MongoClient.connect(MONGODB_URI).then((client) => {
return {
client,
db: client.db(MONGODB_DB)
};
});
}
cached.conn = await cached.promise;
return cached.conn;
So my question is : is hooks.js runs twice all the time, one time on the server and one time on the front? If not, then why the hooks.js running/printing twice the db results in my case?
Anyone?

How to properly reuse MongoDriver connection across NodeJS modules with ES6

First of all it is a similar question like How to properly reuse connection to Mongodb across NodeJs application and modules, but I guess due to the ES6 syntax it's still different.
So I decided to use MongoDriver and created a class for this like in SO answer: dbconnections.js
import { default as mongodb } from 'mongodb';
const MongoClient = mongodb.MongoClient;
const url = "myurl"
let _db;
export const connectToServer = async (callback) => {
try {
MongoClient.connect( url, { useNewUrlParser: true, useUnifiedTopology:true }, ( err, db ) => {
_db = db
return callback( err )
})
} catch (e) {
throw e
}
}
export const getDB = () => _db
export const disconnectDB = () => _db.close()
The problem with this module is, that due to the ES6 syntax is that I can't make like something this
import {getDB} from '../dbconnections.js'
const driverDB=getDB()
export const someFunction= async (req,res) => {
console.log(driverDB)
because I always get undefiened so I have to call my getter in every function.
So my question: how to create properly a structure for a MongoDriver handler and pass this to different modules with ES6?
Pass the client object into objects and functions that need it.
https://en.wikipedia.org/wiki/Dependency_injection

asnyc function for retrieving Google Secret Manager Variable returns promise pending

I am trying to switch my environment variables to Google Secrets Manager, but I am encountering a problem. I am running an expressjs api trying to establish a database connection. But whatever I try, it only returns Promise { <pending> } instead of waiting for the async function to finish. Any help is highly appreciated!
gcloud.js
const { SecretManagerServiceClient } = require('#google-cloud/secret-manager');
const client = new SecretManagerServiceClient();
async function getSecret(name) {
const name = `projects/PROJECT-ID/secrets/${name}/versions/latest`;
const [version] = await client.accessSecretVersion({
name: name,
});
const payload = version.payload.data.toString();
return payload;
}
module.exports.getSecret = getSecret;
config.js
const gcloud = require('./config/gcloud.js')
const config = {
authSecret: gcloud.getSecret(SECRET_NAME)
}
module.exports = config
You need to await the result of gcloud.getSecret:
// Not actually valid
const config = {
authSecret: await gcloud.getSecret(SECRET_NAME)
}
However, top-level await isn't widely supported yet, so you'll need to build the config inside a function: NodeJS Async / Await - Build configuration file with API call

Categories