Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

fix: removes header and use post request with body #12

Merged
merged 2 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@factorialco/tentaclesql",
"version": "0.3.0",
"version": "0.4.0",
"description": "SQL engine from multiple sources",
"bin": {
"tentaclesql": "dist/cli.js"
Expand Down
8 changes: 5 additions & 3 deletions src/executor/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ test('executor', async () => {
const sql = 'SELECT employees.id + goal_configs.id as value FROM goal_configs JOIN employees ON (employees.id + ?) == goal_configs.id'

const result = await executor(sql, [5], headers)
const ast = JSON.stringify(parseSql(sql))
const body = JSON.stringify(
{ query_ast: parseSql(sql) }
)

expect(mockedFetch).toHaveBeenCalledTimes(3)
expect(mockedFetch).toHaveBeenCalledWith('https://api.example.com/schema', { headers })
expect(mockedFetch).toHaveBeenCalledWith('https://api.example.com/tables/goal_configs', { headers: { ...headers, 'x-tentacle-query-ast': ast } })
expect(mockedFetch).toHaveBeenCalledWith('https://api.example.com/tables/employees', { headers: { ...headers, 'x-tentacle-query-ast': ast } })
expect(mockedFetch).toHaveBeenCalledWith('https://api.example.com/tables/goal_configs', { headers: headers, method: 'POST', body: body })
expect(mockedFetch).toHaveBeenCalledWith('https://api.example.com/tables/employees', { headers: headers, method: 'POST', body: body })
expect(result).toEqual([{ value: 25 }])
})

Expand Down
22 changes: 15 additions & 7 deletions src/executor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ function mutateDataframe (
return df.forEach(row => { Object.keys(row).forEach(k => fn(row, k)) })
}

async function fetchTableData (tableDefinition: TableDefinition, headers: any) {
const res = await fetch(tableDefinition.url, { headers })
async function fetchTableData (tableDefinition: TableDefinition, headers: any, queryAst: any) {
const res = await fetch(tableDefinition.url, {
headers: headers,
method: 'POST',
body: JSON.stringify({
query_ast: queryAst
})
}
)

if (!res.ok) {
return Promise.reject(new Error(`Error with the request. Status code: ${res.status}`))
Expand All @@ -53,7 +60,8 @@ async function populateTables (
db: DatabaseType,
usedTables: Array<string>,
headers: any,
schema: any
schema: any,
queryAst: any
) {
const filteredTableDefinition = schema.filter((
tableDefinition: TableDefinition
Expand All @@ -62,7 +70,7 @@ async function populateTables (
const promises = filteredTableDefinition.map(async (tableDefinition: TableDefinition) => {
createTable(db, tableDefinition)

const data = await fetchTableData(tableDefinition, headers)
const data = await fetchTableData(tableDefinition, headers, queryAst)

if (data.length === 0) return

Expand Down Expand Up @@ -125,10 +133,10 @@ async function executor (
usedTables, {
...headers,
host: getHost(),
'user-agent': `tentaclesql/${version}`,
'x-tentacle-query-ast': JSON.stringify(ast)
'user-agent': `tentaclesql/${version}`
},
schema
schema,
ast
)
}

Expand Down