can sign in and out

This commit is contained in:
Avraham Sakal
2025-09-18 10:05:20 -04:00
parent 6d80d3a9a6
commit 83e3f867dd
6 changed files with 204 additions and 12 deletions
+10
View File
@@ -1,4 +1,5 @@
import type { CommittedMessage } from "../types";
import type { Users } from "./generated/public/Users";
export type Conversation = {
id: string;
@@ -25,6 +26,10 @@ export type FactTrigger = {
createdAt?: string;
};
export type User = Omit<Users, "id"> & {
id: string;
};
export interface Entity<T> {
construct: (data: T) => T;
create: (data: Omit<T, "id">) => Promise<T>;
@@ -54,9 +59,14 @@ export type FactTriggerEntity = Entity<FactTrigger> & {
findByConversationId: (conversationId: string) => Promise<Array<FactTrigger>>;
};
export type UserEntity = Entity<User> & {
findByEmailAddress: (emailAddress: string) => Promise<User | undefined>;
};
export interface ApplicationDatabase {
conversations: ConversationEntity;
factTriggers: FactTriggerEntity;
facts: FactEntity;
messages: MessageEntity;
users: UserEntity;
}
+52
View File
@@ -8,6 +8,7 @@ import type {
FactEntity,
FactTriggerEntity,
MessageEntity,
UserEntity,
} from "./common.ts";
import type { CommittedMessage } from "../types";
@@ -262,11 +263,62 @@ export function getDb(POSTGRES_CONNECTION_STRING: string) {
},
};
const users: UserEntity = {
construct: (user) => user,
create: async (user) => {
const insertedRows = await dbClient
.insertInto("users")
.values(user)
.returningAll()
.execute();
return insertedRows[0];
},
createMany: async (users) => {
const insertedRows = await dbClient
.insertInto("users")
.values(users)
.returningAll()
.execute();
return insertedRows;
},
findAll: async () => {
const rows = await dbClient.selectFrom("users").selectAll().execute();
return rows;
},
findById: async (id) => {
const row = await dbClient
.selectFrom("users")
.selectAll()
.where("id", "=", id)
.execute();
return row[0];
},
update: async (id, data) => {
await dbClient
.updateTable("users")
.set(data)
.where("id", "=", id)
.execute();
},
delete: async (id) => {
await dbClient.deleteFrom("users").where("id", "=", id).execute();
},
findByEmailAddress: async (emailAddress) => {
const row = await dbClient
.selectFrom("users")
.selectAll()
.where("email", "=", emailAddress)
.executeTakeFirst();
return row;
},
};
const db = {
conversations,
facts,
factTriggers,
messages,
users,
};
return db;