|
| 1 | +## To install dependencies: |
| 2 | +```sh |
| 3 | +bun install |
| 4 | +``` |
| 5 | + |
| 6 | +## To run: |
| 7 | +```sh |
| 8 | +bun run dev |
| 9 | +``` |
| 10 | + |
| 11 | +open http://localhost:3000 |
| 12 | + |
| 13 | +## To install Drizzle kit |
| 14 | + |
| 15 | +```bash |
| 16 | +bun add -D drizzle-kit |
| 17 | +``` |
| 18 | + |
| 19 | +## To instal Postgres Driver |
| 20 | +```bash |
| 21 | +bun add drizzle-orm/pg |
| 22 | +``` |
| 23 | + |
| 24 | +```bash |
| 25 | +bun add -D drizzle-kit @types/pg |
| 26 | +``` |
| 27 | + |
| 28 | +## Create a drizzle.config.ts file and add this |
| 29 | +```bash |
| 30 | +import {defineConfig} from 'drizzle-kit' |
| 31 | + |
| 32 | +export default defineConfig({ |
| 33 | + schema:'./src/schema.ts', |
| 34 | + out:'./drizzle', |
| 35 | + dialect:'postgresql', |
| 36 | + dbCredentials:{ |
| 37 | + host:"localhost", |
| 38 | + port: 5433, |
| 39 | + user: "postgres", |
| 40 | + password: "password", |
| 41 | + database:"hono" |
| 42 | + } |
| 43 | +}) |
| 44 | +``` |
| 45 | +## Create a migrate.ts file and add this |
| 46 | +```bash |
| 47 | + |
| 48 | +import { client, db } from "../client"; |
| 49 | +import {migrate} from 'drizzle-orm/node-postgres/migrator' |
| 50 | + |
| 51 | + |
| 52 | +async function Migrate(){ |
| 53 | +await migrate(db,{migrationsFolder:'./drizzle'}); |
| 54 | + |
| 55 | + await client.end(); |
| 56 | +} |
| 57 | + |
| 58 | +Migrate(); |
| 59 | +``` |
| 60 | + |
| 61 | +## Create a client.ts file and add this |
| 62 | +```bash |
| 63 | +import { Client } from "pg"; |
| 64 | +import { drizzle } from "drizzle-orm/node-postgres"; |
| 65 | +import * as schema from './src/schema'; |
| 66 | + |
| 67 | +export const client = new Client({ |
| 68 | + host: "localhost", |
| 69 | + port: 5433, |
| 70 | + user: "postgres", |
| 71 | + password: "R3v@y2002", |
| 72 | + database: "hono", |
| 73 | +}); |
| 74 | + |
| 75 | +async function connectClient() { |
| 76 | + try { |
| 77 | + await client.connect(); |
| 78 | + console.log('Connected to PostgreSQL successfully'); |
| 79 | + } catch (err) { |
| 80 | + console.error('Failed to connect to PostgreSQL', err); |
| 81 | + process.exit(1); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +connectClient(); |
| 86 | + |
| 87 | +export const db = drizzle(client, { schema }); |
| 88 | + |
| 89 | +process.on('exit', async () => { |
| 90 | + await client.end(); |
| 91 | + console.log('Disconnected from PostgreSQL'); |
| 92 | +}); |
| 93 | +``` |
| 94 | +
|
| 95 | +## Generate Migration |
| 96 | +```bash |
| 97 | +bunx drizzle-kit generate |
| 98 | +``` |
| 99 | +
|
| 100 | +## To Run Migration: |
| 101 | +```bash |
| 102 | +bunx src/migrate.ts |
| 103 | +``` |
| 104 | +## To View Database |
| 105 | +```bash |
| 106 | +drizzle-kit studio |
| 107 | +``` |
| 108 | +## OR |
| 109 | +```bash |
| 110 | +drizzle-kit studio --port 3000 |
| 111 | +``` |
| 112 | +
|
| 113 | +
|
| 114 | +
|
| 115 | +
|
0 commit comments