Skip to content

Commit

Permalink
feat: stricter typescript rules
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Feb 15, 2023
1 parent 5d7b486 commit 64a2868
Show file tree
Hide file tree
Showing 35 changed files with 296 additions and 850 deletions.
890 changes: 121 additions & 769 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"files": [
"dist/"
],
"engines": {
"node": ">=14.0.0"
},
"scripts": {
"build": "rm -rf ./dist && tsc",
"build:watch": "npm run build -- --watch",
Expand Down Expand Up @@ -67,20 +70,18 @@
"@semantic-release/github": "^8.0.6",
"@semantic-release/npm": "^9.0.2",
"@semantic-release/release-notes-generator": "^10.0.3",
"@tada5hi/eslint-config-typescript": "^1.1.1",
"@types/glob": "^8.0.1",
"@tada5hi/eslint-config-typescript": "^1.1.2",
"@tada5hi/tsconfig": "^0.4.0",
"@types/jest": "^27.5.0",
"@types/node": "^18.11.18",
"@types/yargs": "^17.0.22",
"better-sqlite3": "^8.0.1",
"coveralls": "^3.1.0",
"cross-env": "^7.0.3",
"eslint": "^8.33.0",
"eslint": "^8.34.0",
"husky": "^8.0.3",
"jest": "^27.5.1",
"semantic-release": "^19.0.5",
"ts-jest": "^27.1.4",
"ts-node": "^10.9.1",
"typeorm": "^0.3.11",
"typescript": "^4.9.5",
"vitepress": "^1.0.0-alpha.46",
Expand Down
11 changes: 5 additions & 6 deletions src/data-source/options/singleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ export function setDataSourceOptions(
options: DataSourceOptions,
alias?: string,
) {
alias = alias || 'default';
instances[alias] = options;
instances[alias || 'default'] = options;
}

export function hasDataSourceOptions(alias?: string) : boolean {
alias = alias || 'default';

return Object.prototype.hasOwnProperty.call(instances, alias);
return Object.prototype.hasOwnProperty.call(instances, alias || 'default');
}

export async function useDataSourceOptions(alias?: string) : Promise<DataSourceOptions> {
Expand All @@ -29,7 +26,9 @@ export async function useDataSourceOptions(alias?: string) : Promise<DataSourceO
if (!Object.prototype.hasOwnProperty.call(instancePromises, alias)) {
instancePromises[alias] = buildDataSourceOptions()
.catch((e) => {
delete instancePromises[alias];
if (alias) {
delete instancePromises[alias];
}

throw e;
});
Expand Down
12 changes: 9 additions & 3 deletions src/data-source/singleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export async function useDataSource(alias?: string) : Promise<DataSource> {
if (!Object.prototype.hasOwnProperty.call(initializePromises, alias)) {
initializePromises[alias] = instances[alias].initialize()
.catch((e) => {
delete initializePromises[alias];
if (alias) {
delete initializePromises[alias];
}

throw e;
});
Expand All @@ -64,7 +66,9 @@ export async function useDataSource(alias?: string) : Promise<DataSource> {
if (!Object.prototype.hasOwnProperty.call(optionsPromises, alias)) {
optionsPromises[alias] = useDataSourceOptions(alias)
.catch((e) => {
delete optionsPromises[alias];
if (alias) {
delete optionsPromises[alias];
}

throw e;
});
Expand All @@ -78,7 +82,9 @@ export async function useDataSource(alias?: string) : Promise<DataSource> {
if (!Object.prototype.hasOwnProperty.call(initializePromises, alias)) {
initializePromises[alias] = dataSource.initialize()
.catch((e) => {
delete initializePromises[alias];
if (alias) {
delete initializePromises[alias];
}

throw e;
});
Expand Down
13 changes: 9 additions & 4 deletions src/database/create.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DriverError, OptionsError } from '../errors';
import {
DatabaseCreateContext,
} from './type';
Expand All @@ -9,21 +10,25 @@ import {
createPostgresDatabase,
createSQLiteDatabase,
} from './driver';
import { NotSupportedDriver } from './error';
import { buildDatabaseCreateContext } from './utils';

/**
* Create database for specified driver in ConnectionOptions.
*
* @throws NotSupportedDriver
* @throws DriverError
* @throws OptionsError
*
* @param context
*/
export async function createDatabase(context?: DatabaseCreateContext) {
context = await buildDatabaseCreateContext(context);

if (!context.options) {
throw OptionsError.undeterminable();
}

if (!context.options.type) {
throw new NotSupportedDriver(context.options.type);
throw DriverError.undeterminable();
}

switch (context.options.type) {
Expand All @@ -43,5 +48,5 @@ export async function createDatabase(context?: DatabaseCreateContext) {
return createMsSQLDatabase(context);
}

throw new NotSupportedDriver(context.options.type);
throw DriverError.notSupported(context.options.type);
}
7 changes: 7 additions & 0 deletions src/database/driver/cockroachdb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CockroachDriver } from 'typeorm/driver/cockroachdb/CockroachDriver';
import { OptionsError } from '../../errors';
import { DatabaseCreateContext, DatabaseDropContext } from '../type';
import { createSimplePostgresConnection } from './postgres';
import { buildDriverOptions, createDriver } from './utils';
Expand All @@ -24,6 +25,9 @@ export async function createCockroachDBDatabase(
context?: DatabaseCreateContext,
) {
context = await buildDatabaseCreateContext(context);
if (!context.options) {
throw OptionsError.undeterminable();
}

const options = buildDriverOptions(context.options);
const driver = createDriver(context.options) as CockroachDriver;
Expand Down Expand Up @@ -51,6 +55,9 @@ export async function dropCockroachDBDatabase(
context?: DatabaseDropContext,
) {
context = await buildDatabaseDropContext(context);
if (!context.options) {
throw OptionsError.undeterminable();
}

const options = buildDriverOptions(context.options);
const driver = createDriver(context.options) as CockroachDriver;
Expand Down
7 changes: 7 additions & 0 deletions src/database/driver/mssql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SqlServerDriver } from 'typeorm/driver/sqlserver/SqlServerDriver';
import { OptionsError } from '../../errors';
import { DatabaseCreateContext, DatabaseDropContext } from '../type';
import { DriverOptions } from './type';
import { buildDriverOptions, createDriver } from './utils';
Expand Down Expand Up @@ -26,6 +27,9 @@ export async function createMsSQLDatabase(
context?: DatabaseCreateContext,
) {
context = await buildDatabaseCreateContext(context);
if (!context.options) {
throw OptionsError.undeterminable();
}

const options = buildDriverOptions(context.options);
const driver = createDriver(context.options) as SqlServerDriver;
Expand Down Expand Up @@ -55,6 +59,9 @@ export async function dropMsSQLDatabase(
context?: DatabaseDropContext,
) {
context = await buildDatabaseDropContext(context);
if (!context.options) {
throw OptionsError.undeterminable();
}

const options = buildDriverOptions(context.options);
const driver = createDriver(context.options) as SqlServerDriver;
Expand Down
7 changes: 7 additions & 0 deletions src/database/driver/mysql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MysqlDriver } from 'typeorm/driver/mysql/MysqlDriver';
import { OptionsError } from '../../errors';
import { DatabaseCreateContext, DatabaseDropContext } from '../type';
import { DriverOptions } from './type';
import { buildDriverOptions, createDriver } from './utils';
Expand Down Expand Up @@ -43,6 +44,9 @@ export async function createMySQLDatabase(
context?: DatabaseCreateContext,
) {
context = await buildDatabaseCreateContext(context);
if (!context.options) {
throw OptionsError.undeterminable();
}

const options = buildDriverOptions(context.options);
const driver = createDriver(context.options) as MysqlDriver;
Expand Down Expand Up @@ -85,6 +89,9 @@ export async function dropMySQLDatabase(
context?: DatabaseDropContext,
) {
context = await buildDatabaseDropContext(context);
if (!context.options) {
throw OptionsError.undeterminable();
}

const options = buildDriverOptions(context.options);
const driver = createDriver(context.options) as MysqlDriver;
Expand Down
6 changes: 5 additions & 1 deletion src/database/driver/oracle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { OracleDriver } from 'typeorm/driver/oracle/OracleDriver';
import { OptionsError } from '../../errors';
import { DatabaseCreateContext, DatabaseDropContext } from '../type';
import { DriverOptions } from './type';
import { buildDriverOptions, createDriver } from './utils';
Expand Down Expand Up @@ -46,6 +47,9 @@ export async function createOracleDatabase(
context?: DatabaseCreateContext,
) {
context = await buildDatabaseCreateContext(context);
if (!context.options) {
throw OptionsError.undeterminable();
}

const options = buildDriverOptions(context.options);
const driver = createDriver(context.options) as OracleDriver;
Expand All @@ -66,7 +70,7 @@ export async function createOracleDatabase(
}

export async function dropOracleDatabase(
context?: DatabaseDropContext,
_context?: DatabaseDropContext,
) {
/**
* @link https://github.com/typeorm/typeorm/blob/master/src/driver/oracle/OracleQueryRunner.ts#L295
Expand Down
10 changes: 9 additions & 1 deletion src/database/driver/postgres.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { isObject } from 'locter';
import { PostgresDriver } from 'typeorm/driver/postgres/PostgresDriver';
import { CockroachDriver } from 'typeorm/driver/cockroachdb/CockroachDriver';
import { OptionsError } from '../../errors';
import { DatabaseBaseContext, DatabaseCreateContext, DatabaseDropContext } from '../type';
import { hasOwnProperty } from '../../utils';
import { DriverOptions } from './type';
Expand Down Expand Up @@ -56,6 +58,9 @@ export async function createPostgresDatabase(
context?: DatabaseCreateContext,
) {
context = await buildDatabaseCreateContext(context);
if (!context.options) {
throw OptionsError.undeterminable();
}

const options = buildDriverOptions(context.options);
const driver = createDriver(context.options) as PostgresDriver;
Expand All @@ -67,7 +72,7 @@ export async function createPostgresDatabase(
const existResult = await executeSimplePostgresQuery(connection, existQuery, false);

if (
typeof existResult === 'object' &&
isObject(existResult) &&
hasOwnProperty(existResult, 'rows') &&
Array.isArray(existResult.rows) &&
existResult.rows.length > 0
Expand Down Expand Up @@ -99,6 +104,9 @@ export async function dropPostgresDatabase(
context?: DatabaseDropContext,
) {
context = await buildDatabaseDropContext(context);
if (!context.options) {
throw OptionsError.undeterminable();
}

const options = buildDriverOptions(context.options);
const driver = createDriver(context.options) as PostgresDriver;
Expand Down
15 changes: 15 additions & 0 deletions src/database/driver/sqlite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';
import fs from 'fs';
import { OptionsError } from '../../errors';
import { DatabaseCreateContext, DatabaseDropContext } from '../type';
import { buildDriverOptions } from './utils';
import { buildDatabaseCreateContext, buildDatabaseDropContext, setupDatabaseSchema } from '../utils';
Expand All @@ -9,7 +10,14 @@ export async function createSQLiteDatabase(
) : Promise<void> {
context = await buildDatabaseCreateContext(context);

if (!context.options) {
throw OptionsError.undeterminable();
}

const options = buildDriverOptions(context.options);
if (!options.database) {
throw OptionsError.databaseNotDefined();
}

const filePath : string = path.isAbsolute(options.database) ?
options.database :
Expand All @@ -29,7 +37,14 @@ export async function dropSQLiteDatabase(
) {
context = await buildDatabaseDropContext(context);

if (!context.options) {
throw OptionsError.undeterminable();
}

const options = buildDriverOptions(context.options);
if (!options.database) {
throw OptionsError.databaseNotDefined();
}

const filePath : string = path.isAbsolute(options.database) ?
options.database :
Expand Down
13 changes: 9 additions & 4 deletions src/database/drop.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DriverError, OptionsError } from '../errors';
import {
DatabaseDropContext,
} from './type';
Expand All @@ -9,21 +10,25 @@ import {
dropPostgresDatabase,
dropSQLiteDatabase,
} from './driver';
import { NotSupportedDriver } from './error';
import { buildDatabaseDropContext } from './utils';

/**
* Drop database for specified driver in ConnectionOptions.
*
* @throws NotSupportedDriver
* @throws DriverError
* @throws OptionsError
*
* @param context
*/
export async function dropDatabase(context?: DatabaseDropContext) {
context = await buildDatabaseDropContext(context);

if (!context.options) {
throw OptionsError.undeterminable();
}

if (!context.options.type) {
throw new NotSupportedDriver(context.options.type);
throw DriverError.undeterminable();
}

switch (context.options.type) {
Expand All @@ -43,5 +48,5 @@ export async function dropDatabase(context?: DatabaseDropContext) {
return dropMsSQLDatabase(context);
}

throw new NotSupportedDriver(context.options.type);
throw DriverError.notSupported(context.options.type);
}
5 changes: 0 additions & 5 deletions src/database/error.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './error';
export * from './check';
export * from './create';
export * from './driver';
Expand Down
5 changes: 1 addition & 4 deletions src/database/utils/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ async function setDatabaseContextOptions<T extends DatabaseBaseContext>(context:
synchronize: false,
migrationsRun: false,
dropSchema: false,
logging: [
...(process.env.NODE_ENV !== 'test' ? ['query', 'error', 'schema'] : []),
],
} as DataSourceOptions);
} satisfies Partial<DataSourceOptions>);

return context;
}
Expand Down
3 changes: 2 additions & 1 deletion src/database/utils/query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* istanbul ignore next */
import { ObjectLiteral } from 'rapiq';
import { SelectQueryBuilder } from 'typeorm';

export function existsQuery<T>(builder: SelectQueryBuilder<T>, inverse = false) {
export function existsQuery<T extends ObjectLiteral = ObjectLiteral>(builder: SelectQueryBuilder<T>, inverse = false) {
return `${inverse ? 'not ' : ''}exists (${builder.getQuery()})`;
}
3 changes: 3 additions & 0 deletions src/errors/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class TypeormExtensionError extends Error {

}
Loading

0 comments on commit 64a2868

Please sign in to comment.