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!
dataset.csv
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;
var databaseConnection = null;
csvdb(__dirname + "/dataset.csv").then(function (db) {
databaseConnection = db;
});
databaseConnection.findOne({
firstName: "Olivier"
}).then(function (record) {
// Do some stuff
})
databaseConnection.find({
firstName: "Alex"
}).then(function (records) {
// Do some stuff
})
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
})
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;
});
This lib has been packaged for Node >= v8.9 .
MIT