@mikro-orm/mongodb 7.0.0-dev.3 → 7.0.0-dev.31
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.
- package/MongoConnection.js +19 -9
- package/MongoDriver.d.ts +1 -0
- package/MongoDriver.js +24 -3
- package/MongoSchemaGenerator.js +5 -4
- package/README.md +1 -2
- package/package.json +4 -4
package/MongoConnection.js
CHANGED
|
@@ -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
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
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
|
-
|
|
327
|
-
|
|
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(
|
|
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
|
-
|
|
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 =>
|
|
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
|
}
|
package/MongoSchemaGenerator.js
CHANGED
|
@@ -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
|
-
|
|
29
|
-
const
|
|
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
|
[](https://discord.gg/w8bjxFHS7X)
|
|
12
12
|
[](https://www.npmjs.com/package/@mikro-orm/core)
|
|
13
13
|
[](https://coveralls.io/r/mikro-orm/mikro-orm?branch=master)
|
|
14
|
-
[](https://codeclimate.com/github/mikro-orm/mikro-orm/maintainability)
|
|
15
14
|
[](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
|
-
- [
|
|
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.
|
|
4
|
+
"version": "7.0.0-dev.31",
|
|
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.
|
|
53
|
+
"mongodb": "6.20.0"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@mikro-orm/core": "^6.
|
|
56
|
+
"@mikro-orm/core": "^6.5.7"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
|
-
"@mikro-orm/core": "7.0.0-dev.
|
|
59
|
+
"@mikro-orm/core": "7.0.0-dev.31"
|
|
60
60
|
}
|
|
61
61
|
}
|