Skip to content

Commit

Permalink
[GH-158] feat(db): update mongodb to version 6
Browse files Browse the repository at this point in the history
  • Loading branch information
pablojvritx committed Jun 3, 2024
1 parent 7a1f82e commit a89635b
Show file tree
Hide file tree
Showing 9 changed files with 444 additions and 182 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Usa la imagen base con Node.js
FROM node:18
FROM node:20

# Establece el directorio de trabajo
WORKDIR /app
Expand Down
16 changes: 10 additions & 6 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ services:
MONGO_INITDB_ROOT_PASSWORD: rootPassword
MONGO_USERNAME: localUser
MONGO_PASSWORD: localPassword
healthcheck:
test: ["CMD", "mongo", "--eval", "db.adminCommand('ping')"]
interval: 60s
start_period: 30s
timeout: 10s
retries: 3
# healthcheck:
# test: >
# ["CMD-SHELL", 'mongosh -u root -p rootPassword --quiet --eval "if rs.initiate({_id:\'rs0\',members:[{_id:0,host:\'localhost:27017\'}]}).ok || rs.status().ok then exit 0 else exit 1 end"']
# interval: 10s
# start_period: 30s
# timeout: 10s
# retries: 3


command: ["--replSet", "rs0", "--bind_ip_all", "--keyFile", "/data/replica.key"]

volumes:
ts_api_mongo_data:
5 changes: 4 additions & 1 deletion docker/mongo/Dockerfile_mongo
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
FROM mongo:5.0.0
FROM mongo:6.0.15
RUN apt-get update && apt-get install -y curl iputils-ping
COPY ./mongo-init.js /docker-entrypoint-initdb.d/
RUN openssl rand --base64 768 > /data/replica.key
RUN chmod 400 /data/replica.key
RUN chown 999:999 /data/replica.key
570 changes: 407 additions & 163 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@
"helmet": "^7.1.0",
"http-status": "^1.7.4",
"jsonwebtoken": "^9.0.2",
"mongodb": "^4.17.2",
"mongodb": "^6.7.0",
"node-dependency-injection": "^2.7.3",
"swagger-ui-express": "^5.0.0",
"swagger-ui-express": "^5.0.1",
"ts-node": "^10.9.2",
"typescript": "^5.4.5",
"uuid": "^9.0.1",
Expand All @@ -89,15 +89,15 @@
"devDependencies": {
"@cucumber/cucumber": "^10.8.0",
"@types/bcryptjs": "^2.4.6",
"@types/chai": "^4.3.11",
"@types/chai": "^4.3.16",
"@types/chance": "^1.1.6",
"@types/convict": "^6.1.6",
"@types/cors": "^2.8.17",
"@types/errorhandler": "^1.5.3",
"@types/express": "^4.17.21",
"@types/glob": "^8.1.0",
"@types/jest": "^29.5.12",
"@types/jsonwebtoken": "^9.0.5",
"@types/jsonwebtoken": "^9.0.6",
"@types/supertest": "^2.0.16",
"@types/swagger-ui-express": "^4.1.6",
"@types/uuid-validate": "^0.0.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ObjectId } from 'mongodb';
import { MongoRepository } from '../../../../shared/infrastructure/persistence/mongo/MongoRepository';

import { Author, AuthorRepository } from '../../domain';
Expand Down Expand Up @@ -25,7 +26,9 @@ export class MongoAuthorRepository

public async search(id: string): Promise<Author | null> {
const collection = await this.collection();
const document = await collection.findOne<AuthorDocument>({ _id: id });
const document = await collection.findOne<AuthorDocument>({
_id: id as unknown as ObjectId
});

return document
? Author.fromPrimitives({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ObjectId } from 'bson';
import { RequestOptions } from '../../../../../apps/apiApp/shared/interfaces/RequestOptions';
import { Nullable } from '../../../../shared/domain/Nullable';
import { MongoRepository } from '../../../../shared/infrastructure/persistence/mongo/MongoRepository';
Expand Down Expand Up @@ -39,7 +40,9 @@ export class MongoBookRepository
const collection = await this.collection();

if (Object.keys(options).length === 0) {
const document = await collection.findOne<BookDocument>({ _id: id });
const document = await collection.findOne<BookDocument>({
_id: id as unknown as ObjectId
});

return document
? Book.fromPrimitives({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UUID } from 'bson';
import { MongoClient } from 'mongodb';
import MongoConfig from './MongoConfig';

Expand Down Expand Up @@ -29,7 +30,8 @@ export class MongoClientFactory {
try {
const connectionString = `${config.connection}://${config.username}:${config.password}@${config.url}/${config.db}`;
const client = new MongoClient(connectionString, {
ignoreUndefined: true
ignoreUndefined: true,
pkFactory: { createPk: () => new UUID().toBinary() }
});

await client.connect();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Collection, MongoClient } from 'mongodb';
import { Collection, MongoClient, ObjectId } from 'mongodb';
import { AggregateRoot } from '../../../domain/AggregateRoot';

export abstract class MongoRepository<T extends AggregateRoot> {
Expand All @@ -23,7 +23,7 @@ export abstract class MongoRepository<T extends AggregateRoot> {
};

await collection.updateOne(
{ _id: id },
{ _id: id as unknown as ObjectId },
{ $set: document },
{ upsert: true }
);
Expand All @@ -33,11 +33,14 @@ export abstract class MongoRepository<T extends AggregateRoot> {
const collection = await this.collection();
const { id, ...document } = aggregateRoot.toPrimitives();

await collection.updateOne({ _id: id }, { $set: document });
await collection.updateOne(
{ _id: id as unknown as ObjectId },
{ $set: document }
);
}

protected async delete(id: string): Promise<void> {
const collection = await this.collection();
await collection.deleteOne({ _id: id });
await collection.deleteOne({ _id: id as unknown as ObjectId });
}
}

0 comments on commit a89635b

Please sign in to comment.