@mikro-orm/cli 7.0.0-dev.220 → 7.0.0-dev.222

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.
@@ -7,6 +7,7 @@ import { CreateSeederCommand } from './commands/CreateSeederCommand.js';
7
7
  import { DatabaseSeedCommand } from './commands/DatabaseSeedCommand.js';
8
8
  import { DebugCommand } from './commands/DebugCommand.js';
9
9
  import { GenerateCacheCommand } from './commands/GenerateCacheCommand.js';
10
+ import { CompileCommand } from './commands/CompileCommand.js';
10
11
  import { GenerateEntitiesCommand } from './commands/GenerateEntitiesCommand.js';
11
12
  import { ImportCommand } from './commands/ImportCommand.js';
12
13
  import { MigrationCommandFactory } from './commands/MigrationCommandFactory.js';
@@ -51,6 +52,7 @@ export async function configure() {
51
52
  .version(version)
52
53
  .command(new ClearCacheCommand())
53
54
  .command(new GenerateCacheCommand())
55
+ .command(new CompileCommand())
54
56
  .command(new GenerateEntitiesCommand())
55
57
  .command(new CreateDatabaseCommand())
56
58
  .command(new ImportCommand())
@@ -0,0 +1,21 @@
1
+ import type { ArgumentsCamelCase, Argv } from 'yargs';
2
+ import { MetadataStorage, type Configuration } from '@mikro-orm/core';
3
+ import type { BaseArgs, BaseCommand } from '../CLIConfigurator.js';
4
+ type CompileArgs = BaseArgs & {
5
+ out?: string;
6
+ };
7
+ export declare class CompileCommand implements BaseCommand<CompileArgs> {
8
+ command: string;
9
+ describe: string;
10
+ builder: (args: Argv<BaseArgs>) => Argv<CompileArgs>;
11
+ /**
12
+ * @inheritDoc
13
+ */
14
+ handler(args: ArgumentsCamelCase<CompileArgs>): Promise<void>;
15
+ static capture(metadata: MetadataStorage, config: Configuration): {
16
+ key: string;
17
+ contextKeys: string[];
18
+ code: string;
19
+ }[];
20
+ }
21
+ export {};
@@ -0,0 +1,94 @@
1
+ import { writeFileSync, mkdirSync } from 'node:fs';
2
+ import { resolve, dirname } from 'node:path';
3
+ import { MetadataDiscovery, MetadataStorage, Utils, EntityComparator, ObjectHydrator, colors } from '@mikro-orm/core';
4
+ import { fs } from '@mikro-orm/core/fs-utils';
5
+ import { CLIHelper } from '../CLIHelper.js';
6
+ export class CompileCommand {
7
+ command = 'compile';
8
+ describe = 'Pre-compile optimized entity functions for runtimes that prohibit eval (e.g. Cloudflare Workers)';
9
+ builder = (args) => {
10
+ args.option('out', {
11
+ type: 'string',
12
+ desc: 'Output path for the generated file (defaults to next to your ORM config)',
13
+ });
14
+ return args;
15
+ };
16
+ /**
17
+ * @inheritDoc
18
+ */
19
+ async handler(args) {
20
+ const config = await CLIHelper.getConfiguration(args.contextName, args.config);
21
+ const settings = CLIHelper.getSettings();
22
+ config.set('debug', !!settings.verbose);
23
+ const metadata = await new MetadataDiscovery(new MetadataStorage(), config.getDriver().getPlatform(), config).discover(false);
24
+ // Default output path to next to the ORM config file
25
+ if (!args.out) {
26
+ const configPaths = args.config ?? (await CLIHelper.getConfigPaths());
27
+ for (const configPath of configPaths) {
28
+ const absPath = fs.absolutePath(configPath);
29
+ if (fs.pathExists(absPath)) {
30
+ args.out = resolve(dirname(absPath), 'compiled-functions.js');
31
+ break;
32
+ }
33
+ }
34
+ }
35
+ const captured = CompileCommand.capture(metadata, config);
36
+ const entries = captured.map(({ key, contextKeys, code }) => {
37
+ const params = contextKeys.join(', ');
38
+ const indentedCode = code.replace(/\n/g, '\n ');
39
+ return ` '${key}': function(${params}) {\n ${indentedCode}\n }`;
40
+ });
41
+ const esm = CLIHelper.isESM();
42
+ const version = Utils.getORMVersion();
43
+ const output = esm
44
+ ? `export default {\n __version: '${version}',\n${entries.join(',\n')}\n};\n`
45
+ : `'use strict';\nmodule.exports = {\n __version: '${version}',\n${entries.join(',\n')}\n};\n`;
46
+ const outPath = args.out ?? resolve(process.cwd(), 'compiled-functions.js');
47
+ const dtsPath = outPath.replace(/\.js$/, '.d.ts');
48
+ const dts = esm
49
+ ? `import type { CompiledFunctions } from '@mikro-orm/core';\ndeclare const compiledFunctions: CompiledFunctions;\nexport default compiledFunctions;\n`
50
+ : `import type { CompiledFunctions } from '@mikro-orm/core';\ndeclare const compiledFunctions: CompiledFunctions;\nexport = compiledFunctions;\n`;
51
+ mkdirSync(dirname(outPath), { recursive: true });
52
+ writeFileSync(outPath, output);
53
+ writeFileSync(dtsPath, dts);
54
+ CLIHelper.dump(colors.green(`Compiled functions generated to ${outPath} (${captured.length} functions)`));
55
+ CLIHelper.dump(`\nExample usage in your ORM config:\n`);
56
+ const importPath = esm ? './compiled-functions.js' : './compiled-functions';
57
+ CLIHelper.dump(` ${esm ? 'import' : 'const'} compiledFunctions ${esm ? 'from ' : '= require('}${colors.cyan(`'${importPath}'`)}${esm ? '' : ')'};`);
58
+ CLIHelper.dump('');
59
+ CLIHelper.dump(` export default defineConfig({ compiledFunctions });\n`);
60
+ }
61
+ static capture(metadata, config) {
62
+ const captured = [];
63
+ const original = Utils.createFunction;
64
+ Utils.createFunction = (context, code, _compiledFunctions, key) => {
65
+ captured.push({ key: key, contextKeys: [...context.keys()], code });
66
+ return original.call(Utils, context, code);
67
+ };
68
+ try {
69
+ const platform = config.getDriver().getPlatform();
70
+ const hydrator = new ObjectHydrator(metadata, platform, config);
71
+ const comparator = new EntityComparator(metadata, platform, config);
72
+ for (const meta of metadata) {
73
+ hydrator.getEntityHydrator(meta, 'full', false);
74
+ hydrator.getEntityHydrator(meta, 'full', true);
75
+ comparator.getEntityComparator(meta.class);
76
+ comparator.getSnapshotGenerator(meta.class);
77
+ comparator.getResultMapper(meta);
78
+ if (!meta.embeddable && !meta.virtual) {
79
+ hydrator.getEntityHydrator(meta, 'reference', false);
80
+ hydrator.getEntityHydrator(meta, 'reference', true);
81
+ }
82
+ if (meta.primaryKeys.length > 0) {
83
+ comparator.getPkGetter(meta);
84
+ comparator.getPkGetterConverted(meta);
85
+ comparator.getPkSerializer(meta);
86
+ }
87
+ }
88
+ }
89
+ finally {
90
+ Utils.createFunction = original;
91
+ }
92
+ return captured;
93
+ }
94
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/cli",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.220",
4
+ "version": "7.0.0-dev.222",
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",
@@ -53,8 +53,8 @@
53
53
  "access": "public"
54
54
  },
55
55
  "dependencies": {
56
- "@mikro-orm/core": "7.0.0-dev.220",
57
- "mikro-orm": "7.0.0-dev.220",
56
+ "@mikro-orm/core": "7.0.0-dev.222",
57
+ "mikro-orm": "7.0.0-dev.222",
58
58
  "yargs": "17.7.2"
59
59
  }
60
60
  }