-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ccc2f0a
commit 19e5f43
Showing
6 changed files
with
87 additions
and
4 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
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
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { expect } from 'chai' | ||
import { | ||
DatabaseConnection, | ||
DummyDriver, | ||
Kysely, | ||
PostgresAdapter, | ||
PostgresIntrospector, | ||
PostgresQueryCompiler, | ||
RootOperationNode, | ||
QueryId, | ||
} from '../../..' | ||
import { Database } from './test-setup' | ||
|
||
describe('queryId', () => { | ||
const visits = new Map() | ||
|
||
const db = new Kysely<Database>({ | ||
dialect: { | ||
createAdapter: () => new PostgresAdapter(), | ||
createDriver: () => | ||
new (class extends DummyDriver { | ||
async acquireConnection(): Promise<DatabaseConnection> { | ||
// @ts-ignore | ||
return { | ||
executeQuery: async ({ queryId }) => { | ||
checkIn(queryId, 'connection.executeQuery') | ||
|
||
return { | ||
rows: [], | ||
} | ||
}, | ||
} | ||
} | ||
})(), | ||
createIntrospector: (db) => new PostgresIntrospector(db), | ||
createQueryCompiler: () => | ||
new (class SomeCompiler extends PostgresQueryCompiler { | ||
compileQuery(node: RootOperationNode, queryId: QueryId) { | ||
checkIn(queryId, 'compiler.compileQuery') | ||
|
||
return super.compileQuery(node, queryId) | ||
} | ||
})(), | ||
}, | ||
plugins: [ | ||
{ | ||
transformQuery: (args) => { | ||
checkIn(args.queryId, 'plugin.transformQuery') | ||
|
||
return args.node | ||
}, | ||
transformResult: async (args) => { | ||
checkIn(args.queryId, 'plugin.transformResult') | ||
|
||
return args.result | ||
}, | ||
}, | ||
], | ||
}) | ||
|
||
it('should pass query id around, allowing async communication between compilers, plugins and connections', async () => { | ||
await db.selectFrom('person').where('id', '=', 1).execute() | ||
|
||
expect(Array.from(visits.values())[0]).to.deep.equal([ | ||
'plugin.transformQuery', | ||
'compiler.compileQuery', | ||
'connection.executeQuery', | ||
'plugin.transformResult', | ||
]) | ||
}) | ||
|
||
function checkIn(queryId: QueryId, place: string): void { | ||
visits.set(queryId, [...(visits.get(queryId) || []), place]) | ||
} | ||
}) |