can sign in and out
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user