Skip to content

Commit

Permalink
feat: add mongodb and get-refresh-token repositorie
Browse files Browse the repository at this point in the history
  • Loading branch information
Williancc1557 committed Jul 23, 2022
1 parent a3d1ae0 commit ffe9fb3
Show file tree
Hide file tree
Showing 8 changed files with 4,535 additions and 32 deletions.
12 changes: 12 additions & 0 deletions jest-mongodb-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
mongodbMemoryServerOptions: {
instance: {
dbName: "jest",
},
binary: {
version: "4.0.3",
skipMD5: true,
},
autoStart: false,
},
};
12 changes: 10 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/* eslint-disable */

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/src'],
testEnvironment: "node",
preset: "@shelf/jest-mongodb",
collectCoverageFrom: ['<rootDir>/src/**/*.ts'],
coverageDirectory: 'coverage',
transform: {
".+\\.ts$": "ts-jest",
},
};
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"test:ci": "yarn test -- --coverage"
},
"devDependencies": {
"@shelf/jest-mongodb": "^4.0.0",
"@techmmunity/eslint-config": "^5.2.3",
"@types/jest": "^28.1.6",
"eslint": "^8.20.0",
Expand All @@ -24,5 +25,8 @@
"ts-jest": "^28.0.7",
"ts-node-dev": "^2.0.0",
"typescript": "^4.7.4"
},
"dependencies": {
"mongodb": "^4.8.0"
}
}
37 changes: 37 additions & 0 deletions src/infra/db/mongodb/helpers/mongo-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Collection } from "mongodb";
import { MongoClient } from "mongodb";

export const mongoHelper = {
client: null as MongoClient,
async connect(url?: string): Promise<void> {
this.client = await new MongoClient(url || process.env.MONGO_URL).connect();
},

async disconnect(): Promise<void> {
await this.client.close();
this.client = null;
},

dbCollection(): Collection {
return;
},

async getCollection(name: string): Promise<Collection> {
try {
return this.client.db().collection(name);
} catch {
await this.connect();
return this.client.db().collection(name);
}
},

// eslint-disable-next-line
map(account: any): any {
const { _id, ...accountWithoudId } = account;

return {
...accountWithoudId,
id: _id.toString(),
};
},
};
17 changes: 17 additions & 0 deletions src/infra/db/mongodb/refresh-token-repository/get-refresh-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { GetRefreshTokenRepository } from "../../../../data/protocols/get-refresh-token-repository";
import type { AccountModel } from "../../../../domain/models/account";
import { mongoHelper } from "../helpers/mongo-helper";

export class GetRefreshTokenMongoRepository
implements GetRefreshTokenRepository
{
public async get(refreshToken: string): Promise<AccountModel> {
const accountCollection = await mongoHelper.getCollection("account");

const account = await accountCollection.findOne({
refreshToken,
});

return mongoHelper.map(account);
}
}
19 changes: 4 additions & 15 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
"module": "commonjs",
"target": "es2019",
"esModuleInterop": true,
"allowJs": true
}
}
Loading

0 comments on commit ffe9fb3

Please sign in to comment.