@mikro-orm/mongodb 7.2.8-dev.9 → 7.2.8

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 (2) hide show
  1. package/MongoDriver.js +18 -2
  2. package/package.json +3 -3
package/MongoDriver.js CHANGED
@@ -95,13 +95,19 @@ export class MongoDriver extends DatabaseDriver {
95
95
  }
96
96
  async findWithPartitionLimit(entityName, where, orderBy, fields, partitionLimit, options) {
97
97
  const meta = this.metadata.find(entityName);
98
- const { limit, offset = 0 } = partitionLimit;
98
+ const { limit, offset = 0, owners } = partitionLimit;
99
99
  // Resolve the partition property to its DB field name; callers always pass
100
100
  // a declared property on the target meta (FK of the owning side).
101
101
  const prop = meta.properties[partitionLimit.partitionBy];
102
102
  const partitionField = prop.fieldNames[0];
103
103
  const pipeline = [];
104
104
  pipeline.push({ $match: where });
105
+ // an M:N owner array holds several partitions, so rank each row once per requested owner, like a pivot row in SQL
106
+ if (owners) {
107
+ pipeline.push({ $addFields: { __partition: `$${partitionField}` } });
108
+ pipeline.push({ $unwind: '$__partition' });
109
+ pipeline.push({ $match: { __partition: { $in: owners } } });
110
+ }
105
111
  if (orderBy.length > 0) {
106
112
  const sortSpec = {};
107
113
  for (const order of orderBy) {
@@ -115,7 +121,7 @@ export class MongoDriver extends DatabaseDriver {
115
121
  // $sort before $group ensures $push collects in correct order
116
122
  pipeline.push({
117
123
  $group: {
118
- _id: `$${partitionField}`,
124
+ _id: owners ? '$__partition' : `$${partitionField}`,
119
125
  __docs: { $push: '$$ROOT' },
120
126
  },
121
127
  });
@@ -131,9 +137,19 @@ export class MongoDriver extends DatabaseDriver {
131
137
  for (const field of fields) {
132
138
  projection[field] = 1;
133
139
  }
140
+ if (owners) {
141
+ projection.__partition = 1;
142
+ }
134
143
  pipeline.push({ $project: projection });
135
144
  }
136
145
  const res = await this.rethrow(this.getConnection('read').aggregate(entityName, pipeline, options.ctx, options.logging, options.signal));
146
+ if (owners) {
147
+ partitionLimit.partitions = res.map((row) => {
148
+ const owner = row.__partition;
149
+ delete row.__partition;
150
+ return [owner, row._id];
151
+ });
152
+ }
137
153
  return res.map(r => this.mapResult(r, meta));
138
154
  }
139
155
  async findOne(entityName, where, options = { populate: [], orderBy: {} }) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mongodb",
3
- "version": "7.2.8-dev.9",
3
+ "version": "7.2.8",
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",
@@ -50,10 +50,10 @@
50
50
  "mongodb": "7.8.0"
51
51
  },
52
52
  "devDependencies": {
53
- "@mikro-orm/core": "^7.2.7"
53
+ "@mikro-orm/core": "^7.2.8"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.2.8-dev.9"
56
+ "@mikro-orm/core": "7.2.8"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"