Skip to content

Revaycolizer/hono-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

8## TO CREATE A FRESH PROJECT USING NODE

FOLLOW BELOW INSTRUCTIONS

npm create hono@latest

Install bcrypt for password hashing and comparison

npm install bcrypt
npm install --save @types/bcrypt

To install Drizzle kit

npm install --save  drizzle-kit

To instal Postgres Driver

npm install drizzle-orm pg
npm install --save drizzle-kit @types/pg

Create a drizzle.config.ts file and add this

import {defineConfig} from 'drizzle-kit'

export default defineConfig({
    schema:'./src/schema.ts',
    out:'./drizzle',
    dialect:'postgresql',
    dbCredentials:{
        host:"localhost",
        port: 5433,
        user: "postgres",
        password: "password",
        database:"hono"
    }
})

Create a migrate.ts file inside src folder and add this

import { client, db } from "../client";
import {migrate} from 'drizzle-orm/node-postgres/migrator'


async function Migrate(){
await migrate(db,{migrationsFolder:'./drizzle'});

 await client.end();
}

Migrate();

Create a client.ts file and add this

import { Client } from "pg";
import { drizzle } from "drizzle-orm/node-postgres";
import * as schema from './src/schema';

export const client = new Client({
  host: "localhost",
  port: 5433,
  user: "postgres",
  password: "password",
  database: "hono",
});

async function connectClient() {
  try {
    await client.connect();
    console.log('Connected to PostgreSQL successfully');
  } catch (err) {
    console.error('Failed to connect to PostgreSQL', err);
    process.exit(1); 
  }
}

connectClient();

export const db = drizzle(client, { schema });

process.on('exit', async () => {
  await client.end();
  console.log('Disconnected from PostgreSQL');
});

Generate Migration

bunx drizzle-kit generate

To Run Migration:

npx tax src/migrate.ts

To View Database

npx drizzle-kit studio

OR

npx drizzle-kit studio --port 3000

To run:

npm run dev

open http://localhost:3000

INCASE YOU CLONED THE REPO USE THIS SCRIPTS ONLY

To install dependencies:

bun install

Generate Migration

bunx drizzle-kit generate

To Run Migration:

bunx tsx src/migrate.ts

To View Database

bunx drizzle-kit studio

OR

bunx drizzle-kit studio --port 3000

To run:

bun run dev

open http://localhost:3000

Incase you want to start a new project from Scratch

Proceed with the Instructions Below

Create a new Project

bun create hono@latest

To install Drizzle kit

bun add -D drizzle-kit

To instal Postgres Driver

bun add drizzle-orm pg
bun add -D drizzle-kit @types/pg

Create a drizzle.config.ts file and add this

import {defineConfig} from 'drizzle-kit'

export default defineConfig({
    schema:'./src/schema.ts',
    out:'./drizzle',
    dialect:'postgresql',
    dbCredentials:{
        host:"localhost",
        port: 5433,
        user: "postgres",
        password: "password",
        database:"hono"
    }
})

Create a migrate.ts file inside src folder and add this

import { client, db } from "../client";
import {migrate} from 'drizzle-orm/node-postgres/migrator'


async function Migrate(){
await migrate(db,{migrationsFolder:'./drizzle'});

 await client.end();
}

Migrate();

Create a client.ts file and add this

import { Client } from "pg";
import { drizzle } from "drizzle-orm/node-postgres";
import * as schema from './src/schema';

export const client = new Client({
  host: "localhost",
  port: 5433,
  user: "postgres",
  password: "password",
  database: "hono",
});

async function connectClient() {
  try {
    await client.connect();
    console.log('Connected to PostgreSQL successfully');
  } catch (err) {
    console.error('Failed to connect to PostgreSQL', err);
    process.exit(1); 
  }
}

connectClient();

export const db = drizzle(client, { schema });

process.on('exit', async () => {
  await client.end();
  console.log('Disconnected from PostgreSQL');
});

Generate Migration

bunx drizzle-kit generate

To Run Migration:

bunx tax src/migrate.ts

To View Database

drizzle-kit studio

OR

drizzle-kit studio --port 3000

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published