Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions packages/sqlite/src/sqlite-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class SQLiteConnection extends SQLConnection {
}

export class SQLiteConnectionPool extends SQLConnectionPool {
public maxConnections: number = 10;
protected maxConnections: number = 10;

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

Expand All @@ -186,7 +186,23 @@ export class SQLiteConnectionPool extends SQLConnectionPool {
constructor(protected dbPath: string | ':memory:') {
super();
//memory databases can not have more than one connection
if (dbPath === ':memory:') this.maxConnections = 1;
if (dbPath === ':memory:') this.setMaxConnections(1);
}

getMaxConnections(): number {
return this.maxConnections;
}

setMaxConnections(maxConnections: number) {
if (maxConnections < 1) {
throw new Error('Max connections must be at least 1');
}

if (this.dbPath === ':memory:' && maxConnections !== 1) {
throw new Error('Memory databases can not have more than one connection');
}

this.maxConnections = maxConnections;
}

close() {
Expand Down
7 changes: 4 additions & 3 deletions packages/sqlite/tests/sqlite.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { expect, test } from '@jest/globals';
import { SQLitePlatform } from '../src/sqlite-platform';
import { databaseFactory } from './factory';
import { User, UserCredentials } from '@deepkit/orm-integration';
import { SQLiteConnection, SQLiteDatabaseAdapter, SQLiteDatabaseTransaction } from '../src/sqlite-adapter';
import { SQLiteDatabaseAdapter, SQLiteDatabaseTransaction } from '../src/sqlite-adapter';
import { sleep } from '@deepkit/core';
import { AutoIncrement, cast, Entity, entity, PrimaryKey, Reference, ReflectionClass, serialize, typeOf, UUID, uuid } from '@deepkit/type';
import { Database, DatabaseEntityRegistry } from '@deepkit/orm';
import { DatabaseEntityRegistry } from '@deepkit/orm';
import { BackReference } from '@deepkit/type';

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

expect(sqlite.connectionPool.getActiveConnections()).toBeLessThanOrEqual(sqlite.connectionPool.maxConnections);
expect(sqlite.connectionPool.getActiveConnections())
.toBeLessThanOrEqual(sqlite.connectionPool.getMaxConnections());

expect(c1).toBeDefined();
expect(c2).toBeNull();
Expand Down