Comparing version 0.1.5 to 0.1.6
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PostgresDialect = void 0; | ||
const query_compiler_1 = require("../../query-compiler/query-compiler"); | ||
const default_query_compiler_1 = require("../../query-compiler/default-query-compiler"); | ||
const postgres_driver_1 = require("./postgres-driver"); | ||
@@ -12,5 +12,5 @@ class PostgresDialect { | ||
// The default query compiler is for postgres dialect. | ||
return new query_compiler_1.QueryCompiler(); | ||
return new default_query_compiler_1.DefaultQueryCompiler(); | ||
} | ||
} | ||
exports.PostgresDialect = PostgresDialect; |
@@ -6,9 +6,4 @@ "use strict"; | ||
class PostgresDriver extends driver_1.Driver { | ||
constructor() { | ||
super(...arguments); | ||
this.#pgPool = null; | ||
this.#pgClients = new WeakMap(); | ||
} | ||
#pgPool; | ||
#pgClients; | ||
#pgPool = null; | ||
#pgClients = new WeakMap(); | ||
async init() { | ||
@@ -67,6 +62,6 @@ // Import the `pg` module here instead at the top of the file | ||
class PostgresConnection { | ||
#pgClient; | ||
constructor(pgClient) { | ||
this.#pgClient = pgClient; | ||
} | ||
#pgClient; | ||
async execute(compiledQuery) { | ||
@@ -73,0 +68,0 @@ const result = await this.#pgClient.query(compiledQuery.sql, [ |
@@ -6,4 +6,5 @@ "use strict"; | ||
class Driver { | ||
config; | ||
#initPromise = null; | ||
constructor(config) { | ||
this.#initPromise = null; | ||
this.config = object_utils_1.freeze({ | ||
@@ -16,3 +17,2 @@ ...config, | ||
} | ||
#initPromise; | ||
/** | ||
@@ -19,0 +19,0 @@ * @internal |
@@ -5,2 +5,4 @@ "use strict"; | ||
class TransactionalConnectionProvider { | ||
#driver; | ||
#transactions; | ||
constructor(driver, transactions) { | ||
@@ -10,4 +12,2 @@ this.#driver = driver; | ||
} | ||
#driver; | ||
#transactions; | ||
async withConnection(runner) { | ||
@@ -14,0 +14,0 @@ const connection = await this.acquireConnection(); |
@@ -6,5 +6,2 @@ "use strict"; | ||
class DynamicReferenceBuilder { | ||
constructor(reference) { | ||
this.#dynamicReference = reference; | ||
} | ||
#dynamicReference; | ||
@@ -25,2 +22,5 @@ get dynamicReference() { | ||
} | ||
constructor(reference) { | ||
this.#dynamicReference = reference; | ||
} | ||
toOperationNode() { | ||
@@ -27,0 +27,0 @@ return reference_parser_1.parseStringReference(this.#dynamicReference); |
export * from './kysely'; | ||
export * from './query-builder/query-builder'; | ||
export * from './query-builder/join-builder'; | ||
export * from './raw-builder/raw-builder'; | ||
export * from './query-compiler/query-compiler'; | ||
export * from './query-compiler/default-query-compiler'; | ||
export * from './query-compiler/compiled-query'; | ||
export * from './schema/schema'; | ||
export * from './driver/driver'; | ||
export * from './driver/driver-config'; | ||
export * from './driver/connection'; | ||
export * from './query-compiler/default-query-compiler'; | ||
export * from './query-compiler/query-compiler'; |
@@ -15,5 +15,11 @@ "use strict"; | ||
__exportStar(require("./query-builder/query-builder"), exports); | ||
__exportStar(require("./query-builder/join-builder"), exports); | ||
__exportStar(require("./raw-builder/raw-builder"), exports); | ||
__exportStar(require("./query-compiler/query-compiler"), exports); | ||
__exportStar(require("./query-compiler/default-query-compiler"), exports); | ||
__exportStar(require("./query-compiler/compiled-query"), exports); | ||
__exportStar(require("./schema/schema"), exports); | ||
__exportStar(require("./driver/driver"), exports); | ||
__exportStar(require("./driver/driver-config"), exports); | ||
__exportStar(require("./driver/connection"), exports); | ||
__exportStar(require("./query-compiler/default-query-compiler"), exports); | ||
__exportStar(require("./query-compiler/query-compiler"), exports); |
@@ -210,4 +210,4 @@ import { QueryBuilder } from './query-builder/query-builder'; | ||
* | ||
* See {@link QueryBuilder.where} for examples on how to specify the where | ||
* clauses for the delete operation. | ||
* See the {@link QueryBuilder.where} method for examples on how to specify | ||
* a where clause for the delete operation. | ||
* | ||
@@ -226,4 +226,4 @@ * @example | ||
* | ||
* See {@link QueryBuilder.where} for examples on how to specify the where | ||
* clauses for the update operation. | ||
* See the {@link QueryBuilder.where} method for examples on how to specify | ||
* a where clause for the update operation. | ||
* | ||
@@ -230,0 +230,0 @@ * @example |
@@ -51,4 +51,7 @@ "use strict"; | ||
class Kysely { | ||
#driver; | ||
#compiler; | ||
#transactions = new async_hooks_1.AsyncLocalStorage(); | ||
#connectionProvider; | ||
constructor(config) { | ||
this.#transactions = new async_hooks_1.AsyncLocalStorage(); | ||
const dialect = createDialect(config); | ||
@@ -59,6 +62,2 @@ this.#driver = dialect.createDriver(config); | ||
} | ||
#driver; | ||
#compiler; | ||
#transactions; | ||
#connectionProvider; | ||
get schema() { | ||
@@ -125,4 +124,4 @@ return schema_1.createSchemaObject(this.#compiler, this.#connectionProvider); | ||
* | ||
* See {@link QueryBuilder.where} for examples on how to specify the where | ||
* clauses for the delete operation. | ||
* See the {@link QueryBuilder.where} method for examples on how to specify | ||
* a where clause for the delete operation. | ||
* | ||
@@ -147,4 +146,4 @@ * @example | ||
* | ||
* See {@link QueryBuilder.where} for examples on how to specify the where | ||
* clauses for the update operation. | ||
* See the {@link QueryBuilder.where} method for examples on how to specify | ||
* a where clause for the update operation. | ||
* | ||
@@ -151,0 +150,0 @@ * @example |
@@ -37,3 +37,3 @@ import { AliasNode } from '../operation-node/alias-node'; | ||
#private; | ||
readonly visitNode: (node: OperationNode) => void; | ||
protected readonly visitNode: (node: OperationNode) => void; | ||
protected visitSelectQuery(node: SelectQueryNode): void; | ||
@@ -40,0 +40,0 @@ protected visitSelection(node: SelectionNode): void; |
@@ -5,43 +5,40 @@ "use strict"; | ||
class OperationNodeVisitor { | ||
constructor() { | ||
this.#visitors = { | ||
AliasNode: this.visitAlias.bind(this), | ||
ColumnNode: this.visitColumn.bind(this), | ||
IdentifierNode: this.visitIdentifier.bind(this), | ||
RawNode: this.visitRaw.bind(this), | ||
ReferenceNode: this.visitReference.bind(this), | ||
SelectQueryNode: this.visitSelectQuery.bind(this), | ||
SelectionNode: this.visitSelection.bind(this), | ||
TableNode: this.visitTable.bind(this), | ||
FromNode: this.visitFrom.bind(this), | ||
SelectAllNode: this.visitSelectAll.bind(this), | ||
FilterNode: this.visitFilter.bind(this), | ||
AndNode: this.visitAnd.bind(this), | ||
OrNode: this.visitOr.bind(this), | ||
ValueNode: this.visitValue.bind(this), | ||
ValueListNode: this.visitValueList.bind(this), | ||
PrimitiveValueListNode: this.visitPrimitiveValueList.bind(this), | ||
ParensNode: this.visitParens.bind(this), | ||
JoinNode: this.visitJoin.bind(this), | ||
OperatorNode: this.visitOperator.bind(this), | ||
WhereNode: this.visitWhere.bind(this), | ||
InsertQueryNode: this.visitInsertQuery.bind(this), | ||
DeleteQueryNode: this.visitDeleteQuery.bind(this), | ||
ReturningNode: this.visitReturning.bind(this), | ||
CreateTableNode: this.visitCreateTable.bind(this), | ||
ColumnDefinitionNode: this.visitColumnDefinition.bind(this), | ||
DropTableNode: this.visitDropTable.bind(this), | ||
DataTypeNode: this.visitDataType.bind(this), | ||
OrderByNode: this.visitOrderBy.bind(this), | ||
OrderByItemNode: this.visitOrderByItem.bind(this), | ||
GroupByNode: this.visitGroupBy.bind(this), | ||
GroupByItemNode: this.visitGroupByItem.bind(this), | ||
UpdateQueryNode: this.visitUpdateQuery.bind(this), | ||
ColumnUpdateNode: this.visitColumnUpdate.bind(this), | ||
}; | ||
this.visitNode = (node) => { | ||
this.#visitors[node.kind](node); | ||
}; | ||
} | ||
#visitors; | ||
#visitors = { | ||
AliasNode: this.visitAlias.bind(this), | ||
ColumnNode: this.visitColumn.bind(this), | ||
IdentifierNode: this.visitIdentifier.bind(this), | ||
RawNode: this.visitRaw.bind(this), | ||
ReferenceNode: this.visitReference.bind(this), | ||
SelectQueryNode: this.visitSelectQuery.bind(this), | ||
SelectionNode: this.visitSelection.bind(this), | ||
TableNode: this.visitTable.bind(this), | ||
FromNode: this.visitFrom.bind(this), | ||
SelectAllNode: this.visitSelectAll.bind(this), | ||
FilterNode: this.visitFilter.bind(this), | ||
AndNode: this.visitAnd.bind(this), | ||
OrNode: this.visitOr.bind(this), | ||
ValueNode: this.visitValue.bind(this), | ||
ValueListNode: this.visitValueList.bind(this), | ||
PrimitiveValueListNode: this.visitPrimitiveValueList.bind(this), | ||
ParensNode: this.visitParens.bind(this), | ||
JoinNode: this.visitJoin.bind(this), | ||
OperatorNode: this.visitOperator.bind(this), | ||
WhereNode: this.visitWhere.bind(this), | ||
InsertQueryNode: this.visitInsertQuery.bind(this), | ||
DeleteQueryNode: this.visitDeleteQuery.bind(this), | ||
ReturningNode: this.visitReturning.bind(this), | ||
CreateTableNode: this.visitCreateTable.bind(this), | ||
ColumnDefinitionNode: this.visitColumnDefinition.bind(this), | ||
DropTableNode: this.visitDropTable.bind(this), | ||
DataTypeNode: this.visitDataType.bind(this), | ||
OrderByNode: this.visitOrderBy.bind(this), | ||
OrderByItemNode: this.visitOrderByItem.bind(this), | ||
GroupByNode: this.visitGroupBy.bind(this), | ||
GroupByItemNode: this.visitGroupByItem.bind(this), | ||
UpdateQueryNode: this.visitUpdateQuery.bind(this), | ||
ColumnUpdateNode: this.visitColumnUpdate.bind(this), | ||
}; | ||
visitNode = (node) => { | ||
this.#visitors[node.kind](node); | ||
}; | ||
visitSelectQuery(node) { | ||
@@ -48,0 +45,0 @@ if (node.selections) { |
import { JoinNode } from '../operation-node/join-node'; | ||
import { OperationNodeSource } from '../operation-node/operation-node-source'; | ||
import { RawBuilder } from '../raw-builder/raw-builder'; | ||
import { TableExpression, QueryBuilderWithTable } from '../parser/table-parser'; | ||
@@ -8,26 +7,34 @@ import { FilterOperatorArg } from '../parser/filter-parser'; | ||
import { ValueExpressionOrList } from '../parser/value-parser'; | ||
export declare class JoinBuilder<DB, TB extends keyof DB, O = {}> implements OperationNodeSource { | ||
export declare class JoinBuilder<DB, TB extends keyof DB> implements OperationNodeSource { | ||
#private; | ||
constructor(joinNode: JoinNode); | ||
/** | ||
* This method can be used to start a subquery. | ||
* | ||
* Works just like {@link QueryBuilder.subQuery}. | ||
*/ | ||
raw<T = unknown>(sql: string, args?: any[]): RawBuilder<T>; | ||
subQuery<F extends TableExpression<DB, TB>>(from: F[]): QueryBuilderWithTable<DB, TB, {}, F>; | ||
subQuery<F extends TableExpression<DB, TB>>(from: F): QueryBuilderWithTable<DB, TB, {}, F>; | ||
/** | ||
* Just like {@link QueryBuilder.where} but adds an item to the join's | ||
* `on` clause instead. | ||
* | ||
* See {@link QueryBuilder.where} for documentation and examples. | ||
*/ | ||
subQuery<F extends TableExpression<DB, TB>>(from: F[]): QueryBuilderWithTable<DB, TB, O, F>; | ||
on(lhs: ReferenceExpression<DB, TB>, op: FilterOperatorArg, rhs: ValueExpressionOrList<DB, TB>): JoinBuilder<DB, TB>; | ||
/** | ||
* Just like {@link QueryBuilder.whereRef} but adds an item to the join's | ||
* `on` clause instead. | ||
* | ||
* See {@link QueryBuilder.whereRef} for documentation and examples. | ||
*/ | ||
subQuery<F extends TableExpression<DB, TB>>(from: F): QueryBuilderWithTable<DB, TB, O, F>; | ||
onRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperatorArg, rhs: ReferenceExpression<DB, TB>): JoinBuilder<DB, TB>; | ||
/** | ||
* Just like {@link QueryBuilder.orWhereRef} but adds an item to the join's | ||
* `on` clause instead. | ||
* | ||
* See {@link QueryBuilder.orWhereRef} for documentation and examples. | ||
*/ | ||
onRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperatorArg, rhs: ReferenceExpression<DB, TB>): JoinBuilder<DB, TB, O>; | ||
/** | ||
* | ||
*/ | ||
on(lhs: ReferenceExpression<DB, TB>, op: FilterOperatorArg, rhs: ValueExpressionOrList<DB, TB>): JoinBuilder<DB, TB, O>; | ||
orOnRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperatorArg, rhs: ReferenceExpression<DB, TB>): JoinBuilder<DB, TB>; | ||
toOperationNode(): JoinNode; | ||
} |
@@ -5,3 +5,2 @@ "use strict"; | ||
const join_node_1 = require("../operation-node/join-node"); | ||
const raw_builder_1 = require("../raw-builder/raw-builder"); | ||
const table_parser_1 = require("../parser/table-parser"); | ||
@@ -12,12 +11,6 @@ const filter_parser_1 = require("../parser/filter-parser"); | ||
class JoinBuilder { | ||
#joinNode; | ||
constructor(joinNode) { | ||
this.#joinNode = joinNode; | ||
} | ||
#joinNode; | ||
/** | ||
* | ||
*/ | ||
raw(sql, args) { | ||
return new raw_builder_1.RawBuilder(sql, args); | ||
} | ||
subQuery(table) { | ||
@@ -29,4 +22,16 @@ return new query_builder_1.QueryBuilder({ | ||
/** | ||
* Just like {@link QueryBuilder.where} but adds an item to the join's | ||
* `on` clause instead. | ||
* | ||
* See {@link QueryBuilder.where} for documentation and examples. | ||
*/ | ||
on(lhs, op, rhs) { | ||
return new JoinBuilder(join_node_1.cloneJoinNodeWithOn(this.#joinNode, 'and', filter_parser_1.parseFilterArgs([lhs, op, rhs]))); | ||
} | ||
/** | ||
* Just like {@link QueryBuilder.whereRef} but adds an item to the join's | ||
* `on` clause instead. | ||
* | ||
* See {@link QueryBuilder.whereRef} for documentation and examples. | ||
*/ | ||
onRef(lhs, op, rhs) { | ||
@@ -36,6 +41,9 @@ return new JoinBuilder(join_node_1.cloneJoinNodeWithOn(this.#joinNode, 'and', filter_parser_1.parseReferenceFilterArgs(lhs, op, rhs))); | ||
/** | ||
* Just like {@link QueryBuilder.orWhereRef} but adds an item to the join's | ||
* `on` clause instead. | ||
* | ||
* See {@link QueryBuilder.orWhereRef} for documentation and examples. | ||
*/ | ||
on(lhs, op, rhs) { | ||
return new JoinBuilder(join_node_1.cloneJoinNodeWithOn(this.#joinNode, 'and', filter_parser_1.parseFilterArgs([lhs, op, rhs]))); | ||
orOnRef(lhs, op, rhs) { | ||
return new JoinBuilder(join_node_1.cloneJoinNodeWithOn(this.#joinNode, 'or', filter_parser_1.parseReferenceFilterArgs(lhs, op, rhs))); | ||
} | ||
@@ -42,0 +50,0 @@ toOperationNode() { |
import { AliasNode } from '../operation-node/alias-node'; | ||
import { OperationNodeSource } from '../operation-node/operation-node-source'; | ||
import { CompiledQuery } from '../query-compiler/compiled-query'; | ||
import { QueryCompiler } from '../query-compiler/query-compiler'; | ||
import { JoinCallbackArg, JoinReferenceArg } from '../parser/join-parser'; | ||
@@ -18,2 +17,3 @@ import { TableExpression, QueryBuilderWithTable } from '../parser/table-parser'; | ||
import { MutationObject } from '../parser/mutation-parser'; | ||
import { QueryCompiler } from '../query-compiler/query-compiler'; | ||
/** | ||
@@ -23,4 +23,4 @@ * The main query builder class. | ||
* @typeParam DB - A type whose keys are table names/aliases and values are interfaces that | ||
* define the table's columns and their typs. This type defines the tables, subqueries | ||
* etc. that are avaialable to the query. This type contains all tables, even the ones | ||
* define the table's columns and their types. This type defines the tables, subqueries | ||
* etc. that are available to the query. This type contains all tables, even the ones | ||
* that have not actually been joined to the query. the `TB` parameter defines the | ||
@@ -27,0 +27,0 @@ * table names/aliases that have been joined to the query. |
@@ -23,4 +23,4 @@ "use strict"; | ||
* @typeParam DB - A type whose keys are table names/aliases and values are interfaces that | ||
* define the table's columns and their typs. This type defines the tables, subqueries | ||
* etc. that are avaialable to the query. This type contains all tables, even the ones | ||
* define the table's columns and their types. This type defines the tables, subqueries | ||
* etc. that are available to the query. This type contains all tables, even the ones | ||
* that have not actually been joined to the query. the `TB` parameter defines the | ||
@@ -36,2 +36,5 @@ * table names/aliases that have been joined to the query. | ||
class QueryBuilder { | ||
#queryNode; | ||
#compiler; | ||
#connectionProvider; | ||
constructor({ queryNode, compiler, connectionProvider }) { | ||
@@ -42,5 +45,2 @@ this.#queryNode = queryNode; | ||
} | ||
#queryNode; | ||
#compiler; | ||
#connectionProvider; | ||
subQuery(table) { | ||
@@ -631,2 +631,4 @@ return new QueryBuilder({ | ||
class AliasedQueryBuilder { | ||
#queryBuilder; | ||
#alias; | ||
constructor(queryBuilder, alias) { | ||
@@ -636,4 +638,2 @@ this.#queryBuilder = queryBuilder; | ||
} | ||
#queryBuilder; | ||
#alias; | ||
/** | ||
@@ -640,0 +640,0 @@ * @private |
@@ -1,81 +0,8 @@ | ||
import { AliasNode } from '../operation-node/alias-node'; | ||
import { AndNode } from '../operation-node/and-node'; | ||
import { ColumnDefinitionNode } from '../operation-node/column-definition-node'; | ||
import { ColumnUpdateNode } from '../operation-node/column-update-node'; | ||
import { CreateTableNode } from '../operation-node/create-table-node'; | ||
import { DataTypeNode } from '../operation-node/data-type-node'; | ||
import { DeleteQueryNode } from '../operation-node/delete-query-node'; | ||
import { DropTableNode } from '../operation-node/drop-table-node'; | ||
import { FilterNode } from '../operation-node/filter-node'; | ||
import { FromNode } from '../operation-node/from-node'; | ||
import { GroupByItemNode } from '../operation-node/group-by-item-node'; | ||
import { GroupByNode } from '../operation-node/group-by-node'; | ||
import { IdentifierNode } from '../operation-node/identifier-node'; | ||
import { InsertQueryNode } from '../operation-node/insert-query-node'; | ||
import { JoinNode } from '../operation-node/join-node'; | ||
import { OperationNode } from '../operation-node/operation-node'; | ||
import { OperationNodeVisitor } from '../operation-node/operation-node-visitor'; | ||
import { OperatorNode } from '../operation-node/operator-node'; | ||
import { OrNode } from '../operation-node/or-node'; | ||
import { OrderByItemNode } from '../operation-node/order-by-item-node'; | ||
import { OrderByNode } from '../operation-node/order-by-node'; | ||
import { ParensNode } from '../operation-node/parens-node'; | ||
import { PrimitiveValueListNode } from '../operation-node/primitive-value-list-node'; | ||
import { QueryNode } from '../operation-node/query-node-utils'; | ||
import { RawNode } from '../operation-node/raw-node'; | ||
import { ReferenceNode } from '../operation-node/reference-node'; | ||
import { ReturningNode } from '../operation-node/returning-node'; | ||
import { SelectAllNode } from '../operation-node/select-all-node'; | ||
import { SelectQueryNode } from '../operation-node/select-query-node'; | ||
import { SelectionNode } from '../operation-node/selection-node'; | ||
import { TableNode } from '../operation-node/table-node'; | ||
import { UpdateQueryNode } from '../operation-node/update-query-node'; | ||
import { ValueListNode } from '../operation-node/value-list-node'; | ||
import { ValueNode } from '../operation-node/value-node'; | ||
import { WhereNode } from '../operation-node/where-node'; | ||
import { CompiledQuery } from './compiled-query'; | ||
export declare class QueryCompiler extends OperationNodeVisitor { | ||
#private; | ||
protected queryNodeStack: QueryNode[]; | ||
compile(node: QueryNode | CreateTableNode | DropTableNode): CompiledQuery; | ||
private getSql; | ||
private getBindings; | ||
protected visitSelectQuery(node: SelectQueryNode): void; | ||
protected visitFrom(node: FromNode): void; | ||
protected compileDistinctOn(selections: ReadonlyArray<SelectionNode>): void; | ||
protected compileList(nodes: ReadonlyArray<OperationNode>): void; | ||
protected visitWhere(node: WhereNode): void; | ||
protected visitInsertQuery(node: InsertQueryNode): void; | ||
protected visitDeleteQuery(node: DeleteQueryNode): void; | ||
protected visitReturning(node: ReturningNode): void; | ||
protected visitAlias(node: AliasNode): void; | ||
protected visitReference(node: ReferenceNode): void; | ||
protected visitSelectAll(_: SelectAllNode): void; | ||
protected visitIdentifier(node: IdentifierNode): void; | ||
protected compileUnwrappedIdentifier(node: IdentifierNode): void; | ||
protected visitFilter(node: FilterNode): void; | ||
protected visitAnd(node: AndNode): void; | ||
protected visitOr(node: OrNode): void; | ||
protected visitValue(node: ValueNode): void; | ||
protected visitValueList(node: ValueListNode): void; | ||
protected visitPrimitiveValueList(node: PrimitiveValueListNode): void; | ||
protected visitParens(node: ParensNode): void; | ||
protected visitJoin(node: JoinNode): void; | ||
protected visitRaw(node: RawNode): void; | ||
protected visitOperator(node: OperatorNode): void; | ||
protected visitTable(node: TableNode): void; | ||
protected visitCreateTable(node: CreateTableNode): void; | ||
protected visitColumnDefinition(node: ColumnDefinitionNode): void; | ||
protected visitDropTable(node: DropTableNode): void; | ||
protected visitDataType(node: DataTypeNode): void; | ||
protected visitOrderBy(node: OrderByNode): void; | ||
protected visitOrderByItem(node: OrderByItemNode): void; | ||
protected visitGroupBy(node: GroupByNode): void; | ||
protected visitGroupByItem(node: GroupByItemNode): void; | ||
protected visitUpdateQuery(node: UpdateQueryNode): void; | ||
protected visitColumnUpdate(node: ColumnUpdateNode): void; | ||
protected appendLeftIdentifierWrapper(): void; | ||
protected appendRightIdentifierWrapper(): void; | ||
protected append(str: string): void; | ||
protected appendValue(value: any): void; | ||
export declare type CompileEntryPointNode = QueryNode | CreateTableNode | DropTableNode; | ||
export interface QueryCompiler { | ||
compile(node: CompileEntryPointNode): CompiledQuery; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.QueryCompiler = void 0; | ||
const operation_node_visitor_1 = require("../operation-node/operation-node-visitor"); | ||
const object_utils_1 = require("../utils/object-utils"); | ||
class QueryCompiler extends operation_node_visitor_1.OperationNodeVisitor { | ||
constructor() { | ||
super(...arguments); | ||
this.#sqlFragments = []; | ||
this.#bindings = []; | ||
this.queryNodeStack = []; | ||
} | ||
#sqlFragments; | ||
#bindings; | ||
compile(node) { | ||
this.#sqlFragments = []; | ||
this.#bindings = []; | ||
this.queryNodeStack = []; | ||
this.visitNode(node); | ||
return object_utils_1.freeze({ | ||
sql: this.getSql(), | ||
bindings: this.getBindings(), | ||
}); | ||
} | ||
getSql() { | ||
return this.#sqlFragments.join(''); | ||
} | ||
getBindings() { | ||
return this.#bindings; | ||
} | ||
visitSelectQuery(node) { | ||
const needsParens = !object_utils_1.isEmpty(this.queryNodeStack); | ||
this.queryNodeStack.push(node); | ||
if (needsParens) { | ||
this.append('('); | ||
} | ||
this.append('select '); | ||
if (node.distinctOnSelections && !object_utils_1.isEmpty(node.distinctOnSelections)) { | ||
this.compileDistinctOn(node.distinctOnSelections); | ||
this.append(' '); | ||
} | ||
if (node.modifier === 'Distinct') { | ||
this.append(SELECT_MODIFIER_SQL[node.modifier]); | ||
this.append(' '); | ||
} | ||
if (node.selections) { | ||
this.compileList(node.selections); | ||
this.append(' '); | ||
} | ||
this.visitNode(node.from); | ||
if (node.joins) { | ||
this.append(' '); | ||
node.joins.forEach(this.visitNode); | ||
} | ||
if (node.where) { | ||
this.append(' '); | ||
this.visitNode(node.where); | ||
} | ||
if (node.groupBy) { | ||
this.append(' '); | ||
this.visitNode(node.groupBy); | ||
} | ||
if (node.orderBy) { | ||
this.append(' '); | ||
this.visitNode(node.orderBy); | ||
} | ||
if (node.modifier) { | ||
this.append(' '); | ||
this.append(SELECT_MODIFIER_SQL[node.modifier]); | ||
} | ||
if (needsParens) { | ||
this.append(')'); | ||
} | ||
this.queryNodeStack.pop(); | ||
} | ||
visitFrom(node) { | ||
this.append('from '); | ||
this.compileList(node.froms); | ||
} | ||
compileDistinctOn(selections) { | ||
this.append('distinct on ('); | ||
this.compileList(selections); | ||
this.append(')'); | ||
} | ||
compileList(nodes) { | ||
const lastNode = object_utils_1.getLast(nodes); | ||
for (const node of nodes) { | ||
this.visitNode(node); | ||
if (node !== lastNode) { | ||
this.append(', '); | ||
} | ||
} | ||
} | ||
visitWhere(node) { | ||
this.append('where '); | ||
this.visitNode(node.where); | ||
} | ||
visitInsertQuery(node) { | ||
this.queryNodeStack.push(node); | ||
this.append('insert into '); | ||
this.visitNode(node.into); | ||
if (node.columns) { | ||
this.append(' ('); | ||
this.compileList(node.columns); | ||
this.append(')'); | ||
} | ||
if (node.values) { | ||
this.append(' values '); | ||
if (node.values.length === 1) { | ||
this.visitNode(node.values[0]); | ||
} | ||
else { | ||
this.compileList(node.values); | ||
} | ||
} | ||
if (node.returning) { | ||
this.append(' '); | ||
this.visitNode(node.returning); | ||
} | ||
this.queryNodeStack.pop(); | ||
} | ||
visitDeleteQuery(node) { | ||
this.queryNodeStack.push(node); | ||
this.append('delete '); | ||
this.visitNode(node.from); | ||
if (node.joins) { | ||
this.append(' '); | ||
node.joins.forEach(this.visitNode); | ||
} | ||
if (node.where) { | ||
this.append(' '); | ||
this.visitNode(node.where); | ||
} | ||
if (node.returning) { | ||
this.append(' '); | ||
this.visitNode(node.returning); | ||
} | ||
this.queryNodeStack.pop(); | ||
} | ||
visitReturning(node) { | ||
this.append('returning '); | ||
this.compileList(node.selections); | ||
} | ||
visitAlias(node) { | ||
this.visitNode(node.node); | ||
this.append(' as '); | ||
this.visitNode(node.alias); | ||
} | ||
visitReference(node) { | ||
this.visitNode(node.table); | ||
this.append('.'); | ||
this.visitNode(node.column); | ||
} | ||
visitSelectAll(_) { | ||
this.append('*'); | ||
} | ||
visitIdentifier(node) { | ||
this.appendLeftIdentifierWrapper(); | ||
this.compileUnwrappedIdentifier(node); | ||
this.appendRightIdentifierWrapper(); | ||
} | ||
compileUnwrappedIdentifier(node) { | ||
this.append(node.identifier); | ||
} | ||
visitFilter(node) { | ||
if (node.left) { | ||
this.visitNode(node.left); | ||
this.append(' '); | ||
} | ||
this.visitNode(node.op); | ||
this.append(' '); | ||
this.visitNode(node.right); | ||
} | ||
visitAnd(node) { | ||
this.visitNode(node.left); | ||
this.append(' and '); | ||
this.visitNode(node.right); | ||
} | ||
visitOr(node) { | ||
this.visitNode(node.left); | ||
this.append(' or '); | ||
this.visitNode(node.right); | ||
} | ||
visitValue(node) { | ||
this.appendValue(node.value); | ||
} | ||
visitValueList(node) { | ||
this.append('('); | ||
this.compileList(node.values); | ||
this.append(')'); | ||
} | ||
visitPrimitiveValueList(node) { | ||
this.append('('); | ||
const { values } = node; | ||
for (let i = 0; i < values.length; ++i) { | ||
this.appendValue(values[i]); | ||
if (i !== values.length - 1) { | ||
this.append(', '); | ||
} | ||
} | ||
this.append(')'); | ||
} | ||
visitParens(node) { | ||
this.append('('); | ||
this.visitNode(node.node); | ||
this.append(')'); | ||
} | ||
visitJoin(node) { | ||
this.append(JOIN_TYPE_SQL[node.joinType]); | ||
this.append(' '); | ||
this.visitNode(node.table); | ||
this.append(' on '); | ||
if (node.on) { | ||
this.visitNode(node.on); | ||
} | ||
} | ||
visitRaw(node) { | ||
node.sqlFragments.forEach((sql, i) => { | ||
this.append(sql); | ||
if (node.params.length > i) { | ||
this.visitNode(node.params[i]); | ||
} | ||
}); | ||
} | ||
visitOperator(node) { | ||
this.append(node.operator); | ||
} | ||
visitTable(node) { | ||
if (node.schema) { | ||
this.visitNode(node.schema); | ||
this.append('.'); | ||
} | ||
this.visitNode(node.table); | ||
} | ||
visitCreateTable(node) { | ||
this.append('create table '); | ||
this.visitNode(node.table); | ||
this.append(' ('); | ||
this.compileList(node.columns); | ||
this.append(')'); | ||
} | ||
visitColumnDefinition(node) { | ||
this.visitNode(node.column); | ||
this.append(' '); | ||
if (node.isAutoIncrementing) { | ||
this.append('serial'); | ||
} | ||
else { | ||
this.visitNode(node.dataType); | ||
} | ||
if (!node.isNullable) { | ||
this.append(' not null'); | ||
} | ||
if (node.isUnique) { | ||
this.append(' unique'); | ||
} | ||
if (node.isPrimaryKey) { | ||
this.append(' primary key'); | ||
} | ||
if (node.references) { | ||
this.append(' references '); | ||
this.visitNode(node.references.table); | ||
this.append('('); | ||
this.visitNode(node.references.column); | ||
this.append(')'); | ||
if (node.onDelete) { | ||
this.append(' on delete '); | ||
this.append(node.onDelete); | ||
} | ||
} | ||
} | ||
visitDropTable(node) { | ||
this.append('drop table '); | ||
if (node.modifier === 'IfExists') { | ||
this.append('if exists '); | ||
} | ||
this.visitNode(node.table); | ||
} | ||
visitDataType(node) { | ||
this.append(DATA_TYPE_SQL[node.dataType](node)); | ||
} | ||
visitOrderBy(node) { | ||
this.append('order by '); | ||
this.compileList(node.items); | ||
} | ||
visitOrderByItem(node) { | ||
this.visitNode(node.orderBy); | ||
this.append(' '); | ||
this.append(node.direction); | ||
} | ||
visitGroupBy(node) { | ||
this.append('group by '); | ||
this.compileList(node.items); | ||
} | ||
visitGroupByItem(node) { | ||
this.visitNode(node.groupBy); | ||
} | ||
visitUpdateQuery(node) { | ||
this.queryNodeStack.push(node); | ||
this.append('update '); | ||
this.visitNode(node.table); | ||
this.append(' set '); | ||
if (node.updates) { | ||
this.compileList(node.updates); | ||
} | ||
if (node.joins) { | ||
this.append(' '); | ||
node.joins.forEach(this.visitNode); | ||
} | ||
if (node.where) { | ||
this.append(' '); | ||
this.visitNode(node.where); | ||
} | ||
if (node.returning) { | ||
this.append(' '); | ||
this.visitNode(node.returning); | ||
} | ||
this.queryNodeStack.pop(); | ||
} | ||
visitColumnUpdate(node) { | ||
this.visitNode(node.column); | ||
this.append(' = '); | ||
this.visitNode(node.value); | ||
} | ||
appendLeftIdentifierWrapper() { | ||
this.append('"'); | ||
} | ||
appendRightIdentifierWrapper() { | ||
this.append('"'); | ||
} | ||
append(str) { | ||
this.#sqlFragments.push(str); | ||
} | ||
appendValue(value) { | ||
this.#bindings.push(value); | ||
this.append(`$${this.#bindings.length}`); | ||
} | ||
} | ||
exports.QueryCompiler = QueryCompiler; | ||
const SELECT_MODIFIER_SQL = { | ||
Distinct: 'distinct', | ||
ForKeyShare: 'for key share', | ||
ForNoKeyUpdate: 'for no key update', | ||
ForUpdate: 'for update', | ||
ForShare: 'for share', | ||
NoWait: 'no wait', | ||
SkipLocked: 'skip locked', | ||
}; | ||
const JOIN_TYPE_SQL = { | ||
InnerJoin: 'inner join', | ||
LeftJoin: 'left join', | ||
RightJoin: 'right join', | ||
FullJoin: 'full join', | ||
}; | ||
const DATA_TYPE_SQL = { | ||
BigInteger: () => 'bigint', | ||
Binary: () => '???', | ||
Boolean: () => 'boolean', | ||
Double: () => 'double precision', | ||
Float: () => 'real', | ||
Integer: () => 'integer', | ||
String: (node) => `varchar(${node.size ?? 255})`, | ||
Text: () => 'text', | ||
}; |
@@ -10,2 +10,4 @@ "use strict"; | ||
class RawBuilder { | ||
#sql; | ||
#params; | ||
constructor(sql, params) { | ||
@@ -15,4 +17,2 @@ this.#sql = sql; | ||
} | ||
#sql; | ||
#params; | ||
toOperationNode() { | ||
@@ -49,6 +49,2 @@ const bindingRegex = /(\?\??)/g; | ||
class AliasedRawBuilder { | ||
constructor(rawBuilder, alias) { | ||
this.#rawBuilder = rawBuilder; | ||
this.#alias = alias; | ||
} | ||
#rawBuilder; | ||
@@ -70,2 +66,6 @@ #alias; | ||
} | ||
constructor(rawBuilder, alias) { | ||
this.#rawBuilder = rawBuilder; | ||
this.#alias = alias; | ||
} | ||
} | ||
@@ -72,0 +72,0 @@ exports.AliasedRawBuilder = AliasedRawBuilder; |
@@ -8,6 +8,6 @@ "use strict"; | ||
class ColumnBuilder { | ||
#node; | ||
constructor(node) { | ||
this.#node = node; | ||
} | ||
#node; | ||
/** | ||
@@ -14,0 +14,0 @@ * Makes the column automatically incrementing. |
@@ -10,2 +10,5 @@ "use strict"; | ||
class CreateTableBuilder { | ||
#createTableNode; | ||
#compiler; | ||
#connectionProvider; | ||
constructor({ createTableNode, compiler, connectionProvider, }) { | ||
@@ -16,5 +19,2 @@ this.#createTableNode = createTableNode; | ||
} | ||
#createTableNode; | ||
#compiler; | ||
#connectionProvider; | ||
string(...args) { | ||
@@ -21,0 +21,0 @@ return this.addColumn(args[0], data_type_node_1.createDataTypeNode('String', object_utils_1.isNumber(args[1]) ? args[1] : undefined), object_utils_1.isFunction(args[1]) ? args[1] : args[2]); |
@@ -5,2 +5,5 @@ "use strict"; | ||
class DropTableBuilder { | ||
#dropTableNode; | ||
#compiler; | ||
#connectionProvider; | ||
constructor({ dropTableNode, compiler, connectionProvider, }) { | ||
@@ -11,5 +14,2 @@ this.#dropTableNode = dropTableNode; | ||
} | ||
#dropTableNode; | ||
#compiler; | ||
#connectionProvider; | ||
toOperationNode() { | ||
@@ -16,0 +16,0 @@ return this.#dropTableNode; |
{ | ||
"name": "kysely", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Type safe SQL query builder", | ||
@@ -12,3 +12,4 @@ "main": "lib/index.js", | ||
"build": "rm -rf lib && tsc", | ||
"build:docs": "typedoc src/index.ts", | ||
"docs:build": "typedoc src/index.ts && npm run docs:override-theme", | ||
"docs:override-theme": "cat docs-theme.css >> docs/assets/css/main.css", | ||
"prepublish": "npm run build" | ||
@@ -30,6 +31,6 @@ }, | ||
"ts-node": "^9.1.1", | ||
"tsd": "^0.14.0", | ||
"typedoc": "^0.20.28", | ||
"typescript": "4.1.x" | ||
"tsd": "^0.16.0", | ||
"typedoc": "^0.20.36", | ||
"typescript": "4.2.x" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
245248
151
6332