export const makeMongoDatabaseProviderAcq = Effect.gen(function* () {
const config = yield* ConfigService;
// Acquire a releasable handle on the MongoClient, and specify cleanup behavior for the client
// TODO: Write a test verifying that the cleanup is called when the scope is discarded
const client = yield* Effect.acquireRelease(
Effect.sync(() => new Mongo.MongoClient(config.get().mongoDBURI)).pipe(
Effect.andThen((c) =>
Effect.tryPromise({
try: () => c.connect(),
catch: (e) => new GenericMongoDbException(e),
}),
),
),
(c, _exit) => Effect.promise(() => c.close()),
);
const use = <T extends Document>(
database: string,
collection: string,
options?: {
dbOptions?: Mongo.DbOptions;
collectionOptions: Mongo.CollectionOptions;
},
) =>
Effect.try({
try: () =>
client
.db(database, options?.dbOptions)
.collection<T>(collection, options?.collectionOptions),
catch: (e) => new GenericMongoDbException(e),
});
return MongoDatabaseProvider.of({ use });
});
export const makeMongoDatabaseProviderAcq = Effect.gen(function* () {
const config = yield* ConfigService;
// Acquire a releasable handle on the MongoClient, and specify cleanup behavior for the client
// TODO: Write a test verifying that the cleanup is called when the scope is discarded
const client = yield* Effect.acquireRelease(
Effect.sync(() => new Mongo.MongoClient(config.get().mongoDBURI)).pipe(
Effect.andThen((c) =>
Effect.tryPromise({
try: () => c.connect(),
catch: (e) => new GenericMongoDbException(e),
}),
),
),
(c, _exit) => Effect.promise(() => c.close()),
);
const use = <T extends Document>(
database: string,
collection: string,
options?: {
dbOptions?: Mongo.DbOptions;
collectionOptions: Mongo.CollectionOptions;
},
) =>
Effect.try({
try: () =>
client
.db(database, options?.dbOptions)
.collection<T>(collection, options?.collectionOptions),
catch: (e) => new GenericMongoDbException(e),
});
return MongoDatabaseProvider.of({ use });
});