Giter VIP home page Giter VIP logo

node-csv-query's Introduction

node-csv-query Build Status

Turn your CSV files to queryable objects.

Node-csv-query loads the entire CSV file in memory, thanks to csv-parse, then plug lodash manipulation functions on the loaded dataset. This can be handy for demo project as your code will have a databsae ready syntax while your dataset is easily editable even for non developer.

⚠️Warning⚠️: this library is not meant for performance!

How to use

dataset.csv

id,firstName,lastName,amountOfBooks
1,Olivier,Kaisin,10
2,Emile-Victor,Portenart,2
3,Alex,Mapolice,42
4,Alex,Gaspy,2

Connect to your csv database

'use strict';
    
var csvdb = require("node-csv-query").default;
var databaseConnection = null;


csvdb(__dirname + "/dataset.csv").then(function (db) {
  databaseConnection = db;
});

How to find the row with firstName=Olivier

databaseConnection.findOne({ 
  firstName: "Olivier" 
}).then(function (record) {
  // Do some stuff
})

How to find the rows with firstName=Alex

databaseConnection.find({ 
  firstName: "Alex" 
}).then(function (records) {
  // Do some stuff
})

How to find the rows with having an 'i' in their firstName

This lib use lodash for querying the dataset. All query format are listed here. node-csv-query will just pass the query param to lodash as the predicate argument.

databaseConnection.find(function (record) {
  return record.firstName.indexOf('i') > -1;
}).then(function (records) {
  // Do some stuff
})

How to deal with different dataset format

This lib is based on csv-parse for parsing csv files. All available options for parsing the CSV are listed here. node-csv-query will just pass these option to csv-parse with the exception of the columns option that will be overrided to true for internal mechanism to works.

dataset_custom.csv

# this dataset has comment & semi colon separator & space before separator
id;firstName   ;lastName ;amountOfBooks
1 ;Olivier     ;Kaisin   ;10
2 ;Emile-Victor;Portenart;2
3 ;Alex        ;Mapolice ;42
4 ;Alex        ;Gaspy    ;2
'use strict';

var csvdb = require("node-csv-query").default;

csvdb(
  __dirname + "/dataset_custom.csv", { rtrim: true, delimiter: ';', comment: '#' }
).then(function (db) {
  return db.findOne({ 
    firstName: "Olivier" 
  });
}).then(function (record) {
  // Do some stuff
}).catch(function (error) {
  throw error;
});

Compatibility

This lib has been packaged for Node >= v8.9 .

License

MIT

node-csv-query's People

Contributors

olivierkaisin avatar rdubigny avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

sandybemonkey

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.