这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/atoms/projectScope/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const deleteTableAtom = atom<

/** Stores a function to get a table’s 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 */
Expand Down
2 changes: 1 addition & 1 deletion src/atoms/projectScope/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
109 changes: 101 additions & 8 deletions src/sources/ProjectSourceFirebase/useTableFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
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,
writeBatch,
} from "firebase/firestore";
import { camelCase, find, findIndex, isEmpty } from "lodash-es";

import {
Expand All @@ -23,7 +31,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";

Expand Down Expand Up @@ -104,6 +112,32 @@ export function useTableFunctions() {
{ merge: true }
);

// adding subtables
const batch = writeBatch(firebaseDb);

if (_schema?.subTables) {
const subTableCollectionRef = (id: string) =>
doc(
firebaseDb,
settings.tableType !== "collectionGroup"
? TABLE_SCHEMAS
: TABLE_GROUP_SCHEMAS,
settings.id,
"subTables",
id
);
Object.keys(_schema.subTables).forEach((subTableId: string) => {
if (_schema.subTables) {
batch.set(
subTableCollectionRef(subTableId),
_schema.subTables[subTableId]
);
}
});

delete _schema.subTables;
}

// Creates schema doc with columns
const { functionConfigPath, functionBuilderRef, ...schemaToWrite } =
_schema ?? {};
Expand All @@ -121,7 +155,11 @@ export function useTableFunctions() {
);

// Wait for both to complete
await Promise.all([promiseUpdateSettings, promiseAddSchema]);
await Promise.all([
promiseUpdateSettings,
promiseAddSchema,
batch.commit(),
]);
}
);
}, [currentUser, firebaseDb, readTables, setCreateTable]);
Expand Down Expand Up @@ -167,6 +205,32 @@ export function useTableFunctions() {
{ merge: true }
);

// adding subtables
const batch = writeBatch(firebaseDb);

if (_schema?.subTables) {
const subTableCollectionRef = (id: string) =>
doc(
firebaseDb,
settings.tableType !== "collectionGroup"
? TABLE_SCHEMAS
: TABLE_GROUP_SCHEMAS,
settings.id,
"subTables",
id
);
Object.keys(_schema.subTables).forEach((subTableId: string) => {
if (_schema.subTables) {
batch.set(
subTableCollectionRef(subTableId),
_schema.subTables[subTableId]
);
}
});

delete _schema.subTables;
}

// Updates schema doc if param is provided
const { functionConfigPath, functionBuilderRef, ...schemaToWrite } =
_schema ?? {};
Expand All @@ -182,7 +246,11 @@ export function useTableFunctions() {
: await setDoc(tableSchemaDocRef, schemaToWrite, { merge: true });

// Wait for both to complete
await Promise.all([promiseUpdateSettings, promiseUpdateSchema]);
await Promise.all([
promiseUpdateSettings,
promiseUpdateSchema,
batch.commit(),
]);
}
);
}, [firebaseDb, readTables, setUpdateTable]);
Expand Down Expand Up @@ -220,7 +288,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]);
Expand All @@ -232,9 +300,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]);
}
5 changes: 5 additions & 0 deletions src/types/table.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,15 @@ export type TableSchema = {
webhooks?: IWebhook[];
runtimeOptions?: IRuntimeOptions;

subTables?: SubTablesSchema;
/** @deprecated Migrate to Extensions */
sparks?: string;
};

export type SubTablesSchema = {
[key: string]: TableSchema;
};

export type ColumnConfig = {
/** Unique key for this column. Currently set to the same as fieldName */
key: string;
Expand Down