mm-share-lib 0.0.4 → 0.0.5
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/.eslintrc.js +25 -25
- package/.prettierrc +3 -3
- package/README.md +73 -73
- package/dist/src/exception/bad-request-error.exception.d.ts +4 -0
- package/dist/src/exception/bad-request-error.exception.js +12 -0
- package/dist/src/exception/bad-request-error.exception.js.map +1 -0
- package/dist/src/exception/base.exception.d.ts +21 -0
- package/dist/src/exception/base.exception.js +16 -0
- package/dist/src/exception/base.exception.js.map +1 -0
- package/dist/src/exception/conflict-error.exception.d.ts +4 -0
- package/dist/src/exception/conflict-error.exception.js +12 -0
- package/dist/src/exception/conflict-error.exception.js.map +1 -0
- package/dist/src/exception/forbidden-error.exception.d.ts +4 -0
- package/dist/src/exception/forbidden-error.exception.js +12 -0
- package/dist/src/exception/forbidden-error.exception.js.map +1 -0
- package/dist/src/exception/index.d.ts +7 -0
- package/dist/src/exception/index.js +23 -0
- package/dist/src/exception/index.js.map +1 -1
- package/dist/src/exception/internal-server-error.exception.d.ts +4 -0
- package/dist/src/exception/internal-server-error.exception.js +12 -0
- package/dist/src/exception/internal-server-error.exception.js.map +1 -0
- package/dist/src/exception/not-found-error.exception.d.ts +4 -0
- package/dist/src/exception/not-found-error.exception.js +12 -0
- package/dist/src/exception/not-found-error.exception.js.map +1 -0
- package/dist/src/exception/unauthorized-error.exception.d.ts +4 -0
- package/dist/src/exception/unauthorized-error.exception.js +12 -0
- package/dist/src/exception/unauthorized-error.exception.js.map +1 -0
- package/dist/src/filter/http-exception.filter.d.ts +4 -0
- package/dist/src/filter/http-exception.filter.js +27 -0
- package/dist/src/filter/http-exception.filter.js.map +1 -0
- package/dist/src/filter/index.d.ts +1 -0
- package/dist/src/filter/index.js +18 -0
- package/dist/src/filter/index.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +2 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/setup/index.d.ts +1 -0
- package/dist/src/setup/index.js +18 -0
- package/dist/src/setup/index.js.map +1 -0
- package/dist/src/setup/microservice.setup.d.ts +2 -0
- package/dist/src/setup/microservice.setup.js +25 -0
- package/dist/src/setup/microservice.setup.js.map +1 -0
- package/dist/src/setup/swagger.setup.d.ts +0 -0
- package/dist/src/setup/swagger.setup.js +1 -0
- package/dist/src/setup/swagger.setup.js.map +1 -0
- package/dist/src/util/app.utils.d.ts +4 -0
- package/dist/src/util/app.utils.js +21 -0
- package/dist/src/util/app.utils.js.map +1 -0
- package/dist/src/util/index.d.ts +1 -0
- package/dist/src/util/index.js +1 -0
- package/dist/src/util/index.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/index.ts +1 -1
- package/nest-cli.json +8 -8
- package/package.json +82 -81
- package/src/constant/entity-state.constant.ts +4 -4
- package/src/constant/index.ts +1 -1
- package/src/dto/base-filter.dto.ts +4 -4
- package/src/dto/index.ts +2 -2
- package/src/dto/pagination.dto.ts +4 -4
- package/src/exception/bad-request-error.exception.spec.ts +24 -0
- package/src/exception/bad-request-error.exception.ts +8 -0
- package/src/exception/base.exception.ts +36 -0
- package/src/exception/conflict-error.exception.spec.ts +23 -0
- package/src/exception/conflict-error.exception.ts +8 -0
- package/src/exception/forbidden-error.exception.spec.ts +23 -0
- package/src/exception/forbidden-error.exception.ts +8 -0
- package/src/exception/index.ts +7 -0
- package/src/exception/internal-server-error.exception.spec.ts +23 -0
- package/src/exception/internal-server-error.exception.ts +12 -0
- package/src/exception/not-found-error.exception.spec.ts +23 -0
- package/src/exception/not-found-error.exception.ts +8 -0
- package/src/exception/unauthorized-error.exception.spec.ts +23 -0
- package/src/exception/unauthorized-error.exception.ts +12 -0
- package/src/filter/http-exception.filter.ts +23 -0
- package/src/filter/index.ts +1 -0
- package/src/generic/entity/entity.generic.ts +34 -34
- package/src/generic/entity/index.ts +1 -1
- package/src/generic/index.ts +2 -2
- package/src/generic/service/index.ts +1 -1
- package/src/generic/service/service.generic.ts +112 -112
- package/src/index.ts +2 -0
- package/src/lib/index.ts +1 -1
- package/src/lib/search-engine/document/base.document.ts +2 -2
- package/src/lib/search-engine/index.ts +1 -1
- package/src/lib/search-engine/interface/index.ts +1 -1
- package/src/lib/search-engine/interface/search-document.interface.ts +5 -5
- package/src/lib/search-engine/interface/transform-service.interface.ts +10 -10
- package/src/lib/search-engine/typesense/index.ts +3 -3
- package/src/lib/search-engine/typesense/metadata/index.ts +1 -1
- package/src/lib/search-engine/typesense/metadata/schema.metadata.ts +13 -13
- package/src/lib/search-engine/typesense/metadata/typesense.metadata-registry.ts +28 -28
- package/src/lib/search-engine/typesense/service/client.service.ts +258 -258
- package/src/lib/search-engine/typesense/typesense-module.interface.ts +36 -36
- package/src/lib/search-engine/typesense/typesense.constants.ts +1 -1
- package/src/lib/search-engine/typesense/typesense.module.test.ts +94 -94
- package/src/lib/search-engine/typesense/typesense.module.ts +76 -76
- package/src/lib/search-engine/typesense/typesense.providers.ts +42 -42
- package/src/response/index.ts +1 -1
- package/src/response/pagination.response.ts +37 -37
- package/src/setup/index.ts +1 -0
- package/src/setup/microservice.setup.ts +29 -0
- package/src/setup/swagger.setup.ts +0 -0
- package/src/util/app.utils.ts +18 -0
- package/src/util/date.util.spec.ts +49 -49
- package/src/util/date.util.ts +10 -10
- package/src/util/generator.util.spec.ts +79 -79
- package/src/util/generator.util.ts +34 -34
- package/src/util/index.ts +3 -2
- package/test/app.e2e-spec.ts +24 -24
- package/test/jest-e2e.json +9 -9
- package/tsconfig.build.json +4 -4
- package/tsconfig.json +21 -21
@@ -1,112 +1,112 @@
|
|
1
|
-
import {
|
2
|
-
Repository,
|
3
|
-
Connection,
|
4
|
-
EntityManager,
|
5
|
-
SelectQueryBuilder,
|
6
|
-
} from 'typeorm';
|
7
|
-
import { EntityGeneric } from '../entity';
|
8
|
-
import { PaginationDto, BaseFilterDto } from '../../dto';
|
9
|
-
import { EntityStateConstant } from '../../constant';
|
10
|
-
import { PaginationResponse } from '../../response';
|
11
|
-
|
12
|
-
export abstract class ServiceGeneric<
|
13
|
-
Entity extends EntityGeneric,
|
14
|
-
CustomRepository extends Repository<Entity>,
|
15
|
-
> {
|
16
|
-
protected readonly entityName: string;
|
17
|
-
protected readonly loggable: boolean = false;
|
18
|
-
protected repository: CustomRepository;
|
19
|
-
protected connection: Connection;
|
20
|
-
constructor(
|
21
|
-
protected readonly connectionOrManager: Connection | EntityManager,
|
22
|
-
repositoryType: { new (connection: Connection): CustomRepository },
|
23
|
-
) {
|
24
|
-
if (connectionOrManager instanceof EntityManager) {
|
25
|
-
this.connection = connectionOrManager.connection;
|
26
|
-
} else {
|
27
|
-
this.connection = connectionOrManager;
|
28
|
-
}
|
29
|
-
this.repository = this.connection.getCustomRepository(repositoryType);
|
30
|
-
}
|
31
|
-
|
32
|
-
async create(entity: Entity): Promise<Entity> {
|
33
|
-
return this.repository.save(entity);
|
34
|
-
}
|
35
|
-
|
36
|
-
async update(entity: Entity): Promise<Entity> {
|
37
|
-
return this.repository.save(entity);
|
38
|
-
}
|
39
|
-
|
40
|
-
async getListWithPagination(
|
41
|
-
paginationDto: PaginationDto,
|
42
|
-
callback?: (query: SelectQueryBuilder<Entity>) => void,
|
43
|
-
): Promise<PaginationResponse<Entity>> {
|
44
|
-
const { limit = 25, offset = 0 } = paginationDto;
|
45
|
-
const query = this.repository.createQueryBuilder(this.entityName);
|
46
|
-
query.limit(limit);
|
47
|
-
query.offset(offset);
|
48
|
-
query.where(`${this.entityName}.state != :state`, {
|
49
|
-
state: EntityStateConstant.Archived,
|
50
|
-
});
|
51
|
-
const defaultSelectable = ['createdAt', 'updatedAt'];
|
52
|
-
query.orderBy(`${this.entityName}.updatedAt`, 'DESC');
|
53
|
-
const selection = defaultSelectable.map(
|
54
|
-
(column: string) => `${this.entityName}.${column}`,
|
55
|
-
);
|
56
|
-
query.addSelect(selection);
|
57
|
-
if (callback != null) {
|
58
|
-
callback(query);
|
59
|
-
}
|
60
|
-
const entities = await query.getMany();
|
61
|
-
const total = await query.getCount();
|
62
|
-
const response = new PaginationResponse(entities, total, limit, offset);
|
63
|
-
return response;
|
64
|
-
}
|
65
|
-
|
66
|
-
async getAutocompleteWithPagination(
|
67
|
-
paginationDto: PaginationDto,
|
68
|
-
filter: BaseFilterDto,
|
69
|
-
callback?: (query: SelectQueryBuilder<Entity>) => void,
|
70
|
-
): Promise<PaginationResponse<Entity>> {
|
71
|
-
const { limit = 25, offset = 0 } = paginationDto;
|
72
|
-
const query = this.repository.createQueryBuilder(this.entityName);
|
73
|
-
query.limit(limit);
|
74
|
-
query.offset(offset);
|
75
|
-
query.where(`${this.entityName}.state != :state`, {
|
76
|
-
state: EntityStateConstant.Archived,
|
77
|
-
});
|
78
|
-
const defaultSelectable = ['createdAt', 'updatedAt'];
|
79
|
-
query.orderBy(`${this.entityName}.updatedAt`, 'DESC');
|
80
|
-
const selection = defaultSelectable.map(
|
81
|
-
(column: string) => `${this.entityName}.${column}`,
|
82
|
-
);
|
83
|
-
query.addSelect(selection);
|
84
|
-
if (callback != null) {
|
85
|
-
callback(query);
|
86
|
-
}
|
87
|
-
const { excludeIds = [], includeIds = [] } = { ...filter };
|
88
|
-
// Exclude some ids from the list.
|
89
|
-
if (excludeIds?.length > 0) {
|
90
|
-
query.andWhere(`${this.entityName}.id NOT IN (:ids)`, {
|
91
|
-
ids: excludeIds,
|
92
|
-
});
|
93
|
-
}
|
94
|
-
let entities = await query.getMany();
|
95
|
-
const total = await query.getCount();
|
96
|
-
// Include some ids to the list.
|
97
|
-
const allIds: number[] = [];
|
98
|
-
for (const id of includeIds) {
|
99
|
-
const entity = entities.filter((entity: Entity) => entity.id === id);
|
100
|
-
if (entity.length == 0) {
|
101
|
-
allIds.push(id);
|
102
|
-
}
|
103
|
-
}
|
104
|
-
if (allIds.length > 0) {
|
105
|
-
query.where(`${this.entityName}.id IN (:ids)`, { ids: allIds });
|
106
|
-
const data = await query.getMany();
|
107
|
-
entities = data.concat(entities);
|
108
|
-
}
|
109
|
-
const response = new PaginationResponse(entities, total, limit, offset);
|
110
|
-
return response;
|
111
|
-
}
|
112
|
-
}
|
1
|
+
import {
|
2
|
+
Repository,
|
3
|
+
Connection,
|
4
|
+
EntityManager,
|
5
|
+
SelectQueryBuilder,
|
6
|
+
} from 'typeorm';
|
7
|
+
import { EntityGeneric } from '../entity';
|
8
|
+
import { PaginationDto, BaseFilterDto } from '../../dto';
|
9
|
+
import { EntityStateConstant } from '../../constant';
|
10
|
+
import { PaginationResponse } from '../../response';
|
11
|
+
|
12
|
+
export abstract class ServiceGeneric<
|
13
|
+
Entity extends EntityGeneric,
|
14
|
+
CustomRepository extends Repository<Entity>,
|
15
|
+
> {
|
16
|
+
protected readonly entityName: string;
|
17
|
+
protected readonly loggable: boolean = false;
|
18
|
+
protected repository: CustomRepository;
|
19
|
+
protected connection: Connection;
|
20
|
+
constructor(
|
21
|
+
protected readonly connectionOrManager: Connection | EntityManager,
|
22
|
+
repositoryType: { new (connection: Connection): CustomRepository },
|
23
|
+
) {
|
24
|
+
if (connectionOrManager instanceof EntityManager) {
|
25
|
+
this.connection = connectionOrManager.connection;
|
26
|
+
} else {
|
27
|
+
this.connection = connectionOrManager;
|
28
|
+
}
|
29
|
+
this.repository = this.connection.getCustomRepository(repositoryType);
|
30
|
+
}
|
31
|
+
|
32
|
+
async create(entity: Entity): Promise<Entity> {
|
33
|
+
return this.repository.save(entity);
|
34
|
+
}
|
35
|
+
|
36
|
+
async update(entity: Entity): Promise<Entity> {
|
37
|
+
return this.repository.save(entity);
|
38
|
+
}
|
39
|
+
|
40
|
+
async getListWithPagination(
|
41
|
+
paginationDto: PaginationDto,
|
42
|
+
callback?: (query: SelectQueryBuilder<Entity>) => void,
|
43
|
+
): Promise<PaginationResponse<Entity>> {
|
44
|
+
const { limit = 25, offset = 0 } = paginationDto;
|
45
|
+
const query = this.repository.createQueryBuilder(this.entityName);
|
46
|
+
query.limit(limit);
|
47
|
+
query.offset(offset);
|
48
|
+
query.where(`${this.entityName}.state != :state`, {
|
49
|
+
state: EntityStateConstant.Archived,
|
50
|
+
});
|
51
|
+
const defaultSelectable = ['createdAt', 'updatedAt'];
|
52
|
+
query.orderBy(`${this.entityName}.updatedAt`, 'DESC');
|
53
|
+
const selection = defaultSelectable.map(
|
54
|
+
(column: string) => `${this.entityName}.${column}`,
|
55
|
+
);
|
56
|
+
query.addSelect(selection);
|
57
|
+
if (callback != null) {
|
58
|
+
callback(query);
|
59
|
+
}
|
60
|
+
const entities = await query.getMany();
|
61
|
+
const total = await query.getCount();
|
62
|
+
const response = new PaginationResponse(entities, total, limit, offset);
|
63
|
+
return response;
|
64
|
+
}
|
65
|
+
|
66
|
+
async getAutocompleteWithPagination(
|
67
|
+
paginationDto: PaginationDto,
|
68
|
+
filter: BaseFilterDto,
|
69
|
+
callback?: (query: SelectQueryBuilder<Entity>) => void,
|
70
|
+
): Promise<PaginationResponse<Entity>> {
|
71
|
+
const { limit = 25, offset = 0 } = paginationDto;
|
72
|
+
const query = this.repository.createQueryBuilder(this.entityName);
|
73
|
+
query.limit(limit);
|
74
|
+
query.offset(offset);
|
75
|
+
query.where(`${this.entityName}.state != :state`, {
|
76
|
+
state: EntityStateConstant.Archived,
|
77
|
+
});
|
78
|
+
const defaultSelectable = ['createdAt', 'updatedAt'];
|
79
|
+
query.orderBy(`${this.entityName}.updatedAt`, 'DESC');
|
80
|
+
const selection = defaultSelectable.map(
|
81
|
+
(column: string) => `${this.entityName}.${column}`,
|
82
|
+
);
|
83
|
+
query.addSelect(selection);
|
84
|
+
if (callback != null) {
|
85
|
+
callback(query);
|
86
|
+
}
|
87
|
+
const { excludeIds = [], includeIds = [] } = { ...filter };
|
88
|
+
// Exclude some ids from the list.
|
89
|
+
if (excludeIds?.length > 0) {
|
90
|
+
query.andWhere(`${this.entityName}.id NOT IN (:ids)`, {
|
91
|
+
ids: excludeIds,
|
92
|
+
});
|
93
|
+
}
|
94
|
+
let entities = await query.getMany();
|
95
|
+
const total = await query.getCount();
|
96
|
+
// Include some ids to the list.
|
97
|
+
const allIds: number[] = [];
|
98
|
+
for (const id of includeIds) {
|
99
|
+
const entity = entities.filter((entity: Entity) => entity.id === id);
|
100
|
+
if (entity.length == 0) {
|
101
|
+
allIds.push(id);
|
102
|
+
}
|
103
|
+
}
|
104
|
+
if (allIds.length > 0) {
|
105
|
+
query.where(`${this.entityName}.id IN (:ids)`, { ids: allIds });
|
106
|
+
const data = await query.getMany();
|
107
|
+
entities = data.concat(entities);
|
108
|
+
}
|
109
|
+
const response = new PaginationResponse(entities, total, limit, offset);
|
110
|
+
return response;
|
111
|
+
}
|
112
|
+
}
|
package/src/index.ts
CHANGED
package/src/lib/index.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export * from './search-engine';
|
1
|
+
export * from './search-engine';
|
@@ -1,3 +1,3 @@
|
|
1
|
-
export class BaseDocument {
|
2
|
-
id: string;
|
1
|
+
export class BaseDocument {
|
2
|
+
id: string;
|
3
3
|
}
|
@@ -1 +1 @@
|
|
1
|
-
export * from 'typesense';
|
1
|
+
export * from 'typesense';
|
@@ -1,2 +1,2 @@
|
|
1
|
-
export * from './search-document.interface';
|
1
|
+
export * from './search-document.interface';
|
2
2
|
export * from './transform-service.interface';
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { SearchOptions, SearchParams, SearchResponse, DocumentSchema } from 'typesense/lib/Typesense/Documents';
|
2
|
-
|
3
|
-
export interface SearchDocumentService<Document extends DocumentSchema> {
|
4
|
-
searchDocument(searchParameters: SearchParams, options: SearchOptions): Promise<SearchResponse<any>>;
|
5
|
-
}
|
1
|
+
import { SearchOptions, SearchParams, SearchResponse, DocumentSchema } from 'typesense/lib/Typesense/Documents';
|
2
|
+
|
3
|
+
export interface SearchDocumentService<Document extends DocumentSchema> {
|
4
|
+
searchDocument(searchParameters: SearchParams, options: SearchOptions): Promise<SearchResponse<any>>;
|
5
|
+
}
|
@@ -1,10 +1,10 @@
|
|
1
|
-
import { EntityGeneric } from '../../../generic';
|
2
|
-
import { BaseDocument } from '../document';
|
3
|
-
|
4
|
-
export interface TransformerService<
|
5
|
-
T extends EntityGeneric,
|
6
|
-
Document extends BaseDocument,
|
7
|
-
> {
|
8
|
-
transform(data: T): Document;
|
9
|
-
transforms(datas: T[]): Document[];
|
10
|
-
}
|
1
|
+
import { EntityGeneric } from '../../../generic';
|
2
|
+
import { BaseDocument } from '../document';
|
3
|
+
|
4
|
+
export interface TransformerService<
|
5
|
+
T extends EntityGeneric,
|
6
|
+
Document extends BaseDocument,
|
7
|
+
> {
|
8
|
+
transform(data: T): Document;
|
9
|
+
transforms(datas: T[]): Document[];
|
10
|
+
}
|
@@ -1,3 +1,3 @@
|
|
1
|
-
export * from './typesense.module';
|
2
|
-
export * from './metadata';
|
3
|
-
export * from './service';
|
1
|
+
export * from './typesense.module';
|
2
|
+
export * from './metadata';
|
3
|
+
export * from './service';
|
@@ -1,2 +1,2 @@
|
|
1
|
-
export * from './schema.metadata';
|
1
|
+
export * from './schema.metadata';
|
2
2
|
export * from './typesense.metadata-registry';
|
@@ -1,13 +1,13 @@
|
|
1
|
-
export interface SchemaField {
|
2
|
-
name: string;
|
3
|
-
type: string;
|
4
|
-
facet?: boolean;
|
5
|
-
index?: boolean;
|
6
|
-
optional?: boolean;
|
7
|
-
}
|
8
|
-
|
9
|
-
export interface Schema {
|
10
|
-
name: string;
|
11
|
-
defaultSortingField: string;
|
12
|
-
fields: Array<SchemaField>;
|
13
|
-
}
|
1
|
+
export interface SchemaField {
|
2
|
+
name: string;
|
3
|
+
type: string;
|
4
|
+
facet?: boolean;
|
5
|
+
index?: boolean;
|
6
|
+
optional?: boolean;
|
7
|
+
}
|
8
|
+
|
9
|
+
export interface Schema {
|
10
|
+
name: string;
|
11
|
+
defaultSortingField: string;
|
12
|
+
fields: Array<SchemaField>;
|
13
|
+
}
|
@@ -1,28 +1,28 @@
|
|
1
|
-
import { Injectable, Logger } from '@nestjs/common'
|
2
|
-
|
3
|
-
import { Schema } from './schema.metadata'
|
4
|
-
|
5
|
-
type Constructor = new (...args: any[]) => Record<string, unknown> // {}
|
6
|
-
|
7
|
-
@Injectable()
|
8
|
-
export class TypesenseMetadataRegistry {
|
9
|
-
private logger = new Logger(TypesenseMetadataRegistry.name)
|
10
|
-
|
11
|
-
private schemas: Map<Constructor, Schema> = new Map()
|
12
|
-
|
13
|
-
addSchema(target: Constructor, schema: Schema) {
|
14
|
-
if (this.schemas.has(target)) {
|
15
|
-
this.logger.warn(`Schema ${target} already exists`)
|
16
|
-
}
|
17
|
-
|
18
|
-
this.schemas.set(target, schema)
|
19
|
-
}
|
20
|
-
|
21
|
-
getSchemaByTarget(target: Constructor) {
|
22
|
-
return this.schemas.get(target)
|
23
|
-
}
|
24
|
-
|
25
|
-
getTargets() {
|
26
|
-
return this.schemas.keys()
|
27
|
-
}
|
28
|
-
}
|
1
|
+
import { Injectable, Logger } from '@nestjs/common'
|
2
|
+
|
3
|
+
import { Schema } from './schema.metadata'
|
4
|
+
|
5
|
+
type Constructor = new (...args: any[]) => Record<string, unknown> // {}
|
6
|
+
|
7
|
+
@Injectable()
|
8
|
+
export class TypesenseMetadataRegistry {
|
9
|
+
private logger = new Logger(TypesenseMetadataRegistry.name)
|
10
|
+
|
11
|
+
private schemas: Map<Constructor, Schema> = new Map()
|
12
|
+
|
13
|
+
addSchema(target: Constructor, schema: Schema) {
|
14
|
+
if (this.schemas.has(target)) {
|
15
|
+
this.logger.warn(`Schema ${target} already exists`)
|
16
|
+
}
|
17
|
+
|
18
|
+
this.schemas.set(target, schema)
|
19
|
+
}
|
20
|
+
|
21
|
+
getSchemaByTarget(target: Constructor) {
|
22
|
+
return this.schemas.get(target)
|
23
|
+
}
|
24
|
+
|
25
|
+
getTargets() {
|
26
|
+
return this.schemas.keys()
|
27
|
+
}
|
28
|
+
}
|