Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tidbcloud/serverless

Package Overview
Dependencies
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tidbcloud/serverless

TiDB Serverless driver

  • 0.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
593
decreased by-2.15%
Maintainers
2
Weekly downloads
 
Created
Source

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 @tidbcloud/serverless

Usage

import { connect } from '@tidbcloud/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 '@tidbcloud/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 '@tidbcloud/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:

nametypedefaultcomment
usernamestring/Username of TiDB Severless
passwordstring/Password of TiDB Severless
hoststring/Host of TiDB Severless
databasestringtestDatabase of TiDB Severless
urlstring/A single url format as mysql://username:password@host/database
fetchfunctionglobal fetchCustom 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 '@tidbcloud/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:

optiontypedefaultcomment
arrayModeboolfalsewhether to return results as arrays instead of objects
fullResultboolfalsewhether to return full result object instead of just rows
import { connect } from '@tidbcloud/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})

Features

Supported SQL

The following SQL statements are supported: Select, Show, Explain, Use, Insert, Update, Delete, Begin, Commit, Rollback.

And most of the DDL are supported.

Data Type Mapping

The type mapping between TiDB and Javascript is as follows:

TiDB TypeJavascript Type
TINYINTnumber
UNSIGNED TINYINTnumber
BOOLnumber
SMALLINTnumber
UNSIGNED SMALLINTnumber
MEDIUMINTnumber
INTnumber
UNSIGNED INTnumber
YEARnumber
FLOATnumber
DOUBLEnumber
BIGINTstring
UNSIGNED BIGINTstring
DECIMALstring
CHARstring
VARCHARstring
BINARYstring
VARBINARYstring
TINYTEXTstring
TEXTstring
MEDIUMTEXTstring
LONGTEXTstring
TINYBLOBstring
BLOBstring
MEDIUMBLOBstring
LONGBLOBstring
DATEstring
TIMEstring
DATETIMEstring
TIMESTAMPstring
ENUMstring
SETstring
BITstring
JSONobject
NULLnull
Othersstring

Limitations

  • Up to 10,000 rows can be fetched in a single query.

FAQs

Package last updated on 07 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc