add helpers
This commit is contained in:
parent
321b7e0b0c
commit
64e1602874
@ -1,14 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { ContentModule } from './modules/content/content.module';
|
||||
import { CoreModule } from './modules/core/core.module';
|
||||
import { DatabaseModule } from './modules/database/database.module';
|
||||
|
||||
@Module({
|
||||
imports: [ContentModule, CoreModule, DatabaseModule],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
imports: [ContentModule, CoreModule.forRoot(), DatabaseModule],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
@ -1,4 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { DynamicModule, Module } from '@nestjs/common';
|
||||
|
||||
@Module({})
|
||||
export class CoreModule {}
|
||||
export class CoreModule {
|
||||
static forRoot(): DynamicModule {
|
||||
return {
|
||||
module: CoreModule,
|
||||
global: true,
|
||||
providers: [],
|
||||
exports: [],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
1
src/modules/core/helpers/index.ts
Normal file
1
src/modules/core/helpers/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './utils';
|
34
src/modules/core/helpers/utils.ts
Normal file
34
src/modules/core/helpers/utils.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import deepmerge from 'deepmerge';
|
||||
import { isNil } from 'lodash';
|
||||
|
||||
export function toBoolean(value?: string | boolean): boolean {
|
||||
if (isNil(value)) {
|
||||
return false;
|
||||
}
|
||||
if (typeof value === 'boolean') {
|
||||
return value;
|
||||
}
|
||||
try {
|
||||
return JSON.parse(value.toLowerCase());
|
||||
} catch (error) {
|
||||
return value as unknown as boolean;
|
||||
}
|
||||
}
|
||||
|
||||
export function toNull(value?: string | null): string | null | undefined {
|
||||
return value === null ? null : value;
|
||||
}
|
||||
|
||||
export const deepMerge = <T, P>(
|
||||
x: Partial<T>,
|
||||
y: Partial<P>,
|
||||
arrayMode: 'replace' | 'merge' = 'merge',
|
||||
) => {
|
||||
const options: deepmerge.Options = {};
|
||||
if (arrayMode === 'replace') {
|
||||
options.arrayMerge = (_d, s, _o) => s;
|
||||
} else if (arrayMode === 'merge') {
|
||||
options.arrayMerge = (_d, s, _o) => Array.from(new Set([..._d, ...s]));
|
||||
}
|
||||
return deepmerge(x, y, options) as P extends T ? T : T & P;
|
||||
};
|
23
src/modules/database/types.ts
Normal file
23
src/modules/database/types.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { ObjectLiteral, SelectQueryBuilder } from 'typeorm';
|
||||
|
||||
export type QueryHook<Entity> = (
|
||||
qb: SelectQueryBuilder<Entity>,
|
||||
) => Promise<SelectQueryBuilder<Entity>>;
|
||||
|
||||
export interface PaginateMeta {
|
||||
itemCount: number;
|
||||
totalItems?: number;
|
||||
perPage: number;
|
||||
totalPages?: number;
|
||||
currentPage: number;
|
||||
}
|
||||
|
||||
export interface PaginateOptions {
|
||||
page?: number;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
export interface PaginateReturn<E extends ObjectLiteral> {
|
||||
meta: PaginateMeta;
|
||||
items: E[];
|
||||
}
|
Loading…
Reference in New Issue
Block a user