We analyze superconductivity in the cuprates near the onset of an incommensurate charge density wave (CDW) order with momentum Q = (Q, 0)/(0, Q), as observed in the experiments. We first consider a semi-phenomenological charge-fermion model in which hot fermions, separated by Q, attract each other by exchanging soft CDW fluctuations. We find that in a quantum-critical region near CDW transition, Tc = Aḡc, whereḡc is charge-fermion coupling and A is the prefactor which we explicitly compute. We then consider the particular microscopic scenario in which CDW order parameter emerges as a composite field made out of primary spin-density-wave fields. We show that charge-fermion couplingḡc is of order of spin-fermion couplingḡs. As the consequence, superconducting Tc is substantially enhanced near the onset of CDW order. Finally we analyze the effect of an external magnetic field H. We show that, as H increases, optimal Tc decreases and the superconducting dome becomes progressively more confined to the CDW quantum-critical point. These results are consistent with the experiments.