Skip to content

Commit c22d6c5

Browse files
committed
Add a pool implementation.
1 parent d415afb commit c22d6c5

File tree

4 files changed

+97
-25
lines changed

4 files changed

+97
-25
lines changed

packages/stdext-sql-adapter/src/better-sqlite-client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { BetterSqliteDriver } from '@sqlite-js/better-sqlite3-driver';
2-
import { SqliteClient, SqliteClientOptions } from './core.js';
2+
import { SqliteClientPool, SqliteClientOptions } from './core.js';
33
import { fileURLToPath } from 'node:url';
44

5-
export class BetterSqliteClient extends SqliteClient {
5+
export class BetterSqliteClient extends SqliteClientPool {
66
constructor(connectionUrl: string | URL, options: SqliteClientOptions = {}) {
77
if (typeof connectionUrl != 'string') {
88
connectionUrl = fileURLToPath(connectionUrl);

packages/stdext-sql-adapter/src/connection.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,31 @@ export class SqliteConnection
2727
SqliteQueryOptions
2828
>
2929
{
30-
public driver: SqliteDriverConnection | undefined;
31-
public readonly pool: SqliteDriverConnectionPool;
30+
public driver: SqliteDriverConnection;
3231
public readonly connectionUrl: string;
33-
private reserved: ReservedConnection | undefined;
3432

3533
get connected(): boolean {
36-
// TODO: implement
37-
return true;
34+
return this.driver != null;
3835
}
3936

4037
public readonly options: SqliteConnectionOptions;
4138

4239
constructor(
4340
connectionUrl: string,
44-
pool: SqliteDriverConnectionPool,
41+
driver: SqliteDriverConnection,
4542
options?: SqliteConnectionOptions
4643
) {
4744
this.connectionUrl = connectionUrl;
48-
this.pool = pool;
45+
this.driver = driver;
4946
this.options = options ?? {};
5047
}
5148

5249
async connect(): Promise<void> {
53-
this.reserved = await this.pool.reserveConnection();
54-
this.driver = this.reserved.connection;
50+
// We're always connected
5551
}
5652

5753
async close(): Promise<void> {
58-
await this.reserved?.release();
59-
await this.pool.close();
54+
await this.driver.close();
6055
}
6156

6257
async execute(

packages/stdext-sql-adapter/src/core.ts

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import type {
22
ArrayRow,
33
Row,
44
SqlClient,
5+
SqlClientPool,
56
SqlConnectionOptions,
7+
SqlPoolClient,
8+
SqlPoolClientOptions,
69
SqlPreparable,
710
SqlPreparedStatement,
811
SqlQueriable,
@@ -16,6 +19,7 @@ import type { DatabaseOpenOptions } from './database.js';
1619
import {
1720
SqliteCloseEvent,
1821
SqliteConnectEvent,
22+
SqliteEvents,
1923
SqliteEventTarget
2024
} from './events.js';
2125
import {
@@ -26,6 +30,7 @@ import {
2630
import { SqliteTransactionError } from './errors.js';
2731
import { mergeQueryOptions, transformToAsyncGenerator } from './util.js';
2832
import {
33+
ReservedConnection,
2934
SqliteDriverConnection,
3035
SqliteDriverConnectionPool,
3136
SqliteDriverStatement,
@@ -437,34 +442,43 @@ export class SqliteTransactionable
437442
}
438443
}
439444

440-
/**
441-
* Sqlite client
442-
*/
443-
export class SqliteClient
445+
class SqlitePoolClient
444446
extends SqliteTransactionable
445447
implements
446-
SqlClient<
447-
SqliteEventTarget,
448+
SqlPoolClient<
448449
SqliteConnectionOptions,
450+
SqliteConnection,
449451
SqliteParameterType,
450452
SqliteQueryOptions,
451-
SqliteConnection,
452453
SqlitePreparedStatement,
453454
SqliteTransactionOptions,
454455
SqliteTransaction
455456
>
456457
{
457458
readonly eventTarget: SqliteEventTarget;
459+
readonly reserved: ReservedConnection;
460+
readonly connectionUrl: string;
458461

459462
constructor(
460463
connectionUrl: string,
461-
driver: SqliteDriverConnectionPool,
464+
reserved: ReservedConnection,
462465
options: SqliteClientOptions = {}
463466
) {
464-
const conn = new SqliteConnection(connectionUrl, driver, options);
467+
const conn = new SqliteConnection(
468+
connectionUrl,
469+
reserved.connection,
470+
options
471+
);
465472
super(conn, options);
473+
this.reserved = reserved;
474+
this.connectionUrl = connectionUrl;
466475
this.eventTarget = new SqliteEventTarget();
467476
}
477+
disposed: boolean = false;
478+
async release(): Promise<void> {
479+
this.disposed = true;
480+
await this.reserved.release();
481+
}
468482

469483
async connect(): Promise<void> {
470484
await this.connection.connect();
@@ -484,3 +498,66 @@ export class SqliteClient
484498
await this.close();
485499
}
486500
}
501+
502+
/**
503+
* Sqlite client
504+
*/
505+
export class SqliteClientPool
506+
implements
507+
SqlClientPool<
508+
SqliteConnectionOptions,
509+
SqliteParameterType,
510+
SqliteQueryOptions,
511+
SqliteConnection,
512+
SqlitePreparedStatement,
513+
SqliteTransactionOptions,
514+
SqliteTransaction
515+
>
516+
{
517+
readonly eventTarget: SqliteEventTarget;
518+
519+
readonly connectionUrl: string;
520+
readonly pool: SqliteDriverConnectionPool;
521+
readonly options: SqliteClientOptions;
522+
readonly connected = true;
523+
524+
constructor(
525+
connectionUrl: string,
526+
pool: SqliteDriverConnectionPool,
527+
options: SqliteClientOptions = {}
528+
) {
529+
this.pool = pool;
530+
this.connectionUrl = connectionUrl;
531+
this.options = options;
532+
this.eventTarget = new SqliteEventTarget();
533+
}
534+
535+
async acquire(): Promise<
536+
SqlPoolClient<
537+
SqliteConnectionOptions,
538+
SqliteConnection,
539+
SqliteValue,
540+
SqliteQueryOptions,
541+
SqlitePreparedStatement,
542+
SqliteTransactionOptions,
543+
SqliteTransaction,
544+
SqlPoolClientOptions
545+
>
546+
> {
547+
const reserved = await this.pool.reserveConnection();
548+
return new SqlitePoolClient(this.connectionUrl, reserved, this.options);
549+
}
550+
551+
async connect(): Promise<void> {
552+
// No-op
553+
}
554+
555+
async close(): Promise<void> {
556+
// TODO: this.eventTarget.dispatchEvent(new SqliteCloseEvent({}));
557+
await this.pool.close();
558+
}
559+
560+
async [Symbol.asyncDispose](): Promise<void> {
561+
await this.close();
562+
}
563+
}

packages/stdext-sql-adapter/test/src/adapter.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from 'node:fs/promises';
22
import { fileURLToPath } from 'node:url';
33
import { assert, test } from 'vitest';
4-
import { BetterSqliteClient, SqliteClient } from '../../lib/index.js';
4+
import { BetterSqliteClient, SqliteClientPool } from '../../lib/index.js';
55

66
const assertEquals = assert.deepEqual;
77

@@ -12,8 +12,8 @@ test('sqlite', async () => {
1212
// Remove any existing test.db.
1313
await fs.rm(path).catch(() => {});
1414

15-
let db: SqliteClient = new BetterSqliteClient(path);
16-
await db.connect();
15+
let pool: SqliteClientPool = new BetterSqliteClient(path);
16+
let db = await pool.acquire();
1717
await db.execute('pragma journal_mode = WAL');
1818
await db.execute('pragma synchronous = normal');
1919
assertEquals(await db.execute('pragma temp_store = memory'), 0);

0 commit comments

Comments
 (0)