This repository has been archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroboSchema1.js
151 lines (114 loc) · 3.26 KB
/
roboSchema1.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var fs = require('fs');
var csv2json = require('csvtojson');
var converter = require('json-2-csv');
var rimraf = require("rimraf");
var pluralize = require('pluralize');
//load the CSV
var bulkFields;
csv2json()
.fromFile('./input.csv')
.then((jsonObj)=>{
bulkFields = jsonObj;
transform();
})
function transform() {
var transformed = [];
for (var i=0; i<bulkFields.length; i++) {
var row = bulkFields[i];
var dataProps = getDatatype(row.data_type_ext);
var tableNameNoSpaces = row.table_name.replace(/ /g,'');
var tableNameSingular = pluralize.singular(tableNameNoSpaces);
var tableNamePlural = pluralize.plural(tableNameNoSpaces);
var newRow = {};
newRow.tableField = row.table_name + '.' + row.column_name;
newRow.table_name = row.table_name;
newRow.Object = tableNameSingular + '__c';
newRow['Object Label'] = camelToSpaces(tableNameSingular);
newRow['Object Plural'] = camelToSpaces(tableNamePlural);
newRow.column_name = row.column_name;
newRow['Field Name'] = (row.column_name.replace(/ /g,'')) + '__c';
newRow['Field Label'] = camelToSpaces(row.column_name);
newRow['Type'] = dataProps['Type'];
newRow['Length'] = dataProps['Length'];
newRow['Decimal Places'] = dataProps['Decimal Places'];
newRow['Reference To'] = '';
newRow['Relationship Name'] = '';
newRow['Relationship Label'] = '';
newRow['Required'] = '';
newRow['External Id'] = '';
newRow['Unique'] = '';
newRow['Track Field History'] = '';
transformed.push(newRow);
}
var json2csvCallback = function (err, csv) {
if (err) throw err;
fs.writeFileSync('./transformed.csv', csv);
};
converter.json2csv(transformed, json2csvCallback, {excelBOM: true});
}
//input will be something like int or nvarchar(15)
function getDatatype(dataType) {
dataType = dataType.replace(')', '');
var arrDT = dataType.split('(');
var dt = arrDT[0];
var dtLen = (arrDT.length == 2 ? arrDT[1] : null);
var dataProps = {
'Type' : '',
'Length' : '',
'Decimal Places' : ''
};
var dtMap = {
'bit' : 'Checkbox',
'datetime' : 'DateTime',
'image' : 'Text',
'int' : 'Number',
'money' : 'Currency',
'nchar' : 'Text',
'ntext' : 'Text',
'nvarchar' : 'Text',
'real' : 'Number',
'smallint' : 'Number',
'sysname' : 'Text',
'varbinary' : 'Text'
};
//set the basic data type to a Salesforce type
var dType = '<unknown>';
if (dt in dtMap) {
dType = dtMap[dt];
}
dataProps['Type'] = dType;
//set the field length
if (dType == 'Text') {
if (dtLen == 'MAX') {
dataProps['Length'] = 255;
} else if (dtLen != null) {
dataProps['Length'] = dtLen;
} else {
dataProps['Length'] = 255;
}
}
if (dType == 'Currency') {
dataProps['Length'] = 16;
dataProps['Decimal Places'] = 2;
}
if (dt == 'int') {
dataProps['Length'] = 10;
dataProps['Decimal Places'] = 0;
}
if (dt == 'real') {
dataProps['Length'] = 11;
dataProps['Decimal Places'] = 7;
}
if (dt == 'smallint') {
dataProps['Length'] = 5;
dataProps['Decimal Places'] = 0;
}
return dataProps;
}
function camelToSpaces(input) {
function getCamelCaseArray(camel) {
var reg = /([a-z0-9])([A-Z])/g;
return camel.replace(/([a-z])([A-Z])/g, '$1 $2');
}
return getCamelCaseArray(input);
}