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.
Configure Projections operation
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>;
}
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>;
}
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();
}
}
import { MongoDbContext } from "../abstractions/contexts/mongoDbContext";
import { environment } from "../../../environments/environment";
export class ProjectionDbContext extends MongoDbContext {
constructor() {
super(environment.connectionStrings.projections);
}
}
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