export class MongoService extends Effect.Service<MongoService>()('MongoService', {
scoped: Effect.gen(function* (_) {
yield* Effect.log('MongoService: Initializing MongoDB Client...');
const client = new MongoClient(uri);
yield* Effect.tryPromise({
try: async () => {
await client.connect();
console.log('MongoService: MongoDB connection established');
},
catch: (error) => new Error(`MongoService: Failed to connect - ${error}`)
}).pipe(Effect.orDie);
// Register finalizer to clean up the client
yield* Effect.addFinalizer(() =>
Effect.tryPromise({
try: async () => {
await client.close();
console.log('MongoService: MongoDB client connection closed');
},
catch: (error) =>
new Error(`MongoService: Failed to close connection - ${error}`)
}).pipe(Effect.orDie)
);
const db: Db = client.db('<my-db>');
return {
client,
db
} as const;
})
}) {
}
export class MongoService extends Effect.Service<MongoService>()('MongoService', {
scoped: Effect.gen(function* (_) {
yield* Effect.log('MongoService: Initializing MongoDB Client...');
const client = new MongoClient(uri);
yield* Effect.tryPromise({
try: async () => {
await client.connect();
console.log('MongoService: MongoDB connection established');
},
catch: (error) => new Error(`MongoService: Failed to connect - ${error}`)
}).pipe(Effect.orDie);
// Register finalizer to clean up the client
yield* Effect.addFinalizer(() =>
Effect.tryPromise({
try: async () => {
await client.close();
console.log('MongoService: MongoDB client connection closed');
},
catch: (error) =>
new Error(`MongoService: Failed to close connection - ${error}`)
}).pipe(Effect.orDie)
);
const db: Db = client.db('<my-db>');
return {
client,
db
} as const;
})
}) {
}