autolevel.js

Automatically use the right abstract-leveldown module for your configuration
git clone git://git.finwo.net/lib/autolevel.js
Log | Files | Refs | README | LICENSE

index.js (1082B)


      1 let parseUrl = require('url-parse');
      2 
      3 /**
      4  * Initializes a new levelup instance, based on the given options
      5  *
      6  * @param {String}   location
      7  * @param {Object}   options
      8  * @param {Function} callback
      9  *
     10  * @return {Object|Boolean} levelup
     11  */
     12 module.exports = function autolevel( location, options, callback ) {
     13 
     14   // Ensure proper options
     15 
     16   // Try to detect the url if none given
     17   if (!location) {
     18     location = ('object' === typeof process) && process.env && (
     19       process.env.DATABASE_URL ||
     20       process.env.DATABASE_URI ||
     21       process.env.MONGODB_URL ||
     22       process.env.MONGODB_URI
     23     ) || 'mem://';
     24   }
     25 
     26   // Let the adapters register themselves
     27   require('./adapter')(autolevel);
     28 
     29   // Parse the given location, it should contain a protocol
     30   let parsedLocation = parseUrl(location),
     31       requested      = parsedLocation.protocol.split(':').shift().toLowerCase();
     32 
     33   // Ensure we have the requested protocol
     34   if (!(requested in autolevel)) {
     35     return false;
     36   }
     37 
     38   // Return what we found
     39   return autolevel[requested]( parsedLocation, options, callback );
     40 };