Establishes Drizzle ORM schema; enhances auth

Establishes the initial database schema using Drizzle ORM, replacing the prior placeholder. This migration introduces core entities like users, spaces, pages, access tokens, and their permissions, complete with relationships and constraints.

Updates the authentication flow by securing the sign-in endpoint with JWT validation and extracting user details directly from the token via a new custom parameter decorator, improving user context handling.

Integrates a new `Page` module, laying the groundwork for future page management capabilities.
This commit is contained in:
dahyeon committed 2025-11-10 14:50:29 +00:00
1 parent 469e799b01
commit dff10ff604
12 files changed
+925 -13

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
-- Current sql file was generated after introspecting the database
-- If you want to run this migration please uncomment this code before executing migrations
/*
CREATE TABLE "users" (
"user_id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"entra_object_id" varchar(36) NOT NULL,
"entra_tenant_id" varchar(36) NOT NULL,
"email" varchar(255) NOT NULL,
"display_name" varchar(255) NOT NULL,
"is_system_admin" boolean DEFAULT false,
"is_active" boolean DEFAULT true,
"created_at" timestamp with time zone DEFAULT now(),
"updated_at" timestamp with time zone DEFAULT now(),
"last_login_at" timestamp with time zone,
CONSTRAINT "users_entra_object_id_key" UNIQUE("entra_object_id")
);
--> statement-breakpoint
CREATE TABLE "spaces" (
"space_id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"space_key" varchar(50) NOT NULL,
"name" varchar(255) NOT NULL,
"description" text,
"created_by_user_id" uuid NOT NULL,
"created_at" timestamp with time zone DEFAULT now(),
CONSTRAINT "spaces_space_key_key" UNIQUE("space_key")
);
--> statement-breakpoint
CREATE TABLE "pages" (
"page_id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"space_id" uuid NOT NULL,
"parent_page_id" uuid,
"sort_order" integer DEFAULT 0,
"title" varchar(255) NOT NULL,
"content" text,
"created_by_user_id" uuid NOT NULL,
"created_at" timestamp with time zone DEFAULT now(),
"updated_by_user_id" uuid NOT NULL,
"updated_at" timestamp with time zone DEFAULT now(),
"is_deleted" boolean DEFAULT false
);
--> statement-breakpoint
CREATE TABLE "accesstokens" (
"token_id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"token_value" text NOT NULL,
"description" varchar(255),
"expires_at" timestamp with time zone,
"created_by_user_id" uuid NOT NULL,
"created_at" timestamp with time zone DEFAULT now(),
CONSTRAINT "accesstokens_token_value_key" UNIQUE("token_value")
);
--> statement-breakpoint
CREATE TABLE "tokenpermissions" (
"permission_id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"token_id" uuid NOT NULL,
"space_id" uuid,
"page_id" uuid,
CONSTRAINT "tokenpermissions_token_id_space_id_key" UNIQUE("token_id","space_id"),
CONSTRAINT "tokenpermissions_token_id_page_id_key" UNIQUE("token_id","page_id"),
CONSTRAINT "tokenpermissions_check" CHECK (((space_id IS NOT NULL) AND (page_id IS NULL)) OR ((space_id IS NULL) AND (page_id IS NOT NULL)))
);
--> statement-breakpoint
ALTER TABLE "spaces" ADD CONSTRAINT "spaces_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "public"."users"("user_id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "pages" ADD CONSTRAINT "pages_space_id_fkey" FOREIGN KEY ("space_id") REFERENCES "public"."spaces"("space_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "pages" ADD CONSTRAINT "pages_parent_page_id_fkey" FOREIGN KEY ("parent_page_id") REFERENCES "public"."pages"("page_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "pages" ADD CONSTRAINT "pages_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "public"."users"("user_id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "pages" ADD CONSTRAINT "pages_updated_by_user_id_fkey" FOREIGN KEY ("updated_by_user_id") REFERENCES "public"."users"("user_id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "accesstokens" ADD CONSTRAINT "accesstokens_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "public"."users"("user_id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tokenpermissions" ADD CONSTRAINT "tokenpermissions_token_id_fkey" FOREIGN KEY ("token_id") REFERENCES "public"."accesstokens"("token_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tokenpermissions" ADD CONSTRAINT "tokenpermissions_space_id_fkey" FOREIGN KEY ("space_id") REFERENCES "public"."spaces"("space_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tokenpermissions" ADD CONSTRAINT "tokenpermissions_page_id_fkey" FOREIGN KEY ("page_id") REFERENCES "public"."pages"("page_id") ON DELETE cascade ON UPDATE no action;
*/
+500
View File
@@ -0,0 +1,500 @@
{
"id": "00000000-0000-0000-0000-000000000000",
"prevId": "",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.users": {
"name": "users",
"schema": "",
"columns": {
"user_id": {
"name": "user_id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"entra_object_id": {
"name": "entra_object_id",
"type": "varchar(36)",
"primaryKey": false,
"notNull": true
},
"entra_tenant_id": {
"name": "entra_tenant_id",
"type": "varchar(36)",
"primaryKey": false,
"notNull": true
},
"email": {
"name": "email",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"display_name": {
"name": "display_name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"is_system_admin": {
"name": "is_system_admin",
"type": "boolean",
"primaryKey": false,
"notNull": false,
"default": false
},
"is_active": {
"name": "is_active",
"type": "boolean",
"primaryKey": false,
"notNull": false,
"default": true
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
},
"last_login_at": {
"name": "last_login_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"users_entra_object_id_key": {
"columns": [
"entra_object_id"
],
"nullsNotDistinct": false,
"name": "users_entra_object_id_key"
}
},
"checkConstraints": {},
"policies": {},
"isRLSEnabled": false
},
"public.spaces": {
"name": "spaces",
"schema": "",
"columns": {
"space_id": {
"name": "space_id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"space_key": {
"name": "space_key",
"type": "varchar(50)",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"description": {
"name": "description",
"type": "text",
"primaryKey": false,
"notNull": false
},
"created_by_user_id": {
"name": "created_by_user_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {
"spaces_created_by_user_id_fkey": {
"name": "spaces_created_by_user_id_fkey",
"tableFrom": "spaces",
"tableTo": "users",
"schemaTo": "public",
"columnsFrom": [
"created_by_user_id"
],
"columnsTo": [
"user_id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"spaces_space_key_key": {
"columns": [
"space_key"
],
"nullsNotDistinct": false,
"name": "spaces_space_key_key"
}
},
"checkConstraints": {},
"policies": {},
"isRLSEnabled": false
},
"public.pages": {
"name": "pages",
"schema": "",
"columns": {
"page_id": {
"name": "page_id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"space_id": {
"name": "space_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"parent_page_id": {
"name": "parent_page_id",
"type": "uuid",
"primaryKey": false,
"notNull": false
},
"sort_order": {
"name": "sort_order",
"type": "integer",
"primaryKey": false,
"notNull": false,
"default": 0
},
"title": {
"name": "title",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"content": {
"name": "content",
"type": "text",
"primaryKey": false,
"notNull": false
},
"created_by_user_id": {
"name": "created_by_user_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
},
"updated_by_user_id": {
"name": "updated_by_user_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"updated_at": {
"name": "updated_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
},
"is_deleted": {
"name": "is_deleted",
"type": "boolean",
"primaryKey": false,
"notNull": false,
"default": false
}
},
"indexes": {},
"foreignKeys": {
"pages_space_id_fkey": {
"name": "pages_space_id_fkey",
"tableFrom": "pages",
"tableTo": "spaces",
"schemaTo": "public",
"columnsFrom": [
"space_id"
],
"columnsTo": [
"space_id"
],
"onDelete": "cascade",
"onUpdate": "no action"
},
"pages_parent_page_id_fkey": {
"name": "pages_parent_page_id_fkey",
"tableFrom": "pages",
"tableTo": "pages",
"schemaTo": "public",
"columnsFrom": [
"parent_page_id"
],
"columnsTo": [
"page_id"
],
"onDelete": "cascade",
"onUpdate": "no action"
},
"pages_created_by_user_id_fkey": {
"name": "pages_created_by_user_id_fkey",
"tableFrom": "pages",
"tableTo": "users",
"schemaTo": "public",
"columnsFrom": [
"created_by_user_id"
],
"columnsTo": [
"user_id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"pages_updated_by_user_id_fkey": {
"name": "pages_updated_by_user_id_fkey",
"tableFrom": "pages",
"tableTo": "users",
"schemaTo": "public",
"columnsFrom": [
"updated_by_user_id"
],
"columnsTo": [
"user_id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {},
"policies": {},
"isRLSEnabled": false
},
"public.accesstokens": {
"name": "accesstokens",
"schema": "",
"columns": {
"token_id": {
"name": "token_id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"token_value": {
"name": "token_value",
"type": "text",
"primaryKey": false,
"notNull": true
},
"description": {
"name": "description",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"expires_at": {
"name": "expires_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false
},
"created_by_user_id": {
"name": "created_by_user_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {
"accesstokens_created_by_user_id_fkey": {
"name": "accesstokens_created_by_user_id_fkey",
"tableFrom": "accesstokens",
"tableTo": "users",
"schemaTo": "public",
"columnsFrom": [
"created_by_user_id"
],
"columnsTo": [
"user_id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"accesstokens_token_value_key": {
"columns": [
"token_value"
],
"nullsNotDistinct": false,
"name": "accesstokens_token_value_key"
}
},
"checkConstraints": {},
"policies": {},
"isRLSEnabled": false
},
"public.tokenpermissions": {
"name": "tokenpermissions",
"schema": "",
"columns": {
"permission_id": {
"name": "permission_id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"token_id": {
"name": "token_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"space_id": {
"name": "space_id",
"type": "uuid",
"primaryKey": false,
"notNull": false
},
"page_id": {
"name": "page_id",
"type": "uuid",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {
"tokenpermissions_token_id_fkey": {
"name": "tokenpermissions_token_id_fkey",
"tableFrom": "tokenpermissions",
"tableTo": "accesstokens",
"schemaTo": "public",
"columnsFrom": [
"token_id"
],
"columnsTo": [
"token_id"
],
"onDelete": "cascade",
"onUpdate": "no action"
},
"tokenpermissions_space_id_fkey": {
"name": "tokenpermissions_space_id_fkey",
"tableFrom": "tokenpermissions",
"tableTo": "spaces",
"schemaTo": "public",
"columnsFrom": [
"space_id"
],
"columnsTo": [
"space_id"
],
"onDelete": "cascade",
"onUpdate": "no action"
},
"tokenpermissions_page_id_fkey": {
"name": "tokenpermissions_page_id_fkey",
"tableFrom": "tokenpermissions",
"tableTo": "pages",
"schemaTo": "public",
"columnsFrom": [
"page_id"
],
"columnsTo": [
"page_id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"tokenpermissions_token_id_space_id_key": {
"columns": [
"token_id",
"space_id"
],
"nullsNotDistinct": false,
"name": "tokenpermissions_token_id_space_id_key"
},
"tokenpermissions_token_id_page_id_key": {
"columns": [
"token_id",
"page_id"
],
"nullsNotDistinct": false,
"name": "tokenpermissions_token_id_page_id_key"
}
},
"checkConstraints": {
"tokenpermissions_check": {
"name": "tokenpermissions_check",
"value": "((space_id IS NOT NULL) AND (page_id IS NULL)) OR ((space_id IS NULL) AND (page_id IS NOT NULL))"
}
},
"policies": {},
"isRLSEnabled": false
}
},
"enums": {},
"schemas": {},
"sequences": {},
"roles": {},
"policies": {},
"views": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
},
"internal": {
"tables": {}
}
}
+13
View File
@@ -0,0 +1,13 @@
{
"version": "7",
"dialect": "postgresql",
"entries": [
{
"idx": 0,
"version": "7",
"when": 1762783006983,
"tag": "0000_nosy_forgotten_one",
"breakpoints": true
}
]
}
+71
View File
@@ -0,0 +1,71 @@
import { relations } from "drizzle-orm/relations";
import { users, spaces, pages, accesstokens, tokenpermissions } from "./schema";
export const spacesRelations = relations(spaces, ({one, many}) => ({
user: one(users, {
fields: [spaces.createdByUserId],
references: [users.userId]
}),
pages: many(pages),
tokenpermissions: many(tokenpermissions),
}));
export const usersRelations = relations(users, ({many}) => ({
spaces: many(spaces),
pages_createdByUserId: many(pages, {
relationName: "pages_createdByUserId_users_userId"
}),
pages_updatedByUserId: many(pages, {
relationName: "pages_updatedByUserId_users_userId"
}),
accesstokens: many(accesstokens),
}));
export const pagesRelations = relations(pages, ({one, many}) => ({
space: one(spaces, {
fields: [pages.spaceId],
references: [spaces.spaceId]
}),
page: one(pages, {
fields: [pages.parentPageId],
references: [pages.pageId],
relationName: "pages_parentPageId_pages_pageId"
}),
pages: many(pages, {
relationName: "pages_parentPageId_pages_pageId"
}),
user_createdByUserId: one(users, {
fields: [pages.createdByUserId],
references: [users.userId],
relationName: "pages_createdByUserId_users_userId"
}),
user_updatedByUserId: one(users, {
fields: [pages.updatedByUserId],
references: [users.userId],
relationName: "pages_updatedByUserId_users_userId"
}),
tokenpermissions: many(tokenpermissions),
}));
export const accesstokensRelations = relations(accesstokens, ({one, many}) => ({
user: one(users, {
fields: [accesstokens.createdByUserId],
references: [users.userId]
}),
tokenpermissions: many(tokenpermissions),
}));
export const tokenpermissionsRelations = relations(tokenpermissions, ({one}) => ({
accesstoken: one(accesstokens, {
fields: [tokenpermissions.tokenId],
references: [accesstokens.tokenId]
}),
space: one(spaces, {
fields: [tokenpermissions.spaceId],
references: [spaces.spaceId]
}),
page: one(pages, {
fields: [tokenpermissions.pageId],
references: [pages.pageId]
}),
}));
+112
View File
@@ -0,0 +1,112 @@
import { pgTable, unique, uuid, varchar, boolean, timestamp, foreignKey, text, integer, check } from "drizzle-orm/pg-core"
import { sql } from "drizzle-orm"
export const users = pgTable("users", {
userId: uuid("user_id").defaultRandom().primaryKey().notNull(),
entraObjectId: varchar("entra_object_id", { length: 36 }).notNull(),
entraTenantId: varchar("entra_tenant_id", { length: 36 }).notNull(),
email: varchar({ length: 255 }).notNull(),
displayName: varchar("display_name", { length: 255 }).notNull(),
isSystemAdmin: boolean("is_system_admin").default(false),
isActive: boolean("is_active").default(true),
createdAt: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true, mode: 'string' }).defaultNow(),
lastLoginAt: timestamp("last_login_at", { withTimezone: true, mode: 'string' }),
}, (table) => [
unique("users_entra_object_id_key").on(table.entraObjectId),
]);
export const spaces = pgTable("spaces", {
spaceId: uuid("space_id").defaultRandom().primaryKey().notNull(),
spaceKey: varchar("space_key", { length: 50 }).notNull(),
name: varchar({ length: 255 }).notNull(),
description: text(),
createdByUserId: uuid("created_by_user_id").notNull(),
createdAt: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow(),
}, (table) => [
foreignKey({
columns: [table.createdByUserId],
foreignColumns: [users.userId],
name: "spaces_created_by_user_id_fkey"
}),
unique("spaces_space_key_key").on(table.spaceKey),
]);
export const pages = pgTable("pages", {
pageId: uuid("page_id").defaultRandom().primaryKey().notNull(),
spaceId: uuid("space_id").notNull(),
parentPageId: uuid("parent_page_id"),
sortOrder: integer("sort_order").default(0),
title: varchar({ length: 255 }).notNull(),
content: text(),
createdByUserId: uuid("created_by_user_id").notNull(),
createdAt: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow(),
updatedByUserId: uuid("updated_by_user_id").notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true, mode: 'string' }).defaultNow(),
isDeleted: boolean("is_deleted").default(false),
}, (table) => [
foreignKey({
columns: [table.spaceId],
foreignColumns: [spaces.spaceId],
name: "pages_space_id_fkey"
}).onDelete("cascade"),
foreignKey({
columns: [table.parentPageId],
foreignColumns: [table.pageId],
name: "pages_parent_page_id_fkey"
}).onDelete("cascade"),
foreignKey({
columns: [table.createdByUserId],
foreignColumns: [users.userId],
name: "pages_created_by_user_id_fkey"
}),
foreignKey({
columns: [table.updatedByUserId],
foreignColumns: [users.userId],
name: "pages_updated_by_user_id_fkey"
}),
]);
export const accesstokens = pgTable("accesstokens", {
tokenId: uuid("token_id").defaultRandom().primaryKey().notNull(),
tokenValue: text("token_value").notNull(),
description: varchar({ length: 255 }),
expiresAt: timestamp("expires_at", { withTimezone: true, mode: 'string' }),
createdByUserId: uuid("created_by_user_id").notNull(),
createdAt: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow(),
}, (table) => [
foreignKey({
columns: [table.createdByUserId],
foreignColumns: [users.userId],
name: "accesstokens_created_by_user_id_fkey"
}),
unique("accesstokens_token_value_key").on(table.tokenValue),
]);
export const tokenpermissions = pgTable("tokenpermissions", {
permissionId: uuid("permission_id").defaultRandom().primaryKey().notNull(),
tokenId: uuid("token_id").notNull(),
spaceId: uuid("space_id"),
pageId: uuid("page_id"),
}, (table) => [
foreignKey({
columns: [table.tokenId],
foreignColumns: [accesstokens.tokenId],
name: "tokenpermissions_token_id_fkey"
}).onDelete("cascade"),
foreignKey({
columns: [table.spaceId],
foreignColumns: [spaces.spaceId],
name: "tokenpermissions_space_id_fkey"
}).onDelete("cascade"),
foreignKey({
columns: [table.pageId],
foreignColumns: [pages.pageId],
name: "tokenpermissions_page_id_fkey"
}).onDelete("cascade"),
unique("tokenpermissions_token_id_space_id_key").on(table.tokenId, table.spaceId),
unique("tokenpermissions_token_id_page_id_key").on(table.tokenId, table.pageId),
check("tokenpermissions_check", sql`((space_id IS NOT NULL) AND (page_id IS NULL)) OR ((space_id IS NULL) AND (page_id IS NOT NULL))`),
]);
+5 -3
View File
@@ -2,15 +2,17 @@ import { Controller, Get, UseGuards } from "@nestjs/common";
import { AccountService } from "./account.service";
import { JwtAuthGuard } from "../global/auth/jwt.guard";
import { AllowPublicAccess } from "../global/auth/allow-public-access.decorator";
import { ExtractUserPayloadFromToken } from "../global/auth/extract-user-payload-from-token.decorator";
import { EntraIDPayloadClaims } from "../global/auth/jwt.service";
@Controller('account')
export class AccountController {
constructor(private readonly accountService: AccountService) {}
@Get('signin')
@AllowPublicAccess()
signIn(): string {
return this.accountService.signIn();
@UseGuards(JwtAuthGuard)
signIn(@ExtractUserPayloadFromToken() userPayload: Omit<EntraIDPayloadClaims, 'iss' | 'iat' | 'nbf' | 'rh' | 'ver'>): string {
return this.accountService.signIn('test', userPayload);
}
+2 -1
View File
@@ -2,8 +2,9 @@ import { Injectable } from "@nestjs/common";
@Injectable()
export class AccountService {
signIn(): string {
signIn(p0: any, userPayload: any): string {
//TODO: Implement sign in logic
console.log(userPayload);
return 'Sign In Successful';
}
@@ -0,0 +1,17 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { EntraIDPayloadClaims, JwtService } from './jwt.service';
import { Request } from 'express';
import {
decodeJwt
} from 'jose';
//request에서 user 정보만
export const ExtractUserPayloadFromToken = createParamDecorator(async (data: unknown, ctx: ExecutionContext) => {
const request: Request = ctx.switchToHttp().getRequest();
const token = request.header('authorization')?.replace('Bearer ', '');
const claims = decodeJwt(token!) as EntraIDPayloadClaims
// 요청 메타데이터 저장
const { aud, iss, iat, nbf, rh, ver, ...accountMetadata } = claims
// global.__drizzleContexts = accountMetadata;
return accountMetadata;
});
+111 -9
View File
@@ -1,10 +1,112 @@
import { pgTable } from "drizzle-orm/pg-core";
import { integer } from "drizzle-orm/pg-core";
import { varchar } from "drizzle-orm/pg-core";
import { pgTable, unique, uuid, varchar, boolean, timestamp, foreignKey, text, integer, check } from "drizzle-orm/pg-core"
import { sql } from "drizzle-orm"
export const usersTable = pgTable("users", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
name: varchar({ length: 255 }).notNull(),
age: integer().notNull(),
email: varchar({ length: 255 }).notNull().unique(),
});
export const users = pgTable("users", {
userId: uuid("user_id").defaultRandom().primaryKey().notNull(),
entraObjectId: varchar("entra_object_id", { length: 36 }).notNull(),
entraTenantId: varchar("entra_tenant_id", { length: 36 }).notNull(),
email: varchar({ length: 255 }).notNull(),
displayName: varchar("display_name", { length: 255 }).notNull(),
isSystemAdmin: boolean("is_system_admin").default(false),
isActive: boolean("is_active").default(true),
createdAt: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true, mode: 'string' }).defaultNow(),
lastLoginAt: timestamp("last_login_at", { withTimezone: true, mode: 'string' }),
}, (table) => [
unique("users_entra_object_id_key").on(table.entraObjectId),
]);
export const spaces = pgTable("spaces", {
spaceId: uuid("space_id").defaultRandom().primaryKey().notNull(),
spaceKey: varchar("space_key", { length: 50 }).notNull(),
name: varchar({ length: 255 }).notNull(),
description: text(),
createdByUserId: uuid("created_by_user_id").notNull(),
createdAt: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow(),
}, (table) => [
foreignKey({
columns: [table.createdByUserId],
foreignColumns: [users.userId],
name: "spaces_created_by_user_id_fkey"
}),
unique("spaces_space_key_key").on(table.spaceKey),
]);
export const pages = pgTable("pages", {
pageId: uuid("page_id").defaultRandom().primaryKey().notNull(),
spaceId: uuid("space_id").notNull(),
parentPageId: uuid("parent_page_id"),
sortOrder: integer("sort_order").default(0),
title: varchar({ length: 255 }).notNull(),
content: text(),
createdByUserId: uuid("created_by_user_id").notNull(),
createdAt: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow(),
updatedByUserId: uuid("updated_by_user_id").notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true, mode: 'string' }).defaultNow(),
isDeleted: boolean("is_deleted").default(false),
}, (table) => [
foreignKey({
columns: [table.spaceId],
foreignColumns: [spaces.spaceId],
name: "pages_space_id_fkey"
}).onDelete("cascade"),
foreignKey({
columns: [table.parentPageId],
foreignColumns: [table.pageId],
name: "pages_parent_page_id_fkey"
}).onDelete("cascade"),
foreignKey({
columns: [table.createdByUserId],
foreignColumns: [users.userId],
name: "pages_created_by_user_id_fkey"
}),
foreignKey({
columns: [table.updatedByUserId],
foreignColumns: [users.userId],
name: "pages_updated_by_user_id_fkey"
}),
]);
export const accesstokens = pgTable("accesstokens", {
tokenId: uuid("token_id").defaultRandom().primaryKey().notNull(),
tokenValue: text("token_value").notNull(),
description: varchar({ length: 255 }),
expiresAt: timestamp("expires_at", { withTimezone: true, mode: 'string' }),
createdByUserId: uuid("created_by_user_id").notNull(),
createdAt: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow(),
}, (table) => [
foreignKey({
columns: [table.createdByUserId],
foreignColumns: [users.userId],
name: "accesstokens_created_by_user_id_fkey"
}),
unique("accesstokens_token_value_key").on(table.tokenValue),
]);
export const tokenpermissions = pgTable("tokenpermissions", {
permissionId: uuid("permission_id").defaultRandom().primaryKey().notNull(),
tokenId: uuid("token_id").notNull(),
spaceId: uuid("space_id"),
pageId: uuid("page_id"),
}, (table) => [
foreignKey({
columns: [table.tokenId],
foreignColumns: [accesstokens.tokenId],
name: "tokenpermissions_token_id_fkey"
}).onDelete("cascade"),
foreignKey({
columns: [table.spaceId],
foreignColumns: [spaces.spaceId],
name: "tokenpermissions_space_id_fkey"
}).onDelete("cascade"),
foreignKey({
columns: [table.pageId],
foreignColumns: [pages.pageId],
name: "tokenpermissions_page_id_fkey"
}).onDelete("cascade"),
unique("tokenpermissions_token_id_space_id_key").on(table.tokenId, table.spaceId),
unique("tokenpermissions_token_id_page_id_key").on(table.tokenId, table.pageId),
check("tokenpermissions_check", sql`((space_id IS NOT NULL) AND (page_id IS NULL)) OR ((space_id IS NULL) AND (page_id IS NOT NULL))`),
]);
+7
View File
@@ -0,0 +1,7 @@
import { Controller } from "@nestjs/common";
import { PageService } from "./page.service";
@Controller('page')
export class PageController {
constructor(private readonly pageService: PageService) {}
}
+9
View File
@@ -0,0 +1,9 @@
import { Module } from "@nestjs/common";
import { PageController } from "./page.controller";
import { PageService } from "./page.service";
@Module({
controllers: [PageController],
providers: [PageService],
})
export class PageModule {}
+7
View File
@@ -0,0 +1,7 @@
import { Injectable } from "@nestjs/common";
@Injectable()
export class PageService {
}