mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 13:14:55 +00:00
authentication module done
This commit is contained in:
38
libs/common/src/database/database.module.ts
Normal file
38
libs/common/src/database/database.module.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { SnakeNamingStrategy } from './strategies';
|
||||
import { UserEntity } from '../modules/user/entities/user.entity';
|
||||
import { UserSessionEntity } from '../modules/session/entities/session.entity';
|
||||
import { UserOtpEntity } from '../modules/user-otp/entities';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forRootAsync({
|
||||
imports: [ConfigModule],
|
||||
inject: [ConfigService],
|
||||
useFactory: (configService: ConfigService) => ({
|
||||
name: 'default',
|
||||
type: 'postgres',
|
||||
host: configService.get('DB_HOST'),
|
||||
port: configService.get('DB_PORT'),
|
||||
username: configService.get('DB_USER'),
|
||||
password: configService.get('DB_PASSWORD'),
|
||||
database: configService.get('DB_NAME'),
|
||||
entities: [UserEntity, UserSessionEntity,UserOtpEntity],
|
||||
namingStrategy: new SnakeNamingStrategy(),
|
||||
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
||||
logging: true,
|
||||
extra: {
|
||||
charset: 'utf8mb4',
|
||||
max: 20, // set pool max size
|
||||
idleTimeoutMillis: 5000, // close idle clients after 5 second
|
||||
connectionTimeoutMillis: 11_000, // return an error after 11 second if connection could not be established
|
||||
maxUses: 7500, // close (and replace) a connection after it has been used 7500 times (see below for discussion)
|
||||
},
|
||||
continuationLocalStorage: true,
|
||||
}),
|
||||
}),
|
||||
],
|
||||
})
|
||||
export class DatabaseModule {}
|
||||
1
libs/common/src/database/strategies/index.ts
Normal file
1
libs/common/src/database/strategies/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './snack-naming.strategy';
|
||||
62
libs/common/src/database/strategies/snack-naming.strategy.ts
Normal file
62
libs/common/src/database/strategies/snack-naming.strategy.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import { DefaultNamingStrategy, NamingStrategyInterface } from 'typeorm';
|
||||
import { snakeCase } from 'typeorm/util/StringUtils';
|
||||
|
||||
export class SnakeNamingStrategy
|
||||
extends DefaultNamingStrategy
|
||||
implements NamingStrategyInterface
|
||||
{
|
||||
tableName(className: string, customName: string): string {
|
||||
return customName ? customName : snakeCase(className);
|
||||
}
|
||||
|
||||
columnName(
|
||||
propertyName: string,
|
||||
customName: string,
|
||||
embeddedPrefixes: string[],
|
||||
): string {
|
||||
return (
|
||||
snakeCase(embeddedPrefixes.join('_')) +
|
||||
(customName ? customName : snakeCase(propertyName))
|
||||
);
|
||||
}
|
||||
|
||||
relationName(propertyName: string): string {
|
||||
return snakeCase(propertyName);
|
||||
}
|
||||
|
||||
joinColumnName(relationName: string, referencedColumnName: string): string {
|
||||
return snakeCase(relationName + '_' + referencedColumnName);
|
||||
}
|
||||
|
||||
joinTableName(
|
||||
firstTableName: string,
|
||||
secondTableName: string,
|
||||
firstPropertyName: any,
|
||||
_secondPropertyName: string,
|
||||
): string {
|
||||
return snakeCase(
|
||||
firstTableName +
|
||||
'_' +
|
||||
firstPropertyName.replaceAll(/\./gi, '_') +
|
||||
'_' +
|
||||
secondTableName,
|
||||
);
|
||||
}
|
||||
|
||||
joinTableColumnName(
|
||||
tableName: string,
|
||||
propertyName: string,
|
||||
columnName?: string,
|
||||
): string {
|
||||
return snakeCase(
|
||||
tableName + '_' + (columnName ? columnName : propertyName),
|
||||
);
|
||||
}
|
||||
|
||||
classTableInheritanceParentColumnName(
|
||||
parentTableName: string,
|
||||
parentTableIdPropertyName: string,
|
||||
): string {
|
||||
return snakeCase(`${parentTableName}_${parentTableIdPropertyName}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user