feat: How to create a session

This commit is contained in:
Wanjohi
2025-11-04 00:09:31 +03:00
parent 6947230a1d
commit 86959f93d9
4 changed files with 104 additions and 27 deletions

View File

@@ -1,5 +1,7 @@
import { z } from "zod/v4";
import { fn } from "../utils/fn.js";
import { Identifier } from "../id.js";
import { and, eq, isNull } from "drizzle-orm";
import { sessionTable } from "./session.sql.js";
import {
MachinePresetName,
@@ -12,6 +14,8 @@ import {
type Session as SessionInterface,
type Config as SessionConfig,
} from "./types.js";
import { Actor } from "../actor.js";
import { Database } from "../drizzle/index.js";
// Re-export all types and schemas from types.ts
export {
@@ -49,9 +53,43 @@ export namespace Session {
run: (ctx: any) => Promise<any>;
};
export const list = fn(z.void(), () => {
// Placeholder implementation
});
export const create = fn(Info.partial({ id: true }), (input) =>
Database.transaction(async (tx) => {
const id = Identifier.descending("session", input.id); // Placeholder for Identifier.ascending
await tx
.insert(sessionTable)
.values({
id,
ownerID: Actor.userID(),
...input,
})
.onConflictDoUpdate({
target: sessionTable.id,
set: {
timeDeleted: null,
},
});
return id;
}),
);
export const list = fn(z.void(), () =>
Database.use((tx) =>
tx
.select()
.from(sessionTable)
.where(
and(
eq(sessionTable.ownerID, Actor.userID()),
isNull(sessionTable.timeDeleted),
),
)
.limit(100)
.then((rows) => rows.map(serialize)),
),
);
export function serialize(
input: typeof sessionTable.$inferSelect,

View File

@@ -1,4 +1,4 @@
import { id, timestamps } from "../drizzle/types.js";
import { id, timestamps, ulid } from "../drizzle/types.js";
import {
pgTable,
varchar,
@@ -20,11 +20,17 @@ import {
AudioRateControl,
VideoBitDepth,
} from "./types.js";
import { userTable } from "../user/user.sql.js";
export const sessionTable = pgTable("sessions", {
...id,
...timestamps,
description: text("description"),
ownerID: ulid("owner_id")
.references(() => userTable.id, {
onDelete: "cascade",
})
.notNull(),
retry: jsonb("retry").$type<RetryOptions>(),
queue: jsonb("queue").$type<Queue>(),
machine: varchar("machine", { length: 200 }).$type<MachinePresetName>(),