@mikro-orm/mongodb 6.5.7-dev.14 → 6.5.7-dev.16

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.
@@ -294,12 +294,19 @@ class MongoConnection extends core_1.Connection {
294
294
  createUpdatePayload(row, upsertOptions) {
295
295
  const doc = { $set: row };
296
296
  const $unset = {};
297
- core_1.Utils.keys(row)
298
- .filter(k => typeof row[k] === 'undefined')
299
- .forEach(k => {
300
- $unset[k] = '';
301
- delete row[k];
302
- });
297
+ const $inc = {};
298
+ for (const k of core_1.Utils.keys(row)) {
299
+ const item = row[k];
300
+ if (typeof item === 'undefined') {
301
+ $unset[k] = '';
302
+ delete row[k];
303
+ continue;
304
+ }
305
+ if (core_1.Utils.isPlainObject(item) && '$inc' in item) {
306
+ $inc[k] = item.$inc;
307
+ delete row[k];
308
+ }
309
+ }
303
310
  if (upsertOptions) {
304
311
  if (upsertOptions.onConflictAction === 'ignore') {
305
312
  doc.$setOnInsert = doc.$set;
@@ -325,9 +332,12 @@ class MongoConnection extends core_1.Connection {
325
332
  }
326
333
  if (core_1.Utils.hasObjectKeys($unset)) {
327
334
  doc.$unset = $unset;
328
- if (!core_1.Utils.hasObjectKeys(doc.$set)) {
329
- delete doc.$set;
330
- }
335
+ }
336
+ if (core_1.Utils.hasObjectKeys($inc)) {
337
+ doc.$inc = $inc;
338
+ }
339
+ if (!core_1.Utils.hasObjectKeys(doc.$set)) {
340
+ delete doc.$set;
331
341
  }
332
342
  return doc;
333
343
  }
package/MongoDriver.d.ts CHANGED
@@ -26,4 +26,5 @@ export declare class MongoDriver extends DatabaseDriver<MongoConnection> {
26
26
  private convertObjectIds;
27
27
  private buildFilterById;
28
28
  protected buildFields<T extends object, P extends string = never>(entityName: string, populate: PopulateOptions<T>[], fields?: readonly EntityField<T, P>[], exclude?: string[]): string[] | undefined;
29
+ private handleVersionProperty;
29
30
  }
package/MongoDriver.js CHANGED
@@ -82,11 +82,15 @@ class MongoDriver extends core_1.DatabaseDriver {
82
82
  return this.rethrow(this.getConnection('read').countDocuments(entityName, where, ctx));
83
83
  }
84
84
  async nativeInsert(entityName, data, options = {}) {
85
+ this.handleVersionProperty(entityName, data);
85
86
  data = this.renameFields(entityName, data);
86
87
  return this.rethrow(this.getConnection('write').insertOne(entityName, data, options.ctx));
87
88
  }
88
89
  async nativeInsertMany(entityName, data, options = {}) {
89
- data = data.map(d => this.renameFields(entityName, d));
90
+ data = data.map(item => {
91
+ this.handleVersionProperty(entityName, item);
92
+ return this.renameFields(entityName, item);
93
+ });
90
94
  const meta = this.metadata.find(entityName);
91
95
  /* istanbul ignore next */
92
96
  const pk = meta?.getPrimaryProps()[0].fieldNames[0] ?? '_id';
@@ -98,8 +102,9 @@ class MongoDriver extends core_1.DatabaseDriver {
98
102
  if (core_1.Utils.isPrimaryKey(where)) {
99
103
  where = this.buildFilterById(entityName, where);
100
104
  }
101
- where = this.renameFields(entityName, where, true);
105
+ this.handleVersionProperty(entityName, data, true);
102
106
  data = this.renameFields(entityName, data);
107
+ where = this.renameFields(entityName, where, true);
103
108
  options = { ...options };
104
109
  const meta = this.metadata.find(entityName);
105
110
  /* istanbul ignore next */
@@ -122,7 +127,10 @@ class MongoDriver extends core_1.DatabaseDriver {
122
127
  }
123
128
  return row;
124
129
  });
125
- data = data.map(row => this.renameFields(entityName, row));
130
+ data = data.map(row => {
131
+ this.handleVersionProperty(entityName, row, true);
132
+ return this.renameFields(entityName, row);
133
+ });
126
134
  options = { ...options };
127
135
  const meta = this.metadata.find(entityName);
128
136
  /* istanbul ignore next */
@@ -309,5 +317,18 @@ class MongoDriver extends core_1.DatabaseDriver {
309
317
  }
310
318
  return ret.length > 0 ? ret : undefined;
311
319
  }
320
+ handleVersionProperty(entityName, data, update = false) {
321
+ const meta = this.metadata.find(entityName);
322
+ if (!meta?.versionProperty) {
323
+ return;
324
+ }
325
+ const versionProperty = meta.properties[meta.versionProperty];
326
+ if (versionProperty.runtimeType === 'Date') {
327
+ data[versionProperty.name] ??= new Date();
328
+ }
329
+ else {
330
+ data[versionProperty.name] ??= update ? { $inc: 1 } : 1;
331
+ }
332
+ }
312
333
  }
313
334
  exports.MongoDriver = MongoDriver;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mongodb",
3
- "version": "6.5.7-dev.14",
3
+ "version": "6.5.7-dev.16",
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
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -65,6 +65,6 @@
65
65
  "@mikro-orm/core": "^6.5.6"
66
66
  },
67
67
  "peerDependencies": {
68
- "@mikro-orm/core": "6.5.7-dev.14"
68
+ "@mikro-orm/core": "6.5.7-dev.16"
69
69
  }
70
70
  }