Skip to content

Commit

Permalink
add RawBuilder constructor that can easily be used without a Kysely i…
Browse files Browse the repository at this point in the history
…nstance
  • Loading branch information
koskimas committed Jan 21, 2022
1 parent 53b23a6 commit 7c12299
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kysely",
"version": "0.16.7",
"version": "0.16.8",
"description": "Type safe SQL query builder",
"repository": {
"type": "git",
Expand Down
6 changes: 4 additions & 2 deletions src/query-executor/query-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { KyselyPlugin } from '../plugin/kysely-plugin.js'
import { freeze } from '../util/object-utils.js'
import { QueryId } from '../util/query-id.js'

const NO_PLUGINS: ReadonlyArray<KyselyPlugin> = freeze([])

/**
* This class abstracts away the details of how to compile a query into SQL
* and execute it. Instead of passing around all those details, {@link SelectQueryBuilder}
Expand All @@ -15,8 +17,8 @@ import { QueryId } from '../util/query-id.js'
export abstract class QueryExecutor {
readonly #plugins: ReadonlyArray<KyselyPlugin>

constructor(plugins: KyselyPlugin[] = []) {
this.#plugins = freeze([...plugins])
constructor(plugins?: KyselyPlugin[]) {
this.#plugins = plugins ?? NO_PLUGINS
}

get plugins(): ReadonlyArray<KyselyPlugin> {
Expand Down
21 changes: 16 additions & 5 deletions src/raw-builder/raw-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ import { parseStringReference } from '../parser/reference-parser.js'
import { CompiledQuery } from '../query-compiler/compiled-query.js'
import { preventAwait } from '../util/prevent-await.js'
import { QueryExecutor } from '../query-executor/query-executor.js'
import { QueryId } from '../util/query-id.js'
import { createQueryId, QueryId } from '../util/query-id.js'
import { freeze } from '../util/object-utils.js'
import { KyselyPlugin } from '../plugin/kysely-plugin.js'
import { NoopQueryExecutor } from '../query-executor/noop-query-executor.js'

export class RawBuilder<O = unknown> implements OperationNodeSource {
readonly #props: RawBuilderProps
readonly #props: InternalRawBuilderProps

constructor(props: RawBuilderProps) {
this.#props = freeze(props)
this.#props = freeze({
queryId: props.queryId ?? createQueryId(),
executor: props.executor ?? new NoopQueryExecutor(),
sql: props.sql,
parameters: props.parameters,
})
}

as<A extends string>(alias: A): AliasedRawBuilder<O, A> {
Expand Down Expand Up @@ -146,8 +152,13 @@ function parseRawArg(match: string, arg: any): OperationNode {
}

export interface RawBuilderProps {
readonly queryId: QueryId
readonly executor: QueryExecutor
readonly queryId?: QueryId
readonly executor?: QueryExecutor
readonly sql: string
readonly parameters?: ReadonlyArray<unknown>
}

interface InternalRawBuilderProps extends RawBuilderProps {
readonly queryId: QueryId
readonly executor: QueryExecutor
}

0 comments on commit 7c12299

Please sign in to comment.