Skip to content

Commit 56b9f2f

Browse files
committed
Initialized Repo with Authentication
Initial commit new file: .gitignore new file: README.md new file: bun.lockb new file: client.ts new file: drizzle.config.ts new file: drizzle/0000_luxuriant_warpath.sql new file: drizzle/0001_tough_pandemic.sql new file: drizzle/meta/0000_snapshot.json new file: drizzle/meta/0001_snapshot.json new file: drizzle/meta/_journal.json new file: package-lock.json new file: package.json new file: src/index.ts new file: src/migrate.ts new file: src/schema.ts new file: tsconfig.json
0 parents  commit 56b9f2f

16 files changed

+1294
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# deps
2+
node_modules/
3+
4+
.env
5+

README.md

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+

bun.lockb

38.6 KB
Binary file not shown.

client.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Client } from "pg";
2+
import { drizzle } from "drizzle-orm/node-postgres";
3+
import * as schema from './src/schema';
4+
5+
export const client = new Client({
6+
host: "localhost",
7+
port: 5433,
8+
user: "postgres",
9+
password: "password",
10+
database: "hono",
11+
});
12+
13+
async function connectClient() {
14+
try {
15+
await client.connect();
16+
console.log('Connected to PostgreSQL successfully');
17+
} catch (err) {
18+
console.error('Failed to connect to PostgreSQL', err);
19+
process.exit(1);
20+
}
21+
}
22+
23+
24+
connectClient();
25+
26+
27+
export const db = drizzle(client, { schema });
28+
29+
30+
process.on('exit', async () => {
31+
await client.end();
32+
console.log('Disconnected from PostgreSQL');
33+
});

drizzle.config.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {defineConfig} from 'drizzle-kit'
2+
3+
export default defineConfig({
4+
schema:'./src/schema.ts',
5+
out:'./drizzle',
6+
dialect:'postgresql',
7+
dbCredentials:{
8+
host:"localhost",
9+
port: 5433,
10+
user: "postgres",
11+
password: "R3v@y2002",
12+
database:"hono"
13+
}
14+
})

drizzle/0000_luxuriant_warpath.sql

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CREATE TABLE IF NOT EXISTS "product" (
2+
"id" serial PRIMARY KEY NOT NULL,
3+
"name" text,
4+
"price" bigint,
5+
"src" text,
6+
"userId" serial NOT NULL
7+
);
8+
--> statement-breakpoint
9+
CREATE TABLE IF NOT EXISTS "user" (
10+
"id" serial PRIMARY KEY NOT NULL,
11+
"name" text,
12+
"password" text
13+
);
14+
--> statement-breakpoint
15+
DO $$ BEGIN
16+
ALTER TABLE "product" ADD CONSTRAINT "product_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
17+
EXCEPTION
18+
WHEN duplicate_object THEN null;
19+
END $$;

drizzle/0001_tough_pandemic.sql

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE "user" ALTER COLUMN "password" SET DATA TYPE varchar;

drizzle/meta/0000_snapshot.json

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"id": "be4d4240-c3c0-4726-8483-68d51fef6e30",
3+
"prevId": "00000000-0000-0000-0000-000000000000",
4+
"version": "7",
5+
"dialect": "postgresql",
6+
"tables": {
7+
"public.product": {
8+
"name": "product",
9+
"schema": "",
10+
"columns": {
11+
"id": {
12+
"name": "id",
13+
"type": "serial",
14+
"primaryKey": true,
15+
"notNull": true
16+
},
17+
"name": {
18+
"name": "name",
19+
"type": "text",
20+
"primaryKey": false,
21+
"notNull": false
22+
},
23+
"price": {
24+
"name": "price",
25+
"type": "bigint",
26+
"primaryKey": false,
27+
"notNull": false
28+
},
29+
"src": {
30+
"name": "src",
31+
"type": "text",
32+
"primaryKey": false,
33+
"notNull": false
34+
},
35+
"userId": {
36+
"name": "userId",
37+
"type": "serial",
38+
"primaryKey": false,
39+
"notNull": true
40+
}
41+
},
42+
"indexes": {},
43+
"foreignKeys": {
44+
"product_userId_user_id_fk": {
45+
"name": "product_userId_user_id_fk",
46+
"tableFrom": "product",
47+
"tableTo": "user",
48+
"columnsFrom": [
49+
"userId"
50+
],
51+
"columnsTo": [
52+
"id"
53+
],
54+
"onDelete": "no action",
55+
"onUpdate": "no action"
56+
}
57+
},
58+
"compositePrimaryKeys": {},
59+
"uniqueConstraints": {}
60+
},
61+
"public.user": {
62+
"name": "user",
63+
"schema": "",
64+
"columns": {
65+
"id": {
66+
"name": "id",
67+
"type": "serial",
68+
"primaryKey": true,
69+
"notNull": true
70+
},
71+
"name": {
72+
"name": "name",
73+
"type": "text",
74+
"primaryKey": false,
75+
"notNull": false
76+
},
77+
"password": {
78+
"name": "password",
79+
"type": "text",
80+
"primaryKey": false,
81+
"notNull": false
82+
}
83+
},
84+
"indexes": {},
85+
"foreignKeys": {},
86+
"compositePrimaryKeys": {},
87+
"uniqueConstraints": {}
88+
}
89+
},
90+
"enums": {},
91+
"schemas": {},
92+
"sequences": {},
93+
"_meta": {
94+
"columns": {},
95+
"schemas": {},
96+
"tables": {}
97+
}
98+
}

drizzle/meta/0001_snapshot.json

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"id": "dd7f7953-11e6-4253-ba1b-b9db32381fc9",
3+
"prevId": "be4d4240-c3c0-4726-8483-68d51fef6e30",
4+
"version": "7",
5+
"dialect": "postgresql",
6+
"tables": {
7+
"public.product": {
8+
"name": "product",
9+
"schema": "",
10+
"columns": {
11+
"id": {
12+
"name": "id",
13+
"type": "serial",
14+
"primaryKey": true,
15+
"notNull": true
16+
},
17+
"name": {
18+
"name": "name",
19+
"type": "text",
20+
"primaryKey": false,
21+
"notNull": false
22+
},
23+
"price": {
24+
"name": "price",
25+
"type": "bigint",
26+
"primaryKey": false,
27+
"notNull": false
28+
},
29+
"src": {
30+
"name": "src",
31+
"type": "text",
32+
"primaryKey": false,
33+
"notNull": false
34+
},
35+
"userId": {
36+
"name": "userId",
37+
"type": "serial",
38+
"primaryKey": false,
39+
"notNull": true
40+
}
41+
},
42+
"indexes": {},
43+
"foreignKeys": {
44+
"product_userId_user_id_fk": {
45+
"name": "product_userId_user_id_fk",
46+
"tableFrom": "product",
47+
"tableTo": "user",
48+
"columnsFrom": [
49+
"userId"
50+
],
51+
"columnsTo": [
52+
"id"
53+
],
54+
"onDelete": "no action",
55+
"onUpdate": "no action"
56+
}
57+
},
58+
"compositePrimaryKeys": {},
59+
"uniqueConstraints": {}
60+
},
61+
"public.user": {
62+
"name": "user",
63+
"schema": "",
64+
"columns": {
65+
"id": {
66+
"name": "id",
67+
"type": "serial",
68+
"primaryKey": true,
69+
"notNull": true
70+
},
71+
"name": {
72+
"name": "name",
73+
"type": "text",
74+
"primaryKey": false,
75+
"notNull": false
76+
},
77+
"password": {
78+
"name": "password",
79+
"type": "varchar",
80+
"primaryKey": false,
81+
"notNull": false
82+
}
83+
},
84+
"indexes": {},
85+
"foreignKeys": {},
86+
"compositePrimaryKeys": {},
87+
"uniqueConstraints": {}
88+
}
89+
},
90+
"enums": {},
91+
"schemas": {},
92+
"sequences": {},
93+
"_meta": {
94+
"columns": {},
95+
"schemas": {},
96+
"tables": {}
97+
}
98+
}

0 commit comments

Comments
 (0)