Skip to content

Commit

Permalink
feat!: removes mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
JoFrost committed Sep 20, 2023
1 parent a3bed09 commit f4ef30a
Show file tree
Hide file tree
Showing 23 changed files with 77 additions and 440 deletions.
1 change: 0 additions & 1 deletion .env.local
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Use .env.local to change these variables
# DO NOT EDIT THIS FILE WITH SENSITIVE DATA

MONGODB_URL=mongodb://localhost:27017
PUBLIC_APP_NAME=BlindChat
PUBLIC_APP_DATA_SHARING=false
150 changes: 2 additions & 148 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"highlight.js": "^11.7.0",
"jsdom": "^22.0.0",
"marked": "^4.3.0",
"mongodb": "^5.3.0",
"nanoid": "^4.0.2",
"openid-client": "^5.4.2",
"parquetjs": "^0.11.2",
Expand Down
6 changes: 0 additions & 6 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ export const handle: Handle = async ({ event, resolve }) => {

event.locals.sessionId = token || crypto.randomUUID();

const user = await collections.users.findOne({ sessionId: event.locals.sessionId });

if (user) {
event.locals.user = user;
}

function errorResponse(status: number, message: string) {
const sendJson =
event.request.headers.get("accept")?.includes("application/json") ||
Expand Down
1 change: 0 additions & 1 deletion src/lib/buildPrompt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { BackendModel } from "./server/models";
import type { Message } from "./types/Message";
import { collections } from "$lib/server/database";
import { ObjectId } from "mongodb";
import { authCondition } from "./server/auth";
/**
* Convert [{user: "assistant", content: "hi"}, {user: "user", content: "hello"}] to:
Expand Down
65 changes: 11 additions & 54 deletions src/lib/server/database.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
import { MONGODB_URL, MONGODB_DB_NAME, MONGODB_DIRECT_CONNECTION } from "$env/static/private";
import { MongoClient } from "mongodb";
import type { Conversation } from "$lib/types/Conversation";
import type { SharedConversation } from "$lib/types/SharedConversation";
import type { WebSearch } from "$lib/types/WebSearch";
import type { AbortedGeneration } from "$lib/types/AbortedGeneration";
import type { Settings } from "$lib/types/Settings";
import type { User } from "$lib/types/User";
import type { MessageEvent } from "$lib/types/MessageEvent";
let client = undefined
export const connectPromise = undefined

if (!MONGODB_URL) {
throw new Error(
"Please specify the MONGODB_URL environment variable inside .env.local. Set it to mongodb://localhost:27017 if you are running MongoDB locally, or to a MongoDB Atlas free instance for example."
);
}
const db = undefined

const client = new MongoClient(MONGODB_URL, {
directConnection: MONGODB_DIRECT_CONNECTION === "true",
});

export const connectPromise = client.connect().catch(console.error);

const db = client.db(MONGODB_DB_NAME + (import.meta.env.MODE === "test" ? "-test" : ""));

const conversations = db.collection<Conversation>("conversations");
const sharedConversations = db.collection<SharedConversation>("sharedConversations");
const abortedGenerations = db.collection<AbortedGeneration>("abortedGenerations");
const settings = db.collection<Settings>("settings");
const users = db.collection<User>("users");
const webSearches = db.collection<WebSearch>("webSearches");
const messageEvents = db.collection<MessageEvent>("messageEvents");
const conversations = undefined
const sharedConversations = undefined
const abortedGenerations = undefined
const settings = undefined
const users = undefined
const webSearches = undefined
const messageEvents = undefined

export { client, db };
export const collections = {
Expand All @@ -39,28 +20,4 @@ export const collections = {
users,
webSearches,
messageEvents,
};

client.on("open", () => {
conversations
.createIndex(
{ sessionId: 1, updatedAt: -1 },
{ partialFilterExpression: { sessionId: { $exists: true } } }
)
.catch(console.error);
conversations
.createIndex(
{ userId: 1, updatedAt: -1 },
{ partialFilterExpression: { userId: { $exists: true } } }
)
.catch(console.error);
webSearches.createIndex({ sessionId: 1, updatedAt: -1 }).catch(console.error);
abortedGenerations.createIndex({ updatedAt: 1 }, { expireAfterSeconds: 30 }).catch(console.error);
abortedGenerations.createIndex({ conversationId: 1 }, { unique: true }).catch(console.error);
sharedConversations.createIndex({ hash: 1 }, { unique: true }).catch(console.error);
settings.createIndex({ sessionId: 1 }, { unique: true, sparse: true }).catch(console.error);
settings.createIndex({ userId: 1 }, { unique: true, sparse: true }).catch(console.error);
users.createIndex({ hfUserId: 1 }, { unique: true }).catch(console.error);
users.createIndex({ sessionId: 1 }, { unique: true, sparse: true }).catch(console.error);
messageEvents.createIndex({ createdAt: 1 }, { expireAfterSeconds: 60 }).catch(console.error);
});
};
3 changes: 0 additions & 3 deletions src/lib/types/Conversation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { ObjectId } from "mongodb";
import type { Message } from "./Message";
import type { Timestamps } from "./Timestamps";
import type { User } from "./User";

export interface Conversation extends Timestamps {
_id: ObjectId;

sessionId?: string;
userId?: User["_id"];

Expand Down
3 changes: 0 additions & 3 deletions src/lib/types/User.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { ObjectId } from "mongodb";
import type { Timestamps } from "./Timestamps";

export interface User extends Timestamps {
_id: ObjectId;

username?: string;
name: string;
email?: string;
Expand Down
5 changes: 0 additions & 5 deletions src/lib/types/WebSearch.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import type { ObjectId } from "mongodb";
import type { Conversation } from "./Conversation";
import type { Timestamps } from "./Timestamps";

export interface WebSearch extends Timestamps {
_id: ObjectId;

convId: Conversation["_id"];

prompt: string;

searchQuery: string;
Expand Down
Loading

0 comments on commit f4ef30a

Please sign in to comment.