@tidbcloud/serverless
Advanced tools
Comparing version 0.0.9 to 0.0.10
@@ -34,2 +34,4 @@ interface ServerlessError { | ||
fullResult?: boolean; | ||
decoders?: Decoders; | ||
debug?: boolean; | ||
} | ||
@@ -39,2 +41,4 @@ interface ExecuteOptions { | ||
fullResult?: boolean; | ||
decoders?: Decoders; | ||
debug?: boolean; | ||
} | ||
@@ -45,2 +49,39 @@ interface TxOptions { | ||
type ExecuteArgs = object | any[] | null; | ||
declare const enum ColumnType { | ||
TINYINT = "TINYINT", | ||
UNSIGNED_TINYINT = "UNSIGNED TINYINT", | ||
SMALLINT = "SMALLINT", | ||
UNSIGNED_SMALLINT = "UNSIGNED SMALLINT", | ||
MEDIUMINT = "MEDIUMINT", | ||
UNSIGNED_MEDIUMINT = "UNSIGNED MEDIUMINT", | ||
INT = "INT", | ||
UNSIGNED_INT = "UNSIGNED INT", | ||
YEAR = "YEAR", | ||
FLOAT = "FLOAT", | ||
DOUBLE = "DOUBLE", | ||
BIGINT = "BIGINT", | ||
UNSIGNED_BIGINT = "UNSIGNED BIGINT", | ||
DECIMAL = "DECIMAL", | ||
CHAR = "CHAR", | ||
VARCHAR = "VARCHAR", | ||
BINARY = "BINARY", | ||
VARBINARY = "VARBINARY", | ||
TINYTEXT = "TINYTEXT", | ||
TEXT = "TEXT", | ||
MEDIUMTEXT = "MEDIUMTEXT", | ||
LONGTEXT = "LONGTEXT", | ||
TINYBLOB = "TINYBLOB", | ||
BLOB = "BLOB", | ||
MEDIUMBLOB = "MEDIUMBLOB", | ||
LONGBLOB = "LONGBLOB", | ||
DATE = "DATE", | ||
TIME = "TIME", | ||
DATETIME = "DATETIME", | ||
TIMESTAMP = "TIMESTAMP", | ||
BIT = "BIT", | ||
JSON = "JSON" | ||
} | ||
type Decoders = { | ||
[P in ColumnType]?: (rawValue: string) => any; | ||
}; | ||
@@ -47,0 +88,0 @@ type Row = Record<string, any> | any[]; |
@@ -73,6 +73,9 @@ // src/format.ts | ||
// src/decode.ts | ||
function cast(field, value) { | ||
function cast(field, value, decoder) { | ||
if (value === null) { | ||
return null; | ||
} | ||
if (decoder[field.type]) { | ||
return decoder[field.type](value); | ||
} | ||
switch (field.type) { | ||
@@ -84,2 +87,3 @@ case "TINYINT": | ||
case "MEDIUMINT": | ||
case "UNSIGNED MEDIUMINT": | ||
case "INT": | ||
@@ -130,6 +134,6 @@ case "UNSIGNED INT": | ||
// src/version.ts | ||
var Version = "0.0.9"; | ||
var Version = "0.0.10"; | ||
// src/serverless.ts | ||
async function postQuery(config, body, session = "", isolationLevel = null) { | ||
async function postQuery(config, body, session = "", isolationLevel = null, debug) { | ||
let fetchCacheOption = { cache: "no-store" }; | ||
@@ -141,2 +145,6 @@ try { | ||
} | ||
const requestId = generateUniqueId(); | ||
if (debug) { | ||
console.log(`[serverless-js debug] request id: ${requestId}`); | ||
} | ||
const url = new URL("/v1beta/sql", `https://http-${config.host}`); | ||
@@ -151,3 +159,4 @@ const auth = btoa(`${config.username}:${config.password}`); | ||
"TiDB-Database": database, | ||
"TiDB-Session": session | ||
"TiDB-Session": session, | ||
"X-Debug-Trace-Id": requestId | ||
}; | ||
@@ -163,2 +172,6 @@ if (isolationLevel) { | ||
}); | ||
if (debug) { | ||
const traceId = response?.headers?.get("X-Debug-Trace-Id"); | ||
console.log(`[serverless-js debug] response id: ${traceId}`); | ||
} | ||
if (response.ok) { | ||
@@ -180,8 +193,18 @@ const resp = await response.json(); | ||
} | ||
function generateUniqueId() { | ||
const datetime = (/* @__PURE__ */ new Date()).toISOString().replace(/[^\d]/g, "").slice(0, 14); | ||
return `${datetime}${randomString(20)}`; | ||
} | ||
function randomString(n) { | ||
let result = ""; | ||
const characters = "abcdefghijklmnopqrstuvwxyz0123456789"; | ||
const l = characters.length; | ||
for (let i = 0; i < n; i++) { | ||
result += characters[Math.floor(Math.random() * l)]; | ||
} | ||
return result; | ||
} | ||
// src/index.ts | ||
var defaultExecuteOptions = { | ||
arrayMode: false, | ||
fullResult: false | ||
}; | ||
var defaultExecuteOptions = {}; | ||
var Tx = class { | ||
@@ -237,3 +260,13 @@ constructor(conn) { | ||
const body = JSON.stringify({ query: sql }); | ||
const resp = await postQuery(this.config, body, this.session ?? "", sql == "BEGIN" ? txOptions.isolation : null); | ||
const debug = options.debug ?? this.config.debug ?? false; | ||
if (debug) { | ||
console.log(`[serverless-js debug] sql: ${sql}`); | ||
} | ||
const resp = await postQuery( | ||
this.config, | ||
body, | ||
this.session ?? "", | ||
sql == "BEGIN" ? txOptions.isolation : null, | ||
debug | ||
); | ||
this.session = resp?.session ?? null; | ||
@@ -244,5 +277,6 @@ if (this.session === null || this.session === "") { | ||
const arrayMode = options.arrayMode ?? this.config.arrayMode ?? false; | ||
const fullResult = options.fullResult ?? this.config.arrayMode ?? false; | ||
const fullResult = options.fullResult ?? this.config.fullResult ?? false; | ||
const decoders = { ...this.config.decoders, ...options.decoders }; | ||
const fields = resp?.types ?? []; | ||
const rows = resp ? parse(fields, resp?.rows ?? [], cast, arrayMode) : []; | ||
const rows = resp ? parse(fields, resp?.rows ?? [], cast, arrayMode, decoders) : []; | ||
if (fullResult) { | ||
@@ -268,15 +302,15 @@ const rowsAffected = resp?.rowsAffected ?? 0; | ||
} | ||
function parseArrayRow(fields, rawRow, cast2) { | ||
function parseArrayRow(fields, rawRow, cast2, decoders) { | ||
return fields.map((field, ix) => { | ||
return cast2(field, rawRow[ix]); | ||
return cast2(field, rawRow[ix], decoders); | ||
}); | ||
} | ||
function parseObjectRow(fields, rawRow, cast2) { | ||
function parseObjectRow(fields, rawRow, cast2, decoders) { | ||
return fields.reduce((acc, field, ix) => { | ||
acc[field.name] = cast2(field, rawRow[ix]); | ||
acc[field.name] = cast2(field, rawRow[ix], decoders); | ||
return acc; | ||
}, {}); | ||
} | ||
function parse(fields, rows, cast2, arrayMode) { | ||
return rows.map((row) => arrayMode === true ? parseArrayRow(fields, row, cast2) : parseObjectRow(fields, row, cast2)); | ||
function parse(fields, rows, cast2, arrayMode, decode) { | ||
return rows.map((row) => arrayMode === true ? parseArrayRow(fields, row, cast2, decode) : parseObjectRow(fields, row, cast2, decode)); | ||
} | ||
@@ -283,0 +317,0 @@ export { |
{ | ||
"name": "@tidbcloud/serverless", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "TiDB Cloud Serverless Driver", | ||
@@ -27,3 +27,3 @@ "main": "./dist/index.cjs", | ||
"build": "tsup", | ||
"lint": "eslint src/", | ||
"lint": "eslint src/ integration-test/", | ||
"pretest": "npm run build", | ||
@@ -30,0 +30,0 @@ "test": "jest __tests__ --passWithNoTests", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
39905
755