Skip to content

rdubigny/node-csv-query

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Turn your CSV files to queryable objects

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%