From 23478cc8f344a0b4acb2cf82fd9cf64e453afac2 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 20 Dec 2024 14:06:42 +0100 Subject: [PATCH 1/7] Type datasource table --- .../stores/datasources/{table.js => table.ts} | 5 +++-- .../src/components/grid/stores/index.ts | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) rename packages/frontend-core/src/components/grid/stores/datasources/{table.js => table.ts} (94%) diff --git a/packages/frontend-core/src/components/grid/stores/datasources/table.js b/packages/frontend-core/src/components/grid/stores/datasources/table.ts similarity index 94% rename from packages/frontend-core/src/components/grid/stores/datasources/table.js rename to packages/frontend-core/src/components/grid/stores/datasources/table.ts index bb7bf0835e0..5bf28f67e27 100644 --- a/packages/frontend-core/src/components/grid/stores/datasources/table.js +++ b/packages/frontend-core/src/components/grid/stores/datasources/table.ts @@ -1,9 +1,10 @@ import { SortOrder } from "@budibase/types" import { get } from "svelte/store" +import { Store as StoreContext } from ".." const SuppressErrors = true -export const createActions = context => { +export const createActions = (context: StoreContext) => { const { API, datasource, columns } = context const saveDefinition = async newDefinition => { @@ -58,7 +59,7 @@ export const createActions = context => { } } -export const initialise = context => { +export const initialise = (context: StoreContext) => { const { datasource, fetch, diff --git a/packages/frontend-core/src/components/grid/stores/index.ts b/packages/frontend-core/src/components/grid/stores/index.ts index 763a33c398a..60f93b25fe3 100644 --- a/packages/frontend-core/src/components/grid/stores/index.ts +++ b/packages/frontend-core/src/components/grid/stores/index.ts @@ -50,14 +50,25 @@ const DependencyOrderedStores = [ Cache, ] -export interface BaseStore {} +export interface BaseStore { + API: any +} export type Store = BaseStore & Columns.Store & { // TODO while typing the rest of stores - datasource: any + datasource: Writable definition: Writable - enrichedSchema: any + enrichedSchema: Writable + fetch: Writable + filter: Writable + inlineFilters: Writable + allFilters: Writable + sort: Writable + table: Writable & { actions: any } + initialFilter: Writable + initialSortColumn: Writable + initialSortOrder: Writable } export const attachStores = (context: Store): Store => { From d101a8900397587106cac9e9f59ce1df2bd1722e Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 20 Dec 2024 14:09:48 +0100 Subject: [PATCH 2/7] Type createActions --- .../grid/stores/datasources/table.ts | 19 ++++++++++++------- .../src/components/grid/stores/index.ts | 3 ++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/frontend-core/src/components/grid/stores/datasources/table.ts b/packages/frontend-core/src/components/grid/stores/datasources/table.ts index 5bf28f67e27..dc485dc58eb 100644 --- a/packages/frontend-core/src/components/grid/stores/datasources/table.ts +++ b/packages/frontend-core/src/components/grid/stores/datasources/table.ts @@ -1,4 +1,9 @@ -import { SortOrder } from "@budibase/types" +import { + Row, + SaveRowRequest, + SaveTableRequest, + SortOrder, +} from "@budibase/types" import { get } from "svelte/store" import { Store as StoreContext } from ".." @@ -7,11 +12,11 @@ const SuppressErrors = true export const createActions = (context: StoreContext) => { const { API, datasource, columns } = context - const saveDefinition = async newDefinition => { + const saveDefinition = async (newDefinition: SaveTableRequest) => { await API.saveTable(newDefinition) } - const saveRow = async row => { + const saveRow = async (row: SaveRowRequest) => { row = { ...row, tableId: get(datasource)?.tableId, @@ -19,15 +24,15 @@ export const createActions = (context: StoreContext) => { return await API.saveRow(row, SuppressErrors) } - const deleteRows = async rows => { + const deleteRows = async (rows: (string | Row)[]) => { await API.deleteRows(get(datasource).tableId, rows) } - const isDatasourceValid = datasource => { + const isDatasourceValid = (datasource: { type: string; tableId: any }) => { return datasource?.type === "table" && datasource?.tableId } - const getRow = async id => { + const getRow = async (id: string) => { const res = await API.searchTable(get(datasource).tableId, { limit: 1, query: { @@ -40,7 +45,7 @@ export const createActions = (context: StoreContext) => { return res?.rows?.[0] } - const canUseColumn = name => { + const canUseColumn = (name: string) => { return get(columns).some(col => col.name === name) } diff --git a/packages/frontend-core/src/components/grid/stores/index.ts b/packages/frontend-core/src/components/grid/stores/index.ts index 60f93b25fe3..123a61383a0 100644 --- a/packages/frontend-core/src/components/grid/stores/index.ts +++ b/packages/frontend-core/src/components/grid/stores/index.ts @@ -1,4 +1,5 @@ import { Writable } from "svelte/store" +import type { APIClient } from "../../../api/types" import * as Bounds from "./bounds" import * as Columns from "./columns" @@ -51,7 +52,7 @@ const DependencyOrderedStores = [ ] export interface BaseStore { - API: any + API: APIClient } export type Store = BaseStore & From 02578e2f0d96eed03d47e4e249a7a2a40a3572d9 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 20 Dec 2024 14:38:23 +0100 Subject: [PATCH 3/7] Fix types --- .../grid/stores/datasources/table.ts | 23 ++++++++++++++++--- .../src/components/grid/stores/index.ts | 10 ++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/frontend-core/src/components/grid/stores/datasources/table.ts b/packages/frontend-core/src/components/grid/stores/datasources/table.ts index dc485dc58eb..8a2dea1a70f 100644 --- a/packages/frontend-core/src/components/grid/stores/datasources/table.ts +++ b/packages/frontend-core/src/components/grid/stores/datasources/table.ts @@ -1,6 +1,7 @@ import { Row, SaveRowRequest, + SaveRowResponse, SaveTableRequest, SortOrder, } from "@budibase/types" @@ -9,7 +10,23 @@ import { Store as StoreContext } from ".." const SuppressErrors = true -export const createActions = (context: StoreContext) => { +interface TableActions { + table: { + actions: { + saveDefinition: (newDefinition: SaveTableRequest) => Promise + addRow: (row: SaveRowRequest) => Promise + updateRow: (row: SaveRowRequest) => Promise + deleteRows: (rows: (string | Row)[]) => Promise + getRow: (id: string) => Promise + isDatasourceValid: (datasource: { type: string; tableId: any }) => boolean + canUseColumn: (name: string) => boolean + } + } +} + +export type Store = TableActions + +export const createActions = (context: StoreContext): TableActions => { const { API, datasource, columns } = context const saveDefinition = async (newDefinition: SaveTableRequest) => { @@ -29,7 +46,7 @@ export const createActions = (context: StoreContext) => { } const isDatasourceValid = (datasource: { type: string; tableId: any }) => { - return datasource?.type === "table" && datasource?.tableId + return datasource?.type === "table" && !!datasource?.tableId } const getRow = async (id: string) => { @@ -80,7 +97,7 @@ export const initialise = (context: StoreContext) => { // Keep a list of subscriptions so that we can clear them when the datasource // config changes - let unsubscribers = [] + let unsubscribers: any[] = [] // Observe datasource changes and apply logic for table datasources datasource.subscribe($datasource => { diff --git a/packages/frontend-core/src/components/grid/stores/index.ts b/packages/frontend-core/src/components/grid/stores/index.ts index 123a61383a0..677d8d3f830 100644 --- a/packages/frontend-core/src/components/grid/stores/index.ts +++ b/packages/frontend-core/src/components/grid/stores/index.ts @@ -45,7 +45,7 @@ const DependencyOrderedStores = [ Users, Menu, Pagination, - Config, + Config as any, Clipboard, Notifications, Cache, @@ -56,17 +56,17 @@ export interface BaseStore { } export type Store = BaseStore & - Columns.Store & { + Columns.Store & + Table.Store & { // TODO while typing the rest of stores - datasource: Writable + datasource: Writable & { actions: any } definition: Writable - enrichedSchema: Writable + enrichedSchema: any fetch: Writable filter: Writable inlineFilters: Writable allFilters: Writable sort: Writable - table: Writable & { actions: any } initialFilter: Writable initialSortColumn: Writable initialSortOrder: Writable From b0ef785e7af747274e196fc7939daad529169856 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 20 Dec 2024 14:49:15 +0100 Subject: [PATCH 4/7] Type viewV2 --- .../grid/stores/datasources/table.ts | 2 +- .../datasources/{viewV2.js => viewV2.ts} | 50 +++++++++++++++---- .../src/components/grid/stores/index.ts | 6 ++- 3 files changed, 46 insertions(+), 12 deletions(-) rename packages/frontend-core/src/components/grid/stores/datasources/{viewV2.js => viewV2.ts} (81%) diff --git a/packages/frontend-core/src/components/grid/stores/datasources/table.ts b/packages/frontend-core/src/components/grid/stores/datasources/table.ts index 8a2dea1a70f..2e947c586ee 100644 --- a/packages/frontend-core/src/components/grid/stores/datasources/table.ts +++ b/packages/frontend-core/src/components/grid/stores/datasources/table.ts @@ -49,7 +49,7 @@ export const createActions = (context: StoreContext): TableActions => { return datasource?.type === "table" && !!datasource?.tableId } - const getRow = async (id: string) => { + const getRow = async (id: any) => { const res = await API.searchTable(get(datasource).tableId, { limit: 1, query: { diff --git a/packages/frontend-core/src/components/grid/stores/datasources/viewV2.js b/packages/frontend-core/src/components/grid/stores/datasources/viewV2.ts similarity index 81% rename from packages/frontend-core/src/components/grid/stores/datasources/viewV2.js rename to packages/frontend-core/src/components/grid/stores/datasources/viewV2.ts index b9f4851a604..a24dff1f232 100644 --- a/packages/frontend-core/src/components/grid/stores/datasources/viewV2.js +++ b/packages/frontend-core/src/components/grid/stores/datasources/viewV2.ts @@ -1,16 +1,42 @@ import { get } from "svelte/store" -import { SortOrder } from "@budibase/types" +import { + Row, + SaveRowRequest, + SortOrder, + UpdateViewRequest, +} from "@budibase/types" +import { Store as StoreContext } from ".." const SuppressErrors = true -export const createActions = context => { +interface ViewActions { + viewV2: { + actions: { + saveDefinition: (newDefinition: UpdateViewRequest) => Promise + addRow: (row: SaveRowRequest) => Promise + updateRow: (row: SaveRowRequest) => Promise + deleteRows: (rows: (string | Row)[]) => Promise + getRow: (id: string) => Promise + isDatasourceValid: (datasource: { + type: string + id: string + tableId: string + }) => boolean + canUseColumn: (name: string) => boolean + } + } +} + +export type Store = ViewActions + +export const createActions = (context: StoreContext): ViewActions => { const { API, datasource, columns } = context - const saveDefinition = async newDefinition => { + const saveDefinition = async (newDefinition: UpdateViewRequest) => { await API.viewV2.update(newDefinition) } - const saveRow = async row => { + const saveRow = async (row: SaveRowRequest) => { const $datasource = get(datasource) row = { ...row, @@ -23,11 +49,11 @@ export const createActions = context => { } } - const deleteRows = async rows => { + const deleteRows = async (rows: (string | Row)[]) => { await API.deleteRows(get(datasource).id, rows) } - const getRow = async id => { + const getRow = async (id: string) => { const res = await API.viewV2.fetch(get(datasource).id, { limit: 1, query: { @@ -40,13 +66,17 @@ export const createActions = context => { return res?.rows?.[0] } - const isDatasourceValid = datasource => { + const isDatasourceValid = (datasource: { + type: string + id: string + tableId: string + }) => { return ( - datasource?.type === "viewV2" && datasource?.id && datasource?.tableId + datasource?.type === "viewV2" && !!datasource?.id && !!datasource?.tableId ) } - const canUseColumn = name => { + const canUseColumn = (name: string) => { return get(columns).some(col => col.name === name && col.visible) } @@ -65,7 +95,7 @@ export const createActions = context => { } } -export const initialise = context => { +export const initialise = (context: StoreContext) => { const { definition, datasource, diff --git a/packages/frontend-core/src/components/grid/stores/index.ts b/packages/frontend-core/src/components/grid/stores/index.ts index 677d8d3f830..c8f26fa90a1 100644 --- a/packages/frontend-core/src/components/grid/stores/index.ts +++ b/packages/frontend-core/src/components/grid/stores/index.ts @@ -57,7 +57,8 @@ export interface BaseStore { export type Store = BaseStore & Columns.Store & - Table.Store & { + Table.Store & + ViewV2.Store & { // TODO while typing the rest of stores datasource: Writable & { actions: any } definition: Writable @@ -70,6 +71,9 @@ export type Store = BaseStore & initialFilter: Writable initialSortColumn: Writable initialSortOrder: Writable + rows: Writable & { actions: any } + subscribe: any + config: Writable } export const attachStores = (context: Store): Store => { From b45b409041500cce3a695f1732d65317f9ea1734 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 20 Dec 2024 14:49:59 +0100 Subject: [PATCH 5/7] Fix any --- .../src/components/grid/stores/datasources/viewV2.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend-core/src/components/grid/stores/datasources/viewV2.ts b/packages/frontend-core/src/components/grid/stores/datasources/viewV2.ts index a24dff1f232..304a25efb32 100644 --- a/packages/frontend-core/src/components/grid/stores/datasources/viewV2.ts +++ b/packages/frontend-core/src/components/grid/stores/datasources/viewV2.ts @@ -115,7 +115,7 @@ export const initialise = (context: StoreContext) => { // Keep a list of subscriptions so that we can clear them when the datasource // config changes - let unsubscribers = [] + let unsubscribers: any[] = [] // Observe datasource changes and apply logic for view V2 datasources datasource.subscribe($datasource => { From 7b130c7bcd4bb79731ab788181998d24fdb89ac6 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 20 Dec 2024 14:57:49 +0100 Subject: [PATCH 6/7] Type nonPlus --- .../datasources/{nonPlus.js => nonPlus.ts} | 37 ++++++++++++++++--- .../src/components/grid/stores/index.ts | 3 +- 2 files changed, 33 insertions(+), 7 deletions(-) rename packages/frontend-core/src/components/grid/stores/datasources/{nonPlus.js => nonPlus.ts} (76%) diff --git a/packages/frontend-core/src/components/grid/stores/datasources/nonPlus.js b/packages/frontend-core/src/components/grid/stores/datasources/nonPlus.ts similarity index 76% rename from packages/frontend-core/src/components/grid/stores/datasources/nonPlus.js rename to packages/frontend-core/src/components/grid/stores/datasources/nonPlus.ts index ea558d6236c..4796f5d0711 100644 --- a/packages/frontend-core/src/components/grid/stores/datasources/nonPlus.js +++ b/packages/frontend-core/src/components/grid/stores/datasources/nonPlus.ts @@ -1,7 +1,28 @@ import { SortOrder } from "@budibase/types" import { get } from "svelte/store" +import { Store as StoreContext } from ".." -export const createActions = context => { +interface NonPlusActions { + nonPlus: { + actions: { + saveDefinition: () => Promise + addRow: () => Promise + updateRow: () => Promise + deleteRows: () => Promise + getRow: () => Promise + isDatasourceValid: (datasource: { + type: string + id: string + tableId: string + }) => boolean + canUseColumn: (name: string) => boolean + } + } +} + +export type Store = NonPlusActions + +export const createActions = (context: StoreContext): NonPlusActions => { const { columns, table, viewV2 } = context const saveDefinition = async () => { @@ -20,7 +41,11 @@ export const createActions = context => { throw "This datasource does not support fetching individual rows" } - const isDatasourceValid = datasource => { + const isDatasourceValid = (datasource: { + type: string + id: string + tableId: string + }) => { // There are many different types and shapes of datasource, so we only // check that we aren't null return ( @@ -30,7 +55,7 @@ export const createActions = context => { ) } - const canUseColumn = name => { + const canUseColumn = (name: string) => { return get(columns).some(col => col.name === name) } @@ -50,11 +75,11 @@ export const createActions = context => { } // Small util to compare datasource definitions -const isSameDatasource = (a, b) => { +const isSameDatasource = (a: any, b: any) => { return JSON.stringify(a) === JSON.stringify(b) } -export const initialise = context => { +export const initialise = (context: StoreContext) => { const { datasource, sort, @@ -69,7 +94,7 @@ export const initialise = context => { } = context // Keep a list of subscriptions so that we can clear them when the datasource // config changes - let unsubscribers = [] + let unsubscribers: any[] = [] // Observe datasource changes and apply logic for view V2 datasources datasource.subscribe($datasource => { diff --git a/packages/frontend-core/src/components/grid/stores/index.ts b/packages/frontend-core/src/components/grid/stores/index.ts index c8f26fa90a1..1ef5da03b6b 100644 --- a/packages/frontend-core/src/components/grid/stores/index.ts +++ b/packages/frontend-core/src/components/grid/stores/index.ts @@ -58,7 +58,8 @@ export interface BaseStore { export type Store = BaseStore & Columns.Store & Table.Store & - ViewV2.Store & { + ViewV2.Store & + NonPlus.Store & { // TODO while typing the rest of stores datasource: Writable & { actions: any } definition: Writable From 8fc0930563f868ef569e1f08869022a3665c5546 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 20 Dec 2024 16:01:59 +0100 Subject: [PATCH 7/7] Move type to type/ui --- .../components/grid/stores/datasources/nonPlus.ts | 14 +++----------- .../components/grid/stores/datasources/table.ts | 5 +++-- .../components/grid/stores/datasources/viewV2.ts | 13 +++---------- packages/types/src/ui/stores/grid/datasource.ts | 5 +++++ packages/types/src/ui/stores/grid/index.ts | 1 + 5 files changed, 15 insertions(+), 23 deletions(-) create mode 100644 packages/types/src/ui/stores/grid/datasource.ts diff --git a/packages/frontend-core/src/components/grid/stores/datasources/nonPlus.ts b/packages/frontend-core/src/components/grid/stores/datasources/nonPlus.ts index 4796f5d0711..dcc4d47076b 100644 --- a/packages/frontend-core/src/components/grid/stores/datasources/nonPlus.ts +++ b/packages/frontend-core/src/components/grid/stores/datasources/nonPlus.ts @@ -1,4 +1,4 @@ -import { SortOrder } from "@budibase/types" +import { SortOrder, UIDatasource } from "@budibase/types" import { get } from "svelte/store" import { Store as StoreContext } from ".." @@ -10,11 +10,7 @@ interface NonPlusActions { updateRow: () => Promise deleteRows: () => Promise getRow: () => Promise - isDatasourceValid: (datasource: { - type: string - id: string - tableId: string - }) => boolean + isDatasourceValid: (datasource: UIDatasource) => boolean canUseColumn: (name: string) => boolean } } @@ -41,11 +37,7 @@ export const createActions = (context: StoreContext): NonPlusActions => { throw "This datasource does not support fetching individual rows" } - const isDatasourceValid = (datasource: { - type: string - id: string - tableId: string - }) => { + const isDatasourceValid = (datasource: UIDatasource) => { // There are many different types and shapes of datasource, so we only // check that we aren't null return ( diff --git a/packages/frontend-core/src/components/grid/stores/datasources/table.ts b/packages/frontend-core/src/components/grid/stores/datasources/table.ts index 2e947c586ee..e905c89e448 100644 --- a/packages/frontend-core/src/components/grid/stores/datasources/table.ts +++ b/packages/frontend-core/src/components/grid/stores/datasources/table.ts @@ -4,6 +4,7 @@ import { SaveRowResponse, SaveTableRequest, SortOrder, + UIDatasource, } from "@budibase/types" import { get } from "svelte/store" import { Store as StoreContext } from ".." @@ -18,7 +19,7 @@ interface TableActions { updateRow: (row: SaveRowRequest) => Promise deleteRows: (rows: (string | Row)[]) => Promise getRow: (id: string) => Promise - isDatasourceValid: (datasource: { type: string; tableId: any }) => boolean + isDatasourceValid: (datasource: UIDatasource) => boolean canUseColumn: (name: string) => boolean } } @@ -45,7 +46,7 @@ export const createActions = (context: StoreContext): TableActions => { await API.deleteRows(get(datasource).tableId, rows) } - const isDatasourceValid = (datasource: { type: string; tableId: any }) => { + const isDatasourceValid = (datasource: UIDatasource) => { return datasource?.type === "table" && !!datasource?.tableId } diff --git a/packages/frontend-core/src/components/grid/stores/datasources/viewV2.ts b/packages/frontend-core/src/components/grid/stores/datasources/viewV2.ts index 304a25efb32..677a85312fb 100644 --- a/packages/frontend-core/src/components/grid/stores/datasources/viewV2.ts +++ b/packages/frontend-core/src/components/grid/stores/datasources/viewV2.ts @@ -3,6 +3,7 @@ import { Row, SaveRowRequest, SortOrder, + UIDatasource, UpdateViewRequest, } from "@budibase/types" import { Store as StoreContext } from ".." @@ -17,11 +18,7 @@ interface ViewActions { updateRow: (row: SaveRowRequest) => Promise deleteRows: (rows: (string | Row)[]) => Promise getRow: (id: string) => Promise - isDatasourceValid: (datasource: { - type: string - id: string - tableId: string - }) => boolean + isDatasourceValid: (datasource: UIDatasource) => boolean canUseColumn: (name: string) => boolean } } @@ -66,11 +63,7 @@ export const createActions = (context: StoreContext): ViewActions => { return res?.rows?.[0] } - const isDatasourceValid = (datasource: { - type: string - id: string - tableId: string - }) => { + const isDatasourceValid = (datasource: UIDatasource) => { return ( datasource?.type === "viewV2" && !!datasource?.id && !!datasource?.tableId ) diff --git a/packages/types/src/ui/stores/grid/datasource.ts b/packages/types/src/ui/stores/grid/datasource.ts new file mode 100644 index 00000000000..d7367352d5c --- /dev/null +++ b/packages/types/src/ui/stores/grid/datasource.ts @@ -0,0 +1,5 @@ +export interface UIDatasource { + type: string + id: string + tableId: string +} diff --git a/packages/types/src/ui/stores/grid/index.ts b/packages/types/src/ui/stores/grid/index.ts index 81f23bae69f..f6c3472aaa1 100644 --- a/packages/types/src/ui/stores/grid/index.ts +++ b/packages/types/src/ui/stores/grid/index.ts @@ -1 +1,2 @@ export * from "./columns" +export * from "./datasource"