Skip to content

fix: update Drizzle relations and inferred types for new message model#253

Open
onyemaechiezekiel9 wants to merge 2 commits into
codebestia:mainfrom
onyemaechiezekiel9:fix/drizzle-message-relations
Open

fix: update Drizzle relations and inferred types for new message model#253
onyemaechiezekiel9 wants to merge 2 commits into
codebestia:mainfrom
onyemaechiezekiel9:fix/drizzle-message-relations

Conversation

@onyemaechiezekiel9

Copy link
Copy Markdown

fix: update Drizzle relations and inferred types for new message model

Closes #179


Summary

Updates the messages schema to support the new end-to-end encryption model: content is removed from the messages table and moved to a new messageEnvelopes table. Drizzle relations are wired up, inferred types replace hand-written ones, and all consumers are updated to query with { envelopes: true, senderDevice: true }.


Changes

schema.ts

  • Added senderDeviceId FK to messages table linking to userDevices
  • Removed content column and its messages_content_search_idx index
  • Added messageEnvelopes table: id (UUID PK), messageId (FK), content, createdAt
  • Added messagesRelations: senderDevice (one-to-one → userDevices) + envelopes (one-to-many → messageEnvelopes)
  • Added messageEnvelopesRelations: reverse relation back to messages
  • Added userDevicesRelations: devices to their sent messages

Type exports

  • Message and NewMessage now use typeof messages.$inferSelect / $inferInsert
  • Added MessageEnvelope and NewMessageEnvelope inferred types

lib/messages.ts

  • serializeMessage() extracts content from envelopes array
  • Handles deleted messages (content set to null)
  • Backward-compatible serialization API

socket/messaging.ts

  • send_message: fetches sender's active device, creates message + messageEnvelope separately
  • message_history: queries with { envelopes: true, senderDevice: true }
  • ask_assistant: creates assistant device if needed, uses new message structure

routes/conversations.ts

  • Search SQL updated to JOIN messageEnvelopes instead of querying message.content
  • GET /conversations/:id/messages fetches with { envelopes: true, senderDevice: true }
  • Latest message in conversation relations includes envelopes and senderDevice

Queryability

The following now compiles and is queryable:

db.query.messages.findMany({
  with: {
    envelopes: true,
    senderDevice: true,
  },
});

Security Notes

  • content is now stored in messageEnvelopes, decoupled from the message record — supporting per-recipient envelope encryption
  • No as any or @ts-ignore suppressions introduced anywhere
  • All types are Drizzle-inferred — schema changes automatically propagate to type consumers

@drips-wave

drips-wave Bot commented Jun 27, 2026

Copy link
Copy Markdown

@onyemaechiezekiel9 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update Drizzle relations + inferred types for the new message model

2 participants