-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: added Express
request handler
#7
Changes from all commits
fe3eac2
a2696bb
e4b7b0d
4aab9c0
99c37de
9850912
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,4 +1,10 @@ | ||
export { collectLast } from './util/generators/collectLast'; | ||
export { | ||
type ExpressSynthqlHandler, | ||
type ExpressSynthqlHandlerRequest, | ||
type ExpressSynthqlHandlerResponse, | ||
createExpressSynthqlHandler, | ||
} from './util/handlers/createExpressSynthqlHandler'; | ||
|
||
export type * from './types/QueryPlan'; | ||
export * from './QueryEngine'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { describe, test } from 'vitest'; | ||
import { query } from '@synthql/queries'; | ||
|
||
import { DB } from '../../tests/db'; | ||
const from = query<DB>().from; | ||
|
||
describe('createExpressSynthqlHandler', () => { | ||
test(`Query execution is successful`, async () => { | ||
const q = from('actor') | ||
fhur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.columns('actor_id', 'first_name', 'last_name') | ||
.groupingId('actor_id') | ||
.where({ actor_id: { in: [1] } }) | ||
.one(); | ||
}); | ||
|
||
test(`Query execution is successful with returnLastOnly passed`, async () => { | ||
const q = from('actor') | ||
.columns('actor_id', 'first_name', 'last_name') | ||
.groupingId('actor_id') | ||
.where({ actor_id: { in: [1] } }) | ||
.maybe(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { QueryEngine, collectLast } from '../..'; | ||
import type { Request, Response } from 'express'; | ||
|
||
export type ExpressSynthqlHandlerRequest = Pick<Request, 'body' | 'headers'>; | ||
|
||
export type ExpressSynthqlHandlerResponse = Pick< | ||
Response, | ||
'statusCode' | 'write' | 'setHeader' | 'end' | ||
>; | ||
|
||
export type ExpressSynthqlHandler = ( | ||
req: ExpressSynthqlHandlerRequest, | ||
res: ExpressSynthqlHandlerResponse, | ||
) => void; | ||
|
||
export function createExpressSynthqlHandler<T>( | ||
queryEngine: QueryEngine<T>, | ||
): ExpressSynthqlHandler { | ||
return async (req, res) => { | ||
try { | ||
const headers = req.headers; | ||
const query = await JSON.parse(req.body); | ||
const returnLastOnly = headers['x-return-last-only'] === 'true'; | ||
|
||
if (returnLastOnly) { | ||
try { | ||
const result = await collectLast( | ||
queryEngine.execute(query, { | ||
returnLastOnly, | ||
}), | ||
); | ||
|
||
res.statusCode = 200; | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.write(JSON.stringify(result)); | ||
res.end(); | ||
} catch (error) { | ||
res.statusCode = 500; | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.write(JSON.stringify({ error: String(error) })); | ||
res.end(); | ||
} | ||
} else { | ||
res.statusCode = 200; | ||
res.setHeader('Content-Type', 'application/x-ndjson'); | ||
|
||
for await (const intermediateResult of queryEngine.execute( | ||
query, | ||
)) { | ||
res.write(JSON.stringify(intermediateResult)); | ||
res.write('\n'); | ||
} | ||
|
||
res.end(); | ||
} | ||
} catch (error) { | ||
res.statusCode = 400; | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.write(JSON.stringify({ error: 'Invalid JSON body' })); | ||
res.end(); | ||
} | ||
}; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { IncomingMessage, ServerResponse } from 'http'; | ||
import { | ||
ExpressSynthqlHandler, | ||
ExpressSynthqlHandlerRequest, | ||
ExpressSynthqlHandlerResponse, | ||
} from '@synthql/backend'; | ||
|
||
function readBody(req: IncomingMessage): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
let body = ''; | ||
|
||
req.on('data', (chunk) => { | ||
body += chunk; | ||
}); | ||
|
||
req.on('end', () => { | ||
resolve(body); | ||
}); | ||
|
||
req.on('error', (e) => { | ||
reject(e); | ||
}); | ||
}); | ||
} | ||
|
||
export type IncomingMessageWithBody = IncomingMessage & | ||
ExpressSynthqlHandlerRequest; | ||
|
||
export type ServerResponseWithEnd = ServerResponse & | ||
ExpressSynthqlHandlerResponse; | ||
|
||
export function appendBody(expressHandler: ExpressSynthqlHandler) { | ||
return async (req: IncomingMessage, res: ServerResponse) => { | ||
const body = await readBody(req); | ||
|
||
const newReq = { | ||
...req, | ||
headers: req.headers, | ||
body: body, | ||
} as IncomingMessageWithBody; | ||
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. don't do const newReq: Type = { ... } 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 would be ideal, but the goal here is actually type casting. We don't have all the values for the type, so using a type annotation in this case, causes a type mismatch error. |
||
|
||
const newRes = res as ServerResponseWithEnd; | ||
|
||
expressHandler(newReq, newRes); | ||
}; | ||
} |
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.
This test isn't really testing anything useful @jimezesinachi
A few of the important invariants you should be testing:
http.createServer