Skip to content

Commit 3435fd8

Browse files
authored
Merge branch 'main' into andrewvasilchuk-patch-1
2 parents 4220015 + 691691e commit 3435fd8

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

__tests__/index.test.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { cast, connect, format, hex, ExecutedQuery, DatabaseError } from '../dis
33
import { fetch, MockAgent, setGlobalDispatcher } from 'undici'
44
import packageJSON from '../package.json'
55

6-
const mockHost = 'https://example.com'
7-
6+
const mockHosts = ['http://localhost:8080', 'https://example.com']
87
const CREATE_SESSION_PATH = '/psdb.v1alpha1.Database/CreateSession'
98
const EXECUTE_PATH = '/psdb.v1alpha1.Database/Execute'
109
const config = {
@@ -20,7 +19,7 @@ mockAgent.disableNetConnect()
2019
setGlobalDispatcher(mockAgent)
2120

2221
// Provide the base url to the request
23-
const mockPool = mockAgent.get(mockHost)
22+
const mockPool = mockAgent.get((value) => mockHosts.includes(value))
2423
const mockSession = 42
2524

2625
describe('config', () => {
@@ -40,6 +39,23 @@ describe('config', () => {
4039
const got = await connection.execute('SELECT 1 from dual;')
4140
expect(got).toBeDefined()
4241
})
42+
43+
test('parses database URL when using HTTP', async () => {
44+
const mockResponse = {
45+
session: mockSession,
46+
result: { fields: [], rows: [] }
47+
}
48+
49+
mockPool.intercept({ path: EXECUTE_PATH, method: 'POST' }).reply(200, (opts) => {
50+
expect(opts.headers['Authorization']).toEqual(`Basic ${btoa('someuser:password')}`)
51+
expect(opts.headers['User-Agent']).toEqual(`database-js/${packageJSON.version}`)
52+
return mockResponse
53+
})
54+
55+
const connection = connect({ fetch, url: 'http://someuser:password@localhost:8080' })
56+
const got = await connection.execute('SELECT 1 from dual;')
57+
expect(got).toBeDefined()
58+
})
4359
})
4460

4561
describe('transaction', () => {
@@ -593,4 +609,13 @@ describe('cast', () => {
593609
test('casts int to number', () => {
594610
expect(cast({ name: 'test', type: 'INT8' }, '12')).toEqual(12)
595611
})
612+
613+
test('casts float to number', () => {
614+
expect(cast({ name: 'test', type: 'FLOAT32' }, '2.32')).toEqual(2.32)
615+
expect(cast({ name: 'test', type: 'FLOAT64' }, '2.32')).toEqual(2.32)
616+
})
617+
618+
test('casts JSON string to JSON object', () => {
619+
expect(cast({ name: 'test', type: 'JSON' }, '{ "foo": "bar" }')).toStrictEqual({ foo: 'bar' })
620+
})
596621
})

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@planetscale/database",
3-
"version": "1.10.0",
3+
"version": "1.11.0",
44
"description": "A Fetch API-compatible PlanetScale database driver",
55
"files": [
66
"dist"

src/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,20 @@ class Tx {
155155
}
156156
}
157157

158+
function protocol(protocol: string): string {
159+
return protocol === 'http:' ? protocol : 'https:'
160+
}
161+
162+
function buildURL(url: URL): string {
163+
const scheme = `${protocol(url.protocol)}//`
164+
165+
return new URL(url.pathname, `${scheme}${url.host}`).toString()
166+
}
167+
158168
export class Connection {
159169
private config: Config
160170
private session: QuerySession | null
171+
private url: string
161172

162173
constructor(config: Config) {
163174
this.session = null
@@ -172,6 +183,9 @@ export class Connection {
172183
this.config.username = url.username
173184
this.config.password = url.password
174185
this.config.host = url.hostname
186+
this.url = buildURL(url)
187+
} else {
188+
this.url = new URL(`https://${this.config.host}`).toString()
175189
}
176190
}
177191

@@ -200,7 +214,7 @@ export class Connection {
200214
args: ExecuteArgs = null,
201215
options: ExecuteOptions = defaultExecuteOptions
202216
): Promise<ExecutedQuery> {
203-
const url = new URL('/psdb.v1alpha1.Database/Execute', `https://${this.config.host}`)
217+
const url = new URL('/psdb.v1alpha1.Database/Execute', this.url)
204218

205219
const formatter = this.config.format || format
206220
const sql = args ? formatter(query, args) : query
@@ -251,7 +265,7 @@ export class Connection {
251265
}
252266

253267
private async createSession(): Promise<QuerySession> {
254-
const url = new URL('/psdb.v1alpha1.Database/CreateSession', `https://${this.config.host}`)
268+
const url = new URL('/psdb.v1alpha1.Database/CreateSession', this.url)
255269
const { session } = await postJSON<QueryExecuteResponse>(this.config, url)
256270
this.session = session
257271
return session

src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const Version = '1.10.0'
1+
export const Version = '1.11.0'

0 commit comments

Comments
 (0)