@onivoro/server-typeorm-mysql 24.14.0 → 24.16.0
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/package.json +8 -2
- package/jest.config.ts +0 -11
- package/project.json +0 -33
- package/src/index.ts +0 -24
- package/src/lib/classes/__snapshots__/sql-writer.class.spec.ts.snap +0 -8
- package/src/lib/classes/type-orm-paging-repository.class.ts +0 -22
- package/src/lib/classes/type-orm-repository.class.ts +0 -152
- package/src/lib/constants/many-to-one-relation-options.constant.ts +0 -3
- package/src/lib/decorators/nullable-table-column.decorator.ts +0 -9
- package/src/lib/decorators/primary-table-column.decorator.ts +0 -9
- package/src/lib/decorators/table-column.decorator.ts +0 -9
- package/src/lib/decorators/table.decorator.ts +0 -8
- package/src/lib/functions/data-source-config-factory.function.ts +0 -33
- package/src/lib/functions/data-source-factory.function.ts +0 -10
- package/src/lib/functions/generate-date-query.function.ts +0 -20
- package/src/lib/functions/get-api-type-from-column.function.ts +0 -14
- package/src/lib/functions/get-paging-key.function.ts +0 -3
- package/src/lib/functions/get-skip.function.ts +0 -3
- package/src/lib/functions/remove-falsey-keys.function.ts +0 -8
- package/src/lib/server-typeorm-mysql.module.ts +0 -59
- package/src/lib/types/data-source-options.interface.ts +0 -10
- package/src/lib/types/entity-provider.interface.ts +0 -9
- package/src/lib/types/page-params.interface.ts +0 -6
- package/src/lib/types/paged-data.interface.ts +0 -6
- package/tsconfig.json +0 -16
- package/tsconfig.lib.json +0 -10
- package/tsconfig.spec.json +0 -21
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onivoro/server-typeorm-mysql",
|
|
3
|
-
"version": "24.
|
|
3
|
+
"version": "24.16.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -18,5 +18,11 @@
|
|
|
18
18
|
"reflect-metadata": "~0.2.2",
|
|
19
19
|
"typeorm": "^0.3.22",
|
|
20
20
|
"typeorm-naming-strategies": "~4.1.0"
|
|
21
|
-
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"**/*.js",
|
|
24
|
+
"**/*.d.ts",
|
|
25
|
+
"**/*.js.map",
|
|
26
|
+
"README.md"
|
|
27
|
+
]
|
|
22
28
|
}
|
package/jest.config.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
export default {
|
|
3
|
-
displayName: 'lib-server-typeorm-mysql',
|
|
4
|
-
preset: '../../../jest.preset.js',
|
|
5
|
-
testEnvironment: 'node',
|
|
6
|
-
transform: {
|
|
7
|
-
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
|
|
8
|
-
},
|
|
9
|
-
moduleFileExtensions: ['ts', 'js', 'html'],
|
|
10
|
-
coverageDirectory: '../../../coverage/libs/server/typeorm-mysql',
|
|
11
|
-
};
|
package/project.json
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "lib-server-typeorm-mysql",
|
|
3
|
-
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
-
"sourceRoot": "libs/server/typeorm-mysql/src",
|
|
5
|
-
"projectType": "library",
|
|
6
|
-
"targets": {
|
|
7
|
-
"build": {
|
|
8
|
-
"executor": "@nx/js:tsc",
|
|
9
|
-
"outputs": ["{options.outputPath}"],
|
|
10
|
-
"options": {
|
|
11
|
-
"outputPath": "dist/libs/server/typeorm-mysql",
|
|
12
|
-
"main": "libs/server/typeorm-mysql/src/index.ts",
|
|
13
|
-
"tsConfig": "libs/server/typeorm-mysql/tsconfig.lib.json",
|
|
14
|
-
"assets": [
|
|
15
|
-
"libs/server/typeorm-mysql/README.md",
|
|
16
|
-
"libs/server/typeorm-mysql/package.json"
|
|
17
|
-
],
|
|
18
|
-
"declaration": true,
|
|
19
|
-
"updateBuildableProjectPackageJsonDependencies": true,
|
|
20
|
-
"rootDir": "libs/server/typeorm-mysql/src"
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
"publish": {
|
|
24
|
-
"executor": "@nx/js:npm",
|
|
25
|
-
"outputs": [],
|
|
26
|
-
"dependsOn": ["build"],
|
|
27
|
-
"options": {
|
|
28
|
-
"packageRoot": "dist/libs/server/typeorm-mysql"
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
"tags": []
|
|
33
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export * from './lib/classes/type-orm-paging-repository.class';
|
|
2
|
-
export * from './lib/classes/type-orm-repository.class';
|
|
3
|
-
|
|
4
|
-
export * from './lib/constants/many-to-one-relation-options.constant';
|
|
5
|
-
|
|
6
|
-
export * from './lib/decorators/nullable-table-column.decorator';
|
|
7
|
-
export * from './lib/decorators/primary-table-column.decorator';
|
|
8
|
-
export * from './lib/decorators/table-column.decorator';
|
|
9
|
-
export * from './lib/decorators/table.decorator';
|
|
10
|
-
|
|
11
|
-
export * from './lib/functions/data-source-config-factory.function';
|
|
12
|
-
export * from './lib/functions/data-source-factory.function';
|
|
13
|
-
export * from './lib/functions/generate-date-query.function';
|
|
14
|
-
export * from './lib/functions/get-api-type-from-column.function';
|
|
15
|
-
export * from './lib/functions/get-paging-key.function';
|
|
16
|
-
export * from './lib/functions/get-skip.function';
|
|
17
|
-
export * from './lib/functions/remove-falsey-keys.function';
|
|
18
|
-
|
|
19
|
-
export * from './lib/types/data-source-options.interface';
|
|
20
|
-
export * from './lib/types/entity-provider.interface';
|
|
21
|
-
export * from './lib/types/page-params.interface';
|
|
22
|
-
export * from './lib/types/paged-data.interface';
|
|
23
|
-
|
|
24
|
-
export * from './lib/server-typeorm-mysql.module';
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
// Bun Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
-
|
|
3
|
-
exports[`SqlWriter addColumn worx 1`] = `"ALTER TABLE "blah" ADD "id" character varying PRIMARY KEY "`;
|
|
4
|
-
|
|
5
|
-
exports[`SqlWriter createTable worx 1`] = `
|
|
6
|
-
"CREATE TABLE "blah" ("id" character varying PRIMARY KEY ,
|
|
7
|
-
"active" boolean NOT NULL );"
|
|
8
|
-
`;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
EntityManager,
|
|
3
|
-
} from 'typeorm';
|
|
4
|
-
|
|
5
|
-
import { TypeOrmRepository } from './type-orm-repository.class';
|
|
6
|
-
import { IPagedData } from '../types/paged-data.interface';
|
|
7
|
-
import { getSkip } from '../functions/get-skip.function';
|
|
8
|
-
import { removeFalseyKeys } from '../functions/remove-falsey-keys.function';
|
|
9
|
-
import { getPagingKey } from '../functions/get-paging-key.function';
|
|
10
|
-
import { IPageParams } from '../types/page-params.interface';
|
|
11
|
-
|
|
12
|
-
export abstract class TypeOrmPagingRepository<TEntity, TEntityParams> extends TypeOrmRepository<TEntity> {
|
|
13
|
-
protected getPagingKey = getPagingKey;
|
|
14
|
-
protected getSkip = getSkip;
|
|
15
|
-
protected removeFalseyKeys = removeFalseyKeys;
|
|
16
|
-
|
|
17
|
-
constructor(entityType: any, entityManager: EntityManager) {
|
|
18
|
-
super(entityType, entityManager);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
abstract getPage(pageParams: IPageParams, params: TEntityParams): Promise<IPagedData<TEntity>>;
|
|
22
|
-
}
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
EntityManager,
|
|
3
|
-
FindManyOptions,
|
|
4
|
-
FindOneOptions,
|
|
5
|
-
FindOptionsWhere,
|
|
6
|
-
ILike,
|
|
7
|
-
QueryRunner,
|
|
8
|
-
} from 'typeorm';
|
|
9
|
-
|
|
10
|
-
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';
|
|
11
|
-
import { IEntityProvider } from '../types/entity-provider.interface';
|
|
12
|
-
import { BadRequestException } from '@nestjs/common';
|
|
13
|
-
import { ReadStream } from 'fs';
|
|
14
|
-
|
|
15
|
-
export type TQueryStreamParams<TRecord = any> = {
|
|
16
|
-
query: string,
|
|
17
|
-
onData?: (stream: ReadStream, record: TRecord, count: number) => Promise<any | void>,
|
|
18
|
-
onError?: (stream: ReadStream, error: any) => Promise<any | void>,
|
|
19
|
-
onEnd?: (stream: ReadStream, count: number) => Promise<any | void>,
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export class TypeOrmRepository<TEntity> implements IEntityProvider<
|
|
23
|
-
TEntity,
|
|
24
|
-
FindOneOptions<TEntity>,
|
|
25
|
-
FindManyOptions<TEntity>,
|
|
26
|
-
FindOptionsWhere<TEntity>,
|
|
27
|
-
QueryDeepPartialEntity<TEntity>
|
|
28
|
-
> {
|
|
29
|
-
constructor(private entityType: any, public entityManager: EntityManager) { }
|
|
30
|
-
|
|
31
|
-
forTransaction(entityManager: EntityManager): TypeOrmRepository<TEntity> {
|
|
32
|
-
return {...this, entityManager};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async getMany(options: FindManyOptions<TEntity>): Promise<TEntity[]> {
|
|
36
|
-
return await (this.repo.find as any)(options);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
async getManyAndCount(options: FindManyOptions<TEntity>): Promise<[TEntity[], number]> {
|
|
40
|
-
return await (this.repo.findAndCount as any)(options);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async getOne(options: FindOneOptions<TEntity>): Promise<TEntity> {
|
|
44
|
-
const results = await this.getMany(options);
|
|
45
|
-
|
|
46
|
-
if (results?.length > 1) {
|
|
47
|
-
throw new Error(`${TypeOrmRepository.prototype.getOne.name} expects only 1 result but found ${results.length} results of entity type "${this.entityType}" for criteria ${JSON.stringify(options, null, 2)}`);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return results[0];
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async postOne(body: Partial<TEntity>): Promise<TEntity> {
|
|
54
|
-
return await this.insertAndReturn(body as TEntity);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async postMany(body: Partial<TEntity | undefined>[]): Promise<TEntity[]> {
|
|
58
|
-
return await this.insertAndReturnMany(body as TEntity[]);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async delete(options: FindOptionsWhere<TEntity>): Promise<void> {
|
|
62
|
-
return await (this.repo.delete as any)(options);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
async softDelete(options: FindOptionsWhere<TEntity>): Promise<void> {
|
|
66
|
-
return await (this.repo.softDelete as any)(options);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async put(options: FindOptionsWhere<TEntity>, body: QueryDeepPartialEntity<TEntity>) {
|
|
70
|
-
await this.repo.save(options, body);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async patch(options: FindOptionsWhere<TEntity>, body: QueryDeepPartialEntity<TEntity>) {
|
|
74
|
-
await this.repo.update(options, body);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
get repo() {
|
|
78
|
-
return this.entityManager.getRepository(this.entityType as any);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
protected async insertAndReturn(entityToInsert: TEntity): Promise<TEntity> {
|
|
82
|
-
return (await this.insertAndReturnMany([entityToInsert]))[0];
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
protected async insertAndReturnMany(entitiesToInsert: TEntity[]): Promise<TEntity[]> {
|
|
86
|
-
const insertionResult = await this.repo
|
|
87
|
-
.createQueryBuilder()
|
|
88
|
-
.insert()
|
|
89
|
-
.values(entitiesToInsert)
|
|
90
|
-
.returning('*')
|
|
91
|
-
.execute();
|
|
92
|
-
|
|
93
|
-
const insertedEntity: TEntity[] =
|
|
94
|
-
insertionResult.generatedMaps as TEntity[];
|
|
95
|
-
|
|
96
|
-
return insertedEntity;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
static async queryStream<TRecord = any>(queryRunner: QueryRunner, _: TQueryStreamParams) {
|
|
100
|
-
if (!_.query) {
|
|
101
|
-
throw new BadRequestException(`StreamingQueryRunner requires one of: {query, table}`);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
let processedCount = 0;
|
|
105
|
-
const query = _.query;
|
|
106
|
-
|
|
107
|
-
try {
|
|
108
|
-
const stream = await queryRunner.stream(query);
|
|
109
|
-
|
|
110
|
-
stream.on('data', (record: TRecord) => {
|
|
111
|
-
_.onData?.(stream, record, processedCount++);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
stream.on('error', (error: Error) => {
|
|
115
|
-
console.error({ detail: `Error processing stream for query "${_.query}"`, error });
|
|
116
|
-
_.onError?.(stream, error);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
stream.on('end', () => {
|
|
120
|
-
console.log({ detail: `Finished processing stream for query "${_.query}"`, processedCount });
|
|
121
|
-
_.onEnd?.(stream, processedCount);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
return { stream, error: null };
|
|
125
|
-
} catch (error: any) {
|
|
126
|
-
console.error({ detail: `Error processing stream for query "${_.query}"`, error });
|
|
127
|
-
return { stream: null, error };
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
async queryStream<TRecord = any>(_: TQueryStreamParams) {
|
|
132
|
-
const queryRunner = this.entityManager.connection.createQueryRunner();
|
|
133
|
-
return await TypeOrmRepository.queryStream<TRecord>(queryRunner, _);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
buildWhereILike(filters?: Record<string, any>): FindOptionsWhere<TEntity> {
|
|
137
|
-
|
|
138
|
-
if(!filters) {
|
|
139
|
-
return {};
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return Object.entries(filters || {})
|
|
143
|
-
.reduce(
|
|
144
|
-
(_, [column, filter]) => (
|
|
145
|
-
filter
|
|
146
|
-
? { ..._, [column]: ILike(`%${filter}%`) }
|
|
147
|
-
: _
|
|
148
|
-
),
|
|
149
|
-
{}
|
|
150
|
-
) as any;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { applyDecorators } from "@nestjs/common";
|
|
2
|
-
import { Column, ColumnOptions } from "typeorm";
|
|
3
|
-
import { ApiPropertyOptional } from '@nestjs/swagger';
|
|
4
|
-
import { getApiTypeFromColumn } from "../functions/get-api-type-from-column.function";
|
|
5
|
-
|
|
6
|
-
export const NullableTableColumn = (options?: Pick<ColumnOptions, 'type'>) => {
|
|
7
|
-
const apiType = getApiTypeFromColumn(options?.type);
|
|
8
|
-
return applyDecorators(Column({ type: options?.type, nullable: true }), ApiPropertyOptional({type: apiType}));
|
|
9
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { applyDecorators } from "@nestjs/common";
|
|
2
|
-
import { Column, ColumnOptions } from "typeorm";
|
|
3
|
-
import { ApiProperty } from '@nestjs/swagger';
|
|
4
|
-
import { getApiTypeFromColumn } from "../functions/get-api-type-from-column.function";
|
|
5
|
-
|
|
6
|
-
export const PrimaryTableColumn = (options?: Pick<ColumnOptions, 'type'>) => {
|
|
7
|
-
const apiType = getApiTypeFromColumn(options?.type);
|
|
8
|
-
return applyDecorators(Column({ type: options?.type, nullable: false }), ApiProperty({type: apiType}));
|
|
9
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { applyDecorators } from "@nestjs/common";
|
|
2
|
-
import { Column, ColumnOptions } from "typeorm";
|
|
3
|
-
import { ApiProperty } from '@nestjs/swagger';
|
|
4
|
-
import { getApiTypeFromColumn } from "../functions/get-api-type-from-column.function";
|
|
5
|
-
|
|
6
|
-
export const TableColumn = (options?: Pick<ColumnOptions, 'type'>) => {
|
|
7
|
-
const apiType = getApiTypeFromColumn(options?.type);
|
|
8
|
-
return applyDecorators(Column({ type: options?.type, nullable: false }), ApiProperty({type: apiType}));
|
|
9
|
-
};
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { applyDecorators } from "@nestjs/common";
|
|
2
|
-
import { Entity } from "typeorm";
|
|
3
|
-
import { snakeCase } from '@onivoro/isomorphic-common';
|
|
4
|
-
|
|
5
|
-
export const Table = (EntityClass: { name: string }) => {
|
|
6
|
-
const tableName = snakeCase(EntityClass.name);
|
|
7
|
-
return applyDecorators(Entity(tableName));
|
|
8
|
-
};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { SnakeNamingStrategy } from 'typeorm-naming-strategies';
|
|
2
|
-
import { MysqlConnectionOptions } from 'typeorm/driver/mysql/MysqlConnectionOptions';
|
|
3
|
-
import { IDataSourceOptions } from '../types/data-source-options.interface';
|
|
4
|
-
|
|
5
|
-
export function dataSourceConfigFactory(
|
|
6
|
-
name: string,
|
|
7
|
-
options: IDataSourceOptions,
|
|
8
|
-
entities: any[]
|
|
9
|
-
): MysqlConnectionOptions {
|
|
10
|
-
|
|
11
|
-
const {
|
|
12
|
-
ca, database, host, password, port, username, synchronize = false, logging = false
|
|
13
|
-
} = options;
|
|
14
|
-
|
|
15
|
-
const config: MysqlConnectionOptions = {
|
|
16
|
-
name,
|
|
17
|
-
type: 'mysql',
|
|
18
|
-
host,
|
|
19
|
-
port: port as any,
|
|
20
|
-
username,
|
|
21
|
-
password,
|
|
22
|
-
ssl: ca ? { ca } : undefined,
|
|
23
|
-
database,
|
|
24
|
-
synchronize,
|
|
25
|
-
logging,
|
|
26
|
-
entities,
|
|
27
|
-
subscribers: [],
|
|
28
|
-
migrations: [],
|
|
29
|
-
namingStrategy: new SnakeNamingStrategy(),
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
return config;
|
|
33
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { DataSource } from 'typeorm';
|
|
2
|
-
import { dataSourceConfigFactory } from './data-source-config-factory.function';
|
|
3
|
-
import { IDataSourceOptions } from '../types/data-source-options.interface';
|
|
4
|
-
|
|
5
|
-
export const dataSourceFactory = (
|
|
6
|
-
name: string,
|
|
7
|
-
options: IDataSourceOptions,
|
|
8
|
-
entities: any[]
|
|
9
|
-
) =>
|
|
10
|
-
new DataSource(dataSourceConfigFactory(name, options, entities));
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Between, FindOperator, LessThanOrEqual, MoreThanOrEqual } from "typeorm";
|
|
2
|
-
|
|
3
|
-
export function generateDateQuery(
|
|
4
|
-
minDueDate: string,
|
|
5
|
-
maxDueDate: string
|
|
6
|
-
): FindOperator<Date> {
|
|
7
|
-
if (minDueDate && maxDueDate) {
|
|
8
|
-
return Between(minDueDate as any, maxDueDate as any);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
if (maxDueDate) {
|
|
12
|
-
return LessThanOrEqual(new Date(maxDueDate));
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (minDueDate) {
|
|
16
|
-
return MoreThanOrEqual(new Date(minDueDate));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return undefined as any;
|
|
20
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { ColumnOptions } from "typeorm";
|
|
2
|
-
|
|
3
|
-
export function getApiTypeFromColumn(columnType: ColumnOptions['type']) {
|
|
4
|
-
if(!columnType) {
|
|
5
|
-
return 'string';
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
switch (columnType) {
|
|
9
|
-
case 'boolean':
|
|
10
|
-
return columnType;
|
|
11
|
-
default:
|
|
12
|
-
return 'string';
|
|
13
|
-
}
|
|
14
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { DynamicModule, Module, OnApplicationShutdown } from '@nestjs/common';
|
|
2
|
-
import { DataSource, EntityManager } from 'typeorm';
|
|
3
|
-
import { dataSourceFactory } from './functions/data-source-factory.function';
|
|
4
|
-
import { IDataSourceOptions } from './types/data-source-options.interface';
|
|
5
|
-
|
|
6
|
-
const dataSourceMap = new Map<string, DataSource>();
|
|
7
|
-
|
|
8
|
-
@Module({})
|
|
9
|
-
export class ServerTypeormMysqlModule implements OnApplicationShutdown {
|
|
10
|
-
|
|
11
|
-
async onApplicationShutdown(): Promise<void> {
|
|
12
|
-
for (let dataSourceKey in dataSourceMap.keys()) {
|
|
13
|
-
try {
|
|
14
|
-
const dataSource = dataSourceMap.get(dataSourceKey);
|
|
15
|
-
if (dataSource && dataSource.isInitialized) {
|
|
16
|
-
console.log(`destroying connection ${dataSourceKey}`);
|
|
17
|
-
await dataSource.destroy();
|
|
18
|
-
}
|
|
19
|
-
} catch (e: any) {
|
|
20
|
-
console.error(e?.message || e);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
static configure(
|
|
26
|
-
injectables: any[],
|
|
27
|
-
entities: any[],
|
|
28
|
-
options: IDataSourceOptions,
|
|
29
|
-
name = 'default'
|
|
30
|
-
): DynamicModule {
|
|
31
|
-
const providers = [
|
|
32
|
-
{
|
|
33
|
-
provide: DataSource,
|
|
34
|
-
useFactory: async () => {
|
|
35
|
-
const cachedDataSource = dataSourceMap.get(name);
|
|
36
|
-
if (!cachedDataSource) {
|
|
37
|
-
const dataSource: DataSource = dataSourceFactory(name, options, entities);
|
|
38
|
-
await dataSource.initialize();
|
|
39
|
-
dataSourceMap.set(name, dataSource);
|
|
40
|
-
}
|
|
41
|
-
return dataSourceMap.get(name);
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
provide: EntityManager,
|
|
46
|
-
useFactory: (dataSource: DataSource) => {
|
|
47
|
-
return dataSource?.manager as any;
|
|
48
|
-
},
|
|
49
|
-
inject: [DataSource]
|
|
50
|
-
},
|
|
51
|
-
...injectables,
|
|
52
|
-
];
|
|
53
|
-
return {
|
|
54
|
-
module: ServerTypeormMysqlModule,
|
|
55
|
-
exports: providers,
|
|
56
|
-
providers,
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export interface IEntityProvider<TEntity, TFindOneOptions, TFindManyOptions, TFindOptionsWhere, TQueryDeepPartialEntity> {
|
|
2
|
-
getOne: (options: TFindOneOptions) => Promise<TEntity>;
|
|
3
|
-
getMany: (options: TFindManyOptions) => Promise<TEntity[]>;
|
|
4
|
-
postOne: (body: Partial<TEntity>) => Promise<TEntity>;
|
|
5
|
-
postMany: (body: Partial<TEntity[]>) => Promise<TEntity[]>;
|
|
6
|
-
delete: (options: TFindOptionsWhere) => Promise<void>;
|
|
7
|
-
put: (options: TFindOptionsWhere, body: TQueryDeepPartialEntity) => Promise<void>;
|
|
8
|
-
patch: (options: TFindOptionsWhere, body: TQueryDeepPartialEntity) => Promise<void>;
|
|
9
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../../tsconfig.server.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "../../../dist/out-tsc"
|
|
5
|
-
},
|
|
6
|
-
"files": [],
|
|
7
|
-
"include": [],
|
|
8
|
-
"references": [
|
|
9
|
-
{
|
|
10
|
-
"path": "./tsconfig.lib.json"
|
|
11
|
-
},
|
|
12
|
-
{
|
|
13
|
-
"path": "./tsconfig.spec.json"
|
|
14
|
-
}
|
|
15
|
-
]
|
|
16
|
-
}
|
package/tsconfig.lib.json
DELETED
package/tsconfig.spec.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"types": [
|
|
5
|
-
"jest",
|
|
6
|
-
"node"
|
|
7
|
-
]
|
|
8
|
-
},
|
|
9
|
-
"include": [
|
|
10
|
-
"jest.config.ts",
|
|
11
|
-
"**/*.test.ts",
|
|
12
|
-
"**/*.spec.ts",
|
|
13
|
-
"**/*.test.tsx",
|
|
14
|
-
"**/*.spec.tsx",
|
|
15
|
-
"**/*.test.js",
|
|
16
|
-
"**/*.spec.js",
|
|
17
|
-
"**/*.test.jsx",
|
|
18
|
-
"**/*.spec.jsx",
|
|
19
|
-
"**/*.d.ts"
|
|
20
|
-
]
|
|
21
|
-
}
|