-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
68 lines (51 loc) · 1.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const Database = require('better-sqlite3'),
fs = require('fs'),
path = require('path'),
mkdirp = require('mkdirp'),
JSONbig = require('json-bigint');
module.exports = sqliteJSON;
function sqliteJSON(database) {
if (!(this instanceof sqliteJSON))
return new sqliteJSON(database);
const opts = {readonly: true, fileMustExist: true};
this.client = (database instanceof Database) ? database : new Database(database, opts);
this.client.defaultSafeIntegers();
return this;
}
sqliteJSON.prototype.json = function(sql, options, cb) {
if (options instanceof(Function))
cb = options;
if (sql instanceof(Object)) {
options = sql;
sql = null;
}
if (!sql) {
// make sure the key is in the output
if (options.key && options.columns && options.columns.indexOf(options.key) < 0)
options.columns.push(options.key);
const columns = (options.columns) ? options.columns.join(', ') : '*',
where = (options.where) ? `WHERE ${options.where}` : '';
sql = `SELECT ${columns} FROM ${options.table} ${where}`;
}
var data = this.client.prepare(sql).all();
if (options.key)
data = data.reduce(function(obj, item) { obj[item[options.key]] = item; return obj; }, {});
cb(null, JSONbig.stringify(data));
return this;
};
sqliteJSON.prototype.save = function(table, filename, cb) {
this.json(table, function(err, data) {
if (err) cb(err);
mkdirp.sync(path.dirname(filename));
fs.writeFile(filename, data, function(err) {
if (err) cb(err);
else cb(null, data);
});
});
return this;
};
sqliteJSON.prototype.tables = function(cb) {
const tables = this.client.prepare("SELECT name FROM sqlite_master WHERE type='table'").all();
cb(null, tables.map(function (t) { return t.name; }));
return this;
};