JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q31026F
  • License Apache-2.0

A Fetch API-compatible TiDB Serverless driver

Package Exports

  • @shiyuhang0/serverless
  • @shiyuhang0/serverless/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@shiyuhang0/serverless) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

TiDB Cloud Serverless Driver for JavaScript

This driver is for serverless and edge compute platforms that require HTTP external connections, such as Vercel Edge Functions or Cloudflare Workers.

Installation

npm install @shiyuhang0/serverless

Usage

import { connect } from '@shiyuhang0/serverless'

const config = {
  host: '<host>',
  username: '<user>',
  password: '<password>'
}

const conn = connect(config)
const results = await conn.execute('select 1 from test')

Query with placeholder:

import { connect } from '@shiyuhang0/serverless'

const config = {
  host: '<host>',
  username: '<user>',
  password: '<password>'
}

const conn = connect(config)
const results = await conn.execute('select 1 from test where id = ?', [1])
const results2 = await conn.execute('select 1 from test where id = :id', {id:1})

Use the transaction function to safely perform database transactions:

import { connect } from '@shiyuhang0/serverless'

const config = {
  host: '<host>',
  username: '<user>',
  password: '<password>'
}
const conn = connect(config)
const tx = await conn.begin()
try {
  await tx.execute('insert into test values (1)')
  await tx.execute('select * from test')
  await tx.commit()
}catch (err) {
  await tx.rollback()
  throw err
}

Configuration

The following configurations are supported in connection level:

name type default comment
username string / Username of TiDB Severless
password string / Password of TiDB Severless
host string / Host of TiDB Severless
database string test Database of TiDB Severless
url string / A single url format as mysql://username:password@host/database
fetch function global fetch Custom fetch function

Database URL

A single database URL value can be used to configure the host, username, password and database values:

const conn = connect({url: process.env['DATABASE_URL'] || 'mysql://username:password@host/database'})

Database can be skipped to use the default one:

const conn = connect({url: process.env['DATABASE_URL'] || 'mysql://username:password@host'})

Custom fetch function

You can custom fetch function instead of using the global one. It's useful when you run in the environment without a built-in global fetch function. For example, an older version of Node.js.

import { connect } from '@shiyuhang0/serverless'
import { fetch } from 'undici'

const config = {
  fetch,
  url: process.env['DATABASE_URL'] || 'mysql://username:password@host/database'
}

const conn = connect(config)
const results = await conn.execute('select 1 from test')

Options

The following options are supported in SQL level:

option type default comment
arrayMode bool false whether to return results as arrays instead of objects
fullResult bool false whether to return full result object instead of just rows
import { connect } from '@shiyuhang0/serverless'

const config = {
  url: process.env['DATABASE_URL'] || 'mysql://username:password@host/database'
}

const conn = connect(config)
const results = await conn.execute('select 1 from test',null,{arrayMode:true,fullResult:true})