@mikro-orm/knex 6.0.0-dev.4 → 6.0.0-dev.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/{dist/AbstractSqlConnection.d.ts → AbstractSqlConnection.d.ts} +0 -0
  2. package/{dist/AbstractSqlConnection.js → AbstractSqlConnection.js} +5 -0
  3. package/AbstractSqlDriver.d.ts +66 -0
  4. package/{dist/AbstractSqlDriver.js → AbstractSqlDriver.js} +218 -70
  5. package/{dist/AbstractSqlPlatform.d.ts → AbstractSqlPlatform.d.ts} +6 -6
  6. package/{dist/AbstractSqlPlatform.js → AbstractSqlPlatform.js} +12 -14
  7. package/{dist/MonkeyPatchable.d.ts → MonkeyPatchable.d.ts} +0 -0
  8. package/{dist/MonkeyPatchable.js → MonkeyPatchable.js} +0 -0
  9. package/{dist/README.md → README.md} +2 -2
  10. package/{dist/SqlEntityManager.d.ts → SqlEntityManager.d.ts} +5 -5
  11. package/{dist/SqlEntityManager.js → SqlEntityManager.js} +0 -1
  12. package/{dist/SqlEntityRepository.d.ts → SqlEntityRepository.d.ts} +3 -3
  13. package/{dist/SqlEntityRepository.js → SqlEntityRepository.js} +1 -1
  14. package/{dist/index.d.ts → index.d.ts} +0 -0
  15. package/{dist/index.js → index.js} +5 -1
  16. package/{dist/index.mjs → index.mjs} +0 -0
  17. package/package.json +18 -47
  18. package/{dist/query → query}/ArrayCriteriaNode.d.ts +0 -0
  19. package/{dist/query → query}/ArrayCriteriaNode.js +0 -0
  20. package/{dist/query → query}/CriteriaNode.d.ts +0 -0
  21. package/{dist/query → query}/CriteriaNode.js +7 -4
  22. package/{dist/query → query}/CriteriaNodeFactory.d.ts +0 -0
  23. package/{dist/query → query}/CriteriaNodeFactory.js +0 -0
  24. package/{dist/query → query}/ObjectCriteriaNode.d.ts +0 -0
  25. package/{dist/query → query}/ObjectCriteriaNode.js +14 -1
  26. package/{dist/query → query}/QueryBuilder.d.ts +58 -24
  27. package/{dist/query → query}/QueryBuilder.js +285 -84
  28. package/{dist/query → query}/QueryBuilderHelper.d.ts +19 -11
  29. package/{dist/query → query}/QueryBuilderHelper.js +206 -108
  30. package/{dist/query → query}/ScalarCriteriaNode.d.ts +0 -0
  31. package/{dist/query → query}/ScalarCriteriaNode.js +0 -0
  32. package/{dist/query → query}/enums.d.ts +0 -0
  33. package/{dist/query → query}/enums.js +0 -0
  34. package/{dist/query → query}/index.d.ts +0 -0
  35. package/{dist/query → query}/index.js +5 -1
  36. package/{dist/schema → schema}/DatabaseSchema.d.ts +1 -1
  37. package/{dist/schema → schema}/DatabaseSchema.js +8 -16
  38. package/{dist/schema → schema}/DatabaseTable.d.ts +1 -1
  39. package/{dist/schema → schema}/DatabaseTable.js +2 -1
  40. package/{dist/schema → schema}/SchemaComparator.d.ts +3 -1
  41. package/{dist/schema → schema}/SchemaComparator.js +58 -10
  42. package/{dist/schema → schema}/SchemaGenerator.d.ts +11 -9
  43. package/{dist/schema → schema}/SchemaGenerator.js +81 -28
  44. package/{dist/schema → schema}/SchemaHelper.d.ts +9 -4
  45. package/{dist/schema → schema}/SchemaHelper.js +46 -8
  46. package/{dist/schema → schema}/index.d.ts +0 -0
  47. package/{dist/schema → schema}/index.js +5 -1
  48. package/{dist/typings.d.ts → typings.d.ts} +7 -3
  49. package/{dist/typings.js → typings.js} +0 -0
  50. package/CHANGELOG.md +0 -661
  51. package/dist/AbstractSqlDriver.d.ts +0 -55
  52. package/dist/LICENSE +0 -21
  53. package/dist/package.json +0 -99
@@ -18,20 +18,18 @@ class AbstractSqlPlatform extends core_1.Platform {
18
18
  getSchemaHelper() {
19
19
  return this.schemaHelper;
20
20
  }
21
- getSchemaGenerator(driver) {
22
- return new schema_1.SchemaGenerator(driver); // cast as `any` to get around circular dependencies
23
- }
24
- getEntityGenerator(em) {
25
- // eslint-disable-next-line @typescript-eslint/no-var-requires
26
- const { EntityGenerator } = require('@mikro-orm/entity-generator');
27
- return new EntityGenerator(em);
21
+ /** @inheritDoc */
22
+ lookupExtensions(orm) {
23
+ schema_1.SchemaGenerator.register(orm);
28
24
  }
29
- getMigrator(em) {
30
- // eslint-disable-next-line @typescript-eslint/no-var-requires
31
- const { Migrator } = require('@mikro-orm/migrations');
32
- return new Migrator(em);
25
+ /* istanbul ignore next: kept for type inference only */
26
+ getSchemaGenerator(driver, em) {
27
+ return new schema_1.SchemaGenerator(em ?? driver);
33
28
  }
34
29
  quoteValue(value) {
30
+ if (this.isRaw(value)) {
31
+ return value;
32
+ }
35
33
  /* istanbul ignore if */
36
34
  if (core_1.Utils.isPlainObject(value) || value?.[core_1.JsonProperty]) {
37
35
  return (0, sqlstring_1.escape)(JSON.stringify(value));
@@ -68,11 +66,11 @@ class AbstractSqlPlatform extends core_1.Platform {
68
66
  }
69
67
  return ret;
70
68
  }
71
- getSearchJsonPropertySQL(path, type) {
72
- return this.getSearchJsonPropertyKey(path.split('->'), type);
69
+ getSearchJsonPropertySQL(path, type, aliased) {
70
+ return this.getSearchJsonPropertyKey(path.split('->'), type, aliased);
73
71
  }
74
72
  isRaw(value) {
75
- return super.isRaw(value) || (typeof value === 'object' && value !== null && value.client && value.ref && value.constructor.name === 'Ref');
73
+ return super.isRaw(value) || (typeof value === 'object' && value !== null && value.client && ['Ref', 'Raw'].includes(value.constructor.name));
76
74
  }
77
75
  supportsSchemas() {
78
76
  return false;
File without changes
@@ -8,8 +8,8 @@ MariaDB, PostgreSQL and SQLite databases.
8
8
 
9
9
  > Heavily inspired by [Doctrine](https://www.doctrine-project.org/) and [Nextras Orm](https://nextras.org/orm/).
10
10
 
11
- [![NPM version](https://img.shields.io/npm/v/mikro-orm.svg)](https://www.npmjs.com/package/@mikro-orm/core)
12
- [![NPM dev version](https://img.shields.io/npm/v/mikro-orm/next.svg)](https://www.npmjs.com/package/@mikro-orm/core)
11
+ [![NPM version](https://img.shields.io/npm/v/@mikro-orm/core.svg)](https://www.npmjs.com/package/@mikro-orm/core)
12
+ [![NPM dev version](https://img.shields.io/npm/v/@mikro-orm/core/next.svg)](https://www.npmjs.com/package/@mikro-orm/core)
13
13
  [![Chat on slack](https://img.shields.io/badge/chat-on%20slack-blue.svg)](https://join.slack.com/t/mikroorm/shared_invite/enQtNTM1ODYzMzM4MDk3LWM4ZDExMjU5ZDhmNjA2MmM3MWMwZmExNjhhNDdiYTMwNWM0MGY5ZTE3ZjkyZTMzOWExNDgyYmMzNDE1NDI5NjA)
14
14
  [![Downloads](https://img.shields.io/npm/dm/@mikro-orm/core.svg)](https://www.npmjs.com/package/@mikro-orm/core)
15
15
  [![Coverage Status](https://img.shields.io/coveralls/mikro-orm/mikro-orm.svg)](https://coveralls.io/r/mikro-orm/mikro-orm?branch=master)
@@ -1,5 +1,5 @@
1
1
  import type { Knex } from 'knex';
2
- import type { AnyEntity, EntityData, EntityName, EntityRepository, GetRepository, QueryResult } from '@mikro-orm/core';
2
+ import type { AnyEntity, ConnectionType, EntityData, EntityName, EntityRepository, GetRepository, QueryResult } from '@mikro-orm/core';
3
3
  import { EntityManager } from '@mikro-orm/core';
4
4
  import type { AbstractSqlDriver } from './AbstractSqlDriver';
5
5
  import { QueryBuilder } from './query';
@@ -11,11 +11,11 @@ export declare class SqlEntityManager<D extends AbstractSqlDriver = AbstractSqlD
11
11
  /**
12
12
  * Creates a QueryBuilder instance
13
13
  */
14
- createQueryBuilder<T>(entityName: EntityName<T>, alias?: string, type?: 'read' | 'write'): QueryBuilder<T>;
14
+ createQueryBuilder<T extends object>(entityName: EntityName<T> | QueryBuilder<T>, alias?: string, type?: ConnectionType): QueryBuilder<T>;
15
15
  /**
16
16
  * Shortcut for `createQueryBuilder()`
17
17
  */
18
- qb<T>(entityName: EntityName<T>, alias?: string, type?: 'read' | 'write'): QueryBuilder<T>;
18
+ qb<T extends object>(entityName: EntityName<T>, alias?: string, type?: ConnectionType): QueryBuilder<T>;
19
19
  /**
20
20
  * Creates raw SQL query that won't be escaped when used as a parameter.
21
21
  */
@@ -23,7 +23,7 @@ export declare class SqlEntityManager<D extends AbstractSqlDriver = AbstractSqlD
23
23
  /**
24
24
  * Returns configured knex instance.
25
25
  */
26
- getKnex(type?: 'read' | 'write'): Knex<any, Record<string, any>[]>;
26
+ getKnex(type?: ConnectionType): Knex<any, any[]>;
27
27
  execute<T extends QueryResult | EntityData<AnyEntity> | EntityData<AnyEntity>[] = EntityData<AnyEntity>[]>(queryOrKnex: string | Knex.QueryBuilder | Knex.Raw, params?: any[], method?: 'all' | 'get' | 'run'): Promise<T>;
28
- getRepository<T extends AnyEntity<T>, U extends EntityRepository<T> = SqlEntityRepository<T>>(entityName: EntityName<T>): GetRepository<T, U>;
28
+ getRepository<T extends object, U extends EntityRepository<T> = SqlEntityRepository<T>>(entityName: EntityName<T>): GetRepository<T, U>;
29
29
  }
@@ -11,7 +11,6 @@ class SqlEntityManager extends core_1.EntityManager {
11
11
  * Creates a QueryBuilder instance
12
12
  */
13
13
  createQueryBuilder(entityName, alias, type) {
14
- entityName = core_1.Utils.className(entityName);
15
14
  const context = this.getContext();
16
15
  return new query_1.QueryBuilder(entityName, this.getMetadata(), this.getDriver(), context.getTransactionContext(), alias, type, context);
17
16
  }
@@ -1,9 +1,9 @@
1
1
  import type { Knex } from 'knex';
2
- import type { EntityName } from '@mikro-orm/core';
2
+ import type { ConnectionType, EntityName } from '@mikro-orm/core';
3
3
  import { EntityRepository } from '@mikro-orm/core';
4
4
  import type { SqlEntityManager } from './SqlEntityManager';
5
5
  import type { QueryBuilder } from './query';
6
- export declare class SqlEntityRepository<T> extends EntityRepository<T> {
6
+ export declare class SqlEntityRepository<T extends object> extends EntityRepository<T> {
7
7
  protected readonly _em: SqlEntityManager;
8
8
  protected readonly entityName: EntityName<T>;
9
9
  constructor(_em: SqlEntityManager, entityName: EntityName<T>);
@@ -18,6 +18,6 @@ export declare class SqlEntityRepository<T> extends EntityRepository<T> {
18
18
  /**
19
19
  * Returns configured knex instance.
20
20
  */
21
- getKnex(type?: 'read' | 'write'): Knex;
21
+ getKnex(type?: ConnectionType): Knex;
22
22
  protected get em(): SqlEntityManager;
23
23
  }
@@ -27,7 +27,7 @@ class SqlEntityRepository extends core_1.EntityRepository {
27
27
  return this.em.getConnection(type).getKnex();
28
28
  }
29
29
  get em() {
30
- return this._em.getContext();
30
+ return this._em.getContext(false);
31
31
  }
32
32
  }
33
33
  exports.SqlEntityRepository = SqlEntityRepository;
File without changes
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
File without changes
package/package.json CHANGED
@@ -1,15 +1,18 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.0.0-dev.4+5d8bf0c0c2",
3
+ "version": "6.0.0-dev.6",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "typings": "dist/index.d.ts",
5
+ "main": "index.js",
6
+ "module": "index.mjs",
7
+ "typings": "index.d.ts",
8
8
  "exports": {
9
9
  "./package.json": "./package.json",
10
10
  ".": {
11
- "import": "./dist/index.mjs",
12
- "require": "./dist/index.js"
11
+ "import": {
12
+ "types": "./index.d.ts",
13
+ "default": "./index.mjs"
14
+ },
15
+ "require": "./index.js"
13
16
  }
14
17
  },
15
18
  "repository": {
@@ -43,58 +46,26 @@
43
46
  },
44
47
  "homepage": "https://mikro-orm.io",
45
48
  "engines": {
46
- "node": ">= 14.0.0"
49
+ "node": ">= 16.0.0"
47
50
  },
48
51
  "scripts": {
49
- "build": "yarn clean && yarn compile && yarn copy",
50
- "postbuild": "yarn gen-esm-wrapper dist/index.js dist/index.mjs",
52
+ "build": "yarn clean && yarn compile && yarn copy && yarn run -T gen-esm-wrapper index.js index.mjs",
51
53
  "clean": "rimraf ./dist",
52
54
  "compile": "tsc -p tsconfig.build.json",
53
- "copy": "ts-node -T ../../scripts/copy.ts"
55
+ "copy": "node ../../scripts/copy.mjs"
54
56
  },
55
57
  "publishConfig": {
56
58
  "access": "public"
57
59
  },
58
60
  "dependencies": {
59
- "fs-extra": "10.0.0",
60
- "knex": "1.0.1",
61
- "sqlstring": "2.3.2"
61
+ "fs-extra": "11.1.0",
62
+ "knex": "2.3.0",
63
+ "sqlstring": "2.3.3"
62
64
  },
63
65
  "devDependencies": {
64
- "@mikro-orm/core": "^6.0.0-dev.4+5d8bf0c0c2"
66
+ "@mikro-orm/core": "^5.5.3"
65
67
  },
66
68
  "peerDependencies": {
67
- "@mikro-orm/core": "^5.0.0",
68
- "@mikro-orm/entity-generator": "^5.0.0",
69
- "@mikro-orm/migrations": "^5.0.0",
70
- "@vscode/sqlite3": "^5.0.0",
71
- "mssql": "^7.0.0",
72
- "mysql": "^2.18.1",
73
- "mysql2": "^2.1.0",
74
- "pg": "^8.0.3"
75
- },
76
- "peerDependenciesMeta": {
77
- "@mikro-orm/entity-generator": {
78
- "optional": true
79
- },
80
- "@mikro-orm/migrations": {
81
- "optional": true
82
- },
83
- "@vscode/sqlite3": {
84
- "optional": true
85
- },
86
- "mssql": {
87
- "optional": true
88
- },
89
- "mysql": {
90
- "optional": true
91
- },
92
- "mysql2": {
93
- "optional": true
94
- },
95
- "pg": {
96
- "optional": true
97
- }
98
- },
99
- "gitHead": "5d8bf0c0c2254b0bcc0160827108ee17a8d63442"
69
+ "@mikro-orm/core": "~6.0.0-dev.6"
70
+ }
100
71
  }
File without changes
File without changes
File without changes
@@ -21,9 +21,10 @@ class CriteriaNode {
21
21
  return;
22
22
  }
23
23
  pks.forEach(k => {
24
- this.prop = meta.props.find(prop => prop.name === k || (prop.fieldNames || []).includes(k));
24
+ this.prop = meta.props.find(prop => prop.name === k || (prop.fieldNames?.length === 1 && prop.fieldNames[0] === k));
25
+ const isProp = this.prop || meta.props.find(prop => (prop.fieldNames || []).includes(k));
25
26
  // do not validate if the key is prefixed or type casted (e.g. `k::text`)
26
- if (validate && !this.prop && !k.includes('.') && !k.includes('::') && !core_1.Utils.isOperator(k) && !CriteriaNode.isCustomExpression(k)) {
27
+ if (validate && !isProp && !k.includes('.') && !k.includes('::') && !core_1.Utils.isOperator(k) && !CriteriaNode.isCustomExpression(k)) {
27
28
  throw new Error(`Trying to query by not existing property ${entityName}.${k}`);
28
29
  }
29
30
  });
@@ -56,11 +57,13 @@ class CriteriaNode {
56
57
  }
57
58
  }
58
59
  renameFieldToPK(qb) {
59
- const alias = qb.getAliasForJoinPath(this.getPath()) ?? qb.alias;
60
+ const joinAlias = qb.getAliasForJoinPath(this.getPath());
61
+ const alias = joinAlias ?? qb.alias;
60
62
  if (this.prop.reference === core_1.ReferenceType.MANY_TO_MANY) {
61
63
  return core_1.Utils.getPrimaryKeyHash(this.prop.inverseJoinColumns.map(col => `${alias}.${col}`));
62
64
  }
63
- if (this.prop.joinColumns.length > 1) {
65
+ // if we found a matching join, we need to use the target table column names, as we use that alias instead of the root
66
+ if (!joinAlias && this.prop.owner && this.prop.joinColumns.length > 1) {
64
67
  return core_1.Utils.getPrimaryKeyHash(this.prop.joinColumns.map(col => `${alias}.${col}`));
65
68
  }
66
69
  return core_1.Utils.getPrimaryKeyHash(this.prop.referencedColumnNames.map(col => `${alias}.${col}`));
File without changes
File without changes
File without changes
@@ -22,7 +22,8 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
22
22
  const payload = childNode.process(qb, this.prop ? alias : ownerAlias);
23
23
  const operator = core_1.Utils.isOperator(field);
24
24
  const customExpression = ObjectCriteriaNode.isCustomExpression(field);
25
- const virtual = childNode.prop?.persist === false;
25
+ // we need to keep the prefixing for formulas otherwise we would lose aliasing context when nesting inside group operators
26
+ const virtual = childNode.prop?.persist === false && !childNode.prop?.formula;
26
27
  // if key is missing, we are inside group operator and we need to prefix with alias
27
28
  const primaryKey = this.key && this.metadata.find(this.entityName).primaryKeys.includes(field);
28
29
  if (childNode.shouldInline(payload)) {
@@ -78,10 +79,22 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
78
79
  delete o[key];
79
80
  o.$and = $and;
80
81
  }
82
+ else if (core_1.Utils.isOperator(k) && Array.isArray(payload[k])) {
83
+ o[key] = payload[k].map((child) => Object.keys(child).reduce((o, childKey) => {
84
+ const key = (this.isPrefixed(childKey) || core_1.Utils.isOperator(childKey))
85
+ ? childKey
86
+ : `${childAlias}.${childKey}`;
87
+ o[key] = child[childKey];
88
+ return o;
89
+ }, {}));
90
+ }
81
91
  else {
82
92
  o[key] = payload[k];
83
93
  }
84
94
  }
95
+ else if (ObjectCriteriaNode.isCustomExpression(k)) {
96
+ o[k] = payload[k];
97
+ }
85
98
  else {
86
99
  o[`${childAlias}.${k}`] = payload[k];
87
100
  }
@@ -1,8 +1,10 @@
1
1
  import type { Knex } from 'knex';
2
- import type { AnyEntity, Dictionary, EntityData, EntityProperty, RequiredEntityData, GroupOperator, MetadataStorage, PopulateOptions, QBFilterQuery, QueryOrderMap, QueryResult, FlushMode } from '@mikro-orm/core';
3
- import { LockMode, QueryFlag } from '@mikro-orm/core';
2
+ import type { AnyEntity, ConnectionType, Dictionary, EntityData, EntityName, EntityProperty, FlushMode, GroupOperator, MetadataStorage, ObjectQuery, PopulateOptions, QBFilterQuery, QBQueryOrderMap, QueryResult, RequiredEntityData } from '@mikro-orm/core';
3
+ import { LockMode, PopulateHint, QueryFlag } from '@mikro-orm/core';
4
4
  import { QueryType } from './enums';
5
5
  import type { AbstractSqlDriver } from '../AbstractSqlDriver';
6
+ import { QueryBuilderHelper } from './QueryBuilderHelper';
7
+ import type { Alias } from './QueryBuilderHelper';
6
8
  import type { SqlEntityManager } from '../SqlEntityManager';
7
9
  import type { Field } from '../typings';
8
10
  /**
@@ -24,27 +26,30 @@ import type { Field } from '../typings';
24
26
  * const publisher = await qb.getSingleResult();
25
27
  * ```
26
28
  */
27
- export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
28
- private readonly entityName;
29
+ export declare class QueryBuilder<T extends object = AnyEntity> {
29
30
  private readonly metadata;
30
31
  private readonly driver;
31
32
  private readonly context?;
32
33
  private connectionType?;
33
34
  private readonly em?;
34
- readonly alias: string;
35
+ get mainAlias(): Alias;
36
+ get alias(): string;
37
+ get helper(): QueryBuilderHelper;
35
38
  /** @internal */
36
- type: QueryType;
39
+ type?: QueryType;
37
40
  /** @internal */
38
41
  _fields?: Field<T>[];
39
42
  /** @internal */
40
43
  _populate: PopulateOptions<T>[];
41
44
  /** @internal */
45
+ _populateWhere?: ObjectQuery<T> | PopulateHint;
46
+ /** @internal */
42
47
  _populateMap: Dictionary<string>;
43
48
  private aliasCounter;
44
49
  private flags;
45
50
  private finalized;
46
51
  private _joins;
47
- private _aliasMap;
52
+ private _explicitAlias;
48
53
  private _schema?;
49
54
  private _cond;
50
55
  private _data;
@@ -54,6 +59,7 @@ export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
54
59
  private _onConflict?;
55
60
  private _limit?;
56
61
  private _offset?;
62
+ private _distinctOn?;
57
63
  private _joinedProps;
58
64
  private _cache?;
59
65
  private _indexHint?;
@@ -61,16 +67,20 @@ export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
61
67
  private lockMode?;
62
68
  private lockTables?;
63
69
  private subQueries;
64
- private innerPromise?;
70
+ private _mainAlias?;
71
+ private _aliases;
72
+ private _helper?;
65
73
  private readonly platform;
66
74
  private readonly knex;
67
- private readonly helper;
68
75
  /**
69
76
  * @internal
70
77
  */
71
- constructor(entityName: string, metadata: MetadataStorage, driver: AbstractSqlDriver, context?: Knex.Transaction<any, any[]> | undefined, alias?: string, connectionType?: "write" | "read" | undefined, em?: SqlEntityManager<AbstractSqlDriver<import("..").AbstractSqlConnection>> | undefined);
78
+ constructor(entityName: EntityName<T> | QueryBuilder<T>, metadata: MetadataStorage, driver: AbstractSqlDriver, context?: Knex.Transaction<any, any[]> | undefined, alias?: string, connectionType?: ConnectionType | undefined, em?: SqlEntityManager<AbstractSqlDriver<import("../AbstractSqlConnection").AbstractSqlConnection>> | undefined);
72
79
  select(fields: Field<T> | Field<T>[], distinct?: boolean): SelectQueryBuilder<T>;
73
80
  addSelect(fields: Field<T> | Field<T>[]): SelectQueryBuilder<T>;
81
+ distinct(): SelectQueryBuilder<T>;
82
+ /** postgres only */
83
+ distinctOn(fields: string | string[]): SelectQueryBuilder<T>;
74
84
  insert(data: RequiredEntityData<T> | RequiredEntityData<T>[]): InsertQueryBuilder<T>;
75
85
  update(data: EntityData<T>): UpdateQueryBuilder<T>;
76
86
  delete(cond?: QBFilterQuery): DeleteQueryBuilder<T>;
@@ -78,9 +88,9 @@ export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
78
88
  count(field?: string | string[], distinct?: boolean): CountQueryBuilder<T>;
79
89
  join(field: string, alias: string, cond?: QBFilterQuery, type?: 'leftJoin' | 'innerJoin' | 'pivotJoin', path?: string): this;
80
90
  leftJoin(field: string, alias: string, cond?: QBFilterQuery): this;
81
- joinAndSelect(field: string, alias: string, cond?: QBFilterQuery, type?: 'leftJoin' | 'innerJoin' | 'pivotJoin', path?: string): this;
82
- leftJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery): this;
83
- protected getFieldsForJoinedLoad<U extends AnyEntity<U>>(prop: EntityProperty<U>, alias: string): Field<U>[];
91
+ joinAndSelect(field: string, alias: string, cond?: QBFilterQuery, type?: 'leftJoin' | 'innerJoin' | 'pivotJoin', path?: string): SelectQueryBuilder<T>;
92
+ leftJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery): SelectQueryBuilder<T>;
93
+ protected getFieldsForJoinedLoad<U extends object>(prop: EntityProperty<U>, alias: string): Field<U>[];
84
94
  withSubQuery(subQuery: Knex.QueryBuilder, alias: string): this;
85
95
  where(cond: QBFilterQuery<T>, operator?: keyof typeof GroupOperator): this;
86
96
  where(cond: string, params?: any[], operator?: keyof typeof GroupOperator): this;
@@ -88,16 +98,16 @@ export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
88
98
  andWhere(cond: string, params?: any[]): this;
89
99
  orWhere(cond: QBFilterQuery<T>): this;
90
100
  orWhere(cond: string, params?: any[]): this;
91
- orderBy(orderBy: QueryOrderMap<unknown> | QueryOrderMap<unknown>[]): this;
101
+ orderBy(orderBy: QBQueryOrderMap<T> | QBQueryOrderMap<T>[]): this;
92
102
  groupBy(fields: (string | keyof T) | readonly (string | keyof T)[]): this;
93
103
  having(cond?: QBFilterQuery | string, params?: any[]): this;
94
- onConflict(fields?: string | string[]): this;
104
+ onConflict(fields?: Field<T> | Field<T>[]): this;
95
105
  ignore(): this;
96
106
  merge(data?: EntityData<T> | Field<T>[]): this;
97
107
  /**
98
108
  * @internal
99
109
  */
100
- populate(populate: PopulateOptions<T>[]): this;
110
+ populate(populate: PopulateOptions<T>[], populateWhere?: ObjectQuery<T> | PopulateHint): this;
101
111
  /**
102
112
  * @internal
103
113
  */
@@ -117,6 +127,12 @@ export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
117
127
  * Adds index hint to the FROM clause.
118
128
  */
119
129
  indexHint(sql: string): this;
130
+ /**
131
+ * Specifies FROM which entity's table select/update/delete will be executed, removing all previously set FROM-s.
132
+ * Allows setting a main string alias of the selection data.
133
+ */
134
+ from<T extends AnyEntity<T> = AnyEntity>(target: QueryBuilder<T>, aliasName?: string): QueryBuilder<T>;
135
+ from<T extends AnyEntity<T> = AnyEntity>(target: EntityName<T>): QueryBuilder<T>;
120
136
  getKnexQuery(): Knex.QueryBuilder;
121
137
  /**
122
138
  * Returns the query with parameters as wildcards.
@@ -130,8 +146,18 @@ export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
130
146
  * Returns raw interpolated query string with all the parameters inlined.
131
147
  */
132
148
  getFormattedQuery(): string;
149
+ /**
150
+ * @internal
151
+ */
133
152
  getAliasForJoinPath(path?: string): string | undefined;
153
+ /**
154
+ * @internal
155
+ */
134
156
  getNextAlias(entityName?: string): string;
157
+ /**
158
+ * @internal
159
+ */
160
+ getAliasMap(): Dictionary<string>;
135
161
  /**
136
162
  * Executes this QB and returns the raw results, mapped to the property names (unless disabled via last parameter).
137
163
  * Use `method` to specify what kind of result you want to get (array/single/meta).
@@ -157,7 +183,6 @@ export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
157
183
  * Provides promise-like interface so we can await the QB instance.
158
184
  */
159
185
  then<TResult1 = any, TResult2 = never>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<T[] | number | QueryResult<T>>;
160
- private getInnerPromise;
161
186
  /**
162
187
  * Returns knex instance with sub-query aliased with given alias.
163
188
  * You can provide `EntityName.propName` as alias, then the field name will be used based on the metadata
@@ -170,23 +195,32 @@ export declare class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
170
195
  private init;
171
196
  private getQueryBase;
172
197
  private finalize;
198
+ private hasToManyJoins;
173
199
  private wrapPaginateSubQuery;
174
200
  private wrapModifySubQuery;
175
201
  private autoJoinPivotTable;
202
+ private getSchema;
203
+ private createAlias;
204
+ private createMainAlias;
205
+ private fromSubQuery;
206
+ private fromEntityName;
207
+ private createQueryBuilderHelper;
208
+ private ensureFromClause;
209
+ private ensureNotFinalized;
176
210
  }
177
- export interface RunQueryBuilder<T> extends Omit<QueryBuilder<T>, 'getResult' | 'getSingleResult' | 'getResultList' | 'where'> {
211
+ export interface RunQueryBuilder<T extends object> extends Omit<QueryBuilder<T>, 'getResult' | 'getSingleResult' | 'getResultList' | 'where'> {
178
212
  where(cond: QBFilterQuery<T> | string, params?: keyof typeof GroupOperator | any[], operator?: keyof typeof GroupOperator): this;
179
213
  execute<U = QueryResult<T>>(method?: 'all' | 'get' | 'run', mapResults?: boolean): Promise<U>;
180
214
  then<TResult1 = QueryResult<T>, TResult2 = never>(onfulfilled?: ((value: QueryResult<T>) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<QueryResult<T>>;
181
215
  }
182
- export interface SelectQueryBuilder<T> extends QueryBuilder<T> {
216
+ export interface SelectQueryBuilder<T extends object> extends QueryBuilder<T> {
183
217
  execute<U = T[]>(method?: 'all' | 'get' | 'run', mapResults?: boolean): Promise<U>;
184
218
  execute<U = T[]>(method: 'all', mapResults?: boolean): Promise<U>;
185
219
  execute<U = T>(method: 'get', mapResults?: boolean): Promise<U>;
186
220
  execute<U = QueryResult<T>>(method: 'run', mapResults?: boolean): Promise<U>;
187
221
  then<TResult1 = T[], TResult2 = never>(onfulfilled?: ((value: T[]) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<T[]>;
188
222
  }
189
- export interface CountQueryBuilder<T> extends QueryBuilder<T> {
223
+ export interface CountQueryBuilder<T extends object> extends QueryBuilder<T> {
190
224
  execute<U = {
191
225
  count: number;
192
226
  }[]>(method?: 'all' | 'get' | 'run', mapResults?: boolean): Promise<U>;
@@ -201,11 +235,11 @@ export interface CountQueryBuilder<T> extends QueryBuilder<T> {
201
235
  }>>(method: 'run', mapResults?: boolean): Promise<U>;
202
236
  then<TResult1 = number, TResult2 = never>(onfulfilled?: ((value: number) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<number>;
203
237
  }
204
- export interface InsertQueryBuilder<T> extends RunQueryBuilder<T> {
238
+ export interface InsertQueryBuilder<T extends object> extends RunQueryBuilder<T> {
205
239
  }
206
- export interface UpdateQueryBuilder<T> extends RunQueryBuilder<T> {
240
+ export interface UpdateQueryBuilder<T extends object> extends RunQueryBuilder<T> {
207
241
  }
208
- export interface DeleteQueryBuilder<T> extends RunQueryBuilder<T> {
242
+ export interface DeleteQueryBuilder<T extends object> extends RunQueryBuilder<T> {
209
243
  }
210
- export interface TruncateQueryBuilder<T> extends RunQueryBuilder<T> {
244
+ export interface TruncateQueryBuilder<T extends object> extends RunQueryBuilder<T> {
211
245
  }