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
9 changes: 8 additions & 1 deletion __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import SqlString from 'sqlstring'
import { connect, format, hex, DatabaseError, type Cast } from '../dist/index'
import { connect, format, hex, DatabaseError, type Cast, Client, isClient } from '../dist/index'
import { fetch, MockAgent, setGlobalDispatcher } from 'undici'
import packageJSON from '../package.json'

Expand Down Expand Up @@ -616,3 +616,10 @@ describe('hex', () => {
expect(hex('\0')).toEqual('0x00')
})
})

describe('guards', () => {
test('isClient', () => {
const client = new Client(config)
expect(isClient(client)).toBe(true)
})
})
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,17 @@ type ExecuteOptions<T extends ExecuteAs = 'object'> = T extends 'array'
? { as: 'array'; cast?: Cast }
: never

export const TypeId: unique symbol = Symbol.for('@planetscale/database/Client')
export type TypeId = typeof TypeId

export const isClient = (u: unknown): u is Client => typeof u === 'object' && u !== null && TypeId in u

export class Client {
public readonly config: Config
public readonly config: Config;
readonly [TypeId]: TypeId

constructor(config: Config) {
this[TypeId] = TypeId
this.config = config
}

Expand Down