mm-share-lib 0.0.7 → 0.0.8
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 +1 -2
- package/src/lib/index.ts +0 -1
- package/src/lib/search-engine/index.ts +1 -1
- package/src/lib/typesense/client/index.ts +0 -1
- package/src/lib/typesense/collection/index.ts +0 -1
- package/src/lib/typesense/collection/typesense-colletctions.creator.ts +0 -29
- package/src/lib/typesense/decorator/field.decorator.spec.ts +0 -89
- package/src/lib/typesense/decorator/field.decorator.ts +0 -44
- package/src/lib/typesense/decorator/index.ts +0 -2
- package/src/lib/typesense/decorator/schema.decorator.ts +0 -23
- package/src/lib/typesense/document/base.document.ts +0 -3
- package/src/lib/typesense/document/index.ts +0 -1
- package/src/lib/typesense/index.ts +0 -6
- package/src/lib/typesense/metadata/index.ts +0 -4
- package/src/lib/typesense/metadata/schema.metadata.ts +0 -21
- package/src/lib/typesense/metadata/typesense.metadata-accessor.ts +0 -37
- package/src/lib/typesense/metadata/typesense.metadata-explorer.spec.ts +0 -56
- package/src/lib/typesense/metadata/typesense.metadata-explorer.ts +0 -41
- package/src/lib/typesense/metadata/typesense.metadata-registry.ts +0 -28
- package/src/lib/typesense/module/index.ts +0 -4
- package/src/lib/typesense/module/interface.module.ts +0 -36
- package/src/lib/typesense/module/typesense.constant.ts +0 -1
- package/src/lib/typesense/module/typesense.module.spec.ts +0 -94
- package/src/lib/typesense/module/typesense.module.ts +0 -87
- package/src/lib/typesense/module/typesense.provider.ts +0 -54
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "mm-share-lib",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.8",
|
4
4
|
"description": "Share the generic service, entity, dto.",
|
5
5
|
"author": "Mesa SOT",
|
6
6
|
"license": "MIT",
|
@@ -35,7 +35,6 @@
|
|
35
35
|
"class-transformer": "^0.5.1",
|
36
36
|
"class-validator": "^0.14.0",
|
37
37
|
"dayjs": "^1.11.8",
|
38
|
-
"decamelize": "^6.0.0",
|
39
38
|
"fb": "^2.0.0",
|
40
39
|
"google-auth-library": "^9.0.0",
|
41
40
|
"handlebars": "^4.7.7",
|
package/src/lib/index.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export * from 'typesense';
|
1
|
+
export * from './typesense';
|
@@ -1 +0,0 @@
|
|
1
|
-
export { Client } from 'typesense';
|
@@ -1 +0,0 @@
|
|
1
|
-
export * from './typesense-colletctions.creator';
|
@@ -1,29 +0,0 @@
|
|
1
|
-
import { Client } from 'typesense';
|
2
|
-
import { OnModuleInit, Logger, Injectable } from '@nestjs/common';
|
3
|
-
import { TypesenseMetadataRegistry } from '../metadata';
|
4
|
-
|
5
|
-
@Injectable()
|
6
|
-
export class TypesenseCollectionsCreator implements OnModuleInit {
|
7
|
-
private readonly logger = new Logger(TypesenseCollectionsCreator.name);
|
8
|
-
|
9
|
-
constructor(
|
10
|
-
private readonly registry: TypesenseMetadataRegistry,
|
11
|
-
private readonly typesense: Client,
|
12
|
-
) {}
|
13
|
-
|
14
|
-
async onModuleInit() {
|
15
|
-
for (const target of this.registry.getTargets()) {
|
16
|
-
const schema = this.registry.getSchemaByTarget(target);
|
17
|
-
|
18
|
-
try {
|
19
|
-
// eslint-disable-next-line no-await-in-loop
|
20
|
-
await this.typesense.collections(schema!.name).retrieve();
|
21
|
-
} catch (error) {
|
22
|
-
if ((error as any).httpStatus === 404) {
|
23
|
-
// eslint-disable-next-line no-await-in-loop
|
24
|
-
await this.typesense.collections().create(schema);
|
25
|
-
}
|
26
|
-
}
|
27
|
-
}
|
28
|
-
}
|
29
|
-
}
|
@@ -1,89 +0,0 @@
|
|
1
|
-
/* eslint-disable max-classes-per-file */
|
2
|
-
|
3
|
-
import { Field } from './field.decorator';
|
4
|
-
import { FIELD_METADATA } from './field.decorator';
|
5
|
-
import { Schema } from './schema.decorator';
|
6
|
-
|
7
|
-
describe('typesense', () => {
|
8
|
-
describe('decorators', () => {
|
9
|
-
describe('field', () => {
|
10
|
-
it('should enhance field with metadata', () => {
|
11
|
-
@Schema()
|
12
|
-
class Test {
|
13
|
-
@Field('string')
|
14
|
-
field!: string;
|
15
|
-
}
|
16
|
-
|
17
|
-
expect(Reflect.getMetadata(FIELD_METADATA, Test)).toEqual(
|
18
|
-
expect.arrayContaining([
|
19
|
-
expect.objectContaining({
|
20
|
-
name: 'field',
|
21
|
-
type: 'string',
|
22
|
-
}),
|
23
|
-
]),
|
24
|
-
);
|
25
|
-
});
|
26
|
-
|
27
|
-
it('should enhance field with custom name metadata', () => {
|
28
|
-
@Schema()
|
29
|
-
class Test {
|
30
|
-
@Field('string', { name: 'custom' })
|
31
|
-
field!: string;
|
32
|
-
}
|
33
|
-
|
34
|
-
expect(Reflect.getMetadata(FIELD_METADATA, Test)).toEqual(
|
35
|
-
expect.arrayContaining([
|
36
|
-
expect.objectContaining({
|
37
|
-
name: 'custom',
|
38
|
-
type: 'string',
|
39
|
-
}),
|
40
|
-
]),
|
41
|
-
);
|
42
|
-
});
|
43
|
-
});
|
44
|
-
|
45
|
-
it('should enhance field with options metadata', () => {
|
46
|
-
@Schema()
|
47
|
-
class Test {
|
48
|
-
@Field('string', { facet: true, index: true, optional: true })
|
49
|
-
field!: string;
|
50
|
-
}
|
51
|
-
|
52
|
-
expect(Reflect.getMetadata(FIELD_METADATA, Test)).toEqual(
|
53
|
-
expect.arrayContaining([
|
54
|
-
expect.objectContaining({
|
55
|
-
name: 'field',
|
56
|
-
type: 'string',
|
57
|
-
facet: true,
|
58
|
-
index: true,
|
59
|
-
optional: true,
|
60
|
-
}),
|
61
|
-
]),
|
62
|
-
);
|
63
|
-
});
|
64
|
-
|
65
|
-
it('should enhance field with multiple fields metadata', () => {
|
66
|
-
@Schema()
|
67
|
-
class Test {
|
68
|
-
@Field('string')
|
69
|
-
field!: string;
|
70
|
-
|
71
|
-
@Field('string')
|
72
|
-
field2!: string;
|
73
|
-
}
|
74
|
-
|
75
|
-
expect(Reflect.getMetadata(FIELD_METADATA, Test)).toEqual(
|
76
|
-
expect.arrayContaining([
|
77
|
-
expect.objectContaining({
|
78
|
-
name: 'field',
|
79
|
-
type: 'string',
|
80
|
-
}),
|
81
|
-
expect.objectContaining({
|
82
|
-
name: 'field2',
|
83
|
-
type: 'string',
|
84
|
-
}),
|
85
|
-
]),
|
86
|
-
);
|
87
|
-
});
|
88
|
-
});
|
89
|
-
});
|
@@ -1,44 +0,0 @@
|
|
1
|
-
import { SetMetadata } from '@nestjs/common';
|
2
|
-
import { applyDecorators } from '@nestjs/common';
|
3
|
-
|
4
|
-
export interface FieldMetadata {
|
5
|
-
name?: string;
|
6
|
-
facet?: boolean;
|
7
|
-
index?: boolean;
|
8
|
-
optional?: boolean;
|
9
|
-
}
|
10
|
-
|
11
|
-
export type FieldType =
|
12
|
-
| 'string'
|
13
|
-
| 'int32'
|
14
|
-
| 'int64'
|
15
|
-
| 'float'
|
16
|
-
| 'bool'
|
17
|
-
| 'geopoint'
|
18
|
-
| 'geopoint[]'
|
19
|
-
| 'string[]'
|
20
|
-
| 'int32[]'
|
21
|
-
| 'int64[]'
|
22
|
-
| 'float[]'
|
23
|
-
| 'bool[]'
|
24
|
-
| 'object'
|
25
|
-
| 'object[]'
|
26
|
-
| 'auto'
|
27
|
-
| 'string*';
|
28
|
-
|
29
|
-
export const FIELD_METADATA = '__fieldMetadata__';
|
30
|
-
|
31
|
-
export const Field = (type: FieldType, options: FieldMetadata = {}) =>
|
32
|
-
applyDecorators((target: object, key?: any, descriptor?: any) => {
|
33
|
-
const exists =
|
34
|
-
Reflect.getMetadata(FIELD_METADATA, target.constructor) || [];
|
35
|
-
|
36
|
-
return SetMetadata(FIELD_METADATA, [
|
37
|
-
...exists,
|
38
|
-
{
|
39
|
-
...options,
|
40
|
-
type,
|
41
|
-
name: options.name || key,
|
42
|
-
},
|
43
|
-
])(target.constructor, key, descriptor);
|
44
|
-
});
|
@@ -1,23 +0,0 @@
|
|
1
|
-
import decamelize from 'decamelize';
|
2
|
-
import { SetMetadata, applyDecorators } from '@nestjs/common';
|
3
|
-
|
4
|
-
export interface Schema {
|
5
|
-
name?: string;
|
6
|
-
defaultSortingField?: string;
|
7
|
-
auto?: boolean;
|
8
|
-
}
|
9
|
-
|
10
|
-
export const SCHEMA_METADATA = '__schemaMetadata__';
|
11
|
-
|
12
|
-
export const Schema = (options: Schema = {}): ClassDecorator =>
|
13
|
-
applyDecorators((target: object, key?: any, descriptor?: any) =>
|
14
|
-
SetMetadata(SCHEMA_METADATA, {
|
15
|
-
name:
|
16
|
-
options.name ||
|
17
|
-
decamelize((target as any).name, {
|
18
|
-
separator: '-',
|
19
|
-
preserveConsecutiveUppercase: false,
|
20
|
-
}),
|
21
|
-
defaultSortingField: options.defaultSortingField,
|
22
|
-
})(target, key, descriptor),
|
23
|
-
);
|
@@ -1 +0,0 @@
|
|
1
|
-
export * from './base.document';
|
@@ -1,21 +0,0 @@
|
|
1
|
-
import type { FieldType } from '../decorator/field.decorator';
|
2
|
-
|
3
|
-
export interface SchemaField {
|
4
|
-
name: string;
|
5
|
-
type: FieldType;
|
6
|
-
optional?: boolean;
|
7
|
-
facet?: boolean;
|
8
|
-
index?: boolean;
|
9
|
-
sort?: boolean;
|
10
|
-
locale?: string;
|
11
|
-
infix?: boolean;
|
12
|
-
num_dim?: number;
|
13
|
-
[t: string]: unknown;
|
14
|
-
}
|
15
|
-
|
16
|
-
export interface Schema {
|
17
|
-
name: string;
|
18
|
-
fields: Array<SchemaField>;
|
19
|
-
enable_nested_fields?: boolean;
|
20
|
-
defaultSortingField?: string;
|
21
|
-
}
|
@@ -1,37 +0,0 @@
|
|
1
|
-
import { Injectable } from '@nestjs/common';
|
2
|
-
import { Reflector } from '@nestjs/core';
|
3
|
-
|
4
|
-
import { SCHEMA_METADATA } from '../decorator';
|
5
|
-
import { FIELD_METADATA } from '../decorator';
|
6
|
-
import { Schema } from './schema.metadata';
|
7
|
-
|
8
|
-
@Injectable()
|
9
|
-
export class TypesenseMetadataAccessor {
|
10
|
-
constructor(private readonly reflector: Reflector) {}
|
11
|
-
|
12
|
-
getTypesenseMetadata(target): Schema | undefined {
|
13
|
-
if (target.constructor) {
|
14
|
-
const schema = this.reflector.get(SCHEMA_METADATA, target.constructor);
|
15
|
-
const fields = this.reflector.get(FIELD_METADATA, target.constructor);
|
16
|
-
|
17
|
-
if (!schema) {
|
18
|
-
return undefined;
|
19
|
-
}
|
20
|
-
|
21
|
-
if (!(fields || schema.auto)) {
|
22
|
-
return undefined;
|
23
|
-
}
|
24
|
-
|
25
|
-
return {
|
26
|
-
name: schema.name,
|
27
|
-
defaultSortingField: schema.defaultSortingField,
|
28
|
-
fields: [
|
29
|
-
...(schema.auto ? [{ name: '.*', type: 'auto' }] : []),
|
30
|
-
...(fields || []),
|
31
|
-
],
|
32
|
-
};
|
33
|
-
}
|
34
|
-
|
35
|
-
return undefined;
|
36
|
-
}
|
37
|
-
}
|
@@ -1,56 +0,0 @@
|
|
1
|
-
/* eslint-disable max-classes-per-file */
|
2
|
-
|
3
|
-
import 'reflect-metadata';
|
4
|
-
|
5
|
-
import { Module } from '@nestjs/common';
|
6
|
-
import { DiscoveryModule } from '@nestjs/core';
|
7
|
-
import { Test } from '@nestjs/testing';
|
8
|
-
|
9
|
-
import { Schema } from '../decorators';
|
10
|
-
import { Field } from '../decorators';
|
11
|
-
import { TypesenseMetadataAccessor } from './typesense.metadata-accessor';
|
12
|
-
import { TypesenseMetadataExplorer } from './typesense.metadata-explorer';
|
13
|
-
import { TypesenseMetadataRegistry } from './typesense.metadata-registry';
|
14
|
-
|
15
|
-
describe('typesense', () => {
|
16
|
-
describe('metadata', () => {
|
17
|
-
describe('explorer', () => {
|
18
|
-
let module;
|
19
|
-
|
20
|
-
@Module({
|
21
|
-
imports: [DiscoveryModule],
|
22
|
-
providers: [
|
23
|
-
TypesenseMetadataAccessor,
|
24
|
-
TypesenseMetadataExplorer,
|
25
|
-
TypesenseMetadataRegistry,
|
26
|
-
],
|
27
|
-
})
|
28
|
-
class TestMetadataModule {}
|
29
|
-
|
30
|
-
@Schema()
|
31
|
-
class TestSchema {
|
32
|
-
@Field('string')
|
33
|
-
field!: string;
|
34
|
-
}
|
35
|
-
|
36
|
-
beforeEach(async () => {
|
37
|
-
module = await Test.createTestingModule({
|
38
|
-
imports: [TestMetadataModule],
|
39
|
-
providers: [TestSchema],
|
40
|
-
}).compile();
|
41
|
-
|
42
|
-
await module.init();
|
43
|
-
});
|
44
|
-
|
45
|
-
afterEach(async () => {
|
46
|
-
await module.close();
|
47
|
-
});
|
48
|
-
|
49
|
-
it('should store schema metadata', () => {
|
50
|
-
expect(
|
51
|
-
module.get(TypesenseMetadataRegistry).getSchemaByTarget(TestSchema),
|
52
|
-
).toBeDefined();
|
53
|
-
});
|
54
|
-
});
|
55
|
-
});
|
56
|
-
});
|
@@ -1,41 +0,0 @@
|
|
1
|
-
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
|
2
|
-
import { DiscoveryService } from '@nestjs/core';
|
3
|
-
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
4
|
-
|
5
|
-
import { TypesenseMetadataAccessor } from './typesense.metadata-accessor';
|
6
|
-
import { TypesenseMetadataRegistry } from './typesense.metadata-registry';
|
7
|
-
|
8
|
-
@Injectable()
|
9
|
-
export class TypesenseMetadataExplorer implements OnModuleInit {
|
10
|
-
private readonly logger = new Logger(TypesenseMetadataExplorer.name);
|
11
|
-
|
12
|
-
constructor(
|
13
|
-
private readonly discoveryService: DiscoveryService,
|
14
|
-
private readonly metadataAccessor: TypesenseMetadataAccessor,
|
15
|
-
private readonly metadataRegistry: TypesenseMetadataRegistry,
|
16
|
-
) {}
|
17
|
-
|
18
|
-
onModuleInit() {
|
19
|
-
this.explore();
|
20
|
-
}
|
21
|
-
|
22
|
-
explore() {
|
23
|
-
this.discoveryService.getProviders().forEach((wrapper: InstanceWrapper) => {
|
24
|
-
const { instance } = wrapper;
|
25
|
-
|
26
|
-
if (!instance || !Object.getPrototypeOf(instance)) {
|
27
|
-
return;
|
28
|
-
}
|
29
|
-
|
30
|
-
this.lookupSchema(instance);
|
31
|
-
});
|
32
|
-
}
|
33
|
-
|
34
|
-
lookupSchema(instance) {
|
35
|
-
const metadata = this.metadataAccessor.getTypesenseMetadata(instance);
|
36
|
-
|
37
|
-
if (metadata) {
|
38
|
-
this.metadataRegistry.addSchema(instance.constructor, metadata);
|
39
|
-
}
|
40
|
-
}
|
41
|
-
}
|
@@ -1,28 +0,0 @@
|
|
1
|
-
import { Injectable, Logger } from '@nestjs/common';
|
2
|
-
|
3
|
-
import { Schema } from './schema.metadata';
|
4
|
-
|
5
|
-
type Constructor = new (...args: any[]) => 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,36 +0,0 @@
|
|
1
|
-
import { ModuleMetadata } from '@nestjs/common/interfaces';
|
2
|
-
import { Type } from '@nestjs/common/interfaces';
|
3
|
-
|
4
|
-
export interface TypesenseNodeOptions {
|
5
|
-
host: string;
|
6
|
-
port: number;
|
7
|
-
protocol: string;
|
8
|
-
}
|
9
|
-
|
10
|
-
type LogLevelNames = 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
11
|
-
|
12
|
-
export interface TypesenseModuleOptions {
|
13
|
-
nodes?: Array<TypesenseNodeOptions>;
|
14
|
-
numRetries?: number;
|
15
|
-
apiKey?: string;
|
16
|
-
connectionTimeoutSeconds?: number;
|
17
|
-
retryIntervalSeconds?: number;
|
18
|
-
healthcheckIntervalSeconds?: number;
|
19
|
-
logLevel?: LogLevelNames;
|
20
|
-
}
|
21
|
-
|
22
|
-
export interface TypesenseOptionsFactory {
|
23
|
-
createTypesenseOptions():
|
24
|
-
| Promise<TypesenseModuleOptions>
|
25
|
-
| TypesenseModuleOptions;
|
26
|
-
}
|
27
|
-
|
28
|
-
export interface TypesenseModuleAsyncOptions
|
29
|
-
extends Pick<ModuleMetadata, 'imports'> {
|
30
|
-
useExisting?: Type<TypesenseOptionsFactory>;
|
31
|
-
useClass?: Type<TypesenseOptionsFactory>;
|
32
|
-
useFactory?: (
|
33
|
-
...args: any[]
|
34
|
-
) => Promise<TypesenseModuleOptions> | TypesenseModuleOptions;
|
35
|
-
inject?: any[];
|
36
|
-
}
|
@@ -1 +0,0 @@
|
|
1
|
-
export const TYPESENSE_MODULE_OPTIONS = 'TYPESENSE_MODULE_OPTIONS';
|
@@ -1,94 +0,0 @@
|
|
1
|
-
/* eslint-disable max-classes-per-file */
|
2
|
-
|
3
|
-
import { Module } from '@nestjs/common';
|
4
|
-
import { Test } from '@nestjs/testing';
|
5
|
-
|
6
|
-
import { TypesenseModuleOptions } from './interface.module';
|
7
|
-
import { TYPESENSE_MODULE_OPTIONS } from './typesense.constant';
|
8
|
-
import { TypesenseModule } from './';
|
9
|
-
|
10
|
-
describe('typesense', () => {
|
11
|
-
describe('module', () => {
|
12
|
-
let module;
|
13
|
-
|
14
|
-
afterEach(async () => {
|
15
|
-
await module.close();
|
16
|
-
});
|
17
|
-
|
18
|
-
it(`register`, async () => {
|
19
|
-
module = await Test.createTestingModule({
|
20
|
-
imports: [
|
21
|
-
TypesenseModule.register({
|
22
|
-
apiKey: 'test',
|
23
|
-
}),
|
24
|
-
],
|
25
|
-
}).compile();
|
26
|
-
|
27
|
-
expect(module.get(TYPESENSE_MODULE_OPTIONS)).toBeDefined();
|
28
|
-
});
|
29
|
-
|
30
|
-
it(`register async use factory`, async () => {
|
31
|
-
module = await Test.createTestingModule({
|
32
|
-
imports: [
|
33
|
-
TypesenseModule.registerAsync({
|
34
|
-
useFactory: () => ({
|
35
|
-
apiKey: 'test',
|
36
|
-
}),
|
37
|
-
}),
|
38
|
-
],
|
39
|
-
}).compile();
|
40
|
-
|
41
|
-
expect(module.get(TYPESENSE_MODULE_OPTIONS)).toBeDefined();
|
42
|
-
});
|
43
|
-
|
44
|
-
it(`register async use class`, async () => {
|
45
|
-
class TestTypesenseModuleOptions {
|
46
|
-
createTypesenseOptions(): TypesenseModuleOptions {
|
47
|
-
return {
|
48
|
-
apiKey: 'test',
|
49
|
-
};
|
50
|
-
}
|
51
|
-
}
|
52
|
-
|
53
|
-
module = await Test.createTestingModule({
|
54
|
-
imports: [
|
55
|
-
TypesenseModule.registerAsync({
|
56
|
-
useClass: TestTypesenseModuleOptions,
|
57
|
-
}),
|
58
|
-
],
|
59
|
-
}).compile();
|
60
|
-
|
61
|
-
expect(module.get(TYPESENSE_MODULE_OPTIONS)).toBeDefined();
|
62
|
-
});
|
63
|
-
|
64
|
-
it(`register async use exists`, async () => {
|
65
|
-
class TestTypesenseModuleOptions {
|
66
|
-
createTypesenseOptions(): TypesenseModuleOptions {
|
67
|
-
return {
|
68
|
-
apiKey: 'test',
|
69
|
-
};
|
70
|
-
}
|
71
|
-
}
|
72
|
-
|
73
|
-
@Module({})
|
74
|
-
class TestTypesenseModule {}
|
75
|
-
|
76
|
-
module = await Test.createTestingModule({
|
77
|
-
imports: [
|
78
|
-
TypesenseModule.registerAsync({
|
79
|
-
imports: [
|
80
|
-
{
|
81
|
-
module: TestTypesenseModule,
|
82
|
-
providers: [TestTypesenseModuleOptions],
|
83
|
-
exports: [TestTypesenseModuleOptions],
|
84
|
-
},
|
85
|
-
],
|
86
|
-
useExisting: TestTypesenseModuleOptions,
|
87
|
-
}),
|
88
|
-
],
|
89
|
-
}).compile();
|
90
|
-
|
91
|
-
expect(module.get(TYPESENSE_MODULE_OPTIONS)).toBeDefined();
|
92
|
-
});
|
93
|
-
});
|
94
|
-
});
|
@@ -1,87 +0,0 @@
|
|
1
|
-
import { DynamicModule, Provider, Module } from '@nestjs/common';
|
2
|
-
import { DiscoveryModule } from '@nestjs/core';
|
3
|
-
|
4
|
-
import {
|
5
|
-
TypesenseModuleAsyncOptions,
|
6
|
-
TypesenseOptionsFactory,
|
7
|
-
TypesenseModuleOptions,
|
8
|
-
} from './interface.module';
|
9
|
-
import { TYPESENSE_MODULE_OPTIONS } from './typesense.constant';
|
10
|
-
import {
|
11
|
-
createTypesenseExportsProvider,
|
12
|
-
createTypesenseProvider,
|
13
|
-
createTypesenseOptionsProvider,
|
14
|
-
} from './typesense.provider';
|
15
|
-
|
16
|
-
@Module({
|
17
|
-
imports: [DiscoveryModule],
|
18
|
-
})
|
19
|
-
export class TypesenseModule {
|
20
|
-
static register(options: TypesenseModuleOptions = {}): DynamicModule {
|
21
|
-
const optionsProviders = createTypesenseOptionsProvider(options);
|
22
|
-
const exportsProviders = createTypesenseExportsProvider();
|
23
|
-
const providers = createTypesenseProvider();
|
24
|
-
|
25
|
-
return {
|
26
|
-
global: true,
|
27
|
-
module: TypesenseModule,
|
28
|
-
providers: [...optionsProviders, ...providers, ...exportsProviders],
|
29
|
-
exports: exportsProviders,
|
30
|
-
};
|
31
|
-
}
|
32
|
-
|
33
|
-
static registerAsync(options: TypesenseModuleAsyncOptions): DynamicModule {
|
34
|
-
const exportsProviders = createTypesenseExportsProvider();
|
35
|
-
const providers = createTypesenseProvider();
|
36
|
-
|
37
|
-
return {
|
38
|
-
global: true,
|
39
|
-
module: TypesenseModule,
|
40
|
-
imports: options.imports || [],
|
41
|
-
providers: [
|
42
|
-
...this.createAsyncProviders(options),
|
43
|
-
...providers,
|
44
|
-
...exportsProviders,
|
45
|
-
],
|
46
|
-
exports: exportsProviders,
|
47
|
-
};
|
48
|
-
}
|
49
|
-
|
50
|
-
private static createAsyncProviders(
|
51
|
-
options: TypesenseModuleAsyncOptions,
|
52
|
-
): Provider[] {
|
53
|
-
if (options.useExisting || options.useFactory) {
|
54
|
-
return [this.createAsyncOptionsProvider(options)];
|
55
|
-
}
|
56
|
-
|
57
|
-
return [
|
58
|
-
this.createAsyncOptionsProvider(options),
|
59
|
-
{
|
60
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
61
|
-
provide: options.useClass!,
|
62
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
63
|
-
useClass: options.useClass!,
|
64
|
-
},
|
65
|
-
];
|
66
|
-
}
|
67
|
-
|
68
|
-
private static createAsyncOptionsProvider(
|
69
|
-
options: TypesenseModuleAsyncOptions,
|
70
|
-
): Provider {
|
71
|
-
if (options.useFactory) {
|
72
|
-
return {
|
73
|
-
provide: TYPESENSE_MODULE_OPTIONS,
|
74
|
-
useFactory: options.useFactory,
|
75
|
-
inject: options.inject || [],
|
76
|
-
};
|
77
|
-
}
|
78
|
-
|
79
|
-
return {
|
80
|
-
provide: TYPESENSE_MODULE_OPTIONS,
|
81
|
-
useFactory: (optionsFactory: TypesenseOptionsFactory) =>
|
82
|
-
optionsFactory.createTypesenseOptions(),
|
83
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
84
|
-
inject: [options.useExisting! || options.useClass!],
|
85
|
-
};
|
86
|
-
}
|
87
|
-
}
|
@@ -1,54 +0,0 @@
|
|
1
|
-
import { Client } from 'typesense';
|
2
|
-
import { Provider } from '@nestjs/common';
|
3
|
-
import { TypesenseCollectionsCreator } from '../collection';
|
4
|
-
import {
|
5
|
-
TypesenseMetadataAccessor,
|
6
|
-
TypesenseMetadataRegistry,
|
7
|
-
TypesenseMetadataExplorer,
|
8
|
-
} from '../metadata';
|
9
|
-
import { TypesenseModuleOptions } from './interface.module';
|
10
|
-
import { TYPESENSE_MODULE_OPTIONS } from './typesense.constant';
|
11
|
-
|
12
|
-
export const createTypesenseOptionsProvider = (
|
13
|
-
options: TypesenseModuleOptions = {},
|
14
|
-
): Provider[] => [
|
15
|
-
{
|
16
|
-
provide: TYPESENSE_MODULE_OPTIONS,
|
17
|
-
useValue: options,
|
18
|
-
},
|
19
|
-
];
|
20
|
-
|
21
|
-
export const createTypesenseProvider = (): Provider[] => [
|
22
|
-
TypesenseMetadataAccessor,
|
23
|
-
TypesenseMetadataExplorer,
|
24
|
-
TypesenseMetadataRegistry,
|
25
|
-
TypesenseCollectionsCreator,
|
26
|
-
];
|
27
|
-
|
28
|
-
export const createTypesenseExportsProvider = (): Provider[] => [
|
29
|
-
TypesenseMetadataRegistry,
|
30
|
-
{
|
31
|
-
provide: Client,
|
32
|
-
useFactory: (options: TypesenseModuleOptions) =>
|
33
|
-
new Client({
|
34
|
-
nodes: options.nodes || [
|
35
|
-
{
|
36
|
-
host:
|
37
|
-
process.env.TYPESENSE_HOST ||
|
38
|
-
process.env.NODE_ENV === 'production'
|
39
|
-
? 'ts.typesense.svc.cluster.local'
|
40
|
-
: 'localhost',
|
41
|
-
port: 8108,
|
42
|
-
protocol: 'http',
|
43
|
-
},
|
44
|
-
],
|
45
|
-
numRetries: options.numRetries || 10,
|
46
|
-
apiKey: options.apiKey || process.env.TYPESENSE_API_KEY,
|
47
|
-
connectionTimeoutSeconds: options.connectionTimeoutSeconds || 10,
|
48
|
-
retryIntervalSeconds: options.retryIntervalSeconds || 0.1,
|
49
|
-
healthcheckIntervalSeconds: options.healthcheckIntervalSeconds || 2,
|
50
|
-
logLevel: options.logLevel || 'info',
|
51
|
-
}),
|
52
|
-
inject: [TYPESENSE_MODULE_OPTIONS],
|
53
|
-
},
|
54
|
-
];
|