@anarchitects/nx-typeorm 0.0.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.
- package/README.md +84 -0
- package/dist/executors/ensure-schema/executor.d.ts +10 -0
- package/dist/executors/ensure-schema/executor.d.ts.map +1 -0
- package/dist/executors/ensure-schema/executor.js +77 -0
- package/dist/executors/ensure-schema/schema.json +23 -0
- package/dist/executors/generate/executor.d.ts +12 -0
- package/dist/executors/generate/executor.d.ts.map +1 -0
- package/dist/executors/generate/executor.js +39 -0
- package/dist/executors/generate/schema.json +40 -0
- package/dist/executors/revert/executor.d.ts +10 -0
- package/dist/executors/revert/executor.d.ts.map +1 -0
- package/dist/executors/revert/executor.js +22 -0
- package/dist/executors/revert/schema.json +27 -0
- package/dist/executors/run/executor.d.ts +11 -0
- package/dist/executors/run/executor.d.ts.map +1 -0
- package/dist/executors/run/executor.js +25 -0
- package/dist/executors/run/schema.json +31 -0
- package/dist/executors/seed/executor.d.ts +12 -0
- package/dist/executors/seed/executor.d.ts.map +1 -0
- package/dist/executors/seed/executor.js +44 -0
- package/dist/executors/seed/schema.json +31 -0
- package/dist/executors/shared.d.ts +25 -0
- package/dist/executors/shared.d.ts.map +1 -0
- package/dist/executors/shared.js +71 -0
- package/dist/generators/bootstrap/files/app/docker-compose.yml__tmpl__ +19 -0
- package/dist/generators/bootstrap/files/app/env.example__tmpl__ +1 -0
- package/dist/generators/bootstrap/files/app/src/typeorm.datasource.ts__tmpl__ +15 -0
- package/dist/generators/bootstrap/files/app/tools/typeorm/datasource.migrations.ts__tmpl__ +9 -0
- package/dist/generators/bootstrap/files/app/tools/typeorm/datasource.runtime.ts__tmpl__ +9 -0
- package/dist/generators/bootstrap/files/app/tools/typeorm/migrations/.gitkeep__tmpl__ +0 -0
- package/dist/generators/bootstrap/files/app/tools/typeorm/seeds/index.ts__tmpl__ +3 -0
- package/dist/generators/bootstrap/files/lib/project.json.partial__tmpl__ +9 -0
- package/dist/generators/bootstrap/files/lib/src/infrastructure-persistence/migrations/1700000000000_init_schema.ts__tmpl__ +12 -0
- package/dist/generators/bootstrap/files/lib/src/infrastructure-persistence/schema.ts__tmpl__ +1 -0
- package/dist/generators/bootstrap/generator.d.ts +12 -0
- package/dist/generators/bootstrap/generator.d.ts.map +1 -0
- package/dist/generators/bootstrap/generator.js +167 -0
- package/dist/generators/bootstrap/schema.json +37 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.json +30 -0
- package/dist/plugins/typeorm.d.ts +7 -0
- package/dist/plugins/typeorm.d.ts.map +1 -0
- package/dist/plugins/typeorm.js +161 -0
- package/dist/utils/spawn.d.ts +6 -0
- package/dist/utils/spawn.d.ts.map +1 -0
- package/dist/utils/spawn.js +11 -0
- package/package.json +81 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DataSource } from 'typeorm';
|
|
2
|
+
|
|
3
|
+
export function makeRuntimeDataSource(): DataSource {
|
|
4
|
+
const url = process.env.DATABASE_URL;
|
|
5
|
+
if (!url) {
|
|
6
|
+
throw new Error('Missing DATABASE_URL');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
return new DataSource({
|
|
10
|
+
type: '<%= database %>',
|
|
11
|
+
url,
|
|
12
|
+
synchronize: false,
|
|
13
|
+
logging: false,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { MigrationInterface, QueryRunner } from 'typeorm';
|
|
2
|
+
import { <%= className %>_SCHEMA as SCHEMA } from '../schema';
|
|
3
|
+
|
|
4
|
+
export class Init<%= className %>Schema1700000000000 implements MigrationInterface {
|
|
5
|
+
async up(q: QueryRunner): Promise<void> {
|
|
6
|
+
await q.query(`CREATE SCHEMA IF NOT EXISTS "${SCHEMA}"`);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async down(): Promise<void> {
|
|
10
|
+
// Schema intentionally kept
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const <%= className %>_SCHEMA = '<%= schema %>';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Tree, type GeneratorCallback } from '@nx/devkit';
|
|
2
|
+
interface BootstrapGeneratorSchema {
|
|
3
|
+
project: string;
|
|
4
|
+
domain?: string;
|
|
5
|
+
schema?: string;
|
|
6
|
+
db?: string;
|
|
7
|
+
withCompose?: boolean;
|
|
8
|
+
skipInstall?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export default function bootstrapGenerator(tree: Tree, options: BootstrapGeneratorSchema): Promise<GeneratorCallback>;
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=generator.d.ts.map
|
|
@@ -0,0 +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;AAiBpB,UAAU,wBAAwB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAMD,wBAA8B,kBAAkB,CAC9C,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,wBAAwB,8BAgClC"}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { addDependenciesToPackageJson, formatFiles, generateFiles, joinPathFragments, names, readProjectConfiguration, runTasksInSerial, updateJson, } from '@nx/devkit';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { Node, Project, SyntaxKind, } from 'ts-morph';
|
|
5
|
+
const generatorDir = dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
const DEFAULT_DATABASE = 'postgres';
|
|
7
|
+
const runtimeImportPath = './typeorm.datasource';
|
|
8
|
+
export default async function bootstrapGenerator(tree, options) {
|
|
9
|
+
const project = readProjectConfiguration(tree, options.project);
|
|
10
|
+
const tasks = [];
|
|
11
|
+
const dependencyTask = addDependenciesToPackageJson(tree, {
|
|
12
|
+
typeorm: '^0.3.20',
|
|
13
|
+
pg: '^8.11.0',
|
|
14
|
+
'reflect-metadata': '^0.1.13',
|
|
15
|
+
'@nestjs/typeorm': '^10.0.0',
|
|
16
|
+
}, {
|
|
17
|
+
'ts-node': '^10.9.2',
|
|
18
|
+
'typeorm-ts-node-commonjs': '^0.0.8',
|
|
19
|
+
});
|
|
20
|
+
if (!options.skipInstall) {
|
|
21
|
+
tasks.push(dependencyTask);
|
|
22
|
+
}
|
|
23
|
+
if (project.projectType === 'library') {
|
|
24
|
+
prepareLibrary(tree, project.root, options);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
prepareApplication(tree, project.root, project.sourceRoot, options);
|
|
28
|
+
}
|
|
29
|
+
await formatFiles(tree);
|
|
30
|
+
return tasks.length ? runTasksInSerial(...tasks) : () => undefined;
|
|
31
|
+
}
|
|
32
|
+
function prepareLibrary(tree, projectRoot, options) {
|
|
33
|
+
if (!options.domain) {
|
|
34
|
+
throw new Error('Domain option is required when targeting a library.');
|
|
35
|
+
}
|
|
36
|
+
const schemaName = (options.schema ?? options.domain).toLowerCase();
|
|
37
|
+
const templateOptions = {
|
|
38
|
+
tmpl: '',
|
|
39
|
+
schema: schemaName,
|
|
40
|
+
domain: options.domain,
|
|
41
|
+
...names(options.domain),
|
|
42
|
+
};
|
|
43
|
+
generateFiles(tree, join(generatorDir, 'files', 'lib'), projectRoot, templateOptions);
|
|
44
|
+
const tempPartialPath = joinPathFragments(projectRoot, 'project.json.partial');
|
|
45
|
+
if (tree.exists(tempPartialPath)) {
|
|
46
|
+
tree.delete(tempPartialPath);
|
|
47
|
+
}
|
|
48
|
+
const projectJsonPath = joinPathFragments(projectRoot, 'project.json');
|
|
49
|
+
if (tree.exists(projectJsonPath)) {
|
|
50
|
+
updateJson(tree, projectJsonPath, (json) => {
|
|
51
|
+
json.metadata ??= {};
|
|
52
|
+
json.metadata.typeorm = {
|
|
53
|
+
schema: schemaName,
|
|
54
|
+
domain: options.domain,
|
|
55
|
+
};
|
|
56
|
+
return json;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function prepareApplication(tree, projectRoot, sourceRoot, options) {
|
|
61
|
+
const database = options.db ?? DEFAULT_DATABASE;
|
|
62
|
+
const projectName = names(options.project).fileName.replace(/-/g, '_');
|
|
63
|
+
generateFiles(tree, join(generatorDir, 'files', 'app'), projectRoot, {
|
|
64
|
+
tmpl: '',
|
|
65
|
+
database,
|
|
66
|
+
appDatabase: projectName,
|
|
67
|
+
});
|
|
68
|
+
if (!options.withCompose) {
|
|
69
|
+
const composePath = joinPathFragments(projectRoot, 'docker-compose.yml');
|
|
70
|
+
if (tree.exists(composePath)) {
|
|
71
|
+
tree.delete(composePath);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
patchAppModule(tree, projectRoot, sourceRoot);
|
|
75
|
+
}
|
|
76
|
+
function patchAppModule(tree, projectRoot, sourceRoot) {
|
|
77
|
+
const resolvedSourceRoot = sourceRoot ?? joinPathFragments(projectRoot, 'src');
|
|
78
|
+
const modulePath = joinPathFragments(resolvedSourceRoot, 'app.module.ts');
|
|
79
|
+
if (!tree.exists(modulePath)) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const sourceText = tree.read(modulePath, 'utf-8');
|
|
83
|
+
if (!sourceText) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const project = new Project({
|
|
87
|
+
useInMemoryFileSystem: true,
|
|
88
|
+
skipAddingFilesFromTsConfig: true,
|
|
89
|
+
});
|
|
90
|
+
const sourceFile = project.createSourceFile(modulePath, sourceText, {
|
|
91
|
+
overwrite: true,
|
|
92
|
+
});
|
|
93
|
+
ensureImport(sourceFile, '@nestjs/typeorm', 'TypeOrmModule');
|
|
94
|
+
ensureImport(sourceFile, runtimeImportPath, 'makeRuntimeDataSource');
|
|
95
|
+
const moduleDecorator = sourceFile
|
|
96
|
+
.getDescendantsOfKind(SyntaxKind.Decorator)
|
|
97
|
+
.find((decorator) => decorator.getName() === 'Module');
|
|
98
|
+
if (!moduleDecorator) {
|
|
99
|
+
tree.write(modulePath, sourceFile.getFullText());
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const callExpression = moduleDecorator.getCallExpression();
|
|
103
|
+
if (!callExpression) {
|
|
104
|
+
tree.write(modulePath, sourceFile.getFullText());
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
let moduleLiteral = callExpression.getArguments()[0];
|
|
108
|
+
if (!moduleLiteral ||
|
|
109
|
+
moduleLiteral.getKind() !== SyntaxKind.ObjectLiteralExpression) {
|
|
110
|
+
moduleLiteral = callExpression.addArgument('{}');
|
|
111
|
+
}
|
|
112
|
+
const importsArray = ensureImportsArray(moduleLiteral);
|
|
113
|
+
const hasTypeOrm = importsArray
|
|
114
|
+
.getElements()
|
|
115
|
+
.some((element) => element.getText().includes('TypeOrmModule.forRoot'));
|
|
116
|
+
if (!hasTypeOrm) {
|
|
117
|
+
importsArray.addElement((writer) => {
|
|
118
|
+
writer.write('TypeOrmModule.forRootAsync({');
|
|
119
|
+
writer.indent(() => {
|
|
120
|
+
writer.writeLine('useFactory: async () => ({');
|
|
121
|
+
writer.indent(() => {
|
|
122
|
+
writer.writeLine('...makeRuntimeDataSource().options,');
|
|
123
|
+
writer.writeLine('autoLoadEntities: true,');
|
|
124
|
+
});
|
|
125
|
+
writer.writeLine('}),');
|
|
126
|
+
writer.writeLine('dataSourceFactory: async () => makeRuntimeDataSource().initialize(),');
|
|
127
|
+
});
|
|
128
|
+
writer.write('})');
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
tree.write(modulePath, sourceFile.getFullText());
|
|
132
|
+
}
|
|
133
|
+
function ensureImport(sourceFile, moduleSpecifier, importName) {
|
|
134
|
+
const existing = sourceFile.getImportDeclaration(moduleSpecifier);
|
|
135
|
+
if (!existing) {
|
|
136
|
+
sourceFile.addImportDeclaration({
|
|
137
|
+
moduleSpecifier,
|
|
138
|
+
namedImports: [importName],
|
|
139
|
+
});
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const hasImport = existing
|
|
143
|
+
.getNamedImports()
|
|
144
|
+
.some((namedImport) => namedImport.getName() === importName);
|
|
145
|
+
if (!hasImport) {
|
|
146
|
+
existing.addNamedImport(importName);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function ensureImportsArray(moduleLiteral) {
|
|
150
|
+
const existing = moduleLiteral.getProperty('imports');
|
|
151
|
+
if (existing && Node.isPropertyAssignment(existing)) {
|
|
152
|
+
const initializer = existing.getInitializerIfKind(SyntaxKind.ArrayLiteralExpression);
|
|
153
|
+
if (initializer) {
|
|
154
|
+
return initializer;
|
|
155
|
+
}
|
|
156
|
+
existing.setInitializer('[]');
|
|
157
|
+
return existing.getInitializerIfKindOrThrow(SyntaxKind.ArrayLiteralExpression);
|
|
158
|
+
}
|
|
159
|
+
if (existing) {
|
|
160
|
+
existing.remove();
|
|
161
|
+
}
|
|
162
|
+
const property = moduleLiteral.addPropertyAssignment({
|
|
163
|
+
name: 'imports',
|
|
164
|
+
initializer: '[]',
|
|
165
|
+
});
|
|
166
|
+
return property.getInitializerIfKindOrThrow(SyntaxKind.ArrayLiteralExpression);
|
|
167
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"$id": "BootstrapGenerator",
|
|
4
|
+
"title": "TypeORM Bootstrap Generator",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"properties": {
|
|
7
|
+
"project": {
|
|
8
|
+
"type": "string",
|
|
9
|
+
"description": "Name of the existing Nx project to wire up for TypeORM."
|
|
10
|
+
},
|
|
11
|
+
"domain": {
|
|
12
|
+
"type": "string",
|
|
13
|
+
"description": "Domain name used when scaffolding a library project."
|
|
14
|
+
},
|
|
15
|
+
"schema": {
|
|
16
|
+
"type": "string",
|
|
17
|
+
"description": "Database schema to use. Defaults to the provided domain."
|
|
18
|
+
},
|
|
19
|
+
"db": {
|
|
20
|
+
"type": "string",
|
|
21
|
+
"description": "Database type passed to TypeORM.",
|
|
22
|
+
"default": "postgres"
|
|
23
|
+
},
|
|
24
|
+
"withCompose": {
|
|
25
|
+
"type": "boolean",
|
|
26
|
+
"description": "Include a Docker Compose file for local Postgres development.",
|
|
27
|
+
"default": false
|
|
28
|
+
},
|
|
29
|
+
"skipInstall": {
|
|
30
|
+
"type": "boolean",
|
|
31
|
+
"description": "Skip running package installations after updating dependencies.",
|
|
32
|
+
"default": false
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"required": ["project"],
|
|
36
|
+
"additionalProperties": false
|
|
37
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { default as generateExecutor } from './executors/generate/executor.js';
|
|
2
|
+
export { default as runExecutor } from './executors/run/executor.js';
|
|
3
|
+
export { default as revertExecutor } from './executors/revert/executor.js';
|
|
4
|
+
export { default as ensureSchemaExecutor } from './executors/ensure-schema/executor.js';
|
|
5
|
+
export { default as seedExecutor } from './executors/seed/executor.js';
|
|
6
|
+
export { default as bootstrapGenerator } from './generators/bootstrap/generator.js';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { default as generateExecutor } from './executors/generate/executor.js';
|
|
2
|
+
export { default as runExecutor } from './executors/run/executor.js';
|
|
3
|
+
export { default as revertExecutor } from './executors/revert/executor.js';
|
|
4
|
+
export { default as ensureSchemaExecutor } from './executors/ensure-schema/executor.js';
|
|
5
|
+
export { default as seedExecutor } from './executors/seed/executor.js';
|
|
6
|
+
export { default as bootstrapGenerator } from './generators/bootstrap/generator.js';
|
package/dist/index.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"executors": {
|
|
3
|
+
"generate": {
|
|
4
|
+
"implementation": "./executors/generate/executor",
|
|
5
|
+
"schema": "./executors/generate/schema.json"
|
|
6
|
+
},
|
|
7
|
+
"run": {
|
|
8
|
+
"implementation": "./executors/run/executor",
|
|
9
|
+
"schema": "./executors/run/schema.json"
|
|
10
|
+
},
|
|
11
|
+
"revert": {
|
|
12
|
+
"implementation": "./executors/revert/executor",
|
|
13
|
+
"schema": "./executors/revert/schema.json"
|
|
14
|
+
},
|
|
15
|
+
"ensure-schema": {
|
|
16
|
+
"implementation": "./executors/ensure-schema/executor",
|
|
17
|
+
"schema": "./executors/ensure-schema/schema.json"
|
|
18
|
+
},
|
|
19
|
+
"seed": {
|
|
20
|
+
"implementation": "./executors/seed/executor",
|
|
21
|
+
"schema": "./executors/seed/schema.json"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"generators": {
|
|
25
|
+
"bootstrap": {
|
|
26
|
+
"implementation": "./generators/bootstrap/generator",
|
|
27
|
+
"schema": "./generators/bootstrap/schema.json"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
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;
|
|
7
|
+
//# sourceMappingURL=typeorm.d.ts.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,161 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spawn.d.ts","sourceRoot":"","sources":["../../src/utils/spawn.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,YAAY;IAC3B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;CACzB;AAED,wBAAgB,KAAK,CACnB,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,EAAE,EACd,OAAO,GAAE,YAAiB,GACzB,OAAO,CAAC,MAAM,CAAC,CAUjB"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { spawn as nativeSpawn } from 'node:child_process';
|
|
2
|
+
export function spawn(cmd, args, options = {}) {
|
|
3
|
+
const cwd = options.cwd ?? process.cwd();
|
|
4
|
+
const env = { ...process.env, ...options.env };
|
|
5
|
+
return new Promise((resolve) => {
|
|
6
|
+
const child = nativeSpawn(cmd, args, { stdio: 'inherit', cwd, env });
|
|
7
|
+
child.on('close', (code) => {
|
|
8
|
+
resolve(code ?? 1);
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@anarchitects/nx-typeorm",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Nx plugin providing TypeORM executors, generators, and schema management utilities.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"license": "Apache-2.0",
|
|
8
|
+
"executors": "dist/index.json",
|
|
9
|
+
"generators": "dist/index.json",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"README.md",
|
|
13
|
+
"!**/*.tsbuildinfo"
|
|
14
|
+
],
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public",
|
|
17
|
+
"provenance": false
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/anarchitects/anarchitecture-plugins.git",
|
|
22
|
+
"directory": "packages/typeorm"
|
|
23
|
+
},
|
|
24
|
+
"nx": {
|
|
25
|
+
"name": "nx-typeorm",
|
|
26
|
+
"plugins": [
|
|
27
|
+
{
|
|
28
|
+
"plugin": "dist/plugins/typeorm.js"
|
|
29
|
+
}
|
|
30
|
+
],
|
|
31
|
+
"targets": {
|
|
32
|
+
"build": {
|
|
33
|
+
"executor": "@nx/js:tsc",
|
|
34
|
+
"outputs": [
|
|
35
|
+
"{options.outputPath}"
|
|
36
|
+
],
|
|
37
|
+
"options": {
|
|
38
|
+
"outputPath": "packages/typeorm/dist",
|
|
39
|
+
"main": "packages/typeorm/src/index.ts",
|
|
40
|
+
"tsConfig": "packages/typeorm/tsconfig.lib.json",
|
|
41
|
+
"rootDir": "packages/typeorm/src",
|
|
42
|
+
"generatePackageJson": false,
|
|
43
|
+
"assets": [
|
|
44
|
+
{
|
|
45
|
+
"input": "./packages/typeorm/src",
|
|
46
|
+
"glob": "**/!(*.ts)",
|
|
47
|
+
"output": "."
|
|
48
|
+
}
|
|
49
|
+
]
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"test": {
|
|
53
|
+
"executor": "@nx/jest:jest",
|
|
54
|
+
"outputs": [
|
|
55
|
+
"{projectRoot}/test-output/jest/coverage"
|
|
56
|
+
],
|
|
57
|
+
"options": {
|
|
58
|
+
"jestConfig": "packages/typeorm/jest.config.cts",
|
|
59
|
+
"passWithNoTests": false
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"tags": [
|
|
64
|
+
"type:plugin",
|
|
65
|
+
"domain:typeorm"
|
|
66
|
+
]
|
|
67
|
+
},
|
|
68
|
+
"peerDependencies": {
|
|
69
|
+
"@nx/devkit": "^22.0.0",
|
|
70
|
+
"nx": "22.0.1",
|
|
71
|
+
"ts-morph": "^22.0.0",
|
|
72
|
+
"ts-node": "10.9.1",
|
|
73
|
+
"typeorm": "^0.3.20"
|
|
74
|
+
},
|
|
75
|
+
"dependencies": {
|
|
76
|
+
"tslib": "^2.6.2"
|
|
77
|
+
},
|
|
78
|
+
"devDependencies": {
|
|
79
|
+
"ts-morph": "^22.0.0"
|
|
80
|
+
}
|
|
81
|
+
}
|