Skip to main content
All query functions are defined in lib/db/queries.ts and are marked server-only — they can only be imported in Server Components, Server Actions, or Route Handlers.
import 'server-only';
Every function opens a query against the shared postgres-js client and throws on error after logging to console.error.
The database connection is initialised once at module load time using process.env.POSTGRES_URL. Make sure this environment variable is set before the module is imported.

User queries

getUser

Looks up a user by email address.
export async function getUser(email: string): Promise<Array<User>>
email
string
required
The email address to search for.
Returns an array of User objects. An empty array means no matching account exists. In practice at most one row is returned because email is unique by application convention.

createUser

Inserts a new user with a bcrypt-hashed password.
export async function createUser(email: string, password: string)
email
string
required
The email address for the new account.
password
string
required
The plain-text password. It is hashed with bcrypt-ts using a salt factor of 10 before being stored.
Returns the Drizzle insert result.
Never pass an already-hashed password. The function always calls hashSync internally before inserting.

Chat queries

saveChat

Inserts a new chat row. createdAt is set to new Date() by the function.
export async function saveChat({
  id,
  userId,
  title,
}: {
  id: string;
  userId: string;
  title: string;
})
id
string
required
The UUID to assign to the chat.
userId
string
required
The UUID of the owning user. Must reference an existing User.id.
title
string
required
The title of the chat.
Returns the Drizzle insert result.

deleteChatById

Deletes a chat and all associated votes and messages in the correct dependency order.
export async function deleteChatById({ id }: { id: string })
id
string
required
The UUID of the chat to delete.
Deletion order:
  1. Delete all Vote rows where chatId = id.
  2. Delete all Message rows where chatId = id.
  3. Delete the Chat row itself.
Returns the result of the final chat delete.

getChatsByUserId

Fetches all chats for a user, ordered by createdAt descending (newest first).
export async function getChatsByUserId({ id }: { id: string })
id
string
required
The UUID of the user.
Returns an array of Chat objects.

getChatById

Fetches a single chat by its ID.
export async function getChatById({ id }: { id: string })
id
string
required
The UUID of the chat.
Returns the first matching Chat object, or undefined if no row is found.

updateChatVisiblityById

Updates the visibility field of a chat.
export async function updateChatVisiblityById({
  chatId,
  visibility,
}: {
  chatId: string;
  visibility: 'private' | 'public';
})
chatId
string
required
The UUID of the chat to update.
visibility
'private' | 'public'
required
The new visibility value.
Returns the Drizzle update result.
The function name contains a typo (Visiblity) that matches the source. Import it using the exact spelling.

Message queries

saveMessages

Bulk-inserts an array of messages.
export async function saveMessages({
  messages,
}: {
  messages: Array<Message>;
})
messages
Array<Message>
required
The messages to insert. Each element must conform to the Message type from lib/db/schema.ts.
Returns the Drizzle insert result.

getMessagesByChatId

Fetches all messages for a chat, ordered by createdAt ascending (oldest first).
export async function getMessagesByChatId({ id }: { id: string })
id
string
required
The UUID of the chat.
Returns an array of Message objects.

getMessageById

Fetches messages matching a specific message ID.
export async function getMessageById({ id }: { id: string })
id
string
required
The UUID of the message.
Returns an array of Message objects (typically a single element).

deleteMessagesByChatIdAfterTimestamp

Deletes messages in a chat that were created at or after a given timestamp, along with their associated votes.
export async function deleteMessagesByChatIdAfterTimestamp({
  chatId,
  timestamp,
}: {
  chatId: string;
  timestamp: Date;
})
chatId
string
required
The UUID of the chat.
timestamp
Date
required
Messages with createdAt >= timestamp are deleted.
The function first collects the IDs of messages to be deleted, then:
  1. Deletes matching Vote rows.
  2. Deletes the Message rows.
Returns the result of the final message delete, or undefined if there are no matching messages.

Vote queries

voteMessage

Creates or updates a vote on a message (upsert behaviour).
export async function voteMessage({
  chatId,
  messageId,
  type,
}: {
  chatId: string;
  messageId: string;
  type: 'up' | 'down';
})
chatId
string
required
The UUID of the chat the message belongs to.
messageId
string
required
The UUID of the message being voted on.
type
'up' | 'down'
required
The vote direction. 'up' sets isUpvoted = true; 'down' sets isUpvoted = false.
If a vote row already exists for the messageId, its isUpvoted value is updated. Otherwise a new row is inserted. Returns the Drizzle update or insert result.

getVotesByChatId

Fetches all votes for a chat.
export async function getVotesByChatId({ id }: { id: string })
id
string
required
The UUID of the chat.
Returns an array of Vote objects.

Document queries

saveDocument

Inserts a new document version. createdAt is set to new Date() by the function.
export async function saveDocument({
  id,
  title,
  kind,
  content,
  userId,
}: {
  id: string;
  title: string;
  kind: ArtifactKind;
  content: string;
  userId: string;
})
id
string
required
The UUID for the document. Multiple versions share the same id.
title
string
required
The document title.
kind
ArtifactKind
required
The artifact kind ('text', 'code', 'image', or 'sheet'). Defined in components/artifact.
content
string
required
The document body.
userId
string
required
The UUID of the owning user.
Returns the Drizzle insert result.

getDocumentsById

Fetches all versions of a document by ID, ordered by createdAt ascending (oldest version first).
export async function getDocumentsById({ id }: { id: string })
id
string
required
The UUID shared by all versions of the document.
Returns an array of Document objects, one per version.

getDocumentById

Fetches the latest version of a document by ID.
export async function getDocumentById({ id }: { id: string })
id
string
required
The UUID of the document.
Orders results by createdAt descending and returns the first row. Returns the most recent Document object, or undefined if none exists.

deleteDocumentsByIdAfterTimestamp

Deletes document versions (and their suggestions) created after a given timestamp.
export async function deleteDocumentsByIdAfterTimestamp({
  id,
  timestamp,
}: {
  id: string;
  timestamp: Date;
})
id
string
required
The document UUID.
timestamp
Date
required
Document versions with createdAt > timestamp are deleted.
Deletion order:
  1. Delete Suggestion rows where documentId = id AND documentCreatedAt > timestamp.
  2. Delete Document rows where id = id AND createdAt > timestamp.
Returns the result of the final document delete.

Suggestion queries

saveSuggestions

Bulk-inserts an array of suggestions.
export async function saveSuggestions({
  suggestions,
}: {
  suggestions: Array<Suggestion>;
})
suggestions
Array<Suggestion>
required
The suggestions to insert. Each element must conform to the Suggestion type from lib/db/schema.ts.
Returns the Drizzle insert result.

getSuggestionsByDocumentId

Fetches all suggestions for a specific document ID.
export async function getSuggestionsByDocumentId({
  documentId,
}: {
  documentId: string;
})
documentId
string
required
The UUID of the document. Matches against Suggestion.documentId.
Returns an array of Suggestion objects across all versions of the document.