@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
JavaScript
// 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 };