This repository was archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathutils.js
181 lines (145 loc) · 4.07 KB
/
utils.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Helper functions
*/
var _ = require('lodash');
// Module Exports
var utils = module.exports = {};
/**
* Safe hasOwnProperty
*/
utils.object = {};
/**
* Safer helper for hasOwnProperty checks
*
* @param {Object} obj
* @param {String} prop
* @return {Boolean}
* @api public
*/
var hop = Object.prototype.hasOwnProperty;
utils.object.hasOwnProperty = function(obj, prop) {
return hop.call(obj, prop);
};
/**
* Escape Name
*
* Wraps a name in quotes to allow reserved
* words as table or column names such as user.
*
*
* NOTE: do not use this method to escape strings in a general-purpose way.
* This is intended only to escape schema object (e.g. table and column) names.
* Check out utils.escapeString() for other purposes. No harm in taking a
* peek at https://dev.mysql.com/doc/refman/5.7/en/identifiers.html .
*/
utils.escapeName = function escapeName(name, escapeCharacter, schemaName) {
var regex = new RegExp(escapeCharacter, 'g');
var replacementString = '' + escapeCharacter + escapeCharacter;
var replacementDot = '\.';
if (schemaName && schemaName[name]) {
return utils.escapeName(schemaName[name], escapeCharacter) + '.' +
utils.escapeName(name, escapeCharacter);
}
return '' + escapeCharacter + name.replace(regex, replacementString).replace(/\./g, replacementDot) + escapeCharacter;
};
/**
* Populate alias. Create the alias for an association
*
* @param {string} alias
*
* @returns {string}
*/
utils.populationAlias = function (alias) {
return '__' + alias;
};
/**
* Map Attributes
*
* Takes a js object and creates arrays used for parameterized
* queries in postgres.
*/
utils.mapAttributes = function(data, options) {
var keys = [], // Column Names
values = [], // Column Values
params = [], // Param Index, ex: $1, $2
i = 1;
// Flag whether to use parameterized queries or not
var parameterized = options && utils.object.hasOwnProperty(options, 'parameterized') ? options.parameterized : true;
// Get the escape character
var escapeCharacter = options && utils.object.hasOwnProperty(options, 'escapeCharacter') ? options.escapeCharacter : '"';
// Determine if we should escape the inserted characters
var escapeInserts = options && utils.object.hasOwnProperty(options, 'escapeInserts') ? options.escapeInserts : false;
_.each(_.keys(data), function(key) {
var k = escapeInserts ? (options.escapeCharacter + key + options.escapeCharacter) : key;
keys.push(k);
var value = utils.prepareValue(data[key]);
values.push(value);
if(parameterized) {
params.push('$' + i);
}
else {
if(value === null || value === undefined) {
params.push('null');
}
else {
params.push(value);
}
}
i++;
});
return({ keys: keys, values: values, params: params });
};
/**
* Prepare values
*
* Transform a JS date to SQL date and functions
* to strings.
*/
utils.prepareValue = function(value) {
// Cast dates to SQL
if (_.isDate(value)) {
value = utils.toSqlDate(value);
}
// Cast functions to strings
if (_.isFunction(value)) {
value = value.toString();
}
// Store Arrays as strings
if (_.isArray(value)) {
value = JSON.stringify(value);
}
// Store Buffers as hex strings (for BYTEA)
if (Buffer.isBuffer(value)) {
value = '\\x' + value.toString('hex');
}
return value;
};
/**
* Escape Strings
*/
utils.escapeString = function(value, forLike) {
if(!_.isString(value)) return value;
value = value.replace(/[_%\0\n\r\b\t\\\'\"\x1a]/g, function(s) {
switch(s) {
case "\0": return "\\0";
case "\n": return "\\n";
case "\r": return "\\r";
case "\b": return "\\b";
case "\t": return "\\t";
case "\x1a": return "\\Z";
case "%": return forLike ? "\\%" : "%";
case "_": return forLike ? "\\_" : "_";
default: return "\\"+s;
}
});
return value;
};
/**
* JS Date to UTC Timestamp
*
* Dates should be stored in Postgres with UTC timestamps
* and then converted to local time on the client.
*/
utils.toSqlDate = function(date) {
return date.toUTCString();
};