From 21308fa5bd242c1c77c13ebfced27869e95ac15b Mon Sep 17 00:00:00 2001 From: Sanjib Sen Date: Thu, 16 Jan 2025 15:50:28 +0600 Subject: [PATCH] now we only need tests --- ....sql => 0000_uneven_professor_monster.sql} | 3 +- drizzle/meta/0000_snapshot.json | 10 ++++-- drizzle/meta/_journal.json | 4 +-- package.json | 4 ++- src/api/routes/note/note.controller.ts | 18 +++++----- src/api/routes/note/note.model.ts | 26 ++++++++------ src/api/routes/note/note.route.ts | 35 +++++-------------- 7 files changed, 48 insertions(+), 52 deletions(-) rename drizzle/{0000_pretty_banshee.sql => 0000_uneven_professor_monster.sql} (96%) diff --git a/drizzle/0000_pretty_banshee.sql b/drizzle/0000_uneven_professor_monster.sql similarity index 96% rename from drizzle/0000_pretty_banshee.sql rename to drizzle/0000_uneven_professor_monster.sql index 52d3c12..776ae33 100644 --- a/drizzle/0000_pretty_banshee.sql +++ b/drizzle/0000_uneven_professor_monster.sql @@ -59,8 +59,9 @@ CREATE TABLE "note" ( "id" text PRIMARY KEY NOT NULL, "title" text, "content" text, - "createdAt" timestamp DEFAULT now(), + "createdAt" timestamp DEFAULT now() NOT NULL, "updatedAt" timestamp, + "deletedAt" timestamp, "ownerId" text NOT NULL ); --> statement-breakpoint diff --git a/drizzle/meta/0000_snapshot.json b/drizzle/meta/0000_snapshot.json index be10320..e268da5 100644 --- a/drizzle/meta/0000_snapshot.json +++ b/drizzle/meta/0000_snapshot.json @@ -1,5 +1,5 @@ { - "id": "806f9895-fec6-43da-9c78-46e599e611e8", + "id": "50bd2c27-8d45-478f-a894-b2f7cd4a718b", "prevId": "00000000-0000-0000-0000-000000000000", "version": "7", "dialect": "postgresql", @@ -369,7 +369,7 @@ "name": "createdAt", "type": "timestamp", "primaryKey": false, - "notNull": false, + "notNull": true, "default": "now()" }, "updatedAt": { @@ -378,6 +378,12 @@ "primaryKey": false, "notNull": false }, + "deletedAt": { + "name": "deletedAt", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, "ownerId": { "name": "ownerId", "type": "text", diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index 0c15f9e..729beee 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -5,8 +5,8 @@ { "idx": 0, "version": "7", - "when": 1736970092723, - "tag": "0000_pretty_banshee", + "when": 1737019435130, + "tag": "0000_uneven_professor_monster", "breakpoints": true } ] diff --git a/package.json b/package.json index 2a26265..d9984d9 100644 --- a/package.json +++ b/package.json @@ -5,11 +5,13 @@ "test": "echo \"Error: no test specified\" && exit 1", "dev": "bun run --watch src/index.ts", "email": "email dev --dir src/emails", + "auth:generate": "bun x @better-auth/cli generate --config src/lib/auth/auth.ts --output src/db/schema/auth.ts && drizzle-kit migrate", "db:studio": "drizzle-kit studio", - "auth:generate": "bun x @better-auth/cli generate --config src/lib/auth/auth.ts --output src/db/schema.ts && drizzle-kit migrate", "db:check": "drizzle-kit check", "db:generate": "drizzle-kit generate", "db:migrate": "drizzle-kit migrate", + "db:pull": "drizzle-kit pull", + "db:push": "drizzle-kit push", "build": "bun build --compile --minify-whitespace --minify-syntax --target bun --outfile server ./src/index.ts" }, "dependencies": { diff --git a/src/api/routes/note/note.controller.ts b/src/api/routes/note/note.controller.ts index 2bb08a5..50b75a4 100644 --- a/src/api/routes/note/note.controller.ts +++ b/src/api/routes/note/note.controller.ts @@ -1,10 +1,10 @@ -import { and, eq, notExists } from "drizzle-orm"; +import { and, eq, isNull } from "drizzle-orm"; import { db } from "../../../db"; import { note } from "../../../db/schema/note"; -import { CreateNote } from "./note.model"; +import { CreateNoteType } from "./note.model"; export class NoteController { - async createNote(new_note: CreateNote, ownerId: string) { + async createNote(new_note: CreateNoteType, ownerId: string) { const new_note_data = { ...new_note, ownerId: ownerId }; const result = await db .insert(note) @@ -35,7 +35,7 @@ export class NoteController { updatedAt: note.updatedAt, }) .from(note) - .where(and(eq(note.ownerId, ownerId), notExists(note.deletedAt))) + .where(and(eq(note.ownerId, ownerId), isNull(note.deletedAt))) .limit(limit).offset(offset) .execute(); return { @@ -60,7 +60,7 @@ export class NoteController { and( eq(note.id, noteId), eq(note.ownerId, ownerId), - notExists(note.deletedAt) + isNull(note.deletedAt) ) ) .execute(); @@ -74,7 +74,7 @@ export class NoteController { async updateNoteById( noteId: string, - updated_note: CreateNote, + updated_note: CreateNoteType, ownerId: string ) { const new_note_data = { ...updated_note, updatedAt: new Date() }; @@ -85,7 +85,7 @@ export class NoteController { and( eq(note.id, noteId), eq(note.ownerId, ownerId), - notExists(note.deletedAt) + isNull(note.deletedAt) ) ) .returning({ @@ -113,7 +113,7 @@ export class NoteController { and( eq(note.id, noteId), eq(note.ownerId, ownerId), - notExists(note.deletedAt) + isNull(note.deletedAt) ) ) .execute(); @@ -129,7 +129,7 @@ export class NoteController { await db .update(note) .set({ deletedAt: new Date() }) - .where(and(eq(note.ownerId, ownerId), notExists(note.deletedAt))) + .where(and(eq(note.ownerId, ownerId), isNull(note.deletedAt))) .execute(); return { success: true, diff --git a/src/api/routes/note/note.model.ts b/src/api/routes/note/note.model.ts index 4a253bc..5ca3d38 100644 --- a/src/api/routes/note/note.model.ts +++ b/src/api/routes/note/note.model.ts @@ -1,37 +1,43 @@ import { createSelectSchema } from "drizzle-typebox"; import { t } from "elysia"; import { note } from "../../../db/schema/note"; -import { InferInsertModel, InferSelectModel } from "drizzle-orm"; +import { InferInsertModel } from "drizzle-orm"; +import { commonResponses } from "../../../lib/utils/common"; +export const _NoteSchema = createSelectSchema(note); -export const SelectNoteSchema = createSelectSchema(note); +export const NoteSchema = t.Omit(_NoteSchema, ["deletedAt", "ownerId"]); -export const NoteSchema = t.Omit(SelectNoteSchema, ["deletedAt", "ownerId"]); - -export type Note = InferSelectModel; -export type CreateNote = Pick< +export type CreateNoteType = Pick< InferInsertModel, "title" | "content" >; + export const createNoteSchema = t.Pick(NoteSchema, [ "title", "content", ]); -export const successGetNoteResponse = t.Object({ +export const getNoteResponses = { + 200: t.Object({ success:t.Boolean({default:true}), data: t.Array(NoteSchema), error: t.Null(), message: t.String() }, { description:"Success" -}) +}) , + ...commonResponses +} -export const successDeleteNoteResponse = t.Object({ +export const deleteNoteResponses = { + 200: t.Object({ success:t.Boolean({default:true}), data: t.Null(), error: t.Null(), message: t.String({default:"Note deletion succesful"}) }, { description:"Success" -}) \ No newline at end of file +}), + ...commonResponses +} \ No newline at end of file diff --git a/src/api/routes/note/note.route.ts b/src/api/routes/note/note.route.ts index 56975ee..3863e6d 100644 --- a/src/api/routes/note/note.route.ts +++ b/src/api/routes/note/note.route.ts @@ -1,8 +1,7 @@ -import { Elysia, error, t } from "elysia"; -import { createNoteSchema, NoteSchema, successDeleteNoteResponse, successGetNoteResponse } from "./note.model"; +import { Elysia, t } from "elysia"; +import { createNoteSchema, deleteNoteResponses, getNoteResponses, NoteSchema } from "./note.model"; import { NoteController } from "./note.controller"; import { userMiddleware } from "../../../middlewares/auth-middleware"; -import { commonResponses } from "../../../lib/utils/common"; export const noteRouter = new Elysia({ prefix: "/note", @@ -37,10 +36,7 @@ export const noteRouter = new Elysia({ limit: t.Optional(t.Number()), offset: t.Optional(t.Number()) }), - response:{ - 200: successGetNoteResponse, - ...commonResponses - }, + response:getNoteResponses, detail:{ "description":"Get all notes of the user", "summary":"Get all notes" @@ -56,10 +52,7 @@ export const noteRouter = new Elysia({ params:t.Object({ id: t.String(), }), - response: { - 200: successGetNoteResponse, - ...commonResponses - }, + response: getNoteResponses, detail:{ "description":"Get a note by Id", "summary":"Get a note" @@ -73,10 +66,7 @@ export const noteRouter = new Elysia({ }, { body: createNoteSchema, - response: { - 200: successGetNoteResponse, - ...commonResponses - }, + response: getNoteResponses, detail:{ "description":"Create a new note", "summary":"Create a note" @@ -92,10 +82,7 @@ export const noteRouter = new Elysia({ params:t.Object({ id: t.String(), }), - response: { - 200: successGetNoteResponse, - ...commonResponses - }, + response: getNoteResponses, detail:{ "description":"Update a note by Id", "summary":"Update a note" @@ -110,10 +97,7 @@ export const noteRouter = new Elysia({ params:t.Object({ id: t.String(), }), - response: { - 200: successDeleteNoteResponse, - ...commonResponses - }, + response: deleteNoteResponses, detail:{ "description":"Delete a note by Id", "summary":"Delete a note" @@ -126,10 +110,7 @@ export const noteRouter = new Elysia({ return await note.deleteAllNotes(user.id); }, { - response: { - 200: successDeleteNoteResponse, - ...commonResponses - }, + response: deleteNoteResponses, detail:{ "description":"Delete all notes of an user", "summary":"Delete all notes"