@anarchitects/nx-typeorm 0.4.0 → 0.4.1

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 +1 @@
1
- {"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../../src/generators/bootstrap/generator.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,IAAI,EAEJ,KAAK,iBAAiB,EACvB,MAAM,YAAY,CAAC;AAkBpB,UAAU,wBAAwB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,EAAE,CAAC,EAAE,iBAAiB,GAAG,YAAY,CAAC;IACtC,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAOD,QAAA,MAAM,mBAAmB,gFAOf,CAAC;AAWX,KAAK,iBAAiB,GAAG,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC;AAiB9D,wBAA8B,kBAAkB,CAC9C,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,wBAAwB,8BAqClC"}
1
+ {"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../../src/generators/bootstrap/generator.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,IAAI,EAEJ,KAAK,iBAAiB,EACvB,MAAM,YAAY,CAAC;AAkBpB,UAAU,wBAAwB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,EAAE,CAAC,EAAE,iBAAiB,GAAG,YAAY,CAAC;IACtC,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAOD,QAAA,MAAM,mBAAmB,gFAOf,CAAC;AAWX,KAAK,iBAAiB,GAAG,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC;AAmB9D,wBAA8B,kBAAkB,CAC9C,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,wBAAwB,8BA4ClC"}
@@ -33,8 +33,10 @@ const DRIVER_DEPENDENCIES = {
33
33
  export default async function bootstrapGenerator(tree, options) {
34
34
  const database = normalizeDatabase(options.db);
35
35
  const project = readProjectConfiguration(tree, options.project);
36
- const isNestApplication = project.projectType === 'application' &&
37
- hasNestModuleFile(tree, project.root, project.sourceRoot);
36
+ const applicationBootstrapMode = project.projectType === 'application'
37
+ ? resolveApplicationBootstrapMode(tree, options.project, project.root, project.sourceRoot)
38
+ : 'plain';
39
+ const isNestApplication = applicationBootstrapMode === 'nest';
38
40
  const tasks = [];
39
41
  const dependencyTask = addDependenciesToPackageJson(tree, buildRuntimeDependencies(database, isNestApplication), buildToolDependencies());
40
42
  if (!options.skipInstall) {
@@ -230,9 +232,20 @@ function normalizeProjectRelative(pathValue) {
230
232
  }
231
233
  return normalized;
232
234
  }
233
- function hasNestModuleFile(tree, projectRoot, sourceRoot) {
235
+ function resolveApplicationBootstrapMode(tree, projectName, projectRoot, sourceRoot) {
234
236
  const resolvedSourceRoot = sourceRoot ?? joinPathFragments(projectRoot, 'src');
235
- return tree.exists(joinPathFragments(resolvedSourceRoot, 'app.module.ts'));
237
+ if (tree.exists(joinPathFragments(resolvedSourceRoot, 'app.module.ts'))) {
238
+ return 'nest';
239
+ }
240
+ const mainPath = joinPathFragments(resolvedSourceRoot, 'main.ts');
241
+ const mainSource = tree.exists(mainPath)
242
+ ? tree.read(mainPath, 'utf-8')
243
+ : null;
244
+ const looksLikeNestApplication = mainSource?.includes('@nestjs/core') || mainSource?.includes('NestFactory');
245
+ if (looksLikeNestApplication) {
246
+ throw new Error(`Project "${projectName}" looks like a NestJS application but is missing "${joinPathFragments(resolvedSourceRoot, 'app.module.ts')}". NestJS apps must wire TypeORM through TypeOrmModule in app.module.ts; bootstrap will not patch main.ts for NestJS apps.`);
247
+ }
248
+ return 'plain';
236
249
  }
237
250
  function patchAppModule(tree, projectRoot, sourceRoot) {
238
251
  const resolvedSourceRoot = sourceRoot ?? joinPathFragments(projectRoot, 'src');
@@ -271,21 +284,33 @@ function patchAppModule(tree, projectRoot, sourceRoot) {
271
284
  moduleLiteral = callExpression.addArgument('{}');
272
285
  }
273
286
  const importsArray = ensureImportsArray(moduleLiteral);
274
- const hasTypeOrm = importsArray
287
+ const existingTypeOrmRegistration = importsArray
275
288
  .getElements()
276
- .some((element) => element.getText().includes('TypeOrmModule.forRoot'));
277
- if (!hasTypeOrm) {
289
+ .find((element) => element.getText().includes('TypeOrmModule.forRoot'));
290
+ if (existingTypeOrmRegistration) {
291
+ existingTypeOrmRegistration.replaceWithText((writer) => {
292
+ writeNestTypeOrmRegistration(writer);
293
+ });
294
+ }
295
+ else {
278
296
  importsArray.addElement((writer) => {
279
- writer.write('TypeOrmModule.forRoot({');
280
- writer.indent(() => {
281
- writer.writeLine('...AppDataSource.options,');
282
- writer.writeLine('autoLoadEntities: true,');
283
- });
284
- writer.write('})');
297
+ writeNestTypeOrmRegistration(writer);
285
298
  });
286
299
  }
287
300
  tree.write(modulePath, sourceFile.getFullText());
288
301
  }
302
+ function writeNestTypeOrmRegistration(writer) {
303
+ writer.write('TypeOrmModule.forRootAsync({');
304
+ writer.indent(() => {
305
+ writer.writeLine('useFactory: async () => ({');
306
+ writer.indent(() => {
307
+ writer.writeLine('...AppDataSource.options,');
308
+ writer.writeLine('autoLoadEntities: true,');
309
+ });
310
+ writer.writeLine('}),');
311
+ });
312
+ writer.write('})');
313
+ }
289
314
  function patchMainFile(tree, projectRoot, sourceRoot) {
290
315
  const resolvedSourceRoot = sourceRoot ?? joinPathFragments(projectRoot, 'src');
291
316
  const mainPath = joinPathFragments(resolvedSourceRoot, 'main.ts');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anarchitects/nx-typeorm",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Nx plugin providing TypeORM executors, generators, and schema management utilities.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",