Skip to content

Commit

Permalink
move to modern javascript syntax with babel + remove bluebird + remov…
Browse files Browse the repository at this point in the history
…e event-stream + more flexible parsing & filtering
  • Loading branch information
rdubigny committed Aug 10, 2018
1 parent 5350d61 commit 577f889
Show file tree
Hide file tree
Showing 17 changed files with 4,199 additions and 226 deletions.
9 changes: 9 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"presets": [
["env", {
"targets": {
"node": "8.9"
}
}]
]
}
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
"extends": "airbnb-base"
};
3 changes: 0 additions & 3 deletions .jshintrc

This file was deleted.

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: "node_js"
node_js:
- "0.10"
- "8.9"
87 changes: 71 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,94 @@
node-csv-query [![Build Status](https://travis-ci.org/olivierkaisin/node-csv-query.svg?branch=master)](https://travis-ci.org/olivierkaisin/node-csv-query)
node-csv-query [![Build Status](https://travis-ci.org/rdubigny/node-csv-query.svg?branch=master)](https://travis-ci.org/rdubigny/node-csv-query)
==============

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:Warning:warning:: this library is not meant for performance!
## How to use

dataset.csv
```
```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

```javascript
'use strict';

var csvdb = require("node-csv-query");
var databaseConnection = null;


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

How to find the row with `firstName=Olivier`
#### How to find the row with `firstName=Olivier`

```javascript
"use strict";
databaseConnection.findOne({
firstName: "Olivier"
}).then(function (record) {
// Do some stuff
})
```

#### How to find the rows with `firstName=Alex`

var csv = require("csv-query");
```javascript
databaseConnection.find({
firstName: "Alex"
}).then(function (record) {
// Do some stuff
})
```

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

csv.createFromFile(
__dirname + "/dataset.csv", {
delimiter: ";"
}).then(function (db) {
This lib use lodash for querying the dataset. All query format are listed [here](https://lodash.com/docs/4.17.10#filter).
node-csv-query will just pass the query param to lodash as the predicate argument.

```javascript
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](http://csv.adaltas.com/parse/#parser-options).
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
```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
```

```javascript
'use strict';

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

csvdb(
__dirname + "/dataset_custom.csv", { rtrim: true, delimiter: ';', comment: '#' }
).then(function (db) {
return db.findOne({
firstName: "Olivier"
});
Expand All @@ -37,17 +97,12 @@ csv.createFromFile(
}).catch(function (error) {
throw error;
});

```


## Credits

- bluebird
- csv-parse
- lodash
- event-stream
## Compatibility

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

## License

Expand Down
47 changes: 47 additions & 0 deletions dist/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _lodash = require('lodash');

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Db = function () {
function Db() {
_classCallCheck(this, Db);

this.rows = [];
}

_createClass(Db, [{
key: 'pushRow',
value: function pushRow(row) {
if (!(0, _lodash.isObject)(row)) {
throw new Error('row must be an object');
}

this.rows.push(row);
}

// TODO use the mongo driver syntax (connect, find, ...)

}, {
key: 'find',
value: function find(predicate) {
return Promise.resolve((0, _lodash.filter)(this.rows, predicate));
}
}, {
key: 'findOne',
value: function findOne(predicate) {
return Promise.resolve((0, _lodash.find)(this.rows, predicate));
}
}]);

return Db;
}();

exports.default = Db;
37 changes: 37 additions & 0 deletions dist/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _csvParse = require('csv-parse');

var _csvParse2 = _interopRequireDefault(_csvParse);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _lodash = require('lodash');

var _db = require('./db');

var _db2 = _interopRequireDefault(_db);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function createFromFile(filePath) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

var db = new _db2.default();

var parser = (0, _csvParse2.default)((0, _lodash.merge)(options, { columns: true }));

return new Promise(function (resolve, reject) {
_fs2.default.createReadStream(filePath).pipe(parser).on('data', db.pushRow.bind(db)).on('error', reject).on('finish', function () {
return resolve(db);
});
});
}

exports.default = createFromFile;
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"use strict";
#!/usr/bin/env node

require('babel-polyfill');
require('babel-register');

module.exports = require("./src/loader");
module.exports = require('./src/loader.js');
Loading

0 comments on commit 577f889

Please sign in to comment.