Comparing version 0.0.6 to 0.0.7
38
index.js
@@ -37,3 +37,16 @@ /** | ||
/** | ||
* For fields that need a null instead of "" are converted here; usually for the references | ||
*/ | ||
convertBlankToNull: function( data, fieldArr ){ | ||
for ( let field of fieldArr ){ | ||
field = field.trim(); | ||
if ( _.has(data, field) && data[field] == "" ){ | ||
data[field] = null; | ||
} | ||
} | ||
return this; | ||
}, | ||
/** ------------------------------------------------------------------------------------------ | ||
@@ -239,2 +252,5 @@ * Sanitize strings so they are clean | ||
console.log( sql ); | ||
console.log( vals ); | ||
// Execute the function | ||
@@ -262,11 +278,23 @@ this.lastResult = await dbConn.query(sql, vals); | ||
if (fieldDef.type == "text") { | ||
data[fieldData] = data[fieldData].trim(); | ||
if ( !fieldDef.allowNull && data[fieldData] == null ) | ||
throw new Error("[-] Field=" + fieldData + "; value was null; not permitted" ); | ||
data[fieldData] = ( data[fieldData] == null ) ? null : data[fieldData].trim(); | ||
} else if (fieldDef.type == "varchar") { | ||
if (!_.isString(data[fieldData])) { | ||
if ( !fieldDef.allowNull && data[fieldData] == null ) | ||
throw new Error("[-] Field=" + fieldData + "; value was null; not permitted" ); | ||
if (data[fieldData] != null && !_.isString(data[fieldData])) { | ||
data[fieldData] = data[fieldData] + ""; | ||
} | ||
data[fieldData] = data[fieldData].trim(); | ||
if (data[fieldData].length > fieldDef.len) { | ||
data[fieldData] = ( data[fieldData] == null ) ? null : data[fieldData].trim(); | ||
if ( data[fieldData] != null && data[fieldData].length > fieldDef.len) { | ||
throw new Error("[-] Field=" + fieldData + "; longer than " + fieldDef.len); | ||
} | ||
} else if (fieldDef.type == "enum") { | ||
@@ -419,2 +447,4 @@ if (_.indexOf(fieldDef.values, data[fieldData]) == -1) { | ||
field.allowNull = ( row["Null"] == "YES" ); | ||
if (field.type.indexOf("(") > 0) { | ||
@@ -421,0 +451,0 @@ field.type = field.type.substring(0, field.type.indexOf("(")); |
{ | ||
"name": "mg-dbop", | ||
"private" : false, | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"homepage": "https://github.com/MacLaurinGroup/dbOp", | ||
@@ -6,0 +6,0 @@ "description": "Query builder, and validator, for MySQL and node that utilizes the underlying table to provide a level of checking", |
@@ -129,2 +129,3 @@ ## dbOp | ||
* .checkForMissingEmptyFields(data,fieldArray) // throw an error if any of the fields are missing or empty | ||
* .convertBlankToNull(data,fieldArray) // converts any blank fields to pure null | ||
@@ -247,2 +248,5 @@ This method returns the last SQL result from an INSERT/UPDATE | ||
* 2019-03-10 | ||
* Allow null to be set for varchar/text fields | ||
* convertBlankToNull() helper method added | ||
* 2019-03-02 | ||
@@ -249,0 +253,0 @@ * Added LEFT JOIN to the sqlBuilder |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42861
666
262