adon-candle
Advanced tools
Comparing version 1.0.7 to 1.0.9
@@ -15,2 +15,10 @@ 'use strict'; | ||
var _fs = require('fs'); | ||
var _fs2 = _interopRequireDefault(_fs); | ||
var _path = require('path'); | ||
var _path2 = _interopRequireDefault(_path); | ||
var _bluebird = require('bluebird'); | ||
@@ -20,2 +28,6 @@ | ||
var _xml2js = require('xml2js'); | ||
var _xml2js2 = _interopRequireDefault(_xml2js); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -25,2 +37,5 @@ | ||
var fsAsync = _bluebird2.default.promisifyAll(_fs2.default), | ||
xmlAsync = _bluebird2.default.promisifyAll(_xml2js2.default); | ||
var Candle = function () { | ||
@@ -39,4 +54,24 @@ function Candle(Models) { | ||
return new _bluebird2.default(function (resolve, reject) { | ||
switch (_path2.default.extname(options.file).toLowerCase()) { | ||
case 'csv': | ||
return _this._csvToDatabase(options); | ||
case 'json': | ||
return _this._jsonToDatabase(options); | ||
case 'xml': | ||
return _this._xmlToDatabase(options); | ||
default: | ||
return reject(new Error('File type not supported')); | ||
} | ||
}); | ||
} | ||
}, { | ||
key: '_csvToDatabase', | ||
value: function _csvToDatabase(options) { | ||
var _this2 = this; | ||
return new _bluebird2.default(function (resolve, reject) { | ||
(0, _csvtojson2.default)({ delimiter: options.delimiter || ';' }).fromFile('' + process.cwd() + options.file).on('json', function (line) { | ||
return _this.lineToDatabase(_extends({ line: line }, options)).catch(function (err) { | ||
_this2._lineToDatabase(_extends({ line: line }, options)).then(function (err) { | ||
if (err) console.log(err); | ||
}).catch(function (err) { | ||
return reject(err); | ||
@@ -51,16 +86,15 @@ }); | ||
}, { | ||
key: 'lineToDatabase', | ||
value: function lineToDatabase(options) { | ||
var _this2 = this; | ||
key: '_jsonToDatabase', | ||
value: function _jsonToDatabase(options) { | ||
var _this3 = this; | ||
return new _bluebird2.default(function (resolve, reject) { | ||
var value = options.line; | ||
if (options.database) value.database = options.database; | ||
if (options.date) value.date = Date.now(); | ||
new _this2.Models[options.type](value).save(function (err) { | ||
if (err) { | ||
if (options.safe) return reject(err); | ||
console.log(err); | ||
} | ||
fsAsync.readFileAsync('' + process.cwd() + options.file, 'utf8').then(function (data) { | ||
return JSON.parse(data).reduce(function (promises, line) { | ||
return _this3._lineToDatabase(_extends({ line: line }, options)); | ||
}); | ||
}, _bluebird2.default.resolve()).then(function () { | ||
return resolve(); | ||
}).catch(function (err) { | ||
return reject(err); | ||
}); | ||
@@ -70,13 +104,68 @@ }); | ||
}, { | ||
key: '_xmlToDatabase', | ||
value: function _xmlToDatabase(options) { | ||
var _this4 = this; | ||
return new _bluebird2.default(function (resolve, reject) { | ||
fsAsync.readFileAsync('' + process.cwd() + options.file, 'utf8').then(function (data) { | ||
return xmlAsync.parseStringAsync(data); | ||
}).then(function (json) { | ||
return json[options.root ? options.root : 'root.line'].reduce(function (promises, line) { | ||
return _this4._lineToDatabase(_extends({ line: line }, options)); | ||
}); | ||
}, _bluebird2.default.resolve()).then(function () { | ||
return resolve(); | ||
}).catch(function (err) { | ||
return reject(err); | ||
}); | ||
}); | ||
} | ||
}, { | ||
key: '_lineToDatabase', | ||
value: function _lineToDatabase(options) { | ||
var _this5 = this; | ||
return new _bluebird2.default(function (resolve, reject) { | ||
var line = _extends({ | ||
database: options.database ? options.database : null, | ||
date: options.date ? Date.now() : null | ||
}, options.line); | ||
return new _this5.Models[options.type](line).save().then(function () { | ||
return resolve(); | ||
}).catch(function (err) { | ||
if (!options.safe) return resolve(err); | ||
return _this5.removeFromDatabase(options).then(function () { | ||
return reject(err); | ||
}).catch(function (err2) { | ||
return reject(err2); | ||
}); | ||
}); | ||
}); | ||
} | ||
}, { | ||
key: 'linesFromDatabase', | ||
value: function linesFromDatabase(options) { | ||
var _this3 = this; | ||
var _this6 = this; | ||
return new _bluebird2.default(function (resolve, reject) { | ||
_this3.Models[options.database.type].find({ database: options.database._id }, function (err, lines) { | ||
if (err) return reject(err); | ||
return _this6.Models[options.type].find({ database: options.database }).then(function (lines) { | ||
return resolve(lines); | ||
}).catch(function (err) { | ||
return reject(err); | ||
}); | ||
}); | ||
} | ||
}, { | ||
key: 'removeFromDatabase', | ||
value: function removeFromDatabase(options) { | ||
var _this7 = this; | ||
return new _bluebird2.default(function (resolve, reject) { | ||
return _this7.Models[options.type].deleteMany({ database: options.database }).then(function () { | ||
return resolve(); | ||
}).catch(function (err) { | ||
return reject(err); | ||
}); | ||
}); | ||
} | ||
}]); | ||
@@ -83,0 +172,0 @@ |
{ | ||
"name": "adon-candle", | ||
"version": "1.0.7", | ||
"version": "1.0.9", | ||
"license": "MIT", | ||
@@ -31,3 +31,4 @@ "author": "Martin PARENT <mparent@adon-immo.com> (https://adon-immo.com)", | ||
"bluebird": "^3.5.1", | ||
"csvtojson": "^1.1.12" | ||
"csvtojson": "^1.1.12", | ||
"xml2js": "^0.4.19" | ||
}, | ||
@@ -34,0 +35,0 @@ "devDependencies": { |
@@ -46,3 +46,3 @@ # AdOn Candle | ||
#### fileToDatabase() | ||
#### fileToDatabase(options) | ||
@@ -76,3 +76,5 @@ Assuming you already got a mongoose instance connected to MongoDB and your file uploaded somewhere in your application scope, you should first create a database document to reference all lines for further queries : | ||
new Database({ _id: ObjectId(), type: 'MyModel' }).save(err => // Treat errors) | ||
new Database({ _id: ObjectId(), type: 'MyModel' }).save() | ||
.then(database => // You can use database | ||
.catch(err => // Treat errors) | ||
``` | ||
@@ -87,5 +89,6 @@ | ||
, database: // A valid mongoose ObjectId for reference, default to null | ||
, delimiter: // The CSV delimiter, default to ';' | ||
, date: // Overwrite the 'date' data field with Date.now() if set to true | ||
, safe: // Reject the promise if set to true and a line contains errors | ||
, delimiter: // The CSV delimiter, default to ';' | ||
, root: // The XML root path to lines, default to 'root.line' | ||
}) | ||
@@ -110,3 +113,3 @@ ``` | ||
#### linesFromDatabase() | ||
#### linesFromDatabase(options) | ||
@@ -117,3 +120,4 @@ To retrieve all lines referencing the same `Database` instance, simply pass a single config object with `database` property | ||
candle.linesFromDatabase({ | ||
database: // A database object with type and _id property | ||
type: // The type of database model to use (from those provided to the class constructor) | ||
, database: // A valid mongoose ObjectId | ||
}) | ||
@@ -124,10 +128,27 @@ .then(lines => // Do something with your lines) | ||
#### removeFromDatabase(options) | ||
This function is called by `fileToDatabase()` if an error occur on any lines and the `safe` property is set to true. You can also call it to remove all lines associated to a `database` : | ||
``` | ||
candle.removeFromDatabase({ | ||
type: // The type of database model to use (from those provided to the class constructor) | ||
, database: // A valid mongoose ObjectId | ||
}) | ||
.then(() => // All lines are removed) | ||
.catch(err => // Treat errors) | ||
``` | ||
## Behaviors | ||
The file format will be automatically detected by its extension (CSV - JSON - XML - YAML and maybe XLS) | ||
The file format is automatically detected by its extension (currently CSV, JSON and XML). | ||
A cleaning operation removing all saved lines in case the `safe` property is set to true and a line does not match its model will be added | ||
A cleaning operation removing all saved lines in case the `safe` property is set to true and a line does not match its model is performed. | ||
The reverse operation `databaseToFile` will be added | ||
## Coming Soon | ||
- YAML and XLS format | ||
- Reverse operation `databaseToFile` | ||
## Dependencies | ||
@@ -134,0 +155,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
101709
264
156
3
2
+ Addedxml2js@^0.4.19
+ Addedsax@1.4.1(transitive)
+ Addedxml2js@0.4.23(transitive)
+ Addedxmlbuilder@11.0.1(transitive)