Created
July 18, 2023 17:16
-
-
Save alexalvess/57616383ff1bc042798a181409790dee to your computer and use it in GitHub Desktop.
Configure Projections operation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as Mongoose from "mongoose"; | |
| import { IDisposable } from "../../../../../../utils/idisposable"; | |
| export interface IMongoDbContext extends IDisposable { | |
| getColletion<TSchema extends Mongoose.AnyObject = Mongoose.AnyObject>(collectionName: string): Mongoose.Collection<TSchema>; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export interface IProjectionGateway<TSchema> { | |
| findAsync(filter: {}): Promise<TSchema | null>; | |
| getAsync(id: string): Promise<TSchema | null>; | |
| listAllAsync(): Promise<Array<TSchema>>; | |
| deleteAsync(filter: {}): Promise<boolean>; | |
| upsertAsync(filter: {}, replacement: TSchema): Promise<boolean>; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as Mongoose from "mongoose"; | |
| import { IMongoDbContext } from "./imongoDbContext"; | |
| export abstract class MongoDbContext implements IMongoDbContext{ | |
| private readonly connection: Mongoose.Connection; | |
| constructor(connectionString: string) { | |
| this.connection = Mongoose.createConnection(connectionString); | |
| } | |
| public getColletion<TSchema extends Mongoose.AnyObject = Mongoose.AnyObject>(collectionName: string): Mongoose.Collection<TSchema> { | |
| return this.connection.collection<TSchema>(collectionName); | |
| } | |
| async dispose(): Promise<void> { | |
| await Mongoose.disconnect(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { MongoDbContext } from "../abstractions/contexts/mongoDbContext"; | |
| import { environment } from "../../../environments/environment"; | |
| export class ProjectionDbContext extends MongoDbContext { | |
| constructor() { | |
| super(environment.connectionStrings.projections); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { IMongoDbContext } from "./abstractions/contexts/imongoDbContext"; | |
| import * as Mongoose from "mongoose"; | |
| import { IProjectionGateway } from "../../application/ports/iprojectionGateway"; | |
| export class ProjectionGateway<TSchema extends Mongoose.AnyObject = Mongoose.AnyObject> implements IProjectionGateway<TSchema> { | |
| private readonly collection: Mongoose.Collection<TSchema>; | |
| constructor(context: IMongoDbContext, collectionName: string) { | |
| this.collection = context.getColletion<TSchema>(collectionName); | |
| } | |
| public findAsync(filter: {}): Promise<TSchema | null> { | |
| return this.collection.findOne<TSchema>(filter); | |
| } | |
| public getAsync(id: string): Promise<TSchema | null> { | |
| return this.findAsync({ _id: id }); | |
| } | |
| public listAllAsync(): Promise<Array<TSchema>> { | |
| return this.collection.find<TSchema>({}).toArray(); | |
| } | |
| public deleteAsync(filter: {}): Promise<boolean> { | |
| return this.collection.deleteOne(filter) | |
| .then(result => result.acknowledged); | |
| } | |
| public upsertAsync(filter: {}, replacement: TSchema): Promise<boolean> { | |
| return this.collection.findOneAndReplace(filter, replacement, { upsert: true }) | |
| .then(result => result.ok === 0 ? false : true); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment