Skip to content

Commit

Permalink
Add convertJdbcToDsn utility function (#619)
Browse files Browse the repository at this point in the history
* feat: convert-jdbc-to-dsn helper

* docs: api doc updated

* docs: api doc updated

* feat: convert-jdbc-to-dsn helper

---------

Co-authored-by: Sebastien Vanvelthem <sebastien.vanvelthem@weareretail.ai>
  • Loading branch information
belgattitude and Sebastien Vanvelthem committed Oct 5, 2023
1 parent 017071b commit b7587cd
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 4 deletions.
19 changes: 19 additions & 0 deletions .changeset/nine-seals-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@httpx/dsn-parser': minor
---

Add convertJdbcToDsn utility function

Helps to convert [jdbc](https://learn.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-ver15) dsn.
Useful for prisma using [sqlserver](https://www.prisma.io/docs/concepts/database-connectors/sql-server#connection-details).

```typescript
import { convertJdbcToDsn } from '@httpx/dsn-parser';

const jdbcDsn =
'sqlserver://localhost:1433;database=my-db;authentication=default;user=sa;password=pass03$;encrypt=true;trustServerCertificate=true';

const dsn = convertJdbcToDsn(jdbc);

// -> 'sqlserver://localhost:1433?database=my-db&authentication=default&user=sa&password=pass03$&encrypt=true&trustServerCertificate=true'
```
2 changes: 1 addition & 1 deletion packages/dsn-parser/.size-limit.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = [
{
name: 'JS (ESM)',
path: ['dist/index.mjs'],
limit: '1.15KB',
limit: '1.30KB',
},
{
name: 'JS (CJS)',
Expand Down
18 changes: 18 additions & 0 deletions packages/dsn-parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,24 @@ if (!parsed.success) {
| `'EMPTY_DSN'` | `DSN cannot be empty` | |
| `'INVALID_PORT'` | `Invalid port: ${port}` | [1-65535] |

## Utilities

### convertJdbcToDsn

Utility to convert [jdbc](https://learn.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-ver15) dsn.
Useful for prisma using [sqlserver](https://www.prisma.io/docs/concepts/database-connectors/sql-server#connection-details).

```typescript
import { convertJdbcToDsn } from "@httpx/dsn-parser";

const jdbcDsn =
"sqlserver://localhost:1433;database=my-db;authentication=default;user=sa;password=pass03$;encrypt=true;trustServerCertificate=true";

const dsn = convertJdbcToDsn(jdbc);

// -> 'sqlserver://localhost:1433?database=my-db&authentication=default&user=sa&password=pass03$&encrypt=true&trustServerCertificate=true'
```

## Faq

### Zod integration example
Expand Down
17 changes: 17 additions & 0 deletions packages/dsn-parser/docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
### Functions

- [assertParsableDsn](README.md#assertparsabledsn)
- [convertJdbcToDsn](README.md#convertjdbctodsn)
- [isParsableDsn](README.md#isparsabledsn)
- [parseDsn](README.md#parsedsn)
- [parseDsnOrThrow](README.md#parsedsnorthrow)
Expand Down Expand Up @@ -77,6 +78,22 @@ Error when not parsable

---

### convertJdbcToDsn

**convertJdbcToDsn**(`jdbc`): `string`

#### Parameters

| Name | Type |
| :----- | :------- |
| `jdbc` | `string` |

#### Returns

`string`

---

### isParsableDsn

**isParsableDsn**(`dsn`): dsn is string
Expand Down
36 changes: 36 additions & 0 deletions packages/dsn-parser/src/__tests__/convert-jdbc-to-dsn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { convertJdbcToDsn } from '../convert-jdbc-to-dsn';
import { parseDsnOrThrow } from '../parse-dsn-or-throw';

describe('convertJdbcToDsn', () => {
it('should return a dsn string from a valid jdbc string', () => {
const jdbc =
'sqlserver://localhost:1433;database=my-db;authentication=default;user=sa;password=pass03$;encrypt=true;trustServerCertificate=true';
const dsn = convertJdbcToDsn(jdbc);
expect(dsn).toStrictEqual(
'sqlserver://localhost:1433?database=my-db&authentication=default&user=sa&password=pass03$&encrypt=true&trustServerCertificate=true'
);
expect(parseDsnOrThrow(dsn)).toStrictEqual({
driver: 'sqlserver',
host: 'localhost',
port: 1433,
params: {
authentication: 'default',
database: 'my-db',
encrypt: true,
password: 'pass03$',
trustServerCertificate: true,
user: 'sa',
},
});
});
it('should return a dsn string from a minimal jdbc dsn', () => {
const jdbc = 'sqlserver://localhost:1433';
const dsn = convertJdbcToDsn(jdbc);
expect(dsn).toStrictEqual('sqlserver://localhost:1433');
expect(parseDsnOrThrow(dsn)).toStrictEqual({
driver: 'sqlserver',
host: 'localhost',
port: 1433,
});
});
});
6 changes: 6 additions & 0 deletions packages/dsn-parser/src/convert-jdbc-to-dsn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const convertJdbcToDsn = (jdbc: string): string => {
const [part1, ...rest] = jdbc.split(';');
return [part1, rest ? rest.join('&') : undefined]
.filter((v) => Boolean(v))
.join('?');
};
1 change: 1 addition & 0 deletions packages/dsn-parser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export { parseDsn } from './parse-dsn';
export { parseDsnOrThrow } from './parse-dsn-or-throw';
export { assertParsableDsn } from './assert-parsable-dsn';
export { isParsableDsn } from './is-parsable-dsn';
export { convertJdbcToDsn } from './convert-jdbc-to-dsn';
2 changes: 1 addition & 1 deletion packages/json-api/docs/api/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@httpx/json-api

# @httpx/json-api - v0.4.0
# @httpx/json-api - v0.4.1

## Table of contents

Expand Down
2 changes: 1 addition & 1 deletion packages/json-api/docs/api/classes/JsonApiErrorFactory.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[@httpx/json-api - v0.4.0](../README.md) / JsonApiErrorFactory
[@httpx/json-api - v0.4.1](../README.md) / JsonApiErrorFactory

# Class: JsonApiErrorFactory

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[@httpx/json-api - v0.4.0](../README.md) / JsonApiResponseFactory
[@httpx/json-api - v0.4.1](../README.md) / JsonApiResponseFactory

# Class: JsonApiResponseFactory

Expand Down

0 comments on commit b7587cd

Please sign in to comment.