Skip to content

Latest commit

 

History

History
236 lines (183 loc) · 5.65 KB

File metadata and controls

236 lines (183 loc) · 5.65 KB

Appwrite Integration Complete

Overview

Complete rewrite of Appwrite integration to match the new database structure with two separate databases: whisperrnote for users and chat for all chat features.

Architecture Changes

Database Structure

  • whisperrnote Database (67ff05a9000296822396)

    • users collection (shared with base app)
  • chat Database (chat)

    • conversations, messages, contacts
    • stories, posts, follows
    • wallets, tokenHoldings
    • stickers, gifs, polls, etc.

Key Changes

  1. ✅ Moved from tablesDB API to databases API
  2. ✅ Updated all database/collection IDs from env variables
  3. ✅ Separated WHISPERRNOTE and CHAT collections
  4. ✅ Removed all hardcoded values
  5. ✅ Created clean service architecture

Files Updated

Configuration Files

  • env.sample - Complete rewrite with actual IDs
  • src/lib/appwrite/config/constants.ts - New database structure
  • src/lib/appwrite/config/client.ts - Updated to use Databases API
  • src/lib/appwrite/config/index.ts - Central config export

Service Files (Completely Rewritten)

  • src/lib/appwrite/services/auth.service.ts - Authentication
  • src/lib/appwrite/services/user.service.ts - User management (whisperrnote DB)
  • src/lib/appwrite/services/messaging.service.ts - Conversations & messages
  • src/lib/appwrite/services/contacts.service.ts - Contacts management
  • src/lib/appwrite/services/storage.service.ts - File uploads
  • src/lib/appwrite/services/index.ts - Services export

Main Export

  • src/lib/appwrite/index.ts - Central export for all Appwrite features

Environment Variables

Required (Must be set)

VITE_APPWRITE_ENDPOINT=https://fra.cloud.appwrite.io/v1
VITE_APPWRITE_PROJECT_ID=your-project-id

Database IDs (Pre-configured)

VITE_DATABASE_WHISPERRNOTE=67ff05a9000296822396
VITE_DATABASE_CHAT=chat

Collection IDs (Pre-configured)

All collection IDs are set in env.sample with their actual values from appwrite.config.json.

Usage Examples

Import Services

import { 
  authService, 
  userService, 
  messagingService, 
  contactsService, 
  storageService 
} from '@/lib/appwrite';

Authentication

// Login
const session = await authService.login(email, password);

// Get current user
const user = await authService.getCurrentUser();

// Logout
await authService.logout();

User Management

// Get user from whisperrnote database
const user = await userService.getUser(userId);

// Search users
const users = await userService.searchUsers('john');

Messaging

// Create conversation
const conversation = await messagingService.createConversation({
  type: 'direct',
  creatorId: userId,
  participantIds: [userId, otherUserId],
});

// Send message
const message = await messagingService.sendMessage({
  conversationId: conversation.$id,
  senderId: userId,
  content: 'Hello!',
  contentType: 'text',
});

// Get messages
const messages = await messagingService.getConversationMessages(conversationId);

Storage

// Upload message attachment
const file = await storageService.uploadMessageAttachment(fileObject);

// Upload voice message
const voice = await storageService.uploadVoiceMessage(voiceFile);

// Get file URL
const url = storageService.getFileView(BUCKET_IDS.MESSAGES, fileId);

Configuration Validation

Check Configuration

import { isConfigurationValid, getMissingEnvVars } from '@/lib/appwrite';

if (!isConfigurationValid()) {
  const missing = getMissingEnvVars();
  console.error('Missing env vars:', missing);
}

Type Safety

All services include proper TypeScript interfaces:

  • User - User document from whisperrnote DB
  • Conversation - Conversation document from chat DB
  • Message - Message document from chat DB
  • Contact - Contact document from chat DB

Migration Guide

Old Code

import { tablesDB } from '@/lib/appwrite/config/client';
import { DATABASE_IDS, MAIN_COLLECTIONS } from '@/lib/appwrite/config/constants';

// Old API
const profile = await tablesDB.getRow({
  databaseId: DATABASE_IDS.MAIN,
  tableId: MAIN_COLLECTIONS.PROFILES,
  rowId: userId
});

New Code

import { userService } from '@/lib/appwrite';

// New API
const user = await userService.getUser(userId);

Benefits

Clean Architecture

  • Separate services for each domain
  • Clear separation between whisperrnote and chat databases
  • Type-safe interfaces

No Hardcoding

  • All IDs from environment variables
  • Easy to change configurations
  • No magic strings

Developer Experience

  • Simple, intuitive API
  • Full TypeScript support
  • Comprehensive error handling

Production Ready

  • Proper error logging
  • Validation helpers
  • Secure configuration

Next Steps

  1. ✅ Copy env.sample to .env
  2. ✅ Add your VITE_APPWRITE_PROJECT_ID
  3. ✅ Test authentication flow
  4. ✅ Test messaging features
  5. ✅ Update frontend components to use new services

Backup

Old service files backed up to:

src/lib/appwrite/services/backup/

Testing

Test configuration:

import { isConfigured, getConfig } from '@/lib/appwrite';

console.log('Configured:', isConfigured());
console.log('Config:', getConfig());

Test services:

// Test auth
const user = await authService.getCurrentUser();
console.log('Current user:', user);

// Test database access
const users = await userService.searchUsers('test');
console.log('Found users:', users);

Status

✅ Configuration complete ✅ Services implemented ✅ Type definitions added ✅ Documentation complete ✅ Ready for MVP development