From 5aa597186a8eae3e23d93e9d261b1ef3084df21a Mon Sep 17 00:00:00 2001 From: Steve Korshakov Date: Sun, 13 Jul 2025 20:11:44 -0700 Subject: [PATCH] feat: add metadata field to session --- .../20250714030924_add_session_metadata/migration.sql | 8 ++++++++ prisma/schema.prisma | 2 ++ sources/app/api.ts | 11 ++++++++--- sources/storage/types.ts | 1 + 4 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 prisma/migrations/20250714030924_add_session_metadata/migration.sql diff --git a/prisma/migrations/20250714030924_add_session_metadata/migration.sql b/prisma/migrations/20250714030924_add_session_metadata/migration.sql new file mode 100644 index 0000000..d0cadc4 --- /dev/null +++ b/prisma/migrations/20250714030924_add_session_metadata/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - Added the required column `metadata` to the `Session` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "Session" ADD COLUMN "metadata" TEXT NOT NULL; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 26af446..2ba8d25 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -37,10 +37,12 @@ model Session { tag String accountId String account Account @relation(fields: [accountId], references: [id]) + metadata String seq Int @default(0) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt messages SessionMessage[] + @@unique([accountId, tag]) } diff --git a/sources/app/api.ts b/sources/app/api.ts index 7f9fed3..a299bcc 100644 --- a/sources/app/api.ts +++ b/sources/app/api.ts @@ -160,13 +160,14 @@ export async function startApi() { typed.post('/v1/sessions', { schema: { body: z.object({ - tag: z.string() + tag: z.string(), + metadata: z.string() }) }, preHandler: app.authenticate }, async (request, reply) => { const userId = request.user.id; - const { tag } = request.body; + const { tag, metadata } = request.body; const session = await db.session.findFirst({ where: { @@ -179,6 +180,7 @@ export async function startApi() { session: { id: session.id, seq: session.seq, + metadata: session.metadata, createdAt: session.createdAt.getTime(), updatedAt: session.updatedAt.getTime() } @@ -201,7 +203,8 @@ export async function startApi() { const session = await tx.session.create({ data: { accountId: userId, - tag: tag + tag: tag, + metadata: metadata } }); @@ -210,6 +213,7 @@ export async function startApi() { t: 'new-session', id: session.id, seq: session.seq, + metadata: metadata, createdAt: session.createdAt.getTime(), updatedAt: session.updatedAt.getTime() }; @@ -238,6 +242,7 @@ export async function startApi() { session: { id: result.session.id, seq: result.session.seq, + metadata: result.session.metadata, createdAt: result.session.createdAt.getTime(), updatedAt: result.session.updatedAt.getTime() } diff --git a/sources/storage/types.ts b/sources/storage/types.ts index eb59f74..0e151c1 100644 --- a/sources/storage/types.ts +++ b/sources/storage/types.ts @@ -21,6 +21,7 @@ declare global { t: 'new-session'; id: string; seq: number; + metadata: string; createdAt: number; updatedAt: number; };