Skip to content

Commit

Permalink
add queryId to CompiledQuery.
Browse files Browse the repository at this point in the history
  • Loading branch information
igalklebanov committed Nov 4, 2024
1 parent ccc2f0a commit 19e5f43
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export {
} from './util/type-utils.js'
export * from './util/infer-result.js'
export { logOnce } from './util/log-once.js'
export { QueryId } from './util/query-id.js'

export {
SelectExpression,
Expand Down
3 changes: 3 additions & 0 deletions src/query-compiler/compiled-query.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { RawNode } from '../operation-node/raw-node.js'
import { freeze } from '../util/object-utils.js'
import { createQueryId, QueryId } from '../util/query-id.js'
import { RootOperationNode } from './query-compiler.js'

export interface CompiledQuery<O = unknown> {
readonly query: RootOperationNode
readonly queryId: QueryId
readonly sql: string
readonly parameters: ReadonlyArray<unknown>
}
Expand All @@ -14,6 +16,7 @@ export const CompiledQuery = freeze({
sql,
query: RawNode.createWithSql(sql),
parameters: freeze(parameters),
queryId: createQueryId(),
})
},
})
4 changes: 3 additions & 1 deletion src/query-compiler/default-query-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import { FetchNode } from '../operation-node/fetch-node.js'
import { TopNode } from '../operation-node/top-node.js'
import { OutputNode } from '../operation-node/output-node.js'
import { RefreshMaterializedViewNode } from '../operation-node/refresh-materialized-view-node.js'
import { QueryId } from '../util/query-id.js'

export class DefaultQueryCompiler
extends OperationNodeVisitor
Expand All @@ -124,7 +125,7 @@ export class DefaultQueryCompiler
return this.#parameters.length
}

compileQuery(node: RootOperationNode): CompiledQuery {
compileQuery(node: RootOperationNode, queryId: QueryId): CompiledQuery {
this.#sql = ''
this.#parameters = []
this.nodeStack.splice(0, this.nodeStack.length)
Expand All @@ -133,6 +134,7 @@ export class DefaultQueryCompiler

return freeze({
query: node,
queryId,
sql: this.getSql(),
parameters: [...this.#parameters],
})
Expand Down
3 changes: 2 additions & 1 deletion src/query-compiler/query-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { MergeQueryNode } from '../operation-node/merge-query-node.js'
import { QueryNode } from '../operation-node/query-node.js'
import { RawNode } from '../operation-node/raw-node.js'
import { RefreshMaterializedViewNode } from '../operation-node/refresh-materialized-view-node.js'
import { QueryId } from '../util/query-id.js'
import { CompiledQuery } from './compiled-query.js'

export type RootOperationNode =
Expand All @@ -36,5 +37,5 @@ export type RootOperationNode =
* a `QueryCompiler` compiles a query expressed as a tree of `OperationNodes` into SQL.
*/
export interface QueryCompiler {
compileQuery(node: RootOperationNode): CompiledQuery
compileQuery(node: RootOperationNode, queryId: QueryId): CompiledQuery
}
5 changes: 3 additions & 2 deletions src/query-executor/default-query-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { KyselyPlugin } from '../plugin/kysely-plugin.js'
import { QueryExecutorBase } from './query-executor-base.js'
import { DialectAdapter } from '../dialect/dialect-adapter.js'
import { QueryId } from '../util/query-id.js'

export class DefaultQueryExecutor extends QueryExecutorBase {
#compiler: QueryCompiler
Expand All @@ -31,8 +32,8 @@ export class DefaultQueryExecutor extends QueryExecutorBase {
return this.#adapter
}

compileQuery(node: RootOperationNode): CompiledQuery {
return this.#compiler.compileQuery(node)
compileQuery(node: RootOperationNode, queryId: QueryId): CompiledQuery {
return this.#compiler.compileQuery(node, queryId)
}

provideConnection<T>(
Expand Down
75 changes: 75 additions & 0 deletions test/node/src/query-id.test.ts
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])
}
})

0 comments on commit 19e5f43

Please sign in to comment.