@mikro-orm/mongodb 6.5.8-dev.9 → 6.5.9-dev.0
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/MongoSchemaGenerator.d.ts +2 -0
- package/MongoSchemaGenerator.js +30 -6
- package/index.mjs +1 -0
- package/package.json +3 -3
|
@@ -17,7 +17,9 @@ export declare class MongoSchemaGenerator extends AbstractSchemaGenerator<MongoD
|
|
|
17
17
|
collectionsWithFailedIndexes?: string[];
|
|
18
18
|
}): Promise<void>;
|
|
19
19
|
ensureIndexes(options?: EnsureIndexesOptions): Promise<void>;
|
|
20
|
+
private mapIndexProperties;
|
|
20
21
|
private createIndexes;
|
|
22
|
+
private executeQuery;
|
|
21
23
|
private createUniqueIndexes;
|
|
22
24
|
private createPropertyIndexes;
|
|
23
25
|
}
|
package/MongoSchemaGenerator.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MongoSchemaGenerator = void 0;
|
|
4
|
+
const node_util_1 = require("node:util");
|
|
4
5
|
const core_1 = require("@mikro-orm/core");
|
|
5
6
|
class MongoSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
6
7
|
static register(orm) {
|
|
@@ -63,7 +64,7 @@ class MongoSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
63
64
|
const isIdIndex = index.key._id === 1 && core_1.Utils.getObjectKeysSize(index.key) === 1;
|
|
64
65
|
/* istanbul ignore next */
|
|
65
66
|
if (!isIdIndex && !options?.skipIndexes?.find(idx => idx.collection === collection.name && idx.indexName === index.name)) {
|
|
66
|
-
promises.push(db.collection(collection.name)
|
|
67
|
+
promises.push(this.executeQuery(db.collection(collection.name), 'dropIndex', index.name));
|
|
67
68
|
}
|
|
68
69
|
}
|
|
69
70
|
}
|
|
@@ -115,11 +116,21 @@ class MongoSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
115
116
|
});
|
|
116
117
|
}
|
|
117
118
|
}
|
|
119
|
+
mapIndexProperties(index, meta) {
|
|
120
|
+
return core_1.Utils.flatten(core_1.Utils.asArray(index.properties).map(propName => {
|
|
121
|
+
const rootPropName = propName.substring(0, propName.indexOf('.'));
|
|
122
|
+
const prop = meta.properties[rootPropName];
|
|
123
|
+
if (propName.includes('.')) {
|
|
124
|
+
return [prop.fieldNames[0] + propName.substring(propName.indexOf('.'))];
|
|
125
|
+
}
|
|
126
|
+
return prop?.fieldNames ?? propName;
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
118
129
|
createIndexes(meta) {
|
|
119
130
|
const res = [];
|
|
120
131
|
meta.indexes.forEach(index => {
|
|
121
132
|
let fieldOrSpec;
|
|
122
|
-
const properties =
|
|
133
|
+
const properties = this.mapIndexProperties(index, meta);
|
|
123
134
|
const collection = this.connection.getCollection(meta.className);
|
|
124
135
|
if (Array.isArray(index.options) && index.options.length === 2 && properties.length === 0) {
|
|
125
136
|
res.push([collection.collectionName, collection.createIndex(index.options[0], index.options[1])]);
|
|
@@ -140,7 +151,7 @@ class MongoSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
140
151
|
else {
|
|
141
152
|
fieldOrSpec = properties.reduce((o, i) => { o[i] = 1; return o; }, {});
|
|
142
153
|
}
|
|
143
|
-
res.push([collection.collectionName, collection
|
|
154
|
+
res.push([collection.collectionName, this.executeQuery(collection, 'createIndex', fieldOrSpec, {
|
|
144
155
|
name: index.name,
|
|
145
156
|
unique: false,
|
|
146
157
|
...index.options,
|
|
@@ -148,13 +159,26 @@ class MongoSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
148
159
|
});
|
|
149
160
|
return res;
|
|
150
161
|
}
|
|
162
|
+
async executeQuery(collection, method, ...args) {
|
|
163
|
+
const now = Date.now();
|
|
164
|
+
return collection[method](...args).then((res) => {
|
|
165
|
+
core_1.Utils.dropUndefinedProperties(args);
|
|
166
|
+
const query = `db.getCollection('${collection.collectionName}').${method}(${args.map(arg => (0, node_util_1.inspect)(arg)).join(', ')});`;
|
|
167
|
+
this.config.getLogger().logQuery({
|
|
168
|
+
level: 'info',
|
|
169
|
+
query,
|
|
170
|
+
took: Date.now() - now,
|
|
171
|
+
});
|
|
172
|
+
return res;
|
|
173
|
+
});
|
|
174
|
+
}
|
|
151
175
|
createUniqueIndexes(meta) {
|
|
152
176
|
const res = [];
|
|
153
177
|
meta.uniques.forEach(index => {
|
|
154
|
-
const properties =
|
|
178
|
+
const properties = this.mapIndexProperties(index, meta);
|
|
155
179
|
const fieldOrSpec = properties.reduce((o, i) => { o[i] = 1; return o; }, {});
|
|
156
180
|
const collection = this.connection.getCollection(meta.className);
|
|
157
|
-
res.push([collection.collectionName, collection
|
|
181
|
+
res.push([collection.collectionName, this.executeQuery(collection, 'createIndex', fieldOrSpec, {
|
|
158
182
|
name: index.name,
|
|
159
183
|
unique: true,
|
|
160
184
|
...index.options,
|
|
@@ -170,7 +194,7 @@ class MongoSchemaGenerator extends core_1.AbstractSchemaGenerator {
|
|
|
170
194
|
const fieldOrSpec = prop.embeddedPath
|
|
171
195
|
? prop.embeddedPath.join('.')
|
|
172
196
|
: prop.fieldNames.reduce((o, i) => { o[i] = 1; return o; }, {});
|
|
173
|
-
return [[collection.collectionName, collection
|
|
197
|
+
return [[collection.collectionName, this.executeQuery(collection, 'createIndex', fieldOrSpec, {
|
|
174
198
|
name: (core_1.Utils.isString(prop[type]) ? prop[type] : undefined),
|
|
175
199
|
unique: type === 'unique',
|
|
176
200
|
sparse: prop.nullable === true,
|
package/index.mjs
CHANGED
|
@@ -211,6 +211,7 @@ export const expandDotPaths = mod.expandDotPaths;
|
|
|
211
211
|
export const getLoadingStrategy = mod.getLoadingStrategy;
|
|
212
212
|
export const getOnConflictFields = mod.getOnConflictFields;
|
|
213
213
|
export const getOnConflictReturningFields = mod.getOnConflictReturningFields;
|
|
214
|
+
export const getWhereCondition = mod.getWhereCondition;
|
|
214
215
|
export const helper = mod.helper;
|
|
215
216
|
export const parseJsonSafe = mod.parseJsonSafe;
|
|
216
217
|
export const quote = mod.quote;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/mongodb",
|
|
3
|
-
"version": "6.5.
|
|
3
|
+
"version": "6.5.9-dev.0",
|
|
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",
|
|
@@ -62,9 +62,9 @@
|
|
|
62
62
|
"mongodb": "6.20.0"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
|
-
"@mikro-orm/core": "^6.5.
|
|
65
|
+
"@mikro-orm/core": "^6.5.8"
|
|
66
66
|
},
|
|
67
67
|
"peerDependencies": {
|
|
68
|
-
"@mikro-orm/core": "6.5.
|
|
68
|
+
"@mikro-orm/core": "6.5.9-dev.0"
|
|
69
69
|
}
|
|
70
70
|
}
|