-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added
Express
request handler (#7)
- Loading branch information
1 parent
a846fac
commit b6e4f6e
Showing
9 changed files
with
151 additions
and
37 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
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,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'; |
23 changes: 23 additions & 0 deletions
23
packages/backend/src/util/handlers/createExpressSynthqlHandler.test.ts
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,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') | ||
.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(); | ||
}); | ||
}); |
63 changes: 63 additions & 0 deletions
63
packages/backend/src/util/handlers/createExpressSynthqlHandler.ts
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,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.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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; | ||
|
||
const newRes = res as ServerResponseWithEnd; | ||
|
||
expressHandler(newReq, newRes); | ||
}; | ||
} |
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