Skip to content

Instantly share code, notes, and snippets.

@alexalvess
Created July 18, 2023 17:16
Show Gist options
  • Select an option

  • Save alexalvess/57616383ff1bc042798a181409790dee to your computer and use it in GitHub Desktop.

Select an option

Save alexalvess/57616383ff1bc042798a181409790dee to your computer and use it in GitHub Desktop.

Revisions

  1. alexalvess created this gist Jul 18, 2023.
    6 changes: 6 additions & 0 deletions IMongoDBContext.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    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>;
    }
    7 changes: 7 additions & 0 deletions IProjectionGateway.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    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>;
    }
    18 changes: 18 additions & 0 deletions MongoDBContext.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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();
    }
    }
    8 changes: 8 additions & 0 deletions ProjectionDbContext.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    import { MongoDbContext } from "../abstractions/contexts/mongoDbContext";
    import { environment } from "../../../environments/environment";

    export class ProjectionDbContext extends MongoDbContext {
    constructor() {
    super(environment.connectionStrings.projections);
    }
    }
    33 changes: 33 additions & 0 deletions ProjectionGateway.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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);
    }
    }