From edb6ec3bc51d7c2f20bf2b4daaed98c315c55ec0 Mon Sep 17 00:00:00 2001 From: James Chang Date: Wed, 19 May 2021 12:36:00 -0400 Subject: [PATCH] Update giraffeql boilerplate (backend) --- backend/functions/src/index.ts | 2 ++ .../functions/src/schema/core/helpers/sql.ts | 32 +++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/backend/functions/src/index.ts b/backend/functions/src/index.ts index 91cdc73..adc42cd 100644 --- a/backend/functions/src/index.ts +++ b/backend/functions/src/index.ts @@ -1,5 +1,7 @@ import * as functions from "firebase-functions"; import * as express from "express"; +import * as admin from "firebase-admin"; +admin.initializeApp(); import { initializeGiraffeql } from "giraffeql"; import "./schema"; diff --git a/backend/functions/src/schema/core/helpers/sql.ts b/backend/functions/src/schema/core/helpers/sql.ts index a7b768a..0c91a26 100644 --- a/backend/functions/src/schema/core/helpers/sql.ts +++ b/backend/functions/src/schema/core/helpers/sql.ts @@ -353,22 +353,34 @@ function applyWhere( break; case "in": if (Array.isArray(whereSubObject.value)) { - whereSubstatement += ` IN (${whereSubObject.value.map(() => "?")})`; - whereSubObject.value.forEach((ele) => { - bindings.push(ele); - }); + // if array is empty, is equivalent of FALSE + if (whereSubObject.value.length < 1) { + whereSubstatement = "FALSE"; + } else { + whereSubstatement += ` IN (${whereSubObject.value.map( + () => "?" + )})`; + whereSubObject.value.forEach((ele) => { + bindings.push(ele); + }); + } } else { throw new Error("Must provide array for in/nin operators"); } break; case "nin": if (Array.isArray(whereSubObject.value)) { - whereSubstatement += ` NOT IN (${whereSubObject.value.map( - () => "?" - )})`; - whereSubObject.value.forEach((ele) => { - bindings.push(ele); - }); + // if array is empty, is equivalent of TRUE + if (whereSubObject.value.length < 1) { + whereSubstatement = "TRUE"; + } else { + whereSubstatement += ` NOT IN (${whereSubObject.value.map( + () => "?" + )})`; + whereSubObject.value.forEach((ele) => { + bindings.push(ele); + }); + } } else { throw new Error("Must provide array for in/nin operators"); }