Skip to content

Commit

Permalink
add fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyuhang0 committed Feb 27, 2024
1 parent 773d3fc commit 13f57be
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 159 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ jobs:
- name: lint
run: pnpm lint
- name: build
run: pnpm build
run: pnpm build
- name: fmt
run: pnpm fmt-check
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"types": "dist/index.d.ts",
"scripts": {
"build": "tsup ./src/index.ts --format cjs,esm --dts",
"lint": "tsc -p ./tsconfig.build.json"
"lint": "tsc -p ./tsconfig.build.json",
"fmt": "prettier --write ./src",
"fmt-check": "prettier --check ./src"
},
"files": [
"dist",
Expand Down Expand Up @@ -39,7 +41,8 @@
"@types/node": "^20.5.1",
"tsup": "^7.2.0",
"tsx": "^3.12.7",
"typescript": "^5.1.6"
"typescript": "^5.1.6",
"prettier": "^2.7.1"
},
"peerDependencies": {
"@tidbcloud/serverless": ">= 0.1.0"
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

186 changes: 93 additions & 93 deletions src/conversion.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import { ColumnTypeEnum, type ColumnType } from '@prisma/driver-adapter-utils'
import { ColumnTypeEnum, type ColumnType } from "@prisma/driver-adapter-utils";

export type TiDBCloudColumnType
= 'NULL'
| 'TINYINT'
| 'UNSIGNED TINYINT'
| 'SMALLINT'
| 'UNSIGNED SMALLINT'
| 'MEDIUMINT'
| 'UNSIGNED MEDIUMINT'
| 'INT'
| 'UNSIGNED INT'
| 'YEAR'
| 'FLOAT'
| 'DOUBLE'
| 'BIGINT'
| 'UNSIGNED BIGINT'
| 'DECIMAL'
| 'CHAR'
| 'VARCHAR'
| 'BINARY'
| 'VARBINARY'
| 'TINYTEXT'
| 'TEXT'
| 'MEDIUMTEXT'
| 'LONGTEXT'
| 'TINYBLOB'
| 'BLOB'
| 'MEDIUMBLOB'
| 'LONGBLOB'
| 'DATE'
| 'TIME'
| 'DATETIME'
| 'TIMESTAMP'
| 'JSON'
| 'BIT'
export type TiDBCloudColumnType =
| "NULL"
| "TINYINT"
| "UNSIGNED TINYINT"
| "SMALLINT"
| "UNSIGNED SMALLINT"
| "MEDIUMINT"
| "UNSIGNED MEDIUMINT"
| "INT"
| "UNSIGNED INT"
| "YEAR"
| "FLOAT"
| "DOUBLE"
| "BIGINT"
| "UNSIGNED BIGINT"
| "DECIMAL"
| "CHAR"
| "VARCHAR"
| "BINARY"
| "VARBINARY"
| "TINYTEXT"
| "TEXT"
| "MEDIUMTEXT"
| "LONGTEXT"
| "TINYBLOB"
| "BLOB"
| "MEDIUMBLOB"
| "LONGBLOB"
| "DATE"
| "TIME"
| "DATETIME"
| "TIMESTAMP"
| "JSON"
| "BIT";

/**
* This is a simplification of quaint's value inference logic. Take a look at quaint's conversion.rs
Expand All @@ -42,72 +42,72 @@ export type TiDBCloudColumnType
*/
export function fieldToColumnType(field: TiDBCloudColumnType): ColumnType {
switch (field) {
case 'TINYINT':
case 'UNSIGNED TINYINT':
case 'SMALLINT':
case 'UNSIGNED SMALLINT':
case 'MEDIUMINT':
case 'UNSIGNED MEDIUMINT':
case 'INT':
case 'YEAR':
return ColumnTypeEnum.Int32
case 'UNSIGNED INT':
case 'BIGINT':
case 'UNSIGNED BIGINT':
return ColumnTypeEnum.Int64
case 'FLOAT':
return ColumnTypeEnum.Float
case 'DOUBLE':
return ColumnTypeEnum.Double
case 'TIMESTAMP':
case 'DATETIME':
return ColumnTypeEnum.DateTime
case 'DATE':
return ColumnTypeEnum.Date
case 'TIME':
return ColumnTypeEnum.Time
case 'DECIMAL':
return ColumnTypeEnum.Numeric
case 'CHAR':
case 'TINYTEXT':
case 'TEXT':
case 'MEDIUMTEXT':
case 'LONGTEXT':
case 'VARCHAR':
return ColumnTypeEnum.Text
case 'JSON':
return ColumnTypeEnum.Json
case 'TINYBLOB':
case 'BLOB':
case 'MEDIUMBLOB':
case 'LONGBLOB':
case 'BINARY':
case 'VARBINARY':
case 'BIT':
return ColumnTypeEnum.Bytes
case 'NULL':
case "TINYINT":
case "UNSIGNED TINYINT":
case "SMALLINT":
case "UNSIGNED SMALLINT":
case "MEDIUMINT":
case "UNSIGNED MEDIUMINT":
case "INT":
case "YEAR":
return ColumnTypeEnum.Int32;
case "UNSIGNED INT":
case "BIGINT":
case "UNSIGNED BIGINT":
return ColumnTypeEnum.Int64;
case "FLOAT":
return ColumnTypeEnum.Float;
case "DOUBLE":
return ColumnTypeEnum.Double;
case "TIMESTAMP":
case "DATETIME":
return ColumnTypeEnum.DateTime;
case "DATE":
return ColumnTypeEnum.Date;
case "TIME":
return ColumnTypeEnum.Time;
case "DECIMAL":
return ColumnTypeEnum.Numeric;
case "CHAR":
case "TINYTEXT":
case "TEXT":
case "MEDIUMTEXT":
case "LONGTEXT":
case "VARCHAR":
return ColumnTypeEnum.Text;
case "JSON":
return ColumnTypeEnum.Json;
case "TINYBLOB":
case "BLOB":
case "MEDIUMBLOB":
case "LONGBLOB":
case "BINARY":
case "VARBINARY":
case "BIT":
return ColumnTypeEnum.Bytes;
case "NULL":
// Fall back to Int32 for consistency with quaint.
return ColumnTypeEnum.Int32
return ColumnTypeEnum.Int32;
default:
throw new Error(`Unsupported column type: ${field}`)
throw new Error(`Unsupported column type: ${field}`);
}
}

// define the decoder because TiDB Cloud serverless driver returns Uint8Array for these type
export const customDecoder = {
"BINARY": (value: string) => Array.from(hexToUint8Array(value)),
"VARBINARY": (value: string) => Array.from(hexToUint8Array(value)),
"BLOB": (value: string) => Array.from(hexToUint8Array(value)),
"LONGBLOB": (value: string) => Array.from(hexToUint8Array(value)),
"TINYINT": (value: string) => Array.from(hexToUint8Array(value)),
"MEDIUMBLOB": (value: string) => Array.from(hexToUint8Array(value)),
"BIT": (value: string) => Array.from(hexToUint8Array(value)),
}
BINARY: (value: string) => Array.from(hexToUint8Array(value)),
VARBINARY: (value: string) => Array.from(hexToUint8Array(value)),
BLOB: (value: string) => Array.from(hexToUint8Array(value)),
LONGBLOB: (value: string) => Array.from(hexToUint8Array(value)),
TINYINT: (value: string) => Array.from(hexToUint8Array(value)),
MEDIUMBLOB: (value: string) => Array.from(hexToUint8Array(value)),
BIT: (value: string) => Array.from(hexToUint8Array(value)),
};

function hexToUint8Array(hexString: string): Uint8Array {
const uint8Array = new Uint8Array(hexString.length / 2)
const uint8Array = new Uint8Array(hexString.length / 2);
for (let i = 0; i < hexString.length; i += 2) {
uint8Array[i / 2] = parseInt(hexString.substring(i, i + 2), 16)
uint8Array[i / 2] = parseInt(hexString.substring(i, i + 2), 16);
}
return uint8Array
}
return uint8Array;
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { PrismaTiDBCloud } from './tidbcloud'
export { PrismaTiDBCloud } from "./tidbcloud";
Loading

0 comments on commit 13f57be

Please sign in to comment.