-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
39 lines (34 loc) · 1.02 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
'use strict';
var Promise = require('bluebird');
var _ = require('lodash');
var fs = require('fs');
function getTables (knex, schema) {
return knex
.select('table_schema', 'table_name')
.from('information_schema.tables')
.where('table_schema', schema)
.orderBy('table_name');
}
exports.toJSON = function (connection, schema) {
var knex = require('knex')({ client: 'pg', connection: connection });
return getTables(knex, schema)
.map(function (table) {
return knex.select('*').from(table.table_name)
.then(function (rows) {
return {
table: table.table_name,
rows: _.map(rows, function (row) {
return _.mapValues(row, function (value, key) {
if (_.isArray(value)) {
return "'" + new Buffer(value).toString('hex') + "'::bytea";
}
return value;
});
})
};
});
})
.then(function (tables) {
return _.indexBy(tables, 'table');
});
};