add config module
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { ConfigureFactory, ConfigureRegister } from '../config/types';
|
||||
import { createConnectionOptions } from '../config/utils';
|
||||
import { deepMerge } from '../core/helpers';
|
||||
|
||||
import { DBConfig, DBOptions, TypeormOption } from './types';
|
||||
|
||||
export const createDBConfig: (
|
||||
register: ConfigureRegister<RePartial<DBConfig>>,
|
||||
) => ConfigureFactory<DBConfig, DBOptions> = (register) => ({
|
||||
register,
|
||||
hook: (configure, value) => createDBOptions(value),
|
||||
defaultRegister: () => ({
|
||||
common: { charset: 'utf8mb4', logging: ['error'] },
|
||||
connections: [],
|
||||
}),
|
||||
});
|
||||
|
||||
export const createDBOptions = (options: DBConfig) => {
|
||||
const newOptions: DBOptions = {
|
||||
common: deepMerge(
|
||||
{ charset: 'utf8mb4', logging: ['error'] },
|
||||
options.common ?? {},
|
||||
'replace',
|
||||
),
|
||||
connections: createConnectionOptions(options.connections ?? []),
|
||||
};
|
||||
newOptions.connections = newOptions.connections.map((connection) => {
|
||||
const entities = connection.entities ?? [];
|
||||
const newOption = { ...connection, entities };
|
||||
return deepMerge(
|
||||
newOptions.common,
|
||||
{ ...newOption, autoLoadEntities: true } as any,
|
||||
'replace',
|
||||
) as TypeormOption;
|
||||
});
|
||||
return newOptions;
|
||||
};
|
||||
@@ -1,10 +1,14 @@
|
||||
import { DynamicModule, Module, Provider, Type } from '@nestjs/common';
|
||||
import { DynamicModule, Module, ModuleMetadata, Provider, Type } from '@nestjs/common';
|
||||
import { getDataSourceToken, TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
|
||||
|
||||
import { DataSource, ObjectType } from 'typeorm';
|
||||
|
||||
import { CUSTOM_REPOSITORY_METADATA } from '@/modules/database/constants';
|
||||
|
||||
import { Configure } from '../config/configure';
|
||||
|
||||
import { panic } from '../core/helpers';
|
||||
|
||||
import {
|
||||
DataExistConstraint,
|
||||
TreeUniqueConstraint,
|
||||
@@ -12,21 +16,31 @@ import {
|
||||
UniqueConstraint,
|
||||
UniqueExistConstraint,
|
||||
} from './constraints';
|
||||
import { DBOptions } from './types';
|
||||
|
||||
@Module({})
|
||||
export class DatabaseModule {
|
||||
static forRoot(configRegister: () => TypeOrmModuleOptions): DynamicModule {
|
||||
static async forRoot(configure: Configure): Promise<DynamicModule> {
|
||||
if (!configure.has('database')) {
|
||||
panic({ message: 'Database config not exists' });
|
||||
}
|
||||
const { connections } = await configure.get<DBOptions>('database');
|
||||
const imports: ModuleMetadata['imports'] = [];
|
||||
for (const connection of connections) {
|
||||
imports.push(TypeOrmModule.forRoot(connection as TypeOrmModuleOptions));
|
||||
}
|
||||
const providers: ModuleMetadata['providers'] = [
|
||||
DataExistConstraint,
|
||||
UniqueConstraint,
|
||||
UniqueExistConstraint,
|
||||
TreeUniqueConstraint,
|
||||
TreeUniqueExistContraint,
|
||||
];
|
||||
return {
|
||||
global: true,
|
||||
module: DatabaseModule,
|
||||
imports: [TypeOrmModule.forRoot(configRegister())],
|
||||
providers: [
|
||||
DataExistConstraint,
|
||||
UniqueConstraint,
|
||||
UniqueExistConstraint,
|
||||
TreeUniqueConstraint,
|
||||
TreeUniqueExistContraint,
|
||||
],
|
||||
imports,
|
||||
providers,
|
||||
};
|
||||
}
|
||||
static forRepository<T extends Type<any>>(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
|
||||
import {
|
||||
FindTreeOptions,
|
||||
ObjectLiteral,
|
||||
@@ -67,3 +68,15 @@ export type RepositoryType<T extends ObjectLiteral> =
|
||||
| TreeRepository<T>
|
||||
| BaseRepository<T>
|
||||
| BaseTreeRepository<T>;
|
||||
|
||||
export type DBConfig = {
|
||||
common: RecordAny;
|
||||
connections: Array<TypeOrmModuleOptions & { name?: string }>;
|
||||
};
|
||||
|
||||
export type TypeormOption = Omit<TypeOrmModuleOptions, 'name' | 'migrations'> & { name: string };
|
||||
|
||||
export type DBOptions = RecordAny & {
|
||||
common: RecordAny;
|
||||
connections: TypeormOption[];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user