Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prisma module #764

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions examples/bri-3/prisma/prisma.service.ts

This file was deleted.

2 changes: 1 addition & 1 deletion examples/bri-3/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { classes } from '@automapper/classes';
import { AutomapperModule } from '@automapper/nestjs';
import { Module } from '@nestjs/common';
import { APP_GUARD } from '@nestjs/core';
import { PrismaService } from '../prisma/prisma.service';
import { AuthModule } from './bri/auth/auth.module';
import { DidJwtAuthGuard } from './bri/auth/guards/didJwt.guard';
import { AuthzModule } from './bri/authz/authz.module';
Expand All @@ -18,6 +17,7 @@ import { WorkgroupsModule } from './bri/workgroup/workgroup.module';
import { ZeroKnowledgeProofModule } from './bri/zeroKnowledgeProof/zeroKnowledgeProof.module';
import { EncryptionModule } from './shared/encryption/encryption.module';
import { LoggingModule } from './shared/logging/logging.module';
import { PrismaService } from './shared/prisma/prisma.service';

@Module({
imports: [
Expand Down
2 changes: 1 addition & 1 deletion examples/bri-3/src/bri/authz/guards/authz.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
Injectable,
} from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { PrismaService } from '../../../../prisma/prisma.service';
import { IS_PUBLIC_ENDPOINT_METADATA_KEY } from '../../decorators/public-endpoint';
import { AuthzFactory } from '../authz.factory';
import { CHECK_AUTHZ_METADATA_KEY, IRequirement } from './authz.decorator';
import { PrismaService } from '../../../shared/prisma/prisma.service';

// helper map to know which relations to include in generic prisma query
const prismaTypeToQueryIncludeMap = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs';
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../../../prisma/prisma.service';
import { EncryptionService } from '../../../shared/encryption/encryption.service';
import { BpiMessage } from '../models/bpiMessage';
import { PrismaService } from '../../../shared/prisma/prisma.service';

@Injectable()
export class BpiMessageStorageAgent extends PrismaService {
export class BpiMessageStorageAgent {
constructor(
@InjectMapper() private mapper: Mapper,
private readonly encryptionService: EncryptionService,
) {
super();
}
private readonly prisma: PrismaService,
) {}

async getBpiMessageById(id: string): Promise<BpiMessage | undefined> {
const bpiMessageModel = await this.message.findUnique({
const bpiMessageModel = await this.prisma.message.findUnique({
where: { id },
include: { fromBpiSubject: true, toBpiSubject: true },
});
Expand All @@ -33,15 +32,15 @@ export class BpiMessageStorageAgent extends PrismaService {
}

async getAllBpiMessages(): Promise<BpiMessage[]> {
const bpiMessageModels = await this.message.findMany();
const bpiMessageModels = await this.prisma.message.findMany();

return bpiMessageModels.map((bpiMessageModel) => {
return this.mapper.map(bpiMessageModel, BpiMessage, BpiMessage);
});
}

async storeNewBpiMessage(bpiMessage: BpiMessage): Promise<BpiMessage> {
const newBpiMessageModel = await this.message.create({
const newBpiMessageModel = await this.prisma.message.create({
data: {
id: bpiMessage.id,
fromBpiSubjectId: bpiMessage.fromBpiSubjectId,
Expand All @@ -57,7 +56,7 @@ export class BpiMessageStorageAgent extends PrismaService {
}

async updateBpiMessage(bpiMessage: BpiMessage): Promise<BpiMessage> {
const updatedBpiMessageModel = await this.message.update({
const updatedBpiMessageModel = await this.prisma.message.update({
where: { id: bpiMessage.id },
data: {
id: bpiMessage.id,
Expand All @@ -74,7 +73,7 @@ export class BpiMessageStorageAgent extends PrismaService {
}

async deleteBpiMessage(bpiMessage: BpiMessage): Promise<void> {
await this.message.delete({
await this.prisma.message.delete({
where: { id: bpiMessage.id },
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
} from './err.messages';
import { MessageController } from './messages.controller';
import { mockDeep, DeepMockProxy } from 'jest-mock-extended';
import { ethers } from 'ethers';
import { PrismaService } from '../../../shared/prisma/prisma.service';
import { PrismaClient } from '@prisma/client';

describe('MessageController', () => {
let mController: MessageController;
Expand Down Expand Up @@ -108,6 +109,8 @@ describe('MessageController', () => {
.useValue(mockDeep<BpiMessageStorageAgent>())
.overrideProvider(BpiSubjectStorageAgent)
.useValue(mockDeep<BpiSubjectStorageAgent>())
.overrideProvider(PrismaService)
.useValue(mockDeep<PrismaClient>())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is ok, but what if we add something else to prismaService, wouldn't it make sense to mock PrismaService and not PrismaClient?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that seems to make the most sense. I will leave that to be changed just-in-time given its a small change.

.compile();

mController = app.get<MessageController>(MessageController);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { GetBpiMessageByIdQueryHandler } from './capabilities/getBpiMessageById/
import { ProcessInboundMessageCommandHandler } from './capabilities/processInboundMessage/processInboundMessageCommand.handler';
import { UpdateBpiMessageCommandHandler } from './capabilities/updateBpiMessage/updateBpiMessageCommand.handler';
import { CommunicationProfile } from './communicaton.profile';
import { PrismaModule } from '../../shared/prisma/prisma.module';

export const CommandHandlers = [
CreateBpiMessageCommandHandler,
Expand All @@ -32,6 +33,7 @@ export const QueryHandlers = [GetBpiMessageByIdQueryHandler];
SubjectModule,
LoggingModule,
EncryptionModule,
PrismaModule,
],
controllers: [MessageController],
providers: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs';
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../../../../../prisma/prisma.service';
import { NOT_FOUND_ERR_MESSAGE } from '../api/err.messages';
import { Injectable } from '@nestjs/common';
import { BpiSubjectAccount } from '../models/bpiSubjectAccount';
import { PrismaService } from '../../../../shared/prisma/prisma.service';

// Repositories are the only places that talk the Prisma language of models.
// They are always mapped to and from domain objects so that the business layer of the application
// does not have to care about the ORM.
@Injectable()
export class BpiSubjectAccountStorageAgent extends PrismaService {
constructor(@InjectMapper() private readonly mapper: Mapper) {
super();
}
export class BpiSubjectAccountStorageAgent {
constructor(
@InjectMapper() private readonly mapper: Mapper,
private readonly prisma: PrismaService,
) {}

async getBpiSubjectAccountById(
id: string,
): Promise<BpiSubjectAccount | undefined> {
const bpiSubjectAccountModel = await this.bpiSubjectAccount.findUnique({
where: { id: id },
include: { ownerBpiSubject: true, creatorBpiSubject: true },
});
const bpiSubjectAccountModel =
await this.prisma.bpiSubjectAccount.findUnique({
where: { id: id },
include: { ownerBpiSubject: true, creatorBpiSubject: true },
});

if (!bpiSubjectAccountModel) {
return undefined;
Expand All @@ -34,9 +35,10 @@ export class BpiSubjectAccountStorageAgent extends PrismaService {
}

async getAllBpiSubjectAccounts(): Promise<BpiSubjectAccount[]> {
const bpiSubjectAccountsModels = await this.bpiSubjectAccount.findMany({
include: { ownerBpiSubject: true, creatorBpiSubject: true },
});
const bpiSubjectAccountsModels =
await this.prisma.bpiSubjectAccount.findMany({
include: { ownerBpiSubject: true, creatorBpiSubject: true },
});
return bpiSubjectAccountsModels.map((bp) => {
return this.mapper.map(bp, BpiSubjectAccount, BpiSubjectAccount);
});
Expand All @@ -45,17 +47,18 @@ export class BpiSubjectAccountStorageAgent extends PrismaService {
async storeNewBpiSubjectAccount(
bpiSubjectAccount: BpiSubjectAccount,
): Promise<BpiSubjectAccount> {
const newBpiSubjectAccountModel = await this.bpiSubjectAccount.create({
data: {
creatorBpiSubjectId: bpiSubjectAccount.creatorBpiSubject.id,
ownerBpiSubjectId: bpiSubjectAccount.ownerBpiSubject.id,
authenticationPolicy: bpiSubjectAccount.authenticationPolicy,
authorizationPolicy: bpiSubjectAccount.authorizationPolicy,
verifiableCredential: bpiSubjectAccount.verifiableCredential,
recoveryKey: bpiSubjectAccount.recoveryKey,
},
include: { ownerBpiSubject: true, creatorBpiSubject: true },
});
const newBpiSubjectAccountModel =
await this.prisma.bpiSubjectAccount.create({
data: {
creatorBpiSubjectId: bpiSubjectAccount.creatorBpiSubject.id,
ownerBpiSubjectId: bpiSubjectAccount.ownerBpiSubject.id,
authenticationPolicy: bpiSubjectAccount.authenticationPolicy,
authorizationPolicy: bpiSubjectAccount.authorizationPolicy,
verifiableCredential: bpiSubjectAccount.verifiableCredential,
recoveryKey: bpiSubjectAccount.recoveryKey,
},
include: { ownerBpiSubject: true, creatorBpiSubject: true },
});

return this.mapper.map(
newBpiSubjectAccountModel,
Expand All @@ -67,18 +70,19 @@ export class BpiSubjectAccountStorageAgent extends PrismaService {
async updateBpiSubjectAccount(
bpiSubjectAccount: BpiSubjectAccount,
): Promise<BpiSubjectAccount> {
const newBpiSubjectAccountModel = await this.bpiSubjectAccount.update({
where: { id: bpiSubjectAccount.id },
data: {
creatorBpiSubjectId: bpiSubjectAccount.creatorBpiSubject.id,
ownerBpiSubjectId: bpiSubjectAccount.ownerBpiSubject.id,
authenticationPolicy: bpiSubjectAccount.authenticationPolicy,
authorizationPolicy: bpiSubjectAccount.authorizationPolicy,
verifiableCredential: bpiSubjectAccount.verifiableCredential,
recoveryKey: bpiSubjectAccount.recoveryKey,
},
include: { ownerBpiSubject: true, creatorBpiSubject: true },
});
const newBpiSubjectAccountModel =
await this.prisma.bpiSubjectAccount.update({
where: { id: bpiSubjectAccount.id },
data: {
creatorBpiSubjectId: bpiSubjectAccount.creatorBpiSubject.id,
ownerBpiSubjectId: bpiSubjectAccount.ownerBpiSubject.id,
authenticationPolicy: bpiSubjectAccount.authenticationPolicy,
authorizationPolicy: bpiSubjectAccount.authorizationPolicy,
verifiableCredential: bpiSubjectAccount.verifiableCredential,
recoveryKey: bpiSubjectAccount.recoveryKey,
},
include: { ownerBpiSubject: true, creatorBpiSubject: true },
});

return this.mapper.map(
newBpiSubjectAccountModel,
Expand All @@ -90,7 +94,7 @@ export class BpiSubjectAccountStorageAgent extends PrismaService {
async deleteBpiSubjectAccount(
bpiSubjectAccount: BpiSubjectAccount,
): Promise<void> {
await this.bpiSubjectAccount.delete({
await this.prisma.bpiSubjectAccount.delete({
where: { id: bpiSubjectAccount.id },
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { GetAllBpiSubjectAccountsQueryHandler } from './capabilities/getAllBpiSu
import { GetBpiSubjectAccountByIdQueryHandler } from './capabilities/getBpiSubjectAccountById/getBpiSubjectAccountByIdQuery.handler';
import { UpdateBpiSubjectAccountCommandHandler } from './capabilities/updateBpiSubjectAccount/updateBpiSubjectAccountCommand.handler';
import { SubjectAccountsProfile } from './subjectAccounts.profile';
import { PrismaModule } from '../../../shared/prisma/prisma.module';

export const CommandHandlers = [
CreateBpiSubjectAccountCommandHandler,
Expand All @@ -22,7 +23,7 @@ export const QueryHandlers = [
];

@Module({
imports: [CqrsModule, SubjectModule],
imports: [CqrsModule, SubjectModule, PrismaModule],
controllers: [SubjectAccountController],
providers: [
...CommandHandlers,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../../../../../prisma/prisma.service';
import { NOT_FOUND_ERR_MESSAGE } from '../api/err.messages';
import { BpiSubject } from '../models/bpiSubject';
import { InjectMapper } from '@automapper/nestjs';
import { Mapper } from '@automapper/core';
import { BpiSubjectRole, BpiSubjectRoleName } from '../models/bpiSubjectRole';
import { PrismaService } from '../../../../shared/prisma/prisma.service';

// Repositories are the only places that talk the Prisma language of models.
// They are always mapped to and from domain objects so that the business layer of the application
// does not have to care about the ORM.
@Injectable()
export class BpiSubjectStorageAgent extends PrismaService {
constructor(@InjectMapper() private mapper: Mapper) {
super();
}
export class BpiSubjectStorageAgent {
constructor(
@InjectMapper() private mapper: Mapper,
private readonly prisma: PrismaService,
) {}

async getBpiSubjectById(id: string): Promise<BpiSubject | undefined> {
const bpiSubjectModel = await this.bpiSubject.findUnique({
const bpiSubjectModel = await this.prisma.bpiSubject.findUnique({
where: { id },
});

Expand All @@ -28,14 +29,14 @@ export class BpiSubjectStorageAgent extends PrismaService {
}

async getAllBpiSubjects(): Promise<BpiSubject[]> {
const bpiSubjectModels = await this.bpiSubject.findMany();
const bpiSubjectModels = await this.prisma.bpiSubject.findMany();
return bpiSubjectModels.map((bpiSubjectModel) => {
return this.mapper.map(bpiSubjectModel, BpiSubject, BpiSubject);
});
}

async getBpiSubjectsById(ids: string[]): Promise<BpiSubject[]> {
const bpiSubjectModels = await this.bpiSubject.findMany({
const bpiSubjectModels = await this.prisma.bpiSubject.findMany({
where: {
id: { in: ids },
},
Expand All @@ -49,7 +50,7 @@ export class BpiSubjectStorageAgent extends PrismaService {
async getBpiSubjectRoleByName(
name: BpiSubjectRoleName,
): Promise<BpiSubjectRole> {
const bpiSubjectRole = await this.bpiSubjectRole.findUnique({
const bpiSubjectRole = await this.prisma.bpiSubjectRole.findUnique({
where: { name },
});

Expand All @@ -61,7 +62,7 @@ export class BpiSubjectStorageAgent extends PrismaService {

async storeNewBpiSubject(bpiSubject: BpiSubject): Promise<BpiSubject> {
bpiSubject.publicKey = bpiSubject.publicKey.toLowerCase();
const newBpiSubjectModel = await this.bpiSubject.create({
const newBpiSubjectModel = await this.prisma.bpiSubject.create({
data: {
...bpiSubject,
roles: {
Expand All @@ -78,7 +79,7 @@ export class BpiSubjectStorageAgent extends PrismaService {
}

async updateBpiSubject(bpiSubject: BpiSubject): Promise<BpiSubject> {
const updatedBpiSubjectModel = await this.bpiSubject.update({
const updatedBpiSubjectModel = await this.prisma.bpiSubject.update({
where: { id: bpiSubject.id },
data: {
...bpiSubject,
Expand All @@ -95,13 +96,13 @@ export class BpiSubjectStorageAgent extends PrismaService {
}

async deleteBpiSubject(bpiSubject: BpiSubject): Promise<void> {
await this.bpiSubject.delete({
await this.prisma.bpiSubject.delete({
where: { id: bpiSubject.id },
});
}

async getBpiSubjectByPublicKey(publicKey: string): Promise<BpiSubject> {
const bpiSubjectModel = await this.bpiSubject.findFirst({
const bpiSubjectModel = await this.prisma.bpiSubject.findFirst({
where: {
publicKey: publicKey,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { UpdateBpiSubjectCommandHandler } from './capabilities/updateBpiSubject/
import { BpiSubjectStorageAgent } from './agents/bpiSubjectsStorage.agent';
import { SubjectsProfile } from './subjects.profile';
import { AuthzModule } from '../../authz/authz.module';
import { PrismaModule } from '../../../shared/prisma/prisma.module';

export const CommandHandlers = [
CreateBpiSubjectCommandHandler,
Expand All @@ -22,7 +23,7 @@ export const QueryHandlers = [
];

@Module({
imports: [CqrsModule, AuthzModule],
imports: [CqrsModule, AuthzModule, PrismaModule],
controllers: [SubjectController],
providers: [
...CommandHandlers,
Expand Down
Loading
Loading