Skip to content

Commit

Permalink
Refactor to es6 diskdb file
Browse files Browse the repository at this point in the history
  • Loading branch information
Brice Colucci committed Nov 10, 2016
1 parent 6c2a1c2 commit fd73989
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 95 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
@@ -0,0 +1,12 @@

root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
3 changes: 2 additions & 1 deletion .jshintrc
Expand Up @@ -13,5 +13,6 @@
"eqnull": true,
"node": true,
"browser": true,
"newcap": false
"newcap": false,
"esversion": 6
}
128 changes: 77 additions & 51 deletions dist/diskdb.js
@@ -1,60 +1,86 @@
/*
* diskDB
* http://arvindr21.github.io/diskDB
*
* Copyright (c) 2014 Arvind Ravulavaru
* Licensed under the MIT license.
*/

'use strict';
//global modules

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
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; }; }(); /*
* diskDB
* http://arvindr21.github.io/diskDB
*
* Copyright (c) 2014 Arvind Ravulavaru
* Licensed under the MIT license.
*/

// global modules

var path = require('path'),
c = require('chalk'),
e = c.red,
s = c.green;

//local modules
var util = require('./util');

var db = {
connect: function connect(path, collections) {
if (util.isValidPath(path)) {
var _db = {};
_db.path = path;
this._db = _db;
console.log(s('Successfully connected to : ' + path));
if (collections) {
this.loadCollections(collections);
}
} else {
console.log(e('The DB Path [' + path + '] does not seem to be valid. Recheck the path and try again'));
return false;
}
return this;
},
loadCollections: function loadCollections(collections) {
if (!this._db) {
console.log(e('Initialize the DB before you add collections. Use : ', 'db.connect(\'path-to-db\');'));
return false;
}
if ((typeof collections === 'undefined' ? 'undefined' : _typeof(collections)) === 'object' && collections.length) {
for (var i = 0; i < collections.length; i++) {
var p = path.join(this._db.path, collections[i].indexOf('.json') >= 0 ? collections[i] : collections[i] + '.json');
if (!util.isValidPath(p)) {
util.writeToFile(p);
}
var _c = collections[i].replace('.json', '');
this[_c] = new require('./collection')(this, _c);
}
} else {
console.log(e('Invalid Collections Array.', 'Expected Format : ', '[\'collection1\',\'collection2\',\'collection3\']'));


var _path = require('path');

var _chalk = require('chalk');

var _util = require('./util');

var _collection = require('./collection');

var _collection2 = _interopRequireDefault(_collection);

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

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

var DiskDB = function () {
function DiskDB() {
_classCallCheck(this, DiskDB);
}

_createClass(DiskDB, [{
key: 'connect',
value: function connect(path, collections) {
if ((0, _util.isValidPath)(path)) {
this._db = { path: path };
console.log((0, _chalk.green)('Successfully connected to : ' + path));
if (collections) {
this.loadCollections(collections);
}
return this;
} else {
console.log((0, _chalk.red)('The DB Path [' + path + '] does not seem to be valid. Recheck the path and try again'));
return false;
}
return this;
}
}, {
key: 'loadCollections',
value: function loadCollections(collections) {
var _this = this;

if (!this._db) {
console.log((0, _chalk.red)('Initialize the DB before you add collections. Use : ', 'db.connect(\'path-to-db\');'));
return false;
}
if (Array.isArray(collections)) {
collections.forEach(function (collection) {
if (!collection.includes('.json')) {
collection = collection + '.json';
}
var collectionFile = (0, _path.join)(_this._db.path, collection);
if (!(0, _util.isValidPath)(collectionFile)) {
(0, _util.writeToFile)(collectionFile);
}
var collectionName = collection.replace('.json', '');
_this[collectionName] = new _collection2.default(_this, collectionName);
});
} else {
console.log((0, _chalk.red)('Invalid Collections Array.', 'Expected Format : ', '[\'collection1\',\'collection2\',\'collection3\']'));
}
return this;
}
}]);

};
return DiskDB;
}();

module.exports = db;
exports.default = DiskDB;
82 changes: 40 additions & 42 deletions lib/diskdb.js
Expand Up @@ -6,53 +6,51 @@
* Licensed under the MIT license.
*/

'use strict';
//global modules
var path = require('path'),
c = require('chalk'),
e = c.red,
s = c.green;
// global modules
import { join } from 'path';
import { red as e, green as s } from 'chalk';

//local modules
var util = require('./util');
import { isValidPath, writeToFile } from './util';
import Collection from './collection';

export default class DiskDB {

var db = {
connect: function(path, collections) {
if (util.isValidPath(path)) {
var _db = {};
_db.path = path;
this._db = _db;
console.log(s('Successfully connected to : ' + path));
if (collections) {
this.loadCollections(collections);
}
} else {
console.log(e('The DB Path [' + path + '] does not seem to be valid. Recheck the path and try again'));
return false;
}
return this;
},
loadCollections: function(collections) {
if (!this._db) {
console.log(e('Initialize the DB before you add collections. Use : ', 'db.connect(\'path-to-db\');'));
return false;
connect(path, collections) {
if (isValidPath(path)) {
this._db = { path };
console.log(s('Successfully connected to : ' + path));
if (collections) {
this.loadCollections(collections);
}
} else {
console.log(e('The DB Path [' + path + '] does not seem to be valid. Recheck the path and try again'));
return false;
}
return this;
}

loadCollections(collections) {
if (!this._db) {
console.log(e('Initialize the DB before you add collections. Use : ', 'db.connect(\'path-to-db\');'));
return false;
}
if (Array.isArray(collections)) {
collections.forEach(collection => {
if (!collection.includes('.json')) {
collection = `${collection}.json`;
}
if (typeof collections === 'object' && collections.length) {
for (var i = 0; i < collections.length; i++) {
var p = path.join(this._db.path, (collections[i].indexOf('.json') >= 0 ? collections[i] : collections[i] + '.json'));
if (!util.isValidPath(p)) {
util.writeToFile(p);
}
var _c = collections[i].replace('.json', '');
this[_c] = new require('./collection')(this, _c);
}
} else {
console.log(e('Invalid Collections Array.', 'Expected Format : ', '[\'collection1\',\'collection2\',\'collection3\']'));
const collectionFile = join(this._db.path, collection);
if (!isValidPath(collectionFile)) {
writeToFile(collectionFile);
}
return this;
const collectionName = collection.replace('.json', '');
this[collectionName] = new Collection(this, collectionName);
});
} else {
console.log(e('Invalid Collections Array.', 'Expected Format : ', '[\'collection1\',\'collection2\',\'collection3\']'));
}
return this;
}

};

module.exports = db;
}
4 changes: 3 additions & 1 deletion test/diskdb_test.js
@@ -1,9 +1,11 @@
'use strict';

var diskdb = require('..');
var DiskDB = new require('..').default;
var path = require('path');
var fs = require('fs');

const diskdb = new DiskDB();

/*
======== A Handy Little Nodeunit Reference ========
https://github.com/caolan/nodeunit
Expand Down

0 comments on commit fd73989

Please sign in to comment.