Skip to content

Commit 00fc855

Browse files
committed
add @spinframework/spin-mysql
Signed-off-by: karthik2804 <[email protected]>
1 parent 9d3ddc0 commit 00fc855

File tree

8 files changed

+572
-0
lines changed

8 files changed

+572
-0
lines changed

package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/spin-mysql/package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "@spinframework/spin-mysql",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"type": "module",
8+
"scripts": {
9+
"build": "tsc && cp -r types/* dist/ && cp -r src/types dist/"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "Apache-2.0",
14+
"devDependencies": {
15+
"typescript": "^5.7.3"
16+
},
17+
"config": {
18+
"wasiDep": {
19+
"witDeps": [
20+
{
21+
"witPath": "./wit",
22+
"package": "fermyon:[email protected]",
23+
"world": "spin-mysql"
24+
}
25+
]
26+
}
27+
}
28+
}

packages/spin-mysql/src/index.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as spinMysql from 'fermyon:spin/[email protected]';
2+
import {
3+
RdbmsParameterValue,
4+
RdbmsRow,
5+
RdbmsRowSet,
6+
SpinRdbmsRowSet,
7+
} from './types/rdbms';
8+
import { convertRdbmsToWitTypes } from './rdbmsHelper';
9+
10+
/**
11+
* Interface representing a MySQL connection with methods for querying and executing statements.
12+
* @interface MysqlConnection
13+
*/
14+
export interface MysqlConnection {
15+
query: (statement: string, params: RdbmsParameterValue[]) => RdbmsRowSet;
16+
execute: (statement: string, params: RdbmsParameterValue[]) => void;
17+
}
18+
19+
function createMysqlConnection(
20+
connection: spinMysql.Connection,
21+
): MysqlConnection {
22+
return {
23+
query: (statement: string, params: RdbmsParameterValue[]) => {
24+
let santizedParams = convertRdbmsToWitTypes(params);
25+
let ret = connection.query(statement, santizedParams) as SpinRdbmsRowSet;
26+
let results: RdbmsRowSet = {
27+
columns: ret.columns,
28+
rows: [],
29+
};
30+
ret.rows.map((k: RdbmsRow, rowIndex: number) => {
31+
results.rows.push({});
32+
k.map((val, valIndex: number) => {
33+
results.rows[rowIndex][results.columns[valIndex].name] =
34+
val.tag == 'db-null' || val.tag == 'unsupported' ? null : val.val;
35+
});
36+
});
37+
return results;
38+
},
39+
execute: (statement: string, params: RdbmsParameterValue[]) => {
40+
let santizedParams = convertRdbmsToWitTypes(params);
41+
let ret = connection.execute(statement, santizedParams);
42+
},
43+
};
44+
}
45+
46+
/**
47+
* Opens a MySQL connection to the specified address.
48+
* @param {string} address - The address of the MySQL server.
49+
* @returns {MysqlConnection} The MySQL connection object.
50+
*/
51+
export function open(address: string): MysqlConnection {
52+
return createMysqlConnection(spinMysql.Connection.open(address));
53+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { RdbmsParameterValue, SpinRdbmsParameterValue } from './types/rdbms';
2+
3+
export function convertRdbmsToWitTypes(
4+
parameters: RdbmsParameterValue[],
5+
): SpinRdbmsParameterValue[] {
6+
let sanitized: SpinRdbmsParameterValue[] = [];
7+
for (let k of parameters) {
8+
if (typeof k === 'object') {
9+
sanitized.push(k as SpinRdbmsParameterValue);
10+
continue;
11+
}
12+
if (typeof k === 'string') {
13+
sanitized.push({ tag: 'str', val: k });
14+
continue;
15+
}
16+
if (typeof k === null) {
17+
sanitized.push({ tag: 'db-null' });
18+
continue;
19+
}
20+
if (typeof k === 'boolean') {
21+
sanitized.push({ tag: 'boolean', val: k });
22+
continue;
23+
}
24+
if (typeof k === 'bigint') {
25+
sanitized.push({ tag: 'int64', val: k });
26+
continue;
27+
}
28+
if (typeof k === 'number') {
29+
isFloat(k)
30+
? sanitized.push({ tag: 'floating64', val: k })
31+
: sanitized.push({ tag: 'int32', val: k });
32+
continue;
33+
}
34+
if ((k as any) instanceof Uint8Array) {
35+
sanitized.push({ tag: 'binary', val: k });
36+
continue;
37+
}
38+
}
39+
return sanitized;
40+
}
41+
42+
function isFloat(number: number) {
43+
return number % 1 !== 0;
44+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
export type RdbmsValueBoolean = { tag: 'boolean'; val: boolean };
2+
export type RdbmsValueInt8 = { tag: 'int8'; val: number };
3+
export type RdbmsValueInt16 = { tag: 'int16'; val: number };
4+
export type RdbmsValueInt32 = { tag: 'int32'; val: number };
5+
export type RdbmsValueInt64 = { tag: 'int64'; val: bigint };
6+
export type RdbmsValueUint8 = { tag: 'uint8'; val: number };
7+
export type RdbmsValueUint16 = { tag: 'uint16'; val: number };
8+
export type RdbmsValueUint32 = { tag: 'uint32'; val: number };
9+
export type RdbmsValueUint64 = { tag: 'uint64'; val: bigint };
10+
export type RdbmsValueFloating32 = { tag: 'floating32'; val: number };
11+
export type RdbmsValueFloating64 = { tag: 'floating64'; val: number };
12+
export type RdbmsValueStr = { tag: 'str'; val: string };
13+
export type RdbmsValueBinary = { tag: 'binary'; val: Uint8Array };
14+
export type RdbmsValueDbNull = { tag: 'db-null' };
15+
16+
export type SpinRdbmsParameterValue =
17+
| RdbmsValueBoolean
18+
| RdbmsValueInt8
19+
| RdbmsValueInt16
20+
| RdbmsValueInt32
21+
| RdbmsValueInt64
22+
| RdbmsValueUint8
23+
| RdbmsValueUint16
24+
| RdbmsValueUint32
25+
| RdbmsValueUint64
26+
| RdbmsValueFloating32
27+
| RdbmsValueFloating64
28+
| RdbmsValueStr
29+
| RdbmsValueBinary
30+
| RdbmsValueDbNull;
31+
32+
export type RdbmsParameterValue =
33+
| SpinRdbmsParameterValue
34+
| number
35+
| bigint
36+
| boolean
37+
| null
38+
| Uint8Array
39+
| string;
40+
41+
export interface RdbmsColumn {
42+
name: string;
43+
dataType: RdbmsDataType;
44+
}
45+
46+
export type RdbmsRow = RdbmsDbValue[];
47+
48+
export interface SpinRdbmsRowSet {
49+
columns: RdbmsColumn[];
50+
rows: RdbmsRow[];
51+
}
52+
53+
export interface RdbmsRowSet {
54+
columns: RdbmsColumn[];
55+
rows: {
56+
[key: string]: number | boolean | bigint | null | string | Uint8Array;
57+
}[];
58+
}
59+
60+
export type RdbmsDbBoolean = { tag: 'boolean'; val: boolean };
61+
export type RdbmsDbInt8 = { tag: 'int8'; val: number };
62+
export type RdbmsDbInt16 = { tag: 'int16'; val: number };
63+
export type RdbmsDbInt32 = { tag: 'int32'; val: number };
64+
export type RdbmsDbInt64 = { tag: 'int64'; val: number };
65+
export type RdbmsDbUint8 = { tag: 'uint8'; val: number };
66+
export type RdbmsDbUint16 = { tag: 'uint16'; val: number };
67+
export type RdbmsDbUint32 = { tag: 'uint32'; val: number };
68+
export type RdbmsDbUint64 = { tag: 'uint64'; val: number };
69+
export type RdbmsDbFloating32 = { tag: 'floating32'; val: number };
70+
export type RdbmsDbFloating64 = { tag: 'floating64'; val: number };
71+
export type RdbmsDbStr = { tag: 'str'; val: string };
72+
export type RdbmsDbBinary = { tag: 'binary'; val: Uint8Array };
73+
export type RdbmsDbNull = { tag: 'db-null' };
74+
export type RdbmsDbUnsupported = { tag: 'unsupported' };
75+
76+
export type RdbmsDbValue =
77+
| RdbmsDbBoolean
78+
| RdbmsDbInt8
79+
| RdbmsDbInt16
80+
| RdbmsDbInt32
81+
| RdbmsDbInt64
82+
| RdbmsDbUint8
83+
| RdbmsDbUint16
84+
| RdbmsDbUint32
85+
| RdbmsDbUint64
86+
| RdbmsDbFloating32
87+
| RdbmsDbFloating64
88+
| RdbmsDbStr
89+
| RdbmsDbBinary
90+
| RdbmsDbNull
91+
| RdbmsDbUnsupported;
92+
93+
export enum RdbmsDataType {
94+
RdbmsBoolean = 'boolean',
95+
RdbmsInt8 = 'int8',
96+
RdbmsInt16 = 'int16',
97+
RdbmsInt32 = 'int32',
98+
RdbmsInt64 = 'int64',
99+
RdbmsUint8 = 'uint8',
100+
RdbmsUint16 = 'uint16',
101+
RdbmsUint32 = 'uint32',
102+
RdbmsUint64 = 'uint64',
103+
RdbmsFloating32 = 'floating32',
104+
RdbmsFloating64 = 'floating64',
105+
RdbmsStr = 'str',
106+
RdbmsBinary = 'binary',
107+
RdbmsOther = 'other',
108+
}

packages/spin-mysql/tsconfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "ES2020",
5+
"lib": [
6+
"ES2020",
7+
"WebWorker"
8+
],
9+
"moduleResolution": "node",
10+
"declaration": true,
11+
"outDir": "dist",
12+
"strict": true,
13+
"esModuleInterop": true,
14+
},
15+
"exclude": [
16+
"node_modules",
17+
"dist"
18+
]
19+
}

0 commit comments

Comments
 (0)