@mikro-orm/sql 7.1.0-dev.8 → 7.1.0-dev.9

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.
@@ -1,4 +1,4 @@
1
- import { type EntitySchemaWithMeta, EntityManager, type AnyEntity, type ConnectionType, type EntityData, type EntityName, type EntityRepository, type GetRepository, type QueryResult, type FilterQuery, type LoggingOptions, type RawQueryFragment } from '@mikro-orm/core';
1
+ import { type EntitySchemaWithMeta, EntityManager, type AnyEntity, type ConnectionType, type CountByOptions, type Dictionary, type EntityData, type EntityKey, type EntityName, type EntityRepository, type FilterQuery, type GetRepository, type LoggingOptions, type QueryResult, type RawQueryFragment } from '@mikro-orm/core';
2
2
  import type { AbstractSqlDriver } from './AbstractSqlDriver.js';
3
3
  import type { NativeQueryBuilder } from './query/NativeQueryBuilder.js';
4
4
  import type { QueryBuilder } from './query/QueryBuilder.js';
@@ -29,6 +29,10 @@ export declare class SqlEntityManager<Driver extends AbstractSqlDriver = Abstrac
29
29
  getKysely<TDB = undefined, TOptions extends GetKyselyOptions = GetKyselyOptions>(options?: TOptions): Kysely<TDB extends undefined ? InferKyselyDB<EntitiesFromManager<this>, TOptions> & InferClassEntityDB<AllEntitiesFromManager<this>, TOptions> : TDB>;
30
30
  /** Executes a raw SQL query, using the current transaction context if available. */
31
31
  execute<T extends QueryResult | EntityData<AnyEntity> | EntityData<AnyEntity>[] = EntityData<AnyEntity>[]>(query: string | NativeQueryBuilder | RawQueryFragment, params?: any[], method?: 'all' | 'get' | 'run', loggerContext?: LoggingOptions): Promise<T>;
32
+ /**
33
+ * @inheritDoc
34
+ */
35
+ countBy<Entity extends object>(entityName: EntityName<Entity>, groupBy: EntityKey<Entity> | readonly EntityKey<Entity>[], options?: CountByOptions<Entity>): Promise<Dictionary<number>>;
32
36
  getRepository<T extends object, U extends EntityRepository<T> = SqlEntityRepository<T>>(entityName: EntityName<T>): GetRepository<T, U>;
33
37
  protected applyDiscriminatorCondition<Entity extends object>(entityName: EntityName<Entity>, where: FilterQuery<Entity>): FilterQuery<Entity>;
34
38
  }
@@ -1,4 +1,4 @@
1
- import { EntityManager, } from '@mikro-orm/core';
1
+ import { EntityManager, raw, Utils, } from '@mikro-orm/core';
2
2
  import { MikroKyselyPlugin } from './plugin/index.js';
3
3
  /**
4
4
  * @inheritDoc
@@ -35,6 +35,41 @@ export class SqlEntityManager extends EntityManager {
35
35
  async execute(query, params = [], method = 'all', loggerContext) {
36
36
  return this.getDriver().execute(query, params, method, this.getContext(false).getTransactionContext(), loggerContext);
37
37
  }
38
+ /**
39
+ * @inheritDoc
40
+ */
41
+ async countBy(entityName, groupBy, options = {}) {
42
+ const em = this.getContext(false);
43
+ options = { ...options };
44
+ em.prepareOptions(options);
45
+ const meta = em.getMetadata().find(entityName);
46
+ const fields = Utils.asArray(groupBy);
47
+ const { where: rawWhere, ...countOptions } = options;
48
+ await em.tryFlush(entityName, options);
49
+ const where = await em.processWhere(entityName, rawWhere ?? {}, options, 'read');
50
+ const qb = em.createQueryBuilder(meta.class);
51
+ qb
52
+ .select([...fields, raw('count(*) as cnt')])
53
+ .where(where)
54
+ .groupBy(fields);
55
+ if (countOptions.having) {
56
+ qb.having(countOptions.having);
57
+ }
58
+ if (countOptions.schema) {
59
+ qb.withSchema(countOptions.schema);
60
+ }
61
+ const rows = await qb.execute('all', { mapResults: false });
62
+ const results = {};
63
+ for (const row of rows) {
64
+ const keyParts = fields.map(f => {
65
+ const col = meta.properties[f]?.fieldNames?.[0] ?? f;
66
+ return String(row[col]);
67
+ });
68
+ const key = keyParts.join(Utils.PK_SEPARATOR);
69
+ results[key] = +row.cnt;
70
+ }
71
+ return results;
72
+ }
38
73
  getRepository(entityName) {
39
74
  return super.getRepository(entityName);
40
75
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.1.0-dev.8",
3
+ "version": "7.1.0-dev.9",
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
5
  "keywords": [
6
6
  "data-mapper",
@@ -53,7 +53,7 @@
53
53
  "@mikro-orm/core": "^7.0.11"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.1.0-dev.8"
56
+ "@mikro-orm/core": "7.1.0-dev.9"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"