feat(leaning):完成NestJS整合TypeORM实现CRUD
- 增加CORE模块中的工具函数 - 增加数据库模块和数据库配置SQLite - 增加数据分页处理函数 - 文章模块更新提供者、实体、控制器、存储库替换为TypeORM操作 - 数据库模块增加自定义存储库动态模块
This commit is contained in:
@@ -1,23 +1,11 @@
|
||||
import { DynamicModule, Global, Module } from '@nestjs/common';
|
||||
import { DynamicModule, Module } from '@nestjs/common';
|
||||
|
||||
import { ConfigService } from './services/config.service';
|
||||
|
||||
@Global()
|
||||
@Module({})
|
||||
export class CoreModule {
|
||||
static forRoot(options: { config: RecordAny }): DynamicModule {
|
||||
static forRoot(): DynamicModule {
|
||||
return {
|
||||
module: CoreModule,
|
||||
global: true,
|
||||
providers: [
|
||||
{
|
||||
provide: ConfigService,
|
||||
useFactory() {
|
||||
return new ConfigService(options.config);
|
||||
},
|
||||
},
|
||||
],
|
||||
exports: [ConfigService],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
export * from './utils';
|
||||
@@ -0,0 +1,50 @@
|
||||
import deepmerge from 'deepmerge';
|
||||
|
||||
import { isNil } from 'lodash';
|
||||
|
||||
/**
|
||||
* 用于请求验证中的boolean数据转义
|
||||
* @param value
|
||||
*/
|
||||
export const 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;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 用于请求验证中转义null
|
||||
* @param value
|
||||
*/
|
||||
export const toNull = (value?: string | null): string | null | undefined => {
|
||||
return value === 'null' ? null : value;
|
||||
};
|
||||
|
||||
/**
|
||||
* 深度合并对象
|
||||
* @param x 初始值
|
||||
* @param y 新值
|
||||
* @param arrayMode 对于数组采取的策略,`replace`为直接替换,`merge`为合并数组
|
||||
*/
|
||||
export const deepMerge = <T1, T2>(
|
||||
x: Partial<T1>,
|
||||
y: Partial<T2>,
|
||||
arrayMode: 'replace' | 'merge' = 'merge',
|
||||
) => {
|
||||
const options: deepmerge.Options = {};
|
||||
|
||||
if (arrayMode === 'replace') {
|
||||
options.arrayMerge = (target, source, _options) => source;
|
||||
} else if (arrayMode === 'merge') {
|
||||
options.arrayMerge = (target, source, _options) =>
|
||||
Array.from(new Set({ ...target, ...source }));
|
||||
}
|
||||
|
||||
return deepmerge(x, y, options) as T2 extends T1 ? T1 : T1 & T2;
|
||||
};
|
||||
@@ -1,15 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { get } from 'lodash';
|
||||
|
||||
@Injectable()
|
||||
export class ConfigService {
|
||||
protected config: RecordAny = {};
|
||||
|
||||
constructor(data: RecordAny) {
|
||||
this.config = data;
|
||||
}
|
||||
|
||||
get<T>(key: string, defaultValue?: T): T | undefined {
|
||||
return get(this.config, key, defaultValue);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user