npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

postgres-geolite

v1.0.3

Published

MaxMind Geolite PostgreSQL importer & interface to provide lookups for country, city, timezone, area code, ISP, etc.

Downloads

20

Readme

node-postgres-geolite npm version Build Status

MaxMind Geolite PostgreSQL importer & interface to provide IPv4 and IPv6 lookups for country, subdivisions, city, timezone, area code, ISP, etc. This module is designed to import the GeoIP2 and Geolite Legacy database CSVs into a PostgreSQL database and query it efficiently and effectively.

Apologies

I had originally wanted to return the data similar to the node-maxmind project, thus allowing this module to server as a drop-in replacement. Unfortunately I was unable to figure out how to best map the geoname ID data in the database to allow for geoname_ids on each location type (continent, country, subdivisions, city). If you are able to do this, feel free to fork and submit a PR. Otherwise if you have ideas on doing this feel free to contact me to discuss.

Table of Contents

Requirements

  • 1GB RAM
    • This is the for the build process only, if you plan on building & updating a different machine you can still utilize this on a machine with less RAM.
  • PostgreSQL >= 9.4

Install

$ npm install --save postgres-geolite

Usage

TODO

API

geolite.init(options)

Checks whether the database in PostgreSQL has been initialized, and validates the schema with the current version of this module. If the database is ready, it returns the geoliteDB object in the promise. Otherwise, it updates the PostgreSQL database using the options provided.

Object: options

If the options provided don't get properly validated by Joi, then an error is returned and can be caught via the reject promise. Validation schema can be found here.

{
  database: object,
  downloads: object,
}
Object: database

Takes the configuration object from pg-promise with most of the properties.

Object: downloads
  • (string) location: relative or absolute path to where the GeoLite databases get downloaded (if local is false) and decompressed to.
    • default: './tmp'
  • (boolean) cleanup: the location folder above will be created if it doesn't exist, if cleanup is set to true it will delete the location folder once it is no longer necessary.
    • default: false
  • (boolean) local: if set to true, the resources below are interpreted as filepaths and decompressed to the location specified above.
    • default: false
  • (object) resources
    • (string) geolite2_city: direct download link or filepath for the GeoLite2 City database in CSV format, zipped.
      • https://dev.maxmind.com/geoip/geoip2/geolite2/
    • (string) geolite_asn: direct download link or filepath for the GeoLite ASN database in CSV / zip format.
    • (string) geolite_asn_ipv6: direct download link or filepath for the GeoLite ASN database in CSV / zip format.
      • https://dev.maxmind.com/geoip/legacy/geolite/

Returns: Promise (geoliteDB)

Promise that contains the object used for all future API calls.

geoliteDB.update()

Forces an update on the PostgreSQL database using the options provided in geolite.init.

Returns: Promise ()

Resolves when finished, if any error occurs it rejects.

geoliteDB.get(ip)

Gets all the information available on the provided IP.

String: ip

The IPv4 address or IPv6 address you are querying, as a string.

Returns: Promise (result)

The resulting lookup data from you query.

Example
{
  "city":{
    "names":{
      "de":"Mountain View",
      "en":"Mountain View",
      "fr":"Mountain View",
      "ja":"マウンテンビュー",
      "ru":"Маунтин-Вью",
      "zh-CN":"芒廷维尤"
    }
  },
  "continent":{
    "code":"NA",
    "names":{
      "de":"Nordamerika",
      "en":"North America",
      "es":"Norteamérica",
      "fr":"Amérique du Nord",
      "ja":"北アメリカ",
      "pt-BR":"América do Norte",
      "ru":"Северная Америка",
      "zh-CN":"北美洲"
    }
  },
  "country":{
    "iso_code":"US",
    "names":{
      "de":"USA",
      "en":"United States",
      "es":"Estados Unidos",
      "fr":"États-Unis",
      "ja":"アメリカ合衆国",
      "pt-BR":"Estados Unidos",
      "ru":"США",
      "zh-CN":"美国"
    }
  },
  "isp":{
    "asn":"AS15169",
    "org":"Google Inc."
  },
  "location":{
    "latitude":"37.386",
    "longitude":"-122.0838",
    "time_zone":"America/Los_Angeles"
  },
  "postal":{
    "code":"94035"
  },
  "registered_country":{
    "iso_code":"US",
    "names":{
      "de":"USA",
      "en":"United States",
      "es":"Estados Unidos",
      "fr":"États-Unis",
      "ja":"アメリカ合衆国",
      "pt-BR":"Estados Unidos",
      "ru":"США",
      "zh-CN":"美国"
    }
  },
  "subdivisions":[
    {
      "iso_code":"CA",
      "names":{
        "de":"Kalifornien",
        "en":"California",
        "es":"California",
        "fr":"Californie",
        "ja":"カリフォルニア州",
        "pt-BR":"Califórnia",
        "ru":"Калифорния",
        "zh-CN":"加利福尼亚州"
      }
    }
  ]
}

Performance

If you clone the repo and run the tests on your target system you should get a good idea of expected performance from the test results.

Initializing & updating the database

  • System: Intel Core i5 6600K running Windows 10 with the PostgreSQL DB on a SATA 6Gbps SSD
    • Result: (complete imports incomplete, no result available at this time)

Building gets done on secondary tables, and when complete the original table gets dropped and replaced with the new one. This is what I considered the best approach for minimizing potential downtime during an update.

You can also refer to the tests on Travis-CI for performance.

Queries

Thanks to the built in operator class GiST inet_ops, queries take ~30ms on my test system. The test cases ran on Travis-CI can also give you an idea of expected performance on systems with their infrastructure.

License

MIT © Caleb Blankemeyer

While I'm not technically including the data, I'm including the licensing disclaimers as per GeoLite Documentation & GeoLite 2 Documentation

This product includes GeoLite2 data created by MaxMind, available from http://www.maxmind.com.

This product includes GeoLite data created by MaxMind, available from http://www.maxmind.com.