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

fix: fix query builder #2773

Merged
merged 6 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions apps/nuxt3-ssr/components/ResourceCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ const iconStarClasses = computed(() => {
</dd>
</div>
</dl> -->
<template
v-if="!resource.acronym && !resource.name && !resource.description"
>
<div>{{ resource }}</div>
<hr class="m-3" />
<div>{{ resourceId }}</div>
</template>
</div>
</article>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ watch(filters, () => {

// build resource query for cards

const fields = buildRecordListQueryFields(resourceType, schemaName, schemas);
const fields = buildRecordListQueryFields(
tableMetaData.value.name,
schemaName,
schemas
);

const query = computed(() => {
return `
Expand Down Expand Up @@ -105,7 +109,12 @@ const { data, pending, error, refresh } = await useFetch(
);

function buildRecordId(record: any) {
return extractKeyFromRecord(record, resourceType, schemaName, schemas);
return extractKeyFromRecord(
record,
tableMetaData.value.name,
schemaName,
schemas
);
}
</script>
<template>
Expand Down
65 changes: 38 additions & 27 deletions apps/nuxt3-ssr/utils/tableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,17 @@ export const buildRecordDetailsQueryFields = (
};

export const buildRecordListQueryFields = (
tableId: string,
tableName: string,
schemaName: string,
schemas: Record<string, ISchemaMetaData>
) => {
const keyFields = buildKeyFields(tableId, schemaName, schemas);

const tableMetaData = schemas[schemaName].tables.find(
(t: ITableMetaData) =>
t.id.toLocaleLowerCase() === tableId.toLocaleLowerCase()
);
const keyFields = buildKeyFields(tableName, schemaName, schemas);
const tableMetaData = getTableMetaData(schemas[schemaName], tableName);

if (tableMetaData === undefined) {
throw new Error(
"buildRecordListQueryFields; tableMetaData is undefined for tableId " +
tableId +
"buildRecordListQueryFields; tableMetaData is undefined for tableName " +
tableName +
" in schema " +
schemaName
);
Expand All @@ -122,29 +118,30 @@ export const buildRecordListQueryFields = (

const queryFields = [...new Set([...keyFields, ...additionalFields])];

const fieldsString = queryFields.reduce((acc: string, field: any) => {
const fieldsString = fieldsToQueryString(queryFields);

return fieldsString;
};

const fieldsToQueryString = (fields: string[][]): string => {
return fields.reduce((acc: string, field: any) => {
if (Array.isArray(field)) {
return (acc += " { " + field + " } ");
return (acc += " { " + fieldsToQueryString(field) + " } ");
} else {
return (acc += " " + field);
}
}, "");

return fieldsString;
};

const buildKeyFields = (
tableId: string,
tableName: string,
schemaName: string,
schemas: Record<string, ISchemaMetaData>
) => {
const schemaMetaData = schemas[schemaName];
const tableMetaData = schemaMetaData.tables.find(
(t: ITableMetaData) =>
t.id.toLocaleLowerCase() === tableId.toLocaleLowerCase()
);
const tableMetaData = getTableMetaData(schemaMetaData, tableName);

const keyFields = tableMetaData?.columns.reduce(
const keyFields = tableMetaData.columns.reduce(
(acc: any, column: IColumn) => {
if (column.key === 1) {
if (isValueType(column)) {
Expand All @@ -155,7 +152,7 @@ const buildKeyFields = (
"refTable is undefined for refColumn with id " +
column.id +
" in table " +
tableId +
tableName +
""
);
} else {
Expand Down Expand Up @@ -206,17 +203,14 @@ export const extractExternalSchemas = (schemaMetaData: ISchemaMetaData) => {

export const extractKeyFromRecord = (
record: any,
tableId: string,
tableName: string,
schemaId: string,
schemas: Record<string, ISchemaMetaData>
) => {
const schemaMetaData = schemas[schemaId];
const tableMetaData = schemaMetaData.tables.find(
(t: ITableMetaData) =>
t.id.toLocaleLowerCase() === tableId.toLocaleLowerCase()
);
const tableMetaData = getTableMetaData(schemaMetaData, tableName);

const key = tableMetaData?.columns.reduce((acc: any, column: IColumn) => {
const key = tableMetaData.columns.reduce((acc: any, column: IColumn) => {
if (column.key === 1 && record[column.id]) {
if (isValueType(column)) {
acc[column.id] = record[column.id];
Expand All @@ -226,7 +220,7 @@ export const extractKeyFromRecord = (
"refTable is undefined for refColumn with id " +
column.id +
" in table " +
tableId +
tableName +
""
);
} else {
Expand Down Expand Up @@ -265,3 +259,20 @@ export const buildFilterFromKeysObject = (keys: Record<string, string>) => {
{}
);
};

export const getTableMetaData = (
schemaMetaData: ISchemaMetaData,
tableName: string
): ITableMetaData => {
const tableMetaData = schemaMetaData.tables.find(
(t: ITableMetaData) =>
t.name.toLocaleLowerCase() === tableName.toLocaleLowerCase()
);

if (tableMetaData === undefined) {
const msg = "ERROR: tableMetaData is undefined for tableName " + tableName;
console.log(msg);
throw new Error(msg);
}
return tableMetaData;
};