@anarchitects/nx-typeorm 0.4.0 → 0.4.2
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;
|
|
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;AAUX,KAAK,iBAAiB,GAAG,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC;AAmB9D,wBAA8B,kBAAkB,CAC9C,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,wBAAwB,8BA4ClC"}
|
|
@@ -20,7 +20,6 @@ const DEFAULT_MIGRATIONS_DIR = 'src/infrastructure-persistence/migrations';
|
|
|
20
20
|
const DEFAULT_MIGRATION_FILE = '1700000000000_init_schema.ts';
|
|
21
21
|
const APP_MIGRATIONS_DATASOURCE_PATH = 'tools/typeorm/datasource.migrations.ts';
|
|
22
22
|
const APP_CONNECTION_OPTIONS_PATH = 'tools/typeorm/connection-options.ts';
|
|
23
|
-
const runtimeImportPath = './data-source';
|
|
24
23
|
const SUPPORTED_DATABASE_DISPLAY = [...SUPPORTED_DATABASES, 'postgresql'].join(', ');
|
|
25
24
|
const DRIVER_DEPENDENCIES = {
|
|
26
25
|
postgres: { packageName: 'pg', version: '^8.20.0' },
|
|
@@ -33,8 +32,10 @@ const DRIVER_DEPENDENCIES = {
|
|
|
33
32
|
export default async function bootstrapGenerator(tree, options) {
|
|
34
33
|
const database = normalizeDatabase(options.db);
|
|
35
34
|
const project = readProjectConfiguration(tree, options.project);
|
|
36
|
-
const
|
|
37
|
-
|
|
35
|
+
const applicationBootstrapMode = project.projectType === 'application'
|
|
36
|
+
? resolveApplicationBootstrapMode(tree, options.project, project.root, project.sourceRoot)
|
|
37
|
+
: 'plain';
|
|
38
|
+
const isNestApplication = applicationBootstrapMode === 'nest';
|
|
38
39
|
const tasks = [];
|
|
39
40
|
const dependencyTask = addDependenciesToPackageJson(tree, buildRuntimeDependencies(database, isNestApplication), buildToolDependencies());
|
|
40
41
|
if (!options.skipInstall) {
|
|
@@ -145,7 +146,7 @@ function prepareApplication(tree, projectRoot, sourceRoot, options, isNestApplic
|
|
|
145
146
|
}
|
|
146
147
|
}
|
|
147
148
|
if (isNestApplication) {
|
|
148
|
-
patchAppModule(tree, projectRoot, sourceRoot);
|
|
149
|
+
patchAppModule(tree, options.project, projectRoot, sourceRoot);
|
|
149
150
|
}
|
|
150
151
|
else {
|
|
151
152
|
patchMainFile(tree, projectRoot, sourceRoot);
|
|
@@ -230,20 +231,72 @@ function normalizeProjectRelative(pathValue) {
|
|
|
230
231
|
}
|
|
231
232
|
return normalized;
|
|
232
233
|
}
|
|
233
|
-
function
|
|
234
|
+
function resolveApplicationBootstrapMode(tree, projectName, projectRoot, sourceRoot) {
|
|
234
235
|
const resolvedSourceRoot = sourceRoot ?? joinPathFragments(projectRoot, 'src');
|
|
235
|
-
|
|
236
|
+
if (resolveNestAppModulePath(tree, projectName, resolvedSourceRoot)) {
|
|
237
|
+
return 'nest';
|
|
238
|
+
}
|
|
239
|
+
const mainPath = joinPathFragments(resolvedSourceRoot, 'main.ts');
|
|
240
|
+
const mainSource = tree.exists(mainPath)
|
|
241
|
+
? tree.read(mainPath, 'utf-8')
|
|
242
|
+
: null;
|
|
243
|
+
const looksLikeNestApplication = mainSource?.includes('@nestjs/core') || mainSource?.includes('NestFactory');
|
|
244
|
+
if (looksLikeNestApplication) {
|
|
245
|
+
throw new Error(`Project "${projectName}" looks like a NestJS application but is missing an app.module.ts under "${resolvedSourceRoot}" (for example "${joinPathFragments(resolvedSourceRoot, 'app.module.ts')}" or "${joinPathFragments(resolvedSourceRoot, 'app', 'app.module.ts')}"). NestJS apps must wire TypeORM through TypeOrmModule in app.module.ts; bootstrap will not patch main.ts for NestJS apps.`);
|
|
246
|
+
}
|
|
247
|
+
return 'plain';
|
|
248
|
+
}
|
|
249
|
+
function resolveNestAppModulePath(tree, projectName, sourceRoot) {
|
|
250
|
+
const rootModulePath = joinPathFragments(sourceRoot, 'app.module.ts');
|
|
251
|
+
if (tree.exists(rootModulePath)) {
|
|
252
|
+
return rootModulePath;
|
|
253
|
+
}
|
|
254
|
+
const nestedModulePaths = findNestedAppModulePaths(tree, sourceRoot);
|
|
255
|
+
if (nestedModulePaths.length === 0) {
|
|
256
|
+
return undefined;
|
|
257
|
+
}
|
|
258
|
+
if (nestedModulePaths.length === 1) {
|
|
259
|
+
return nestedModulePaths[0];
|
|
260
|
+
}
|
|
261
|
+
const listedPaths = nestedModulePaths
|
|
262
|
+
.map((modulePath) => `"${modulePath}"`)
|
|
263
|
+
.join(', ');
|
|
264
|
+
throw new Error(`Project "${projectName}" has multiple app.module.ts files under "${sourceRoot}": ${listedPaths}. Keep a single NestJS module entrypoint or move the canonical module to "${rootModulePath}".`);
|
|
265
|
+
}
|
|
266
|
+
function findNestedAppModulePaths(tree, rootDirectory) {
|
|
267
|
+
const matches = [];
|
|
268
|
+
function visit(directory) {
|
|
269
|
+
let children;
|
|
270
|
+
try {
|
|
271
|
+
children = [...tree.children(directory)].sort();
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
for (const child of children) {
|
|
277
|
+
const childPath = joinPathFragments(directory, child);
|
|
278
|
+
if (child === 'app.module.ts' && tree.exists(childPath)) {
|
|
279
|
+
matches.push(childPath);
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
visit(childPath);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
visit(rootDirectory);
|
|
286
|
+
return matches.sort();
|
|
236
287
|
}
|
|
237
|
-
function patchAppModule(tree, projectRoot, sourceRoot) {
|
|
288
|
+
function patchAppModule(tree, projectName, projectRoot, sourceRoot) {
|
|
238
289
|
const resolvedSourceRoot = sourceRoot ?? joinPathFragments(projectRoot, 'src');
|
|
239
|
-
const modulePath =
|
|
240
|
-
if (!tree.exists(modulePath)) {
|
|
290
|
+
const modulePath = resolveNestAppModulePath(tree, projectName, resolvedSourceRoot);
|
|
291
|
+
if (!modulePath || !tree.exists(modulePath)) {
|
|
241
292
|
return;
|
|
242
293
|
}
|
|
243
294
|
const sourceText = tree.read(modulePath, 'utf-8');
|
|
244
295
|
if (!sourceText) {
|
|
245
296
|
return;
|
|
246
297
|
}
|
|
298
|
+
const runtimeDataSourcePath = joinPathFragments(resolvedSourceRoot, 'data-source.ts');
|
|
299
|
+
const runtimeImportPath = importPathFrom(modulePath, runtimeDataSourcePath);
|
|
247
300
|
const project = new Project({
|
|
248
301
|
useInMemoryFileSystem: true,
|
|
249
302
|
skipAddingFilesFromTsConfig: true,
|
|
@@ -271,21 +324,33 @@ function patchAppModule(tree, projectRoot, sourceRoot) {
|
|
|
271
324
|
moduleLiteral = callExpression.addArgument('{}');
|
|
272
325
|
}
|
|
273
326
|
const importsArray = ensureImportsArray(moduleLiteral);
|
|
274
|
-
const
|
|
327
|
+
const existingTypeOrmRegistration = importsArray
|
|
275
328
|
.getElements()
|
|
276
|
-
.
|
|
277
|
-
if (
|
|
329
|
+
.find((element) => element.getText().includes('TypeOrmModule.forRoot'));
|
|
330
|
+
if (existingTypeOrmRegistration) {
|
|
331
|
+
existingTypeOrmRegistration.replaceWithText((writer) => {
|
|
332
|
+
writeNestTypeOrmRegistration(writer);
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
278
336
|
importsArray.addElement((writer) => {
|
|
279
|
-
writer
|
|
280
|
-
writer.indent(() => {
|
|
281
|
-
writer.writeLine('...AppDataSource.options,');
|
|
282
|
-
writer.writeLine('autoLoadEntities: true,');
|
|
283
|
-
});
|
|
284
|
-
writer.write('})');
|
|
337
|
+
writeNestTypeOrmRegistration(writer);
|
|
285
338
|
});
|
|
286
339
|
}
|
|
287
340
|
tree.write(modulePath, sourceFile.getFullText());
|
|
288
341
|
}
|
|
342
|
+
function writeNestTypeOrmRegistration(writer) {
|
|
343
|
+
writer.write('TypeOrmModule.forRootAsync({');
|
|
344
|
+
writer.indent(() => {
|
|
345
|
+
writer.writeLine('useFactory: async () => ({');
|
|
346
|
+
writer.indent(() => {
|
|
347
|
+
writer.writeLine('...AppDataSource.options,');
|
|
348
|
+
writer.writeLine('autoLoadEntities: true,');
|
|
349
|
+
});
|
|
350
|
+
writer.writeLine('}),');
|
|
351
|
+
});
|
|
352
|
+
writer.write('})');
|
|
353
|
+
}
|
|
289
354
|
function patchMainFile(tree, projectRoot, sourceRoot) {
|
|
290
355
|
const resolvedSourceRoot = sourceRoot ?? joinPathFragments(projectRoot, 'src');
|
|
291
356
|
const mainPath = joinPathFragments(resolvedSourceRoot, 'main.ts');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/plugin/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAGV,aAAa,EAEd,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/plugin/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAGV,aAAa,EAEd,MAAM,YAAY,CAAC;AAmUpB,eAAO,MAAM,aAAa,EAAE,aAgB3B,CAAC;AAEF,eAAO,MAAM,IAAI,uCAAuC,CAAC;;;;;AAEzD,wBAGE"}
|
package/dist/plugin/index.js
CHANGED
|
@@ -20,8 +20,9 @@ const LIB_DATASOURCE_CANDIDATES = [
|
|
|
20
20
|
'tools/typeorm/datasource.migrations.js',
|
|
21
21
|
];
|
|
22
22
|
function isRelevantFile(file) {
|
|
23
|
+
const appModuleInSourceRoot = file.endsWith('/app.module.ts') && file.includes('/src/');
|
|
23
24
|
return (file.endsWith('/project.json') ||
|
|
24
|
-
|
|
25
|
+
appModuleInSourceRoot ||
|
|
25
26
|
file.endsWith('/src/data-source.ts') ||
|
|
26
27
|
file.endsWith('/src/data-source.js') ||
|
|
27
28
|
file.endsWith('/src/typeorm.datasource.ts') ||
|