Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support regex syntax #700

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
16 changes: 14 additions & 2 deletions lib/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down