getting subtable schema

This commit is contained in:
Anish Roy
2023-01-07 13:53:45 +00:00
parent 355a8b2b06
commit c726e7829b
4 changed files with 45 additions and 8 deletions

View File

@@ -105,7 +105,7 @@ export const deleteTableAtom = atom<
/** Stores a function to get a tables schema doc (without listener) */
export const getTableSchemaAtom = atom<
((id: string) => Promise<TableSchema>) | undefined
((id: string, withSubtables?: boolean) => Promise<TableSchema>) | undefined
>(undefined);
/** Roles used in the project based on table settings */

View File

@@ -141,7 +141,7 @@ export const tableSettingsDialogSchemaAtom = atom(async (get) => {
const tableId = get(tableSettingsDialogIdAtom);
const getTableSchema = get(getTableSchemaAtom);
if (!tableId || !getTableSchema) return {} as TableSchema;
return getTableSchema(tableId);
return getTableSchema(tableId, true);
});
/** Open the Get Started checklist from anywhere */

View File

@@ -1,7 +1,14 @@
import { useEffect, useCallback } from "react";
import { useAtom, useSetAtom } from "jotai";
import { useAtomCallback } from "jotai/utils";
import { doc, getDoc, setDoc, deleteDoc } from "firebase/firestore";
import {
doc,
getDoc,
setDoc,
deleteDoc,
collection,
getDocs,
} from "firebase/firestore";
import { camelCase, find, findIndex, isEmpty } from "lodash-es";
import {
@@ -23,7 +30,7 @@ import {
TABLE_GROUP_SCHEMAS,
} from "@src/config/dbPaths";
import { rowyUser } from "@src/utils/table";
import { TableSettings, TableSchema } from "@src/types/table";
import { TableSettings, TableSchema, SubTablesSchema } from "@src/types/table";
import { FieldType } from "@src/constants/fields";
import { getFieldProp } from "@src/components/fields";
@@ -220,7 +227,7 @@ export function useTableFunctions() {
// Set the getTableSchema function
const setGetTableSchema = useSetAtom(getTableSchemaAtom, projectScope);
useEffect(() => {
setGetTableSchema(() => async (id: string) => {
setGetTableSchema(() => async (id: string, withSubtables?: boolean) => {
// Get latest tables
const tables = (await readTables()) || [];
const table = find(tables, ["id", id]);
@@ -232,9 +239,34 @@ export function useTableFunctions() {
: TABLE_SCHEMAS,
id
);
return getDoc(tableSchemaDocRef).then(
(doc) => (doc.data() || {}) as TableSchema
);
let tableSchema: TableSchema | Promise<TableSchema> = getDoc(
tableSchemaDocRef
).then((doc) => (doc.data() || {}) as TableSchema);
if (withSubtables) {
let subTables: SubTablesSchema | Promise<SubTablesSchema> = getDocs(
collection(
firebaseDb,
`${
table?.tableType === "collectionGroup"
? TABLE_GROUP_SCHEMAS
: TABLE_SCHEMAS
}/${id}/subTables`
)
).then((querySnapshot) => {
let subTables: SubTablesSchema = {};
querySnapshot.forEach((doc) => {
subTables[doc.id] = doc.data();
});
return subTables;
});
[tableSchema, subTables] = await Promise.all([tableSchema, subTables]);
tableSchema.subTables = subTables;
}
return tableSchema as TableSchema;
});
}, [firebaseDb, readTables, setGetTableSchema]);
}

View File

@@ -110,10 +110,15 @@ export type TableSchema = {
webhooks?: IWebhook[];
runtimeOptions?: IRuntimeOptions;
subTables?: SubTablesSchema;
/** @deprecated Migrate to Extensions */
sparks?: string;
};
export type SubTablesSchema = {
[key: string]: TablesSchema;
};
export type ColumnConfig = {
/** Unique key for this column. Currently set to the same as fieldName */
key: string;