Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

69
Views
¿Cómo vincular a GridFSBucket en un módulo?

Así es como construyo una clase para conectarme a mi mongoDb antes. Como puede ver, me conecto a la base de datos y también creo un gfs , que usa mongo.GridFSBucket . Esto es necesario para manejar archivos de imagen.

 export default class Db { constructor (uri, callback) { const mongo = process.env.MONGO || 'mongodb://localhost:27017' this.mongodb = 'data' this.db = null this.gfs = null this.connection = MongoClient.connect(mongo) return this } async connect (msg) { if (!this.connected) { try { this.connection = await this.connection this.connection = this.connection.db(this.mongodb) this.gfs = new mongo.GridFSBucket(this.connection) this.connected = true } catch (err) { console.error('mongo connection error', err) } } return this } }

No, cambié el backend a una aplicación nestJS y no sé cómo crear la instancia gfs en un módulo:

 @Module({ providers: [ { provide: 'DATABASE_CLIENT', useFactory: () => ({ client: null }) }, { provide: 'DATABASE_CONNECTION', inject: ['DATABASE_CLIENT'], useFactory: async (dbClient): Promise<Db> => { const mongo = 'mongodb://localhost:27017' const options = {} const client = new MongoClient(mongo, options) try { await client.connect() dbClient.client = client const db = client.db('data') // How to create a GridFSBucket instance? // dbClient.gfs = new GridFSBucket(db) return db } catch (error) { console.error(error) } } } ], exports: ['DATABASE_CONNECTION', 'DATABASE_CLIENT'] }) export class DatabaseModule { constructor(@Inject('DATABASE_CLIENT') private dbClient) {} onApplicationShutdown(signal) { if (signal) console.log(signal + ' signal recieved') // eg "SIGINT" } }

Además, este gfs debe devolverse solo si es necesario. El DatabaseModule se usa así en cualquier otro módulo, que necesita conexión db:

 @Module({ imports: [DatabaseModule], providers: [PhotosService, PhotosResolvers] }) export class PhotosModule {}

Pero no todos los módulos necesitan la instancia de gfs , por lo que debo pasar algo como useGfs = true . ¿Cómo paso un parámetro?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!