@mikro-orm/mongodb 7.0.0-dev.11 → 7.0.0-dev.111

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/MongoMikroORM.js CHANGED
@@ -1,24 +1,28 @@
1
1
  import { defineConfig, MikroORM, } from '@mikro-orm/core';
2
2
  import { MongoDriver } from './MongoDriver.js';
3
+ export function defineMongoConfig(options) {
4
+ return defineConfig({ driver: MongoDriver, ...options });
5
+ }
3
6
  /**
4
7
  * @inheritDoc
5
8
  */
6
9
  export class MongoMikroORM extends MikroORM {
7
- static DRIVER = MongoDriver;
8
10
  /**
9
11
  * @inheritDoc
10
12
  */
11
13
  static async init(options) {
12
- return super.init(options);
14
+ return super.init(defineMongoConfig(options));
13
15
  }
14
16
  /**
15
17
  * @inheritDoc
16
18
  */
17
- static initSync(options) {
18
- return super.initSync(options);
19
+ constructor(options) {
20
+ super(defineMongoConfig(options));
21
+ }
22
+ /**
23
+ * Gets the Migrator.
24
+ */
25
+ get migrator() {
26
+ return this.driver.getPlatform().getExtension('Migrator', '@mikro-orm/migrator', '@mikro-orm/migrations-mongodb', this.em);
19
27
  }
20
- }
21
- /* v8 ignore next 3 */
22
- export function defineMongoConfig(options) {
23
- return defineConfig({ driver: MongoDriver, ...options });
24
28
  }
@@ -1,5 +1,5 @@
1
1
  import { ObjectId } from 'mongodb';
2
- import { Platform, type IPrimaryKey, type Primary, type NamingStrategy, type Constructor, type EntityRepository, type EntityProperty, type PopulateOptions, type EntityMetadata, type IDatabaseDriver, type EntityManager, type Configuration, type MikroORM } from '@mikro-orm/core';
2
+ import { Platform, type IPrimaryKey, type Primary, type NamingStrategy, type Constructor, type EntityRepository, type EntityProperty, type PopulateOptions, type EntityMetadata, type IDatabaseDriver, type EntityManager, type Configuration, type MikroORM, type TransformContext } from '@mikro-orm/core';
3
3
  import { MongoExceptionConverter } from './MongoExceptionConverter.js';
4
4
  import { MongoSchemaGenerator } from './MongoSchemaGenerator.js';
5
5
  export declare class MongoPlatform extends Platform {
@@ -16,12 +16,10 @@ export declare class MongoPlatform extends Platform {
16
16
  getSchemaGenerator(driver: IDatabaseDriver, em?: EntityManager): MongoSchemaGenerator;
17
17
  normalizePrimaryKey<T extends number | string = number | string>(data: Primary<T> | IPrimaryKey | ObjectId): T;
18
18
  denormalizePrimaryKey(data: number | string): IPrimaryKey;
19
- getSerializedPrimaryKeyField(field: string): string;
20
- usesDifferentSerializedPrimaryKey(): boolean;
21
19
  usesImplicitTransactions(): boolean;
22
20
  convertsJsonAutomatically(): boolean;
23
21
  convertJsonToDatabaseValue(value: unknown): unknown;
24
- convertJsonToJSValue(value: unknown, prop: EntityProperty): unknown;
22
+ convertJsonToJSValue(value: unknown, context?: TransformContext): unknown;
25
23
  marshallArray(values: string[]): string;
26
24
  cloneEmbeddable<T>(data: T): T;
27
25
  shouldHaveColumn<T>(prop: EntityProperty<T>, populate: PopulateOptions<T>[], exclude?: string[]): boolean;
package/MongoPlatform.js CHANGED
@@ -32,24 +32,18 @@ export class MongoPlatform extends Platform {
32
32
  /* v8 ignore next */
33
33
  return super.getExtension(extensionName, extensionKey, moduleName, em);
34
34
  }
35
- /* v8 ignore next 3: kept for type inference only */
35
+ /* v8 ignore next: kept for type inference only */
36
36
  getSchemaGenerator(driver, em) {
37
37
  return new MongoSchemaGenerator(em ?? driver);
38
38
  }
39
39
  normalizePrimaryKey(data) {
40
- if (Utils.isObjectID(data)) {
40
+ if (Utils.isObject(data) && data.constructor?.name === 'ObjectId') {
41
41
  return data.toHexString();
42
42
  }
43
43
  return data;
44
44
  }
45
45
  denormalizePrimaryKey(data) {
46
- return new ObjectId(data);
47
- }
48
- getSerializedPrimaryKeyField(field) {
49
- return 'id';
50
- }
51
- usesDifferentSerializedPrimaryKey() {
52
- return true;
46
+ return new ObjectId('' + data);
53
47
  }
54
48
  usesImplicitTransactions() {
55
49
  return false;
@@ -60,7 +54,7 @@ export class MongoPlatform extends Platform {
60
54
  convertJsonToDatabaseValue(value) {
61
55
  return Utils.copy(value);
62
56
  }
63
- convertJsonToJSValue(value, prop) {
57
+ convertJsonToJSValue(value, context) {
64
58
  return value;
65
59
  }
66
60
  marshallArray(values) {
@@ -2,13 +2,13 @@ import { AbstractSchemaGenerator, type CreateSchemaOptions, type MikroORM } from
2
2
  import type { MongoDriver } from './MongoDriver.js';
3
3
  export declare class MongoSchemaGenerator extends AbstractSchemaGenerator<MongoDriver> {
4
4
  static register(orm: MikroORM): void;
5
- createSchema(options?: MongoCreateSchemaOptions): Promise<void>;
6
- dropSchema(options?: {
5
+ create(options?: MongoCreateSchemaOptions): Promise<void>;
6
+ drop(options?: {
7
7
  dropMigrationsTable?: boolean;
8
8
  }): Promise<void>;
9
- updateSchema(options?: MongoCreateSchemaOptions): Promise<void>;
9
+ update(options?: MongoCreateSchemaOptions): Promise<void>;
10
10
  ensureDatabase(): Promise<boolean>;
11
- refreshDatabase(options?: MongoCreateSchemaOptions): Promise<void>;
11
+ refresh(options?: MongoCreateSchemaOptions): Promise<void>;
12
12
  dropIndexes(options?: {
13
13
  skipIndexes?: {
14
14
  collection: string;
@@ -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
  }
@@ -1,14 +1,14 @@
1
- import { AbstractSchemaGenerator, Utils, } from '@mikro-orm/core';
1
+ import { AbstractSchemaGenerator, Utils, inspect, } from '@mikro-orm/core';
2
2
  export class MongoSchemaGenerator extends AbstractSchemaGenerator {
3
3
  static register(orm) {
4
4
  orm.config.registerExtension('@mikro-orm/schema-generator', () => new MongoSchemaGenerator(orm.em));
5
5
  }
6
- async createSchema(options = {}) {
6
+ async create(options = {}) {
7
7
  await this.connection.ensureConnection();
8
8
  options.ensureIndexes ??= true;
9
9
  const existing = await this.connection.listCollections();
10
10
  const metadata = this.getOrderedMetadata();
11
- /* v8 ignore start */
11
+ /* v8 ignore next */
12
12
  const promises = metadata
13
13
  .filter(meta => !existing.includes(meta.collection))
14
14
  .map(meta => this.connection.createCollection(meta.collection).catch(err => {
@@ -18,13 +18,12 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
18
18
  throw err;
19
19
  }
20
20
  }));
21
- /* v8 ignore stop */
22
21
  if (options.ensureIndexes) {
23
22
  await this.ensureIndexes({ ensureCollections: false });
24
23
  }
25
24
  await Promise.all(promises);
26
25
  }
27
- async dropSchema(options = {}) {
26
+ async drop(options = {}) {
28
27
  await this.connection.ensureConnection();
29
28
  const existing = await this.connection.listCollections();
30
29
  const metadata = this.getOrderedMetadata();
@@ -36,16 +35,16 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
36
35
  .map(meta => this.connection.dropCollection(meta.collection));
37
36
  await Promise.all(promises);
38
37
  }
39
- async updateSchema(options = {}) {
40
- await this.createSchema(options);
38
+ async update(options = {}) {
39
+ await this.create(options);
41
40
  }
42
41
  async ensureDatabase() {
43
42
  return false;
44
43
  }
45
- async refreshDatabase(options = {}) {
44
+ async refresh(options = {}) {
46
45
  await this.ensureDatabase();
47
- await this.dropSchema();
48
- await this.createSchema(options);
46
+ await this.drop();
47
+ await this.create(options);
49
48
  }
50
49
  async dropIndexes(options) {
51
50
  await this.connection.ensureConnection();
@@ -59,9 +58,9 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
59
58
  const indexes = await db.collection(collection.name).listIndexes().toArray();
60
59
  for (const index of indexes) {
61
60
  const isIdIndex = index.key._id === 1 && Utils.getObjectKeysSize(index.key) === 1;
62
- /* v8 ignore next 3 */
61
+ /* v8 ignore next */
63
62
  if (!isIdIndex && !options?.skipIndexes?.find(idx => idx.collection === collection.name && idx.indexName === index.name)) {
64
- promises.push(db.collection(collection.name).dropIndex(index.name));
63
+ promises.push(this.executeQuery(db.collection(collection.name), 'dropIndex', index.name));
65
64
  }
66
65
  }
67
66
  }
@@ -72,7 +71,7 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
72
71
  options.ensureCollections ??= true;
73
72
  options.retryLimit ??= 3;
74
73
  if (options.ensureCollections) {
75
- await this.createSchema({ ensureIndexes: false });
74
+ await this.create({ ensureIndexes: false });
76
75
  }
77
76
  const promises = [];
78
77
  for (const meta of this.getOrderedMetadata()) {
@@ -113,11 +112,21 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
113
112
  });
114
113
  }
115
114
  }
115
+ mapIndexProperties(index, meta) {
116
+ return Utils.flatten(Utils.asArray(index.properties).map(propName => {
117
+ const rootPropName = propName.split('.')[0];
118
+ const prop = meta.properties[rootPropName];
119
+ if (propName.includes('.')) {
120
+ return [prop.fieldNames[0] + propName.substring(propName.indexOf('.'))];
121
+ }
122
+ return prop?.fieldNames ?? propName;
123
+ }));
124
+ }
116
125
  createIndexes(meta) {
117
126
  const res = [];
118
127
  meta.indexes.forEach(index => {
119
128
  let fieldOrSpec;
120
- const properties = Utils.flatten(Utils.asArray(index.properties).map(prop => meta.properties[prop].fieldNames));
129
+ const properties = this.mapIndexProperties(index, meta);
121
130
  const collection = this.connection.getCollection(meta.className);
122
131
  if (Array.isArray(index.options) && index.options.length === 2 && properties.length === 0) {
123
132
  res.push([collection.collectionName, collection.createIndex(index.options[0], index.options[1])]);
@@ -138,7 +147,7 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
138
147
  else {
139
148
  fieldOrSpec = properties.reduce((o, i) => { o[i] = 1; return o; }, {});
140
149
  }
141
- res.push([collection.collectionName, collection.createIndex(fieldOrSpec, {
150
+ res.push([collection.collectionName, this.executeQuery(collection, 'createIndex', fieldOrSpec, {
142
151
  name: index.name,
143
152
  unique: false,
144
153
  ...index.options,
@@ -146,13 +155,26 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
146
155
  });
147
156
  return res;
148
157
  }
158
+ async executeQuery(collection, method, ...args) {
159
+ const now = Date.now();
160
+ return collection[method](...args).then((res) => {
161
+ Utils.dropUndefinedProperties(args);
162
+ const query = `db.getCollection('${collection.collectionName}').${method}(${args.map(arg => inspect(arg)).join(', ')});`;
163
+ this.config.getLogger().logQuery({
164
+ level: 'info',
165
+ query,
166
+ took: Date.now() - now,
167
+ });
168
+ return res;
169
+ });
170
+ }
149
171
  createUniqueIndexes(meta) {
150
172
  const res = [];
151
173
  meta.uniques.forEach(index => {
152
- const properties = Utils.flatten(Utils.asArray(index.properties).map(prop => meta.properties[prop].fieldNames));
174
+ const properties = this.mapIndexProperties(index, meta);
153
175
  const fieldOrSpec = properties.reduce((o, i) => { o[i] = 1; return o; }, {});
154
176
  const collection = this.connection.getCollection(meta.className);
155
- res.push([collection.collectionName, collection.createIndex(fieldOrSpec, {
177
+ res.push([collection.collectionName, this.executeQuery(collection, 'createIndex', fieldOrSpec, {
156
178
  name: index.name,
157
179
  unique: true,
158
180
  ...index.options,
@@ -168,8 +190,8 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
168
190
  const fieldOrSpec = prop.embeddedPath
169
191
  ? prop.embeddedPath.join('.')
170
192
  : 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),
193
+ return [[collection.collectionName, this.executeQuery(collection, 'createIndex', fieldOrSpec, {
194
+ name: typeof prop[type] === 'string' ? prop[type] : undefined,
173
195
  unique: type === 'unique',
174
196
  sparse: prop.nullable === true,
175
197
  })]];
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)
@@ -382,6 +381,8 @@ See also the list of contributors who [participated](https://github.com/mikro-or
382
381
 
383
382
  Please ⭐️ this repository if this project helped you!
384
383
 
384
+ > If you'd like to support my open-source work, consider sponsoring me directly at [github.com/sponsors/b4nan](https://github.com/sponsors/b4nan).
385
+
385
386
  ## 📝 License
386
387
 
387
388
  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,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/mongodb",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.11",
4
+ "version": "7.0.0-dev.111",
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",
@@ -38,10 +38,10 @@
38
38
  },
39
39
  "homepage": "https://mikro-orm.io",
40
40
  "engines": {
41
- "node": ">= 22.11.0"
41
+ "node": ">= 22.17.0"
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"
@@ -50,12 +50,12 @@
50
50
  "access": "public"
51
51
  },
52
52
  "dependencies": {
53
- "mongodb": "6.15.0"
53
+ "mongodb": "7.0.0"
54
54
  },
55
55
  "devDependencies": {
56
- "@mikro-orm/core": "^6.4.13"
56
+ "@mikro-orm/core": "^6.6.2"
57
57
  },
58
58
  "peerDependencies": {
59
- "@mikro-orm/core": "7.0.0-dev.11"
59
+ "@mikro-orm/core": "7.0.0-dev.111"
60
60
  }
61
61
  }