Skip to content

Commit

Permalink
finish pg setup
Browse files Browse the repository at this point in the history
  • Loading branch information
tianheg committed Jun 25, 2024
1 parent 9863d81 commit ecb1608
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 41 deletions.
Binary file modified bun.lockb
Binary file not shown.
10 changes: 6 additions & 4 deletions data/words.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export default [
{
content: "\"There's no thought crimes and no thought heroisms\" is honestly such a good piece of life advice. You could be having the most fucked up problematic thoughts 24/7 but if you treat people with kindness, the good you do is the only thing that matters. But if you have only the purest thoughts and all the correct beliefs, it doesn't matter one bit if you spend most of your time being an asshole to people."
},
{
content: "In the end, it's about what you want to be, not what you want to have. When you sign up to run a marathon, you don't want a taxi to take you to the finish line."
content:
"\"There's no thought crimes and no thought heroisms\" is honestly such a good piece of life advice. You could be having the most fucked up problematic thoughts 24/7 but if you treat people with kindness, the good you do is the only thing that matters. But if you have only the purest thoughts and all the correct beliefs, it doesn't matter one bit if you spend most of your time being an asshole to people.",
},
{
content:
"In the end, it's about what you want to be, not what you want to have. When you sign up to run a marathon, you don't want a taxi to take you to the finish line.",
},
{
content: "做一个清醒的人,在保证自己和家人生活的情况下,不再过度追求财富",
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"nodemon": "^3.1.4",
"pino": "^9.2.0",
"pino-pretty": "^11.2.1",
"tap": "^19.2.5"
"tap": "^20.0.0"
},
"dependencies": {
"@fastify/autoload": "^5.10.0",
Expand All @@ -19,14 +19,14 @@
"@fastify/postgres": "^5.2.2",
"@fastify/rate-limit": "^9.1.0",
"@fastify/swagger": "^8.14.0",
"@fastify/swagger-ui": "^3.1.0",
"@fastify/swagger-ui": "^4.0.0",
"@fastify/under-pressure": "^8.5.0",
"fastify": "^4.28.0",
"pg": "^8.12.0"
},
"scripts": {
"dev": "export NODE_ENV=development && nodemon .",
"test": "tap --bail --timeout=5000 *.test.js",
"lint": "biome check --apply ."
"lint": "biome check --write ."
}
}
89 changes: 61 additions & 28 deletions routes.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import booksData from "./data/books.js";
import feedsData from "./data/feeds.js";
import moviesData from "./data/movies.js";
import musicData from "./data/music.js";
import musicalsData from "./data/musicals.js";
import promptsData from "./data/prompts.js";
import seriesData from "./data/series.js";
import wordsData from "./data/words.js";
import { createRoute, paginationSchema } from "./utils.js";

export default async function registerRoutes(app) {
const client = await app.pg.connect();

const booksData = await client.query("SELECT * FROM books");
const feedsData = await client.query("SELECT * FROM feeds");
const moviesData = await client.query("SELECT * FROM movies");
const musicData = await client.query("SELECT * FROM music");
const musicalsData = await client.query("SELECT * FROM musicals");
const promptsData = await client.query("SELECT * FROM prompts");
const seriesData = await client.query("SELECT * FROM series");
const wordsData = await client.query("SELECT * FROM words");

app.get("/", (request, reply) => {
const baseUrl = "https://api.tianheg.org";
const routes = [
Expand All @@ -31,25 +34,55 @@ export default async function registerRoutes(app) {
});
});

app.get("/calc", (request, reply) => {
const client = app.pg.connect();

console.log(client);
// const sumResult = client.query < { sum } > "SELECT 2 + 2 as sum";

// client.release();

// return {
// sum: sumResult.rows,
// };
});
console.log(booksData.rows.map((r) => r.json_data));
createRoute(
app,
"/books",
booksData.rows.map((r) => r.json_data),
{ schema: paginationSchema },
);
createRoute(
app,
"/feeds",
feedsData.rows.map((r) => r.json_data),
{ schema: paginationSchema },
);
createRoute(
app,
"/movies",
moviesData.rows.map((r) => r.json_data),
{ schema: paginationSchema },
);
createRoute(
app,
"/music",
musicData.rows.map((r) => r.json_data),
{ schema: paginationSchema },
);
createRoute(
app,
"/musicals",
musicalsData.rows.map((r) => r.json_data),
{ schema: paginationSchema },
);
createRoute(
app,
"/prompts",
promptsData.rows.map((r) => r.json_data),
{ schema: paginationSchema },
);
createRoute(
app,
"/series",
seriesData.rows.map((r) => r.json_data),
{ schema: paginationSchema },
);
createRoute(
app,
"/words",
wordsData.rows.map((r) => r.json_data),
{ schema: paginationSchema },
);

createRoute(app, "/books", booksData, { schema: paginationSchema });
createRoute(app, "/feeds", feedsData, { schema: paginationSchema });
createRoute(app, "/movies", moviesData, { schema: paginationSchema });
createRoute(app, "/music", musicData, { schema: paginationSchema });
createRoute(app, "/musicals", musicalsData, { schema: paginationSchema });
createRoute(app, "/prompts", promptsData, { schema: paginationSchema });
createRoute(app, "/series", seriesData, { schema: paginationSchema });
createRoute(app, "/words", wordsData, { schema: paginationSchema });
client.release();
}
17 changes: 16 additions & 1 deletion routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@ import tap from "tap";
import registerRoutes from "./routes.js";

tap.test("routes.js", async (t) => {
const mockClient = {
query: () => {
return {
rows: [
{
json_data: "test",
},
],
};
},
release: () => {},
};
const app = Fastify();
app.decorate("pg", {
connect: async () => mockClient,
});
await registerRoutes(app);

t.test("GET /", async (t) => {
Expand Down Expand Up @@ -114,5 +129,5 @@ tap.test("routes.js", async (t) => {
t.type(response.body, String);
});

t.end();
t.teardown(() => app.close());
});
17 changes: 14 additions & 3 deletions scripts/jsonToDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,28 @@ async function uploadJsonDataFromFile(client, relativePath, tableName) {
await client.query(query);
}

console.log(`Data from ${relativePath} inserted successfully into ${tableName}`);
console.log(
`Data from ${relativePath} inserted successfully into ${tableName}`,
);
} catch (err) {
console.error(`Error uploading data from file ${relativePath}:`, err);
}
}

async function uploadAllFiles(client) {
const files = ["books.js", "feeds.js", "movies.js", "music.js", "musicals.js", "prompts.js", "series.js", "words.js"];
const files = [
"books.js",
"feeds.js",
"movies.js",
"music.js",
"musicals.js",
"prompts.js",
"series.js",
"words.js",
];

for (const file of files) {
const tableName = file.replace('.js', ''); // Remove '.js' to form the table name
const tableName = file.replace(".js", ""); // Remove '.js' to form the table name
await uploadJsonDataFromFile(client, `data/${file}`, tableName);
}
}
Expand Down
4 changes: 2 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import compress from "@fastify/compress";
import cors from "@fastify/cors";
import helmet from "@fastify/helmet";
import postgres from "@fastify/postgres";
import rateLimit from "@fastify/rate-limit";
import swagger from "@fastify/swagger";
import swaggerUI from "@fastify/swagger-ui";
import postgres from "@fastify/postgres";
import Fastify from "fastify";
import pino from "pino";
import pretty from "pino-pretty";
Expand Down Expand Up @@ -72,7 +72,7 @@ await app.register(postgres, {
});

/// routes
registerRoutes(app);
await registerRoutes(app);

if (process.env.NODE_ENV === "development") {
/**
Expand Down

0 comments on commit ecb1608

Please sign in to comment.