-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Fix response streams in template #42
Changes from all commits
b4d5f31
fbf2c2f
59d9e37
4a2fdca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
import { IDataQueryBuilder } from '@vulcan-sql/core/data-query'; | ||
import { | ||
DataResult, | ||
FilterRunner, | ||
FilterRunnerTransformOptions, | ||
VulcanInternalExtension, | ||
} from '@vulcan-sql/core/models'; | ||
import { streamToArray } from '@vulcan-sql/core/utils'; | ||
import { EXECUTE_FILTER_NAME } from './constants'; | ||
|
||
const isDataResult = (response: any): response is DataResult => { | ||
return response.getColumns && response.getData; | ||
}; | ||
|
||
@VulcanInternalExtension() | ||
export class ExecutorRunner extends FilterRunner { | ||
public filterName = EXECUTE_FILTER_NAME; | ||
|
||
public async transform({ | ||
value, | ||
value: builder, | ||
}: FilterRunnerTransformOptions): Promise<any> { | ||
const builder: IDataQueryBuilder = value; | ||
return builder.value(); | ||
const response = await builder.value(); | ||
|
||
// if input value is not a query builder, call the function .value and do nothing. | ||
if (!isDataResult(response)) return response; | ||
|
||
const { getData } = response; | ||
const dataStream = getData(); | ||
const data = await streamToArray(dataStream); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we execute There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This filter here doesn't affect the final query builder, the formators will receive an unchanged stream. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, thanks |
||
return data; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './normalizedStringValue'; | ||
export * from './logger'; | ||
export * from './module'; | ||
export * from './streams'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Readable } from 'stream'; | ||
|
||
export const arrayToStream = (array: any[]) => { | ||
let index = 0; | ||
const stream = new Readable({ | ||
objectMode: true, | ||
read() { | ||
if (index >= array.length) { | ||
this.push(null); | ||
return; | ||
} | ||
this.push(array[index++]); | ||
}, | ||
}); | ||
return stream; | ||
}; | ||
|
||
export const streamToArray = (stream: Readable) => { | ||
const rows: any[] = []; | ||
return new Promise<any[]>((resolve, reject) => { | ||
stream.on('data', (data) => { | ||
rows.push(data); | ||
}); | ||
stream.on('end', () => { | ||
resolve(rows); | ||
}); | ||
stream.on('error', (err) => reject(err)); | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious about
replace
, when we callreplace
what do we replace to ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a callback function to remove or replace the current node in order to modify the AST tree, which provided by visitChildren helper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for replying to me