bunql 1.0.1-dev.1 → 1.0.1-dev.2
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 +33 -1
- package/package.json +1 -1
- package/src/index.test.ts +4 -0
- package/src/index.ts +17 -8
package/README.md
CHANGED
|
@@ -194,6 +194,37 @@ const user = await db.get('SELECT * FROM users WHERE id = ?', [1]);
|
|
|
194
194
|
|
|
195
195
|
### Transactions
|
|
196
196
|
|
|
197
|
+
BunQL provides two ways to handle transactions:
|
|
198
|
+
|
|
199
|
+
#### Method 1: Using `db.transaction()` (Recommended)
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
// Clean transaction API with automatic rollback on error
|
|
203
|
+
const result = await db.transaction(async (trx) => {
|
|
204
|
+
// Insert user
|
|
205
|
+
const userResult = await trx.insert('users')
|
|
206
|
+
.values({ name: 'John', email: 'john@example.com' });
|
|
207
|
+
|
|
208
|
+
// Insert user profile
|
|
209
|
+
await trx.insert('user_profiles')
|
|
210
|
+
.values({
|
|
211
|
+
user_id: userResult.lastInsertRowid,
|
|
212
|
+
bio: 'Software developer'
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// Update user status
|
|
216
|
+
await trx.update('users')
|
|
217
|
+
.set({ active: true })
|
|
218
|
+
.where('id', '=', userResult.lastInsertRowid);
|
|
219
|
+
|
|
220
|
+
return userResult.lastInsertRowid;
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
console.log('Transaction completed, user ID:', result);
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
#### Method 2: Using `db.begin()` (Legacy)
|
|
227
|
+
|
|
197
228
|
```typescript
|
|
198
229
|
// Transaction with automatic rollback on error
|
|
199
230
|
const result = await db.begin(async (tx) => {
|
|
@@ -421,7 +452,8 @@ Main class for building and executing queries.
|
|
|
421
452
|
- `run(query, params?)`: Execute raw SQL
|
|
422
453
|
- `all(query, params?)`: Execute raw SQL and return all results
|
|
423
454
|
- `get(query, params?)`: Execute raw SQL and return first result
|
|
424
|
-
- `begin(callback)`: Execute queries in a transaction
|
|
455
|
+
- `begin(callback)`: Execute queries in a transaction (legacy)
|
|
456
|
+
- `transaction(callback)`: Execute queries in a transaction (recommended)
|
|
425
457
|
|
|
426
458
|
### SelectQuery
|
|
427
459
|
|
package/package.json
CHANGED
package/src/index.test.ts
CHANGED
|
@@ -287,6 +287,10 @@ describe('BunQL', () => {
|
|
|
287
287
|
expect(typeof db.begin).toBe('function');
|
|
288
288
|
});
|
|
289
289
|
|
|
290
|
+
it('should have transaction method', () => {
|
|
291
|
+
expect(typeof db.transaction).toBe('function');
|
|
292
|
+
});
|
|
293
|
+
|
|
290
294
|
it('should have commit method', () => {
|
|
291
295
|
expect(typeof db.commit).toBe('function');
|
|
292
296
|
});
|
package/src/index.ts
CHANGED
|
@@ -42,35 +42,35 @@ async function executeSql(executor: any, query: string, params?: any[]): Promise
|
|
|
42
42
|
|
|
43
43
|
export class BunQL {
|
|
44
44
|
private sql: SQL;
|
|
45
|
-
private
|
|
45
|
+
private _transaction?: any;
|
|
46
46
|
|
|
47
47
|
constructor(connectionString?: string) {
|
|
48
48
|
this.sql = connectionString ? new SQL(connectionString) : sql;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
select<T = any>(columns?: string | string[]): SelectQuery<T> {
|
|
52
|
-
return new SelectQuery<T>(this.sql, this.
|
|
52
|
+
return new SelectQuery<T>(this.sql, this._transaction, columns);
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
update(table: string): UpdateQuery {
|
|
56
|
-
return new UpdateQuery(this.sql, table, this.
|
|
56
|
+
return new UpdateQuery(this.sql, table, this._transaction);
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
insert(table: string): InsertQuery {
|
|
60
|
-
return new InsertQuery(this.sql, table, this.
|
|
60
|
+
return new InsertQuery(this.sql, table, this._transaction);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
delete(table: string): DeleteQuery {
|
|
64
|
-
return new DeleteQuery(this.sql, table, this.
|
|
64
|
+
return new DeleteQuery(this.sql, table, this._transaction);
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
async run(query: string, params?: any[]): Promise<any> {
|
|
68
|
-
const executor = this.
|
|
68
|
+
const executor = this._transaction || this.sql;
|
|
69
69
|
return await executeSql(executor, query, params);
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
async all<T = any>(query: string, params?: any[]): Promise<T[]> {
|
|
73
|
-
const executor = this.
|
|
73
|
+
const executor = this._transaction || this.sql;
|
|
74
74
|
const result = await executeSql(executor, query, params);
|
|
75
75
|
return Array.isArray(result) ? result : [];
|
|
76
76
|
}
|
|
@@ -84,7 +84,16 @@ export class BunQL {
|
|
|
84
84
|
return await this.sql.begin(async (tx) => {
|
|
85
85
|
const transactionBuilder = new BunQL();
|
|
86
86
|
transactionBuilder.sql = this.sql;
|
|
87
|
-
transactionBuilder.
|
|
87
|
+
transactionBuilder._transaction = tx;
|
|
88
|
+
return await callback(transactionBuilder);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async transaction<T>(callback: (trx: BunQL) => Promise<T>): Promise<T> {
|
|
93
|
+
return await this.sql.begin(async (tx) => {
|
|
94
|
+
const transactionBuilder = new BunQL();
|
|
95
|
+
transactionBuilder.sql = this.sql;
|
|
96
|
+
transactionBuilder._transaction = tx;
|
|
88
97
|
return await callback(transactionBuilder);
|
|
89
98
|
});
|
|
90
99
|
}
|