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;
}
}

View file

@ -1,24 +1,80 @@
import { Message } from "ai";
import { InferSelectModel } from "drizzle-orm";
import { pgTable, varchar, timestamp, json, uuid } from "drizzle-orm/pg-core";
import { Message } from 'ai';
import { InferSelectModel } from 'drizzle-orm';
import {
pgTable,
varchar,
timestamp,
json,
uuid,
text,
primaryKey,
foreignKey,
boolean,
} from 'drizzle-orm/pg-core';
export const user = pgTable("User", {
id: uuid("id").primaryKey().notNull().defaultRandom(),
email: varchar("email", { length: 64 }).notNull(),
password: varchar("password", { length: 64 }),
export const user = pgTable('User', {
id: uuid('id').primaryKey().notNull().defaultRandom(),
email: varchar('email', { length: 64 }).notNull(),
password: varchar('password', { length: 64 }),
});
export type User = InferSelectModel<typeof user>;
export const chat = pgTable("Chat", {
id: uuid("id").primaryKey().notNull().defaultRandom(),
createdAt: timestamp("createdAt").notNull(),
messages: json("messages").notNull(),
userId: uuid("userId")
export const chat = pgTable('Chat', {
id: uuid('id').primaryKey().notNull().defaultRandom(),
createdAt: timestamp('createdAt').notNull(),
messages: json('messages').notNull(),
userId: uuid('userId')
.notNull()
.references(() => user.id),
});
export type Chat = Omit<InferSelectModel<typeof chat>, "messages"> & {
export type Chat = Omit<InferSelectModel<typeof chat>, 'messages'> & {
messages: Array<Message>;
};
export const document = pgTable(
'Document',
{
id: uuid('id').notNull().defaultRandom(),
createdAt: timestamp('createdAt').notNull(),
title: text('title').notNull(),
content: text('content'),
userId: uuid('userId')
.notNull()
.references(() => user.id),
},
(table) => {
return {
pk: primaryKey({ columns: [table.id, table.createdAt] }),
};
}
);
export type Document = InferSelectModel<typeof document>;
export const Suggestion = pgTable(
'Suggestion',
{
id: uuid('id').notNull().defaultRandom(),
documentId: uuid('documentId').notNull(),
documentCreatedAt: timestamp('documentCreatedAt').notNull(),
originalText: text('originalText').notNull(),
suggestedText: text('suggestedText').notNull(),
description: text('description'),
isResolved: boolean('isResolved').notNull().default(false),
userId: uuid('userId')
.notNull()
.references(() => user.id),
createdAt: timestamp('createdAt').notNull(),
},
(table) => ({
pk: primaryKey({ columns: [table.id] }),
documentRef: foreignKey({
columns: [table.documentId, table.documentCreatedAt],
foreignColumns: [document.id, document.createdAt],
}),
})
);
export type Suggestion = InferSelectModel<typeof Suggestion>;