Skip to content

Commit

Permalink
refactor: update the ways to use the nestjs services inside the rest …
Browse files Browse the repository at this point in the history
…and graphql module
  • Loading branch information
floross committed Feb 6, 2023
1 parent 49720ca commit cd5fc45
Show file tree
Hide file tree
Showing 53 changed files with 611 additions and 196 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"cdktf",
"codecov",
"Concat",
"datasources",
"datetime",
"dbml",
"deepmerge",
Expand Down
27 changes: 12 additions & 15 deletions apps/api/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
FileStorageModule,
GraphQLModule,
MailerModule,
ModelsModule,
ModelsRestModule,
ModelsServicesModule,
PasswordModule,
} from './modules';

Expand All @@ -16,25 +17,21 @@ import { LoggerModule } from '@trxn/nestjs-core';
@Module({
imports: [
// Internal database services
DatabaseModule,

// DatabaseModule,
// API modules
ModelsModule,
GraphQLModule,

ModelsServicesModule,
ModelsRestModule,
// GraphQLModule,
// Authentication modules
AuthenticationModule,
PasswordModule,

// AuthenticationModule,
// PasswordModule,
// Miscellaneous
MailerModule,
FileStorageModule,

// MailerModule,
// FileStorageModule,
// Logger
LoggerModule,

// LoggerModule,
// Cli
ConsoleModule,
// ConsoleModule,
],
})
export class AppModule {}
4 changes: 2 additions & 2 deletions apps/api/src/app/modules/authentication.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';

import { CaslModule } from './casl.module';
import { EncryptionModule } from './encryption.module';
import { ModelsModule } from './models.module';
import { ModelsServicesModule } from './models-services.module';

import { getSelectPrismaUserQuery } from '@trxn/generated-casl';
import { USER_SERVICE } from '@trxn/generated-nestjs-models-common';
Expand All @@ -18,7 +18,7 @@ import { CaslExceptionInterceptor, PoliciesGuard } from '@trxn/nestjs-casl';
imports: [
CaslModule,
TraxionAuthenticationModule.registerAsync({
imports: [ModelsModule, EncryptionModule],
imports: [ModelsServicesModule, EncryptionModule],
useFactory: (userService, encryptionService) => ({
user: {
customSelect: getSelectPrismaUserQuery(),
Expand Down
10 changes: 7 additions & 3 deletions apps/api/src/app/modules/database.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Global, Module } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { PrismaClient } from '@prisma/client';

import { PrismaExceptionInterceptor } from '@trxn/nestjs-core';
import { DatabaseModule as TraxionDatabaseModule } from '@trxn/nestjs-database';

@Global()
@Module({
imports: [TraxionDatabaseModule.register({})],
imports: [
TraxionDatabaseModule.register({
// prismaClient: new PrismaClient(),
}),
],
exports: [TraxionDatabaseModule],
providers: [
{ provide: APP_INTERCEPTOR, useClass: PrismaExceptionInterceptor },
Expand Down
3 changes: 2 additions & 1 deletion apps/api/src/app/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export * from './encryption.module';
export * from './file-storage.module';
export * from './graphql.module';
export * from './mailer.module';
export * from './models.module';
export * from './models-services.module';
export * from './rest.module';
export * from './password.module';
32 changes: 32 additions & 0 deletions apps/api/src/app/modules/models-services.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Module } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

import { DatabaseModule } from './database.module';
import { EncryptionModule } from './encryption.module';

import {
ModelsModule as TraxionModelsModule,
UserMiddlewareModule,
} from '@trxn/generated-nestjs-models';
import { ModelsServicesModule as ServicesModule } from '@trxn/generated-nestjs-models-common';
import { ModelsRestModule } from '@trxn/generated-nestjs-models-rest';
import { BcryptService } from '@trxn/nestjs-bcrypt';
import { DATABASE_SERVICE } from '@trxn/nestjs-database';

@Module({
imports: [
// UserMiddlewareModule.registerAsync({
// imports: [EncryptionModule],
// inject: [BcryptService],
// useFactory: (bcryptService: BcryptService) => ({
// encryptionService: bcryptService,
// }),
// }),

ServicesModule.register({
imports: [DatabaseModule],
}),
],
exports: [ServicesModule],
})
export class ModelsServicesModule {}
24 changes: 0 additions & 24 deletions apps/api/src/app/modules/models.module.ts

This file was deleted.

4 changes: 2 additions & 2 deletions apps/api/src/app/modules/password.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Module } from '@nestjs/common';

import { EncryptionModule } from './encryption.module';
import { MailerModule } from './mailer.module';
import { ModelsModule } from './models.module';
import { ModelsServicesModule } from './models-services.module';

import { USER_SERVICE } from '@trxn/generated-nestjs-models-common';
import { BcryptService, EncryptionService } from '@trxn/nestjs-bcrypt';
Expand All @@ -12,7 +12,7 @@ import { PasswordModule as TraxionPasswordModule } from '@trxn/nestjs-password';
@Module({
imports: [
TraxionPasswordModule.registerAsync({
imports: [ModelsModule, MailerModule, EncryptionModule],
imports: [ModelsServicesModule, MailerModule, EncryptionModule],
useFactory: (
userService,
resetPasswordSendEmailService: ResetPasswordSendEmailService,
Expand Down
15 changes: 15 additions & 0 deletions apps/api/src/app/modules/rest.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Module } from '@nestjs/common';

import { ModelsServicesModule } from './models-services.module';

import { ModelsRestModule as RestModule } from '@trxn/generated-nestjs-models-rest';

@Module({
imports: [
RestModule.register({
imports: [ModelsServicesModule],
}),
],
exports: [RestModule],
})
export class ModelsRestModule {}
2 changes: 1 addition & 1 deletion hapify-models.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
},
{
"name": "name",
"properties": ["unique", "label", "searchable"],
"properties": ["unique", "label", "searchable", "sortable"],
"type": "string"
},
{
Expand Down
2 changes: 1 addition & 1 deletion libs/generated/prisma/prisma/schemas/base.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ datasource db {
generator client {
provider = "prisma-client-js"
output = "../../../../node_modules/.prisma/client"
previewFeatures = ["interactiveTransactions"]
previewFeatures = ["clientExtensions"]
}

generator nestjsGraphql {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
<<if Model not noGuest>>Public,<<endif>>
} from '@trxn/nestjs-core';
import {
DatabaseService,
DATABASE_SERVICE,
} from '@trxn/nestjs-database';
import {
Args,
Expand All @@ -40,7 +40,7 @@ import {
import { GraphQLResolveInfo } from 'graphql';
import { ForbiddenError } from 'apollo-server-core';
import { PrismaSelect } from '@paljs/plugins';
import { User as PrismaUser } from '@prisma/client';
import { User as PrismaUser, PrismaClient } from '@prisma/client';

import {
<<Model pascal>>Service,
Expand Down Expand Up @@ -83,7 +83,8 @@ export class <<Model pascal>>Resolver {
@Inject(<<ref constant>>_SERVICE)
protected <<ref camel>>Service: <<ref pascal>>Service,
<<endfor>>
protected databaseService: DatabaseService,
@Inject(DATABASE_SERVICE)
protected databaseService: PrismaClient,
) {}

/**
Expand Down
32 changes: 31 additions & 1 deletion libs/hapify/templates/nestjs-models-common/.hapifyrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,48 @@ module.exports = {
engine: 'hpf',
input: 'all',
},
{
path: 'generated/nestjs-models-common/src/models-services.providers.ts',
engine: 'hpf',
input: 'all',
},
{
path: 'generated/nestjs-models-common/src/models-services.module-definition.ts',
engine: 'hpf',
input: 'all',
},
{
path: 'generated/nestjs-models-common/src/models-services.module.ts',
engine: 'hpf',
input: 'all',
},
{
path: 'generated/nestjs-models-common/src/index.ts',
engine: 'hpf',
input: 'all',
},
{
path: 'generated/nestjs-models-common/src/interfaces/index.ts',
engine: 'hpf',
input: 'one',
},
{
path: 'generated/nestjs-models-common/src/interfaces/models-services.interface.ts',
engine: 'hpf',
input: 'one',
},
{
path: 'generated/nestjs-models-common/src/{kebab}/index.ts',
engine: 'hpf',
input: 'one',
},
{
path: 'generated/nestjs-models-common/src/{kebab}/{kebab}-model.constant.ts',
path: 'generated/nestjs-models-common/src/{kebab}/{kebab}-model.constants.ts',
engine: 'hpf',
input: 'one',
},
{
path: 'generated/nestjs-models-common/src/{kebab}/{kebab}-model.providers.ts',
engine: 'hpf',
input: 'one',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Provider } from '@nestjs/common';

import {
<<Model pascal>>Service,
} from './services';

import {
<<Model constant>>_SERVICE,
} from './<<Model kebab>>-model.constants';

export const <<Model constant>>_SERVICES_PROVIDERS: Provider[] = [
{
provide: <<Model constant>>_SERVICE,
useClass: <<Model pascal>>Service,
}
];
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './services';
export * from './<<Model kebab>>-model.constant';
export * from './<<Model kebab>>-model.constants';
export * from './<<Model kebab>>-model.providers';
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { DatabaseService } from '@trxn/nestjs-database';
import { DATABASE_SERVICE } from '@trxn/nestjs-database';

import { Prisma } from '@prisma/client';
import { Prisma, PrismaClient } from '@prisma/client';

export type <<Model pascal>>DatabaseService = Prisma.<<Model pascal>>Delegate<undefined>

export function <<Model camel>>DatabaseServiceFactory(
databaseService: DatabaseService,
databaseService: PrismaClient,
): Prisma.<<Model pascal>>Delegate<undefined> {
return databaseService.<<Model camel>>;
}

export const <<Model camel>>DatabaseServiceInject = [DatabaseService];
export const <<Model camel>>DatabaseServiceInject = [DATABASE_SERVICE];
Loading

0 comments on commit cd5fc45

Please sign in to comment.