Hello guys,
We will integrate DRIZZLE ORM and Argon2 in NodeJs project. First we have to understand, what is ORM. ORM refers to Object Relational Mapper, it is a technique that lets developers query and change data in a database using an object-oriented programming language. Instead of writing raw database code, you write code in the language you already know.
Argon2 is modern cryptographic hashing algorithem to secure passwords against hackers. It transforms passwords into fixed-length strings called hashes, acting as digital fingerprints without reveling the actual password.
In this post, we will first hash the user password and store in db, then verify the hashed password with original password to delete the data.
We will use PostgreSQL for our main Database.
Follow these steps for Integration:
Install the packages
npm init -y
npm i drizzle-orm pg dotenv argon2
npm i -D drizzle-kit
Create .env
DATABASE_URL=postgresql://username:password@localhost:5432/db_name
Create db.js
import dotenv from "dotenv";
import { drizzle } from 'drizzle-orm/node-postgres';
dotenv.config();
const db = drizzle(process.env.DATABASE_URL);
Create schema.js
import { integer, pgTable, varchar, text } from "drizzle-orm/pg-core";
export const usersTable = pgTable("users", {
id: serial("id").primaryKey(),
name: varchar("name", { length: 255 }).notNull(),
age: integer("age").notNull(),
email: varchar("email", { length: 255 }).notNull().unique(),
password: text("password").notNull()
});
Config drizzle.config.js
import dotenv from "dotenv";
import { defineConfig } from 'drizzle-kit';
dotenv.config();
export default defineConfig({
out: './drizzle',
schema: './src/db/schema.ts',
dialect: 'postgresql',
dbCredentials: {
url: process.env.DATABASE_URL!,
},
});
Applying changes to the Database
We can use
npx drizzle-kit push
command directly. But from this command, migration files will not create in out project. It will changes the database directly. This command use only in Developement.
For Production, we have two different commands, first one is
npx dirzzle-kit generate
that can create migration files. Second one is
npx drizzle-kit migrate
that can apply the changes to the database.
Drizzle configuration has been done. It's time to test the cod. Let's see what happen when we insert data using Drizzle ORM.
Create app.js
import {db} from "./config/db.js";
import {usersTable} from "./drizzle/schema.js";
import { eq } from "drizzle-orm";
import argon2 from "argon2";
const hashing = async (password) => {
return await argon2.hash(password);
}
async function verifyPassword(hashPassword, password){
return argon2.verify(hashPassword,password)
}
const user = {
name: "Ravi",
age: 27,
email: "ravikishan@gmail.com",
password: "ravikishan"
}
const main = async ()=>{
const {name, age, email, password} = user;
const hashPassword = await hashing(password);
const isVeriied = await verifyPassword(hashPassword, password);
const insertUser = await db.insert(usersTable).values({
name: name,
age: age,
email: email,
password: hashPassword
})
if(isVeriied){
await db.delete(usersTable).where(eq(usersTable.email,email));
}else{
console.log("verification failed");
}
}
main().catch((e)=>console.error(e));
Thank you for reading.
If you like the post, then hit like and do comment for any queries.
Top comments (0)