This repository has been archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
ormconfig.js
162 lines (159 loc) · 5.26 KB
/
ormconfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const fg = require('fast-glob');
const ConnectionString = require('connection-string');
const load = require('dotenv').load;
const fs = require('fs');
const path = require('path');
const runnedInMigration = process.env.MIGRATIONS === 'true';
const envName = process.env.NODE_ENV || 'develop';
const isDevelop = envName === 'develop';
const sourceRootKey = isDevelop ? 'sourceRoot' : 'outputPath';
const entitiesExt = '{.js,.ts}';;
const angularJson = JSON.parse(fs.readFileSync('angular.json'));
const packageJson = JSON.parse(fs.readFileSync('package.json'));
const vendors = packageJson.externalLibs || [];
try {
fs.accessSync(`${envName}.env`);
load({ path: `${envName}.env` });
console.log(`env file: ${envName}.env`);
} catch (error) {
console.log(`error on get env file: ${envName}.env`);
try {
fs.accessSync(`.env`);
load();
console.log(`env file: .env`);
} catch (error) {
console.log(`error on get env file: .env`);
}
}
const connectionString = new ConnectionString(process.env.DATABASE_URL || 'sqlite://database/sqlitedb.db');
const vendorsLibs = Object.keys(vendors).map(index => {
const vendorConfig = {};
vendorConfig[sourceRootKey] = vendors[index];
return vendorConfig;
});
const libs = Object.keys(angularJson.projects).filter(key => angularJson.projects[key].projectType === 'library').filter(lib =>
isDevelop ||
lib[sourceRootKey] !== undefined ||
(
lib.architect &&
lib.architect.build &&
lib.architect.build.options &&
lib.architect.build.options[sourceRootKey] !== undefined
)
).map(key => angularJson.projects[key]);
const apps = Object.keys(angularJson.projects).filter(key => angularJson.projects[key].projectType === 'application').filter(lib =>
isDevelop ||
lib[sourceRootKey] !== undefined ||
(
lib.architect &&
lib.architect.build &&
lib.architect.build.options &&
lib.architect.build.options[sourceRootKey] !== undefined
)
).map(key => angularJson.projects[key]);
const defaultProject = angularJson.defaultProject;
const defaultApp = angularJson.projects[defaultProject];
const migrationsDir = `${defaultApp[sourceRootKey]}/migrations`;
const entities = (
fg.sync(
[
...(
runnedInMigration ? [
...vendorsLibs,
...libs,
...apps
].map(lib =>
`${lib[sourceRootKey] || lib.architect.build.options[sourceRootKey]}/**/migrations_entities/**/*.entity${entitiesExt}`
) : []
),
...(
!runnedInMigration ? [
...vendorsLibs,
...libs,
...apps
].map(lib =>
`${lib[sourceRootKey] || lib.architect.build.options[sourceRootKey]}/**/entities/**/*.entity${entitiesExt}`
) : []
)
]
)
);
const migrations = normalizationFileList(
fg.sync(
runnedInMigration ? [
...vendorsLibs,
...libs,
...apps
].map(lib =>
`${lib[sourceRootKey] || lib.architect.build.options[sourceRootKey]}/**/migrations/**/*${entitiesExt}`
) : []
)
);
const subscribers = (
fg.sync(
[
...vendorsLibs,
...libs,
...apps
].map(lib =>
`${lib[sourceRootKey] || lib.architect.build.options[sourceRootKey]}/**/subscribers/**/*${entitiesExt}`
)
)
);
if (connectionString.protocol === 'sqlite') {
const dbFile =
'./' +
(connectionString.hosts ? connectionString.hosts[0].name : '') +
(connectionString.path ? '/' + connectionString.path[0] : '');
module.exports = {
type: 'sqlite',
database: dbFile,
entities: entities,
migrations: migrations,
subscribers: subscribers,
logging: 'all',
synchronize: false,
cli: {
migrationsDir: migrationsDir
}
}
} else {
module.exports = {
type: connectionString.protocol,
host: connectionString.hosts && connectionString.hosts[0].name,
port: connectionString.hosts && +connectionString.hosts[0].port,
username: connectionString.user,
password: connectionString.password,
database: connectionString.path && connectionString.path[0],
entities: entities,
migrations: migrations,
subscribers: subscribers,
logging: 'all',
synchronize: false,
cli: {
migrationsDir: migrationsDir
}
}
}
function normalizationFileList(files) {
const newFiles = [];
for (var i = 0; i < files.length; i++) {
const filename = files[i];
if (filename.indexOf('.d.ts') === -1) {
var founded = false;
for (var j = 0; j < newFiles.length; j++) {
const filename = newFiles[j];
if (
path.basename(filename, path.parse(filename).ext) ===
path.basename(files[i], path.parse(files[i]).ext)
) {
founded = true;
}
}
if (!founded) {
newFiles.push(files[i]);
}
}
}
return newFiles;
}