switch to redis

This commit is contained in:
sbriat 2023-10-25 09:09:45 +02:00
parent 3505d2a520
commit da82d0f289
15 changed files with 33 additions and 118 deletions

View File

@ -2,5 +2,7 @@
SERVICE_URL=0.0.0.0
SERVICE_PORT=5003
# PRISMA
DATABASE_URL="postgresql://mobicoop:mobicoop@localhost:5432/mobicoop-test?schema=configuration"
# REDIS
REDIS_HOST=v3-redis-test
REDIS_PASSWORD=redis
REDIS_PORT=6380

View File

@ -12,12 +12,8 @@ WORKDIR /usr/src/app
# Copying this first prevents re-running npm install on every code change.
COPY --chown=node:node package*.json ./
# Copy prisma (needed for prisma error types)
COPY --chown=node:node ./prisma prisma
# Install app dependencies using the `npm ci` command instead of `npm install`
RUN npm ci
RUN npx prisma generate
# Bundle app source
COPY --chown=node:node . .
@ -43,9 +39,6 @@ COPY --chown=node:node --from=development /usr/src/app/node_modules ./node_modul
COPY --chown=node:node . .
# Copy prisma (needed for migrations)
COPY --chown=node:node ./prisma prisma
# Run the build command which creates the production bundle
RUN npm run build
@ -70,7 +63,6 @@ COPY --chown=node:node package*.json ./
# Copy the bundled code from the build stage to the production image
COPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modules
COPY --chown=node:node --from=build /usr/src/app/prisma ./prisma
COPY --chown=node:node --from=build /usr/src/app/dist ./dist
# Start the server using the production build

View File

@ -2,14 +2,6 @@
SERVICE_URL=0.0.0.0
SERVICE_PORT=5003
# PRISMA
DATABASE_URL="postgresql://mobicoop:mobicoop@v3-db:5432/mobicoop?schema=public"
# RABBIT MQ
RMQ_URI=amqp://v3-broker:5672
# MESSAGE BROKER
BROKER_IMAGE=rabbitmq:3-alpine
# POSTGRES
POSTGRES_IMAGE=postgres:15.0
# REDIS
REDIS_IMAGE=redis:7.0-alpine
REDIS_PASSWORD=redis

View File

@ -16,9 +16,6 @@ RUN npm ci
# Bundle app source
COPY . .
# Generate prisma client
RUN npx prisma generate
# Create a "dist" folder
RUN npm run build

View File

@ -1,12 +1,12 @@
#!/bin/bash
testlog() {
docker logs v3-db | grep -q "database system is ready to accept connections"
docker logs v3-redis | grep -q "Ready to accept connections"
}
testlog 2> /dev/null
while [ $? -ne 0 ];
do
sleep 5
echo "Waiting for Test DB to be up..."
echo "Waiting for Test Redis to be up..."
testlog 2> /dev/null
done

View File

@ -1,23 +1,12 @@
version: '3.8'
services:
db:
container_name: v3-db
image: ${POSTGRES_IMAGE}
environment:
POSTGRES_DB: mobicoop
POSTGRES_USER: mobicoop
POSTGRES_PASSWORD: mobicoop
redis:
container_name: v3-redis
image: ${REDIS_IMAGE}
command: redis-server --requirepass ${REDIS_PASSWORD}
ports:
- 5432:5432
networks:
- v3-network
broker:
container_name: v3-broker
image: ${BROKER_IMAGE}
ports:
- 5672:5672
- 6379:6379
networks:
- v3-network

View File

@ -17,18 +17,13 @@
"lint:check": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix-dry-run --ignore-path .gitignore",
"pretty:check": "./node_modules/.bin/prettier --check .",
"pretty": "./node_modules/.bin/prettier --write .",
"test": "npm run migrate:test && dotenv -e .env.test jest",
"test": "dotenv -e .env.test jest",
"test:unit": "jest --testPathPattern 'tests/unit/' --verbose",
"test:unit:ci": "jest --testPathPattern 'tests/unit/' --coverage",
"test:integration": "npm run migrate:test && dotenv -e .env.test -- jest --testPathPattern 'tests/integration/' --verbose",
"test:integration:ci": "npm run migrate:test:ci && dotenv -e ci/.env.ci -- jest --testPathPattern 'tests/integration/'",
"test:integration": "dotenv -e .env.test -- jest --testPathPattern 'tests/integration/' --verbose",
"test:integration:ci": "dotenv -e ci/.env.ci -- jest --testPathPattern 'tests/integration/'",
"test:cov": "jest --testPathPattern 'tests/unit/' --coverage",
"test:e2e": "jest --config ./test/jest-e2e.json",
"generate": "docker exec v3-configuration-api sh -c 'npx prisma generate'",
"migrate": "docker exec v3-configuration-api sh -c 'npx prisma migrate dev'",
"migrate:test": "dotenv -e .env.test -- npx prisma migrate deploy",
"migrate:test:ci": "dotenv -e ci/.env.ci -- npx prisma migrate deploy",
"migrate:deploy": "npx prisma migrate deploy"
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@grpc/grpc-js": "^1.9.6",
@ -44,7 +39,6 @@
"@nestjs/microservices": "^10.2.7",
"@nestjs/platform-express": "^10.2.7",
"@nestjs/terminus": "^10.1.1",
"@prisma/client": "^5.4.2",
"@songkeys/nestjs-redis": "^10.0.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
@ -71,7 +65,6 @@
"eslint-plugin-prettier": "^5.0.1",
"jest": "29.7.0",
"prettier": "^3.0.3",
"prisma": "^5.4.2",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "29.1.1",

View File

@ -5,12 +5,6 @@ export const SERVICE_NAME = 'configuration';
export const GRPC_PACKAGE_NAME = 'configuration';
export const GRPC_SERVICE_NAME = 'ConfigurationService';
// messaging
export const CONFIGURATION_SET_ROUTING_KEY = 'configuration.set';
export const CONFIGURATION_DELETED_ROUTING_KEY = 'configuration.deleted';
export const CONFIGURATION_PROPAGATED_ROUTING_KEY = 'configuration.propagated';
// health
export const GRPC_HEALTH_PACKAGE_NAME = 'health';
export const HEALTH_CONFIGURATION_REPOSITORY = 'ConfigurationRepository';
export const HEALTH_CRITICAL_LOGGING_KEY = 'logging.configuration.health.crit';

View File

@ -1,19 +1,10 @@
import {
HealthModule,
HealthModuleOptions,
HealthRepositoryPort,
} from '@mobicoop/health-module';
import { HealthModule, HealthModuleOptions } from '@mobicoop/health-module';
import { MessagerModule } from '@modules/messager/messager.module';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import {
HEALTH_CONFIGURATION_REPOSITORY,
HEALTH_CRITICAL_LOGGING_KEY,
SERVICE_NAME,
} from './app.constants';
import { HEALTH_CRITICAL_LOGGING_KEY, SERVICE_NAME } from './app.constants';
import { MessagePublisherPort } from '@mobicoop/ddd-library';
import { MESSAGE_PUBLISHER } from '@modules/messager/messager.di-tokens';
import { CONFIGURATION_REPOSITORY } from '@modules/configuration/configuration.di-tokens';
import { ConfigurationModule } from '@modules/configuration/configuration.module';
import { EventEmitterModule } from '@nestjs/event-emitter';
import brokerConfig from './config/broker.config';
@ -22,6 +13,7 @@ import paginationConfig from './config/pagination.config';
import serviceConfig from './config/service.config';
import { RedisModule, RedisModuleOptions } from '@songkeys/nestjs-redis';
import redisConfig from './config/redis.config';
import { Transport } from '@nestjs/microservices';
@Module({
imports: [
@ -37,21 +29,24 @@ import redisConfig from './config/redis.config';
}),
EventEmitterModule.forRoot(),
HealthModule.forRootAsync({
imports: [ConfigurationModule, MessagerModule],
inject: [CONFIGURATION_REPOSITORY, MESSAGE_PUBLISHER],
imports: [MessagerModule, ConfigModule],
inject: [MESSAGE_PUBLISHER, ConfigService],
useFactory: async (
configurationRepository: HealthRepositoryPort,
messagePublisher: MessagePublisherPort,
configService: ConfigService,
): Promise<HealthModuleOptions> => ({
serviceName: SERVICE_NAME,
criticalLoggingKey: HEALTH_CRITICAL_LOGGING_KEY,
checkRepositories: [
messagePublisher,
checkMicroservices: [
{
name: HEALTH_CONFIGURATION_REPOSITORY,
repository: configurationRepository,
host: configService.get<string>('redis.host') as string,
port: configService.get<string>('redis.port') as string,
password: configService.get<string>('redis.password'),
name: 'Redis',
transport: Transport.REDIS,
},
],
messagePublisher,
}),
}),
RedisModule.forRootAsync({

View File

@ -1,4 +1 @@
export const CONFIGURATION_MESSAGE_PUBLISHER = Symbol(
'CONFIGURATION_MESSAGE_PUBLISHER',
);
export const CONFIGURATION_REPOSITORY = Symbol('CONFIGURATION_REPOSITORY');

View File

@ -5,11 +5,7 @@ import { SetConfigurationGrpcController } from './interface/grpc-controllers/set
import { SetConfigurationService } from './core/application/commands/set-configuration/set-configuration.service';
import { GetConfigurationQueryHandler } from './core/application/queries/get-configuration/get-configuration.query-handler';
import { ConfigurationMapper } from './configuration.mapper';
import {
CONFIGURATION_MESSAGE_PUBLISHER,
CONFIGURATION_REPOSITORY,
} from './configuration.di-tokens';
import { MessageBrokerPublisher } from '@mobicoop/message-broker-module';
import { CONFIGURATION_REPOSITORY } from './configuration.di-tokens';
import { PopulateService } from './core/application/services/populate.service';
import { ConfigurationRepository } from './infrastructure/configuration.repository';
@ -33,13 +29,6 @@ const repositories: Provider[] = [
},
];
const messagePublishers: Provider[] = [
{
provide: CONFIGURATION_MESSAGE_PUBLISHER,
useExisting: MessageBrokerPublisher,
},
];
@Module({
imports: [CqrsModule],
controllers: [...grpcControllers],
@ -49,7 +38,6 @@ const messagePublishers: Provider[] = [
...mappers,
...providers,
...repositories,
...messagePublishers,
],
exports: [ConfigurationMapper, CONFIGURATION_REPOSITORY],
})

View File

@ -3,7 +3,6 @@ import { v4 } from 'uuid';
import {
ConfigurationProps,
CreateConfigurationProps,
UpdateConfigurationProps,
} from './configuration.types';
export class ConfigurationEntity extends AggregateRoot<ConfigurationProps> {
@ -16,10 +15,6 @@ export class ConfigurationEntity extends AggregateRoot<ConfigurationProps> {
return configuration;
};
update(props: UpdateConfigurationProps): void {
this.props.value = props.value;
}
validate(): void {
// entity business rules validation to protect it's invariant before saving entity to a database
}

View File

@ -10,10 +10,6 @@ export interface CreateConfigurationProps {
value: ConfigurationValue;
}
export interface UpdateConfigurationProps {
value: ConfigurationValue;
}
export enum ConfigurationDomain {
CARPOOL = 'CARPOOL',
PAGINATION = 'PAGINATION',

View File

@ -2,7 +2,6 @@ import { ConfigurationEntity } from '@modules/configuration/core/domain/configur
import {
CreateConfigurationProps,
ConfigurationDomain,
UpdateConfigurationProps,
} from '@modules/configuration/core/domain/configuration.types';
const createConfigurationProps: CreateConfigurationProps = {
@ -13,10 +12,6 @@ const createConfigurationProps: CreateConfigurationProps = {
value: '3',
};
const updateConfigurationProps: UpdateConfigurationProps = {
value: '2',
};
describe('Configuration entity create', () => {
it('should create a new configuration entity', async () => {
const configurationEntity: ConfigurationEntity = ConfigurationEntity.create(
@ -26,13 +21,3 @@ describe('Configuration entity create', () => {
expect(configurationEntity.getProps().value).toBe('3');
});
});
describe('Configuration entity update', () => {
it('should update a configuration entity', async () => {
const configurationEntity: ConfigurationEntity = ConfigurationEntity.create(
createConfigurationProps,
);
configurationEntity.update(updateConfigurationProps);
expect(configurationEntity.getProps().value).toBe('2');
});
});

View File

@ -18,7 +18,7 @@ const mockConfigurationMapper = {
toDomain: jest.fn().mockImplementation(
() =>
new ConfigurationEntity({
id: ' 001199d4-7187-4e83-a044-12159cba2e33',
id: '001199d4-7187-4e83-a044-12159cba2e33',
props: {
identifier: {
domain: ConfigurationDomain.CARPOOL,