UNPKG

@budytalk/activity-server

Version:

Complete social media content management server with built-in PostgreSQL database, real-time features, and zero-configuration setup

190 lines (160 loc) • 7.09 kB
// PostgreSQL example using Neon database const { BudyTalkContent } = require('../dist/index'); async function postgresExample() { console.log('🐘 Testing BudyTalk Content Server with Neon PostgreSQL...\n'); try { // Initialize the content server with PostgreSQL const budytalk = new BudyTalkContent({ port: 3000, database: { type: 'postgres', url: process.env.DATABASE_URL || 'postgres://neondb_owner:npg_JsVijpTH5F2n@ep-restless-dream-a4b0o0sz-pooler.us-east-1.aws.neon.tech/neondb?sslmode=require' }, logging: { level: 'info', format: 'simple' } }); await budytalk.initialize(); await budytalk.start(); console.log('āœ… Content server started with Neon PostgreSQL on port 3000\n'); // Create user accounts console.log('šŸ“ Creating user accounts in PostgreSQL...'); const alice = await budytalk.syncUserAccount({ userId: 'postgres_alice_123', username: 'alice_postgres', displayName: 'Alice PostgreSQL', avatar: 'https://example.com/alice.jpg' }); const bob = await budytalk.syncUserAccount({ userId: 'postgres_bob_456', username: 'bob_postgres', displayName: 'Bob PostgreSQL', avatar: 'https://example.com/bob.jpg' }); console.log(`āœ… Created accounts: ${alice.displayName}, ${bob.displayName}\n`); // Bob follows Alice console.log('šŸ“ Setting up following relationship...'); await budytalk.followUser(bob.userId, alice.userId); console.log('āœ… Bob is now following Alice\n'); // Alice creates posts console.log('šŸ“ Creating posts in PostgreSQL...'); const textPost = await budytalk.createPost({ userId: alice.userId, content: 'Hello from Neon PostgreSQL! This data is stored in the cloud! ā˜ļøšŸ˜', postType: 'text', hashtags: ['#postgresql', '#neon', '#cloud', '#database'], mentions: ['@bob_postgres'] }); console.log(`āœ… Created text post: ${textPost.id}`); const pollPost = await budytalk.createPost({ userId: alice.userId, content: 'PostgreSQL vs SQLite: Which do you prefer for production?', postType: 'poll', poll: { question: 'Which database do you prefer for production?', options: [ { text: 'PostgreSQL' }, { text: 'SQLite' }, { text: 'MySQL' }, { text: 'MongoDB' } ], settings: { allowMultipleChoices: false, showResults: 'after_vote', duration: 48 } } }); console.log(`āœ… Created poll post: ${pollPost.id}\n`); // Bob interacts with posts console.log('šŸ“ Adding interactions...'); await budytalk.likePost(bob.userId, textPost.id); console.log('āœ… Bob liked Alice\'s post'); await budytalk.commentOnPost({ userId: bob.userId, postId: textPost.id, content: 'PostgreSQL is amazing! Neon makes it so easy to use! šŸš€', mentions: ['@alice_postgres'] }); console.log('āœ… Bob commented on Alice\'s post'); // Vote on poll if (pollPost.poll) { const postgresOption = pollPost.poll.options.find(opt => opt.text === 'PostgreSQL'); if (postgresOption) { await budytalk.voteOnPoll({ userId: bob.userId, pollId: pollPost.poll.id, optionIds: [postgresOption.id] }); console.log('āœ… Bob voted for PostgreSQL'); } } // Bookmark posts await budytalk.bookmarkPost({ userId: bob.userId, postId: textPost.id, collectionName: 'PostgreSQL Tips', notes: 'Great example of using Neon PostgreSQL' }); console.log('āœ… Bob bookmarked Alice\'s post\n'); // Check timeline console.log('šŸ“ Checking timeline and feeds...'); const bobTimeline = await budytalk.getTimeline(bob.userId, 10, 0); console.log(`āœ… Bob's timeline: ${bobTimeline.length} posts`); const aliceFeed = await budytalk.getUserFeed(alice.userId, 10, 0); console.log(`āœ… Alice's feed: ${aliceFeed.length} posts`); const trending = await budytalk.getTrendingPosts(5, 24); console.log(`āœ… Trending posts: ${trending.length} posts\n`); // Check bookmarks const bookmarks = await budytalk.getUserBookmarks(bob.userId); console.log(`āœ… Bob's bookmarks: ${bookmarks.length} posts`); const collections = await budytalk.getUserBookmarkCollections(bob.userId); console.log('āœ… Bob\'s bookmark collections:'); collections.forEach(collection => { console.log(` šŸ“ ${collection.name}: ${collection.count} bookmarks`); }); console.log(''); console.log('šŸŽ‰ POSTGRESQL EXAMPLE COMPLETED!'); console.log('================================='); console.log('āœ… Database Connection: Successfully connected to Neon PostgreSQL'); console.log('āœ… User Management: User accounts synced to cloud database'); console.log('āœ… Post Creation: Posts stored in PostgreSQL with full metadata'); console.log('āœ… Following System: Relationships tracked in cloud database'); console.log('āœ… Reactions & Comments: All interactions stored persistently'); console.log('āœ… Poll System: Polls and votes stored with real-time updates'); console.log('āœ… Bookmark System: Personal collections stored securely'); console.log('āœ… Timeline Generation: Personalized feeds from cloud data'); console.log('āœ… Trending Algorithm: Engagement-based ranking from PostgreSQL'); console.log(''); console.log('🐘 PostgreSQL Benefits:'); console.log(' 🌐 Cloud-hosted: Data accessible from anywhere'); console.log(' šŸ”’ ACID Compliance: Guaranteed data consistency'); console.log(' šŸ“ˆ Scalable: Handles millions of posts and users'); console.log(' šŸ”„ Real-time: Instant updates across all users'); console.log(' šŸ’¾ Persistent: Data survives server restarts'); console.log(' šŸ” Advanced Queries: Complex analytics and reporting'); console.log(''); console.log('šŸš€ Your content is now stored in a production-ready cloud database!'); // Keep server running for testing console.log('\nā° Server will keep running for 60 seconds for testing...'); console.log('šŸ’” You can test the API endpoints while the server is running!'); setTimeout(async () => { await budytalk.stop(); console.log('āœ… Server stopped'); }, 60000); } catch (error) { console.error('āŒ PostgreSQL example failed:', error.message); console.error('\nTroubleshooting tips:'); console.error('1. Check your DATABASE_URL environment variable'); console.error('2. Ensure your Neon database is accessible'); console.error('3. Verify SSL connection settings'); console.error('4. Check if you have the correct permissions'); process.exit(1); } } // Run the example if (require.main === module) { postgresExample(); } module.exports = { postgresExample };