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

nosqlimport

v0.1.7

Published

CSV/TSV to JSON converter for importing data into NoSQL document stores

Downloads

64

Readme

nosqplimport

A command-line tool and Node.js library that allows CSV/TSV data to be imported into a variety of NoSQL databases

schematic

Installing

Command-line tool

Install with npm:

> npm install -g nosqlimport

You can preview the type of JSON documents nosqlimport will create by piping a file into it:

> cat test.tsv | nosqlimport

or

> cat test.csv | nosqlimport --delimiter ','

To import data into a NoSQL database, you'll probably want one of the NoSQL plugins:

> npm install -g nosqlimport-couchdb

or install everything

> npm install -g nosqlimport-couchdb nosqlimport-mongodb nosqlimport-elasticsearch

Then import data like so:

> cat mydatafile.tsv | nosqlimport --nosql couchdb --url http://localhost:5984 --db mydb
nosqlimport Using couchdb nosqlimport writer +0ms
nosqlimport CouchDB URL: http://localhost:5984 +388ms
nosqlimport CouchDB Database: mydb +2ms
nosqlimport { documents: 26, failed: 0, total: 26, totalfailed: 0 } +153ms
nosqlimport writecomplete +0ms { total: 26, totalfailed: 0 }

Environment variables can be used to save typing:

> export NOSQL_URL=http://localhost:5984
> export NOSQL_DATABASE=mydb
> cat mydatafile.tsv | nosqlimport --nosql couchdb

Using within your own application

Use npm install the library

> npm install --save nosqlimport

You'll probably want one of the NoSQL plugins too e.g.

> npm install --save nosqlimport-couchdb

And import data in your code:

    var rs = fs.createReadStream('./guitars.tsv', { encoding: 'utf8'});
    var opts = {
      url: 'http://localhost:5984',
      nosql: 'couchdb',
      db: 'mydb'
    }
    nosqlimport.importStream(rs, null, opts, function(err, data) {
      assert.equal(typeof data, 'object');
      assert.equal(data.total, 26)
      assert.equal(data.totalfailed, 0)
      assert.equal(err, null);     
      done(); 
    });

Nomenclature

Different NoSQL databases have different names for things. CouchDB stores documents in "databases". MongoDB in "collections" that live in databases. Elasticseach in "types" that live in "indexes".

When using nosqlimport with CouchDB/Cloudant the url parameter defines the URL of the CouchDB instance and the database parameter means the database to be written to e.g.:

  cat test.txt | nosqlimport --nosql couchdb --url https://myusername:[email protected] --db mydb

When using nosqlimport with MongoDB, the url parameter defines the URL of the MongoDB instance including the database and the database parameter means the MongoDB collection to writer to e.g.:

 cat test.txt | nosqlimport --nosql mongodb --url mongodb://localhost:27017/mydatabase --database mycollection

When using nosqlimport with Elasticsearch, the url parameter defines the URL of the Elastic instance including the index and the database parameter means the Elasticsearch type to writer to e.g.:

 cat test.txt | nosqlimport --nosql elasticsearch --urlhttp://localhost:9200/mydatabase --database mycollection

Transform functions

You can supply your own JavaScript function into the document processing stream for casting types, filtering documents or any other purpose. Your JavaScript function must be synchronous, taking a single doc parameter which it must return e.g

module.exports = function(doc) {
  // your code codes here
  return doc;
};

Save your JavaScript to a file and then run nosqlimport passing in the -t parameter:

cat test.csv | nosqlimport -t './mytransform.js' -n 'couchdb'

Transform - Casting

module.exports = function(doc) {
  doc.price = parseFloat(doc.price);
  return doc;
};

Transform - Filtering

module.exports = function(doc) {
  if (doc.instock === 'true') {
    doc.instock = true;
    return doc;
  } else {
    // nothing is written to the database
    return {}
  }
};

Command-line parameters

  • -n --nosql [db type] - the type of NoSQL database to write to (default stdout)
  • -t --transform [filename] - the filename containing the JavaScript transform function (default none)
  • -d --delimiter [character] - the delimiter character to use (default '\t')
  • --db --database [db name] - the name of the database to write to
  • -u --url [url] - the url of the database to write to

Environment variables

  • NOSQL_URL - same as --url
  • NOSQL_DATABASE - same as --database
  • DEBUG - set to 'nosqlimport' to output debug messages

Function reference

importStream(rs, ws, opts, callback)

Parameters

  • rs - the read stream to read data from
  • ws - the write stream to send data to
  • opts - an object containing configuration options
  • callback - calls back with (err, data) on completion

importFile(filename, ws, opts, callback)

Parameters

  • rs - the filename to read data from
  • ws - the write stream to send data to
  • opts - an object containing configuration options
  • callback - calls back with (err, data) on completion

previewFile(filename, callback)

Parameters

  • filename - the name of the file to preview
  • callback - calls back with (err, data, delimiter)

previewFile(filename, callback)

Parameters

  • filename - the name of the file to preview
  • callback - calls back with (err, data, delimiter)

previewURL(url, callback)

Parameters

  • url - the url to preview
  • callback - calls back with (err, data, delimiter)

previewStream(rs, callback)

Parameters

  • rs - the read stream
  • callback - calls back with (err, data, delimiter)

Origins of nosqlimport

This project is a refactor of my couchimport importer script for CouchDB/Cloudant. This codebase is simpler, more modular and makes more sense.

Contributing

The projected is released under the Apache-2.0 license so forks, issues and pull requests are very welcome.