basic crud operations on article table
This commit is contained in:
Generated
+15
-1
@@ -12,7 +12,8 @@
|
|||||||
"dotenv": "^17.2.3",
|
"dotenv": "^17.2.3",
|
||||||
"drizzle-orm": "^0.44.7",
|
"drizzle-orm": "^0.44.7",
|
||||||
"express": "^5.1.0",
|
"express": "^5.1.0",
|
||||||
"pg": "^8.16.3"
|
"pg": "^8.16.3",
|
||||||
|
"uuid": "^13.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/express": "^5.0.5",
|
"@types/express": "^5.0.5",
|
||||||
@@ -2285,6 +2286,19 @@
|
|||||||
"node": ">= 0.8"
|
"node": ">= 0.8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/uuid": {
|
||||||
|
"version": "13.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/uuid/-/uuid-13.0.0.tgz",
|
||||||
|
"integrity": "sha512-XQegIaBTVUjSHliKqcnFqYypAd4S+WCYt5NIeRs6w/UAry7z8Y9j5ZwRRL4kzq9U3sD6v+85er9FvkEaBpji2w==",
|
||||||
|
"funding": [
|
||||||
|
"https://github.com/sponsors/broofa",
|
||||||
|
"https://github.com/sponsors/ctavan"
|
||||||
|
],
|
||||||
|
"license": "MIT",
|
||||||
|
"bin": {
|
||||||
|
"uuid": "dist-node/bin/uuid"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/vary": {
|
"node_modules/vary": {
|
||||||
"version": "1.1.2",
|
"version": "1.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
|
||||||
|
|||||||
@@ -12,7 +12,8 @@
|
|||||||
"dotenv": "^17.2.3",
|
"dotenv": "^17.2.3",
|
||||||
"drizzle-orm": "^0.44.7",
|
"drizzle-orm": "^0.44.7",
|
||||||
"express": "^5.1.0",
|
"express": "^5.1.0",
|
||||||
"pg": "^8.16.3"
|
"pg": "^8.16.3",
|
||||||
|
"uuid": "^13.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/express": "^5.0.5",
|
"@types/express": "^5.0.5",
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { integer, pgTable, varchar } from "drizzle-orm/pg-core";
|
import { integer, uuid, pgTable, varchar, date } from "drizzle-orm/pg-core";
|
||||||
|
|
||||||
export const usersTable = pgTable("users", {
|
export const articles = pgTable("articles", {
|
||||||
id: integer().primaryKey().generatedAlwaysAsIdentity(),
|
id: uuid().notNull().primaryKey().defaultRandom(),
|
||||||
name: varchar({ length: 255 }).notNull(),
|
name: varchar({ length: 255 }).notNull(),
|
||||||
age: integer().notNull(),
|
date: date({ mode: "date" }).notNull(),
|
||||||
email: varchar({ length: 255 }).notNull().unique(),
|
text: varchar({ length: 9999 }).notNull()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+49
-2
@@ -1,19 +1,66 @@
|
|||||||
import 'dotenv/config';
|
import 'dotenv/config';
|
||||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||||
|
import { eq } from 'drizzle-orm';
|
||||||
import express, { Request, Response } from "express";
|
import express, { Request, Response } from "express";
|
||||||
|
import { articles } from './db/schema.ts';
|
||||||
|
|
||||||
// Create a connection to the database
|
// Create a connection to the database
|
||||||
const db = drizzle(process.env.DATABASE_URL!);
|
const db = drizzle(process.env.DATABASE_URL!);
|
||||||
|
|
||||||
// Create a new express application instance
|
// Create a new express application instance
|
||||||
const app = express();
|
const app = express();
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
// Set the network port
|
// Set the network port
|
||||||
const port = process.env.PORT || 3000;
|
const port = process.env.PORT || 3000;
|
||||||
|
|
||||||
// Define the root path with a greeting message
|
// Define the root path with a greeting message
|
||||||
app.get("/", (req: Request, res: Response) => {
|
app.get("/status", (req: Request, res: Response) => {
|
||||||
res.json({ message: "Welcome to the Express + TypeScript Server!" });
|
res.json({ message: "Backend Operational" });
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get("/getAllArticles", async (req: Request, res: Response) => {
|
||||||
|
const resp = await db.select().from(articles);
|
||||||
|
res.json({ resp });
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get("/getArticle/:articleId", async (req: Request, res: Response) => {
|
||||||
|
const { articleId } = req.params;
|
||||||
|
|
||||||
|
const resp = await db.select()
|
||||||
|
.from(articles)
|
||||||
|
.where(eq(articles.id, articleId));
|
||||||
|
res.json({ resp });
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post("/postArticle", async (req: Request, res: Response) => {
|
||||||
|
const resp = await db
|
||||||
|
.insert(articles)
|
||||||
|
.values({
|
||||||
|
...req.body,
|
||||||
|
date: new Date(req.body.date)
|
||||||
|
});
|
||||||
|
res.json({ resp });
|
||||||
|
});
|
||||||
|
|
||||||
|
app.put("/putArticle/:articleId", async (req: Request, res: Response) => {
|
||||||
|
const { articleId } = req.params;
|
||||||
|
const resp = await db.update(articles)
|
||||||
|
.set({
|
||||||
|
name: req.body.name,
|
||||||
|
date: new Date(req.body.date),
|
||||||
|
text: req.body.text
|
||||||
|
})
|
||||||
|
.where(eq(articles.id, articleId));
|
||||||
|
res.json({ resp });
|
||||||
|
});
|
||||||
|
|
||||||
|
app.delete("/deleteArticle/:articleId", async (req: Request, res: Response) => {
|
||||||
|
const { articleId } = req.params;
|
||||||
|
const resp = await db.delete(articles)
|
||||||
|
.where(eq(articles.id, articleId));
|
||||||
|
|
||||||
|
res.json({ resp });
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start the Express server
|
// Start the Express server
|
||||||
|
|||||||
Reference in New Issue
Block a user