Skip to content

Commit f5fb9d1

Browse files
committed
refactor(sqlite): getter/setter for maxConnections
1 parent 2e72cff commit f5fb9d1

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

Diff for: packages/sqlite/src/sqlite-adapter.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export class SQLiteConnection extends SQLConnection {
176176
}
177177

178178
export class SQLiteConnectionPool extends SQLConnectionPool {
179-
public maxConnections: number = 10;
179+
protected maxConnections: number = 10;
180180

181181
protected queue: ((connection: SQLiteConnection) => void)[] = [];
182182

@@ -186,7 +186,23 @@ export class SQLiteConnectionPool extends SQLConnectionPool {
186186
constructor(protected dbPath: string | ':memory:') {
187187
super();
188188
//memory databases can not have more than one connection
189-
if (dbPath === ':memory:') this.maxConnections = 1;
189+
if (dbPath === ':memory:') this.setMaxConnections(1);
190+
}
191+
192+
getMaxConnections(): number {
193+
return this.maxConnections;
194+
}
195+
196+
setMaxConnections(maxConnections: number) {
197+
if (maxConnections < 1) {
198+
throw new Error('Max connections must be at least 1');
199+
}
200+
201+
if (this.dbPath === ':memory:' && maxConnections !== 1) {
202+
throw new Error('Memory databases can not have more than one connection');
203+
}
204+
205+
this.maxConnections = maxConnections;
190206
}
191207

192208
close() {

Diff for: packages/sqlite/tests/sqlite.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { expect, test } from '@jest/globals';
22
import { SQLitePlatform } from '../src/sqlite-platform';
33
import { databaseFactory } from './factory';
44
import { User, UserCredentials } from '@deepkit/orm-integration';
5-
import { SQLiteConnection, SQLiteDatabaseAdapter, SQLiteDatabaseTransaction } from '../src/sqlite-adapter';
5+
import { SQLiteDatabaseAdapter, SQLiteDatabaseTransaction } from '../src/sqlite-adapter';
66
import { sleep } from '@deepkit/core';
77
import { AutoIncrement, cast, Entity, entity, PrimaryKey, Reference, ReflectionClass, serialize, typeOf, UUID, uuid } from '@deepkit/type';
8-
import { Database, DatabaseEntityRegistry } from '@deepkit/orm';
8+
import { DatabaseEntityRegistry } from '@deepkit/orm';
99
import { BackReference } from '@deepkit/type';
1010

1111
test('reflection circular reference', () => {
@@ -281,7 +281,8 @@ test(':memory: connection pool', async () => {
281281
const c2 = await createConnectionOrNull();
282282
const c3 = await createConnectionOrNull();
283283

284-
expect(sqlite.connectionPool.getActiveConnections()).toBeLessThanOrEqual(sqlite.connectionPool.maxConnections);
284+
expect(sqlite.connectionPool.getActiveConnections())
285+
.toBeLessThanOrEqual(sqlite.connectionPool.getMaxConnections());
285286

286287
expect(c1).toBeDefined();
287288
expect(c2).toBeNull();

0 commit comments

Comments
 (0)