add users table and users router
This commit is contained in:
@@ -8,9 +8,12 @@ import {
|
||||
integer,
|
||||
bigint,
|
||||
uniqueIndex,
|
||||
index
|
||||
index,
|
||||
boolean,
|
||||
smallint,
|
||||
check
|
||||
} from "drizzle-orm/pg-core";
|
||||
import { relations } from "drizzle-orm";
|
||||
import { sql, relations } from "drizzle-orm";
|
||||
|
||||
export const blockTypeEnum = pgEnum("post_block_type", [
|
||||
"title",
|
||||
@@ -34,7 +37,7 @@ export const articles = pgTable("articles", {
|
||||
name: varchar({ length: 255 }).notNull(),
|
||||
article_status: articleStatusEnum().default("draft"),
|
||||
author_id: uuid().notNull(),
|
||||
slug: varchar({ length: 255 }).unique().notNull(),
|
||||
slug: varchar({ length: 511 }).unique().notNull(),
|
||||
summary: varchar({ length: 255 }).notNull(),
|
||||
|
||||
date_created: timestamp({ mode: "date", withTimezone: true }).defaultNow().notNull(),
|
||||
@@ -70,6 +73,22 @@ export const images = pgTable("images", {
|
||||
|
||||
});
|
||||
|
||||
export const users = pgTable("users", {
|
||||
id: uuid().notNull().primaryKey().defaultRandom(),
|
||||
display_name: varchar({ length: 63 }).notNull(),
|
||||
|
||||
username: varchar({ length: 63 }).notNull(),
|
||||
hashed_password: varchar({ length: 255 }).notNull(),
|
||||
logged_in: boolean().notNull().default(false),
|
||||
|
||||
token: varchar({ length: 511 }),
|
||||
permissions: smallint().notNull().default(44),
|
||||
},
|
||||
(users) => [
|
||||
check("permissions lower bound", sql`users.permissions >= 0`),
|
||||
check("permissions upper bound", sql`users.permissions <= 77`),
|
||||
]
|
||||
);
|
||||
|
||||
export const articlesRelations = relations(articles, ({ many }) => ({
|
||||
blocks: many(articleBlocks),
|
||||
|
||||
@@ -4,6 +4,7 @@ import { articlesRouter } from './routes/articles.ts';
|
||||
import { blocksRouter } from './routes/blocks.ts';
|
||||
import { imagesRouter } from './routes/images.ts';
|
||||
import { authRouter } from './routes/auth.ts';
|
||||
import { usersRouter } from './routes/users.ts';
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
@@ -16,6 +17,7 @@ app.use("/articles", articlesRouter);
|
||||
app.use("/blocks", blocksRouter);
|
||||
app.use("/images", imagesRouter);
|
||||
app.use("/auth", authRouter);
|
||||
app.use("/users", usersRouter);
|
||||
|
||||
|
||||
const port = process.env.PORT || 3000;
|
||||
|
||||
@@ -5,5 +5,3 @@ import express, { Request, Response } from "express";
|
||||
|
||||
const db = drizzle(process.env.DATABASE_URL!);
|
||||
export const authRouter = express.Router();
|
||||
|
||||
// TODO: implement
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { users } from '../db/schema.ts';
|
||||
import express, { Request, Response } from "express";
|
||||
|
||||
const db = drizzle(process.env.DATABASE_URL!);
|
||||
export const usersRouter = express.Router();
|
||||
|
||||
usersRouter.get("/getUser/:userId", async (req: Request, res: Response) => {
|
||||
const { userId } = req.params;
|
||||
|
||||
const resp = await db.select()
|
||||
.from(users)
|
||||
.where(eq(users.id, userId));
|
||||
|
||||
res.json(resp);
|
||||
});
|
||||
Reference in New Issue
Block a user