Skip to content

Commit

Permalink
Merge pull request #4 from zhangzewei/main
Browse files Browse the repository at this point in the history
feature: upgrade db with vercel postgres
  • Loading branch information
zhangzewei authored Mar 26, 2024
2 parents 71b2c56 + 20c0208 commit 274dbba
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,5 @@ next-env.d.ts

# misc
.DS_Store
*.pem
*.pem
.vercel
11 changes: 10 additions & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};
const nextConfig = {
async rewrites() {
return [
{
source: '/api/:path*',
destination: 'http://localhost:3000/api/:path*',
},
];
},
};

export default nextConfig;
162 changes: 159 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@story-protocol/core-sdk": "^0.0.1-beta-rc.10",
"@vercel/postgres": "^0.7.2",
"async": "^3.2.5",
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
Expand Down
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 })
}
12 changes: 7 additions & 5 deletions src/app/book/api/fetchBooks/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,
title: string,
Expand All @@ -17,12 +17,14 @@ export interface Book {


export async function GET() {
const db = await connectDB();
const db = await connectPostgres();
if (db) {
try {
const result: Book[] = await db?.all('select * from book', []);
db.close();
return Response.json({ data: result });
const { rows } = await db.query<Book>({
text: 'select * from book'
});
db.end();
return Response.json({ data: rows });
} catch (err) {
return Response.json({ data: err })
}
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: [] })
}
Loading

0 comments on commit 274dbba

Please sign in to comment.