chore: create a new TypeORM config using @nestjs/typeorm for public schema (#2241)

* chore: create a new TypeORM config using @nestjs/typeorm for public schema

Co-authored-by: v1b3m <vibenjamin6@gmail.com>

* Refactor according to review

Co-authored-by: v1b3m <vibenjamin6@gmail.com>

* Refactor according to review

Co-authored-by: v1b3m <vibenjamin6@gmail.com>

* Refactor according to review

Co-authored-by: v1b3m <vibenjamin6@gmail.com>

* Refactor according to review

Co-authored-by: v1b3m <vibenjamin6@gmail.com>

* Remove unnecessary changes

Co-authored-by: v1b3m <vibenjamin6@gmail.com>

* Refactor imports

Co-authored-by: v1b3m <vibenjamin6@gmail.com>

---------

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
This commit is contained in:
gitstart-twenty 2023-11-08 16:11:52 +05:45 committed by GitHub
parent cafffd973f
commit 56dc87a60f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 145 additions and 0 deletions

16
server/ormconfig.ts Normal file
View File

@ -0,0 +1,16 @@
import dotenv from 'dotenv';
import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions';
dotenv.config();
export default {
url: process.env.PG_DATABASE_URL,
type: 'postgres',
entities: [__dirname + '/src/coreV2/**/*.entity{.ts,.js}'],
synchronize: false,
migrationsRun: true,
migrationsTableName: '_typeorm_migrations',
migrations: [__dirname + '/migrations/**/*{.ts,.js}'],
cli: {
migrationsDir: __dirname + '/migrations',
},
} as PostgresConnectionOptions;

View File

@ -12,6 +12,7 @@ import { TokenExpiredError, JsonWebTokenError, verify } from 'jsonwebtoken';
import { AppService } from './app.service';
import { CoreModule } from './core/core.module';
import { CoreV2Module } from './coreV2/core.module';
import { IntegrationsModule } from './integrations/integrations.module';
import { PrismaModule } from './database/prisma.module';
import { HealthModule } from './health/health.module';
@ -102,6 +103,7 @@ import { ExceptionFilter } from './filters/exception.filter';
AbilityModule,
IntegrationsModule,
CoreModule,
CoreV2Module,
TenantModule,
],
providers: [

View File

@ -0,0 +1,29 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { GraphQLModule } from '@nestjs/graphql';
import { YogaDriverConfig, YogaDriver } from '@graphql-yoga/nestjs';
import GraphQLJSON from 'graphql-type-json';
// eslint-disable-next-line no-restricted-imports
import config from '../../ormconfig';
import { UserModule } from './userv2/user.module';
@Module({
imports: [
TypeOrmModule.forRoot(config),
GraphQLModule.forRoot<YogaDriverConfig>({
context: ({ req }) => ({ req }),
driver: YogaDriver,
autoSchemaFile: true,
include: [CoreV2Module],
resolvers: { JSON: GraphQLJSON },
plugins: [],
path: '/graphqlv2',
}),
UserModule,
],
exports: [UserModule],
})
export class CoreV2Module {}

View File

@ -0,0 +1,19 @@
import { Field, ID, ObjectType } from '@nestjs/graphql';
@ObjectType()
export class TUser {
@Field(() => ID, { nullable: false })
id: number;
@Field(() => String, { nullable: true })
firstName: string | null;
@Field(() => String, { nullable: true })
lastName: string | null;
@Field(() => String, { nullable: false })
email: string;
@Field(() => Boolean, { nullable: false, defaultValue: false })
emailVerified: boolean;
}

View File

@ -0,0 +1,19 @@
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity('users')
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
firstName: string;
@Column()
lastName: string;
@Column()
email: string;
@Column({ default: false })
emailVerified: boolean;
}

View File

@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AbilityModule } from 'src/ability/ability.module';
import { User } from './user.entity';
import { UserResolver } from './user.resolver';
import { UserService } from './user.service';
@Module({
imports: [TypeOrmModule.forFeature([User]), AbilityModule],
providers: [UserService, UserResolver],
})
export class UserModule {}

View File

@ -0,0 +1,28 @@
import { Resolver, Query } from '@nestjs/graphql';
import { UseFilters, UseGuards } from '@nestjs/common';
import { ExceptionFilter } from 'src/filters/exception.filter';
import { JwtAuthGuard } from 'src/guards/jwt.auth.guard';
import { AbilityGuard } from 'src/guards/ability.guard';
import { CheckAbilities } from 'src/decorators/check-abilities.decorator';
import { ReadUserAbilityHandler } from 'src/ability/handlers/user.ability-handler';
import { TUser } from './user.dto';
import { UserService } from './user.service';
import { User } from './user.entity';
@UseGuards(JwtAuthGuard)
@Resolver(() => TUser)
export class UserResolver {
constructor(private readonly userService: UserService) {}
@UseFilters(ExceptionFilter)
@Query(() => [TUser], {
nullable: false,
})
@UseGuards(AbilityGuard)
@CheckAbilities(ReadUserAbilityHandler)
async findManyUserV2(): Promise<Partial<User>[]> {
return this.userService.findAll();
}
}

View File

@ -0,0 +1,18 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './user.entity';
@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private usersRepository: Repository<User>,
) {}
async findAll() {
return this.usersRepository.find();
}
}