From d0787d295b347bcc3fa29b034ea8012e2e98d157 Mon Sep 17 00:00:00 2001 From: James Chang Date: Fri, 5 Mar 2021 23:06:13 -0500 Subject: [PATCH] Resolve backend pivot issue in typeDef helper. Frontend updates --- .../functions/src/schema/helpers/typeDef.ts | 30 +++++++++++-------- .../src/schema/models/user/typeDef.ts | 2 +- backend/functions/src/schema/scalars/index.ts | 1 - .../src/schema/scalars/jsonAsString.ts | 16 ---------- .../interface/crud/crudRecordInterface.vue | 2 ++ .../interface/crud/editRecordInterface.vue | 2 ++ frontend/mixins/crud.js | 7 +++-- frontend/mixins/editRecordInterface.js | 9 ++++++ frontend/models/personalBest.ts | 9 ++++++ frontend/types/index.ts | 2 ++ 10 files changed, 48 insertions(+), 32 deletions(-) delete mode 100644 backend/functions/src/schema/scalars/jsonAsString.ts diff --git a/backend/functions/src/schema/helpers/typeDef.ts b/backend/functions/src/schema/helpers/typeDef.ts index 5614630..972d520 100644 --- a/backend/functions/src/schema/helpers/typeDef.ts +++ b/backend/functions/src/schema/helpers/typeDef.ts @@ -583,6 +583,13 @@ export function generatePaginatorPivotResolverObject(params: { ? currentService.typename.toLowerCase() : null; + // if filterByField, ensure that filterByField is a valid filterField on pivotService + if (filterByField && !pivotService.filterFieldsMap[filterByField]) { + throw new JomqlInitializationError({ + message: `Filter Key '${filterByField}' does not exist on type '${pivotService.typename}'`, + }); + } + const sortByScalarDefinition: ScalarDefinition = { name: pivotService.typename + "SortByKey", types: Object.keys(pivotService.sortFieldsMap).map((ele) => `"${ele}"`), @@ -756,21 +763,20 @@ export function generatePaginatorPivotResolverObject(params: { // parentValue.id should be requested (via requiredSqlFields) const parentItemId = parentValue.id; + // apply the filterByField as an arg to each filterObject + const filterObjectArray = validatedArgs.filterBy ?? [{}]; + filterObjectArray.forEach((filterObject) => { + filterObject[filterByField] = { eq: parentItemId }; + }); + console.log(filterObjectArray); + return pivotService.paginator.getRecord({ req, fieldPath, - args: deepAssign( - { ...validatedArgs }, - { - filterBy: { - [filterByField]: [ - { - value: parentItemId, - }, - ], - }, - } - ), + args: { + ...validatedArgs, + filterBy: filterObjectArray, + }, query, data, }); diff --git a/backend/functions/src/schema/models/user/typeDef.ts b/backend/functions/src/schema/models/user/typeDef.ts index a0c79fa..9b660a2 100644 --- a/backend/functions/src/schema/models/user/typeDef.ts +++ b/backend/functions/src/schema/models/user/typeDef.ts @@ -1,6 +1,6 @@ // import * as bcrypt from "bcryptjs"; -import { JomqlObjectType, lookupSymbol, ObjectTypeDefinition } from "jomql"; +import { JomqlObjectType, ObjectTypeDefinition } from "jomql"; import { User } from "../../services"; import { generateIdField, diff --git a/backend/functions/src/schema/scalars/index.ts b/backend/functions/src/schema/scalars/index.ts index 8326c4a..7bd7f03 100644 --- a/backend/functions/src/schema/scalars/index.ts +++ b/backend/functions/src/schema/scalars/index.ts @@ -17,7 +17,6 @@ export { number } from "./number"; // replacing the built-in number type to auto export { imageUrl } from "./imageUrl"; export { unixTimestamp } from "./unixTimestamp"; export { date } from "./date"; -export { jsonAsString } from "./jsonAsString"; export { id } from "./id"; export { regex } from "./regex"; diff --git a/backend/functions/src/schema/scalars/jsonAsString.ts b/backend/functions/src/schema/scalars/jsonAsString.ts deleted file mode 100644 index 6d42ee7..0000000 --- a/backend/functions/src/schema/scalars/jsonAsString.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { JomqlScalarType } from "jomql"; - -export const jsonAsString = new JomqlScalarType({ - name: "jsonAsString", - types: ["unknown"], - description: "Valid generic JSON that is stored in database as string", - serialize(value) { - if (typeof value !== "string") throw true; - - return JSON.parse(value); - }, - - parseValue(value) { - return JSON.stringify(value); - }, -}); diff --git a/frontend/components/interface/crud/crudRecordInterface.vue b/frontend/components/interface/crud/crudRecordInterface.vue index d087a8e..2c542b3 100644 --- a/frontend/components/interface/crud/crudRecordInterface.vue +++ b/frontend/components/interface/crud/crudRecordInterface.vue @@ -142,6 +142,7 @@ :prepend-icon="item.fieldInfo.icon" clearable filled + return-object class="py-0" @change="filterChanged = true" > @@ -162,6 +163,7 @@ filled hide-no-data cache-items + return-object class="py-0" @update:search-input="handleSearchUpdate(item)" @blur="item.focused = false" diff --git a/frontend/components/interface/crud/editRecordInterface.vue b/frontend/components/interface/crud/editRecordInterface.vue index 465ab21..4a55f4f 100644 --- a/frontend/components/interface/crud/editRecordInterface.vue +++ b/frontend/components/interface/crud/editRecordInterface.vue @@ -132,6 +132,7 @@ :readonly="item.readonly || mode === 'view'" :clearable="!item.readonly && mode !== 'view'" filled + return-object class="py-0" > >{ 'created_by.id': { text: 'Created By', inputType: 'server-autocomplete', + lookupFilters: (_that) => { + return [ + { + is_public: { + eq: true, + }, + }, + ] + }, typename: 'user', parseQueryValue: (val) => Number(val), }, diff --git a/frontend/types/index.ts b/frontend/types/index.ts index 1c1eb12..10b6e7b 100644 --- a/frontend/types/index.ts +++ b/frontend/types/index.ts @@ -36,6 +36,8 @@ export type RecordInfo = { inputRules?: any[] getOptions?: (that) => Promise typename?: string + // filters that should be applied when looking up results (server-X input type) + lookupFilters?: (that) => any[] // is the field hidden? if yes, won't fetch it for edit fields hidden?: boolean