Skip to content

Commit 4d2660a

Browse files
committed
Added initial snapshot tests
1 parent f8c9751 commit 4d2660a

File tree

7 files changed

+398
-164
lines changed

7 files changed

+398
-164
lines changed

.editorconfig

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[*]
2+
insert_final_newline = true
3+
indent_style = space
4+
indent_size = 2
5+
charset = utf-8
6+
trim_trailing_whitespace = true
7+
insert_final_newline = true

.eslintrc

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
11
{
2-
"extends": "bliss"
2+
"extends": "bliss",
3+
"rules": {
4+
"class-methods-use-this": "off",
5+
"fp/no-arguments": "off",
6+
"fp/no-class": "off",
7+
"fp/no-delete": "off",
8+
"fp/no-events": "off",
9+
"fp/no-get-set": "off",
10+
"fp/no-let": "off",
11+
"fp/no-loops": "off",
12+
"fp/no-mutating-assign": "off",
13+
"fp/no-mutating-methods": "off",
14+
"fp/no-mutation": "off",
15+
"fp/no-nil": "off",
16+
"fp/no-proxy": "off",
17+
"fp/no-rest-parameters": "off",
18+
"fp/no-this": "off",
19+
"fp/no-throw": "off",
20+
"fp/no-unused-expression": "off",
21+
"fp/no-valueof-field": "off",
22+
"no-var": "off",
23+
"no-let": "off",
24+
"no-plusplus": "off",
25+
"no-use-before-define": "off",
26+
"no-console": "off",
27+
"promise/avoid-new": "off"
28+
}
329
}

bin/sqlite-json.js

+56-51
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,67 @@
11
#!/usr/bin/env node
22

3-
const program = require('commander'),
4-
sj = require('..');
3+
const program = require('commander');
4+
const SqliteJson = require('..');
55

6-
function list(val) { return val.split(','); }
6+
function list(val) {
7+
return val.split(',');
8+
}
9+
10+
const sqliteJson = new SqliteJson();
711

812
program
9-
.version('1.0.0')
10-
.usage('[options] <database> [sql]')
11-
.description('Export a SQLite table to JSON')
12-
.option('-k, --key <key>', 'Key output to column', String)
13-
.option('-t, --table <table>', 'table to query', String)
14-
.option('-c, --columns <list>', 'Comma-delimited list of columns to output (Default: all)', list)
15-
.option('-w, --where <clause>', 'WHERE clause to add to table query', String)
16-
.option('-o, --output <file>', 'Save result to file', String)
17-
.action(function(database, sql, options) {
18-
if (typeof(sql) == 'object' && typeof(options) === 'undefined') {
19-
options = sql;
20-
sql = null;
13+
.version('1.0.0')
14+
.usage('[options] <database> [sql]')
15+
.description('Export a SQLite table to JSON')
16+
.option('-k, --key <key>', 'Key output to column', String)
17+
.option('-t, --table <table>', 'table to query', String)
18+
.option(
19+
'-c, --columns <list>',
20+
'Comma-delimited list of columns to output (Default: all)',
21+
list
22+
)
23+
.option('-w, --where <clause>', 'WHERE clause to add to table query', String)
24+
.option('-o, --output <file>', 'Save result to file', String)
25+
.action((database, sql, options) => {
26+
if (typeof sql === 'object' && typeof options === 'undefined') {
27+
options = sql;
28+
sql = null;
29+
}
30+
31+
const output = options.output;
32+
33+
options = {
34+
table: options.table || null,
35+
query: options.query || null,
36+
key: options.key || null,
37+
where: options.where || null,
38+
columns: options.columns || null
39+
};
40+
41+
sqliteJson(database).json(sql, options, (err, json) => {
42+
if (err) {
43+
if (String(err).indexOf('no such table') > -1) {
44+
console.error('error: table not found');
45+
} else {
46+
console.error(err.message);
2147
}
2248

23-
const output = options.output;
24-
25-
options = {
26-
table: options.table || null,
27-
query: options.query || null,
28-
key: options.key || null,
29-
where: options.where || null,
30-
columns: options.columns || null
31-
};
32-
33-
sj(database).json(sql, options, function(err, json) {
34-
35-
if (err) {
36-
if (String(err).indexOf('no such table') > -1)
37-
console.error('error: table not found');
38-
else
39-
console.error(err.message);
40-
41-
process.exit(1);
42-
43-
} else if (output) {
44-
require('fs').writeFile(output, json, function(err) {
45-
if (err) {
46-
process.stderr.write(err.message);
47-
process.exit(1);
48-
}
49-
else process.stdout.write(program.output + '\n');
50-
});
51-
52-
} else {
53-
process.stdout.on('error', function(err) {
54-
console.error(err.message);
55-
process.exit(1);
56-
});
57-
process.stdout.write(json + '\n');
58-
}
49+
process.exit(1);
50+
} else if (output) {
51+
require('fs').writeFile(output, json, err => {
52+
if (err) {
53+
process.stderr.write(err.message);
54+
process.exit(1);
55+
} else process.stdout.write(`${program.output}\n`);
56+
});
57+
} else {
58+
process.stdout.on('error', err => {
59+
console.error(err.message);
60+
process.exit(1);
5961
});
62+
process.stdout.write(`${json}\n`);
63+
}
6064
});
65+
});
6166

6267
program.parse(process.argv);

index.js

+66-64
Original file line numberDiff line numberDiff line change
@@ -3,91 +3,93 @@ const fs = require('fs');
33
const path = require('path');
44
const mkdirp = require('mkdirp');
55

6-
module.exports = sqliteJSON;
7-
8-
function sqliteJSON(database) {
9-
if (!(this instanceof sqliteJSON)) {
10-
return new sqliteJSON(database);
11-
}
12-
13-
this.client = database instanceof sqlite.Database
14-
? database
15-
: new sqlite.Database(database);
6+
class SqliteJson {
7+
constructor(database) {
8+
if (!(this instanceof SqliteJson)) {
9+
return new SqliteJson(database);
10+
}
1611

17-
return this;
18-
}
12+
this.client = database instanceof sqlite.Database
13+
? database
14+
: new sqlite.Database(database);
1915

20-
sqliteJSON.prototype.json = function (sql, options, cb) {
21-
if (options instanceof Function) {
22-
cb = options;
16+
return this;
2317
}
2418

25-
if (sql instanceof Object) {
26-
options = sql;
27-
sql = null;
28-
}
19+
json(sql, options, cb) {
20+
if (options instanceof Function) {
21+
cb = options;
22+
}
2923

30-
if (!sql) {
31-
// make sure the key is in the output
32-
if (
33-
options.key &&
34-
options.columns &&
35-
options.columns.indexOf(options.key) < 0
36-
) {
37-
options.columns.push(options.key);
24+
if (sql instanceof Object) {
25+
options = sql;
26+
sql = null;
3827
}
3928

40-
const columns = options.columns ? options.columns.join(', ') : '*';
41-
const where = options.where ? ` WHERE ${options.where}` : '';
29+
if (!sql) {
30+
// make sure the key is in the output
31+
if (
32+
options.key &&
33+
options.columns &&
34+
options.columns.indexOf(options.key) < 0
35+
) {
36+
options.columns.push(options.key);
37+
}
4238

43-
sql = `SELECT ${columns} FROM ${options.table}${where};`;
44-
}
39+
const columns = options.columns ? options.columns.join(', ') : '*';
40+
const where = options.where ? ` WHERE ${options.where}` : '';
4541

46-
this.client.all(sql, (err, data) => {
47-
if (err) {
48-
cb(err);
49-
return;
42+
sql = `SELECT ${columns} FROM ${options.table}${where};`;
5043
}
5144

52-
if (options.key) {
53-
data = data.reduce((obj, item) => {
54-
obj[item[options.key]] = item;
55-
return obj;
56-
}, {});
57-
}
45+
this.client.all(sql, (err, data) => {
46+
if (err) {
47+
cb(err);
48+
return;
49+
}
5850

59-
cb(null, JSON.stringify(data));
60-
});
51+
if (options.key) {
52+
data = data.reduce((obj, item) => {
53+
obj[item[options.key]] = item;
54+
return obj;
55+
}, {});
56+
}
6157

62-
return this;
63-
};
58+
cb(null, JSON.stringify(data));
59+
});
6460

65-
sqliteJSON.prototype.save = function (table, filename, cb) {
66-
this.json(table, (err, data) => {
67-
if (err) cb(err);
61+
return this;
62+
}
6863

69-
mkdirp(path.dirname(filename), err => {
64+
save(table, filename, cb) {
65+
this.json(table, (err, data) => {
7066
if (err) cb(err);
7167

72-
fs.writeFile(filename, data, err => {
68+
mkdirp(path.dirname(filename), err => {
7369
if (err) cb(err);
74-
else cb(null, data);
70+
71+
fs.writeFile(filename, data, err => {
72+
if (err) cb(err);
73+
else cb(null, data);
74+
});
7575
});
7676
});
77-
});
7877

79-
return this;
80-
};
78+
return this;
79+
}
8180

82-
sqliteJSON.prototype.tables = function (cb) {
83-
const query = "SELECT name FROM sqlite_master WHERE type='table'";
81+
tables(cb) {
82+
const query = "SELECT name FROM sqlite_master WHERE type='table'";
8483

85-
this.client.all(query, (err, tables) => {
86-
if (err) {
87-
cb(err);
88-
}
89-
cb(null, tables.map(t => t.name));
90-
});
84+
this.client.all(query, (err, tables) => {
85+
if (err) {
86+
cb(err);
87+
}
88+
cb(null, tables.map(t => t.name));
89+
});
90+
91+
return this;
92+
}
93+
}
9194

92-
return this;
93-
};
95+
module.exports = SqliteJson;

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
"license": "MIT",
1919
"main": "lib/index.js",
2020
"engines": {
21-
"node": ">=4"
21+
"node": ">=8"
2222
},
2323
"scripts": {
24+
"lint": "eslint .",
2425
"test": "jest"
2526
},
2627
"devDependencies": {
2728
"eslint": "^3.19.0",
2829
"eslint-config-bliss": "^1.0.8",
2930
"jest-cli": "^20.0.4",
31+
"rimraf": "^2.6.1",
3032
"sqlite3": "^3.1.8"
3133
},
3234
"keywords": [

0 commit comments

Comments
 (0)