Add canvas interface (#461)

This commit is contained in:
Jeremy 2024-10-30 16:01:24 +05:30 committed by GitHub
parent 1a74a5ca9a
commit b3cb0ea755
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
44 changed files with 7454 additions and 4691 deletions

View file

@ -1,11 +1,11 @@
"server-only";
import { genSaltSync, hashSync } from "bcrypt-ts";
import { desc, eq } from "drizzle-orm";
import { and, asc, desc, eq, gt } from "drizzle-orm";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { user, chat, User } from "./schema";
import { user, chat, User, document, Suggestion } from "./schema";
// Optionally, if not using email/pass login, you can
// use the Drizzle adapter for Auth.js / NextAuth
@ -98,3 +98,108 @@ export async function getChatById({ id }: { id: string }) {
throw error;
}
}
export async function saveDocument({
id,
title,
content,
userId,
}: {
id: string;
title: string;
content: string;
userId: string;
}) {
try {
return await db.insert(document).values({
id,
title,
content,
userId,
createdAt: new Date(),
});
} catch (error) {
console.error("Failed to save document in database");
throw error;
}
}
export async function getDocumentsById({ id }: { id: string }) {
try {
const documents = await db
.select()
.from(document)
.where(eq(document.id, id))
.orderBy(asc(document.createdAt));
return documents;
} catch (error) {
console.error("Failed to get document by id from database");
throw error;
}
}
export async function getDocumentById({ id }: { id: string }) {
try {
const [selectedDocument] = await db
.select()
.from(document)
.where(eq(document.id, id))
.orderBy(desc(document.createdAt));
return selectedDocument;
} catch (error) {
console.error("Failed to get document by id from database");
throw error;
}
}
export async function deleteDocumentsByIdAfterTimestamp({
id,
timestamp,
}: {
id: string;
timestamp: Date;
}) {
try {
return await db
.delete(document)
.where(and(eq(document.id, id), gt(document.createdAt, timestamp)));
} catch (error) {
console.error(
"Failed to delete documents by id after timestamp from database",
);
throw error;
}
}
export async function saveSuggestions({
suggestions,
}: {
suggestions: Array<Suggestion>;
}) {
try {
return await db.insert(Suggestion).values(suggestions);
} catch (error) {
console.error("Failed to save suggestions in database");
throw error;
}
}
export async function getSuggestionsByDocumentId({
documentId,
}: {
documentId: string;
}) {
try {
return await db
.select()
.from(Suggestion)
.where(and(eq(Suggestion.documentId, documentId)));
} catch (error) {
console.error(
"Failed to get suggestions by document version from database",
);
throw error;
}
}