Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | /** * Database Integration Example for Coherent.js * * This example demonstrates the complete database integration layer including: * - Connection management with multiple database engines * - ORM-style models with relationships and validation * - Query builder with fluent interface * - Migration system with schema management * - Router middleware integration * - Transaction support and connection pooling */ import { SimpleRouter } from '../src/api/router.js'; import { DatabaseManager, QueryBuilder, Model, Migration, withDatabase, withTransaction, withModel, withPagination, createModel, runMigrations } from '../src/database/index.js'; // Database configuration examples for different engines const configs = { sqlite: { type: 'sqlite', database: './example.db', pool: { min: 2, max: 10 }, debug: true }, postgresql: { type: 'postgresql', host: 'localhost', port: 5432, database: 'coherent_example', username: 'postgres', password: 'password', pool: { min: 2, max: 10 }, debug: true }, mysql: { type: 'mysql', host: 'localhost', port: 3306, database: 'coherent_example', username: 'root', password: 'password', pool: { min: 2, max: 10 }, debug: true }, mongodb: { type: 'mongodb', host: 'localhost', port: 27017, database: 'coherent_example', username: null, password: null, pool: { min: 2, max: 10 }, debug: true } }; // Initialize database (using SQLite for this example) const db = new DatabaseManager(configs.sqlite); // Define User model const User = createModel('User', { tableName: 'users', fillable: ['name', 'email', 'password', 'age', 'active'], hidden: ['password'], casts: { age: 'number', active: 'boolean' }, validationRules: { name: { required: true, minLength: 2, maxLength: 100 }, email: { required: true, email: true, unique: true }, password: { required: true, minLength: 6 }, age: { min: 0, max: 120 } }, relationships: { posts: { type: 'hasMany', model: 'Post', foreignKey: 'user_id' }, profile: { type: 'hasOne', model: 'Profile', foreignKey: 'user_id' } }, methods: { // Instance method getFullName() { return this.getAttribute('name'); }, async getPosts() { return await this.getRelation('posts'); } }, staticMethods: { // Static method async findByEmail(email) { return await this.where('email', email).first(); }, async getActiveUsers() { return await this.where('active', true).execute(); } } }, db); // Define Post model const Post = createModel('Post', { tableName: 'posts', fillable: ['title', 'content', 'user_id', 'published'], casts: { published: 'boolean', created_at: 'date' }, validationRules: { title: { required: true, minLength: 5, maxLength: 200 }, content: { required: true, minLength: 10 }, user_id: { required: true } }, relationships: { author: { type: 'belongsTo', model: 'User', foreignKey: 'user_id' } } }, db); // Define Profile model const Profile = createModel('Profile', { tableName: 'profiles', fillable: ['user_id', 'bio', 'avatar_url', 'website'], validationRules: { user_id: { required: true }, bio: { maxLength: 500 } }, relationships: { user: { type: 'belongsTo', model: 'User', foreignKey: 'user_id' } } }, db); // Create router with database middleware const router = new SimpleRouter(); // Add database middleware to all routes router.use(withDatabase(db, { autoConnect: true, attachModels: true })); // Example routes demonstrating database integration // Get all users with pagination router.get('/users', withPagination({ defaultLimit: 10 }), async (req, res) => { try { const users = await User.query() .select(['id', 'name', 'email', 'active', 'created_at']) .limit(req.pagination.limit) .offset(req.pagination.offset) .orderBy('created_at', 'DESC') .execute(); // Set pagination info const totalCount = await User.count(); req.pagination.totalCount = totalCount; req.pagination.totalPages = Math.ceil(totalCount / req.pagination.limit); req.pagination.hasNext = req.pagination.page < req.pagination.totalPages; res.json({ data: users.rows.map(user => new User(user).toJSON()), pagination: req.pagination }); } catch (error) { res.status(500).json({ error: error.message }); } }); // Get user by ID with model binding router.get('/users/:id', withModel(User), async (req, res) => { try { // req.user is automatically loaded by withModel middleware const posts = await req.user.getPosts(); const profile = await req.user.getRelation('profile'); res.json({ user: req.user.toJSON(), posts: posts.map(post => post.toJSON()), profile: profile ? profile.toJSON() : null }); } catch (error) { res.status(500).json({ error: error.message }); } }); // Create new user router.post('/users', async (req, res) => { try { const user = new User(req.body); await user.save(); res.status(201).json({ message: 'User created successfully', user: user.toJSON() }); } catch (error) { res.status(400).json({ error: error.message }); } }); // Update user router.put('/users/:id', withModel(User), async (req, res) => { try { req.user.fill(req.body); await req.user.save(); res.json({ message: 'User updated successfully', user: req.user.toJSON() }); } catch (error) { res.status(400).json({ error: error.message }); } }); // Delete user router.delete('/users/:id', withModel(User), async (req, res) => { try { await req.user.delete(); res.json({ message: 'User deleted successfully' }); } catch (error) { res.status(500).json({ error: error.message }); } }); // Complex query example with joins router.get('/posts/with-authors', async (req, res) => { try { // Using query builder for complex queries const query = new QueryBuilder(db, 'posts') .select([ 'posts.id', 'posts.title', 'posts.content', 'posts.created_at', 'users.name as author_name', 'users.email as author_email' ]) .join('users', 'posts.user_id', '=', 'users.id') .where('posts.published', true) .orderBy('posts.created_at', 'DESC') .limit(20); const result = await query.execute(); res.json({ posts: result.rows }); } catch (error) { res.status(500).json({ error: error.message }); } }); // Transaction example - transfer operation router.post('/transfer', withTransaction(db), async (req, res) => { try { const { fromUserId, toUserId, amount } = req.body; // All operations in this handler are wrapped in a transaction // If any operation fails, the entire transaction is rolled back // Deduct from sender await req.tx.query( 'UPDATE user_accounts SET balance = balance - ? WHERE user_id = ?', [amount, fromUserId] ); // Add to receiver await req.tx.query( 'UPDATE user_accounts SET balance = balance + ? WHERE user_id = ?', [amount, toUserId] ); // Log transaction await req.tx.query( 'INSERT INTO transactions (from_user_id, to_user_id, amount, created_at) VALUES (?, ?, ?, ?)', [fromUserId, toUserId, amount, new Date()] ); res.json({ message: 'Transfer completed successfully' }); } catch (error) { res.status(400).json({ error: error.message }); } }); // Raw SQL query example router.get('/stats', async (req, res) => { try { const stats = await req.db.query(` SELECT COUNT(*) as total_users, COUNT(CASE WHEN active = 1 THEN 1 END) as active_users, AVG(age) as average_age FROM users `, [], { single: true }); const postStats = await req.db.query(` SELECT COUNT(*) as total_posts, COUNT(CASE WHEN published = 1 THEN 1 END) as published_posts FROM posts `, [], { single: true }); res.json({ users: stats, posts: postStats }); } catch (error) { res.status(500).json({ error: error.message }); } }); // Search users with query validation router.get('/search/users', async (req, res) => { try { const { name, email, active, minAge, maxAge } = req.query; let query = User.query(); if (name) { query = query.where('name', 'LIKE', `%${name}%`); } if (email) { query = query.where('email', 'LIKE', `%${email}%`); } if (active !== undefined) { query = query.where('active', active === 'true'); } if (minAge) { query = query.where('age', '>=', parseInt(minAge)); } if (maxAge) { query = query.where('age', '<=', parseInt(maxAge)); } const users = await query.limit(50).execute(); res.json({ users: users.rows.map(user => new User(user).toJSON()) }); } catch (error) { res.status(500).json({ error: error.message }); } }); // Database health check router.get('/health/database', async (req, res) => { try { const startTime = Date.now(); await db.query('SELECT 1'); const responseTime = Date.now() - startTime; const stats = db.getStats(); res.json({ status: 'healthy', responseTime, database: { type: db.config.type, connected: db.isConnected, stats } }); } catch (error) { res.status(500).json({ status: 'unhealthy', error: error.message }); } }); // Migration management endpoints router.post('/admin/migrate', async (req, res) => { try { const appliedMigrations = await runMigrations(db); res.json({ message: `Applied ${appliedMigrations.length} migrations`, migrations: appliedMigrations }); } catch (error) { res.status(500).json({ error: error.message }); } }); // Example migration files (would be in ./migrations/ directory) const exampleMigrations = { '20231201000001_create_users_table.js': ` export async function up(schema) { await schema.createTable('users', (table) => { table.id(); table.string('name').notNull(); table.string('email').unique().notNull(); table.string('password').notNull(); table.integer('age'); table.boolean('active').default(true); table.timestamps(); }); } export async function down(schema) { await schema.dropTable('users'); } `, '20231201000002_create_posts_table.js': ` export async function up(schema) { await schema.createTable('posts', (table) => { table.id(); table.string('title').notNull(); table.text('content').notNull(); table.integer('user_id').notNull(); table.boolean('published').default(false); table.timestamps(); }); } export async function down(schema) { await schema.dropTable('posts'); } `, '20231201000003_create_profiles_table.js': ` export async function up(schema) { await schema.createTable('profiles', (table) => { table.id(); table.integer('user_id').unique().notNull(); table.text('bio'); table.string('avatar_url'); table.string('website'); table.timestamps(); }); } export async function down(schema) { await schema.dropTable('profiles'); } ` }; // Demo function to show database operations async function demonstrateDatabase() { try { console.log('š Starting Coherent.js Database Integration Demo'); // Connect to database await db.connect(); console.log('ā Database connected successfully'); // Run migrations (in a real app, migration files would exist) console.log('š¦ Running migrations...'); // await runMigrations(db); // Create sample users console.log('š„ Creating sample users...'); const user1 = await User.create({ name: 'John Doe', email: 'john@example.com', password: 'password123', age: 30, active: true }); const user2 = await User.create({ name: 'Jane Smith', email: 'jane@example.com', password: 'password456', age: 25, active: true }); console.log('ā Users created:', user1.toJSON(), user2.toJSON()); // Create posts console.log('š Creating sample posts...'); const post1 = await Post.create({ title: 'Getting Started with Coherent.js Database', content: 'This is a comprehensive guide to using the database layer...', user_id: user1.getAttribute('id'), published: true }); const post2 = await Post.create({ title: 'Advanced Query Building', content: 'Learn how to build complex queries with the query builder...', user_id: user2.getAttribute('id'), published: true }); console.log('ā Posts created'); // Demonstrate query builder console.log('š Demonstrating query builder...'); const activeUsers = await User.where('active', true) .where('age', '>', 20) .orderBy('created_at', 'DESC') .execute(); console.log(`Found ${activeUsers.rows.length} active users over 20`); // Demonstrate complex join query const postsWithAuthors = await new QueryBuilder(db, 'posts') .select(['posts.*', 'users.name as author_name']) .join('users', 'posts.user_id', '=', 'users.id') .where('posts.published', true) .execute(); console.log(`Found ${postsWithAuthors.rows.length} published posts with authors`); // Demonstrate transaction console.log('š³ Demonstrating transaction...'); const tx = await db.transaction(); try { await tx.query('UPDATE users SET age = age + 1 WHERE id = ?', [user1.getAttribute('id')]); await tx.query('UPDATE users SET age = age + 1 WHERE id = ?', [user2.getAttribute('id')]); await tx.commit(); console.log('ā Transaction completed successfully'); } catch (error) { await tx.rollback(); console.error('ā Transaction failed:', error.message); } // Show database stats const stats = db.getStats(); console.log('š Database statistics:', stats); console.log('š Database demo completed successfully!'); } catch (error) { console.error('ā Database demo failed:', error.message); } } // Start the demo if (import.meta.url === `file://${process.argv[1]}`) { demonstrateDatabase().then(() => { console.log('Demo finished. Starting HTTP server...'); // Start HTTP server const server = router.createServer(); const port = 3000; server.listen(port, () => { console.log(`š Database API server running at http://localhost:${port}`); console.log('\nš Available endpoints:'); console.log(' GET /users - List users with pagination'); console.log(' GET /users/:id - Get user with posts and profile'); console.log(' POST /users - Create new user'); console.log(' PUT /users/:id - Update user'); console.log(' DELETE /users/:id - Delete user'); console.log(' GET /posts/with-authors - Get posts with author info'); console.log(' POST /transfer - Transfer between users (transaction demo)'); console.log(' GET /search/users - Search users with filters'); console.log(' GET /stats - Database statistics'); console.log(' GET /health/database - Database health check'); console.log(' POST /admin/migrate - Run migrations'); }); }); } export { router, db, User, Post, Profile, demonstrateDatabase }; |