Skip to content
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default defineConfig([
// custom rules
"@typescript-eslint/consistent-type-exports": "error",
"@typescript-eslint/consistent-type-imports": "error",
"@typescript-eslint/prefer-nullish-coalescing": "error",
"@typescript-eslint/prefer-optional-chain": "error",

// exceptions from typescript-eslint/recommended
Expand Down
4 changes: 2 additions & 2 deletions sample/sample10-mixed/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ dataSource
postRepository
.save(post)
.then((result) => console.log(result))
.catch((error) => console.log(error.stack ? error.stack : error))
.catch((error) => console.log(error.stack ?? error))
})
.catch((error) => console.log(error.stack ? error.stack : error))
.catch((error) => console.log(error.stack ?? error))
8 changes: 2 additions & 6 deletions sample/sample13-everywhere-abstraction/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,8 @@ dataSource.initialize().then(
return blogRepository.save(blog)
})
.catch((error) =>
console.log(
"Cannot save. Error: ",
error.stack ? error.stack : error,
),
console.log("Cannot save. Error: ", error.stack ?? error),
)
},
(error) =>
console.log("Cannot connect: ", error.stack ? error.stack : error),
(error) => console.log("Cannot connect: ", error.stack ?? error),
)
4 changes: 2 additions & 2 deletions sample/sample24-schemas/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
// NOTE: this example is not working yet, only concepts of how this feature must work described here

const PostEntity = new EntitySchema<Post>(
require(

Check warning on line 12 in sample/sample24-schemas/app.ts

View workflow job for this annotation

GitHub Actions / lint

A `require()` style import is forbidden
__dirname + "/../../../../sample/sample24-schemas/schemas/post.json",
),
)
const PostDetailsEntity = new EntitySchema<PostDetails>(
require(

Check warning on line 17 in sample/sample24-schemas/app.ts

View workflow job for this annotation

GitHub Actions / lint

A `require()` style import is forbidden
__dirname +
"/../../../../sample/sample24-schemas/schemas/post-details.json",
),
)
const CategoryEntity = new EntitySchema<Category>(
require(

Check warning on line 23 in sample/sample24-schemas/app.ts

View workflow job for this annotation

GitHub Actions / lint

A `require()` style import is forbidden
__dirname +
"/../../../../sample/sample24-schemas/schemas/category.json",
),
)
const ImageEntity = new EntitySchema<Image>(
require(

Check warning on line 29 in sample/sample24-schemas/app.ts

View workflow job for this annotation

GitHub Actions / lint

A `require()` style import is forbidden
__dirname + "/../../../../sample/sample24-schemas/schemas/image.json",
),
)
Expand Down Expand Up @@ -66,6 +66,6 @@
.then((result) => {
console.log(result)
})
.catch((error) => console.log(error.stack ? error.stack : error))
.catch((error) => console.log(error.stack ?? error))
})
.catch((error) => console.log(error.stack ? error.stack : error))
.catch((error) => console.log(error.stack ?? error))
8 changes: 2 additions & 6 deletions sample/sample5-subscribers/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,8 @@ dataSource.initialize().then(
console.log("post removed.")
})
.catch((error) =>
console.log(
"Cannot save. Error: ",
error.stack ? error.stack : error,
),
console.log("Cannot save. Error: ", error.stack ?? error),
)
},
(error) =>
console.log("Cannot connect: ", error.stack ? error.stack : error),
(error) => console.log("Cannot connect: ", error.stack ?? error),
)
8 changes: 2 additions & 6 deletions sample/sample6-abstract-table/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,8 @@ dataSource.initialize().then(
return blogRepository.save(blog)
})
.catch((error) =>
console.log(
"Cannot save. Error: ",
error.stack ? error.stack : error,
),
console.log("Cannot save. Error: ", error.stack ?? error),
)
},
(error) =>
console.log("Cannot connect: ", error.stack ? error.stack : error),
(error) => console.log("Cannot connect: ", error.stack ?? error),
)
8 changes: 2 additions & 6 deletions sample/sample7-pagination/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,8 @@ dataSource.initialize().then(
console.log("count: ", entitiesWithCount[1])
})
.catch((error) =>
console.log(
"Cannot save. Error: ",
error.stack ? error.stack : error,
),
console.log("Cannot save. Error: ", error.stack ?? error),
)
},
(error) =>
console.log("Cannot connect: ", error.stack ? error.stack : error),
(error) => console.log("Cannot connect: ", error.stack ?? error),
)
8 changes: 2 additions & 6 deletions sample/sample8-self-referencing/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ dataSource.initialize().then(
console.log("saved category: ", savedCategory)
})
.catch((error) =>
console.log(
"Cannot save. Error: ",
error.stack ? error.stack : error,
),
console.log("Cannot save. Error: ", error.stack ?? error),
)
},
(error) =>
console.log("Cannot connect: ", error.stack ? error.stack : error),
(error) => console.log("Cannot connect: ", error.stack ?? error),
)
8 changes: 2 additions & 6 deletions sample/sample9-entity-listeners/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,8 @@ dataSource.initialize().then(
console.log("post removed.")
})
.catch((error) =>
console.log(
"Cannot save. Error: ",
error.stack ? error.stack : error,
),
console.log("Cannot save. Error: ", error.stack ?? error),
)
},
(error) =>
console.log("Cannot connect: ", error.stack ? error.stack : error),
(error) => console.log("Cannot connect: ", error.stack ?? error),
)
6 changes: 4 additions & 2 deletions src/cache/DbQueryResultCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export class DbQueryResultCache implements QueryResultCache {
typeof this.dataSource.options.cache === "object"
? this.dataSource.options.cache
: {}
const cacheTableName = cacheOptions.tableName || "query-result-cache"
const tableName =
cacheOptions.tableName === "" ? undefined : cacheOptions.tableName
const cacheTableName = tableName ?? "query-result-cache"

this.queryResultCacheDatabase = database
this.queryResultCacheSchema = schema
Expand Down Expand Up @@ -317,7 +319,7 @@ export class DbQueryResultCache implements QueryResultCache {
identifiers: string[],
queryRunner?: QueryRunner,
): Promise<void> {
const _queryRunner: QueryRunner = queryRunner || this.getQueryRunner()
const _queryRunner: QueryRunner = queryRunner ?? this.getQueryRunner()
await Promise.all(
identifiers.map((identifier) => {
const qb = _queryRunner.manager.createQueryBuilder()
Expand Down
8 changes: 6 additions & 2 deletions src/cache/RedisQueryResultCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ export class RedisQueryResultCache implements QueryResultCache {
options: QueryResultCacheOptions,
queryRunner?: QueryRunner,
): Promise<QueryResultCacheOptions | undefined> {
const key = options.identifier || options.query
const identifier =
options.identifier === "" ? undefined : options.identifier
const key = identifier ?? options.query
if (!key) return undefined

const result = await this.client.get(key)
Expand Down Expand Up @@ -153,7 +155,9 @@ export class RedisQueryResultCache implements QueryResultCache {
savedCache: QueryResultCacheOptions,
queryRunner?: QueryRunner,
): Promise<void> {
const key = options.identifier || options.query
const identifier =
options.identifier === "" ? undefined : options.identifier
const key = identifier ?? options.query
if (!key) return

const value = JSON.stringify(options)
Expand Down
4 changes: 2 additions & 2 deletions src/cli-ts-node-esm.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
import { spawnSync } from "child_process"

if ((process.env["NODE_OPTIONS"] || "").includes("--loader ts-node"))
if ((process.env["NODE_OPTIONS"] ?? "").includes("--loader ts-node"))
require("./cli")
else {
const childProcess = spawnSync(process.argv[0], process.argv.slice(1), {
Expand All @@ -19,5 +19,5 @@ else {
windowsHide: true,
})

process.exit(childProcess.status || 0)
process.exit(childProcess.status ?? 0)
}
10 changes: 5 additions & 5 deletions src/commands/InitCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class InitCommand implements yargs.CommandModule {

async handler(args: yargs.Arguments) {
try {
const database: string = (args.database as any) || "postgres"
const database: string = (args.database as any) ?? "postgres"
const isExpress = args.express !== undefined ? true : false
const isDocker = args.docker !== undefined ? true : false
const basePath = process.cwd() + (args.name ? "/" + args.name : "")
Expand Down Expand Up @@ -581,7 +581,7 @@ AppDataSource.initialize().then(async () => {
): string {
return JSON.stringify(
{
name: projectName || "typeorm-sample",
name: projectName ?? "typeorm-sample",
version: "0.0.1",
description: "Awesome project developed with TypeORM.",
type: projectIsEsm ? "module" : "commonjs",
Expand Down Expand Up @@ -741,15 +741,15 @@ Steps to run this project:
),
)

if (!packageJson.devDependencies) packageJson.devDependencies = {}
packageJson.devDependencies ??= {}
packageJson.devDependencies = {
"@types/node": ourPackageJson.devDependencies["@types/node"],
"ts-node": ourPackageJson.devDependencies["ts-node"],
typescript: ourPackageJson.devDependencies.typescript,
...packageJson.devDependencies,
}

if (!packageJson.dependencies) packageJson.dependencies = {}
packageJson.dependencies ??= {}
packageJson.dependencies = {
...packageJson.dependencies,
"reflect-metadata": ourPackageJson.dependencies["reflect-metadata"],
Expand Down Expand Up @@ -794,7 +794,7 @@ Steps to run this project:
packageJson.dependencies["body-parser"] = "^1.20.3"
}

if (!packageJson.scripts) packageJson.scripts = {}
packageJson.scripts ??= {}

if (projectIsEsm)
Object.assign(packageJson.scripts, {
Expand Down
2 changes: 1 addition & 1 deletion src/connection/ConnectionOptionsReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class ConnectionOptionsReader {

// try to find any of following configuration formats
const foundFileFormat =
fileExtension ||
fileExtension ??
fileFormats.find((format) => {
return PlatformTools.fileExist(this.baseFilePath + "." + format)
})
Expand Down
26 changes: 13 additions & 13 deletions src/data-source/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ export class DataSource {
this.driver = new DriverFactory().create(this)
this.manager = this.createEntityManager()
this.namingStrategy =
options.namingStrategy || new DefaultNamingStrategy()
this.metadataTableName = options.metadataTableName || "typeorm_metadata"
options.namingStrategy ?? new DefaultNamingStrategy()
this.metadataTableName = options.metadataTableName ?? "typeorm_metadata"
this.queryResultCache = options.cache
? new QueryResultCacheFactory(this).create()
: undefined
Expand Down Expand Up @@ -201,8 +201,8 @@ export class DataSource {

if (options.logger || options.logging) {
this.logger = new LoggerFactory().create(
options.logger || this.options.logger,
options.logging || this.options.logging,
options.logger ?? this.options.logger,
options.logging ?? this.options.logging,
)
}

Expand Down Expand Up @@ -361,10 +361,10 @@ export class DataSource {

const migrationExecutor = new MigrationExecutor(this)
migrationExecutor.transaction =
options?.transaction ||
this.options?.migrationsTransactionMode ||
options?.transaction ??
this.options?.migrationsTransactionMode ??
"all"
migrationExecutor.fake = options?.fake || false
migrationExecutor.fake = options?.fake ?? false

const successMigrations =
await migrationExecutor.executePendingMigrations()
Expand All @@ -386,8 +386,8 @@ export class DataSource {
if (!this.isInitialized) throw new CannotExecuteNotConnectedError()

const migrationExecutor = new MigrationExecutor(this)
migrationExecutor.transaction = options?.transaction || "all"
migrationExecutor.fake = options?.fake || false
migrationExecutor.transaction = options?.transaction ?? "all"
migrationExecutor.fake = options?.fake ?? false

await migrationExecutor.undoLastMigration()
}
Expand Down Expand Up @@ -508,7 +508,7 @@ export class DataSource {
if (queryRunner?.isReleased)
throw new QueryRunnerProviderAlreadyReleasedError()

const usedQueryRunner = queryRunner || this.createQueryRunner()
const usedQueryRunner = queryRunner ?? this.createQueryRunner()

try {
return await usedQueryRunner.query(query, parameters) // await is needed here because we are using finally
Expand Down Expand Up @@ -710,7 +710,7 @@ export class DataSource {

// create subscribers instances if they are not disallowed from high-level (for example they can disallowed from migrations run process)
const flattenedSubscribers = ObjectUtils.mixedListToArray(
this.options.subscribers || [],
this.options.subscribers ?? [],
)
const subscribers =
await connectionMetadataBuilder.buildSubscribers(
Expand All @@ -720,7 +720,7 @@ export class DataSource {

// build entity metadatas
const flattenedEntities = ObjectUtils.mixedListToArray(
this.options.entities || [],
this.options.entities ?? [],
)
const entityMetadatas =
await connectionMetadataBuilder.buildEntityMetadatas(
Expand All @@ -735,7 +735,7 @@ export class DataSource {

// create migration instances
const flattenedMigrations = ObjectUtils.mixedListToArray(
this.options.migrations || [],
this.options.migrations ?? [],
)
const migrations =
await connectionMetadataBuilder.buildMigrations(flattenedMigrations)
Expand Down
2 changes: 1 addition & 1 deletion src/decorator/Check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function Check(
maybeExpression?: string,
): ClassDecorator & PropertyDecorator {
const name = maybeExpression ? nameOrExpression : undefined
const expression = maybeExpression ? maybeExpression : nameOrExpression
const expression = maybeExpression ?? nameOrExpression

if (!expression) throw new TypeORMError(`Check expression is required`)

Expand Down
11 changes: 5 additions & 6 deletions src/decorator/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,11 @@ export function Index(
!Array.isArray(nameOrFieldsOrOptions)
? (nameOrFieldsOrOptions as IndexOptions)
: maybeOptions
if (!options)
options =
ObjectUtils.isObject(maybeFieldsOrOptions) &&
!Array.isArray(maybeFieldsOrOptions)
? (maybeFieldsOrOptions as IndexOptions)
: maybeOptions
options ??=
ObjectUtils.isObject(maybeFieldsOrOptions) &&
!Array.isArray(maybeFieldsOrOptions)
? (maybeFieldsOrOptions as IndexOptions)
: maybeOptions

return function (
clsOrObject: Function | Object,
Expand Down
11 changes: 5 additions & 6 deletions src/decorator/Unique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ export function Unique(
!Array.isArray(nameOrFieldsOrOptions)
? (nameOrFieldsOrOptions as UniqueOptions)
: maybeOptions
if (!options)
options =
ObjectUtils.isObject(nameOrFieldsOrOptions) &&
!Array.isArray(maybeFieldsOrOptions)
? (maybeFieldsOrOptions as UniqueOptions)
: maybeOptions
options ??=
ObjectUtils.isObject(nameOrFieldsOrOptions) &&
!Array.isArray(maybeFieldsOrOptions)
? (maybeFieldsOrOptions as UniqueOptions)
: maybeOptions

return function (
clsOrObject: Function | Object,
Expand Down
5 changes: 2 additions & 3 deletions src/decorator/columns/Column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export function Column(
options = <ColumnOptions>typeOrOptions
type = typeOrOptions.type
}
if (!options) options = {} as ColumnOptions
options ??= {} as ColumnOptions

// if type is not given explicitly then try to guess it
const reflectMetadataType =
Expand Down Expand Up @@ -215,8 +215,7 @@ export function Column(
propertyName: propertyName,
isArray:
reflectMetadataType === Array || options.array === true,
prefix:
options.prefix !== undefined ? options.prefix : undefined,
prefix: options.prefix ?? undefined,
type: typeOrOptions as (type?: any) => Function,
} as EmbeddedMetadataArgs)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/decorator/columns/CreateDateColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function CreateDateColumn(options?: ColumnOptions): PropertyDecorator {
target: object.constructor,
propertyName: propertyName,
mode: "createDate",
options: options || {},
options: options ?? {},
} as ColumnMetadataArgs)
}
}
2 changes: 1 addition & 1 deletion src/decorator/columns/DeleteDateColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function DeleteDateColumn(options?: ColumnOptions): PropertyDecorator {
target: object.constructor,
propertyName: propertyName,
mode: "deleteDate",
options: options || {},
options: options ?? {},
} as ColumnMetadataArgs)
}
}
Loading
Loading