aetherframework-database 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +853 -0
  2. package/index.js +26 -0
  3. package/package.json +54 -0
  4. package/types.js +129 -0
package/README.md ADDED
@@ -0,0 +1,853 @@
1
+ AetherFramework Database - Zero-Dependency Multi-Database Integration for Node.js
2
+
3
+ 🚀 Why Choose AetherFramework Database?
4
+
5
+ AetherFramework Database is a revolutionary database integration solution designed specifically for the AetherJS API framework, but flexible enough to work with any Node.js application. In a world of bloated dependencies and complex ORMs, we offer a refreshing alternative that prioritizes performance, simplicity, and developer experience.
6
+
7
+ 📊 Key Advantages Over Other Solutions
8
+
9
+ | Feature | AetherFramework Database | TypeORM | Sequelize | Knex.js | Prisma |
10
+ |---------|-----------------------------|---------|-----------|---------|--------|
11
+ | Zero Dependencies | ✅ No external dependencies | ❌ 50+ dependencies | ❌ 40+ dependencies | ❌ 15+ dependencies | ❌ 100+ dependencies |
12
+ | Bundle Size | ✅ ~100KB | ❌ ~10MB | ❌ ~8MB | ❌ ~2MB | ❌ ~20MB |
13
+ | Multi-Database Support | ✅ 8+ databases unified API | ✅ 7+ databases | ✅ 6+ databases | ✅ 7+ databases | ✅ 5+ databases |
14
+ | Built-in Features | ✅ Pooling, Caching, Monitoring, Migrations | ❌ Requires plugins | ❌ Requires plugins | ❌ Basic only | ❌ Limited |
15
+ | TypeScript Support | ✅ First-class with full types | ✅ Excellent | ✅ Good | ✅ Basic | ✅ Excellent |
16
+ | Learning Curve | ✅ Gentle, intuitive API | 🔴 Complex | 🟡 Moderate | 🟢 Simple | 🔴 Complex |
17
+ | Performance | ✅ Native speed, no overhead | 🟡 Good | 🟡 Good | 🟢 Excellent | 🟢 Good |
18
+
19
+ 🎯 Why Developers Love Us
20
+
21
+ 1. Zero Dependency Architecture - No hidden dependencies, no version conflicts, no security vulnerabilities from third-party packages
22
+ 2. Unified API Across Databases - Write once, run anywhere (MySQL, PostgreSQL, SQLite, MongoDB, Redis, MSSQL, Oracle)
23
+ 3. Built-in Production Features - Connection pooling, query caching, performance monitoring, and migrations out of the box
24
+ 4. Exceptional Performance - Minimal abstraction overhead, optimized for high-throughput applications
25
+ 5. Developer Experience - Intuitive API, comprehensive TypeScript support, excellent documentation
26
+
27
+ 📦 Installation
28
+
29
+ ```bash
30
+ Using npm
31
+ npm install aetherframework-database
32
+ ```
33
+
34
+ 🚀 Quick Start
35
+
36
+ With AetherJS
37
+
38
+ ```javascript
39
+ // In your AetherJS project
40
+ import { Database } from 'aetherframework-database';
41
+
42
+ // Minimal configuration
43
+ const db = new Database({
44
+ connections: {
45
+ primary: {
46
+ type: 'sqlite',
47
+ database: './data/app.db'
48
+ }
49
+ }
50
+ });
51
+
52
+ // Initialize
53
+ await db.init();
54
+
55
+ // Start using immediately
56
+ const users = await db.table('users').select('*').execute();
57
+ console.log(users.rows);
58
+ ```
59
+
60
+ With Express.js
61
+
62
+ ```javascript
63
+ import express from 'express';
64
+ import { Database } from 'aetherframework-database';
65
+
66
+ const app = express();
67
+ const db = new Database({
68
+ connections: {
69
+ primary: {
70
+ type: 'mysql',
71
+ host: 'localhost',
72
+ user: 'root',
73
+ password: 'password',
74
+ database: 'myapp'
75
+ }
76
+ }
77
+ });
78
+
79
+ await db.init();
80
+
81
+ app.get('/users', async (req, res) => {
82
+ const users = await db.table('users').select('*').execute();
83
+ res.json(users.rows);
84
+ });
85
+
86
+ app.listen(3000, () => {
87
+ console.log('Server running with aetherframework-database');
88
+ });
89
+ ```
90
+
91
+ With Fastify
92
+
93
+ ```javascript
94
+ import Fastify from 'fastify';
95
+ import { Database } from 'aetherframework-database';
96
+
97
+ const fastify = Fastify();
98
+ const db = new Database({
99
+ connections: {
100
+ primary: {
101
+ type: 'postgresql',
102
+ host: 'localhost',
103
+ user: 'postgres',
104
+ password: 'password',
105
+ database: 'myapp'
106
+ }
107
+ }
108
+ });
109
+
110
+ await db.init();
111
+
112
+ fastify.get('/users', async (request, reply) => {
113
+ const users = await db.table('users').select('*').execute();
114
+ return users.rows;
115
+ });
116
+
117
+ fastify.listen({ port: 3000 }, (err) => {
118
+ if (err) throw err;
119
+ console.log('Fastify server running with aetherframework-database');
120
+ });
121
+ ```
122
+
123
+ With NestJS
124
+
125
+ ```javascript
126
+ // database.module.ts
127
+ import { Module } from '@nestjs/common';
128
+ import { Database } from 'aetherframework-database';
129
+
130
+ @Module({
131
+ providers: [
132
+ {
133
+ provide: 'DATABASE',
134
+ useFactory: async () => {
135
+ const db = new Database({
136
+ connections: {
137
+ primary: {
138
+ type: 'mysql',
139
+ host: 'localhost',
140
+ user: 'root',
141
+ password: 'password',
142
+ database: 'myapp'
143
+ }
144
+ }
145
+ });
146
+ await db.init();
147
+ return db;
148
+ }
149
+ }
150
+ ],
151
+ exports: ['DATABASE']
152
+ })
153
+ export class DatabaseModule {}
154
+
155
+ // users.service.ts
156
+ import { Injectable, Inject } from '@nestjs/common';
157
+
158
+ @Injectable()
159
+ export class UsersService {
160
+ constructor(@Inject('DATABASE') private db) {}
161
+
162
+ async findAll() {
163
+ return await this.db.table('users').select('*').execute();
164
+ }
165
+ }
166
+ ```
167
+
168
+ 📚 Basic Usage Guide
169
+
170
+ 1. Simple Configuration
171
+
172
+ ```javascript
173
+ import { Database } from 'aetherframework-database';
174
+
175
+ // Basic setup for a single database
176
+ const db = new Database({
177
+ connections: {
178
+ primary: {
179
+ type: 'sqlite', // or 'mysql', 'postgresql', 'mongodb', 'redis'
180
+ database: './data/myapp.db' // SQLite file path
181
+ }
182
+ }
183
+ });
184
+
185
+ await db.init();
186
+ ```
187
+
188
+ 2. Creating Tables
189
+
190
+ ```javascript
191
+ // Create a simple users table
192
+ await db.query(`
193
+ CREATE TABLE IF NOT EXISTS users (
194
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
195
+ name TEXT NOT NULL,
196
+ email TEXT UNIQUE NOT NULL,
197
+ age INTEGER,
198
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
199
+ )
200
+ `);
201
+ ```
202
+
203
+ 3. Basic CRUD Operations
204
+
205
+ Create (Insert)
206
+
207
+ ```javascript
208
+ // Insert a single record
209
+ const result = await db.table('users')
210
+ .insert({
211
+ name: 'John Doe',
212
+ email: 'john@example.com',
213
+ age: 30
214
+ })
215
+ .execute();
216
+
217
+ console.log(`Inserted user with ID: ${result.lastID}`);
218
+
219
+ // Insert multiple records
220
+ const batchResult = await db.table('users')
221
+ .insert([
222
+ { name: 'Alice', email: 'alice@example.com', age: 25 },
223
+ { name: 'Bob', email: 'bob@example.com', age: 35 },
224
+ { name: 'Charlie', email: 'charlie@example.com', age: 28 }
225
+ ])
226
+ .execute();
227
+
228
+ console.log(`Inserted ${batchResult.affectedRows} users`);
229
+ ```
230
+
231
+ Read (Select)
232
+
233
+ ```javascript
234
+ // Get all users
235
+ const allUsers = await db.table('users').select('*').execute();
236
+ console.log(`Total users: ${allUsers.rows.length}`);
237
+
238
+ // Get specific columns
239
+ const names = await db.table('users')
240
+ .select('id', 'name', 'email')
241
+ .execute();
242
+
243
+ // Get with conditions
244
+ const adults = await db.table('users')
245
+ .select('*')
246
+ .where('age', '>=', 18)
247
+ .execute();
248
+
249
+ // Get with multiple conditions
250
+ const activeUsers = await db.table('users')
251
+ .select('*')
252
+ .where('age', '>=', 18)
253
+ .where('status', '=', 'active')
254
+ .execute();
255
+
256
+ // Get with OR conditions
257
+ const specificUsers = await db.table('users')
258
+ .select('*')
259
+ .where('name', '=', 'John')
260
+ .orWhere('name', '=', 'Jane')
261
+ .execute();
262
+
263
+ // Get with ordering and limits
264
+ const recentUsers = await db.table('users')
265
+ .select('*')
266
+ .orderBy('created_at', 'desc')
267
+ .limit(10)
268
+ .execute();
269
+
270
+ // Get single record
271
+ const user = await db.table('users')
272
+ .where('id', '=', 1)
273
+ .first();
274
+ ```
275
+
276
+ Update
277
+
278
+ ```javascript
279
+ // Update single record
280
+ const updateResult = await db.table('users')
281
+ .where('id', '=', 1)
282
+ .update({
283
+ name: 'John Updated',
284
+ age: 31
285
+ })
286
+ .execute();
287
+
288
+ console.log(`Updated ${updateResult.affectedRows} user(s)`);
289
+
290
+ // Update multiple records
291
+ const bulkUpdate = await db.table('users')
292
+ .where('status', '=', 'inactive')
293
+ .update({ status: 'active' })
294
+ .execute();
295
+
296
+ // Increment/decrement values
297
+ await db.table('users')
298
+ .where('id', '=', 1)
299
+ .increment('login_count', 1)
300
+ .execute();
301
+
302
+ await db.table('users')
303
+ .where('id', '=', 2)
304
+ .decrement('balance', 100)
305
+ .execute();
306
+ ```
307
+
308
+ Delete
309
+
310
+ ```javascript
311
+ // Delete single record
312
+ const deleteResult = await db.table('users')
313
+ .where('id', '=', 1)
314
+ .delete()
315
+ .execute();
316
+
317
+ console.log(`Deleted ${deleteResult.affectedRows} user(s)`);
318
+
319
+ // Delete with conditions
320
+ await db.table('users')
321
+ .where('status', '=', 'banned')
322
+ .where('last_login', '<', new Date('2023-01-01'))
323
+ .delete()
324
+ .execute();
325
+ ```
326
+
327
+ 4. Working with Different Databases
328
+
329
+ MySQL
330
+
331
+ ```javascript
332
+ const mysqlDB = new Database({
333
+ connections: {
334
+ primary: {
335
+ type: 'mysql',
336
+ host: 'localhost',
337
+ port: 3306,
338
+ user: 'root',
339
+ password: 'password',
340
+ database: 'myapp',
341
+ charset: 'utf8mb4'
342
+ }
343
+ }
344
+ });
345
+ ```
346
+
347
+ PostgreSQL
348
+
349
+ ```javascript
350
+ const pgDB = new Database({
351
+ connections: {
352
+ primary: {
353
+ type: 'postgresql',
354
+ host: 'localhost',
355
+ port: 5432,
356
+ user: 'postgres',
357
+ password: 'password',
358
+ database: 'myapp',
359
+ ssl: false
360
+ }
361
+ }
362
+ });
363
+ ```
364
+
365
+ SQLite
366
+
367
+ ```javascript
368
+ const sqliteDB = new Database({
369
+ connections: {
370
+ primary: {
371
+ type: 'sqlite',
372
+ database: './data/app.db' // File path
373
+ // or ':memory:' for in-memory database
374
+ }
375
+ }
376
+ });
377
+ ```
378
+
379
+ MongoDB
380
+
381
+ ```javascript
382
+ const mongoDB = new Database({
383
+ connections: {
384
+ primary: {
385
+ type: 'mongodb',
386
+ host: 'localhost',
387
+ port: 27017,
388
+ database: 'myapp',
389
+ // Optional authentication
390
+ username: 'admin',
391
+ password: 'password'
392
+ }
393
+ }
394
+ });
395
+
396
+ // MongoDB specific operations
397
+ const users = await mongoDB.collection('users')
398
+ .find({ age: { $gt: 18 } })
399
+ .sort({ name: 1 })
400
+ .limit(10)
401
+ .execute();
402
+ ```
403
+
404
+ Redis
405
+
406
+ ```javascript
407
+ const redisDB = new Database({
408
+ connections: {
409
+ cache: {
410
+ type: 'redis',
411
+ host: 'localhost',
412
+ port: 6379,
413
+ // Optional authentication
414
+ password: 'password',
415
+ db: 0,
416
+ keyPrefix: 'myapp:'
417
+ }
418
+ }
419
+ });
420
+
421
+ // Redis operations
422
+ await redisDB.getConnection('cache').set('user:1', JSON.stringify(user));
423
+ const cachedUser = await redisDB.getConnection('cache').get('user:1');
424
+ ```
425
+
426
+ 5. Simple Joins and Relationships
427
+
428
+ ```javascript
429
+ // Basic join
430
+ const usersWithOrders = await db.table('users')
431
+ .select('users.*', 'orders.total_amount')
432
+ .join('orders', 'users.id', '=', 'orders.user_id')
433
+ .execute();
434
+
435
+ // Left join
436
+ const allUsersWithOrders = await db.table('users')
437
+ .select('users.*', 'orders.total_amount')
438
+ .leftJoin('orders', 'users.id', '=', 'orders.user_id')
439
+ .execute();
440
+
441
+ // Multiple joins
442
+ const detailedData = await db.table('users')
443
+ .select(
444
+ 'users.name',
445
+ 'orders.order_number',
446
+ 'products.name as product_name',
447
+ 'order_items.quantity'
448
+ )
449
+ .join('orders', 'users.id', '=', 'orders.user_id')
450
+ .join('order_items', 'orders.id', '=', 'order_items.order_id')
451
+ .join('products', 'order_items.product_id', '=', 'products.id')
452
+ .where('users.status', '=', 'active')
453
+ .execute();
454
+ ```
455
+
456
+ 6. Aggregation Functions
457
+
458
+ ```javascript
459
+ // Count records
460
+ const userCount = await db.table('users').count().execute();
461
+ console.log(`Total users: ${userCount.rows.count}`);
462
+
463
+ // Count with condition
464
+ const activeUsers = await db.table('users')
465
+ .where('status', '=', 'active')
466
+ .count()
467
+ .execute();
468
+
469
+ // Sum values
470
+ const totalSales = await db.table('orders')
471
+ .where('status', '=', 'completed')
472
+ .sum('amount')
473
+ .execute();
474
+
475
+ // Average values
476
+ const avgAge = await db.table('users').avg('age').execute();
477
+
478
+ // Minimum and maximum
479
+ const youngest = await db.table('users').min('age').execute();
480
+ const oldest = await db.table('users').max('age').execute();
481
+
482
+ // Group by
483
+ const salesByMonth = await db.table('orders')
484
+ .select(
485
+ db.raw('YEAR(created_at) as year'),
486
+ db.raw('MONTH(created_at) as month'),
487
+ db.raw('SUM(amount) as total_sales')
488
+ )
489
+ .where('status', '=', 'completed')
490
+ .groupBy('year', 'month')
491
+ .orderBy('year', 'desc')
492
+ .orderBy('month', 'desc')
493
+ .execute();
494
+ ```
495
+
496
+ 7. Simple Transactions
497
+
498
+ ```javascript
499
+ // Basic transaction
500
+ try {
501
+ const result = await db.transaction(async (trx) => {
502
+ // Insert user
503
+ const user = await trx.table('users')
504
+ .insert({
505
+ name: 'John',
506
+ email: 'john@example.com'
507
+ })
508
+ .execute();
509
+
510
+ // Insert user profile
511
+ await trx.table('profiles')
512
+ .insert({
513
+ user_id: user.lastID,
514
+ bio: 'Software Developer'
515
+ })
516
+ .execute();
517
+
518
+ return user;
519
+ });
520
+
521
+ console.log('Transaction completed:', result);
522
+ } catch (error) {
523
+ console.error('Transaction failed:', error);
524
+ }
525
+ ```
526
+
527
+ 8. Error Handling
528
+
529
+ ```javascript
530
+ try {
531
+ const result = await db.table('users')
532
+ .where('id', '=', 999)
533
+ .first();
534
+
535
+ if (!result) {
536
+ console.log('User not found');
537
+ }
538
+ } catch (error) {
539
+ console.error('Database error:', {
540
+ message: error.message,
541
+ code: error.code,
542
+ sql: error.sql,
543
+ params: error.params
544
+ });
545
+
546
+ // Handle specific error types
547
+ if (error.code === 'ER_DUP_ENTRY') {
548
+ console.log('Duplicate entry error');
549
+ } else if (error.code === 'ER_NO_SUCH_TABLE') {
550
+ console.log('Table does not exist');
551
+ }
552
+ }
553
+ ```
554
+
555
+ 9. Connection Management
556
+
557
+ ```javascript
558
+ // Check connection status
559
+ const health = await db.getAllHealthChecks();
560
+ console.log('Database health:', health);
561
+
562
+ // Get connection statistics
563
+ const stats = db.getMetrics();
564
+ console.log('Query statistics:', {
565
+ totalQueries: stats.totalQueries,
566
+ successfulQueries: stats.successfulQueries,
567
+ failedQueries: stats.failedQueries,
568
+ avgQueryTime: stats.avgQueryTime
569
+ });
570
+
571
+ // Close connections when done
572
+ await db.close();
573
+ console.log('Database connections closed');
574
+ ```
575
+
576
+ 🔧 Common Patterns
577
+
578
+ 1. Pagination
579
+
580
+ ```javascript
581
+ async function getUsers(page = 1, limit = 10) {
582
+ const offset = (page - 1) * limit;
583
+
584
+ const users = await db.table('users')
585
+ .select('*')
586
+ .orderBy('created_at', 'desc')
587
+ .limit(limit)
588
+ .offset(offset)
589
+ .execute();
590
+
591
+ const total = await db.table('users').count().execute();
592
+
593
+ return {
594
+ data: users.rows,
595
+ pagination: {
596
+ page,
597
+ limit,
598
+ total: total.rows.count,
599
+ pages: Math.ceil(total.rows.count / limit)
600
+ }
601
+ };
602
+ }
603
+ ```
604
+
605
+ 2. Search Functionality
606
+
607
+ ```javascript
608
+ async function searchUsers(query, filters = {}) {
609
+ let builder = db.table('users').select('*');
610
+
611
+ // Search in multiple fields
612
+ if (query) {
613
+ builder = builder.where(function(qb) {
614
+ qb.where('name', 'LIKE', `%${query}%`)
615
+ .orWhere('email', 'LIKE', `%${query}%`)
616
+ .orWhere('bio', 'LIKE', `%${query}%`);
617
+ });
618
+ }
619
+
620
+ // Apply filters
621
+ if (filters.status) {
622
+ builder = builder.where('status', '=', filters.status);
623
+ }
624
+
625
+ if (filters.minAge) {
626
+ builder = builder.where('age', '>=', filters.minAge);
627
+ }
628
+
629
+ if (filters.maxAge) {
630
+ builder = builder.where('age', '<=', filters.maxAge);
631
+ }
632
+
633
+ // Order and paginate
634
+ const result = await builder
635
+ .orderBy(filters.sortBy || 'created_at', filters.sortOrder || 'desc')
636
+ .limit(filters.limit || 20)
637
+ .offset(filters.offset || 0)
638
+ .execute();
639
+
640
+ return result.rows;
641
+ }
642
+ ```
643
+
644
+ 3. Batch Operations
645
+
646
+ ```javascript
647
+ // Batch insert with validation
648
+ async function batchCreateUsers(users) {
649
+ const validUsers = users.filter(user =>
650
+ user.name && user.email && user.age > 0
651
+ );
652
+
653
+ if (validUsers.length === 0) {
654
+ return { success: false, message: 'No valid users to insert' };
655
+ }
656
+
657
+ try {
658
+ const result = await db.table('users')
659
+ .insert(validUsers)
660
+ .execute();
661
+
662
+ return {
663
+ success: true,
664
+ inserted: result.affectedRows,
665
+ skipped: users.length - validUsers.length
666
+ };
667
+ } catch (error) {
668
+ console.error('Batch insert failed:', error);
669
+ return { success: false, error: error.message };
670
+ }
671
+ }
672
+ ```
673
+
674
+ ⚙️ Configuration Examples
675
+
676
+ Minimal Configuration
677
+
678
+ ```javascript
679
+ const minimalConfig = {
680
+ connections: {
681
+ primary: {
682
+ type: 'sqlite',
683
+ database: './data/app.db'
684
+ }
685
+ }
686
+ };
687
+ ```
688
+
689
+ Production Configuration
690
+
691
+ ```javascript
692
+ const productionConfig = {
693
+ enabled: true,
694
+ crossDb: true,
695
+ default: 'primary',
696
+
697
+ connections: {
698
+ primary: {
699
+ type: 'mysql',
700
+ host: process.env.DB_HOST,
701
+ port: parseInt(process.env.DB_PORT),
702
+ user: process.env.DB_USER,
703
+ password: process.env.DB_PASSWORD,
704
+ database: process.env.DB_NAME,
705
+ charset: 'utf8mb4',
706
+ timezone: '+00:00',
707
+ pool: {
708
+ min: 2,
709
+ max: 10,
710
+ idleTimeout: 30000,
711
+ acquireTimeout: 10000
712
+ }
713
+ },
714
+ cache: {
715
+ type: 'redis',
716
+ host: process.env.REDIS_HOST,
717
+ port: parseInt(process.env.REDIS_PORT),
718
+ password: process.env.REDIS_PASSWORD,
719
+ db: 0,
720
+ keyPrefix: 'app:'
721
+ }
722
+ },
723
+
724
+ driverModules: {
725
+ mysql: true,
726
+ redis: true,
727
+ sqlite: false,
728
+ postgres: false,
729
+ mongodb: false,
730
+ mssql: false,
731
+ oracle: false
732
+ },
733
+
734
+ middleware: {
735
+ queryLogger: {
736
+ enabled: process.env.NODE_ENV !== 'production',
737
+ logLevel: 'info',
738
+ slowQueryThreshold: 1000
739
+ },
740
+ connectionPool: {
741
+ enabled: true,
742
+ maxConnections: 10,
743
+ minConnections: 2
744
+ },
745
+ queryCache: {
746
+ enabled: true,
747
+ ttl: 300000
748
+ }
749
+ }
750
+ };
751
+ ```
752
+
753
+ 🔄 Migration from Other Libraries
754
+
755
+ From Knex.js
756
+
757
+ ```javascript
758
+ // Knex.js
759
+ knex('users').where('id', 1).first();
760
+
761
+ // aetherframework-database
762
+ db.table('users').where('id', '=', 1).first();
763
+ ```
764
+
765
+ From Sequelize
766
+
767
+ ```javascript
768
+ // Sequelize
769
+ User.findAll({ where: { status: 'active' } });
770
+
771
+ // aetherframework-database
772
+ db.table('users').where('status', '=', 'active').execute();
773
+ ```
774
+
775
+ From TypeORM
776
+
777
+ ```javascript
778
+ // TypeORM
779
+ userRepository.find({ where: { age: MoreThan(18) } });
780
+
781
+ // aetherframework-database
782
+ db.table('users').where('age', '>', 18).execute();
783
+ ```
784
+
785
+ 🏆 Best Practices
786
+
787
+ 1. Always use parameterized queries - Prevents SQL injection
788
+ 2. Close connections when done - Prevents connection leaks
789
+ 3. Use transactions for multiple operations - Ensures data consistency
790
+ 4. Enable query logging in development - Helps with debugging
791
+ 5. Use connection pooling in production - Improves performance
792
+ 6. Implement proper error handling - Graceful degradation
793
+ 7. Use TypeScript for type safety - Catches errors at compile time
794
+
795
+ 🔧 Troubleshooting
796
+
797
+ Common Issues
798
+
799
+ 1. Connection refused
800
+ ```javascript
801
+ // Check your connection settings
802
+ const db = new Database({
803
+ connections: {
804
+ primary: {
805
+ type: 'mysql',
806
+ host: 'localhost', // Make sure this is correct
807
+ port: 3306, // Default MySQL port
808
+ user: 'root', // Check username
809
+ password: 'password', // Check password
810
+ database: 'myapp' // Database must exist
811
+ }
812
+ }
813
+ });
814
+ ```
815
+
816
+ 2. Table doesn't exist
817
+ ```javascript
818
+ // Create the table first
819
+ await db.query(`
820
+ CREATE TABLE IF NOT EXISTS users (
821
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
822
+ name TEXT NOT NULL
823
+ )
824
+ `);
825
+ ```
826
+
827
+ 3. Slow queries
828
+ ```javascript
829
+ // Enable query logging to identify slow queries
830
+ const db = new Database({
831
+ middleware: {
832
+ queryLogger: {
833
+ enabled: true,
834
+ slowQueryThreshold: 100 // Log queries slower than 100ms
835
+ }
836
+ }
837
+ });
838
+ ```
839
+
840
+ 🚀 Next Steps
841
+
842
+ Once you're comfortable with the basics, explore these advanced features:
843
+
844
+ 1. Query Caching - Improve performance with built-in caching
845
+ 2. Performance Monitoring - Track and optimize query performance
846
+ 3. Database Migrations - Version control for your database schema
847
+ 4. Multiple Database Connections - Work with different databases simultaneously
848
+ 5. Custom Drivers - Extend support for other database systems
849
+
850
+
851
+ 📄 License
852
+
853
+ MIT © AetherJS Team
package/index.js ADDED
@@ -0,0 +1,26 @@
1
+ // packages/database/index.js
2
+ import DatabaseManager from './src/DatabaseManager.js';
3
+
4
+
5
+ export {
6
+ getDriver,
7
+ hasDriver,
8
+ getAvailableDrivers,
9
+ registerDriver,
10
+ unregisterDriver,
11
+ preloadDrivers,
12
+ clearDriverCache,
13
+ getMySQLDriver,
14
+ getPostgreSQLDriver,
15
+ getSQLiteDriver,
16
+ getMongoDBDriver,
17
+ getRedisDriver,
18
+ getMSSQLDriver,
19
+ getOracleDriver
20
+ } from './src/drivers/index.js';
21
+
22
+
23
+ export { default as ConnectionManager } from './src/core/ConnectionManager.js';
24
+ export { default as TransactionManager } from './src/core/TransactionManager.js';
25
+
26
+ export default DatabaseManager;
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "aetherframework-database",
3
+ "version": "1.0.0",
4
+ "description": "Zero-dependency, multi-database integration for AetherJS API framework with advanced query builder and connection pooling",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "types": "types.js",
8
+ "keywords": [
9
+ "database",
10
+ "multi-database",
11
+ "query-builder",
12
+ "connection-pool",
13
+ "mysql",
14
+ "postgresql",
15
+ "redis",
16
+ "mongodb",
17
+ "sqlite",
18
+ "mssql",
19
+ "oracle",
20
+ "zero-dependency",
21
+ "aetherjs",
22
+ "orm",
23
+ "driver"
24
+ ],
25
+ "author": "Aether Framework Team",
26
+ "license": "MIT",
27
+ "devDependencies": {
28
+ "@types/node": "^20.0.0",
29
+ "eslint": "^8.0.0",
30
+ "jest": "^29.0.0",
31
+ "jsdoc": "^4.0.0",
32
+ "prettier": "^3.0.0"
33
+ },
34
+ "engines": {
35
+ "node": ">=18.0.0"
36
+ },
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "https://github.com/aetherframework/database.git"
40
+ },
41
+ "bugs": {
42
+ "url": "https://github.com/aetherframework/database/issues"
43
+ },
44
+ "homepage": "https://github.com/aetherframework/database#readme",
45
+ "files": [
46
+ "index.js",
47
+ "connection-pool.js",
48
+ "query-builder.js",
49
+ "drivers/",
50
+ "types.js",
51
+ "README.md",
52
+ "LICENSE"
53
+ ]
54
+ }
package/types.js ADDED
@@ -0,0 +1,129 @@
1
+ // packages/database/types.js
2
+ /**
3
+ * @typedef {Object} DatabaseConfig
4
+ * @property {string} type - Database type: 'mysql', 'postgres', 'redis', 'mongodb', 'sqlite', 'mssql', 'oracle'
5
+ * @property {string} [host] - Host address
6
+ * @property {number} [port] - Port number
7
+ * @property {string} [user] - Username
8
+ * @property {string} [password] - Password
9
+ * @property {string} [database] - Database name
10
+ * @property {string} [filename] - SQLite file path
11
+ * @property {Object} [options] - Additional options
12
+ * @property {Object} [pool] - Connection pool options
13
+ * @property {number} [pool.min] - Minimum connections
14
+ * @property {number} [pool.max] - Maximum connections
15
+ * @property {number} [pool.idleTimeout] - Idle timeout in ms
16
+ * @property {number} [pool.acquireTimeout] - Acquire timeout in ms
17
+ */
18
+
19
+ /**
20
+ * @typedef {Object} QueryResult
21
+ * @property {Array<any>} rows - Result rows
22
+ * @property {number} rowCount - Number of affected rows
23
+ * @property {any} [lastInsertId] - Last insert ID
24
+ * @property {Array<string>} [columns] - Column names
25
+ * @property {string} [commandTag] - Command tag (PostgreSQL)
26
+ * @property {bigint} [cursorId] - Cursor ID (MongoDB)
27
+ * @property {number} [numberReturned] - Number returned (MongoDB)
28
+ */
29
+
30
+ /**
31
+ * @typedef {Object} Connection
32
+ * @property {boolean} connected - Connection status
33
+ * @property {function(string, Array): Promise<QueryResult>} query - Execute query
34
+ * @property {function(): Promise<void>} close - Close connection
35
+ * @property {function(): Promise<void>} [beginTransaction] - Begin transaction
36
+ * @property {function(): Promise<void>} [commit] - Commit transaction
37
+ * @property {function(): Promise<void>} [rollback] - Rollback transaction
38
+ * @property {function(function): Promise<any>} [transaction] - Execute transaction
39
+ * @property {function(Array): Promise<Array>} [batch] - Batch operations
40
+ */
41
+
42
+ /**
43
+ * @typedef {Object} DriverInterface
44
+ * @property {string} name - Driver name
45
+ * @property {string} version - Driver version
46
+ * @property {function(DatabaseConfig): Promise<Connection>} connect - Connect method
47
+ * @property {function(Connection): Promise<void>} close - Close method
48
+ * @property {function(Connection): Promise<Object>} [healthCheck] - Health check
49
+ */
50
+
51
+ /**
52
+ * @typedef {Object} QueryBuilderOptions
53
+ * @property {string} tableName - Table name
54
+ * @property {Connection} connection - Database connection
55
+ * @property {string} dialect - Database dialect
56
+ */
57
+
58
+ /**
59
+ * @typedef {Object} QueryBuilder
60
+ * @property {function(...string): QueryBuilder} select - Select columns
61
+ * @property {function(): QueryBuilder} distinct - Distinct query
62
+ * @property {function(string, string, any): QueryBuilder} where - Where condition
63
+ * @property {function(string, Array): QueryBuilder} whereIn - Where IN condition
64
+ * @property {function(string, Array): QueryBuilder} whereNotIn - Where NOT IN condition
65
+ * @property {function(string): QueryBuilder} whereNull - Where NULL condition
66
+ * @property {function(string): QueryBuilder} whereNotNull - Where NOT NULL condition
67
+ * @property {function(string, Array): QueryBuilder} whereBetween - Where BETWEEN condition
68
+ * @property {function(string, string): QueryBuilder} whereLike - Where LIKE condition
69
+ * @property {function(string, string, any): QueryBuilder} orWhere - OR Where condition
70
+ * @property {function(string, Array): QueryBuilder} whereRaw - Raw WHERE condition
71
+ * @property {function(string, string): QueryBuilder} orderBy - Order by
72
+ * @property {function(string, Array): QueryBuilder} orderByRaw - Raw ORDER BY
73
+ * @property {function(...string): QueryBuilder} groupBy - Group by
74
+ * @property {function(string, string, any): QueryBuilder} having - Having condition
75
+ * @property {function(number): QueryBuilder} limit - Limit results
76
+ * @property {function(number): QueryBuilder} offset - Offset results
77
+ * @property {function(string, string, string, string, string): QueryBuilder} join - Join table
78
+ * @property {function(string, string, string, string): QueryBuilder} leftJoin - LEFT JOIN
79
+ * @property {function(string, string, string, string): QueryBuilder} rightJoin - RIGHT JOIN
80
+ * @property {function(string): QueryBuilder} crossJoin - CROSS JOIN
81
+ * @property {function(Object|Array): QueryBuilder} insert - Insert data
82
+ * @property {function(Object): QueryBuilder} update - Update data
83
+ * @property {function(): QueryBuilder} delete - Delete data
84
+ * @property {function(...string): QueryBuilder} returning - Returning clause
85
+ * @property {function(string): QueryBuilder} lock - Lock rows
86
+ * @property {function(): Object} toSQL - Generate SQL
87
+ * @property {function(): Promise<QueryResult>} execute - Execute query
88
+ * @property {function(): Promise<Array>} all - Get all rows
89
+ * @property {function(): Promise<any>} first - Get first row
90
+ * @property {function(string): Promise<number>} count - Count rows
91
+ * @property {function(number, number): Promise<Object>} paginate - Paginate results
92
+ * @property {function(Object, string): Promise<any>} insertGetId - Insert and get ID
93
+ * @property {function(Array, number): Promise<Array>} batchInsert - Batch insert
94
+ * @property {function(): QueryBuilder} clone - Clone builder
95
+ * @property {function(): string} toDebugSQL - Debug SQL
96
+ * @property {function(): Promise<any>} explain - Explain query
97
+ */
98
+
99
+ /**
100
+ * @typedef {Object} DatabaseManager
101
+ * @property {function(DatabaseConfig): Promise<DatabaseManager>} init - Initialize
102
+ * @property {function(string, DatabaseConfig): Promise<Connection>} getConnection - Get connection
103
+ * @property {function(string, Array, string): Promise<QueryResult>} query - Execute query
104
+ * @property {function(string): QueryBuilder} table - Get query builder for table
105
+ * @property {function(function, string): Promise<any>} transaction - Execute transaction
106
+ * @property {function(Object): Promise<Object>} crossDbQuery - Cross-database query
107
+ * @property {function(Array, string): Promise<Array>} batch - Batch operations
108
+ * @property {function(string): Promise<Object>} healthCheck - Health check
109
+ * @property {function(): Promise<Object>} getAllHealthChecks - All health checks
110
+ * @property {function(): Object} getMetrics - Get metrics
111
+ * @property {function(): void} clearCache - Clear query cache
112
+ * @property {function(): Promise<void>} close - Close all connections
113
+ * @property {function(boolean): void} setEnabled - Enable/disable
114
+ * @property {function(): boolean} isDatabaseEnabled - Check if enabled
115
+ */
116
+
117
+ /**
118
+ * @typedef {Object} ConnectionPool
119
+ * @property {function(): Promise<void>} init - Initialize pool
120
+ * @property {function(): Promise<Connection>} getConnection - Get connection
121
+ * @property {function(Connection): void} releaseConnection - Release connection
122
+ * @property {function(string, Array): Promise<QueryResult>} query - Execute query
123
+ * @property {function(): Object} getStats - Get statistics
124
+ * @property {function(): Promise<Object>} healthCheck - Health check
125
+ * @property {function(): Promise<void>} close - Close pool
126
+ */
127
+
128
+ // Export for CommonJS
129
+ module.exports = {};