-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add babel dependencies && configuration
- Add the compile npm command - Use (for now) the compiled entry point (dist/diskdb) in the test - Add the polyfill requirement
- Loading branch information
Brice Colucci
committed
Nov 10, 2016
1 parent
b39b652
commit 6c2a1c2
Showing
7 changed files
with
374 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"presets": [ | ||
"latest" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
require('babel-polyfill'); | ||
module.exports = require('./dist/diskdb'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
'use strict'; | ||
|
||
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; }; | ||
|
||
/* | ||
* diskDB | ||
* http://arvindr21.github.io/diskDB | ||
* | ||
* Copyright (c) 2014 Arvind Ravulavaru | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
var util = require('./util'), | ||
path = require('path'), | ||
uuid = require('node-uuid'); | ||
|
||
module.exports = function (db, collectionName, opts) { | ||
|
||
opts = opts || {}; | ||
|
||
// throw an exception if the collection's JSON file is invalid? | ||
opts.throwParseError = opts.throwParseError === undefined ? false : opts.throwParseError; | ||
|
||
var coltn = {}; | ||
coltn.collectionName = collectionName; | ||
coltn._f = path.join(db._db.path, collectionName + '.json'); | ||
|
||
coltn._parse = function () { | ||
try { | ||
return JSON.parse(String(util.readFromFile(this._f))); | ||
} catch (err) { | ||
if (opts.throwParseError) { | ||
throw err; | ||
} | ||
} | ||
return []; | ||
}; | ||
|
||
coltn.find = function (query) { | ||
var collection = this._parse(); | ||
if (!query || Object.keys(query).length === 0) { | ||
return collection; | ||
} else { | ||
var searcher = new util.ObjectSearcher(); | ||
return searcher.findAllInObject(collection, query, true); | ||
} | ||
}; | ||
|
||
coltn.findOne = function (query) { | ||
var collection = this._parse(); | ||
if (!query) { | ||
return collection[0]; | ||
} else { | ||
var searcher = new util.ObjectSearcher(); | ||
return searcher.findAllInObject(collection, query, false)[0]; | ||
} | ||
}; | ||
|
||
coltn.save = function (data) { | ||
var collection = this._parse(); | ||
if ((typeof data === 'undefined' ? 'undefined' : _typeof(data)) === 'object' && data.length) { | ||
if (data.length === 1) { | ||
if (data[0].length > 0) { | ||
data = data[0]; | ||
} | ||
} | ||
var retCollection = []; | ||
for (var i = data.length - 1; i >= 0; i--) { | ||
var d = data[i]; | ||
d._id = uuid.v4().replace(/-/g, ''); | ||
collection.push(d); | ||
retCollection.push(d); | ||
} | ||
util.writeToFile(this._f, collection); | ||
return retCollection; | ||
}{ | ||
data._id = uuid.v4().replace(/-/g, ''); | ||
collection.push(data); | ||
util.writeToFile(this._f, collection); | ||
return data; | ||
} | ||
}; | ||
|
||
coltn.update = function (query, data, options) { | ||
var ret = {}, | ||
collection = this._parse(); // update | ||
var records = util.finder(collection, query, true); | ||
if (records.length) { | ||
if (options && options.multi) { | ||
collection = util.updateFiltered(collection, query, data, true); | ||
ret.updated = records.length; | ||
ret.inserted = 0; | ||
} else { | ||
collection = util.updateFiltered(collection, query, data, false); | ||
ret.updated = 1; | ||
ret.inserted = 0; | ||
} | ||
} else { | ||
if (options && options.upsert) { | ||
data._id = uuid.v4().replace(/-/g, ''); | ||
collection.push(data); | ||
ret.updated = 0; | ||
ret.inserted = 1; | ||
} else { | ||
ret.updated = 0; | ||
ret.inserted = 0; | ||
} | ||
} | ||
util.writeToFile(this._f, collection); | ||
return ret; | ||
}; | ||
|
||
coltn.remove = function (query, multi) { | ||
if (query) { | ||
var collection = this._parse(); | ||
if (typeof multi === 'undefined') { | ||
multi = true; | ||
} | ||
collection = util.removeFiltered(collection, query, multi); | ||
|
||
util.writeToFile(this._f, collection); | ||
} else { | ||
util.removeFile(this._f); | ||
delete db[collectionName]; | ||
} | ||
return true; | ||
}; | ||
|
||
coltn.count = function () { | ||
return this._parse().length; | ||
}; | ||
|
||
return coltn; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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; }; | ||
|
||
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\']')); | ||
} | ||
return this; | ||
} | ||
|
||
}; | ||
|
||
module.exports = db; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
'use strict'; | ||
|
||
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; }; | ||
|
||
/* | ||
* diskDB | ||
* http://arvindr21.github.io/diskDB | ||
* | ||
* Copyright (c) 2014 Arvind Ravulavaru | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
/*jshint -W027*/ | ||
var fs = require('fs'); | ||
var merge = require('merge'); | ||
var util = {}; | ||
|
||
util.isValidPath = function (path) { | ||
return fs.existsSync(path); | ||
}; | ||
|
||
util.writeToFile = function (outputFilename, content) { | ||
if (!content) { | ||
content = []; | ||
} | ||
fs.writeFileSync(outputFilename, JSON.stringify(content, null, 0)); | ||
}; | ||
|
||
util.readFromFile = function (file) { | ||
return fs.readFileSync(file, 'utf-8'); | ||
}; | ||
|
||
util.removeFile = function (file) { | ||
return fs.unlinkSync(file); | ||
}; | ||
|
||
util.updateFiltered = function (collection, query, data, multi) { | ||
// break 2 loops at once - multi : false | ||
loop: for (var i = collection.length - 1; i >= 0; i--) { | ||
var c = collection[i]; | ||
for (var p in query) { | ||
if (p in c && c[p] == query[p]) { | ||
collection[i] = merge(c, data); | ||
if (!multi) { | ||
break loop; | ||
} | ||
} | ||
} | ||
} | ||
return collection; | ||
}; | ||
|
||
// [TODO] : Performance | ||
util.removeFiltered = function (collection, query, multi) { | ||
// break 2 loops at once - multi : false | ||
loop: for (var i = collection.length - 1; i >= 0; i--) { | ||
var c = collection[i]; | ||
for (var p in query) { | ||
if (p in c && c[p] == query[p]) { | ||
collection.splice(i, 1); | ||
if (!multi) { | ||
break loop; | ||
} | ||
} | ||
} | ||
} | ||
return collection; | ||
}; | ||
|
||
// [TODO] : Performance | ||
util.finder = function (collection, query, multi) { | ||
var retCollection = []; | ||
loop: for (var i = collection.length - 1; i >= 0; i--) { | ||
var c = collection[i]; | ||
for (var p in query) { | ||
if (p in c && c[p] == query[p]) { | ||
retCollection.push(collection[i]); | ||
if (!multi) { | ||
break loop; | ||
} | ||
} | ||
} | ||
} | ||
return retCollection; | ||
}; | ||
|
||
/** recursive finder **/ | ||
util.ObjectSearcher = function () { | ||
this.results = []; | ||
this.objects = []; | ||
this.resultIDS = {}; | ||
}; | ||
|
||
util.ObjectSearcher.prototype.findAllInObject = function (object, valueOBj, isMulti) { | ||
for (var objKey in object) { | ||
this.performSearch(object[objKey], valueOBj, object[objKey]); | ||
if (!isMulti && this.results.length == 1) { | ||
return this.results; | ||
} | ||
} | ||
|
||
while (this.objects.length !== 0) { | ||
var objRef = this.objects.pop(); | ||
this.performSearch(objRef['_obj'], valueOBj, objRef['parent']); | ||
if (!isMulti && this.results.length == 1) { | ||
return this.results; | ||
} | ||
} | ||
|
||
return this.results; | ||
}; | ||
|
||
util.ObjectSearcher.prototype.performSearch = function (object, valueOBj, opt_parentObj) { | ||
for (var criteria in valueOBj) { | ||
var query = {}; | ||
query[criteria] = valueOBj[criteria]; | ||
this.searchObject(object, query, opt_parentObj); | ||
} | ||
|
||
for (var i = 0; i < this.results.length; i++) { | ||
var result = this.results[i]; | ||
for (var field in valueOBj) { | ||
if (result[field] !== undefined) { | ||
if (result[field] !== valueOBj[field]) { | ||
this.results.splice(i, 1); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
util.ObjectSearcher.prototype.searchObject = function (object, valueOBj, opt_parentObj) { | ||
for (var objKey in object) { | ||
if (_typeof(object[objKey]) != 'object') { | ||
if (valueOBj[objKey] == object[objKey]) { | ||
if (opt_parentObj !== undefined) { | ||
if (this.resultIDS[opt_parentObj['_id']] === undefined) { | ||
this.results.push(opt_parentObj); | ||
this.resultIDS[opt_parentObj['_id']] = ''; | ||
} | ||
} else { | ||
if (this.resultIDS[object['_id']] === undefined) { | ||
this.results.push(object); | ||
this.resultIDS[object['_id']] = ''; | ||
} | ||
} | ||
} | ||
} else { | ||
var obj = object; | ||
if (opt_parentObj !== undefined) { | ||
obj = opt_parentObj; | ||
} | ||
var objRef = { | ||
parent: obj, | ||
_obj: object[objKey] | ||
}; | ||
|
||
this.objects.push(objRef); | ||
} | ||
} | ||
}; | ||
|
||
module.exports = util; |
Oops, something went wrong.