@mikro-orm/mongodb 7.0.0-dev.3 → 7.0.0-dev.30

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.
@@ -292,12 +292,19 @@ export class MongoConnection extends Connection {
292
292
  createUpdatePayload(row, upsertOptions) {
293
293
  const doc = { $set: row };
294
294
  const $unset = {};
295
- Utils.keys(row)
296
- .filter(k => typeof row[k] === 'undefined')
297
- .forEach(k => {
298
- $unset[k] = '';
299
- delete row[k];
300
- });
295
+ const $inc = {};
296
+ for (const k of Utils.keys(row)) {
297
+ const item = row[k];
298
+ if (typeof item === 'undefined') {
299
+ $unset[k] = '';
300
+ delete row[k];
301
+ continue;
302
+ }
303
+ if (Utils.isPlainObject(item) && '$inc' in item) {
304
+ $inc[k] = item.$inc;
305
+ delete row[k];
306
+ }
307
+ }
301
308
  if (upsertOptions) {
302
309
  if (upsertOptions.onConflictAction === 'ignore') {
303
310
  doc.$setOnInsert = doc.$set;
@@ -323,9 +330,12 @@ export class MongoConnection extends Connection {
323
330
  }
324
331
  if (Utils.hasObjectKeys($unset)) {
325
332
  doc.$unset = $unset;
326
- if (!Utils.hasObjectKeys(doc.$set)) {
327
- delete doc.$set;
328
- }
333
+ }
334
+ if (Utils.hasObjectKeys($inc)) {
335
+ doc.$inc = $inc;
336
+ }
337
+ if (!Utils.hasObjectKeys(doc.$set)) {
338
+ delete doc.$set;
329
339
  }
330
340
  return doc;
331
341
  }
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
@@ -79,11 +79,15 @@ export class MongoDriver extends DatabaseDriver {
79
79
  return this.rethrow(this.getConnection('read').countDocuments(entityName, where, ctx));
80
80
  }
81
81
  async nativeInsert(entityName, data, options = {}) {
82
+ this.handleVersionProperty(entityName, data);
82
83
  data = this.renameFields(entityName, data);
83
84
  return this.rethrow(this.getConnection('write').insertOne(entityName, data, options.ctx));
84
85
  }
85
86
  async nativeInsertMany(entityName, data, options = {}) {
86
- data = data.map(d => this.renameFields(entityName, d));
87
+ data = data.map(item => {
88
+ this.handleVersionProperty(entityName, item);
89
+ return this.renameFields(entityName, item);
90
+ });
87
91
  const meta = this.metadata.find(entityName);
88
92
  /* v8 ignore next */
89
93
  const pk = meta?.getPrimaryProps()[0].fieldNames[0] ?? '_id';
@@ -95,8 +99,9 @@ export class MongoDriver extends DatabaseDriver {
95
99
  if (Utils.isPrimaryKey(where)) {
96
100
  where = this.buildFilterById(entityName, where);
97
101
  }
98
- where = this.renameFields(entityName, where, true);
102
+ this.handleVersionProperty(entityName, data, true);
99
103
  data = this.renameFields(entityName, data);
104
+ where = this.renameFields(entityName, where, true);
100
105
  options = { ...options };
101
106
  const meta = this.metadata.find(entityName);
102
107
  /* v8 ignore next */
@@ -119,7 +124,10 @@ export class MongoDriver extends DatabaseDriver {
119
124
  }
120
125
  return row;
121
126
  });
122
- data = data.map(row => this.renameFields(entityName, row));
127
+ data = data.map(row => {
128
+ this.handleVersionProperty(entityName, row, true);
129
+ return this.renameFields(entityName, row);
130
+ });
123
131
  options = { ...options };
124
132
  const meta = this.metadata.find(entityName);
125
133
  /* v8 ignore next */
@@ -307,4 +315,17 @@ export class MongoDriver extends DatabaseDriver {
307
315
  }
308
316
  return ret.length > 0 ? ret : undefined;
309
317
  }
318
+ handleVersionProperty(entityName, data, update = false) {
319
+ const meta = this.metadata.find(entityName);
320
+ if (!meta?.versionProperty) {
321
+ return;
322
+ }
323
+ const versionProperty = meta.properties[meta.versionProperty];
324
+ if (versionProperty.runtimeType === 'Date') {
325
+ data[versionProperty.name] ??= new Date();
326
+ }
327
+ else {
328
+ data[versionProperty.name] ??= update ? { $inc: 1 } : 1;
329
+ }
330
+ }
310
331
  }
@@ -4,10 +4,10 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
4
4
  orm.config.registerExtension('@mikro-orm/schema-generator', () => new MongoSchemaGenerator(orm.em));
5
5
  }
6
6
  async createSchema(options = {}) {
7
+ await this.connection.ensureConnection();
7
8
  options.ensureIndexes ??= true;
8
9
  const existing = await this.connection.listCollections();
9
10
  const metadata = this.getOrderedMetadata();
10
- metadata.push({ collection: this.config.get('migrations').tableName });
11
11
  /* v8 ignore start */
12
12
  const promises = metadata
13
13
  .filter(meta => !existing.includes(meta.collection))
@@ -25,9 +25,8 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
25
25
  await Promise.all(promises);
26
26
  }
27
27
  async dropSchema(options = {}) {
28
- const db = this.connection.getDb();
29
- const collections = await db.listCollections().toArray();
30
- const existing = collections.map(c => c.name);
28
+ await this.connection.ensureConnection();
29
+ const existing = await this.connection.listCollections();
31
30
  const metadata = this.getOrderedMetadata();
32
31
  if (options.dropMigrationsTable) {
33
32
  metadata.push({ collection: this.config.get('migrations').tableName });
@@ -49,6 +48,7 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
49
48
  await this.createSchema(options);
50
49
  }
51
50
  async dropIndexes(options) {
51
+ await this.connection.ensureConnection();
52
52
  const db = this.connection.getDb();
53
53
  const collections = await db.listCollections().toArray();
54
54
  const promises = [];
@@ -68,6 +68,7 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
68
68
  await Promise.all(promises);
69
69
  }
70
70
  async ensureIndexes(options = {}) {
71
+ await this.connection.ensureConnection();
71
72
  options.ensureCollections ??= true;
72
73
  options.retryLimit ??= 3;
73
74
  if (options.ensureCollections) {
package/README.md CHANGED
@@ -11,7 +11,6 @@ TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-or
11
11
  [![Chat on discord](https://img.shields.io/discord/1214904142443839538?label=discord&color=blue)](https://discord.gg/w8bjxFHS7X)
12
12
  [![Downloads](https://img.shields.io/npm/dm/@mikro-orm/core.svg)](https://www.npmjs.com/package/@mikro-orm/core)
13
13
  [![Coverage Status](https://img.shields.io/coveralls/mikro-orm/mikro-orm.svg)](https://coveralls.io/r/mikro-orm/mikro-orm?branch=master)
14
- [![Maintainability](https://api.codeclimate.com/v1/badges/27999651d3adc47cfa40/maintainability)](https://codeclimate.com/github/mikro-orm/mikro-orm/maintainability)
15
14
  [![Build Status](https://github.com/mikro-orm/mikro-orm/workflows/tests/badge.svg?branch=master)](https://github.com/mikro-orm/mikro-orm/actions?workflow=tests)
16
15
 
17
16
  ## 🤔 Unit of What?
@@ -141,7 +140,7 @@ There is also auto-generated [CHANGELOG.md](CHANGELOG.md) file based on commit m
141
140
  - [Composite and Foreign Keys as Primary Key](https://mikro-orm.io/docs/composite-keys)
142
141
  - [Filters](https://mikro-orm.io/docs/filters)
143
142
  - [Using `QueryBuilder`](https://mikro-orm.io/docs/query-builder)
144
- - [Preloading Deeply Nested Structures via populate](https://mikro-orm.io/docs/nested-populate)
143
+ - [Populating relations](https://mikro-orm.io/docs/populating-relations)
145
144
  - [Property Validation](https://mikro-orm.io/docs/property-validation)
146
145
  - [Lifecycle Hooks](https://mikro-orm.io/docs/events#hooks)
147
146
  - [Vanilla JS Support](https://mikro-orm.io/docs/usage-with-js)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/mongodb",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.3",
4
+ "version": "7.0.0-dev.30",
5
5
  "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.",
6
6
  "exports": {
7
7
  "./package.json": "./package.json",
@@ -50,12 +50,12 @@
50
50
  "access": "public"
51
51
  },
52
52
  "dependencies": {
53
- "mongodb": "6.13.0"
53
+ "mongodb": "6.20.0"
54
54
  },
55
55
  "devDependencies": {
56
- "@mikro-orm/core": "^6.4.5"
56
+ "@mikro-orm/core": "^6.5.7"
57
57
  },
58
58
  "peerDependencies": {
59
- "@mikro-orm/core": "7.0.0-dev.3"
59
+ "@mikro-orm/core": "7.0.0-dev.30"
60
60
  }
61
61
  }