@mikro-orm/mongodb 7.1.0-dev.13 → 7.1.0-dev.15

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.
@@ -21,6 +21,11 @@ export declare class MongoSchemaGenerator extends AbstractSchemaGenerator<MongoD
21
21
  ensureIndexes(options?: EnsureIndexesOptions): Promise<void>;
22
22
  private mapIndexProperties;
23
23
  private createIndexes;
24
+ /**
25
+ * An explicit `options.partialFilterExpression` wins over `where` — this preserves the
26
+ * long-standing `options: { partialFilterExpression }` escape hatch.
27
+ */
28
+ private applyPartialFilter;
24
29
  private executeQuery;
25
30
  private createUniqueIndexes;
26
31
  private createPropertyIndexes;
@@ -132,10 +132,21 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
132
132
  const properties = this.mapIndexProperties(index, meta);
133
133
  const collection = this.connection.getCollection(meta.class);
134
134
  if (Array.isArray(index.options) && index.options.length === 2 && properties.length === 0) {
135
- res.push([collection.collectionName, collection.createIndex(index.options[0], index.options[1])]);
135
+ // The array-form escape hatch passes raw [spec, options] to the driver; fold `where`
136
+ // into the options dict (on a clone, to avoid mutating the user's metadata).
137
+ const opts = { ...index.options[1] };
138
+ this.applyPartialFilter(opts, index.where, index.name, meta.className);
139
+ res.push([collection.collectionName, collection.createIndex(index.options[0], opts)]);
136
140
  return;
137
141
  }
138
142
  if (index.options && properties.length === 0) {
143
+ // The plain escape hatch forwards `index.options` as the sole argument; there is
144
+ // no options slot for `partialFilterExpression`, so warn instead of silently dropping.
145
+ if (index.where != null) {
146
+ this.config
147
+ .getLogger()
148
+ .warn('schema', `Index '${index.name ?? '(unnamed)'}' on entity '${meta.className}': \`where\` was ignored because \`options\` is used as the raw index spec and leaves no slot for \`partialFilterExpression\` — pass \`options: [spec, { partialFilterExpression }]\` (array form) to combine both.`);
149
+ }
139
150
  res.push([collection.collectionName, collection.createIndex(index.options)]);
140
151
  return;
141
152
  }
@@ -162,10 +173,30 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
162
173
  if (index.invisible) {
163
174
  indexOptions.hidden = true;
164
175
  }
176
+ this.applyPartialFilter(indexOptions, index.where, index.name, meta.className);
165
177
  res.push([collection.collectionName, this.executeQuery(collection, 'createIndex', fieldOrSpec, indexOptions)]);
166
178
  });
167
179
  return res;
168
180
  }
181
+ /**
182
+ * An explicit `options.partialFilterExpression` wins over `where` — this preserves the
183
+ * long-standing `options: { partialFilterExpression }` escape hatch.
184
+ */
185
+ applyPartialFilter(options, where, indexName, entityName) {
186
+ if (where == null) {
187
+ return;
188
+ }
189
+ if (options.partialFilterExpression != null) {
190
+ this.config
191
+ .getLogger()
192
+ .warn('schema', `Index '${indexName ?? '(unnamed)'}' on entity '${entityName}': both \`where\` and \`options.partialFilterExpression\` are set; \`options.partialFilterExpression\` wins.`);
193
+ return;
194
+ }
195
+ if (typeof where === 'string') {
196
+ throw new Error(`Index '${indexName ?? '(unnamed)'}' on entity '${entityName}': string \`where\` is not supported on MongoDB; pass an object/FilterQuery (it maps to MongoDB's \`partialFilterExpression\`).`);
197
+ }
198
+ options.partialFilterExpression = where;
199
+ }
169
200
  async executeQuery(collection, method, ...args) {
170
201
  const now = Date.now();
171
202
  return collection[method](...args).then((res) => {
@@ -188,14 +219,13 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
188
219
  return o;
189
220
  }, {});
190
221
  const collection = this.connection.getCollection(meta.class);
191
- res.push([
192
- collection.collectionName,
193
- this.executeQuery(collection, 'createIndex', fieldOrSpec, {
194
- name: index.name,
195
- unique: true,
196
- ...index.options,
197
- }),
198
- ]);
222
+ const indexOptions = {
223
+ name: index.name,
224
+ unique: true,
225
+ ...index.options,
226
+ };
227
+ this.applyPartialFilter(indexOptions, index.where, index.name, meta.className);
228
+ res.push([collection.collectionName, this.executeQuery(collection, 'createIndex', fieldOrSpec, indexOptions)]);
199
229
  });
200
230
  return res;
201
231
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mongodb",
3
- "version": "7.1.0-dev.13",
3
+ "version": "7.1.0-dev.15",
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.13"
56
+ "@mikro-orm/core": "7.1.0-dev.15"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"