Skip to content

Commit

Permalink
fix: remove dynamic require calls (#10196)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dukezo authored Dec 29, 2023
1 parent f6b87e3 commit a939654
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
3 changes: 3 additions & 0 deletions src/data-source/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ import { RelationIdLoader } from "../query-builder/RelationIdLoader"
import { DriverUtils } from "../driver/DriverUtils"
import { InstanceChecker } from "../util/InstanceChecker"
import { ObjectLiteral } from "../common/ObjectLiteral"
import { registerQueryBuilders } from "../query-builder"

registerQueryBuilders()

/**
* DataSource is a pre-defined connection configuration to a specific database.
Expand Down
6 changes: 4 additions & 2 deletions src/platform/BrowserPlatformTools.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Buffer } from "buffer";

/**
* Browser's implementation of the platform-specific tools.
*
Expand Down Expand Up @@ -164,8 +166,8 @@ interface Window {
}

declare var window: Window;
if (typeof window !== "undefined" && typeof require !== "undefined") {
window.Buffer = require("buffer/").Buffer;
if (typeof window !== "undefined") {
window.Buffer = Buffer;
}
// NativeScript uses global, not window
if (typeof global !== "undefined" && typeof require !== "undefined") {
Expand Down
44 changes: 16 additions & 28 deletions src/query-builder/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export abstract class QueryBuilder<Entity extends ObjectLiteral> {
*/
private parameterIndex = 0

/**
* Contains all registered query builder classes.
*/
private static queryBuilderRegistry: Record<string, any> = {}

// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -179,12 +184,9 @@ export abstract class QueryBuilder<Entity extends ObjectLiteral> {
]
}

// loading it dynamically because of circular issue
const SelectQueryBuilderCls =
require("./SelectQueryBuilder").SelectQueryBuilder
if (InstanceChecker.isSelectQueryBuilder(this)) return this as any

return new SelectQueryBuilderCls(this)
return QueryBuilder.queryBuilderRegistry["SelectQueryBuilder"](this)
}

/**
Expand All @@ -193,12 +195,9 @@ export abstract class QueryBuilder<Entity extends ObjectLiteral> {
insert(): InsertQueryBuilder<Entity> {
this.expressionMap.queryType = "insert"

// loading it dynamically because of circular issue
const InsertQueryBuilderCls =
require("./InsertQueryBuilder").InsertQueryBuilder
if (InstanceChecker.isInsertQueryBuilder(this)) return this as any

return new InsertQueryBuilderCls(this)
return QueryBuilder.queryBuilderRegistry["InsertQueryBuilder"](this)
}

/**
Expand Down Expand Up @@ -256,12 +255,9 @@ export abstract class QueryBuilder<Entity extends ObjectLiteral> {
this.expressionMap.queryType = "update"
this.expressionMap.valuesSet = updateSet

// loading it dynamically because of circular issue
const UpdateQueryBuilderCls =
require("./UpdateQueryBuilder").UpdateQueryBuilder
if (InstanceChecker.isUpdateQueryBuilder(this)) return this as any

return new UpdateQueryBuilderCls(this)
return QueryBuilder.queryBuilderRegistry["UpdateQueryBuilder"](this)
}

/**
Expand All @@ -270,34 +266,25 @@ export abstract class QueryBuilder<Entity extends ObjectLiteral> {
delete(): DeleteQueryBuilder<Entity> {
this.expressionMap.queryType = "delete"

// loading it dynamically because of circular issue
const DeleteQueryBuilderCls =
require("./DeleteQueryBuilder").DeleteQueryBuilder
if (InstanceChecker.isDeleteQueryBuilder(this)) return this as any

return new DeleteQueryBuilderCls(this)
return QueryBuilder.queryBuilderRegistry["DeleteQueryBuilder"](this)
}

softDelete(): SoftDeleteQueryBuilder<any> {
this.expressionMap.queryType = "soft-delete"

// loading it dynamically because of circular issue
const SoftDeleteQueryBuilderCls =
require("./SoftDeleteQueryBuilder").SoftDeleteQueryBuilder
if (InstanceChecker.isSoftDeleteQueryBuilder(this)) return this as any

return new SoftDeleteQueryBuilderCls(this)
return QueryBuilder.queryBuilderRegistry["SoftDeleteQueryBuilder"](this)
}

restore(): SoftDeleteQueryBuilder<any> {
this.expressionMap.queryType = "restore"

// loading it dynamically because of circular issue
const SoftDeleteQueryBuilderCls =
require("./SoftDeleteQueryBuilder").SoftDeleteQueryBuilder
if (InstanceChecker.isSoftDeleteQueryBuilder(this)) return this as any

return new SoftDeleteQueryBuilderCls(this)
return QueryBuilder.queryBuilderRegistry["SoftDeleteQueryBuilder"](this)
}

/**
Expand Down Expand Up @@ -335,12 +322,9 @@ export abstract class QueryBuilder<Entity extends ObjectLiteral> {
this.expressionMap.setMainAlias(mainAlias)
}

// loading it dynamically because of circular issue
const RelationQueryBuilderCls =
require("./RelationQueryBuilder").RelationQueryBuilder
if (InstanceChecker.isRelationQueryBuilder(this)) return this as any

return new RelationQueryBuilderCls(this)
return QueryBuilder.queryBuilderRegistry["RelationQueryBuilder"](this)
}

/**
Expand Down Expand Up @@ -1650,4 +1634,8 @@ export abstract class QueryBuilder<Entity extends ObjectLiteral> {
protected hasCommonTableExpressions(): boolean {
return this.expressionMap.commonTableExpressions.length > 0
}

static registerQueryBuilderClass(name: string, factory: any) {
QueryBuilder.queryBuilderRegistry[name] = factory
}
}
34 changes: 34 additions & 0 deletions src/query-builder/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { DeleteQueryBuilder } from "./DeleteQueryBuilder"
import { InsertQueryBuilder } from "./InsertQueryBuilder"
import { QueryBuilder } from "./QueryBuilder"
import { RelationQueryBuilder } from "./RelationQueryBuilder"
import { SelectQueryBuilder } from "./SelectQueryBuilder"
import { SoftDeleteQueryBuilder } from "./SoftDeleteQueryBuilder"
import { UpdateQueryBuilder } from "./UpdateQueryBuilder"

export function registerQueryBuilders() {
QueryBuilder.registerQueryBuilderClass(
"DeleteQueryBuilder",
(qb: QueryBuilder<any>) => new DeleteQueryBuilder(qb),
)
QueryBuilder.registerQueryBuilderClass(
"InsertQueryBuilder",
(qb: QueryBuilder<any>) => new InsertQueryBuilder(qb),
)
QueryBuilder.registerQueryBuilderClass(
"RelationQueryBuilder",
(qb: QueryBuilder<any>) => new RelationQueryBuilder(qb),
)
QueryBuilder.registerQueryBuilderClass(
"SelectQueryBuilder",
(qb: QueryBuilder<any>) => new SelectQueryBuilder(qb),
)
QueryBuilder.registerQueryBuilderClass(
"SoftDeleteQueryBuilder",
(qb: QueryBuilder<any>) => new SoftDeleteQueryBuilder(qb),
)
QueryBuilder.registerQueryBuilderClass(
"UpdateQueryBuilder",
(qb: QueryBuilder<any>) => new UpdateQueryBuilder(qb),
)
}

0 comments on commit a939654

Please sign in to comment.