Skip to content

Commit e72d3e3

Browse files
author
Alexander Kharkovey
committed
fix(json-api-nestjs): get DataSource by name in ajv factory
1 parent 6c2eea1 commit e72d3e3

File tree

12 files changed

+154
-20
lines changed

12 files changed

+154
-20
lines changed

libs/json-api-nestjs/src/lib/constants/reflection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
export const JSON_API_DECORATOR_ENTITY = Symbol('JSON_API_ENTITY');
22
export const JSON_API_DECORATOR_OPTIONS = Symbol('JSON_API_OPTIONS');
3+
export const GLOBAL_MODULE_OPTIONS_TOKEN = Symbol('GLOBAL_MODULE_OPTIONS');
4+
export const CURRENT_DATA_SOURCE_TOKEN = Symbol('CURRENT_DATA_SOURCE_TOKEN');
35

46
export const PARAMS_RESOURCE_ID = 'id';
57
export const PARAMS_RELATION_ID = 'relId';
68
export const PARAMS_RELATION_NAME = 'relName';
79

810
export const JSON_API_CONFIG = 'JSON_API_CONFIG';
911

10-
export const GLOBAL_MODULE_OPTIONS_TOKEN = Symbol('GLOBAL_MODULE_OPTIONS');
11-
1212
export const JSON_API_SERVICE_POSTFIX = 'JsonApiService';
1313
export const CONFIG_PARAM_POSTFIX = 'JsonApiConfigParam';
1414
export const JSON_API_CONTROLLER_POSTFIX = 'JsonApiController';

libs/json-api-nestjs/src/lib/factory/ajv/ajv.factory.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import AjvCall from 'ajv';
22
import { FactoryProvider } from '@nestjs/common/interfaces/modules/provider.interface';
3-
import { getDataSourceToken } from '@nestjs/typeorm';
43
import { DataSource } from 'typeorm';
54
import { plainToClass } from 'class-transformer';
65

76
import { ModuleOptions } from '../../types';
8-
import { GLOBAL_MODULE_OPTIONS_TOKEN } from '../../constants';
7+
import {
8+
CURRENT_DATA_SOURCE_TOKEN,
9+
GLOBAL_MODULE_OPTIONS_TOKEN,
10+
} from '../../constants';
11+
912
import {
1013
inputQuerySchema,
1114
inputBodyPostSchema,
@@ -141,7 +144,10 @@ export const ajvFactory: FactoryProvider<AjvCall> = {
141144
provide: AjvCall,
142145
useFactory: AjvCallFactory,
143146
inject: [
144-
getDataSourceToken(),
147+
{
148+
token: CURRENT_DATA_SOURCE_TOKEN,
149+
optional: false,
150+
},
145151
{
146152
token: GLOBAL_MODULE_OPTIONS_TOKEN,
147153
optional: false,

libs/json-api-nestjs/src/lib/json-api-nestjs-common.module.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { DynamicModule, Module } from '@nestjs/common';
2-
import { TypeOrmModule } from '@nestjs/typeorm';
2+
import { getDataSourceToken, TypeOrmModule } from '@nestjs/typeorm';
3+
import { DataSource } from 'typeorm';
34

45
import { ModuleOptions } from '../lib/types';
5-
import { GLOBAL_MODULE_OPTIONS_TOKEN } from './constants';
6+
import {
7+
CURRENT_DATA_SOURCE_TOKEN,
8+
GLOBAL_MODULE_OPTIONS_TOKEN,
9+
} from './constants';
610
import { ajvFactory } from './factory';
711
import { ErrorInterceptors } from './mixin/interceptors';
812

@@ -14,6 +18,12 @@ export class JsonApiNestJsCommonModule {
1418
useValue: options,
1519
};
1620

21+
const currentDataSourceProvider = {
22+
provide: CURRENT_DATA_SOURCE_TOKEN,
23+
useFactory: (dataSource: DataSource) => dataSource,
24+
inject: [getDataSourceToken(options.connectionName)],
25+
};
26+
1727
const typeOrmModule = TypeOrmModule.forFeature(
1828
options.entities,
1929
options.connectionName
@@ -25,6 +35,7 @@ export class JsonApiNestJsCommonModule {
2535
...(options.providers || []),
2636
ajvFactory,
2737
optionProvider,
38+
currentDataSourceProvider,
2839
ErrorInterceptors,
2940
],
3041
exports: [

libs/json-api-nestjs/src/lib/mixin/pipes/body-input-patch/body-input-patch.pipe.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getDataSourceToken, getRepositoryToken } from '@nestjs/typeorm';
33
import { DataSource } from 'typeorm';
44

55
import {
6+
CURRENT_DATA_SOURCE_TOKEN,
67
DEFAULT_CONNECTION_NAME,
78
GLOBAL_MODULE_OPTIONS_TOKEN,
89
} from '../../../constants';
@@ -35,12 +36,22 @@ describe('BodyInputPatchPipe', () => {
3536
connectionName: DEFAULT_CONNECTION_NAME,
3637
},
3738
},
39+
{
40+
provide: CURRENT_DATA_SOURCE_TOKEN,
41+
useFactory: (dataSource: DataSource) => dataSource,
42+
inject: [getDataSourceToken(mockConnectionName)],
43+
},
3844
{
3945
provide: getRepositoryToken(Users, mockConnectionName),
4046
useFactory(dataSource: DataSource) {
4147
return dataSource.getRepository<Users>(Users);
4248
},
43-
inject: [getDataSourceToken()],
49+
inject: [
50+
{
51+
token: CURRENT_DATA_SOURCE_TOKEN,
52+
optional: false,
53+
},
54+
],
4455
},
4556
],
4657
}).compile();

libs/json-api-nestjs/src/lib/mixin/pipes/body-input-post/body-input-post.pipe.spec.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getDataSourceToken, getRepositoryToken } from '@nestjs/typeorm';
33
import { DataSource } from 'typeorm';
44

55
import {
6+
CURRENT_DATA_SOURCE_TOKEN,
67
DEFAULT_CONNECTION_NAME,
78
GLOBAL_MODULE_OPTIONS_TOKEN,
89
} from '../../../constants';
@@ -29,15 +30,25 @@ describe('BodyInputPostPipe', () => {
2930
provide: GLOBAL_MODULE_OPTIONS_TOKEN,
3031
useValue: {
3132
entities: entities,
32-
connectionName: DEFAULT_CONNECTION_NAME,
33+
connectionName: mockConnectionName,
3334
},
3435
},
36+
{
37+
provide: CURRENT_DATA_SOURCE_TOKEN,
38+
useFactory: (dataSource: DataSource) => dataSource,
39+
inject: [getDataSourceToken(mockConnectionName)],
40+
},
3541
{
3642
provide: getRepositoryToken(Users, mockConnectionName),
3743
useFactory(dataSource: DataSource) {
3844
return dataSource.getRepository<Users>(Users);
3945
},
40-
inject: [getDataSourceToken()],
46+
inject: [
47+
{
48+
token: CURRENT_DATA_SOURCE_TOKEN,
49+
optional: false,
50+
},
51+
],
4152
},
4253
],
4354
}).compile();

libs/json-api-nestjs/src/lib/mixin/pipes/body-relationship-patch/body-relationship-patch.pipe.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import { ajvFactory } from '../../../factory';
44
import { entities, mockDBTestModule, Users } from '../../../mock-utils';
55
import { BodyRelationshipPatchPipe } from './body-relationship-patch.pipe';
66
import {
7+
CURRENT_DATA_SOURCE_TOKEN,
78
DEFAULT_CONNECTION_NAME,
89
GLOBAL_MODULE_OPTIONS_TOKEN,
910
} from '../../../constants';
1011
import { BadRequestException } from '@nestjs/common';
12+
import { DataSource } from 'typeorm';
13+
import { getDataSourceToken, getRepositoryToken } from '@nestjs/typeorm';
1114

1215
describe('BodyRelationshipPatchPipe', () => {
1316
let pipe: BodyRelationshipPatchPipe<Users>;
@@ -25,6 +28,23 @@ describe('BodyRelationshipPatchPipe', () => {
2528
connectionName: DEFAULT_CONNECTION_NAME,
2629
},
2730
},
31+
{
32+
provide: CURRENT_DATA_SOURCE_TOKEN,
33+
useFactory: (dataSource: DataSource) => dataSource,
34+
inject: [getDataSourceToken(DEFAULT_CONNECTION_NAME)],
35+
},
36+
{
37+
provide: getRepositoryToken(Users, DEFAULT_CONNECTION_NAME),
38+
useFactory(dataSource: DataSource) {
39+
return dataSource.getRepository<Users>(Users);
40+
},
41+
inject: [
42+
{
43+
token: CURRENT_DATA_SOURCE_TOKEN,
44+
optional: false,
45+
},
46+
],
47+
},
2848
],
2949
}).compile();
3050

libs/json-api-nestjs/src/lib/mixin/pipes/body-relationship/body-relationship.pipe.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import { BodyRelationshipPipe } from './body-relationship.pipe';
55
import { ajvFactory } from '../../../factory';
66
import { entities, mockDBTestModule, Users } from '../../../mock-utils';
77
import {
8+
CURRENT_DATA_SOURCE_TOKEN,
89
DEFAULT_CONNECTION_NAME,
910
GLOBAL_MODULE_OPTIONS_TOKEN,
1011
} from '../../../constants';
12+
import { DataSource } from 'typeorm';
13+
import { getDataSourceToken, getRepositoryToken } from '@nestjs/typeorm';
1114

1215
describe('BodyRelationshipPipe', () => {
1316
let pipe: BodyRelationshipPipe<Users>;
@@ -25,6 +28,23 @@ describe('BodyRelationshipPipe', () => {
2528
connectionName: DEFAULT_CONNECTION_NAME,
2629
},
2730
},
31+
{
32+
provide: CURRENT_DATA_SOURCE_TOKEN,
33+
useFactory: (dataSource: DataSource) => dataSource,
34+
inject: [getDataSourceToken(DEFAULT_CONNECTION_NAME)],
35+
},
36+
{
37+
provide: getRepositoryToken(Users, DEFAULT_CONNECTION_NAME),
38+
useFactory(dataSource: DataSource) {
39+
return dataSource.getRepository<Users>(Users);
40+
},
41+
inject: [
42+
{
43+
token: CURRENT_DATA_SOURCE_TOKEN,
44+
optional: false,
45+
},
46+
],
47+
},
2848
],
2949
}).compile();
3050

libs/json-api-nestjs/src/lib/mixin/pipes/parse-relationship-name/parse-relationship-name.pipe.spec.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { BadRequestException } from '@nestjs/common';
55

66
import { entities, mockDBTestModule, Users } from '../../../mock-utils';
77
import {
8+
CURRENT_DATA_SOURCE_TOKEN,
89
DEFAULT_CONNECTION_NAME,
910
GLOBAL_MODULE_OPTIONS_TOKEN,
1011
} from '../../../constants';
@@ -34,11 +35,21 @@ describe('ParseRelationshipNamePipe', () => {
3435
},
3536
},
3637
{
37-
provide: getRepositoryToken(Users, mockConnectionName),
38+
provide: CURRENT_DATA_SOURCE_TOKEN,
39+
useFactory: (dataSource: DataSource) => dataSource,
40+
inject: [getDataSourceToken(DEFAULT_CONNECTION_NAME)],
41+
},
42+
{
43+
provide: getRepositoryToken(Users, DEFAULT_CONNECTION_NAME),
3844
useFactory(dataSource: DataSource) {
3945
return dataSource.getRepository<Users>(Users);
4046
},
41-
inject: [getDataSourceToken()],
47+
inject: [
48+
{
49+
token: CURRENT_DATA_SOURCE_TOKEN,
50+
optional: false,
51+
},
52+
],
4253
},
4354
],
4455
}).compile();

libs/json-api-nestjs/src/lib/mixin/pipes/query-filed-in-include/query-filed-in-include.pipe.spec.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
QueryParams,
1111
} from '../../../types';
1212
import {
13+
CURRENT_DATA_SOURCE_TOKEN,
1314
DEFAULT_CONNECTION_NAME,
1415
DEFAULT_PAGE_SIZE,
1516
DEFAULT_QUERY_PAGE,
@@ -45,11 +46,21 @@ describe('QueryFiledInIncludePipe', () => {
4546
providers: [
4647
querySchemaMixinPip,
4748
{
48-
provide: getRepositoryToken(Users, mockConnectionName),
49+
provide: CURRENT_DATA_SOURCE_TOKEN,
50+
useFactory: (dataSource: DataSource) => dataSource,
51+
inject: [getDataSourceToken(DEFAULT_CONNECTION_NAME)],
52+
},
53+
{
54+
provide: getRepositoryToken(Users, DEFAULT_CONNECTION_NAME),
4955
useFactory(dataSource: DataSource) {
5056
return dataSource.getRepository<Users>(Users);
5157
},
52-
inject: [getDataSourceToken()],
58+
inject: [
59+
{
60+
token: CURRENT_DATA_SOURCE_TOKEN,
61+
optional: false,
62+
},
63+
],
5364
},
5465
],
5566
}).compile();

libs/json-api-nestjs/src/lib/mixin/pipes/query-schema/query-schema.pipe.spec.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { querySchemaMixin } from '../';
1010
import { QuerySchemaTypes } from '../../../types';
1111
import { mockDBTestModule, entities, Users } from '../../../mock-utils';
1212
import {
13+
CURRENT_DATA_SOURCE_TOKEN,
1314
DEFAULT_CONNECTION_NAME,
1415
GLOBAL_MODULE_OPTIONS_TOKEN,
1516
} from '../../../constants';
@@ -33,11 +34,21 @@ describe('QuerySchema', () => {
3334
},
3435
},
3536
{
36-
provide: getRepositoryToken(Users, mockConnectionName),
37+
provide: CURRENT_DATA_SOURCE_TOKEN,
38+
useFactory: (dataSource: DataSource) => dataSource,
39+
inject: [getDataSourceToken(DEFAULT_CONNECTION_NAME)],
40+
},
41+
{
42+
provide: getRepositoryToken(Users, DEFAULT_CONNECTION_NAME),
3743
useFactory(dataSource: DataSource) {
3844
return dataSource.getRepository<Users>(Users);
3945
},
40-
inject: [getDataSourceToken()],
46+
inject: [
47+
{
48+
token: CURRENT_DATA_SOURCE_TOKEN,
49+
optional: false,
50+
},
51+
],
4152
},
4253
],
4354
}).compile();

0 commit comments

Comments
 (0)