lib/db/queries.ts and are marked server-only — they can only be imported in Server Components, Server Actions, or Route Handlers.
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.
The email address to search for.
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.
The email address for the new account.
The plain-text password. It is hashed with
bcrypt-ts using a salt factor of 10 before being stored.Chat queries
saveChat
Inserts a new chat row. createdAt is set to new Date() by the function.
The UUID to assign to the chat.
The UUID of the owning user. Must reference an existing
User.id.The title of the chat.
deleteChatById
Deletes a chat and all associated votes and messages in the correct dependency order.
The UUID of the chat to delete.
- Delete all
Voterows wherechatId = id. - Delete all
Messagerows wherechatId = id. - Delete the
Chatrow itself.
chat delete.
getChatsByUserId
Fetches all chats for a user, ordered by createdAt descending (newest first).
The UUID of the user.
Chat objects.
getChatById
Fetches a single chat by its ID.
The UUID of the chat.
Chat object, or undefined if no row is found.
updateChatVisiblityById
Updates the visibility field of a chat.
The UUID of the chat to update.
The new visibility value.
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.
The messages to insert. Each element must conform to the
Message type from lib/db/schema.ts.getMessagesByChatId
Fetches all messages for a chat, ordered by createdAt ascending (oldest first).
The UUID of the chat.
Message objects.
getMessageById
Fetches messages matching a specific message ID.
The UUID of the message.
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.
The UUID of the chat.
Messages with
createdAt >= timestamp are deleted.- Deletes matching
Voterows. - Deletes the
Messagerows.
undefined if there are no matching messages.
Vote queries
voteMessage
Creates or updates a vote on a message (upsert behaviour).
The UUID of the chat the message belongs to.
The UUID of the message being voted on.
The vote direction.
'up' sets isUpvoted = true; 'down' sets isUpvoted = false.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.
The UUID of the chat.
Vote objects.
Document queries
saveDocument
Inserts a new document version. createdAt is set to new Date() by the function.
The UUID for the document. Multiple versions share the same
id.The document title.
The artifact kind (
'text', 'code', 'image', or 'sheet'). Defined in components/artifact.The document body.
The UUID of the owning user.
getDocumentsById
Fetches all versions of a document by ID, ordered by createdAt ascending (oldest version first).
The UUID shared by all versions of the document.
Document objects, one per version.
getDocumentById
Fetches the latest version of a document by ID.
The UUID of the document.
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.
The document UUID.
Document versions with
createdAt > timestamp are deleted.- Delete
Suggestionrows wheredocumentId = id AND documentCreatedAt > timestamp. - Delete
Documentrows whereid = id AND createdAt > timestamp.
Suggestion queries
saveSuggestions
Bulk-inserts an array of suggestions.
The suggestions to insert. Each element must conform to the
Suggestion type from lib/db/schema.ts.getSuggestionsByDocumentId
Fetches all suggestions for a specific document ID.
The UUID of the document. Matches against
Suggestion.documentId.Suggestion objects across all versions of the document.