sqlite-json
Advanced tools
Comparing version
@@ -6,6 +6,9 @@ #!/usr/bin/env node | ||
function list(val) { return val.split(','); } | ||
program | ||
.version('0.1.6') | ||
.version('0.1.7') | ||
.usage('[options] <database> <table>') | ||
.description('Export a SQLite table to JSON') | ||
.option('-c, --columns <list>', 'Comma-delimited list of columns to output (Default: all)', list) | ||
.option('-o, --output <file>', 'Save result to file', String) | ||
@@ -16,7 +19,11 @@ .option('-k, --key <key>', 'Key output to column', String) | ||
options = (options) || {}; | ||
options = { | ||
key: options && options.key || null, | ||
where: options && options.where || null | ||
key: options.key || null, | ||
where: options.where || null, | ||
columns: options.columns || null | ||
}; | ||
console.error(options.columns); | ||
sj(database).json(table, options, function(err, json) { | ||
@@ -27,5 +34,4 @@ | ||
return; | ||
} | ||
if (options && options.output) | ||
} else if (options && options.output) { | ||
require('fs').writeFile(options.output, json, function(err) { | ||
@@ -36,3 +42,3 @@ if (err) process.stderr.write(err); | ||
else { | ||
} else { | ||
process.stdout.on('error', function(err) { console.error(err); }); | ||
@@ -39,0 +45,0 @@ process.stdout.write(json); |
@@ -23,3 +23,5 @@ const sqlite = require('sqlite3'), | ||
const query = 'SELECT * FROM ' + table + ((options.where) ? ' WHERE ' + options.where : ''); | ||
const columns = (options.columns) ? options.columns.join(', ') : '*', | ||
where = (options.where) ? ' WHERE ' + options.where : '', | ||
query = 'SELECT ' + columns + ' FROM ' + table + where + ';'; | ||
@@ -26,0 +28,0 @@ this.client.all(query, function(err, data) { |
{ | ||
"name": "sqlite-json", | ||
"description": "Convert Sqlite3 tables to JSON", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"homepage": "https://github.com/fitnr/sqlite-json", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -16,3 +16,5 @@ # sqlite-json | ||
-o, --output <file> Save result to file | ||
-c, --columns <list> Comma-delimited list of columns to output (Default: all) | ||
-k, --key <key> Key output to column | ||
-w, --where <clause> WHERE clause to add to table query | ||
``` | ||
@@ -67,2 +69,15 @@ | ||
#### options.columns | ||
An optional list of columns to output. | ||
Type: Array | ||
Example: | ||
```js | ||
exporter.json('myTable', {columns: ['foo']}, function (err, json) { | ||
// "[{"foo": 1}, {"foo": 2}, {"foo": 3}]" | ||
}); | ||
``` | ||
#### options.key | ||
@@ -69,0 +84,0 @@ |
62
test.js
@@ -13,12 +13,8 @@ const fs = require('fs'); | ||
const data = [ | ||
{ name: 'one' }, | ||
{ name: 'two' }, | ||
{ name: 'three' }, | ||
{ name: 'four' }, | ||
{ name: 'five' }, | ||
{ name: 'six' }, | ||
{ name: 'seven' }, | ||
{ name: 'eight' }, | ||
{ name: 'nine' }, | ||
{ name: 'ten' } | ||
{ name: 'Washington', id: 1 }, | ||
{ name: 'Adams', id: 2 }, | ||
{ name: 'Jefferson', id: 3 }, | ||
{ name: 'Madison', id: 4 }, | ||
{ name: 'Monroe', id: 5 }, | ||
{ name: 'Adams', id: 6 }, | ||
]; | ||
@@ -35,7 +31,7 @@ | ||
db.serialize(function(e) { | ||
db.run("CREATE TABLE numbers (name TEXT)"); | ||
var stmt = db.prepare("INSERT INTO numbers VALUES (?)"); | ||
db.run("CREATE TABLE presidents (name TEXT, id INT)"); | ||
var stmt = db.prepare("INSERT INTO presidents VALUES (?, ?)"); | ||
data.forEach(function(row) { | ||
stmt.run(row.name); | ||
stmt.run(row.name, row.id); | ||
}); | ||
@@ -57,3 +53,3 @@ | ||
result.should.have.length(1); | ||
result.should.be.containDeep(['numbers']); | ||
result.should.be.containDeep(['presidents']); | ||
done(e); | ||
@@ -64,4 +60,3 @@ }); | ||
it('should export a table to JSON', function (done) { | ||
sqlitejson.json('numbers', function (err, json) { | ||
sqlitejson.json('presidents', function (err, json) { | ||
should.deepEqual(json, | ||
@@ -78,3 +73,4 @@ JSON.stringify(data), | ||
var dest = 'tmp/bar'; | ||
sqlitejson.save('numbers', dest, function (err, data) { | ||
sqlitejson.save('presidents', dest, function (err, data) { | ||
should.deepEqual(JSON.parse(data), | ||
@@ -91,3 +87,3 @@ JSON.parse(fs.readFileSync(dest)), | ||
const desired = data.reduce(function(o, v) { o[v.name] = v; return o; }, {}); | ||
sqlitejson.json('numbers', {key: "name"}, function (err, json) { | ||
sqlitejson.json('presidents', {key: "name"}, function (err, json) { | ||
should.deepEqual(json, | ||
@@ -102,4 +98,4 @@ JSON.stringify(desired), | ||
it('should filter with a where option', function (done) { | ||
const desired = data.filter(function(i) { return i.name.substr(0, 1) == 't' }, {}); | ||
sqlitejson.json('numbers', {where: "name LIKE 't%'"}, function (err, json) { | ||
const desired = data.filter(function(i) { return i.name == 'Adams'; }, {}); | ||
sqlitejson.json('presidents', {where: "name = 'Adams'"}, function (err, json) { | ||
should.deepEqual(json, | ||
@@ -111,6 +107,32 @@ JSON.stringify(desired), | ||
}); | ||
}); | ||
it('should filter with a columns option', function (done) { | ||
const desired = data.map(function(i) { return {"name": i.name}; }, {}); | ||
sqlitejson.json('presidents', {columns: ["name"]}, function (err, json) { | ||
should.deepEqual(json, | ||
JSON.stringify(desired), | ||
'data should match filtered' | ||
); | ||
done(err); | ||
}); | ||
}); | ||
it('should accept where, key, columns simulataneously', function (done) { | ||
const opts = { | ||
columns: ["name"], | ||
key: "name", | ||
where: "id == 1" | ||
}, | ||
desired = {"Washington": {"name": "Washington"}}; | ||
sqlitejson.json('presidents', opts, function (err, json) { | ||
should.deepEqual(json, | ||
JSON.stringify(desired), | ||
'data should match filtered' | ||
); | ||
done(err); | ||
}); | ||
}); | ||
after(function(){ | ||
@@ -117,0 +139,0 @@ rimraf('./tmp'); |
Sorry, the diff of this file is not supported yet
19922
97.5%9
12.5%194
16.17%130
13.04%