more prep work for better-sqlite3

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2022-03-08 12:02:02 +01:00
parent 257af4c80e
commit dc104b3f4a
17 changed files with 193 additions and 22 deletions
+1
View File
@@ -106,6 +106,7 @@
"@types/unzipper": "^0.10.3",
"@types/webpack-env": "^1.15.2",
"aws-sdk-mock": "^5.2.1",
"better-sqlite3": "^7.5.0",
"http-errors": "^2.0.0",
"jest": "^26.0.1",
"mock-fs": "^5.1.0",
@@ -119,7 +119,7 @@ export class DatabaseManager {
private getDatabaseName(pluginId: string): string | undefined {
const connection = this.getConnectionConfig(pluginId);
if (this.getClientType(pluginId).client === 'sqlite3') {
if (this.getClientType(pluginId).client.includes('sqlite3')) {
const sqliteFilename: string | undefined = (
connection as Knex.Sqlite3ConnectionConfig
).filename;
@@ -224,7 +224,7 @@ export class DatabaseManager {
);
if (
client === 'sqlite3' &&
client.includes('sqlite3') &&
'filename' in baseConnection &&
baseConnection.filename !== ':memory:'
) {
@@ -23,7 +23,13 @@ import { DatabaseConnector } from './types';
import { mysqlConnector, pgConnector, sqlite3Connector } from './connectors';
type DatabaseClient = 'pg' | 'sqlite3' | 'mysql' | 'mysql2' | string;
type DatabaseClient =
| 'pg'
| 'better-sqlite3'
| 'sqlite3'
| 'mysql'
| 'mysql2'
| string;
/**
* Mapping of client type to supported database connectors
@@ -101,3 +101,84 @@ describe('sqlite3', () => {
});
});
});
describe('better-sqlite3', () => {
const createConfig = (connection: any) =>
new ConfigReader({ client: 'better-sqlite3', connection });
describe('buildSqliteDatabaseConfig', () => {
it('builds an in-memory connection', () => {
expect(buildSqliteDatabaseConfig(createConfig(':memory:'))).toEqual({
client: 'better-sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true,
});
});
it('builds an in-memory connection by override with filename', () => {
expect(
buildSqliteDatabaseConfig(
createConfig(path.join('path', 'to', 'foo')),
{ connection: ':memory:' },
),
).toEqual({
client: 'better-sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true,
});
});
it('builds a persistent connection, normalize config with filename', () => {
expect(
buildSqliteDatabaseConfig(createConfig(path.join('path', 'to', 'foo'))),
).toEqual({
client: 'better-sqlite3',
connection: { filename: path.join('path', 'to', 'foo') },
useNullAsDefault: true,
});
});
it('builds a persistent connection', () => {
expect(
buildSqliteDatabaseConfig(
createConfig({
filename: path.join('path', 'to', 'foo'),
}),
),
).toEqual({
client: 'better-sqlite3',
connection: {
filename: path.join('path', 'to', 'foo'),
},
useNullAsDefault: true,
});
});
it('replaces the connection with an override', () => {
expect(
buildSqliteDatabaseConfig(createConfig(':memory:'), {
connection: { filename: path.join('path', 'to', 'foo') },
}),
).toEqual({
client: 'better-sqlite3',
connection: {
filename: path.join('path', 'to', 'foo'),
},
useNullAsDefault: true,
});
});
});
describe('createSqliteDatabaseClient', () => {
it('creates an in memory knex instance', () => {
expect(
createSqliteDatabaseClient(
createConfig({
client: 'better-sqlite3',
connection: ':memory:',
}),
),
).toBeTruthy();
});
});
});
@@ -182,6 +182,7 @@ export class TestDatabases {
return this.initPostgres(properties);
case 'mysql2':
return this.initMysql(properties);
case 'better-sqlite3':
case 'sqlite3':
return this.initSqlite(properties);
default:
@@ -240,13 +241,13 @@ export class TestDatabases {
}
private async initSqlite(
_properties: TestDatabaseProperties,
properties: TestDatabaseProperties,
): Promise<Instance> {
const databaseManager = DatabaseManager.fromConfig(
new ConfigReader({
backend: {
database: {
client: 'sqlite3',
client: properties.driver,
connection: ':memory:',
},
},
@@ -40,6 +40,7 @@ export type Instance = {
databaseManager: DatabaseManager;
connections: Array<Knex>;
};
export const allDatabases: Record<TestDatabaseId, TestDatabaseProperties> =
Object.freeze({
POSTGRES_13: {