@mikro-orm/mongodb 7.0.0-dev.33 → 7.0.0-dev.331

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.
@@ -1,30 +1,30 @@
1
- import { AbstractSchemaGenerator, Utils, } from '@mikro-orm/core';
1
+ import { Utils, inspect, } from '@mikro-orm/core';
2
+ import { AbstractSchemaGenerator } from '@mikro-orm/core/schema';
2
3
  export class MongoSchemaGenerator extends AbstractSchemaGenerator {
3
4
  static register(orm) {
4
5
  orm.config.registerExtension('@mikro-orm/schema-generator', () => new MongoSchemaGenerator(orm.em));
5
6
  }
6
- async createSchema(options = {}) {
7
+ async create(options = {}) {
7
8
  await this.connection.ensureConnection();
8
9
  options.ensureIndexes ??= true;
9
10
  const existing = await this.connection.listCollections();
10
11
  const metadata = this.getOrderedMetadata();
11
- /* v8 ignore start */
12
+ /* v8 ignore next */
12
13
  const promises = metadata
13
14
  .filter(meta => !existing.includes(meta.collection))
14
- .map(meta => this.connection.createCollection(meta.collection).catch(err => {
15
+ .map(meta => this.connection.createCollection(meta.class).catch(err => {
15
16
  const existsErrorMessage = `Collection ${this.config.get('dbName')}.${meta.collection} already exists.`;
16
17
  // ignore errors about the collection already existing
17
18
  if (!(err.name === 'MongoServerError' && err.message.includes(existsErrorMessage))) {
18
19
  throw err;
19
20
  }
20
21
  }));
21
- /* v8 ignore stop */
22
22
  if (options.ensureIndexes) {
23
23
  await this.ensureIndexes({ ensureCollections: false });
24
24
  }
25
25
  await Promise.all(promises);
26
26
  }
27
- async dropSchema(options = {}) {
27
+ async drop(options = {}) {
28
28
  await this.connection.ensureConnection();
29
29
  const existing = await this.connection.listCollections();
30
30
  const metadata = this.getOrderedMetadata();
@@ -33,19 +33,19 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
33
33
  }
34
34
  const promises = metadata
35
35
  .filter(meta => existing.includes(meta.collection))
36
- .map(meta => this.connection.dropCollection(meta.collection));
36
+ .map(meta => this.connection.dropCollection(meta.class));
37
37
  await Promise.all(promises);
38
38
  }
39
- async updateSchema(options = {}) {
40
- await this.createSchema(options);
39
+ async update(options = {}) {
40
+ await this.create(options);
41
41
  }
42
42
  async ensureDatabase() {
43
43
  return false;
44
44
  }
45
- async refreshDatabase(options = {}) {
45
+ async refresh(options = {}) {
46
46
  await this.ensureDatabase();
47
- await this.dropSchema();
48
- await this.createSchema(options);
47
+ await this.drop();
48
+ await this.create(options);
49
49
  }
50
50
  async dropIndexes(options) {
51
51
  await this.connection.ensureConnection();
@@ -59,9 +59,10 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
59
59
  const indexes = await db.collection(collection.name).listIndexes().toArray();
60
60
  for (const index of indexes) {
61
61
  const isIdIndex = index.key._id === 1 && Utils.getObjectKeysSize(index.key) === 1;
62
- /* v8 ignore next 3 */
63
- if (!isIdIndex && !options?.skipIndexes?.find(idx => idx.collection === collection.name && idx.indexName === index.name)) {
64
- promises.push(db.collection(collection.name).dropIndex(index.name));
62
+ /* v8 ignore next */
63
+ if (!isIdIndex &&
64
+ !options?.skipIndexes?.find(idx => idx.collection === collection.name && idx.indexName === index.name)) {
65
+ promises.push(this.executeQuery(db.collection(collection.name), 'dropIndex', index.name));
65
66
  }
66
67
  }
67
68
  }
@@ -72,7 +73,7 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
72
73
  options.ensureCollections ??= true;
73
74
  options.retryLimit ??= 3;
74
75
  if (options.ensureCollections) {
75
- await this.createSchema({ ensureIndexes: false });
76
+ await this.create({ ensureIndexes: false });
76
77
  }
77
78
  const promises = [];
78
79
  for (const meta of this.getOrderedMetadata()) {
@@ -113,12 +114,22 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
113
114
  });
114
115
  }
115
116
  }
117
+ mapIndexProperties(index, meta) {
118
+ return Utils.flatten(Utils.asArray(index.properties).map(propName => {
119
+ const rootPropName = propName.split('.')[0];
120
+ const prop = meta.properties[rootPropName];
121
+ if (propName.includes('.')) {
122
+ return [prop.fieldNames[0] + propName.substring(propName.indexOf('.'))];
123
+ }
124
+ return prop?.fieldNames ?? propName;
125
+ }));
126
+ }
116
127
  createIndexes(meta) {
117
128
  const res = [];
118
129
  meta.indexes.forEach(index => {
119
130
  let fieldOrSpec;
120
- const properties = Utils.flatten(Utils.asArray(index.properties).map(prop => meta.properties[prop].fieldNames));
121
- const collection = this.connection.getCollection(meta.className);
131
+ const properties = this.mapIndexProperties(index, meta);
132
+ const collection = this.connection.getCollection(meta.class);
122
133
  if (Array.isArray(index.options) && index.options.length === 2 && properties.length === 0) {
123
134
  res.push([collection.collectionName, collection.createIndex(index.options[0], index.options[1])]);
124
135
  return;
@@ -132,31 +143,58 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
132
143
  index.type = 'text';
133
144
  }
134
145
  const spec = {};
135
- properties.forEach(prop => spec[prop] = index.type);
146
+ properties.forEach(prop => (spec[prop] = index.type));
136
147
  fieldOrSpec = spec;
137
148
  }
138
149
  else {
139
- fieldOrSpec = properties.reduce((o, i) => { o[i] = 1; return o; }, {});
150
+ fieldOrSpec = properties.reduce((o, i) => {
151
+ o[i] = 1;
152
+ return o;
153
+ }, {});
140
154
  }
141
- res.push([collection.collectionName, collection.createIndex(fieldOrSpec, {
142
- name: index.name,
143
- unique: false,
144
- ...index.options,
145
- })]);
155
+ // MongoDB uses 'hidden' for invisible indexes
156
+ const indexOptions = {
157
+ name: index.name,
158
+ unique: false,
159
+ ...index.options,
160
+ };
161
+ if (index.invisible) {
162
+ indexOptions.hidden = true;
163
+ }
164
+ res.push([collection.collectionName, this.executeQuery(collection, 'createIndex', fieldOrSpec, indexOptions)]);
146
165
  });
147
166
  return res;
148
167
  }
168
+ async executeQuery(collection, method, ...args) {
169
+ const now = Date.now();
170
+ return collection[method](...args).then((res) => {
171
+ Utils.dropUndefinedProperties(args);
172
+ const query = `db.getCollection('${collection.collectionName}').${method}(${args.map(arg => inspect(arg)).join(', ')});`;
173
+ this.config.getLogger().logQuery({
174
+ level: 'info',
175
+ query,
176
+ took: Date.now() - now,
177
+ });
178
+ return res;
179
+ });
180
+ }
149
181
  createUniqueIndexes(meta) {
150
182
  const res = [];
151
183
  meta.uniques.forEach(index => {
152
- const properties = Utils.flatten(Utils.asArray(index.properties).map(prop => meta.properties[prop].fieldNames));
153
- const fieldOrSpec = properties.reduce((o, i) => { o[i] = 1; return o; }, {});
154
- const collection = this.connection.getCollection(meta.className);
155
- res.push([collection.collectionName, collection.createIndex(fieldOrSpec, {
184
+ const properties = this.mapIndexProperties(index, meta);
185
+ const fieldOrSpec = properties.reduce((o, i) => {
186
+ o[i] = 1;
187
+ return o;
188
+ }, {});
189
+ const collection = this.connection.getCollection(meta.class);
190
+ res.push([
191
+ collection.collectionName,
192
+ this.executeQuery(collection, 'createIndex', fieldOrSpec, {
156
193
  name: index.name,
157
194
  unique: true,
158
195
  ...index.options,
159
- })]);
196
+ }),
197
+ ]);
160
198
  });
161
199
  return res;
162
200
  }
@@ -164,14 +202,22 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
164
202
  if (!prop[type] || !meta.collection) {
165
203
  return [];
166
204
  }
167
- const collection = this.connection.getCollection(meta.className);
205
+ const collection = this.connection.getCollection(meta.class);
168
206
  const fieldOrSpec = prop.embeddedPath
169
207
  ? prop.embeddedPath.join('.')
170
- : prop.fieldNames.reduce((o, i) => { o[i] = 1; return o; }, {});
171
- return [[collection.collectionName, collection.createIndex(fieldOrSpec, {
172
- name: (Utils.isString(prop[type]) ? prop[type] : undefined),
208
+ : prop.fieldNames.reduce((o, i) => {
209
+ o[i] = 1;
210
+ return o;
211
+ }, {});
212
+ return [
213
+ [
214
+ collection.collectionName,
215
+ this.executeQuery(collection, 'createIndex', fieldOrSpec, {
216
+ name: typeof prop[type] === 'string' ? prop[type] : undefined,
173
217
  unique: type === 'unique',
174
218
  sparse: prop.nullable === true,
175
- })]];
219
+ }),
220
+ ],
221
+ ];
176
222
  }
177
223
  }
package/README.md CHANGED
@@ -2,14 +2,14 @@
2
2
  <a href="https://mikro-orm.io"><img src="https://raw.githubusercontent.com/mikro-orm/mikro-orm/master/docs/static/img/logo-readme.svg?sanitize=true" alt="MikroORM" /></a>
3
3
  </h1>
4
4
 
5
- TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-orm.io/docs/unit-of-work/) and [Identity Map](https://mikro-orm.io/docs/identity-map/) patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL and SQLite (including libSQL) databases.
5
+ TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-orm.io/docs/unit-of-work/) and [Identity Map](https://mikro-orm.io/docs/identity-map/) patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL, SQLite (including libSQL), MSSQL and Oracle databases.
6
6
 
7
7
  > Heavily inspired by [Doctrine](https://www.doctrine-project.org/) and [Hibernate](https://hibernate.org/).
8
8
 
9
- [![NPM version](https://img.shields.io/npm/v/@mikro-orm/core.svg)](https://www.npmjs.com/package/@mikro-orm/core)
10
- [![NPM dev version](https://img.shields.io/npm/v/@mikro-orm/core/next.svg)](https://www.npmjs.com/package/@mikro-orm/core)
9
+ [![NPM version](https://img.shields.io/npm/v/@mikro-orm/core.svg)](https://npmx.dev/package/@mikro-orm/core)
10
+ [![NPM dev version](https://img.shields.io/npm/v/@mikro-orm/core/next.svg)](https://npmx.dev/package/@mikro-orm/core)
11
11
  [![Chat on discord](https://img.shields.io/discord/1214904142443839538?label=discord&color=blue)](https://discord.gg/w8bjxFHS7X)
12
- [![Downloads](https://img.shields.io/npm/dm/@mikro-orm/core.svg)](https://www.npmjs.com/package/@mikro-orm/core)
12
+ [![Downloads](https://img.shields.io/npm/dm/@mikro-orm/core.svg)](https://npmx.dev/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
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)
15
15
 
@@ -181,6 +181,7 @@ yarn add @mikro-orm/core @mikro-orm/mysql # for mysql/mariadb
181
181
  yarn add @mikro-orm/core @mikro-orm/mariadb # for mysql/mariadb
182
182
  yarn add @mikro-orm/core @mikro-orm/postgresql # for postgresql
183
183
  yarn add @mikro-orm/core @mikro-orm/mssql # for mssql
184
+ yarn add @mikro-orm/core @mikro-orm/oracledb # for oracle
184
185
  yarn add @mikro-orm/core @mikro-orm/sqlite # for sqlite
185
186
  yarn add @mikro-orm/core @mikro-orm/libsql # for libsql
186
187
  ```
@@ -381,6 +382,8 @@ See also the list of contributors who [participated](https://github.com/mikro-or
381
382
 
382
383
  Please ⭐️ this repository if this project helped you!
383
384
 
385
+ > If you'd like to support my open-source work, consider sponsoring me directly at [github.com/sponsors/b4nan](https://github.com/sponsors/b4nan).
386
+
384
387
  ## 📝 License
385
388
 
386
389
  Copyright © 2018 [Martin Adámek](https://github.com/b4nan).
package/index.d.ts CHANGED
@@ -8,4 +8,4 @@ export * from './MongoEntityRepository.js';
8
8
  export * from './MongoSchemaGenerator.js';
9
9
  export { MongoEntityManager as EntityManager } from './MongoEntityManager.js';
10
10
  export { MongoEntityRepository as EntityRepository } from './MongoEntityRepository.js';
11
- export { MongoMikroORM as MikroORM, MongoOptions as Options, defineMongoConfig as defineConfig, } from './MongoMikroORM.js';
11
+ export { MongoMikroORM as MikroORM, type MongoOptions as Options, defineMongoConfig as defineConfig, } from './MongoMikroORM.js';
package/package.json CHANGED
@@ -1,61 +1,61 @@
1
1
  {
2
2
  "name": "@mikro-orm/mongodb",
3
- "type": "module",
4
- "version": "7.0.0-dev.33",
3
+ "version": "7.0.0-dev.331",
5
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.",
6
- "exports": {
7
- "./package.json": "./package.json",
8
- ".": "./index.js"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "git+ssh://git@github.com/mikro-orm/mikro-orm.git"
13
- },
14
5
  "keywords": [
15
- "orm",
6
+ "data-mapper",
7
+ "ddd",
8
+ "entity",
9
+ "identity-map",
10
+ "javascript",
11
+ "js",
12
+ "mariadb",
13
+ "mikro-orm",
16
14
  "mongo",
17
15
  "mongodb",
18
16
  "mysql",
19
- "mariadb",
17
+ "orm",
20
18
  "postgresql",
21
19
  "sqlite",
22
20
  "sqlite3",
23
21
  "ts",
24
22
  "typescript",
25
- "js",
26
- "javascript",
27
- "entity",
28
- "ddd",
29
- "mikro-orm",
30
- "unit-of-work",
31
- "data-mapper",
32
- "identity-map"
23
+ "unit-of-work"
33
24
  ],
34
- "author": "Martin Adámek",
35
- "license": "MIT",
25
+ "homepage": "https://mikro-orm.io",
36
26
  "bugs": {
37
27
  "url": "https://github.com/mikro-orm/mikro-orm/issues"
38
28
  },
39
- "homepage": "https://mikro-orm.io",
40
- "engines": {
41
- "node": ">= 22.11.0"
29
+ "license": "MIT",
30
+ "author": "Martin Adámek",
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+ssh://git@github.com/mikro-orm/mikro-orm.git"
34
+ },
35
+ "type": "module",
36
+ "exports": {
37
+ "./package.json": "./package.json",
38
+ ".": "./index.js"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
42
  },
43
43
  "scripts": {
44
- "build": "yarn clean && yarn compile && yarn copy",
44
+ "build": "yarn compile && yarn copy",
45
45
  "clean": "yarn run -T rimraf ./dist",
46
46
  "compile": "yarn run -T tsc -p tsconfig.build.json",
47
47
  "copy": "node ../../scripts/copy.mjs"
48
48
  },
49
- "publishConfig": {
50
- "access": "public"
51
- },
52
49
  "dependencies": {
53
- "mongodb": "6.20.0"
50
+ "mongodb": "7.1.0"
54
51
  },
55
52
  "devDependencies": {
56
- "@mikro-orm/core": "^6.5.7"
53
+ "@mikro-orm/core": "^6.6.9"
57
54
  },
58
55
  "peerDependencies": {
59
- "@mikro-orm/core": "7.0.0-dev.33"
56
+ "@mikro-orm/core": "7.0.0-dev.331"
57
+ },
58
+ "engines": {
59
+ "node": ">= 22.17.0"
60
60
  }
61
61
  }