diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 76d86e1..f3b10e0 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -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' @@ -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) + }) +}) diff --git a/src/index.ts b/src/index.ts index f45a35a..202ed4b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -115,10 +115,17 @@ type ExecuteOptions = 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 }