Skip to content

Commit

Permalink
Fix database sequences (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored Apr 27, 2023
1 parent 194a989 commit d7024ec
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
4 changes: 4 additions & 0 deletions init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ VALUES
(99, '2020-01-21 11:52:16', 'Et dolores nostrum quia ad et.'),
(100, '2020-06-17 16:58:55', 'Asperiores dolores optio accusantium atque illum dolores eos.');

SELECT setval('thread_id_seq', (SELECT max(id) FROM thread), true);

INSERT INTO post(id, thread_id, created_at, title, body)
VALUES
(1, 1, '2021-11-15 12:40:56', 'Doloribus ut aut enim natus.', 'Vero explicabo odit quod quis impedit nostrum quae voluptas. Eius numquam minus. At nam voluptatem eius magnam iste quidem. Laboriosam dolorum ut sequi dicta reiciendis est reprehenderit et iure.'),
Expand Down Expand Up @@ -2831,3 +2833,5 @@ Quis omnis omnis iure repudiandae est ut. Dignissimos eligendi consequatur amet
Ut sapiente nihil quia molestias beatae rerum sit quae. Eveniet non aut. Excepturi esse tenetur et debitis earum voluptas velit. Saepe voluptates nesciunt id nam quam excepturi dicta asperiores laborum.
Possimus ut nemo unde veritatis eum beatae. Commodi nobis velit. Vero quam officia saepe recusandae. Quaerat voluptas illo omnis tempore incidunt cum.');

SELECT setval('post_id_seq', (SELECT max(id) FROM post), true);
10 changes: 4 additions & 6 deletions pages/api/connection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NextApiRequest, NextApiResponse } from "next";
import type { NextApiHandler } from "next";
import { Pool, DatabaseError } from "pg";

const pool = new Pool({
Expand All @@ -8,11 +8,7 @@ const pool = new Pool({
export interface Connection {
error: string | null;
}

export default async (
req: NextApiRequest,
res: NextApiResponse<Connection>
) => {
const connectionApi: NextApiHandler<Connection> = async (req, res) => {
try {
await Promise.all([
pool.query("SELECT count(*) FROM thread"),
Expand All @@ -23,3 +19,5 @@ export default async (
res.status(200).json({ error: (error as DatabaseError).message });
}
};

export default connectionApi;
16 changes: 10 additions & 6 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import { CircularProgress } from "@mui/material";
const Home: NextPage = () => {
const [connection, setConnection] = React.useState<Connection | null>(null);
React.useEffect(() => {
fetch("/api/connection")
.then((response) => response.json())
.then(
(data) => setConnection(data),
(error) => setConnection({ error: error.message })
);
fetch("/api/connection").then(async (response) => {
try {
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
setConnection(await response.json());
} catch (error: any) {
setConnection({ error: error.message });
}
});
}, []);

return (
Expand Down

0 comments on commit d7024ec

Please sign in to comment.