From 6a7f0a8515b8595487cf200a4ddcd5b552a7b439 Mon Sep 17 00:00:00 2001 From: "Zewei.Zhang" Date: Tue, 26 Mar 2024 15:46:36 +0800 Subject: [PATCH 1/3] feat: use vercel postgres to book table --- .gitignore | 3 +- package-lock.json | 162 ++++++++++++++++++++++++++- package.json | 1 + src/app/book/api/fetchBooks/route.ts | 12 +- src/app/book/api/utils.ts | 14 +++ src/db/scripts/createTableScript.js | 3 +- 6 files changed, 185 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index f8b0753..91a25c3 100644 --- a/.gitignore +++ b/.gitignore @@ -135,4 +135,5 @@ next-env.d.ts # misc .DS_Store -*.pem \ No newline at end of file +*.pem +.vercel diff --git a/package-lock.json b/package-lock.json index 3ce9b10..a9b05e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0", "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", @@ -258,6 +259,14 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@neondatabase/serverless": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/@neondatabase/serverless/-/serverless-0.7.2.tgz", + "integrity": "sha512-wU3WA2uTyNO7wjPs3Mg0G01jztAxUxzd9/mskMmtPwPTjf7JKWi9AW5/puOGXLxmZ9PVgRFeBVRVYq5nBPhsCg==", + "dependencies": { + "@types/pg": "8.6.6" + } + }, "node_modules/@next/env": { "version": "14.1.3", "resolved": "https://registry.npmjs.org/@next/env/-/env-14.1.3.tgz", @@ -595,11 +604,20 @@ "version": "20.11.27", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.27.tgz", "integrity": "sha512-qyUZfMnCg1KEz57r7pzFtSGt49f6RPkPBis3Vo4PbS7roQEDn22hiHzl/Lo1q4i4hDEgBJmBF/NTNg2XR0HbFg==", - "dev": true, "dependencies": { "undici-types": "~5.26.4" } }, + "node_modules/@types/pg": { + "version": "8.6.6", + "resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.6.6.tgz", + "integrity": "sha512-O2xNmXebtwVekJDD+02udOncjVcMZQuTEQEMpKJ0ZRf5E7/9JJX3izhKUcUifBkyKpljyUM6BTgy2trmviKlpw==", + "dependencies": { + "@types/node": "*", + "pg-protocol": "*", + "pg-types": "^2.2.0" + } + }, "node_modules/@types/prop-types": { "version": "15.7.11", "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.11.tgz", @@ -765,6 +783,40 @@ "integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==", "dev": true }, + "node_modules/@vercel/postgres": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/@vercel/postgres/-/postgres-0.7.2.tgz", + "integrity": "sha512-IqR/ZAvoPGcPaXl9eWWB5KaA+w/81RzZa/18P4izQRHpNBkTGt9HwGfYi9+wut5UgxNq4QSX9A7HIQR6QDvX2Q==", + "dependencies": { + "@neondatabase/serverless": "0.7.2", + "bufferutil": "4.0.8", + "utf-8-validate": "6.0.3", + "ws": "8.14.2" + }, + "engines": { + "node": ">=14.6" + } + }, + "node_modules/@vercel/postgres/node_modules/ws": { + "version": "8.14.2", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.14.2.tgz", + "integrity": "sha512-wEBG1ftX4jcglPxgFCMJmZ2PLtSbJ2Peg6TmpJFTbe9GZYOQCDPdMYu/Tm0/bGZkw8paZnJY45J4K2PZrLYq8g==", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", @@ -2632,6 +2684,18 @@ "ieee754": "^1.1.13" } }, + "node_modules/bufferutil": { + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.8.tgz", + "integrity": "sha512-4T53u4PdgsXqKaIctwF8ifXlRTTmEPJ8iEPWFdGZvcf7sbwYo6FKFEX9eNNAnzFZ7EzJAQ3CJeOtCRA4rDp7Pw==", + "hasInstallScript": true, + "dependencies": { + "node-gyp-build": "^4.3.0" + }, + "engines": { + "node": ">=6.14.2" + } + }, "node_modules/busboy": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", @@ -6323,6 +6387,16 @@ "node": ">= 10.12.0" } }, + "node_modules/node-gyp-build": { + "version": "4.8.0", + "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.0.tgz", + "integrity": "sha512-u6fs2AEUljNho3EYTJNBfImO5QTo/J/1Etd+NVdCj7qWKUSN/bSLkZwhDv7I+w/MSC6qJ4cknepkAYykDdK8og==", + "bin": { + "node-gyp-build": "bin.js", + "node-gyp-build-optional": "optional.js", + "node-gyp-build-test": "build-test.js" + } + }, "node_modules/node-gyp/node_modules/glob": { "version": "7.2.3", "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", @@ -6839,6 +6913,34 @@ "node": ">=8" } }, + "node_modules/pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/pg-protocol": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.6.0.tgz", + "integrity": "sha512-M+PDm637OY5WM307051+bsDia5Xej6d9IR4GwJse1qA1DIhiKlksvrneZOYQq42OM+spubpcNYEo2FcKQrDk+Q==" + }, + "node_modules/pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "dependencies": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + }, + "engines": { + "node": ">=4" + } + }, "node_modules/picocolors": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz", @@ -7041,6 +7143,41 @@ "integrity": "sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==", "dev": true }, + "node_modules/postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==", + "engines": { + "node": ">=4" + } + }, + "node_modules/postgres-bytea": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", + "integrity": "sha512-xy3pmLuQqRBZBXDULy7KbaitYqLcmxigw14Q5sj8QBVLqEwXfeybIKVWiqAXTlcvdvb0+xkOtDbfQMOf4lST1w==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/postgres-date": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", + "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "dependencies": { + "xtend": "^4.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/prebuild-install": { "version": "7.1.2", "resolved": "https://registry.npmjs.org/prebuild-install/-/prebuild-install-7.1.2.tgz", @@ -8771,8 +8908,7 @@ "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", - "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", - "dev": true + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==" }, "node_modules/union-value": { "version": "1.0.1", @@ -8936,6 +9072,18 @@ "node": ">=0.10.0" } }, + "node_modules/utf-8-validate": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/utf-8-validate/-/utf-8-validate-6.0.3.tgz", + "integrity": "sha512-uIuGf9TWQ/y+0Lp+KGZCMuJWc3N9BHA+l/UmHd/oUHwJJDeysyTRxNQVkbzsIWfGFbRe3OcgML/i0mvVRPOyDA==", + "hasInstallScript": true, + "dependencies": { + "node-gyp-build": "^4.3.0" + }, + "engines": { + "node": ">=6.14.2" + } + }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", @@ -9223,6 +9371,14 @@ } } }, + "node_modules/xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", + "engines": { + "node": ">=0.4" + } + }, "node_modules/yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", diff --git a/package.json b/package.json index 3928a1e..d1d28e4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/app/book/api/fetchBooks/route.ts b/src/app/book/api/fetchBooks/route.ts index 9067c27..6efc738 100644 --- a/src/app/book/api/fetchBooks/route.ts +++ b/src/app/book/api/fetchBooks/route.ts @@ -1,4 +1,4 @@ -import { connectDB } from "../utils"; +import { connectDB, connectPostgres } from "../utils"; export interface Book { id: string, title: string, @@ -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({ + text: 'select * from book' + }); + db.end(); + return Response.json({ data: rows }); } catch (err) { return Response.json({ data: err }) } diff --git a/src/app/book/api/utils.ts b/src/app/book/api/utils.ts index 3c22d18..84ca297 100644 --- a/src/app/book/api/utils.ts +++ b/src/app/book/api/utils.ts @@ -1,5 +1,7 @@ import { open } from 'sqlite' import { verbose } from "sqlite3"; + +import { createPool } from '@vercel/postgres'; export async function connectDB() { const sqlite3 = verbose(); let DB = null; @@ -14,4 +16,16 @@ export async function connectDB() { console.log(err) } return DB; +} + +export async function connectPostgres() { + const db = createPool({ + connectionString: `postgres://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@${process.env.POSTGRES_HOST}:5432/${process.env.POSTGRES_DATABASE}`, + }); + try { + await db.connect(); + return db; + } catch (error) { + console.error('Error executing query', error); + } } \ No newline at end of file diff --git a/src/db/scripts/createTableScript.js b/src/db/scripts/createTableScript.js index 1e665f3..75ad911 100644 --- a/src/db/scripts/createTableScript.js +++ b/src/db/scripts/createTableScript.js @@ -14,7 +14,8 @@ create table if not exists ${DB_TABLE_NAME.BOOK} total_words integer, source_url varchar, nft_ip_id varchar, - genre varchar + genre varchar, + img_url varchar ) `; From 06cba937f3e8a65c48f0d4326447335f250742c0 Mon Sep 17 00:00:00 2001 From: "Zewei.Zhang" Date: Tue, 26 Mar 2024 16:07:21 +0800 Subject: [PATCH 2/3] feat: update config of api rewrite --- next.config.mjs | 11 ++++++++++- src/app/book/page.tsx | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/next.config.mjs b/next.config.mjs index 4678774..f1d33f4 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -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; diff --git a/src/app/book/page.tsx b/src/app/book/page.tsx index 02b288a..03856d8 100644 --- a/src/app/book/page.tsx +++ b/src/app/book/page.tsx @@ -8,7 +8,7 @@ export default function () { const [books, setBooks] = useState([]); const fetchBooks = async () => { try { - const { data } = await fetch('http://localhost:3000/book/api/fetchBooks').then(res => res.json()); + const { data } = await fetch('/book/api/fetchBooks').then(res => res.json()); setBooks(data); } catch (err) { console.log(err); From 20c0208029fa6b0a239779fe476fffb6eaf06827 Mon Sep 17 00:00:00 2001 From: "Zewei.Zhang" Date: Tue, 26 Mar 2024 16:56:00 +0800 Subject: [PATCH 3/3] feat: update db with vercel postgres --- src/app/book/[slug]/page.tsx | 6 ++--- src/app/book/api/fetchBook/route.ts | 15 ++++++----- src/app/book/api/fetchBooks/route.ts | 4 +-- .../book/api/fetchChaptersByBookId/route.ts | 25 ++++++++++++------- .../book/api/fetchCharactersByBookId/route.ts | 22 ++++++++++------ src/app/book/api/utils.ts | 18 ------------- src/db/scripts/createTableScript.js | 2 +- 7 files changed, 45 insertions(+), 47 deletions(-) diff --git a/src/app/book/[slug]/page.tsx b/src/app/book/[slug]/page.tsx index 3a9b913..e554512 100644 --- a/src/app/book/[slug]/page.tsx +++ b/src/app/book/[slug]/page.tsx @@ -17,7 +17,7 @@ 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); @@ -25,7 +25,7 @@ export default function Page({ params }: { params: { slug: string } }) { } 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); @@ -33,7 +33,7 @@ export default function Page({ params }: { params: { slug: string } }) { } 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); diff --git a/src/app/book/api/fetchBook/route.ts b/src/app/book/api/fetchBook/route.ts index f9b8c22..c74d473 100644 --- a/src/app/book/api/fetchBook/route.ts +++ b/src/app/book/api/fetchBook/route.ts @@ -1,4 +1,4 @@ -import { connectDB } from "../utils"; +import { connectPostgres } from "../utils"; export interface Book { id: string, @@ -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({ + 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 }) } \ No newline at end of file diff --git a/src/app/book/api/fetchBooks/route.ts b/src/app/book/api/fetchBooks/route.ts index 6efc738..08e9d19 100644 --- a/src/app/book/api/fetchBooks/route.ts +++ b/src/app/book/api/fetchBooks/route.ts @@ -1,4 +1,4 @@ -import { connectDB, connectPostgres } from "../utils"; +import { connectPostgres } from "../utils"; export interface Book { id: string, title: string, @@ -20,7 +20,7 @@ export async function GET() { const db = await connectPostgres(); if (db) { try { - const { rows } = await db?.query({ + const { rows } = await db.query({ text: 'select * from book' }); db.end(); diff --git a/src/app/book/api/fetchChaptersByBookId/route.ts b/src/app/book/api/fetchChaptersByBookId/route.ts index ad97749..b389762 100644 --- a/src/app/book/api/fetchChaptersByBookId/route.ts +++ b/src/app/book/api/fetchChaptersByBookId/route.ts @@ -1,4 +1,4 @@ -import { connectDB } from "../utils"; +import { connectPostgres } from "../utils"; export interface Chapter { id: string, @@ -6,18 +6,25 @@ export interface Chapter { 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({ + 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: [] }) } \ No newline at end of file diff --git a/src/app/book/api/fetchCharactersByBookId/route.ts b/src/app/book/api/fetchCharactersByBookId/route.ts index 686fb4b..8ffab4c 100644 --- a/src/app/book/api/fetchCharactersByBookId/route.ts +++ b/src/app/book/api/fetchCharactersByBookId/route.ts @@ -1,4 +1,4 @@ -import { connectDB } from "../utils"; +import { connectPostgres } from "../utils"; export interface Character { id: string, @@ -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({ + 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: [] }) } \ No newline at end of file diff --git a/src/app/book/api/utils.ts b/src/app/book/api/utils.ts index 84ca297..b5ae82f 100644 --- a/src/app/book/api/utils.ts +++ b/src/app/book/api/utils.ts @@ -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({ diff --git a/src/db/scripts/createTableScript.js b/src/db/scripts/createTableScript.js index 75ad911..d584baa 100644 --- a/src/db/scripts/createTableScript.js +++ b/src/db/scripts/createTableScript.js @@ -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,