-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from tidbcloud/playground
feat: playground
- Loading branch information
Showing
32 changed files
with
1,472 additions
and
387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
node_modules | ||
dist | ||
build | ||
|
||
# Local Netlify folder | ||
.netlify |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default () => new Response('Hello world') | ||
|
||
export const config = { path: '/api/hello' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { connect } from 'https://esm.sh/@tidbcloud/serverless' | ||
|
||
type SchemaRes = { | ||
name: string | ||
tables: { | ||
name: string | ||
columns: { | ||
col: string | ||
data_type: string | ||
nullable: boolean | ||
}[] | ||
}[] | ||
}[] | ||
|
||
export default async () => { | ||
// url: | ||
// mysql://[user]:[pwd]@[host]/ | ||
// database is skipped and its default value is `test` | ||
const conn = connect({ | ||
url: Netlify.env.get('TIDBCLOUD_DATABASE_URL') | ||
}) | ||
|
||
let schema: SchemaRes = [] | ||
|
||
// step 1: get all databases | ||
let dbs = await conn.execute('show databases') | ||
// responses: | ||
// [ | ||
// { | ||
// "Database": "INFORMATION_SCHEMA" | ||
// }, | ||
// { | ||
// "Database": "PERFORMANCE_SCHEMA" | ||
// }, | ||
// { | ||
// "Database": "game" | ||
// }, | ||
// ... | ||
// ] | ||
schema = dbs | ||
.map((db) => db.Database) | ||
.filter( | ||
(db) => | ||
![ | ||
'INFORMATION_SCHEMA', | ||
'PERFORMANCE_SCHEMA', | ||
'lightning_task_info', | ||
'mysql' | ||
].includes(db) | ||
) | ||
.sort() | ||
.map((db) => ({ name: db, tables: [] })) | ||
console.log('dbs:', schema) | ||
|
||
// step 2: get tables for each db | ||
for (let i = 0; i < schema.length; i++) { | ||
const db = schema[i] | ||
const tables = await conn.execute( | ||
` | ||
SELECT | ||
TABLE_NAME as name | ||
FROM | ||
information_schema.tables | ||
WHERE | ||
table_schema = ? | ||
ORDER BY | ||
name ASC`, | ||
[db.name] | ||
) | ||
db.tables = tables.map((t) => ({ ...t, columns: [] })) | ||
|
||
// step 3: get columns for each table | ||
for (let j = 0; j < db.tables.length; j++) { | ||
const table = db.tables[j] | ||
|
||
const columns = await conn.execute( | ||
` | ||
SELECT | ||
COLUMN_NAME as col, DATA_TYPE as data_type, IS_NULLABLE as is_nullable | ||
FROM | ||
information_schema.columns | ||
WHERE | ||
table_schema = ? AND table_name = ? | ||
ORDER BY | ||
col ASC`, | ||
[db.name, table.name] | ||
) | ||
table.columns = columns.map((c) => ({ | ||
col: c.col, | ||
data_type: c.data_type, | ||
nullable: c.is_nullable === 'YES' | ||
})) | ||
} | ||
} | ||
return new Response( | ||
JSON.stringify({ code: 200, message: 'ok', data: schema }), | ||
{ | ||
headers: { | ||
'cache-control': 'public, s-maxage=3600' | ||
} | ||
} | ||
) | ||
} | ||
|
||
export const config = { path: '/api/schema', cache: 'manual' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { Config, Context } from '@netlify/edge-functions' | ||
import { connect } from 'https://esm.sh/@tidbcloud/serverless' | ||
|
||
type StatementReq = { | ||
database: string | ||
sql: string | ||
} | ||
|
||
export default async (req: Request, _context: Context) => { | ||
const body: StatementReq = await req.json() | ||
|
||
// url: | ||
// mysql://[user]:[pwd]@[host]/ | ||
// database is skipped and its default value is `test` | ||
const conn = connect({ | ||
url: Netlify.env.get('TIDBCLOUD_DATABASE_URL') + body.database | ||
}) | ||
|
||
if (body.sql.trim().toLowerCase().startsWith('select')) { | ||
const res = await conn.execute(body.sql, null, { | ||
arrayMode: true, | ||
fullResult: true | ||
}) | ||
|
||
return new Response(JSON.stringify({ code: 200, message: 'ok', data: res })) | ||
} else { | ||
return new Response( | ||
JSON.stringify({ | ||
code: 403, | ||
message: | ||
'forbidden, only select statement is available to run in this playground', | ||
data: {} | ||
}), | ||
{ status: 403 } | ||
) | ||
} | ||
} | ||
|
||
export const config: Config = { path: '/api/statement' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.