Comparing version 0.1.0 to 0.2.0
@@ -5,3 +5,3 @@ var fs = require('fs'); | ||
class CsvDB { | ||
constructor (file, fields) { | ||
constructor (file, fields = null) { | ||
this.file = file; | ||
@@ -29,2 +29,11 @@ this.fields = fields; | ||
var lines = input.split('\n'); | ||
let fields; | ||
if (this.fields === null) { | ||
fields = lines.shift().split(';'); | ||
if (fields[fields.length -1] === '') { | ||
fields.pop(); | ||
} | ||
} else { | ||
fields = this.fields; | ||
} | ||
var result = []; | ||
@@ -36,4 +45,4 @@ if (Array.isArray(lines) && lines.length > 0) { | ||
for (var j = 0; j < this.fields.length; j++) { | ||
result[i][this.fields[j]] = cols[j]; | ||
for (var j = 0; j < fields.length; j++) { | ||
result[i][fields[j]] = cols[j]; | ||
} | ||
@@ -108,6 +117,10 @@ } | ||
if (Array.isArray(input) && input.length > 0) { | ||
let fields = Object.keys(input[0]); | ||
result.push(fields.join(';') + ';'); | ||
for (var i = 0; i < input.length; i++) { | ||
row = []; | ||
for (var j = 0; j < this.fields.length; j++) { | ||
row.push(input[i][this.fields[j]]); | ||
for (var j = 0; j < fields.length; j++) { | ||
row.push(input[i][fields[j]]); | ||
} | ||
@@ -128,3 +141,3 @@ result.push(row.join(';') + ';'); | ||
existingContent = existingContent.map(item => { | ||
if (item.id === data[i].id) { | ||
if (item.id === data[i].id.toString) { | ||
return Object.assign(item, data[i]); | ||
@@ -131,0 +144,0 @@ } |
{ | ||
"name": "csv-db", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Simple filebased database", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -21,2 +21,3 @@ # CSV based Database | ||
``` | ||
const CsvDb = require('csv-db'); | ||
const csvDb = new CsvDb('input.csv', ['id', 'username', 'password']); | ||
@@ -39,7 +40,51 @@ ``` | ||
one or more objects, depending on what was fetched. | ||
``` | ||
csvDb.get().then((data) => { | ||
console.log(data); | ||
}, (err) => { | ||
console.log(err); | ||
}); | ||
``` | ||
## insert(newData) | ||
Insert a new row to the database. Data is an object with column names as keys and the values to be inserted as - the values. | ||
``` | ||
const user = { | ||
name: 'basti', | ||
secret: 'topSecret' | ||
}; | ||
csvDb.insert(user).then((data) => { | ||
console.log(data); | ||
}, (err) => { | ||
console.log(err); | ||
}); | ||
``` | ||
## update(data, id) | ||
Update an existing row. Data is an object containing ALL the values excluding the id. id is the identifier of the row to be updated. | ||
``` | ||
const user = { | ||
id: 2, | ||
name: 'basti', | ||
secret: 'topSecret' | ||
}; | ||
csvDb.update(user, 2).then((data) => { | ||
console.log(data); | ||
}, (err) => { | ||
console.log(err); | ||
}); | ||
``` | ||
## delete(id) | ||
Delete an existing row. id is the identifier of the row to be deleted. | ||
``` | ||
csvDb.delete(2).then((data) => { | ||
console.log(data); | ||
}, (err) => { | ||
console.log(err); | ||
}); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18124
455
89
8