Skip to content

Commit b3a27e5

Browse files
authored
refactor(ConnectionOptionsReader)!: remove ConnectionOptionsEnvReader and TYPEORM_* env variable support (#12134)
1 parent 52d2d7f commit b3a27e5

13 files changed

Lines changed: 19 additions & 324 deletions

File tree

docs/docs/guides/8-migration-v1.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,20 @@ const migrations = await migrationExecutor.getAllMigrations()
287287
// After
288288
const migrations = migrationExecutor.getMigrations()
289289
```
290+
291+
## Configuration
292+
293+
### Drop support for configuration via environment variables
294+
295+
The deprecated `ConnectionOptionsEnvReader` class and the ability to configure connections via `TYPEORM_CONNECTION`, `TYPEORM_URL`, and other `TYPEORM_*` environment variables has been removed. The `ormconfig.env` file format is also no longer supported. TypeORM no longer auto-loads `.env` files or depends on `dotenv`.
296+
297+
Use a TypeScript or JavaScript configuration file (`ormconfig.ts`, `ormconfig.js`) instead, referencing environment variables directly:
298+
299+
```typescript
300+
// ormconfig.ts
301+
export default {
302+
type: process.env.DB_TYPE,
303+
url: process.env.DB_URL,
304+
// ...
305+
}
306+
```

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
"dayjs": "^1.11.19",
100100
"debug": "^4.4.3",
101101
"dedent": "^1.7.1",
102-
"dotenv": "^17.3.1",
103102
"reflect-metadata": "^0.2.2",
104103
"sql-highlight": "^6.1.0",
105104
"tinyglobby": "^0.2.15",

pnpm-lock.yaml

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/connection/ConnectionOptionsReader.ts

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { TypeORMError } from "../error"
66
import { PlatformTools } from "../platform/PlatformTools"
77
import { importOrRequireFile } from "../util/ImportUtils"
88
import { isAbsolute } from "../util/PathUtils"
9-
import { ConnectionOptionsEnvReader } from "./options-reader/ConnectionOptionsEnvReader"
109

1110
/**
1211
* Reads connection options from the ormconfig.
@@ -97,16 +96,7 @@ export class ConnectionOptionsReader {
9796
| DataSourceOptions[]
9897
| undefined = undefined
9998

100-
const fileFormats = [
101-
"env",
102-
"js",
103-
"mjs",
104-
"cjs",
105-
"ts",
106-
"mts",
107-
"cts",
108-
"json",
109-
]
99+
const fileFormats = ["js", "mjs", "cjs", "ts", "mts", "cts", "json"]
110100

111101
// Detect if baseFilePath contains file extension
112102
const possibleExtension = this.baseFilePath.substr(
@@ -128,34 +118,8 @@ export class ConnectionOptionsReader {
128118
? this.baseFilePath
129119
: this.baseFilePath + "." + foundFileFormat
130120

131-
// if .env file found then load all its variables into process.env using dotenv package
132-
if (foundFileFormat === "env") {
133-
try {
134-
PlatformTools.dotenv(configFile)
135-
} catch (err) {
136-
PlatformTools.logWarn(
137-
`Warning: Could not load environment variables from .env file at ${configFile}`,
138-
err instanceof Error ? err.message : String(err),
139-
)
140-
}
141-
} else if (PlatformTools.fileExist(this.baseDirectory + "/.env")) {
142-
try {
143-
PlatformTools.dotenv(this.baseDirectory + "/.env")
144-
} catch (err) {
145-
PlatformTools.logWarn(
146-
`Warning: Could not load environment variables from .env file at ${this.baseDirectory + "/.env"}`,
147-
err instanceof Error ? err.message : String(err),
148-
)
149-
}
150-
}
151-
152121
// try to find connection options from any of available sources of configuration
153122
if (
154-
PlatformTools.getEnvVariable("TYPEORM_CONNECTION") ||
155-
PlatformTools.getEnvVariable("TYPEORM_URL")
156-
) {
157-
connectionOptions = new ConnectionOptionsEnvReader().read()
158-
} else if (
159123
foundFileFormat === "js" ||
160124
foundFileFormat === "mjs" ||
161125
foundFileFormat === "cjs" ||

src/connection/options-reader/ConnectionOptionsEnvReader.ts

Lines changed: 0 additions & 155 deletions
This file was deleted.

src/platform/BrowserConnectionOptionsReaderDummy.template

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
/**
2-
* Dummy class for replacement via `package.json` in browser builds.
3-
*
4-
* If we don't include these functions typeorm will throw an error on runtime
5-
* as well as during webpack builds.
6-
*/
7-
export class ConnectionOptionsEnvReader {
8-
async read() {
9-
throw new Error(`Cannot read connection options in a browser context.`);
10-
}
11-
}
12-
131
/**
142
* Dummy class for replacement via `package.json` in browser builds.
153
*

src/platform/BrowserPlatformTools.template

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,6 @@ export class PlatformTools {
7979
return false;
8080
}
8181

82-
static dotenv(pathStr: string): void {
83-
if (this.type === "browser")
84-
throw new Error(`This option/function is not supported in the browser environment. Failed operation: dotenv.config({ path: "${pathStr}" }).`);
85-
}
86-
87-
/**
88-
* Gets environment variable.
89-
*/
90-
static getEnvVariable(name: string): any {
91-
// if (this.type === "browser")
92-
// throw new Error(`This option/function is not supported in the browser environment. Failed operation: process.env["${name}"].`);
93-
return undefined;
94-
}
95-
9682
static readFileSync(filename: string): any {
9783
if (this.type === "browser")
9884
throw new Error(`This option/function is not supported in the browser environment. Failed operation: fs.readFileSync("${filename}").`);

src/platform/PlatformTools.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import ansi from "ansis"
2-
import dotenv from "dotenv"
32
import fs from "fs"
43
import path from "path"
54
import { highlight } from "sql-highlight"
@@ -197,23 +196,6 @@ export class PlatformTools {
197196
return fs.promises.writeFile(path, data)
198197
}
199198

200-
/**
201-
* Loads a dotenv file into the environment variables.
202-
* @param path The file to load as a dotenv configuration
203-
* @param pathStr
204-
*/
205-
static dotenv(pathStr: string): void {
206-
dotenv.config({ path: pathStr })
207-
}
208-
209-
/**
210-
* Gets environment variable.
211-
* @param name
212-
*/
213-
static getEnvVariable(name: string): any {
214-
return process.env[name]
215-
}
216-
217199
/**
218200
* Highlights sql string to be printed in the console.
219201
* @param sql

test/functional/ormconfigs/all/ormconfig.env

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/functional/ormconfigs/all/ormconfig.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)