Skip to content

Commit

Permalink
- first release
Browse files Browse the repository at this point in the history
  • Loading branch information
aalfiann committed Nov 3, 2019
0 parents commit 0a15fae
Show file tree
Hide file tree
Showing 12 changed files with 519 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
package-lock.json
coverage
.nyc_output
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: node_js
node_js:
- "node"
- "10"
- "9"
- "8"
- "7"
- "6"
before_install:
- npm install -g npm@latest
install:
- npm install --only=dev
script:
- npm test
after_success: npm run coverage
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2019 M ABD AZIZ ALFIAN

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# fly-json-db
[![NPM](https://nodei.co/npm/fly-json-db.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/fly-json-db/)

[![npm version](https://img.shields.io/npm/v/fly-json-db.svg?style=flat-square)](https://www.npmjs.org/package/fly-json-db)
[![Build Status](https://travis-ci.org/aalfiann/fly-json-db.svg?branch=master)](https://travis-ci.org/aalfiann/fly-json-db)
[![Coverage Status](https://coveralls.io/repos/github/aalfiann/fly-json-db/badge.svg?branch=master)](https://coveralls.io/github/aalfiann/fly-json-db?branch=master)
[![Known Vulnerabilities](https://snyk.io//test/github/aalfiann/fly-json-db/badge.svg?targetFile=package.json)](https://snyk.io//test/github/aalfiann/fly-json-db?targetFile=package.json)
[![dependencies Status](https://david-dm.org/aalfiann/fly-json-db/status.svg)](https://david-dm.org/aalfiann/fly-json-db)
![License](https://img.shields.io/npm/l/fly-json-db)
![NPM download/month](https://img.shields.io/npm/dm/fly-json-db.svg)
![NPM download total](https://img.shields.io/npm/dt/fly-json-db.svg)
NoSQL Embedded Database on top json file and stream for NodeJS.

### Install using NPM
```bash
$ npm install fly-json-db
```

### Documentation
Documentation is available in our [Wiki](https://github.com/aalfiann/fly-json-db/wiki).

### Unit Test
If you want to play around with unit test.
```bash
$ npm test
```
40 changes: 40 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "fly-json-db",
"version": "1.0.0",
"description": "NoSQL Embedded Database on top json and stream for NodeJS",
"main": "src/flyjsonstream.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "nyc --reporter=html --reporter=text mocha",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
"repository": {
"type": "git",
"url": "git+https://github.com/aalfiann/fly-json-stream.git"
},
"keywords": [
"json-db",
"json-query",
"json-nosql",
"json-stream"
],
"author": "M ABD AZIZ ALFIAN",
"license": "MIT",
"bugs": {
"url": "https://github.com/aalfiann/fly-json-stream/issues"
},
"homepage": "https://github.com/aalfiann/fly-json-stream#readme",
"devDependencies": {
"coveralls": "^3.0.7",
"mocha": "^6.2.2",
"mocha-lcov-reporter": "^1.3.0",
"nyc": "^14.1.1"
},
"dependencies": {
"JSONStream": "^1.3.5",
"event-stream": "^4.0.1",
"fly-json-odm": "^1.8.4"
}
}
238 changes: 238 additions & 0 deletions src/flyjsonstream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
const FlyJsonOdm = require('fly-json-odm');
const es = require('event-stream');
const jsonStream = require('JSONStream');
const fs = require('fs');

class FlyJsonStream {

constructor() {
this._odm = new FlyJsonOdm();
}

/**
* Get json odm
* @return {unirest}
*/
get odm() {
return this._odm;
}

/**
* Determine is function
* @param {function} fn
* @return {bool}
*/
_isFunction(fn) {
return (typeof fn === "function");
}

/**
* Load file json
* @param {string} filePath this is the file path of file json
* @param {fn} callback [optional] callback(error, data)
* @return {promise|callback}
*/
loadFile(filePath,callback) {
var self = this;
self._odm.clean();
var promise = new Promise((resolve,reject) => {
fs.createReadStream(filePath,{flag:'w+'})
.pipe(jsonStream.parse())
.pipe(
es.mapSync(function(line) {
self._odm.data1.push(line);
})
)
.on('error',function(err) {
reject(err);
})
.on('end', function() {
resolve({
status:true,
message:'Load data finished!'
});
});
});

if(self._isFunction(callback)) {
return promise.then(result => {
callback(null,result);
}, error => {
callback(error);
});
}
return promise;
}

/**
* Load json array
* @param {array} array this is the json array
* @param {fn} callback [optional] callback(error, data)
* @return {promise|callback}
*/
loadArray(array,callback) {
var self = this;
self._odm.clean();
var promise = new Promise((resolve,reject) => {
es.readArray(array)
.pipe(
es.mapSync(function(line) {
self._odm.data1.push(line);
})
)
.on('error',function(err) {
reject(err);
})
.on('end', function() {
resolve({
status:true,
message:'Load data finished!'
});
});
});

if(self._isFunction(callback)) {
return promise.then(result => {
callback(null,result);
}, error => {
callback(error);
});
}
return promise;
}

/**
* Join file json
* @param {string} filePath this is the file path of file json
* @param {string} name join identifier name
* @param {fn} callback [optional] callback(error, data)
* @return {promise|callback}
*/
joinFile(filePath,name,callback) {
var self = this;
self._odm.name = name;
self._odm.scope = 'join';
var promise = new Promise((resolve,reject) => {
fs.createReadStream(filePath,{flag:'w'})
.pipe(jsonStream.parse())
.pipe(
es.mapSync(function(line) {
self._odm.data2.push(line);
})
)
.on('error',function(err) {
reject(err);
})
.on('end', function() {
resolve({
status:true,
message:'Load data finished!'
});
});
});

if(self._isFunction(callback)) {
return promise.then(result => {
callback(null,result);
}, error => {
callback(error);
});
}
return promise;
}

/**
* Join json array
* @param {array} array this is the json array
* @param {string} name join identifier name
* @param {fn} callback [optional] callback(error, data)
* @return {promise|callback}
*/
joinArray(array,name,callback) {
var self = this;
self._odm.name = name;
self._odm.scope = 'join';
var promise = new Promise((resolve,reject) => {
es.readArray(array)
.pipe(
es.mapSync(function(line) {
self._odm.data2.push(line);
})
)
.on('error',function(err) {
reject(err);
})
.on('end', function() {
resolve({
status:true,
message:'Load data finished!'
});
});
});

if(self._isFunction(callback)) {
return promise.then(result => {
callback(null,result);
}, error => {
callback(error);
});
}
return promise;
}

/**
* Save json to file
* @param {string} filePath this is the file path of file json
* @param {fn} callback [optional] callback(error, data)
* @return {promise|callback}
*/
save(filePath, callback) {
var self = this;
const writer = fs.createWriteStream(filePath,{flag:'w'});
var promise = new Promise((resolve,reject) => {
es.readArray(self._odm.data1)
.pipe(
es.stringify()
)
.pipe(writer);
writer.on('error',function(err) {
reject(err);
});
writer.on('finish', function() {
resolve({
status:true,
message:'Save data finished!'
});
});
});

if(self._isFunction(callback)) {
return promise.then(result => {
callback(null,result);
}, error => {
callback(error);
});
}
return promise;
}

/**
* Drop or delete file json
* @param {string} filePath this is the file path of file json
* @param {fn} callback [optional] callback(error, data)
*/
drop(filePath,callback) {
var self = this;
fs.unlink(filePath, function (err) {
if(self._isFunction(callback)) {
if(err) return callback(err);
callback(null,{
status:true,
message:'Data has been dropped!'
});
}
});
}
}

module.exports = FlyJsonStream;
14 changes: 14 additions & 0 deletions test/failure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const assert = require('assert');
const FJS = require('../src/flyjsonstream');
const path = require('path');

describe('Intentional failure test', function() {

it('drop with wrong file path', function() {
var nosql = new FJS();
nosql.drop('test/fixtures/saved.nosqls',function(err,data) {
assert.notEqual(err,null);
});
});

});
3 changes: 3 additions & 0 deletions test/fixtures/data1.nosql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"user_id":1,"name":"budi","age":10}
{"user_id":5,"name":"wawan","age":20}
{"user_id":3,"name":"tono","age":30}
5 changes: 5 additions & 0 deletions test/fixtures/data2.nosql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{"id":1,"address":"bandung","email":"a@b.com"}
{"id":2,"address":"jakarta","email":"c@d.com"}
{"id":3,"address":"solo","email":"e@f.com"}
{"id":4,"address":"solo, balapan","email":"g@h.com"}
{"id":5,"address":"surabaya","email":"i@j.com"}
Loading

0 comments on commit 0a15fae

Please sign in to comment.