Skip to content

Commit

Permalink
Allow multiple use of orderBy and where Constraints (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
GogoVega authored Aug 17, 2024
2 parents 3fdaa7c + 87c6628 commit af61376
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 83 deletions.
12 changes: 6 additions & 6 deletions build/nodes/locales/en-US/firestore-get.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,25 @@ <h3>Details</h3>
<li><code>limitToFirst</code><span class="property-type">number</span></li>
<li><code>limitToLast</code><span class="property-type">number</span></li>
<li><code>offset</code><span class="property-type">number</span></li>
<li><code>orderBy</code><span class="property-type">object</span></li>
<li><code>orderBy</code><span class="property-type">array</span></li>
<li><code>select</code><span class="property-type">string | string array</span></li>
<li><code>startAfter</code><span class="property-type">various</span></li>
<li><code>startAt</code><span class="property-type">various</span></li>
<li><code>where</code><span class="property-type">object</span></li>
<li><code>where</code><span class="property-type">array</span></li>
</ul>
<p>The <code>msg.constraints</code> will look like this:</p>
<pre>
{
"limitToFirst": 5,
"orderBy": {
"orderBy": [{
"fieldPath": "some-path",
"direction": "asc"
},
"where": {
}],
"where": [{
"fieldPath": "some-path",
"filter": ">",
"value": 1
}
}]
}</pre>
<p>
Click <a href="https://github.com/GogoVega/node-red-contrib-cloud-firestore/wiki/firestore-get">here</a>
Expand Down
12 changes: 6 additions & 6 deletions build/nodes/locales/en-US/firestore-in.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,25 @@ <h3>Details</h3>
<li><code>limitToFirst</code><span class="property-type">number</span></li>
<li><code>limitToLast</code><span class="property-type">number</span></li>
<li><code>offset</code><span class="property-type">number</span></li>
<li><code>orderBy</code><span class="property-type">object</span></li>
<li><code>orderBy</code><span class="property-type">array</span></li>
<li><code>select</code><span class="property-type">string | string array</span></li>
<li><code>startAfter</code><span class="property-type">various</span></li>
<li><code>startAt</code><span class="property-type">various</span></li>
<li><code>where</code><span class="property-type">object</span></li>
<li><code>where</code><span class="property-type">array</span></li>
</ul>
<p>The <code>msg.constraints</code> will look like this:</p>
<pre>
{
"limitToFirst": 5,
"orderBy": {
"orderBy": [{
"fieldPath": "some-path",
"direction": "asc"
},
"where": {
}],
"where": [{
"fieldPath": "some-path",
"filter": ">",
"value": 1
}
}]
}</pre>
<p>
Click <a href="https://github.com/GogoVega/node-red-contrib-cloud-firestore/wiki/firestore-in">here</a>
Expand Down
12 changes: 6 additions & 6 deletions build/nodes/locales/fr/firestore-get.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,25 @@ <h3>Détails</h3>
<li><code>limitToFirst</code><span class="property-type">nombre</span></li>
<li><code>limitToLast</code><span class="property-type">nombre</span></li>
<li><code>offset</code><span class="property-type">nombre</span></li>
<li><code>orderBy</code><span class="property-type">objet</span></li>
<li><code>orderBy</code><span class="property-type">tableau</span></li>
<li><code>select</code><span class="property-type">chaîne | tableau de chaînes</span></li>
<li><code>startAfter</code><span class="property-type">divers</span></li>
<li><code>startAt</code><span class="property-type">divers</span></li>
<li><code>where</code><span class="property-type">objet</span></li>
<li><code>where</code><span class="property-type">tableau</span></li>
</ul>
<p>Le <code>msg.constraints</code> ressemblera à ceci :</p>
<pre>
{
"limitToFirst": 5,
"orderBy": {
"orderBy": [{
"fieldPath": "some-path",
"direction": "asc"
},
"where": {
}],
"where": [{
"fieldPath": "some-path",
"filter": ">",
"value": 1
}
}]
}</pre>
<p>
Cliquer <a href="https://github.com/GogoVega/node-red-contrib-cloud-firestore/wiki/firestore-get">ici</a>
Expand Down
12 changes: 6 additions & 6 deletions build/nodes/locales/fr/firestore-in.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,25 @@ <h3>Détails</h3>
<li><code>limitToFirst</code><span class="property-type">nombre</span></li>
<li><code>limitToLast</code><span class="property-type">nombre</span></li>
<li><code>offset</code><span class="property-type">nombre</span></li>
<li><code>orderBy</code><span class="property-type">objet</span></li>
<li><code>orderBy</code><span class="property-type">tableau</span></li>
<li><code>select</code><span class="property-type">chaîne | tableau de chaînes</span></li>
<li><code>startAfter</code><span class="property-type">divers</span></li>
<li><code>startAt</code><span class="property-type">divers</span></li>
<li><code>where</code><span class="property-type">objet</span></li>
<li><code>where</code><span class="property-type">tableau</span></li>
</ul>
<p>Le <code>msg.constraints</code> ressemblera à ceci :</p>
<pre>
{
"limitToFirst": 5,
"orderBy": {
"orderBy": [{
"fieldPath": "some-path",
"direction": "asc"
},
"where": {
}],
"where": [{
"fieldPath": "some-path",
"filter": ">",
"value": 1
}
}]
}</pre>
<p>
Cliquer <a href="https://github.com/GogoVega/node-red-contrib-cloud-firestore/wiki/firestore-in">ici</a>
Expand Down
62 changes: 31 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"version": ">=3"
},
"dependencies": {
"@gogovega/firebase-config-node": "^0.1.0"
"@gogovega/firebase-config-node": "^0.1.1"
},
"devDependencies": {
"@types/node-red": "^1.3.5",
Expand Down
19 changes: 16 additions & 3 deletions resources/constraints.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,18 @@ var FirestoreQueryConstraintsContainer = FirestoreQueryConstraintsContainer || (

#constraintsHandler() {
if (this.useConstraints?.prop("checked") === true) {
const constraints = Object.entries(this.node.constraints || {});
const constraints = Object.entries(this.node.constraints || {}).reduce((array, [type, value]) => {
// For `orderBy` or `where`
if (Array.isArray(value)) {
value.forEach((val) => {
array.push([type, val]);
});
} else {
array.push([type, value]);
}

return array;
}, []);

if (!constraints.length) constraints.push(["limitToLast", { value: "5", valueType: "num" }]);

Expand Down Expand Up @@ -163,7 +174,8 @@ var FirestoreQueryConstraintsContainer = FirestoreQueryConstraintsContainer || (
case "orderBy":
if (pathType === "str" && validators.path()(path) !== true) RED.notify("Query Constraints: Setted value is not a valid path!", "error");

node.constraints[constraintType] = { path: path, pathType: pathType, direction: options };
node.constraints[constraintType] ||= [];
node.constraints[constraintType].push({ path: path, pathType: pathType, direction: options });
break;
case "select": {
const result = isSelectValueValid(value, {});
Expand All @@ -178,7 +190,8 @@ var FirestoreQueryConstraintsContainer = FirestoreQueryConstraintsContainer || (
case "where":
if (pathType === "str" && validators.path()(path) !== true) RED.notify("Query Constraints: Setted value is not a valid path!", "error");

node.constraints[constraintType] = { path: path, pathType: pathType, value: value, valueType: valueType, filter: options };
node.constraints[constraintType] ||= [];
node.constraints[constraintType].push({ path: path, pathType: pathType, value: value, valueType: valueType, filter: options });
break;
}
});
Expand Down
66 changes: 44 additions & 22 deletions src/lib/firestore-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,28 @@ class Firestore<Node extends FirestoreNode, Config extends FirestoreConfig = Nod
break;
}
case "orderBy": {
const typesAllowed: Array<typeof value.pathType> = ["flow", "global", "jsonata", "env", "msg", "str"];
if (!typesAllowed.includes(value.pathType))
throw new Error(`Invalid type (${value.pathType}) for the ${key} field. Please reconfigure this node.`);
const typesAllowed = ["flow", "global", "jsonata", "env", "msg", "str"];
let valArray = value;

constraints[key] = {
fieldPath: await this.evaluateNodeProperty(value.path, value.pathType, this.node, msg),
direction: value.direction,
};
// Ensure it's an array - v < 0.0.2
if (!Array.isArray(value)) {
valArray = [value];
}

for (const [index, val] of valArray.entries()) {
if (!typesAllowed.includes(val.pathType))
throw new Error(`Invalid type (${val.pathType}) for the ${key} field. Please reconfigure this node.`);

constraints[key] ||= [];
constraints[key][index] = {
fieldPath: await this.evaluateNodeProperty(val.path, val.pathType, this.node, msg),
direction: val.direction,
};

if (typeof constraints[key][index].fieldPath !== "string")
throw new TypeError("The OrderBy fieldPath value of Query Constraints must be a string.");
}

if (typeof constraints[key].fieldPath !== "string")
throw new TypeError("The OrderBy fieldPath value of Query Constraints must be a string.");
break;
}
case "select": {
Expand All @@ -296,7 +307,7 @@ class Firestore<Node extends FirestoreNode, Config extends FirestoreConfig = Nod
break;
}
case "where": {
const valueTypesAllowed: Array<typeof value.valueType> = [
const valueTypesAllowed = [
"bool",
"date",
"env",
Expand All @@ -309,20 +320,31 @@ class Firestore<Node extends FirestoreNode, Config extends FirestoreConfig = Nod
"num",
"str",
];
const pathTypesAllowed: Array<typeof value.pathType> = ["flow", "global", "jsonata", "env", "msg", "str"];
if (!valueTypesAllowed.includes(value.valueType))
throw new Error(`Invalid type (${value.valueType}) for the ${key} field. Please reconfigure this node.`);
if (!pathTypesAllowed.includes(value.pathType))
throw new Error(`Invalid type (${value.pathType}) for the ${key} field. Please reconfigure this node.`);
const pathTypesAllowed = ["flow", "global", "jsonata", "env", "msg", "str"];
let valArray = value;

// Ensure it's an array - v < 0.0.2
if (!Array.isArray(value)) {
valArray = [value];
}

constraints[key] = {
fieldPath: await this.evaluateNodeProperty(value.path, value.pathType, this.node, msg),
filter: value.filter,
value: await this.evaluateNodeProperty(value.value, value.valueType, this.node, msg),
};
for (const [index, val] of valArray.entries()) {
if (!valueTypesAllowed.includes(val.valueType))
throw new Error(`Invalid type (${val.valueType}) for the ${key} field. Please reconfigure this node.`);
if (!pathTypesAllowed.includes(val.pathType))
throw new Error(`Invalid type (${val.pathType}) for the ${key} field. Please reconfigure this node.`);

constraints[key] ||= [];
constraints[key][index] = {
fieldPath: await this.evaluateNodeProperty(val.path, val.pathType, this.node, msg),
filter: val.filter,
value: await this.evaluateNodeProperty(val.value, val.valueType, this.node, msg),
};

if (typeof constraints[key][index].fieldPath !== "string")
throw new TypeError("The Where fieldPath value of Query Constraints must be a string.");
}

if (typeof constraints[key].fieldPath !== "string")
throw new TypeError("The Where fieldPath value of Query Constraints must be a string.");
break;
}
}
Expand Down
Loading

0 comments on commit af61376

Please sign in to comment.