-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
84 lines (75 loc) · 2.23 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var fs = require('fs');
var async = require('async');
var readTable = require('./lib/readTable');
var makeIndexFile = require('./lib/makeIndexFile');
var fileFromEnum = require('./lib/fileFromEnum');
var enumsFromTable = require('./lib/enumsFromTable');
var SqlConnectionPool = require('mssql').ConnectionPool;
function tableDefinition(def) {
return {
file: def.file,
// [STR] Relative path to the file
table: def.table,
// [STR] Table name from the database
keyColumn: def.keyColumn,
// [STR] Column to key enum off of
keyTransform: def.keyTransform || stringify,
// [FN(enumKeys)] FN to transform keys
tableFilter: def.tableFilter || alwaysTrue,
// [FN(tableRows)] FN to filter table rows
valueColumns: def.valueColumns,
// [Array[STR]] Creates one enum per value column
valueTransform: def.valueTransform || respectType
// [FN(enumValues)] FN to transform values
};
function alwaysTrue() { return true; }
function stringify(v) {
return "'" + v + "'";
}
function respectType(v) {
if(typeof v === 'string') return "'" + v + "'";
return v;
}
}
function writeConstantsDirectory(
sqlParams,
directoryPath,
tableDefinitions,
done
) {
const definitionWrapper = tableDefinitions.map(function(d){
return { def: d, table: [], enums: [] };
});
let sqlConnPool = null;
const connectToSql = (done) => {
let firstTime = !sqlConnPool;
if (!firstTime) sqlConnPool.removeListener('error', connectToSql);
sqlConnPool = new SqlConnectionPool(
sqlParams,
(sqlConnErr) => {
if (sqlConnErr) setTimeout(connectToSql, 5000);
else sqlConnPool.on('error', connectToSql);
if (firstTime) done();
}
);
};
connectToSql(() => {
async.series([
mapFn(readTable(sqlConnPool), definitionWrapper),
mapFn(enumsFromTable(), definitionWrapper),
mapFn(fileFromEnum(directoryPath, tableDefinitions), definitionWrapper)
], (err) => {
if (err) throw err;
makeIndexFile(directoryPath, done);
});
});
};
function mapFn(op, list) {
return function mappedFn (cb) {
async.map(list, op, cb);
};
}
module.exports = {
tableDefinition: tableDefinition,
createConstantFiles: writeConstantsDirectory
};