diff --git a/packages/builder/src/components/backend/DataTable/DataTable.svelte b/packages/builder/src/components/backend/DataTable/DataTable.svelte index 1b0c92bde0..4ed41c5b34 100644 --- a/packages/builder/src/components/backend/DataTable/DataTable.svelte +++ b/packages/builder/src/components/backend/DataTable/DataTable.svelte @@ -1,5 +1,5 @@
@@ -37,7 +46,7 @@ allowDeleteRows={!isUsersTable} schemaOverrides={isUsersTable ? userSchemaOverrides : null} showAvatars={false} - on:updatetable={e => tables.replaceTable(id, e.detail)} + on:updatetable={handleGridTableUpdate} > {#if isInternal} diff --git a/packages/builder/src/components/backend/TableNavigator/modals/CreateTableModal.svelte b/packages/builder/src/components/backend/TableNavigator/modals/CreateTableModal.svelte index bd1761620d..bfca91afaa 100644 --- a/packages/builder/src/components/backend/TableNavigator/modals/CreateTableModal.svelte +++ b/packages/builder/src/components/backend/TableNavigator/modals/CreateTableModal.svelte @@ -93,6 +93,7 @@ try { await beforeSave() table = await tables.save(newTable) + await datasources.fetch() await afterSave(table) } catch (e) { notifications.error(e) diff --git a/packages/builder/src/components/backend/TableNavigator/popovers/EditTablePopover.svelte b/packages/builder/src/components/backend/TableNavigator/popovers/EditTablePopover.svelte index 01c62d56f7..11ef60480b 100644 --- a/packages/builder/src/components/backend/TableNavigator/popovers/EditTablePopover.svelte +++ b/packages/builder/src/components/backend/TableNavigator/popovers/EditTablePopover.svelte @@ -65,6 +65,7 @@ const updatedTable = cloneDeep(table) updatedTable.name = updatedName await tables.save(updatedTable) + await datasources.fetch() notifications.success("Table renamed successfully") } diff --git a/packages/frontend-core/src/components/grid/stores/columns.js b/packages/frontend-core/src/components/grid/stores/columns.js index 024fdc29bc..82a26d923c 100644 --- a/packages/frontend-core/src/components/grid/stores/columns.js +++ b/packages/frontend-core/src/components/grid/stores/columns.js @@ -90,12 +90,12 @@ export const deriveStores = context => { // Update local state table.set(newTable) + // Update server + await API.saveTable(newTable) + // Broadcast change to external state can be updated, as this change // will not be received by the builder websocket because we caused it ourselves dispatch("updatetable", newTable) - - // Update server - await API.saveTable(newTable) } return { diff --git a/packages/server/src/api/controllers/table/external.ts b/packages/server/src/api/controllers/table/external.ts index f35691cb62..24d4242478 100644 --- a/packages/server/src/api/controllers/table/external.ts +++ b/packages/server/src/api/controllers/table/external.ts @@ -322,9 +322,7 @@ export async function save(ctx: UserCtx) { // Since tables are stored inside datasources, we need to notify clients // that the datasource definition changed const updatedDatasource = await db.get(datasource._id) - builderSocket?.emitDatasourceUpdate(ctx, updatedDatasource, { - includeOriginator: true, - }) + builderSocket?.emitDatasourceUpdate(ctx, updatedDatasource) return tableToSave } @@ -352,6 +350,11 @@ export async function destroy(ctx: UserCtx) { await db.put(datasource) + // Since tables are stored inside datasources, we need to notify clients + // that the datasource definition changed + const updatedDatasource = await db.get(datasource._id) + builderSocket?.emitDatasourceUpdate(ctx, updatedDatasource) + return tableToDelete } diff --git a/packages/server/src/websockets/builder.ts b/packages/server/src/websockets/builder.ts index 53b0a10905..0f2c43e5ab 100644 --- a/packages/server/src/websockets/builder.ts +++ b/packages/server/src/websockets/builder.ts @@ -3,13 +3,7 @@ import { BaseSocket } from "./websocket" import { permissions, events } from "@budibase/backend-core" import http from "http" import Koa from "koa" -import { - Datasource, - Table, - SocketSession, - ContextUser, - SocketMessageOptions, -} from "@budibase/types" +import { Datasource, Table, SocketSession, ContextUser } from "@budibase/types" import { gridSocket } from "./index" import { clearLock, updateLock } from "../utilities/redis" import { Socket } from "socket.io" @@ -72,61 +66,33 @@ export default class BuilderSocket extends BaseSocket { } } - emitTableUpdate(ctx: any, table: Table, options?: SocketMessageOptions) { - this.emitToRoom( - ctx, - ctx.appId, - BuilderSocketEvent.TableChange, - { - id: table._id, - table, - }, - options - ) - gridSocket?.emitTableUpdate(ctx, table, options) + emitTableUpdate(ctx: any, table: Table) { + this.emitToRoom(ctx, ctx.appId, BuilderSocketEvent.TableChange, { + id: table._id, + table, + }) + gridSocket?.emitTableUpdate(ctx, table) } - emitTableDeletion(ctx: any, id: string, options?: SocketMessageOptions) { - this.emitToRoom( - ctx, - ctx.appId, - BuilderSocketEvent.TableChange, - { - id, - table: null, - }, - options - ) - gridSocket?.emitTableDeletion(ctx, id, options) + emitTableDeletion(ctx: any, id: string) { + this.emitToRoom(ctx, ctx.appId, BuilderSocketEvent.TableChange, { + id, + table: null, + }) + gridSocket?.emitTableDeletion(ctx, id) } - emitDatasourceUpdate( - ctx: any, - datasource: Datasource, - options?: SocketMessageOptions - ) { - this.emitToRoom( - ctx, - ctx.appId, - BuilderSocketEvent.DatasourceChange, - { - id: datasource._id, - datasource, - }, - options - ) + emitDatasourceUpdate(ctx: any, datasource: Datasource) { + this.emitToRoom(ctx, ctx.appId, BuilderSocketEvent.DatasourceChange, { + id: datasource._id, + datasource, + }) } - emitDatasourceDeletion(ctx: any, id: string, options?: SocketMessageOptions) { - this.emitToRoom( - ctx, - ctx.appId, - BuilderSocketEvent.DatasourceChange, - { - id, - datasource: null, - }, - options - ) + emitDatasourceDeletion(ctx: any, id: string) { + this.emitToRoom(ctx, ctx.appId, BuilderSocketEvent.DatasourceChange, { + id, + datasource: null, + }) } } diff --git a/packages/server/src/websockets/grid.ts b/packages/server/src/websockets/grid.ts index 38ec091816..6731c2d899 100644 --- a/packages/server/src/websockets/grid.ts +++ b/packages/server/src/websockets/grid.ts @@ -4,7 +4,7 @@ import { permissions } from "@budibase/backend-core" import http from "http" import Koa from "koa" import { getTableId } from "../api/controllers/row/utils" -import { Row, SocketMessageOptions, Table } from "@budibase/types" +import { Row, Table } from "@budibase/types" import { Socket } from "socket.io" import { GridSocketEvent } from "@budibase/shared-core" @@ -29,51 +29,27 @@ export default class GridSocket extends BaseSocket { }) } - emitRowUpdate(ctx: any, row: Row, options?: SocketMessageOptions) { + emitRowUpdate(ctx: any, row: Row) { const tableId = getTableId(ctx) - this.emitToRoom( - ctx, - tableId, - GridSocketEvent.RowChange, - { - id: row._id, - row, - }, - options - ) + this.emitToRoom(ctx, tableId, GridSocketEvent.RowChange, { + id: row._id, + row, + }) } - emitRowDeletion(ctx: any, id: string, options?: SocketMessageOptions) { + emitRowDeletion(ctx: any, id: string) { const tableId = getTableId(ctx) - this.emitToRoom( - ctx, - tableId, - GridSocketEvent.RowChange, - { id, row: null }, - options - ) + this.emitToRoom(ctx, tableId, GridSocketEvent.RowChange, { id, row: null }) } - emitTableUpdate(ctx: any, table: Table, options?: SocketMessageOptions) { - this.emitToRoom( - ctx, - table._id!, - GridSocketEvent.TableChange, - { - id: table._id, - table, - }, - options - ) + emitTableUpdate(ctx: any, table: Table) { + this.emitToRoom(ctx, table._id!, GridSocketEvent.TableChange, { + id: table._id, + table, + }) } - emitTableDeletion(ctx: any, id: string, options?: SocketMessageOptions) { - this.emitToRoom( - ctx, - id, - GridSocketEvent.TableChange, - { id, table: null }, - options - ) + emitTableDeletion(ctx: any, id: string) { + this.emitToRoom(ctx, id, GridSocketEvent.TableChange, { id, table: null }) } } diff --git a/packages/server/src/websockets/websocket.ts b/packages/server/src/websockets/websocket.ts index 59a9d698d7..89fcc8869d 100644 --- a/packages/server/src/websockets/websocket.ts +++ b/packages/server/src/websockets/websocket.ts @@ -9,7 +9,7 @@ import { createAdapter } from "@socket.io/redis-adapter" import { Socket } from "socket.io" import { getSocketPubSubClients } from "../utilities/redis" import { SocketEvent, SocketSessionTTL } from "@budibase/shared-core" -import { SocketSession, SocketMessageOptions } from "@budibase/types" +import { SocketSession } from "@budibase/types" export class BaseSocket { io: Server @@ -277,23 +277,10 @@ export class BaseSocket { } // Emit an event to everyone in a room - emitToRoom( - ctx: any, - room: string, - event: string, - payload: any, - options?: SocketMessageOptions - ) { - // By default, we include the session API of the originator so that they can ignore - // this event. If we want to include the originator then we leave it unset to that all - // clients will react to it. - let apiSessionId = null - if (!options?.includeOriginator) { - apiSessionId = ctx.headers?.[Header.SESSION_ID] - } + emitToRoom(ctx: any, room: string, event: string, payload: any) { this.io.in(room).emit(event, { ...payload, - apiSessionId, + apiSessionId: ctx.headers?.[Header.SESSION_ID], }) } } diff --git a/packages/types/src/sdk/websocket.ts b/packages/types/src/sdk/websocket.ts index 770f44d9b2..40e2654e82 100644 --- a/packages/types/src/sdk/websocket.ts +++ b/packages/types/src/sdk/websocket.ts @@ -7,7 +7,3 @@ export interface SocketSession { room?: string connectedAt: number } - -export interface SocketMessageOptions { - includeOriginator?: boolean -}