title: Nodejs - Generating unique random alpha numeric random string for Database
body: <p>I have a use case where I need to generate alpha numeric capital case strings of length 25, so the total possible unique combinations are very high:</p>
  
  <pre><code>36 pow (25) = 808281277464764060643139600456536293376
  </code></pre>
  
  <p>The string is to be stored in <code>MySql</code> database table with <code>unique</code> set to <code>true</code></p>
  
  <p>I am using following code to generate the string:</p>
  
  <pre><code>const Chance = require('chance');
  const chance = new Chance(Date.now() + Math.random());
  let randomStr = chance.string({length: 25, 
                  pool: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'});
  console.log(randomStr);
  </code></pre>
  
  <p>Node.js can run in cluster mode, so value of timestamp can be same for different requests so I also added <code>Math.random()</code>. Is this enough to ensure that <code>MySql</code> unique constraint won't be violated by the random strings.</p>