diff --git a/README.md b/README.md index beaa8e70..3035d02c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,32 @@ +# Modification + +Added extra logic for select (left join and right outer join). + + { + "where": { + "foo": "bar" + }, + "leftJoin": { + "relationName": { + "relatedModelPropName": "value" + } + } + } + +SQL generated will look somthing like this: + + SELECT + Model."id", + Model."foo" + FROM + "public"."model" AS Model + LEFT JOIN + "public"."relatedModel" AS RelatedModel ON RelatedModel."dependsOfRelationTypeAndForeignKey" = Model."dependsOfRelationTypeAndForeignKey" + WHERE + Model."foo" = "bar" AND RelatedModel."relatedModelPropName" = "value" + GROUP BY + Model."id" + # loopback-connector-postgresql [PostgreSQL](https://www.postgresql.org/), is a popular open-source object-relational database. diff --git a/lib/postgresql.js b/lib/postgresql.js index 6e186392..cd189f74 100644 --- a/lib/postgresql.js +++ b/lib/postgresql.js @@ -475,7 +475,7 @@ PostgreSQL.prototype.columnEscaped = function(model, property) { return idx == 0 ? next : idx < arr.length - 1 ? prev + '->' + next : prev + '->>' + next; }); } else { - return this.escapeName(this.column(model, property)); + return model + "." + this.escapeName(this.column(model, property)); } }; @@ -508,7 +508,7 @@ PostgreSQL.prototype.escapeValue = function(value) { PostgreSQL.prototype.tableEscaped = function(model) { const schema = this.schema(model) || 'public'; return this.escapeName(schema) + '.' + - this.escapeName(this.table(model)); + this.escapeName(this.table(model)) + " AS " + model; }; function buildLimit(limit, offset) { @@ -731,6 +731,15 @@ PostgreSQL.prototype._buildWhere = function(model, where) { } } else if (operator === 'regexp' && expression instanceof RegExp) { // do not coerce RegExp based on property definitions + columnValue = expression; + } else if (operator === 'regexp' && !(expression instanceof RegExp)) { + if (expression.startsWith('/')) { + var lastSlash = expression.lastIndexOf("/"); + expression = new RegExp(expression.slice(1, lastSlash), expression.slice(lastSlash + 1)); + } else { + expression = new RegExp(expression); + } + columnValue = expression; } else { columnValue = this.toColumnValue(p, expression, true); @@ -789,6 +798,9 @@ PostgreSQL.prototype.toColumnValue = function(prop, val, isWhereClause) { return null; } } + if(typeof val == "object" && val instanceof String) { + val += ""; + } if (prop.type === String) { return String(val); } diff --git a/package.json b/package.json index 3eded6af..b24a9ec2 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "bluebird": "^3.4.6", "chalk": "^4.0.0", "debug": "^4.1.1", - "loopback-connector": "^6.0.0", + "loopback-connector": "https://github.com/mo-solutions/loopback-connector.git", "pg": "^8.0.2", "strong-globalize": "^6.0.0", "uuid": "^10.0.0"