Skip to content

Commit

Permalink
feat: update db with vercel postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzewei committed Mar 26, 2024
1 parent 06cba93 commit 20c0208
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 47 deletions.
6 changes: 3 additions & 3 deletions src/app/book/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ export default function Page({ params }: { params: { slug: string } }) {
const [open, setOpen] = useState(false);
const fetchBookChapters = async () => {
try {
const { data } = await fetch('http://localhost:3000/book/api/fetchChaptersByBookId?id=' + params.slug).then(res => res.json());
const { data } = await fetch('/book/api/fetchChaptersByBookId?id=' + params.slug).then(res => res.json());
setChapters(data);
} catch (err) {
console.log(err);
}
}
const fetchBook = async () => {
try {
const { data } = await fetch('http://localhost:3000/book/api/fetchBook?id=' + params.slug).then(res => res.json());
const { data } = await fetch('/book/api/fetchBook?id=' + params.slug).then(res => res.json());
setBook(data);
} catch (err) {
console.log(err);
}
}
const fetchCharacters = async () => {
try {
const { data } = await fetch('http://localhost:3000/book/api/fetchCharactersByBookId?id=' + params.slug).then(res => res.json());
const { data } = await fetch('/book/api/fetchCharactersByBookId?id=' + params.slug).then(res => res.json());
setCharacters(data);
} catch (err) {
console.log(err);
Expand Down
15 changes: 9 additions & 6 deletions src/app/book/api/fetchBook/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { connectDB } from "../utils";
import { connectPostgres } from "../utils";

export interface Book {
id: string,
Expand All @@ -16,17 +16,20 @@ export interface Book {
}

export async function GET(request: Request) {
const db = await connectDB();
const db = await connectPostgres();
const { searchParams } = new URL(request.url)
const id = searchParams.get('id');
if (db) {
try {
const result = await db?.get(`select * from book where id = "${id}"`, []);
db.close();
return Response.json({ data: result });
const { rows } = await db.query<Book>({
text: 'select * from book where id = $1',
values: [id]
});
db.end();
return Response.json({ data: rows[0] });
} catch (err) {
return Response.json({ data: err })
}
}
return Response.json({ data: [] })
return Response.json({ data: null })
}
4 changes: 2 additions & 2 deletions src/app/book/api/fetchBooks/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { connectDB, connectPostgres } from "../utils";
import { connectPostgres } from "../utils";
export interface Book {
id: string,
title: string,
Expand All @@ -20,7 +20,7 @@ export async function GET() {
const db = await connectPostgres();
if (db) {
try {
const { rows } = await db?.query<Book>({
const { rows } = await db.query<Book>({
text: 'select * from book'
});
db.end();
Expand Down
25 changes: 16 additions & 9 deletions src/app/book/api/fetchChaptersByBookId/route.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import { connectDB } from "../utils";
import { connectPostgres } from "../utils";

export interface Chapter {
id: string,
belongs_to: string,
chapter_num: number,
chapter_name: string,
content: string,
nft_ip_id: string
nft_ip_id: string,
image_url: string
}

export async function GET(request: Request) {
const { searchParams } = new URL(request.url)
const id = searchParams.get('id');
const db = await connectDB();
try {
const result = await db?.all(`select * from chapter where belongs_to = '${id}'`, []);
db?.close();
return Response.json({ data: result });
} catch (err) {
return Response.json({ data: err })
const db = await connectPostgres();
if (db) {
try {
const { rows } = await db.query<Chapter>({
text: 'select * from chapter where belongs_to = $1',
values: [id]
});
db.end();
return Response.json({ data: rows });
} catch (err) {
return Response.json({ data: err })
}
}
return Response.json({ data: [] })
}
22 changes: 14 additions & 8 deletions src/app/book/api/fetchCharactersByBookId/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { connectDB } from "../utils";
import { connectPostgres } from "../utils";

export interface Character {
id: string,
Expand All @@ -11,12 +11,18 @@ export interface Character {
export async function GET(request: Request) {
const { searchParams } = new URL(request.url)
const id = searchParams.get('id');
const db = await connectDB();
try {
const result = await db?.all(`select * from character where belongs_to = '${id}'`, []);
db?.close();
return Response.json({ data: result });
} catch (err) {
return Response.json({ data: err })
const db = await connectPostgres();
if (db) {
try {
const { rows } = await db.query<Character>({
text: 'select * from character where belongs_to = $1',
values: [id]
});
db.end();
return Response.json({ data: rows });
} catch (err) {
return Response.json({ data: err })
}
}
return Response.json({ data: [] })
}
18 changes: 0 additions & 18 deletions src/app/book/api/utils.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
import { open } from 'sqlite'
import { verbose } from "sqlite3";

import { createPool } from '@vercel/postgres';
export async function connectDB() {
const sqlite3 = verbose();
let DB = null;
try {
DB = await open({
filename: 'src/db/public_10book.db',
driver: sqlite3.Database
});
console.log('DB connection is ready');
}
catch (err) {
console.log(err)
}
return DB;
}

export async function connectPostgres() {
const db = createPool({
Expand Down
2 changes: 1 addition & 1 deletion src/db/scripts/createTableScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ create table if not exists ${DB_TABLE_NAME.CHAPTER}
(
id varchar PRIMARY KEY,
belongs_to varchar,
chapter_num intager,
chapter_num integer,
chapter_name varchar,
content varchar,
nft_ip_id varchar,
Expand Down

0 comments on commit 20c0208

Please sign in to comment.