mirror of
https://github.com/twentyhq/twenty.git
synced 2024-11-30 23:23:47 +03:00
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:
parent
cafffd973f
commit
56dc87a60f
16
server/ormconfig.ts
Normal file
16
server/ormconfig.ts
Normal 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;
|
@ -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: [
|
||||
|
29
server/src/coreV2/core.module.ts
Normal file
29
server/src/coreV2/core.module.ts
Normal 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 {}
|
19
server/src/coreV2/userv2/user.dto.ts
Normal file
19
server/src/coreV2/userv2/user.dto.ts
Normal 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;
|
||||
}
|
19
server/src/coreV2/userv2/user.entity.ts
Normal file
19
server/src/coreV2/userv2/user.entity.ts
Normal 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;
|
||||
}
|
14
server/src/coreV2/userv2/user.module.ts
Normal file
14
server/src/coreV2/userv2/user.module.ts
Normal 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 {}
|
28
server/src/coreV2/userv2/user.resolver.ts
Normal file
28
server/src/coreV2/userv2/user.resolver.ts
Normal 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();
|
||||
}
|
||||
}
|
18
server/src/coreV2/userv2/user.service.ts
Normal file
18
server/src/coreV2/userv2/user.service.ts
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user