@mikro-orm/cli 7.1.0-dev.5 → 7.1.0-dev.50

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.
@@ -73,5 +73,6 @@ export async function configure() {
73
73
  .command(MigrationCommandFactory.create('fresh'))
74
74
  .command(MigrationCommandFactory.create('log'))
75
75
  .command(MigrationCommandFactory.create('unlog'))
76
+ .command(MigrationCommandFactory.create('rollup'))
76
77
  .command(new DebugCommand());
77
78
  }
package/CLIHelper.js CHANGED
@@ -24,7 +24,6 @@ export class CLIHelper {
24
24
  }
25
25
  contextName ??= process.env.MIKRO_ORM_CONTEXT_NAME ?? 'default';
26
26
  const env = await this.loadEnvironmentVars();
27
- await loadOptionalDependencies(options);
28
27
  // oxfmt-ignore
29
28
  const configFinder = (cfg) => {
30
29
  return typeof cfg === 'object' && cfg !== null && ('contextName' in cfg ? cfg.contextName === contextName : contextName === 'default');
@@ -35,7 +34,9 @@ export class CLIHelper {
35
34
  const result = await this.getConfigFile(paths);
36
35
  if (!result[0]) {
37
36
  if (Utils.hasObjectKeys(env)) {
38
- return new Configuration(Utils.mergeConfig({ contextName }, options.preferEnvVars ? options : env, options.preferEnvVars ? env : options));
37
+ const merged = Utils.mergeConfig({ contextName }, options.preferEnvVars ? options : env, options.preferEnvVars ? env : options);
38
+ await loadOptionalDependencies(merged);
39
+ return new Configuration(merged);
39
40
  }
40
41
  throw new Error(`MikroORM config file not found in ['${paths.join(`', '`)}']`);
41
42
  }
@@ -209,6 +210,15 @@ export class CLIHelper {
209
210
  case 'postgresql':
210
211
  ret.driver ??= await import('@mikro-orm/postgresql').then(m => m.PostgreSqlDriver);
211
212
  break;
213
+ case 'pglite': {
214
+ // Variable specifier (instead of a literal) keeps `@electric-sql/pglite`'s
215
+ // d.ts — which references DOM/Emscripten ambient globals not in our TS
216
+ // lib — out of the CLI's compile graph. Safe here: the CLI is a Node
217
+ // binary, not bundled into end-user apps.
218
+ const name = '@mikro-orm/pglite';
219
+ ret.driver ??= (await import(name)).PgliteDriver;
220
+ break;
221
+ }
212
222
  case 'sqlite':
213
223
  ret.driver ??= await import('@mikro-orm/sqlite').then(m => m.SqliteDriver);
214
224
  break;
package/README.md CHANGED
@@ -2,7 +2,7 @@
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, SQLite (including libSQL), MSSQL and Oracle 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 (including CockroachDB and PGlite), 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
 
@@ -19,6 +19,7 @@ Install a driver package for your database:
19
19
 
20
20
  ```sh
21
21
  npm install @mikro-orm/postgresql # PostgreSQL
22
+ npm install @mikro-orm/pglite # PGlite (embedded PostgreSQL in WASM)
22
23
  npm install @mikro-orm/mysql # MySQL
23
24
  npm install @mikro-orm/mariadb # MariaDB
24
25
  npm install @mikro-orm/sqlite # SQLite
@@ -1,10 +1,11 @@
1
1
  import { mkdirSync, writeFileSync } from 'node:fs';
2
- import { basename, dirname, join, relative, resolve } from 'node:path';
2
+ import { basename, dirname, join, resolve } from 'node:path';
3
3
  import { colors, EntitySchema, MetadataStorage } from '@mikro-orm/core';
4
4
  import { fs } from '@mikro-orm/core/fs-utils';
5
5
  import { CLIHelper } from '../CLIHelper.js';
6
6
  const driverPackageMap = {
7
7
  PostgreSqlDriver: '@mikro-orm/postgresql',
8
+ PgliteDriver: '@mikro-orm/pglite',
8
9
  MySqlDriver: '@mikro-orm/mysql',
9
10
  MariaDbDriver: '@mikro-orm/mariadb',
10
11
  SqliteDriver: '@mikro-orm/sqlite',
@@ -176,10 +177,8 @@ export class DiscoveryExportCommand {
176
177
  }
177
178
  // Generate import lines
178
179
  for (const [filePath, items] of byFile) {
179
- let rel = relative(outDir, filePath);
180
- if (!rel.startsWith('.')) {
181
- rel = './' + rel;
182
- }
180
+ // `fs.relativePath` ensures POSIX separators (node:path.relative returns backslashes on Windows)
181
+ let rel = fs.relativePath(filePath, outDir);
183
182
  // Remove .ts extension and optionally add .js for ESM
184
183
  rel = rel.replace(/\.[cm]?[jt]s$/, '');
185
184
  if (esm) {
@@ -195,11 +194,12 @@ export class DiscoveryExportCommand {
195
194
  lines.push(`import { ${names} } from '${rel}';`);
196
195
  }
197
196
  }
198
- const isMongo = driverPackage === '@mikro-orm/mongodb';
199
- // Type imports (Kysely types are not available for MongoDB)
200
- if (!isMongo) {
201
- lines.push(`import type { EntitySchemaWithMeta, InferKyselyDB, InferClassEntityDB } from '${driverPackage}';`);
202
- }
197
+ // Bring in the driver's `EntityManager` class as a runtime value, not just
198
+ // a type — DI containers (NestJS, etc.) read it via `design:paramtypes`
199
+ // reflect-metadata, so the consumer needs the actual class reference, not
200
+ // an erased type alias. Aliasing keeps the local name free for our own
201
+ // `EntityManager` re-export.
202
+ lines.push(`import { EntityManager as DriverEntityManager } from '${driverPackage}';`);
203
203
  lines.push('');
204
204
  // entities array
205
205
  lines.push('export const entities = [');
@@ -207,12 +207,20 @@ export class DiscoveryExportCommand {
207
207
  lines.push(` ${item.exportName},`);
208
208
  }
209
209
  lines.push('] as const;');
210
- // Database type (Kysely is SQL-only, skip for MongoDB)
211
- if (!isMongo) {
212
- lines.push('');
213
- lines.push('export type Database = InferKyselyDB<Extract<(typeof entities)[number], EntitySchemaWithMeta>>');
214
- lines.push(' & InferClassEntityDB<(typeof entities)[number]>;');
215
- }
210
+ lines.push('');
211
+ // The entity tuple type — usable in `MikroORM<Driver, EM, Database>`,
212
+ // for typed repository helpers, and anywhere entity classes/schemas are
213
+ // accepted as a tuple.
214
+ lines.push('export type Database = typeof entities;');
215
+ lines.push('');
216
+ // Typed `EntityManager` for DI / NestJS contexts. Declaration merging
217
+ // lets us export the same name twice: the `type` carries the entity
218
+ // tuple via the `'~entities'` graft (so `em.getKysely(opts)` keeps full
219
+ // inference), and the `const` is the driver's actual EM class — so
220
+ // `constructor(em: EntityManager) {}` resolves through Nest's container
221
+ // just like importing the class straight from the driver package.
222
+ lines.push("export type EntityManager = DriverEntityManager & { '~entities': Database };");
223
+ lines.push('export const EntityManager = DriverEntityManager;');
216
224
  lines.push('');
217
225
  return lines.join('\n');
218
226
  }
@@ -11,6 +11,7 @@ export declare class MigrationCommandFactory {
11
11
  fresh: string;
12
12
  log: string;
13
13
  unlog: string;
14
+ rollup: string;
14
15
  };
15
16
  static create<const T extends MigratorMethod>(command: T): {
16
17
  command: string;
@@ -24,6 +25,7 @@ export declare class MigrationCommandFactory {
24
25
  fresh: string;
25
26
  log: string;
26
27
  unlog: string;
28
+ rollup: string;
27
29
  }[T];
28
30
  builder: (args: Argv<BaseArgs>) => Argv<MigrationOptionsMap[T]>;
29
31
  handler: (args: ArgumentsCamelCase<MigrationOptionsMap[T]>) => Promise<void>;
@@ -41,6 +43,7 @@ export declare class MigrationCommandFactory {
41
43
  private static handleCheckCommand;
42
44
  private static handleFreshCommand;
43
45
  private static handleLogUnlogCommand;
46
+ private static handleRollupCommand;
44
47
  private static getUpDownOptions;
45
48
  private static getUpDownSuccessMessage;
46
49
  }
@@ -48,6 +51,7 @@ type CliUpDownOptions = BaseArgs & {
48
51
  to?: string | number;
49
52
  from?: string | number;
50
53
  only?: string;
54
+ schema?: string;
51
55
  };
52
56
  type MigratorFreshOptions = BaseArgs & {
53
57
  dropDb?: boolean;
@@ -73,6 +77,7 @@ type MigrationOptionsMap = {
73
77
  fresh: MigratorFreshOptions;
74
78
  log: MigratorLogUnlogOptions;
75
79
  unlog: MigratorLogUnlogOptions;
80
+ rollup: BaseArgs;
76
81
  };
77
82
  type MigratorMethod = keyof MigrationOptionsMap;
78
83
  type Opts = BaseArgs & MigratorCreateOptions & CliUpDownOptions & MigratorFreshOptions & MigratorLogUnlogOptions;
@@ -11,6 +11,7 @@ export class MigrationCommandFactory {
11
11
  fresh: 'Clear the database and rerun all migrations',
12
12
  log: 'Mark a migration as executed without running it',
13
13
  unlog: 'Remove a migration from the executed list without reverting it',
14
+ rollup: 'Combine multiple migrations into a single migration',
14
15
  };
15
16
  static create(command) {
16
17
  // oxfmt-ignore
@@ -61,6 +62,11 @@ export class MigrationCommandFactory {
61
62
  type: 'string',
62
63
  desc: 'Migrate only specified versions',
63
64
  });
65
+ args.option('s', {
66
+ alias: 'schema',
67
+ type: 'string',
68
+ desc: 'Target schema to run the migration against (PostgreSQL, MySQL, Oracle)',
69
+ });
64
70
  return args;
65
71
  }
66
72
  static configureCreateCommand(args) {
@@ -119,6 +125,9 @@ export class MigrationCommandFactory {
119
125
  case 'unlog':
120
126
  await this.handleLogUnlogCommand(args, orm.migrator, method);
121
127
  break;
128
+ case 'rollup':
129
+ await this.handleRollupCommand(orm.migrator);
130
+ break;
122
131
  }
123
132
  await orm.close(true);
124
133
  }
@@ -206,12 +215,16 @@ export class MigrationCommandFactory {
206
215
  const action = method === 'log' ? 'logged' : 'unlogged';
207
216
  CLIHelper.dump(colors.green(`Successfully ${action} migration '${args.name}'`));
208
217
  }
218
+ static async handleRollupCommand(migrator) {
219
+ const ret = await migrator.rollup();
220
+ CLIHelper.dump(colors.green(`${ret.fileName} successfully created (rollup)`));
221
+ }
209
222
  static getUpDownOptions(flags) {
210
- if (!flags.to && !flags.from && flags.only) {
211
- return { migrations: flags.only.split(/[, ]+/) };
212
- }
213
- const ret = {};
223
+ const ret = !flags.to && !flags.from && flags.only ? { migrations: flags.only.split(/[, ]+/) } : {};
214
224
  ['from', 'to'].filter(k => flags[k]).forEach(k => (ret[k] = flags[k] === '0' ? 0 : flags[k]));
225
+ if (flags.schema) {
226
+ ret.schema = flags.schema;
227
+ }
215
228
  return ret;
216
229
  }
217
230
  static getUpDownSuccessMessage(method, options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/cli",
3
- "version": "7.1.0-dev.5",
3
+ "version": "7.1.0-dev.50",
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
  "keywords": [
6
6
  "data-mapper",
@@ -50,8 +50,8 @@
50
50
  "copy": "node ../../scripts/copy.mjs"
51
51
  },
52
52
  "dependencies": {
53
- "@mikro-orm/core": "7.1.0-dev.5",
54
- "mikro-orm": "7.1.0-dev.5",
53
+ "@mikro-orm/core": "7.1.0-dev.50",
54
+ "mikro-orm": "7.1.0-dev.50",
55
55
  "yargs": "17.7.2"
56
56
  },
57
57
  "engines": {