Skip to content
Merged
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: 6 additions & 14 deletions core/src/sessions/db/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,20 @@ export async function getConnectionOptionsFromUri(
throw new Error(`Unsupported database URI: ${uri}`);
}

if (uri === 'sqlite://:memory:') {
if (uri.startsWith('sqlite://')) {
return {
entities: ENTITIES,
dbName: ':memory:',
dbName:
uri === 'sqlite://:memory:'
? ':memory:'
: uri.substring('sqlite://'.length),
driver,
} as MikroORMOptions;
}

const {host, port, username, password, pathname} = new URL(uri);
const hostName = host.split(':')[0];

const dbName = uri.startsWith('sqlite://')
? uri.substring('sqlite://'.length)
: pathname.slice(1);

return {
entities: ENTITIES,
dbName,
host: hostName,
port: port ? parseInt(port) : undefined,
user: username,
password,
clientUrl: uri,
driver,
} as MikroORMOptions;
}
Expand Down
33 changes: 24 additions & 9 deletions core/test/sessions/db/operations_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,35 @@ describe('operations', () => {
const options = await getConnectionOptionsFromUri(
'postgres://user:pass@localhost:5432/db',
);
expect(options.dbName).toBe('db');
expect(options.host).toBe('localhost');
expect(options.port).toBe(5432);
expect(options.user).toBe('user');
expect(options.password).toBe('pass');
expect(options.driver).toBeDefined();
expect(options.clientUrl).toBe('postgres://user:pass@localhost:5432/db');
});

it('should parse postgresql URI with query params and preserve them in clientUrl', async () => {
const uri = 'postgres://user:pass@localhost:5432/db?sslmode=require';
const options = await getConnectionOptionsFromUri(uri);
expect(options.clientUrl).toBe(uri);
});

it('should parse postgresql Unix-socket URI with percent-encoded host', async () => {
const uri =
'postgresql://user:pass@%2Fcloudsql%2Fmy-project%3Aus-central1%3Amy-instance/mydb';
const options = await getConnectionOptionsFromUri(uri);
expect(options.clientUrl).toBe(uri);
});

it('should parse postgresql Unix-socket URI with query param host', async () => {
const uri =
'postgresql://user:pass@/mydb?host=/cloudsql/my-project:us-central1:my-instance';
const options = await getConnectionOptionsFromUri(uri);
expect(options.clientUrl).toBe(uri);
});

it('should parse mysql URI', async () => {
const options = await getConnectionOptionsFromUri(
'mysql://user:pass@localhost:3306/db',
);
const uri = 'mysql://user:pass@localhost:3306/db';
const options = await getConnectionOptionsFromUri(uri);
expect(options.driver).toBeDefined();
expect(options.dbName).toBe('db');
expect(options.clientUrl).toBe(uri);
});

it('should parse mariadb URI', async () => {
Expand Down
6 changes: 4 additions & 2 deletions dev/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,12 @@ const LOG_LEVEL_OPTION = new Option(
'Optional. The log level of the server',
).default('info');
const SESSION_SERVICE_URI_OPTION = new Option(
'--session_service_uri <string>, Optional. The URI of the session service. Supported URIs: memory:// for in-memory session service.',
'--session_service_uri <string>',
'Optional. The URI of the session service. Supported URIs: memory:// for in-memory session service.',
);
const ARTIFACT_SERVICE_URI_OPTION = new Option(
'--artifact_service_uri <string>, Optional. The URI of the artifact service. Supported URIs: gs://<bucket name> for GCS artifact service.',
'--artifact_service_uri <string>',
'Optional. The URI of the artifact service. Supported URIs: gs://<bucket name> for GCS artifact service.',
);
const OTEL_TO_CLOUD_OPTION = new Option(
'--otel_to_cloud [boolean]',
Expand Down
Loading