@anarchitects/nx-typeorm 0.0.3 → 0.2.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 +203 -43
- package/dist/executors/cache-clear/executor.d.ts +6 -0
- package/dist/executors/cache-clear/executor.d.ts.map +1 -0
- package/dist/executors/cache-clear/executor.js +5 -0
- package/dist/executors/cache-clear/schema.json +28 -0
- package/dist/executors/ensure-schema/executor.d.ts +1 -5
- package/dist/executors/ensure-schema/executor.d.ts.map +1 -1
- package/dist/executors/ensure-schema/executor.js +1 -1
- package/dist/executors/entity-create/executor.d.ts +6 -0
- package/dist/executors/entity-create/executor.d.ts.map +1 -0
- package/dist/executors/entity-create/executor.js +15 -0
- package/dist/executors/entity-create/schema.json +23 -0
- package/dist/executors/generate/executor.d.ts +1 -7
- package/dist/executors/generate/executor.d.ts.map +1 -1
- package/dist/executors/generate/executor.js +12 -24
- package/dist/executors/generate/schema.json +11 -1
- package/dist/executors/init/executor.d.ts +6 -0
- package/dist/executors/init/executor.d.ts.map +1 -0
- package/dist/executors/init/executor.js +24 -0
- package/dist/executors/init/schema.json +40 -0
- package/dist/executors/migration-create/executor.d.ts +6 -0
- package/dist/executors/migration-create/executor.d.ts.map +1 -0
- package/dist/executors/migration-create/executor.js +19 -0
- package/dist/executors/migration-create/schema.json +31 -0
- package/dist/executors/migration-show/executor.d.ts +6 -0
- package/dist/executors/migration-show/executor.d.ts.map +1 -0
- package/dist/executors/migration-show/executor.js +5 -0
- package/dist/executors/migration-show/schema.json +28 -0
- package/dist/executors/query/executor.d.ts +6 -0
- package/dist/executors/query/executor.d.ts.map +1 -0
- package/dist/executors/query/executor.js +9 -0
- package/dist/executors/query/schema.json +33 -0
- package/dist/executors/revert/executor.d.ts +1 -5
- package/dist/executors/revert/executor.d.ts.map +1 -1
- package/dist/executors/revert/executor.js +8 -19
- package/dist/executors/revert/schema.json +7 -1
- package/dist/executors/run/executor.d.ts +1 -6
- package/dist/executors/run/executor.d.ts.map +1 -1
- package/dist/executors/run/executor.js +7 -18
- package/dist/executors/run/schema.json +6 -0
- package/dist/executors/schema-drop/executor.d.ts +6 -0
- package/dist/executors/schema-drop/executor.d.ts.map +1 -0
- package/dist/executors/schema-drop/executor.js +5 -0
- package/dist/executors/schema-drop/schema.json +28 -0
- package/dist/executors/schema-log/executor.d.ts +6 -0
- package/dist/executors/schema-log/executor.d.ts.map +1 -0
- package/dist/executors/schema-log/executor.js +5 -0
- package/dist/executors/schema-log/schema.json +28 -0
- package/dist/executors/schema-sync/executor.d.ts +6 -0
- package/dist/executors/schema-sync/executor.d.ts.map +1 -0
- package/dist/executors/schema-sync/executor.js +5 -0
- package/dist/executors/schema-sync/schema.json +28 -0
- package/dist/executors/seed/executor.d.ts +1 -7
- package/dist/executors/seed/executor.d.ts.map +1 -1
- package/dist/executors/seed/executor.js +1 -1
- package/dist/executors/subscriber-create/executor.d.ts +6 -0
- package/dist/executors/subscriber-create/executor.d.ts.map +1 -0
- package/dist/executors/subscriber-create/executor.js +13 -0
- package/dist/executors/subscriber-create/schema.json +23 -0
- package/dist/executors/version/executor.d.ts +6 -0
- package/dist/executors/version/executor.d.ts.map +1 -0
- package/dist/executors/version/executor.js +5 -0
- package/dist/executors/version/schema.json +14 -0
- package/dist/generators/bootstrap/files/app/env.example__tmpl__ +55 -1
- package/dist/generators/bootstrap/files/app/src/data-source.ts__tmpl__ +6 -0
- package/dist/generators/bootstrap/files/app/src/typeorm.datasource.ts__tmpl__ +1 -15
- package/dist/generators/bootstrap/files/app/tools/typeorm/connection-options.ts__tmpl__ +292 -0
- package/dist/generators/bootstrap/files/app/tools/typeorm/datasource.migrations.ts__tmpl__ +2 -7
- package/dist/generators/bootstrap/files/lib/project.json.partial__tmpl__ +3 -1
- package/dist/generators/bootstrap/generator.d.ts +5 -1
- package/dist/generators/bootstrap/generator.d.ts.map +1 -1
- package/dist/generators/bootstrap/generator.js +179 -31
- package/dist/generators/bootstrap/schema.json +18 -1
- package/dist/generators/entity-create/generator.d.ts +4 -0
- package/dist/generators/entity-create/generator.d.ts.map +1 -0
- package/dist/generators/entity-create/generator.js +70 -0
- package/dist/generators/entity-create/schema.json +28 -0
- package/dist/generators/init/generator.d.ts +4 -0
- package/dist/generators/init/generator.d.ts.map +1 -0
- package/dist/generators/init/generator.js +31 -0
- package/dist/generators/init/schema.json +18 -0
- package/dist/generators/migration-create/generator.d.ts +4 -0
- package/dist/generators/migration-create/generator.d.ts.map +1 -0
- package/dist/generators/migration-create/generator.js +72 -0
- package/dist/generators/migration-create/schema.json +40 -0
- package/dist/generators/subscriber-create/generator.d.ts +4 -0
- package/dist/generators/subscriber-create/generator.d.ts.map +1 -0
- package/dist/generators/subscriber-create/generator.js +70 -0
- package/dist/generators/subscriber-create/schema.json +28 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -0
- package/dist/index.json +72 -0
- package/dist/plugin/index.d.ts +9 -0
- package/dist/plugin/index.d.ts.map +1 -0
- package/dist/plugin/index.js +241 -0
- package/dist/plugins/typeorm.d.ts +1 -6
- package/dist/plugins/typeorm.d.ts.map +1 -1
- package/dist/plugins/typeorm.js +1 -161
- package/dist/utils/cli.d.ts +17 -0
- package/dist/utils/cli.d.ts.map +1 -0
- package/dist/utils/cli.js +55 -0
- package/dist/{executors → utils}/shared.d.ts +12 -9
- package/dist/utils/shared.d.ts.map +1 -0
- package/dist/utils/shared.js +260 -0
- package/package.json +6 -7
- package/dist/executors/shared.d.ts.map +0 -1
- package/dist/executors/shared.js +0 -71
- package/dist/generators/bootstrap/files/app/tools/typeorm/datasource.runtime.ts__tmpl__ +0 -9
|
@@ -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
|
-
|
|
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":"
|
|
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"}
|
package/dist/plugins/typeorm.js
CHANGED
|
@@ -1,161 +1 @@
|
|
|
1
|
-
|
|
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
|
+
}
|
|
@@ -1,25 +1,28 @@
|
|
|
1
1
|
import type { ExecutorContext } from '@nx/devkit';
|
|
2
|
-
|
|
2
|
+
type ModuleSystem = 'auto' | 'commonjs' | 'esm';
|
|
3
|
+
type TypeormCliRunner = 'typeorm-ts-node-commonjs' | 'typeorm-ts-node-esm';
|
|
4
|
+
type ProjectRootDataSourceOptions = {
|
|
3
5
|
projectRoot?: string;
|
|
4
6
|
dataSource?: string;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
};
|
|
8
|
+
type ModuleSystemOptions = {
|
|
9
|
+
moduleSystem?: ModuleSystem;
|
|
10
|
+
};
|
|
11
|
+
type ResolvedProjectPaths = {
|
|
8
12
|
projectRoot: string;
|
|
9
13
|
absoluteProjectRoot: string;
|
|
10
14
|
dataSource: string;
|
|
11
|
-
}
|
|
12
|
-
export declare function ensureProjectRoot(options:
|
|
15
|
+
};
|
|
16
|
+
export declare function ensureProjectRoot(options: ProjectRootDataSourceOptions, context: ExecutorContext): ResolvedProjectPaths;
|
|
17
|
+
export declare function resolveTypeormCliRunner(options: ModuleSystemOptions, context: ExecutorContext, absoluteProjectRoot: string): TypeormCliRunner;
|
|
13
18
|
export declare function resolveProjectRoot(configuredRoot: string | undefined, context: ExecutorContext): {
|
|
14
19
|
projectRoot: string;
|
|
15
20
|
absoluteProjectRoot: string;
|
|
16
21
|
};
|
|
17
|
-
export declare function splitCommand(command: string): [string, string[]];
|
|
18
|
-
export declare function relativeToWorkspace(root: string, filePath: string): string;
|
|
19
22
|
export declare function defaultMigrationsDirectory(projectRoot: string, projectType: 'application' | 'library' | undefined): string;
|
|
20
|
-
export declare function ensureArgs(options?: string[]): string[];
|
|
21
23
|
export declare function projectTypeFor(context: ExecutorContext): 'application' | 'library' | undefined;
|
|
22
24
|
export declare function schemaFilePath(projectRoot: string): string;
|
|
23
25
|
export declare function schemaNameFrom(contents: string): string | undefined;
|
|
24
26
|
export declare function normalizeName(name: string): string;
|
|
27
|
+
export {};
|
|
25
28
|
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../src/utils/shared.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAwB,MAAM,YAAY,CAAC;AAExE,KAAK,YAAY,GAAG,MAAM,GAAG,UAAU,GAAG,KAAK,CAAC;AAChD,KAAK,gBAAgB,GAAG,0BAA0B,GAAG,qBAAqB,CAAC;AAE3E,KAAK,4BAA4B,GAAG;IAClC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,KAAK,mBAAmB,GAAG;IACzB,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B,CAAC;AAEF,KAAK,oBAAoB,GAAG;IAC1B,WAAW,EAAE,MAAM,CAAC;IACpB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AA0BF,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,4BAA4B,EACrC,OAAO,EAAE,eAAe,GACvB,oBAAoB,CAStB;AAED,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,mBAAmB,EAC5B,OAAO,EAAE,eAAe,EACxB,mBAAmB,EAAE,MAAM,GAC1B,gBAAgB,CAWlB;AAED,wBAAgB,kBAAkB,CAChC,cAAc,EAAE,MAAM,GAAG,SAAS,EAClC,OAAO,EAAE,eAAe,GACvB;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,mBAAmB,EAAE,MAAM,CAAA;CAAE,CActD;AA4SD,wBAAgB,0BAA0B,CACxC,WAAW,EAAE,MAAM,EACnB,WAAW,EAAE,aAAa,GAAG,SAAS,GAAG,SAAS,GACjD,MAAM,CAMR;AAED,wBAAgB,cAAc,CAC5B,OAAO,EAAE,eAAe,GACvB,aAAa,GAAG,SAAS,GAAG,SAAS,CAKvC;AAED,wBAAgB,cAAc,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAE1D;AAED,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAGnE;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD"}
|