I want to use a secrets folder to store my bigquery api client secrets json and use BigQuery client library for node to make queries to the database. But in the documentations, it shows way to load credentials from a json file and not directly.
I am using typescript to query a table and append row, In NEXT js, I am a littlebit confused about how to access the path of a secrets folder that's not going to be exposed to client side and stays in for my middleware.
Here's the function for the same:
import type { NextApiHandler } from 'next';
import axios from 'axios';
import { BigQuery } from '@google-cloud/bigquery';
import bqSecrets from '../../../bigquery/keys.json';
const options = {
keyFilename: '../../../secrets/keys.json',
projectId: 'XXXXXXXXXXXXXXXXXXXXXX',
};
const bigquery = new BigQuery(options);
const submitUserData: NextApiHandler = async (request, response) => {
const { geoData, googleData, post, userRole } = request.body;
if (!post) response.json({ error: false, msg: 'Not Required' });
else {
delete googleData.isAuthenticated;
try {
const rep = await bigquery
.dataset('stackconnect')
.table('googleoAuth')
.insert([requestPayload]);
console.log(await rep);
response.json({ error: false, msg: 'Success' });
} catch (e) {
console.log('Error = ', e);
response.json({ error: true, msg: 'Error' });
}
}
};
But this throws an error saying: File not Found. Can someone help me identify about how to either locate the file in NEXT or how can I directly pass the JSON data using BQ Node Client?
Additionally, I want to know in NEXT, which is the ideal place to store secrets that are not exposed to client side?
After digging into the source code, I realized that there's an attribute called credentials, and so I was able to make it work by doing the following modification:
import bqSecrets from '../../../bigquery/keys.json';
const options = {
credentials: bqSecrets,
projectId: 'project_id',
};
const bigquery = new BigQuery(options);
I hope this helps anyone who's looking to directly inject the json credentials to the BigQuery Node Client.
Additionally, I am still not very sure about the file structure of NEXT as I still don't know if any other assets than those placed under public folder are exposed to client side or not.