[--BEGINNER--]how to clone sensitive info like password?
Answered
Asian black bear posted this in #help-forum
Asian black bearOP
Im exporting
when I imported it I found it very hard to store db on any other variable...
but
cloned it, i can see
but
how to store
const { Client } = require('pg');
const db = new Client({
user: 'user1',
host: 'localhost',
database: 'dbName',
password: "p@ss",
port: 1234,
});
export default db ;
when I imported it I found it very hard to store db on any other variable...
const client = db
doesnt work , client is always nullbut
const client = new Client({ ...db });
cloned it, i can see
client.user
in the consle correctlybut
password
is null ... how to store
db
on client
correctly ?70 Replies
@Asian black bear Im exporting
js
const { Client } = require('pg');
const db = new Client({
user: 'user1',
host: 'localhost',
database: 'dbName',
password: "p@ss",
port: 1234,
});
export default db ;
when I imported it I found it very hard to store db on any other variable...
`const client = db` doesnt work , client is always null
but
`const client = new Client({ ...db });`
cloned it, i can see `client.user` in the consle correctly
but `password` is null ...
how to store `db` on `client` correctly ?
instead of writing plain SQL you should use an ORM like drizzle. From there it's pretty easy to init your database like you want to:
// Make sure to install the 'pg' package
import { drizzle } from 'drizzle-orm/node-postgres';
const db = drizzle(process.env.DATABASE_URL);
const result = await db.execute('select 1');
@James4u by `password` which password did you mean?
Asian black bearOP
the one in db > password: "p@ss",
@Asian black bear the one in db > password: "p@ss",
the password that you need to connect your DB?
@James4u the password that you need to connect your DB?
Asian black bearOP
yes!
@Asian black bear yes!
why would you need to clone that password?
Asian black bearOP
i have console log, i logged user on
when I tried the same with Password
db logged it correctly
client logged null
db
and client
they both logged the write user name when I tried the same with Password
db logged it correctly
client logged null
@James4u why would you need to clone that password?
Asian black bearOP
im just following a tutorial .. im not sure why they did that
user would be public but passworld is a private one
@Asian black bear im just following a tutorial .. im not sure why they did that
don't just follow - unless you are not sure what you are gonna do
there is nothing I can assume you are gonna do with the password
Asian black bearOP
Just to connect to the database
i tried client.connect(); it failed
i tried client.connect(); it failed
password was not a string, after looking into it, it turned out to be null
const db = new Client({
user: 'user1',
host: 'localhost',
database: 'dbName',
password: "p@ss",
port: 1234,
});
do you really create db instance in this way or do you use env variables?
@James4u const db = new Client({
user: 'user1',
host: 'localhost',
database: 'dbName',
password: "p@ss",
port: 1234,
});
Asian black bearOP
its exported from another file
then imported in the new file
import { Client } from 'pg';
const db = new Client({
user: 'user1',
host: 'localhost',
database: 'dbName',
password: "p@ss",
port: 1234,
});
export default db ;
then imported in the new file
trying to clone it on
const client
then connect to the databasedo you have a string or do you import from env?
@James4u do you have a string or do you import from env?
Asian black bearOP
oh just a string
@B33fb0n3 instead of writing plain SQL you should use an ORM like drizzle. From there it's pretty easy to init your database like you want to:
tsx
// Make sure to install the 'pg' package
import { drizzle } from 'drizzle-orm/node-postgres';
const db = drizzle(process.env.DATABASE_URL);
const result = await db.execute('select 1');
Asian black bearOP
I'll look into it, but Im just learning next and react for the first time, i just need to learn the basics
@Asian black bear I'll look into it, but Im just learning next and react for the first time, i just need to learn the basics
Than you should start by learning SQL
@gin wdym by "clone"?
Asian black bearOP
im doing the tutorial for nextJS https://nextjs.org/learn/dashboard-app/setting-up-your-database
cool
Asian black bearOP
there is a file the seeds the database just to so we can have data to work with through out the tutorial, appearntly pg has been updated so the files are no longer working
@Asian black bear im doing the tutorial for nextJS https://nextjs.org/learn/dashboard-app/setting-up-your-database
I don't see anything there that says "clone the password". Instead I see a lot of using env files, that you don't have 🤔
@Asian black bear there is a file the seeds the database just to so we can have data to work with through out the tutorial, appearntly pg has been updated so the files are no longer working
seeds the databse? do u mean filling out the config for the db object?
Asian black bearOP
original code was
after importing
but that is no longer working as client is always undefined
after importing
db
const client = await db.connect();
but that is no longer working as client is always undefined
why is it always undefined?
Asian black bearOP
so i kept trying to figure out why and what has changed so i can update my code
do u get any errors
@gin do u get any errors
Asian black bearOP
just undefined when u call
client.query();
I get error yes .. it saied somethin bout calling query
from undefined@B33fb0n3 I don't see anything there that says "clone the password". Instead I see a lot of using env files, that you don't have 🤔
Asian black bearOP
the file has
const client = await db.connect();
that is no longer working, i assume it clones db but I can be wrongit doesnt clone anything, the word clone is not correct here
db.connect just returns an instance of the db after connect has finishede
u need to check the documentation of pg
there we go
check this
everything u need is on this page
and store everything like db logins and else in env files
everything that is dynamic and need to be protected
so in your case, u will have a object in a singleton that always returns the client after client.connect has finished
@Asian black bear the file has `const client = await db.connect();` that is no longer working, i assume it clones db but I can be wrong
That’s just creates a connection to the database with the preconfigured object (db) that contains the credentials
@B33fb0n3 That’s just creates a connection to the database with the preconfigured object (db) that contains the credentials
Asian black bearOP
yes i get that, but its no longer working for some reason
@Asian black bear yes i get that, but its no longer working for some reason
do what i told u above
u are probably not calling client.connect in your exported db const
@gin and store everything like db logins and else in env files
Asian black bearOP
oh yea im trying to understand how do I import data from env file to the script later
its everything automatic
just create a .env file
Asian black bearOP
where ?
in your root
Asian black bearOP
ok
check the tutorial
there should be a section that documents that i think
@gin check the tutorial
Asian black bearOP
ohh , im hosting postgress locally, im not using that Vercel's
its not practicle to let other ppl handle ur data anyway
check this and u are good
also i recommend always checking the docs correctly otherwise u will waste a lot of time understanding things
@Asian black bear It seems like you are new in the whole web developement thing. In developement you can't just start by watching a youtube video or ask other people and follow every step the guy in the video does or someone said to you. That works, until you get an error. So start by learning the basics and then one after another. Right now it seems like you are learning React, Nextjs, Typescript, (Libraries), JSX, (Javascript), CSS, HTML.
That's way to much to start. So, start with the basics: Javascript, CSS, HTML. When you know them go in the advanced direction: JSX, React, important Libraries. And then go in the OP stuff like: NextJs and Typescript.
I learned all these stuff from this guy: [Codevolution](https://www.youtube.com/@Codevolution)
Very much recommended.
For the beginning I recommend you, you using his crash course, for the topics I wrote down: https://youtu.be/XIOLqoPHCJ4?feature=shared
Please do it, else you won't make it that long in programming
After this playlist you know the basics of webdevelopement and can start big projects like a ecommerce store with a db connenction
Enjoy the developement journey and best of luck ^^
That's way to much to start. So, start with the basics: Javascript, CSS, HTML. When you know them go in the advanced direction: JSX, React, important Libraries. And then go in the OP stuff like: NextJs and Typescript.
I learned all these stuff from this guy: [Codevolution](https://www.youtube.com/@Codevolution)
Very much recommended.
For the beginning I recommend you, you using his crash course, for the topics I wrote down: https://youtu.be/XIOLqoPHCJ4?feature=shared
Please do it, else you won't make it that long in programming
After this playlist you know the basics of webdevelopement and can start big projects like a ecommerce store with a db connenction
Enjoy the developement journey and best of luck ^^
@B33fb0n3 <@999484225348698233> It seems like you are new in the whole web developement thing. In developement you can't just start by watching a youtube video or ask other people and follow every step the guy in the video does or someone said to you. That works, until you get an error. So start by learning the basics and then one after another. Right now it seems like you are learning React, Nextjs, Typescript, (Libraries), JSX, (Javascript), CSS, HTML.
That's way to much to start. So, start with the basics: Javascript, CSS, HTML. When you know them go in the advanced direction: JSX, React, important Libraries. And then go in the OP stuff like: NextJs and Typescript.
I learned all these stuff from this guy: [Codevolution](https://www.youtube.com/@Codevolution)
Very much recommended.
For the beginning I recommend you, you using his crash course, for the topics I wrote down: https://youtu.be/XIOLqoPHCJ4?feature=shared
Please do it, else you won't make it that long in programming
After this playlist you know the basics of webdevelopement and can start big projects like a ecommerce store with a db connenction
Enjoy the developement journey and best of luck ^^
Asian black bearOP
I have been using web dev for4 years, I just stopped the past two years because I was working on game dev, now Im back, im just new js frameworks
I've finished a react crash course
@gin https://nextjs.org/docs/app/building-your-application/configuring/environment-variables
Asian black bearOP
thanks, I'm not really stuck, i was just wondering after cloing the whole db why password was still null
const client = new Client({ ...db });
user was cloned correctly but not the password
I guess i could use db to query instead of client but I was just trying to figure out how things work under the hood
I guess i could use db to query instead of client but I was just trying to figure out how things work under the hood