@anarchitects/nx-typeorm 0.0.2 → 0.1.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.
Files changed (97) hide show
  1. package/README.md +155 -49
  2. package/dist/executors/cache-clear/executor.d.ts +6 -0
  3. package/dist/executors/cache-clear/executor.d.ts.map +1 -0
  4. package/dist/executors/cache-clear/executor.js +5 -0
  5. package/dist/executors/cache-clear/schema.json +28 -0
  6. package/dist/executors/ensure-schema/executor.d.ts +1 -5
  7. package/dist/executors/ensure-schema/executor.d.ts.map +1 -1
  8. package/dist/executors/ensure-schema/executor.js +1 -1
  9. package/dist/executors/entity-create/executor.d.ts +6 -0
  10. package/dist/executors/entity-create/executor.d.ts.map +1 -0
  11. package/dist/executors/entity-create/executor.js +15 -0
  12. package/dist/executors/entity-create/schema.json +23 -0
  13. package/dist/executors/generate/executor.d.ts +1 -7
  14. package/dist/executors/generate/executor.d.ts.map +1 -1
  15. package/dist/executors/generate/executor.js +12 -24
  16. package/dist/executors/generate/schema.json +11 -1
  17. package/dist/executors/init/executor.d.ts +6 -0
  18. package/dist/executors/init/executor.d.ts.map +1 -0
  19. package/dist/executors/init/executor.js +24 -0
  20. package/dist/executors/init/schema.json +40 -0
  21. package/dist/executors/migration-create/executor.d.ts +6 -0
  22. package/dist/executors/migration-create/executor.d.ts.map +1 -0
  23. package/dist/executors/migration-create/executor.js +19 -0
  24. package/dist/executors/migration-create/schema.json +31 -0
  25. package/dist/executors/migration-show/executor.d.ts +6 -0
  26. package/dist/executors/migration-show/executor.d.ts.map +1 -0
  27. package/dist/executors/migration-show/executor.js +5 -0
  28. package/dist/executors/migration-show/schema.json +28 -0
  29. package/dist/executors/query/executor.d.ts +6 -0
  30. package/dist/executors/query/executor.d.ts.map +1 -0
  31. package/dist/executors/query/executor.js +9 -0
  32. package/dist/executors/query/schema.json +33 -0
  33. package/dist/executors/revert/executor.d.ts +1 -5
  34. package/dist/executors/revert/executor.d.ts.map +1 -1
  35. package/dist/executors/revert/executor.js +8 -19
  36. package/dist/executors/revert/schema.json +7 -1
  37. package/dist/executors/run/executor.d.ts +1 -6
  38. package/dist/executors/run/executor.d.ts.map +1 -1
  39. package/dist/executors/run/executor.js +7 -18
  40. package/dist/executors/run/schema.json +6 -0
  41. package/dist/executors/schema-drop/executor.d.ts +6 -0
  42. package/dist/executors/schema-drop/executor.d.ts.map +1 -0
  43. package/dist/executors/schema-drop/executor.js +5 -0
  44. package/dist/executors/schema-drop/schema.json +28 -0
  45. package/dist/executors/schema-log/executor.d.ts +6 -0
  46. package/dist/executors/schema-log/executor.d.ts.map +1 -0
  47. package/dist/executors/schema-log/executor.js +5 -0
  48. package/dist/executors/schema-log/schema.json +28 -0
  49. package/dist/executors/schema-sync/executor.d.ts +6 -0
  50. package/dist/executors/schema-sync/executor.d.ts.map +1 -0
  51. package/dist/executors/schema-sync/executor.js +5 -0
  52. package/dist/executors/schema-sync/schema.json +28 -0
  53. package/dist/executors/seed/executor.d.ts +1 -7
  54. package/dist/executors/seed/executor.d.ts.map +1 -1
  55. package/dist/executors/seed/executor.js +1 -1
  56. package/dist/executors/subscriber-create/executor.d.ts +6 -0
  57. package/dist/executors/subscriber-create/executor.d.ts.map +1 -0
  58. package/dist/executors/subscriber-create/executor.js +13 -0
  59. package/dist/executors/subscriber-create/schema.json +23 -0
  60. package/dist/executors/version/executor.d.ts +6 -0
  61. package/dist/executors/version/executor.d.ts.map +1 -0
  62. package/dist/executors/version/executor.js +5 -0
  63. package/dist/executors/version/schema.json +14 -0
  64. package/dist/generators/bootstrap/files/app/env.example__tmpl__ +55 -1
  65. package/dist/generators/bootstrap/files/app/src/data-source.ts__tmpl__ +6 -0
  66. package/dist/generators/bootstrap/files/app/src/typeorm.datasource.ts__tmpl__ +1 -15
  67. package/dist/generators/bootstrap/files/app/tools/typeorm/connection-options.ts__tmpl__ +292 -0
  68. package/dist/generators/bootstrap/files/app/tools/typeorm/datasource.migrations.ts__tmpl__ +2 -7
  69. package/dist/generators/bootstrap/files/lib/project.json.partial__tmpl__ +3 -1
  70. package/dist/generators/bootstrap/generator.d.ts +5 -1
  71. package/dist/generators/bootstrap/generator.d.ts.map +1 -1
  72. package/dist/generators/bootstrap/generator.js +179 -31
  73. package/dist/generators/bootstrap/schema.json +18 -1
  74. package/dist/generators/init/generator.d.ts +4 -0
  75. package/dist/generators/init/generator.d.ts.map +1 -0
  76. package/dist/generators/init/generator.js +31 -0
  77. package/dist/generators/init/schema.json +18 -0
  78. package/dist/index.d.ts +13 -0
  79. package/dist/index.d.ts.map +1 -1
  80. package/dist/index.js +13 -0
  81. package/dist/index.json +48 -0
  82. package/dist/plugin/index.d.ts +9 -0
  83. package/dist/plugin/index.d.ts.map +1 -0
  84. package/dist/plugin/index.js +241 -0
  85. package/dist/plugins/typeorm.d.ts +1 -6
  86. package/dist/plugins/typeorm.d.ts.map +1 -1
  87. package/dist/plugins/typeorm.js +1 -161
  88. package/dist/utils/cli.d.ts +17 -0
  89. package/dist/utils/cli.d.ts.map +1 -0
  90. package/dist/utils/cli.js +55 -0
  91. package/dist/{executors → utils}/shared.d.ts +12 -9
  92. package/dist/utils/shared.d.ts.map +1 -0
  93. package/dist/utils/shared.js +260 -0
  94. package/package.json +7 -12
  95. package/dist/executors/shared.d.ts.map +0 -1
  96. package/dist/executors/shared.js +0 -71
  97. package/dist/generators/bootstrap/files/app/tools/typeorm/datasource.runtime.ts__tmpl__ +0 -9
@@ -0,0 +1,18 @@
1
+ {
2
+ "$schema": "http://json-schema.org/schema",
3
+ "title": "TypeORM Init Generator",
4
+ "type": "object",
5
+ "properties": {
6
+ "skipFormat": {
7
+ "type": "boolean",
8
+ "default": false,
9
+ "description": "Skip formatting files after generation."
10
+ },
11
+ "skipInstall": {
12
+ "type": "boolean",
13
+ "default": false,
14
+ "description": "Skip running package manager install tasks."
15
+ }
16
+ },
17
+ "additionalProperties": false
18
+ }
package/dist/index.d.ts CHANGED
@@ -3,5 +3,18 @@ export { default as runExecutor } from './executors/run/executor.js';
3
3
  export { default as revertExecutor } from './executors/revert/executor.js';
4
4
  export { default as ensureSchemaExecutor } from './executors/ensure-schema/executor.js';
5
5
  export { default as seedExecutor } from './executors/seed/executor.js';
6
+ export { default as migrationCreateExecutor } from './executors/migration-create/executor.js';
7
+ export { default as migrationShowExecutor } from './executors/migration-show/executor.js';
8
+ export { default as schemaSyncExecutor } from './executors/schema-sync/executor.js';
9
+ export { default as schemaLogExecutor } from './executors/schema-log/executor.js';
10
+ export { default as schemaDropExecutor } from './executors/schema-drop/executor.js';
11
+ export { default as queryExecutor } from './executors/query/executor.js';
12
+ export { default as cacheClearExecutor } from './executors/cache-clear/executor.js';
13
+ export { default as entityCreateExecutor } from './executors/entity-create/executor.js';
14
+ export { default as subscriberCreateExecutor } from './executors/subscriber-create/executor.js';
15
+ export { default as versionExecutor } from './executors/version/executor.js';
16
+ export { default as initExecutor } from './executors/init/executor.js';
17
+ export { default as initGenerator } from './generators/init/generator.js';
6
18
  export { default as bootstrapGenerator } from './generators/bootstrap/generator.js';
19
+ export { default } from './plugin/index.js';
7
20
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AAC/E,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAC3E,OAAO,EAAE,OAAO,IAAI,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AACxF,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAEvE,OAAO,EAAE,OAAO,IAAI,kBAAkB,EAAE,MAAM,qCAAqC,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AAC/E,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAC3E,OAAO,EAAE,OAAO,IAAI,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AACxF,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,8BAA8B,CAAC;AACvE,OAAO,EAAE,OAAO,IAAI,uBAAuB,EAAE,MAAM,0CAA0C,CAAC;AAC9F,OAAO,EAAE,OAAO,IAAI,qBAAqB,EAAE,MAAM,wCAAwC,CAAC;AAC1F,OAAO,EAAE,OAAO,IAAI,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AACpF,OAAO,EAAE,OAAO,IAAI,iBAAiB,EAAE,MAAM,oCAAoC,CAAC;AAClF,OAAO,EAAE,OAAO,IAAI,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AACpF,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,+BAA+B,CAAC;AACzE,OAAO,EAAE,OAAO,IAAI,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AACpF,OAAO,EAAE,OAAO,IAAI,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AACxF,OAAO,EAAE,OAAO,IAAI,wBAAwB,EAAE,MAAM,2CAA2C,CAAC;AAChG,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,iCAAiC,CAAC;AAC7E,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAEvE,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,gCAAgC,CAAC;AAC1E,OAAO,EAAE,OAAO,IAAI,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AAEpF,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/index.js CHANGED
@@ -3,4 +3,17 @@ export { default as runExecutor } from './executors/run/executor.js';
3
3
  export { default as revertExecutor } from './executors/revert/executor.js';
4
4
  export { default as ensureSchemaExecutor } from './executors/ensure-schema/executor.js';
5
5
  export { default as seedExecutor } from './executors/seed/executor.js';
6
+ export { default as migrationCreateExecutor } from './executors/migration-create/executor.js';
7
+ export { default as migrationShowExecutor } from './executors/migration-show/executor.js';
8
+ export { default as schemaSyncExecutor } from './executors/schema-sync/executor.js';
9
+ export { default as schemaLogExecutor } from './executors/schema-log/executor.js';
10
+ export { default as schemaDropExecutor } from './executors/schema-drop/executor.js';
11
+ export { default as queryExecutor } from './executors/query/executor.js';
12
+ export { default as cacheClearExecutor } from './executors/cache-clear/executor.js';
13
+ export { default as entityCreateExecutor } from './executors/entity-create/executor.js';
14
+ export { default as subscriberCreateExecutor } from './executors/subscriber-create/executor.js';
15
+ export { default as versionExecutor } from './executors/version/executor.js';
16
+ export { default as initExecutor } from './executors/init/executor.js';
17
+ export { default as initGenerator } from './generators/init/generator.js';
6
18
  export { default as bootstrapGenerator } from './generators/bootstrap/generator.js';
19
+ export { default } from './plugin/index.js';
package/dist/index.json CHANGED
@@ -19,9 +19,57 @@
19
19
  "seed": {
20
20
  "implementation": "./executors/seed/executor",
21
21
  "schema": "./executors/seed/schema.json"
22
+ },
23
+ "migration-create": {
24
+ "implementation": "./executors/migration-create/executor",
25
+ "schema": "./executors/migration-create/schema.json"
26
+ },
27
+ "migration-show": {
28
+ "implementation": "./executors/migration-show/executor",
29
+ "schema": "./executors/migration-show/schema.json"
30
+ },
31
+ "schema-sync": {
32
+ "implementation": "./executors/schema-sync/executor",
33
+ "schema": "./executors/schema-sync/schema.json"
34
+ },
35
+ "schema-log": {
36
+ "implementation": "./executors/schema-log/executor",
37
+ "schema": "./executors/schema-log/schema.json"
38
+ },
39
+ "schema-drop": {
40
+ "implementation": "./executors/schema-drop/executor",
41
+ "schema": "./executors/schema-drop/schema.json"
42
+ },
43
+ "query": {
44
+ "implementation": "./executors/query/executor",
45
+ "schema": "./executors/query/schema.json"
46
+ },
47
+ "cache-clear": {
48
+ "implementation": "./executors/cache-clear/executor",
49
+ "schema": "./executors/cache-clear/schema.json"
50
+ },
51
+ "entity-create": {
52
+ "implementation": "./executors/entity-create/executor",
53
+ "schema": "./executors/entity-create/schema.json"
54
+ },
55
+ "subscriber-create": {
56
+ "implementation": "./executors/subscriber-create/executor",
57
+ "schema": "./executors/subscriber-create/schema.json"
58
+ },
59
+ "version": {
60
+ "implementation": "./executors/version/executor",
61
+ "schema": "./executors/version/schema.json"
62
+ },
63
+ "init": {
64
+ "implementation": "./executors/init/executor",
65
+ "schema": "./executors/init/schema.json"
22
66
  }
23
67
  },
24
68
  "generators": {
69
+ "init": {
70
+ "implementation": "./generators/init/generator",
71
+ "schema": "./generators/init/schema.json"
72
+ },
25
73
  "bootstrap": {
26
74
  "implementation": "./generators/bootstrap/generator",
27
75
  "schema": "./generators/bootstrap/schema.json"
@@ -0,0 +1,9 @@
1
+ import type { CreateNodesV2 } from '@nx/devkit';
2
+ export declare const createNodesV2: CreateNodesV2;
3
+ export declare const name = "@anarchitects/nx-typeorm/inference";
4
+ declare const _default: {
5
+ name: string;
6
+ createNodesV2: CreateNodesV2;
7
+ };
8
+ export default _default;
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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;AAwTpB,eAAO,MAAM,aAAa,EAAE,aAgB3B,CAAC;AAEF,eAAO,MAAM,IAAI,uCAAuC,CAAC;;;;;AAEzD,wBAGE"}
@@ -0,0 +1,241 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { dirname, join } from 'node:path';
3
+ import { createNodesFromFiles } from '@nx/devkit';
4
+ import { defaultMigrationsDirectory, schemaFilePath, schemaNameFrom, } from '../utils/shared.js';
5
+ const FILE_PATTERN = '**/{project.json,app.module.ts,data-source.ts,data-source.js,typeorm.datasource.ts,typeorm.datasource.js,datasource.migrations.ts,datasource.migrations.js,schema.ts}';
6
+ const APP_DATASOURCE_CANDIDATES = [
7
+ 'tools/typeorm/datasource.migrations.ts',
8
+ 'tools/typeorm/datasource.migrations.js',
9
+ 'src/data-source.ts',
10
+ 'src/data-source.js',
11
+ 'src/typeorm.datasource.ts',
12
+ 'src/typeorm.datasource.js',
13
+ ];
14
+ const LIB_DATASOURCE_CANDIDATES = [
15
+ 'src/data-source.ts',
16
+ 'src/data-source.js',
17
+ 'src/typeorm.datasource.ts',
18
+ 'src/typeorm.datasource.js',
19
+ 'tools/typeorm/datasource.migrations.ts',
20
+ 'tools/typeorm/datasource.migrations.js',
21
+ ];
22
+ function isRelevantFile(file) {
23
+ return (file.endsWith('/project.json') ||
24
+ file.endsWith('/src/app.module.ts') ||
25
+ file.endsWith('/src/data-source.ts') ||
26
+ file.endsWith('/src/data-source.js') ||
27
+ file.endsWith('/src/typeorm.datasource.ts') ||
28
+ file.endsWith('/src/typeorm.datasource.js') ||
29
+ file.endsWith('/tools/typeorm/datasource.migrations.ts') ||
30
+ file.endsWith('/tools/typeorm/datasource.migrations.js') ||
31
+ file.endsWith('/src/infrastructure-persistence/schema.ts'));
32
+ }
33
+ function projectRootOf(file) {
34
+ const normalized = file.replace(/\\/g, '/');
35
+ if (normalized.endsWith('/project.json')) {
36
+ return dirname(normalized);
37
+ }
38
+ const toolsIndex = normalized.indexOf('/tools/');
39
+ if (toolsIndex !== -1) {
40
+ return normalized.substring(0, toolsIndex);
41
+ }
42
+ const srcIndex = normalized.indexOf('/src/');
43
+ if (srcIndex !== -1) {
44
+ return normalized.substring(0, srcIndex);
45
+ }
46
+ return dirname(normalized);
47
+ }
48
+ function detectProjectType(file) {
49
+ if (file.endsWith('/src/infrastructure-persistence/schema.ts')) {
50
+ return 'library';
51
+ }
52
+ return 'application';
53
+ }
54
+ function inferDataSource(workspaceRoot, projectRoot, type) {
55
+ const candidates = type === 'application'
56
+ ? APP_DATASOURCE_CANDIDATES
57
+ : LIB_DATASOURCE_CANDIDATES;
58
+ for (const candidate of candidates) {
59
+ const workspaceCandidate = join(workspaceRoot, projectRoot, candidate);
60
+ if (existsSync(workspaceCandidate)) {
61
+ return join(projectRoot, candidate);
62
+ }
63
+ }
64
+ return undefined;
65
+ }
66
+ function inferSchema(workspaceRoot, projectRoot, schemaPath) {
67
+ const file = schemaPath
68
+ ? join(workspaceRoot, projectRoot, schemaPath)
69
+ : schemaFilePath(join(workspaceRoot, projectRoot));
70
+ if (!existsSync(file)) {
71
+ return undefined;
72
+ }
73
+ const contents = readFileSync(file, 'utf-8');
74
+ return schemaNameFrom(contents);
75
+ }
76
+ function readTypeormMetadata(workspaceRoot, projectRoot) {
77
+ const projectJsonPath = join(workspaceRoot, projectRoot, 'project.json');
78
+ if (!existsSync(projectJsonPath)) {
79
+ return {};
80
+ }
81
+ try {
82
+ const projectJson = JSON.parse(readFileSync(projectJsonPath, 'utf-8'));
83
+ const typeormMetadata = projectJson.metadata?.typeorm;
84
+ const migrationsDir = toRelativePath(typeormMetadata?.migrationsDir);
85
+ return {
86
+ projectType: projectJson.projectType,
87
+ schema: typeormMetadata?.schema,
88
+ schemaPath: toRelativePath(typeormMetadata?.schemaPath),
89
+ migrationsDirectory: migrationsDir
90
+ ? join(projectRoot, migrationsDir)
91
+ : undefined,
92
+ };
93
+ }
94
+ catch {
95
+ return {};
96
+ }
97
+ }
98
+ function toRelativePath(pathValue) {
99
+ if (!pathValue) {
100
+ return undefined;
101
+ }
102
+ const normalized = pathValue.replace(/\\/g, '/').replace(/^\.\/+/, '');
103
+ if (!normalized || normalized.startsWith('/')) {
104
+ return undefined;
105
+ }
106
+ return normalized;
107
+ }
108
+ function cloneTarget(target) {
109
+ return {
110
+ ...target,
111
+ options: target.options ? { ...target.options } : undefined,
112
+ inputs: target.inputs ? [...target.inputs] : undefined,
113
+ outputs: target.outputs ? [...target.outputs] : undefined,
114
+ };
115
+ }
116
+ function createTargets(projectRoot, type, facts) {
117
+ if (!facts.dataSource && !facts.schema) {
118
+ return {};
119
+ }
120
+ const targets = {};
121
+ if (facts.dataSource) {
122
+ targets['db:migrate:generate'] = {
123
+ executor: '@anarchitects/nx-typeorm:generate',
124
+ options: {
125
+ projectRoot,
126
+ dataSource: facts.dataSource,
127
+ outputPath: facts.migrationsDirectory,
128
+ },
129
+ outputs: [facts.migrationsDirectory],
130
+ };
131
+ targets['db:migrate:run'] = {
132
+ executor: '@anarchitects/nx-typeorm:run',
133
+ options: {
134
+ projectRoot,
135
+ dataSource: facts.dataSource,
136
+ },
137
+ };
138
+ targets['db:migrate:revert'] = {
139
+ executor: '@anarchitects/nx-typeorm:revert',
140
+ options: {
141
+ projectRoot,
142
+ dataSource: facts.dataSource,
143
+ },
144
+ };
145
+ targets['db:migrate:show'] = {
146
+ executor: '@anarchitects/nx-typeorm:migration-show',
147
+ options: {
148
+ projectRoot,
149
+ dataSource: facts.dataSource,
150
+ },
151
+ };
152
+ targets['db:schema:sync'] = {
153
+ executor: '@anarchitects/nx-typeorm:schema-sync',
154
+ options: {
155
+ projectRoot,
156
+ dataSource: facts.dataSource,
157
+ },
158
+ };
159
+ targets['db:schema:log'] = {
160
+ executor: '@anarchitects/nx-typeorm:schema-log',
161
+ options: {
162
+ projectRoot,
163
+ dataSource: facts.dataSource,
164
+ },
165
+ };
166
+ targets['db:cache:clear'] = {
167
+ executor: '@anarchitects/nx-typeorm:cache-clear',
168
+ options: {
169
+ projectRoot,
170
+ dataSource: facts.dataSource,
171
+ },
172
+ };
173
+ targets['typeorm:generate'] = cloneTarget(targets['db:migrate:generate']);
174
+ targets['typeorm:run'] = cloneTarget(targets['db:migrate:run']);
175
+ targets['typeorm:revert'] = cloneTarget(targets['db:migrate:revert']);
176
+ targets['typeorm:show'] = cloneTarget(targets['db:migrate:show']);
177
+ targets['typeorm:schema:sync'] = cloneTarget(targets['db:schema:sync']);
178
+ targets['typeorm:schema:log'] = cloneTarget(targets['db:schema:log']);
179
+ targets['typeorm:cache:clear'] = cloneTarget(targets['db:cache:clear']);
180
+ }
181
+ targets['db:ensure-schema'] = {
182
+ executor: '@anarchitects/nx-typeorm:ensure-schema',
183
+ options: {
184
+ projectRoot,
185
+ schema: facts.schema,
186
+ dataSource: facts.dataSource,
187
+ },
188
+ };
189
+ targets['typeorm:ensure-schema'] = cloneTarget(targets['db:ensure-schema']);
190
+ if (type === 'application') {
191
+ targets['db:seed'] = {
192
+ executor: '@anarchitects/nx-typeorm:seed',
193
+ options: {
194
+ projectRoot,
195
+ file: 'tools/typeorm/seeds/index.ts',
196
+ },
197
+ };
198
+ targets['typeorm:seed'] = cloneTarget(targets['db:seed']);
199
+ }
200
+ return targets;
201
+ }
202
+ async function createNodesInternal(file, context) {
203
+ if (!isRelevantFile(file)) {
204
+ return { projects: {} };
205
+ }
206
+ const projectRoot = projectRootOf(file);
207
+ const metadata = readTypeormMetadata(context.workspaceRoot, projectRoot);
208
+ const projectType = metadata.projectType ?? detectProjectType(file);
209
+ const migrationsDirectory = metadata.migrationsDirectory ??
210
+ defaultMigrationsDirectory(projectRoot, projectType);
211
+ const dataSource = inferDataSource(context.workspaceRoot, projectRoot, projectType);
212
+ const schema = metadata.schema ??
213
+ inferSchema(context.workspaceRoot, projectRoot, metadata.schemaPath);
214
+ const targets = createTargets(projectRoot, projectType, {
215
+ migrationsDirectory,
216
+ dataSource,
217
+ schema,
218
+ });
219
+ if (Object.keys(targets).length === 0) {
220
+ return { projects: {} };
221
+ }
222
+ return {
223
+ projects: {
224
+ [projectRoot]: {
225
+ targets,
226
+ },
227
+ },
228
+ };
229
+ }
230
+ export const createNodesV2 = [
231
+ FILE_PATTERN,
232
+ async (configFiles, _options, context) => {
233
+ const results = await createNodesFromFiles((file) => createNodesInternal(file, context), configFiles, {}, context);
234
+ return results;
235
+ },
236
+ ];
237
+ export const name = '@anarchitects/nx-typeorm/inference';
238
+ export default {
239
+ name,
240
+ createNodesV2,
241
+ };
@@ -1,7 +1,2 @@
1
- import type { CreateNodesContextV2, CreateNodesResultV2 } from '@nx/devkit';
2
- declare const _default: {
3
- name: string;
4
- createNodesV2: (string | ((configFiles: readonly string[], _options: unknown, context: CreateNodesContextV2) => Promise<CreateNodesResultV2>))[];
5
- };
6
- export default _default;
1
+ export { createNodesV2, default } from '../plugin/index.js';
7
2
  //# sourceMappingURL=typeorm.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"typeorm.d.ts","sourceRoot":"","sources":["../../src/plugins/typeorm.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,oBAAoB,EAEpB,mBAAmB,EAEpB,MAAM,YAAY,CAAC;;;4CAoND,SAAS,MAAM,EAAE,YACpB,OAAO,WACR,oBAAoB;;AAPnC,wBAwBE"}
1
+ {"version":3,"file":"typeorm.d.ts","sourceRoot":"","sources":["../../src/plugins/typeorm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC"}
@@ -1,161 +1 @@
1
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
2
- import { dirname, join } from 'node:path';
3
- import { createNodesFromFiles } from '@nx/devkit';
4
- import { calculateHashesForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes.js';
5
- import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs.js';
6
- import { workspaceDataDirectory } from 'nx/src/utils/cache-directory.js';
7
- import { defaultMigrationsDirectory, schemaFilePath, schemaNameFrom, } from '../executors/shared.js';
8
- const FILE_PATTERN = '**/src/{app.module.ts,infrastructure-persistence/schema.ts}';
9
- const CACHE_PATH = join(workspaceDataDirectory, 'nx-typeorm-targets.json');
10
- function readCache() {
11
- try {
12
- return JSON.parse(readFileSync(CACHE_PATH, 'utf-8'));
13
- }
14
- catch {
15
- return {};
16
- }
17
- }
18
- function writeCache(data) {
19
- try {
20
- mkdirSync(dirname(CACHE_PATH), { recursive: true });
21
- writeFileSync(CACHE_PATH, `${JSON.stringify(data, null, 2)}\n`);
22
- }
23
- catch {
24
- // ignore cache write failures
25
- }
26
- }
27
- function projectRootOf(file) {
28
- const normalized = file.replace(/\\/g, '/');
29
- const marker = '/src/';
30
- const index = normalized.indexOf(marker);
31
- if (index === -1) {
32
- return dirname(normalized);
33
- }
34
- return normalized.substring(0, index);
35
- }
36
- function detectProjectType(file) {
37
- return file.endsWith('app.module.ts') ? 'application' : 'library';
38
- }
39
- function inferDataSource(workspaceRoot, projectRoot, type) {
40
- const candidates = [
41
- join(projectRoot, 'tools/typeorm/datasource.migrations.ts'),
42
- join(projectRoot, 'tools/typeorm/datasource.migrations.js'),
43
- join(projectRoot, 'src/typeorm.datasource.ts'),
44
- join(projectRoot, 'src/typeorm.datasource.js'),
45
- ];
46
- for (const candidate of candidates) {
47
- if (existsSync(join(workspaceRoot, candidate))) {
48
- return candidate;
49
- }
50
- }
51
- return type === 'application'
52
- ? join(projectRoot, 'tools/typeorm/datasource.migrations.ts')
53
- : join(projectRoot, 'src/typeorm.datasource.ts');
54
- }
55
- function inferSchema(workspaceRoot, projectRoot) {
56
- const file = schemaFilePath(join(workspaceRoot, projectRoot));
57
- if (!existsSync(file)) {
58
- return undefined;
59
- }
60
- const contents = readFileSync(file, 'utf-8');
61
- return schemaNameFrom(contents);
62
- }
63
- function createTargets(projectRoot, type, context, facts) {
64
- const namedInputs = getNamedInputs(projectRoot, context);
65
- const defaultInputs = namedInputs?.default
66
- ? ['default', '^default']
67
- : undefined;
68
- const targets = {
69
- 'db:migrate:generate': {
70
- executor: '@anarchitects/nx-typeorm:generate',
71
- options: {
72
- projectRoot,
73
- dataSource: facts.dataSource,
74
- outputPath: facts.migrationsDirectory,
75
- },
76
- outputs: [facts.migrationsDirectory],
77
- },
78
- 'db:migrate:run': {
79
- executor: '@anarchitects/nx-typeorm:run',
80
- options: {
81
- projectRoot,
82
- dataSource: facts.dataSource,
83
- },
84
- },
85
- 'db:migrate:revert': {
86
- executor: '@anarchitects/nx-typeorm:revert',
87
- options: {
88
- projectRoot,
89
- dataSource: facts.dataSource,
90
- },
91
- },
92
- 'db:ensure-schema': {
93
- executor: '@anarchitects/nx-typeorm:ensure-schema',
94
- options: {
95
- projectRoot,
96
- schema: facts.schema,
97
- },
98
- },
99
- };
100
- if (type === 'application') {
101
- targets['db:seed'] = {
102
- executor: '@anarchitects/nx-typeorm:seed',
103
- options: {
104
- projectRoot,
105
- file: 'tools/typeorm/seeds/index.ts',
106
- },
107
- };
108
- }
109
- if (defaultInputs) {
110
- for (const target of Object.values(targets)) {
111
- target.inputs = defaultInputs;
112
- }
113
- }
114
- return targets;
115
- }
116
- async function createNodesInternal(file, context, cache) {
117
- const projectRoot = projectRootOf(file);
118
- const projectType = detectProjectType(file);
119
- const migrationsDirectory = defaultMigrationsDirectory(projectRoot, projectType);
120
- const dataSource = inferDataSource(context.workspaceRoot, projectRoot, projectType);
121
- const schema = inferSchema(context.workspaceRoot, projectRoot);
122
- const candidateInputs = new Set([
123
- file,
124
- dataSource,
125
- migrationsDirectory,
126
- join(projectRoot, 'tools/typeorm/migrations/**/*'),
127
- join(projectRoot, 'tools/typeorm/seeds/**/*'),
128
- ]);
129
- const additionalInputs = Array.from(candidateInputs).filter((entry) => {
130
- if (entry.includes('*')) {
131
- return true;
132
- }
133
- return existsSync(join(context.workspaceRoot, entry));
134
- });
135
- const [hashKey] = await calculateHashesForCreateNodes([projectRoot], {}, context, [additionalInputs.length ? additionalInputs : [file]]);
136
- if (cache[hashKey]) {
137
- return { projects: { [projectRoot]: { targets: cache[hashKey] } } };
138
- }
139
- const targets = createTargets(projectRoot, projectType, context, {
140
- migrationsDirectory,
141
- dataSource,
142
- schema,
143
- });
144
- cache[hashKey] = targets;
145
- return { projects: { [projectRoot]: { targets } } };
146
- }
147
- export default {
148
- name: '@anarchitects/nx-typeorm/inference',
149
- createNodesV2: [
150
- FILE_PATTERN,
151
- async (configFiles, _options, context) => {
152
- if (configFiles.length === 0) {
153
- return [];
154
- }
155
- const cache = readCache();
156
- const results = await createNodesFromFiles((file) => createNodesInternal(file, context, cache), configFiles, {}, context);
157
- writeCache(cache);
158
- return results;
159
- },
160
- ],
161
- };
1
+ export { createNodesV2, default } from '../plugin/index.js';
@@ -0,0 +1,17 @@
1
+ import type { ExecutorContext } from '@nx/devkit';
2
+ type CliOptions = {
3
+ args?: string[];
4
+ };
5
+ type DataSourceCliOptions = CliOptions & {
6
+ projectRoot?: string;
7
+ dataSource?: string;
8
+ moduleSystem?: 'auto' | 'commonjs' | 'esm';
9
+ };
10
+ export interface DataSourceCommandArgs {
11
+ beforeDataSourceArgs?: string[];
12
+ afterDataSourceArgs?: string[];
13
+ }
14
+ export declare function runTypeormCommand(subcommand: string, options: CliOptions, context: ExecutorContext, commandArgs?: string[]): Promise<number>;
15
+ export declare function runDataSourceTypeormCommand(subcommand: string, options: DataSourceCliOptions, context: ExecutorContext, commandArgsOrConfig?: string[] | DataSourceCommandArgs): Promise<number>;
16
+ export {};
17
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../../src/utils/cli.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAKlD,KAAK,UAAU,GAAG;IAChB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;CACjB,CAAC;AAEF,KAAK,oBAAoB,GAAG,UAAU,GAAG;IACvC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,GAAG,UAAU,GAAG,KAAK,CAAC;CAC5C,CAAC;AAEF,MAAM,WAAW,qBAAqB;IACpC,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAC;IAChC,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAC;CAChC;AAqBD,wBAAsB,iBAAiB,CACrC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,UAAU,EACnB,OAAO,EAAE,eAAe,EACxB,WAAW,GAAE,MAAM,EAAO,GACzB,OAAO,CAAC,MAAM,CAAC,CAWjB;AAED,wBAAsB,2BAA2B,CAC/C,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,oBAAoB,EAC7B,OAAO,EAAE,eAAe,EACxB,mBAAmB,GAAE,MAAM,EAAE,GAAG,qBAA0B,GACzD,OAAO,CAAC,MAAM,CAAC,CA6BjB"}
@@ -0,0 +1,55 @@
1
+ import { relative } from 'node:path';
2
+ import { getPackageManagerCommand } from '@nx/devkit';
3
+ import { ensureProjectRoot, resolveTypeormCliRunner } from './shared.js';
4
+ import { spawn } from './spawn.js';
5
+ function splitCommand(command) {
6
+ const [cmd, ...rest] = command.split(' ');
7
+ return [cmd, rest];
8
+ }
9
+ function relativeToWorkspace(root, filePath) {
10
+ return relative(root, filePath) || '.';
11
+ }
12
+ function ensureArgs(options) {
13
+ return options?.filter((arg) => arg.trim().length > 0) ?? [];
14
+ }
15
+ function packageManagerCommand() {
16
+ const pmc = getPackageManagerCommand();
17
+ const execCommand = pmc.exec ?? pmc.dlx ?? 'npx';
18
+ return splitCommand(execCommand);
19
+ }
20
+ export async function runTypeormCommand(subcommand, options, context, commandArgs = []) {
21
+ const [command, baseArgs] = packageManagerCommand();
22
+ const args = [
23
+ ...baseArgs,
24
+ 'typeorm',
25
+ subcommand,
26
+ ...commandArgs,
27
+ ...ensureArgs(options.args),
28
+ ];
29
+ return spawn(command, args, { cwd: context.root });
30
+ }
31
+ export async function runDataSourceTypeormCommand(subcommand, options, context, commandArgsOrConfig = []) {
32
+ const commandArgs = Array.isArray(commandArgsOrConfig)
33
+ ? {
34
+ beforeDataSourceArgs: commandArgsOrConfig,
35
+ afterDataSourceArgs: [],
36
+ }
37
+ : {
38
+ beforeDataSourceArgs: commandArgsOrConfig.beforeDataSourceArgs ?? [],
39
+ afterDataSourceArgs: commandArgsOrConfig.afterDataSourceArgs ?? [],
40
+ };
41
+ const paths = ensureProjectRoot(options, context);
42
+ const runner = resolveTypeormCliRunner(options, context, paths.absoluteProjectRoot);
43
+ const [command, baseArgs] = packageManagerCommand();
44
+ const args = [
45
+ ...baseArgs,
46
+ runner,
47
+ subcommand,
48
+ ...commandArgs.beforeDataSourceArgs,
49
+ '-d',
50
+ relativeToWorkspace(context.root, paths.dataSource),
51
+ ...commandArgs.afterDataSourceArgs,
52
+ ...ensureArgs(options.args),
53
+ ];
54
+ return spawn(command, args, { cwd: context.root });
55
+ }