@anarchitects/nx-typeorm 0.2.0 → 0.4.0
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/README.md +32 -0
- package/dist/generators/bootstrap/files/app/src/data-source.ts__tmpl__ +55 -2
- package/dist/generators/bootstrap/files/app/src/typeorm.datasource.ts__tmpl__ +2 -1
- package/dist/generators/bootstrap/files/app/tools/typeorm/datasource.migrations.ts__tmpl__ +57 -2
- package/dist/generators/bootstrap/files/lib/src/infrastructure-persistence/migrations/1700000000000_init_schema.ts__tmpl__ +7 -3
- package/dist/generators/bootstrap/generator.d.ts +1 -0
- package/dist/generators/bootstrap/generator.d.ts.map +1 -1
- package/dist/generators/bootstrap/generator.js +65 -19
- package/dist/generators/bootstrap/schema.json +5 -0
- package/dist/generators/entity-create/generator.d.ts.map +1 -1
- package/dist/generators/entity-create/generator.js +15 -4
- package/dist/generators/migration-api/generator.d.ts +4 -0
- package/dist/generators/migration-api/generator.d.ts.map +1 -0
- package/dist/generators/migration-api/generator.js +1695 -0
- package/dist/generators/migration-api/schema.json +37 -0
- package/dist/generators/subscriber-create/generator.d.ts.map +1 -1
- package/dist/generators/subscriber-create/generator.js +15 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.json +8 -0
- package/dist/plugin/index.d.ts.map +1 -1
- package/dist/plugin/index.js +9 -1
- package/dist/utils/shared.d.ts +1 -0
- package/dist/utils/shared.d.ts.map +1 -1
- package/dist/utils/shared.js +6 -0
- package/package.json +5 -9
- package/dist/generators/bootstrap/files/app/tools/typeorm/connection-options.ts__tmpl__ +0 -292
package/README.md
CHANGED
|
@@ -100,6 +100,38 @@ nx g @anarchitects/nx-typeorm:migration --project=api --name="add users"
|
|
|
100
100
|
- app: `tools/typeorm/migrations`
|
|
101
101
|
- lib: `src/infrastructure-persistence/migrations`
|
|
102
102
|
|
|
103
|
+
`migration-api` (alias: `migration-api-lib`)
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
nx g @anarchitects/nx-typeorm:migration-api --project=data-access --name="init users"
|
|
107
|
+
nx g @anarchitects/nx-typeorm:migration-api-lib --project=data-access --name="init users"
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
- library-only generator (fails for applications)
|
|
111
|
+
- generates migration files using TypeORM migration API (`QueryRunner`, `Table`,
|
|
112
|
+
`TableColumn`, etc.) without raw SQL strings
|
|
113
|
+
- maintains migration state in
|
|
114
|
+
`src/infrastructure-persistence/migrations/.nx-typeorm-migration-manifest.json`
|
|
115
|
+
- default behavior:
|
|
116
|
+
- no file when entity snapshot is unchanged
|
|
117
|
+
- new consolidated delta migration when entities changed
|
|
118
|
+
- options:
|
|
119
|
+
- required: `--project`
|
|
120
|
+
- required in `fileMode=new` (default): `--name`
|
|
121
|
+
- optional: `--fileMode=new|patch-init` (default `new`)
|
|
122
|
+
- optional: `--entityGlob=<glob-relative-to-project-root>`
|
|
123
|
+
(default `src/infrastructure-persistence/entities/**/*.ts`)
|
|
124
|
+
- optional: `--allowMixedMigrations` to opt into mixed manual/tool migration
|
|
125
|
+
directories
|
|
126
|
+
- optional: `--timestamp=<number>`
|
|
127
|
+
- safety behavior:
|
|
128
|
+
- fails on unsupported/unresolvable decorators/options
|
|
129
|
+
- fails when implicit naming cannot be resolved safely
|
|
130
|
+
- fails on rename-like table/column diffs (manual migration required)
|
|
131
|
+
- `patch-init` only patches bootstrap init migration
|
|
132
|
+
`1700000000000_init_schema.ts` and is only allowed before any generated
|
|
133
|
+
delta migration exists
|
|
134
|
+
|
|
103
135
|
`entity-create` (alias: `entity`)
|
|
104
136
|
|
|
105
137
|
```bash
|
|
@@ -1,6 +1,59 @@
|
|
|
1
1
|
import { DataSource } from 'typeorm';
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
export const AppDataSource = new DataSource({
|
|
4
|
+
<% if (database === 'postgres') { %> type: 'postgres',
|
|
5
|
+
host: process.env.TYPEORM_HOST ?? 'localhost',
|
|
6
|
+
port: Number.parseInt(process.env.TYPEORM_PORT ?? '5432', 10) || 5432,
|
|
7
|
+
username: process.env.TYPEORM_USERNAME ?? 'postgres',
|
|
8
|
+
password: process.env.TYPEORM_PASSWORD ?? 'postgres',
|
|
9
|
+
database: process.env.TYPEORM_DATABASE ?? '<%= appDatabase %>',
|
|
10
|
+
schema: process.env.TYPEORM_SCHEMA ?? 'public',
|
|
11
|
+
ssl: process.env.TYPEORM_SSL === 'true',
|
|
12
|
+
applicationName: process.env.TYPEORM_APPLICATION_NAME ?? '<%= appDatabase %>-service',
|
|
13
|
+
connectTimeoutMS: Number.parseInt(process.env.TYPEORM_CONNECT_TIMEOUT_MS ?? '5000', 10) || 5000,
|
|
14
|
+
<% } %><% if (database === 'mysql' || database === 'mariadb') { %> type: '<%= database %>',
|
|
15
|
+
host: process.env.TYPEORM_HOST ?? 'localhost',
|
|
16
|
+
port: Number.parseInt(process.env.TYPEORM_PORT ?? '3306', 10) || 3306,
|
|
17
|
+
username: process.env.TYPEORM_USERNAME ?? 'root',
|
|
18
|
+
password: process.env.TYPEORM_PASSWORD ?? 'root',
|
|
19
|
+
database: process.env.TYPEORM_DATABASE ?? '<%= appDatabase %>',
|
|
20
|
+
ssl: process.env.TYPEORM_SSL === 'true',
|
|
21
|
+
socketPath: process.env.TYPEORM_SOCKET_PATH?.trim() || undefined,
|
|
22
|
+
charset: process.env.TYPEORM_CHARSET ?? 'utf8mb4',
|
|
23
|
+
timezone: process.env.TYPEORM_TIMEZONE ?? 'Z',
|
|
24
|
+
connectTimeout: Number.parseInt(process.env.TYPEORM_CONNECT_TIMEOUT ?? '10000', 10) || 10000,
|
|
25
|
+
<% } %><% if (database === 'sqlite') { %> type: 'sqlite',
|
|
26
|
+
database: process.env.TYPEORM_DATABASE ?? './tmp/<%= appDatabase %>.sqlite',
|
|
27
|
+
busyTimeout: Number.parseInt(process.env.TYPEORM_BUSY_TIMEOUT ?? '5000', 10) || 5000,
|
|
28
|
+
enableWAL: process.env.TYPEORM_ENABLE_WAL === undefined ? true : process.env.TYPEORM_ENABLE_WAL === 'true',
|
|
29
|
+
<% } %><% if (database === 'better-sqlite3') { %> type: 'better-sqlite3',
|
|
30
|
+
database: process.env.TYPEORM_DATABASE ?? './tmp/<%= appDatabase %>.sqlite',
|
|
31
|
+
enableWAL: process.env.TYPEORM_ENABLE_WAL === undefined ? true : process.env.TYPEORM_ENABLE_WAL === 'true',
|
|
32
|
+
timeout: Number.parseInt(process.env.TYPEORM_TIMEOUT ?? '5000', 10) || 5000,
|
|
33
|
+
readonly: process.env.TYPEORM_READONLY === 'true',
|
|
34
|
+
fileMustExist: process.env.TYPEORM_FILE_MUST_EXIST === 'true',
|
|
35
|
+
statementCacheSize: Number.parseInt(process.env.TYPEORM_STATEMENT_CACHE_SIZE ?? '100', 10) || 100,
|
|
36
|
+
<% } %><% if (database === 'mssql') { %> type: 'mssql',
|
|
37
|
+
host: process.env.TYPEORM_HOST ?? 'localhost',
|
|
38
|
+
port: Number.parseInt(process.env.TYPEORM_PORT ?? '1433', 10) || 1433,
|
|
39
|
+
username: process.env.TYPEORM_USERNAME ?? 'sa',
|
|
40
|
+
password: process.env.TYPEORM_PASSWORD ?? 'StrongPassword!123',
|
|
41
|
+
database: process.env.TYPEORM_DATABASE ?? '<%= appDatabase %>',
|
|
42
|
+
domain: process.env.TYPEORM_DOMAIN?.trim() || undefined,
|
|
43
|
+
connectionTimeout: Number.parseInt(process.env.TYPEORM_CONNECTION_TIMEOUT ?? '15000', 10) || 15000,
|
|
44
|
+
requestTimeout: Number.parseInt(process.env.TYPEORM_REQUEST_TIMEOUT ?? '15000', 10) || 15000,
|
|
45
|
+
options: {
|
|
46
|
+
encrypt: process.env.TYPEORM_ENCRYPT === 'true',
|
|
47
|
+
trustServerCertificate:
|
|
48
|
+
process.env.TYPEORM_TRUST_SERVER_CERTIFICATE === undefined
|
|
49
|
+
? true
|
|
50
|
+
: process.env.TYPEORM_TRUST_SERVER_CERTIFICATE === 'true',
|
|
51
|
+
instanceName: process.env.TYPEORM_INSTANCE_NAME?.trim() || undefined,
|
|
52
|
+
},
|
|
53
|
+
<% } %> synchronize: false,
|
|
54
|
+
logging: false,
|
|
55
|
+
});
|
|
3
56
|
|
|
4
57
|
export function makeRuntimeDataSource(): DataSource {
|
|
5
|
-
return new DataSource(
|
|
58
|
+
return new DataSource(AppDataSource.options);
|
|
6
59
|
}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export { makeRuntimeDataSource } from './data-source';
|
|
1
|
+
export { AppDataSource, makeRuntimeDataSource } from './data-source';
|
|
2
|
+
export { AppDataSource as default } from './data-source';
|
|
@@ -1,4 +1,59 @@
|
|
|
1
1
|
import { DataSource } from 'typeorm';
|
|
2
|
-
import { createMigrationDataSourceOptions } from './connection-options';
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
const MigrationDataSource = new DataSource({
|
|
4
|
+
<% if (database === 'postgres') { %> type: 'postgres',
|
|
5
|
+
host: process.env.TYPEORM_HOST ?? 'localhost',
|
|
6
|
+
port: Number.parseInt(process.env.TYPEORM_PORT ?? '5432', 10) || 5432,
|
|
7
|
+
username: process.env.TYPEORM_USERNAME ?? 'postgres',
|
|
8
|
+
password: process.env.TYPEORM_PASSWORD ?? 'postgres',
|
|
9
|
+
database: process.env.TYPEORM_DATABASE ?? '<%= appDatabase %>',
|
|
10
|
+
schema: process.env.TYPEORM_SCHEMA ?? 'public',
|
|
11
|
+
ssl: process.env.TYPEORM_SSL === 'true',
|
|
12
|
+
applicationName: process.env.TYPEORM_APPLICATION_NAME ?? '<%= appDatabase %>-service',
|
|
13
|
+
connectTimeoutMS: Number.parseInt(process.env.TYPEORM_CONNECT_TIMEOUT_MS ?? '5000', 10) || 5000,
|
|
14
|
+
<% } %><% if (database === 'mysql' || database === 'mariadb') { %> type: '<%= database %>',
|
|
15
|
+
host: process.env.TYPEORM_HOST ?? 'localhost',
|
|
16
|
+
port: Number.parseInt(process.env.TYPEORM_PORT ?? '3306', 10) || 3306,
|
|
17
|
+
username: process.env.TYPEORM_USERNAME ?? 'root',
|
|
18
|
+
password: process.env.TYPEORM_PASSWORD ?? 'root',
|
|
19
|
+
database: process.env.TYPEORM_DATABASE ?? '<%= appDatabase %>',
|
|
20
|
+
ssl: process.env.TYPEORM_SSL === 'true',
|
|
21
|
+
socketPath: process.env.TYPEORM_SOCKET_PATH?.trim() || undefined,
|
|
22
|
+
charset: process.env.TYPEORM_CHARSET ?? 'utf8mb4',
|
|
23
|
+
timezone: process.env.TYPEORM_TIMEZONE ?? 'Z',
|
|
24
|
+
connectTimeout: Number.parseInt(process.env.TYPEORM_CONNECT_TIMEOUT ?? '10000', 10) || 10000,
|
|
25
|
+
<% } %><% if (database === 'sqlite') { %> type: 'sqlite',
|
|
26
|
+
database: process.env.TYPEORM_DATABASE ?? './tmp/<%= appDatabase %>.sqlite',
|
|
27
|
+
busyTimeout: Number.parseInt(process.env.TYPEORM_BUSY_TIMEOUT ?? '5000', 10) || 5000,
|
|
28
|
+
enableWAL: process.env.TYPEORM_ENABLE_WAL === undefined ? true : process.env.TYPEORM_ENABLE_WAL === 'true',
|
|
29
|
+
<% } %><% if (database === 'better-sqlite3') { %> type: 'better-sqlite3',
|
|
30
|
+
database: process.env.TYPEORM_DATABASE ?? './tmp/<%= appDatabase %>.sqlite',
|
|
31
|
+
enableWAL: process.env.TYPEORM_ENABLE_WAL === undefined ? true : process.env.TYPEORM_ENABLE_WAL === 'true',
|
|
32
|
+
timeout: Number.parseInt(process.env.TYPEORM_TIMEOUT ?? '5000', 10) || 5000,
|
|
33
|
+
readonly: process.env.TYPEORM_READONLY === 'true',
|
|
34
|
+
fileMustExist: process.env.TYPEORM_FILE_MUST_EXIST === 'true',
|
|
35
|
+
statementCacheSize: Number.parseInt(process.env.TYPEORM_STATEMENT_CACHE_SIZE ?? '100', 10) || 100,
|
|
36
|
+
<% } %><% if (database === 'mssql') { %> type: 'mssql',
|
|
37
|
+
host: process.env.TYPEORM_HOST ?? 'localhost',
|
|
38
|
+
port: Number.parseInt(process.env.TYPEORM_PORT ?? '1433', 10) || 1433,
|
|
39
|
+
username: process.env.TYPEORM_USERNAME ?? 'sa',
|
|
40
|
+
password: process.env.TYPEORM_PASSWORD ?? 'StrongPassword!123',
|
|
41
|
+
database: process.env.TYPEORM_DATABASE ?? '<%= appDatabase %>',
|
|
42
|
+
domain: process.env.TYPEORM_DOMAIN?.trim() || undefined,
|
|
43
|
+
connectionTimeout: Number.parseInt(process.env.TYPEORM_CONNECTION_TIMEOUT ?? '15000', 10) || 15000,
|
|
44
|
+
requestTimeout: Number.parseInt(process.env.TYPEORM_REQUEST_TIMEOUT ?? '15000', 10) || 15000,
|
|
45
|
+
options: {
|
|
46
|
+
encrypt: process.env.TYPEORM_ENCRYPT === 'true',
|
|
47
|
+
trustServerCertificate:
|
|
48
|
+
process.env.TYPEORM_TRUST_SERVER_CERTIFICATE === undefined
|
|
49
|
+
? true
|
|
50
|
+
: process.env.TYPEORM_TRUST_SERVER_CERTIFICATE === 'true',
|
|
51
|
+
instanceName: process.env.TYPEORM_INSTANCE_NAME?.trim() || undefined,
|
|
52
|
+
},
|
|
53
|
+
<% } %> entities: [],
|
|
54
|
+
migrations: ['dist/tools/typeorm/migrations/*.js'],
|
|
55
|
+
synchronize: false,
|
|
56
|
+
logging: false,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
export default MigrationDataSource;
|
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
import { MigrationInterface, QueryRunner } from 'typeorm';
|
|
1
|
+
import { MigrationInterface, QueryRunner, Table, TableCheck, TableColumn, TableExclusion, TableForeignKey, TableIndex, TableUnique } from 'typeorm';
|
|
2
2
|
import { <%= className %>_SCHEMA as SCHEMA } from '../schema';
|
|
3
3
|
|
|
4
4
|
export class Init<%= className %>Schema1700000000000 implements MigrationInterface {
|
|
5
|
-
async up(
|
|
6
|
-
await
|
|
5
|
+
async up(queryRunner: QueryRunner): Promise<void> {
|
|
6
|
+
await queryRunner.createSchema(SCHEMA, true);
|
|
7
|
+
// nx-typeorm:migration-api:up:start
|
|
8
|
+
// nx-typeorm:migration-api:up:end
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
async down(): Promise<void> {
|
|
12
|
+
// nx-typeorm:migration-api:down:start
|
|
13
|
+
// nx-typeorm:migration-api:down:end
|
|
10
14
|
// Schema intentionally kept
|
|
11
15
|
}
|
|
12
16
|
}
|
|
@@ -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;
|
|
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"}
|
|
@@ -19,11 +19,9 @@ const DEFAULT_SCHEMA_PATH = 'src/infrastructure-persistence/schema.ts';
|
|
|
19
19
|
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
|
+
const APP_CONNECTION_OPTIONS_PATH = 'tools/typeorm/connection-options.ts';
|
|
22
23
|
const runtimeImportPath = './data-source';
|
|
23
|
-
const SUPPORTED_DATABASE_DISPLAY = [
|
|
24
|
-
...SUPPORTED_DATABASES,
|
|
25
|
-
'postgresql',
|
|
26
|
-
].join(', ');
|
|
24
|
+
const SUPPORTED_DATABASE_DISPLAY = [...SUPPORTED_DATABASES, 'postgresql'].join(', ');
|
|
27
25
|
const DRIVER_DEPENDENCIES = {
|
|
28
26
|
postgres: { packageName: 'pg', version: '^8.20.0' },
|
|
29
27
|
mysql: { packageName: 'mysql2', version: '^3.20.0' },
|
|
@@ -38,11 +36,7 @@ export default async function bootstrapGenerator(tree, options) {
|
|
|
38
36
|
const isNestApplication = project.projectType === 'application' &&
|
|
39
37
|
hasNestModuleFile(tree, project.root, project.sourceRoot);
|
|
40
38
|
const tasks = [];
|
|
41
|
-
const dependencyTask = addDependenciesToPackageJson(tree, buildRuntimeDependencies(database, isNestApplication),
|
|
42
|
-
'ts-node': '^10.9.2',
|
|
43
|
-
'typeorm-ts-node-commonjs': '^0.3.20',
|
|
44
|
-
'typeorm-ts-node-esm': '^0.3.20',
|
|
45
|
-
});
|
|
39
|
+
const dependencyTask = addDependenciesToPackageJson(tree, buildRuntimeDependencies(database, isNestApplication), buildToolDependencies());
|
|
46
40
|
if (!options.skipInstall) {
|
|
47
41
|
tasks.push(dependencyTask);
|
|
48
42
|
}
|
|
@@ -70,6 +64,13 @@ function buildRuntimeDependencies(database, isNestApplication) {
|
|
|
70
64
|
}
|
|
71
65
|
return dependencies;
|
|
72
66
|
}
|
|
67
|
+
function buildToolDependencies() {
|
|
68
|
+
return {
|
|
69
|
+
'ts-node': '^10.9.2',
|
|
70
|
+
'typeorm-ts-node-commonjs': '^0.3.20',
|
|
71
|
+
'typeorm-ts-node-esm': '^0.3.20',
|
|
72
|
+
};
|
|
73
|
+
}
|
|
73
74
|
function prepareLibrary(tree, projectName, projectRoot, options) {
|
|
74
75
|
if (!options.domain) {
|
|
75
76
|
throw new Error('Domain option is required when targeting a library.');
|
|
@@ -117,6 +118,7 @@ function prepareApplication(tree, projectRoot, sourceRoot, options, isNestApplic
|
|
|
117
118
|
const database = normalizeDatabase(options.db);
|
|
118
119
|
const projectName = names(options.project).fileName.replace(/-/g, '_');
|
|
119
120
|
const migrationsDatasourcePath = joinPathFragments(projectRoot, APP_MIGRATIONS_DATASOURCE_PATH);
|
|
121
|
+
const connectionOptionsPath = joinPathFragments(projectRoot, APP_CONNECTION_OPTIONS_PATH);
|
|
120
122
|
const existingMigrationsDatasource = tree.exists(migrationsDatasourcePath)
|
|
121
123
|
? tree.read(migrationsDatasourcePath)
|
|
122
124
|
: null;
|
|
@@ -125,9 +127,17 @@ function prepareApplication(tree, projectRoot, sourceRoot, options, isNestApplic
|
|
|
125
127
|
database,
|
|
126
128
|
appDatabase: projectName,
|
|
127
129
|
});
|
|
128
|
-
if (existingMigrationsDatasource) {
|
|
130
|
+
if (options.migrationDatasource && existingMigrationsDatasource) {
|
|
129
131
|
tree.write(migrationsDatasourcePath, existingMigrationsDatasource);
|
|
130
132
|
}
|
|
133
|
+
else if (!options.migrationDatasource &&
|
|
134
|
+
tree.exists(migrationsDatasourcePath)) {
|
|
135
|
+
tree.delete(migrationsDatasourcePath);
|
|
136
|
+
}
|
|
137
|
+
// Legacy helper template is no longer generated; clean up when re-running.
|
|
138
|
+
if (tree.exists(connectionOptionsPath)) {
|
|
139
|
+
tree.delete(connectionOptionsPath);
|
|
140
|
+
}
|
|
131
141
|
if (!options.withCompose) {
|
|
132
142
|
const composePath = joinPathFragments(projectRoot, 'docker-compose.yml');
|
|
133
143
|
if (tree.exists(composePath)) {
|
|
@@ -137,6 +147,9 @@ function prepareApplication(tree, projectRoot, sourceRoot, options, isNestApplic
|
|
|
137
147
|
if (isNestApplication) {
|
|
138
148
|
patchAppModule(tree, projectRoot, sourceRoot);
|
|
139
149
|
}
|
|
150
|
+
else {
|
|
151
|
+
patchMainFile(tree, projectRoot, sourceRoot);
|
|
152
|
+
}
|
|
140
153
|
}
|
|
141
154
|
function normalizeDatabase(database) {
|
|
142
155
|
const normalized = (database ?? DEFAULT_DATABASE).trim().toLowerCase();
|
|
@@ -239,7 +252,7 @@ function patchAppModule(tree, projectRoot, sourceRoot) {
|
|
|
239
252
|
overwrite: true,
|
|
240
253
|
});
|
|
241
254
|
ensureImport(sourceFile, '@nestjs/typeorm', 'TypeOrmModule');
|
|
242
|
-
ensureImport(sourceFile, runtimeImportPath, '
|
|
255
|
+
ensureImport(sourceFile, runtimeImportPath, 'AppDataSource');
|
|
243
256
|
const moduleDecorator = sourceFile
|
|
244
257
|
.getDescendantsOfKind(SyntaxKind.Decorator)
|
|
245
258
|
.find((decorator) => decorator.getName() === 'Module');
|
|
@@ -263,21 +276,54 @@ function patchAppModule(tree, projectRoot, sourceRoot) {
|
|
|
263
276
|
.some((element) => element.getText().includes('TypeOrmModule.forRoot'));
|
|
264
277
|
if (!hasTypeOrm) {
|
|
265
278
|
importsArray.addElement((writer) => {
|
|
266
|
-
writer.write('TypeOrmModule.
|
|
279
|
+
writer.write('TypeOrmModule.forRoot({');
|
|
267
280
|
writer.indent(() => {
|
|
268
|
-
writer.writeLine('
|
|
269
|
-
writer.
|
|
270
|
-
writer.writeLine('...makeRuntimeDataSource().options,');
|
|
271
|
-
writer.writeLine('autoLoadEntities: true,');
|
|
272
|
-
});
|
|
273
|
-
writer.writeLine('}),');
|
|
274
|
-
writer.writeLine('dataSourceFactory: async () => makeRuntimeDataSource().initialize(),');
|
|
281
|
+
writer.writeLine('...AppDataSource.options,');
|
|
282
|
+
writer.writeLine('autoLoadEntities: true,');
|
|
275
283
|
});
|
|
276
284
|
writer.write('})');
|
|
277
285
|
});
|
|
278
286
|
}
|
|
279
287
|
tree.write(modulePath, sourceFile.getFullText());
|
|
280
288
|
}
|
|
289
|
+
function patchMainFile(tree, projectRoot, sourceRoot) {
|
|
290
|
+
const resolvedSourceRoot = sourceRoot ?? joinPathFragments(projectRoot, 'src');
|
|
291
|
+
const mainPath = joinPathFragments(resolvedSourceRoot, 'main.ts');
|
|
292
|
+
if (!tree.exists(mainPath)) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
const sourceText = tree.read(mainPath, 'utf-8');
|
|
296
|
+
if (!sourceText) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
const project = new Project({
|
|
300
|
+
useInMemoryFileSystem: true,
|
|
301
|
+
skipAddingFilesFromTsConfig: true,
|
|
302
|
+
});
|
|
303
|
+
const sourceFile = project.createSourceFile(mainPath, sourceText, {
|
|
304
|
+
overwrite: true,
|
|
305
|
+
});
|
|
306
|
+
ensureImport(sourceFile, './typeorm.datasource', 'AppDataSource');
|
|
307
|
+
const hasDataSourceInit = sourceFile
|
|
308
|
+
.getDescendantsOfKind(SyntaxKind.CallExpression)
|
|
309
|
+
.some((callExpression) => callExpression.getExpression().getText() === 'AppDataSource.initialize');
|
|
310
|
+
if (!hasDataSourceInit) {
|
|
311
|
+
const importDeclarations = sourceFile.getImportDeclarations();
|
|
312
|
+
const insertionIndex = importDeclarations.length > 0
|
|
313
|
+
? importDeclarations[importDeclarations.length - 1].getChildIndex() + 1
|
|
314
|
+
: 0;
|
|
315
|
+
sourceFile.insertStatements(insertionIndex, (writer) => {
|
|
316
|
+
writer.writeLine('');
|
|
317
|
+
writer.writeLine('void AppDataSource.initialize().catch((error) => {');
|
|
318
|
+
writer.indent(() => {
|
|
319
|
+
writer.writeLine("console.error('Failed to initialize TypeORM data source', error);");
|
|
320
|
+
writer.writeLine('process.exit(1);');
|
|
321
|
+
});
|
|
322
|
+
writer.writeLine('});');
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
tree.write(mainPath, sourceFile.getFullText());
|
|
326
|
+
}
|
|
281
327
|
function ensureImport(sourceFile, moduleSpecifier, importName) {
|
|
282
328
|
const existing = sourceFile.getImportDeclaration(moduleSpecifier);
|
|
283
329
|
if (!existing) {
|
|
@@ -35,6 +35,11 @@
|
|
|
35
35
|
"description": "Include a Docker Compose file for local Postgres development.",
|
|
36
36
|
"default": false
|
|
37
37
|
},
|
|
38
|
+
"migrationDatasource": {
|
|
39
|
+
"type": "boolean",
|
|
40
|
+
"description": "Create a separate migration datasource file for CLI commands.",
|
|
41
|
+
"default": false
|
|
42
|
+
},
|
|
38
43
|
"skipInstall": {
|
|
39
44
|
"type": "boolean",
|
|
40
45
|
"description": "Skip running package installations after updating dependencies.",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../../src/generators/entity-create/generator.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,IAAI,EACV,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../../src/generators/entity-create/generator.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,IAAI,EACV,MAAM,YAAY,CAAC;AAKpB,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAC;AAE/D,wBAA8B,qBAAqB,CACjD,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,2BAA2B,iBA+BrC"}
|
|
@@ -1,21 +1,32 @@
|
|
|
1
1
|
import { joinPathFragments, readProjectConfiguration, } from '@nx/devkit';
|
|
2
|
-
import {
|
|
2
|
+
import { existsSync, renameSync } from 'node:fs';
|
|
3
|
+
import { isAbsolute, join, posix } from 'node:path';
|
|
3
4
|
import createEntity from '../../executors/entity-create/executor.js';
|
|
4
|
-
import { normalizeName } from '../../utils/shared.js';
|
|
5
|
+
import { normalizeName, toPascalCase } from '../../utils/shared.js';
|
|
5
6
|
export default async function entityCreateGenerator(tree, options) {
|
|
6
7
|
const projectName = requireOption(options.project, 'project');
|
|
7
|
-
const
|
|
8
|
+
const kebabName = normalizeName(requireOption(options.name, 'name'));
|
|
9
|
+
const classBaseName = ensureSuffix(toPascalCase(kebabName), 'Entity');
|
|
8
10
|
const projectConfig = readProjectConfiguration(tree, projectName);
|
|
9
11
|
const targetDirectory = resolveTargetDirectory(projectConfig.projectType, options.directory);
|
|
10
12
|
const context = createExecutorContext(tree, projectName);
|
|
11
13
|
const result = await createEntity({
|
|
12
14
|
projectRoot: projectConfig.root,
|
|
13
|
-
path: joinPathFragments(targetDirectory,
|
|
15
|
+
path: joinPathFragments(targetDirectory, classBaseName),
|
|
14
16
|
args: options.args,
|
|
15
17
|
}, context);
|
|
16
18
|
if (!result.success) {
|
|
17
19
|
throw new Error('TypeORM entity:create failed.');
|
|
18
20
|
}
|
|
21
|
+
const absDir = join(tree.root, projectConfig.root, targetDirectory);
|
|
22
|
+
const generatedFile = join(absDir, `${classBaseName}.ts`);
|
|
23
|
+
const expectedFile = join(absDir, `${kebabName}.entity.ts`);
|
|
24
|
+
if (existsSync(generatedFile)) {
|
|
25
|
+
renameSync(generatedFile, expectedFile);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function ensureSuffix(value, suffix) {
|
|
29
|
+
return value.endsWith(suffix) ? value : `${value}${suffix}`;
|
|
19
30
|
}
|
|
20
31
|
function resolveTargetDirectory(projectType, directory) {
|
|
21
32
|
if (!directory) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../../src/generators/migration-api/generator.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,IAAI,EACV,MAAM,YAAY,CAAC;AAkBpB,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAC;AAwM/D,wBAA8B,qBAAqB,CACjD,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,2BAA2B,iBA2IrC"}
|