-
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.
Signed-off-by: Jim Ezesinachi <ezesinachijim@gmail.com>
- Loading branch information
1 parent
e4b7b0d
commit 4aab9c0
Showing
8 changed files
with
87 additions
and
158 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,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'; |
This file was deleted.
Oops, something went wrong.
85 changes: 3 additions & 82 deletions
85
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 |
---|---|---|
@@ -1,102 +1,23 @@ | ||
import { afterAll, beforeAll, describe, expect, test } from 'vitest'; | ||
import { | ||
type ExpressSynthqlHandler, | ||
createExpressSynthqlHandler, | ||
} from './createExpressSynthqlHandler'; | ||
import { queryEngine } from '../../tests/queryEngine'; | ||
import { getMockReq, getMockRes } from 'vitest-mock-express'; | ||
import { describe, test } from 'vitest'; | ||
import { query } from '@synthql/queries'; | ||
|
||
import { DB } from '../../tests/db'; | ||
const from = query<DB>().from; | ||
|
||
import { HttpServer, createHttpServer } from '../../tests/createHttpServer'; | ||
import { query } from '@synthql/queries'; | ||
|
||
describe('createExpressSynthqlHandler', () => { | ||
let httpServer: HttpServer; | ||
let handler: ExpressSynthqlHandler; | ||
|
||
beforeAll(async () => { | ||
handler = await createExpressSynthqlHandler<DB>(queryEngine); | ||
|
||
// httpServer = await createHttpServer(handler); | ||
}); | ||
|
||
afterAll(() => { | ||
// httpServer?.server.close(); | ||
}); | ||
|
||
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(); | ||
|
||
// Mock Express request object | ||
const request = { | ||
method: 'POST', | ||
url: '/synthql', | ||
body: JSON.stringify(q), | ||
}; | ||
|
||
const req = getMockReq(request); | ||
const { res } = getMockRes(); | ||
|
||
handler(req, res); | ||
|
||
expect(req.body).toEqual(request.body); | ||
|
||
expect(res.status).toHaveBeenCalledWith(200); | ||
|
||
expect(res.setHeader).toHaveBeenCalledWith( | ||
'Content-Type', | ||
'application/x-ndjson', | ||
); | ||
|
||
expect(res.write).toHaveBeenCalledWith(expect.stringContaining('\n')); | ||
|
||
expect(res.end).toHaveBeenCalled(); | ||
}); | ||
|
||
test(`Query execution is successful with returnLastOnly passed`, async () => { | ||
const handler = await createExpressSynthqlHandler<DB>(queryEngine); | ||
|
||
const q = from('actor') | ||
.columns('actor_id', 'first_name', 'last_name') | ||
.groupingId('actor_id') | ||
.where({ actor_id: { in: [1] } }) | ||
.maybe(); | ||
|
||
// Mock Express request object | ||
const request = { | ||
method: 'POST', | ||
url: '/synthql', | ||
body: JSON.stringify(q), | ||
headers: { | ||
'x-return-last-only': 'true', | ||
}, | ||
}; | ||
|
||
const req = getMockReq(request); | ||
const { res } = getMockRes(); | ||
|
||
handler(req, res); | ||
|
||
expect(req.headers['x-return-last-only']).toEqual( | ||
request.headers['x-return-last-only'], | ||
); | ||
|
||
expect(req.body).toEqual(request.body); | ||
|
||
expect(res.status).toHaveBeenCalledWith(200); | ||
|
||
expect(res.setHeader).toHaveBeenCalledWith( | ||
'Content-Type', | ||
'application/x-ndjson', | ||
); | ||
|
||
expect(res.write).toHaveBeenCalledWith(expect.stringContaining('\n')); | ||
|
||
expect(res.end).toHaveBeenCalled(); | ||
}); | ||
}); |
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
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