bunql 1.0.1-dev.3 โ 1.0.1-dev.5
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.
- package/README.md +91 -3
- package/package.json +1 -1
- package/src/index.test.ts +4 -0
- package/src/index.ts +408 -171
- package/src/schema-new.ts +563 -0
- package/src/schema.test.ts +2 -3
- package/src/schema.ts +214 -617
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# BunQL
|
|
2
2
|
|
|
3
|
-
A fluent SQL query builder for Bun with transaction support, built on top of Bun's native SQL bindings.
|
|
3
|
+
A fluent SQL query builder for Bun with transaction support, built on top of Bun's native SQL bindings. **PostgreSQL-only** for maximum performance and efficiency.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
@@ -8,10 +8,12 @@ A fluent SQL query builder for Bun with transaction support, built on top of Bun
|
|
|
8
8
|
- โก **Auto-Execute**: Queries automatically execute when awaited (no `.execute()` needed!)
|
|
9
9
|
- ๐ **Transaction Support**: Built-in transaction handling with rollback support
|
|
10
10
|
- ๐ก๏ธ **SQL Injection Protection**: Parameterized queries prevent SQL injection
|
|
11
|
-
-
|
|
11
|
+
- ๐ **PostgreSQL Optimized**: Built specifically for PostgreSQL for maximum performance
|
|
12
12
|
- ๐ฆ **Zero Dependencies**: Built on Bun's native SQL bindings
|
|
13
13
|
- ๐งช **TypeScript Support**: Full TypeScript support with type safety
|
|
14
|
-
- ๐๏ธ **Schema Management**: Complete
|
|
14
|
+
- ๐๏ธ **Schema Management**: Complete PostgreSQL schema creation and management API
|
|
15
|
+
- ๐ **Auto-Close**: Connections automatically close after non-transaction queries
|
|
16
|
+
- ๐ **Auto-Reconnect**: Automatically reconnects when needed for subsequent queries
|
|
15
17
|
|
|
16
18
|
## Installation
|
|
17
19
|
|
|
@@ -49,6 +51,49 @@ const users = await db.select('*').from('users').execute();
|
|
|
49
51
|
|
|
50
52
|
This makes the API more concise and intuitive while maintaining backward compatibility.
|
|
51
53
|
|
|
54
|
+
## Auto-Close & Auto-Reconnect
|
|
55
|
+
|
|
56
|
+
BunQL automatically manages database connections for optimal performance and resource usage:
|
|
57
|
+
|
|
58
|
+
### Auto-Close
|
|
59
|
+
- **Non-transaction queries**: Connections automatically close after execution
|
|
60
|
+
- **Transaction queries**: Connections stay open during the transaction, then auto-close
|
|
61
|
+
- **Error handling**: Connections auto-close even if queries fail
|
|
62
|
+
|
|
63
|
+
### Auto-Reconnect
|
|
64
|
+
- **Seamless reconnection**: Automatically reconnects when needed for subsequent queries
|
|
65
|
+
- **No manual management**: No need to manually open/close connections
|
|
66
|
+
- **Resource efficient**: Prevents connection leaks and "too many clients" errors
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
// โ
Auto-close after each query
|
|
70
|
+
const count1 = await db.select('*').from('users').count();
|
|
71
|
+
const count2 = await db.select('*').from('users').count(); // Auto-reconnects
|
|
72
|
+
|
|
73
|
+
// โ
Transaction keeps connection open
|
|
74
|
+
const result = await db.transaction(async (trx) => {
|
|
75
|
+
const user = await trx.insert('users').values({ name: 'John' });
|
|
76
|
+
const profile = await trx.insert('profiles').values({ user_id: user.lastInsertRowid });
|
|
77
|
+
return { user, profile };
|
|
78
|
+
}); // Auto-closes after transaction
|
|
79
|
+
|
|
80
|
+
// โ
Perfect for Bun.serve applications
|
|
81
|
+
Bun.serve({
|
|
82
|
+
port: 3000,
|
|
83
|
+
async fetch(request) {
|
|
84
|
+
const db = new BunQL(process.env.DATABASE_URL!);
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
const users = await db.select('*').from('users').all();
|
|
88
|
+
return new Response(JSON.stringify(users));
|
|
89
|
+
} catch (error) {
|
|
90
|
+
return new Response('Error', { status: 500 });
|
|
91
|
+
}
|
|
92
|
+
// Connection automatically closed - no manual cleanup needed!
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
52
97
|
## Count Functionality
|
|
53
98
|
|
|
54
99
|
BunQL provides `.count()` methods on all query types to get the number of records:
|
|
@@ -475,6 +520,48 @@ BunQL works with all databases supported by Bun's native SQL bindings:
|
|
|
475
520
|
- **MySQL**: `mysql://user:pass@localhost:3306/db`
|
|
476
521
|
- **SQLite**: `sqlite://path/to/database.db` or `:memory:`
|
|
477
522
|
|
|
523
|
+
## Connection Management
|
|
524
|
+
|
|
525
|
+
### Opening Connections
|
|
526
|
+
|
|
527
|
+
```typescript
|
|
528
|
+
// SQLite (file)
|
|
529
|
+
const db = new BunQL('sqlite://database.db');
|
|
530
|
+
|
|
531
|
+
// SQLite (in-memory)
|
|
532
|
+
const db = new BunQL('sqlite://:memory:');
|
|
533
|
+
|
|
534
|
+
// PostgreSQL
|
|
535
|
+
const db = new BunQL('postgres://user:pass@localhost:5432/mydb');
|
|
536
|
+
|
|
537
|
+
// MySQL
|
|
538
|
+
const db = new BunQL('mysql://user:pass@localhost:3306/mydb');
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
### Closing Connections
|
|
542
|
+
|
|
543
|
+
Always close database connections when you're done to free up resources:
|
|
544
|
+
|
|
545
|
+
```typescript
|
|
546
|
+
const db = new BunQL('sqlite://database.db');
|
|
547
|
+
|
|
548
|
+
try {
|
|
549
|
+
// Your database operations
|
|
550
|
+
await db.insert('users').values({ name: 'John' });
|
|
551
|
+
const users = await db.select('*').from('users').all();
|
|
552
|
+
} finally {
|
|
553
|
+
// Always close the connection
|
|
554
|
+
await db.close();
|
|
555
|
+
}
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
### Best Practices
|
|
559
|
+
|
|
560
|
+
- **Always close connections** in `finally` blocks or use try-catch-finally
|
|
561
|
+
- **Use connection pooling** for production applications
|
|
562
|
+
- **Close connections** after transactions complete
|
|
563
|
+
- **Handle connection errors** gracefully
|
|
564
|
+
|
|
478
565
|
## API Reference
|
|
479
566
|
|
|
480
567
|
### BunQL
|
|
@@ -492,6 +579,7 @@ Main class for building and executing queries.
|
|
|
492
579
|
- `get(query, params?)`: Execute raw SQL and return first result
|
|
493
580
|
- `begin(callback)`: Execute queries in a transaction (legacy)
|
|
494
581
|
- `transaction(callback)`: Execute queries in a transaction (recommended)
|
|
582
|
+
- `close()`: Close the database connection
|
|
495
583
|
|
|
496
584
|
### SelectQuery
|
|
497
585
|
|
package/package.json
CHANGED
package/src/index.test.ts
CHANGED