// NewDB creates a new DB struct from the given connection string.
NewDB creates a new DB struct from the given connection string.
// NewDB creates a new DB struct from the given connection string .
func NewDB(connection string) (*DB, error) {
	client, err := mongo.NewClient(options.Client().ApplyURI(connection))
	if err != nil {
		log.Error().Msg(err.Error())
		return nil, err
	}
	ctx, _ := context.WithTimeout(context.Background(), 7*time.Second)
	err = client.Connect(ctx)
	if err != nil {
		log.Error().Msg(err.Error())
		return nil, err
	}
	err = client.Ping(ctx, readpref.Primary())
	if err != nil {
		log.Error().Msg(err.Error())
		return nil, err
	}
	log.Info().Msg("Successfully connected to MongoDB.")
	return &DB{Client: client}, nil
}
func NewDB ( connection string ) ( * DB , error ) { client , err := mongo . NewClient ( options . Client ( ) . ApplyURI ( connection ) ) if err != nil { log . Error ( ) . Msg ( err . Error ( ) ) return nil , err } ctx , _ := context . WithTimeout ( context . Background ( ) , 7 * time . Second ) err = client . Connect ( ctx ) if err != nil { log . Error ( ) . Msg ( err . Error ( ) ) return nil , err } err = client . Ping ( ctx , readpref . Primary ( ) ) if err != nil { log . Error ( ) . Msg ( err . Error ( ) ) return nil , err } log . Info ( ) . Msg ( " " ) return & DB { Client : client } , nil }
NewDB creates a new DB struct from the given connection string.
NewDB creates a new DB struct from the given connection string .