From d9fa8de5aec88353d5df42769365c870f9f29c5d Mon Sep 17 00:00:00 2001 From: Andrew Kingston <andrew@kingston.dev> Date: Mon, 6 Jan 2025 09:46:44 +0000 Subject: [PATCH 001/131] Convert portal menu store to TS --- packages/builder/src/stores/portal/menu.js | 138 ------------------- packages/builder/src/stores/portal/menu.ts | 149 +++++++++++++++++++++ 2 files changed, 149 insertions(+), 138 deletions(-) delete mode 100644 packages/builder/src/stores/portal/menu.js create mode 100644 packages/builder/src/stores/portal/menu.ts diff --git a/packages/builder/src/stores/portal/menu.js b/packages/builder/src/stores/portal/menu.js deleted file mode 100644 index 75a9b363be..0000000000 --- a/packages/builder/src/stores/portal/menu.js +++ /dev/null @@ -1,138 +0,0 @@ -import { derived } from "svelte/store" -import { admin } from "./admin" -import { auth } from "./auth" -import { isEnabled } from "@/helpers/featureFlags" -import { sdk } from "@budibase/shared-core" -import { FeatureFlag } from "@budibase/types" - -export const menu = derived([admin, auth], ([$admin, $auth]) => { - const user = $auth?.user - const isAdmin = sdk.users.isAdmin(user) - const cloud = $admin?.cloud - // Determine user sub pages - let userSubPages = [ - { - title: "Users", - href: "/builder/portal/users/users", - }, - ] - userSubPages.push({ - title: "Groups", - href: "/builder/portal/users/groups", - }) - - // Pages that all devs and admins can access - let menu = [ - { - title: "Apps", - href: "/builder/portal/apps", - }, - ] - if (sdk.users.isGlobalBuilder(user)) { - menu.push({ - title: "Users", - href: "/builder/portal/users", - subPages: userSubPages, - }) - menu.push({ - title: "Plugins", - href: "/builder/portal/plugins", - }) - } - - // Add settings page for admins - if (isAdmin) { - let settingsSubPages = [ - { - title: "Auth", - href: "/builder/portal/settings/auth", - }, - { - title: "Email", - href: "/builder/portal/settings/email", - }, - { - title: "Organisation", - href: "/builder/portal/settings/organisation", - }, - { - title: "Branding", - href: "/builder/portal/settings/branding", - }, - { - title: "Environment", - href: "/builder/portal/settings/environment", - }, - ] - if (isEnabled(FeatureFlag.AI_CUSTOM_CONFIGS)) { - settingsSubPages.push({ - title: "AI", - href: "/builder/portal/settings/ai", - }) - } - - if (!cloud) { - settingsSubPages.push({ - title: "Version", - href: "/builder/portal/settings/version", - }) - settingsSubPages.push({ - title: "Diagnostics", - href: "/builder/portal/settings/diagnostics", - }) - } - menu.push({ - title: "Settings", - href: "/builder/portal/settings", - subPages: [...settingsSubPages].sort((a, b) => - a.title.localeCompare(b.title) - ), - }) - } - - // Add account page - let accountSubPages = [ - { - title: "Usage", - href: "/builder/portal/account/usage", - }, - ] - if (isAdmin) { - accountSubPages.push({ - title: "Audit Logs", - href: "/builder/portal/account/auditLogs", - }) - - if (!cloud) { - accountSubPages.push({ - title: "System Logs", - href: "/builder/portal/account/systemLogs", - }) - } - } - if (cloud && user?.accountPortalAccess) { - accountSubPages.push({ - title: "Upgrade", - href: $admin?.accountPortalUrl + "/portal/upgrade", - }) - } else if (!cloud && isAdmin) { - accountSubPages.push({ - title: "Upgrade", - href: "/builder/portal/account/upgrade", - }) - } - // add license check here - if (user?.accountPortalAccess && user.account.stripeCustomerId) { - accountSubPages.push({ - title: "Billing", - href: $admin?.accountPortalUrl + "/portal/billing", - }) - } - menu.push({ - title: "Account", - href: "/builder/portal/account", - subPages: accountSubPages, - }) - - return menu -}) diff --git a/packages/builder/src/stores/portal/menu.ts b/packages/builder/src/stores/portal/menu.ts new file mode 100644 index 0000000000..3b1ece9156 --- /dev/null +++ b/packages/builder/src/stores/portal/menu.ts @@ -0,0 +1,149 @@ +import { derived, Readable } from "svelte/store" +import { admin } from "./admin" +import { auth } from "./auth" +import { isEnabled } from "@/helpers/featureFlags" +import { sdk } from "@budibase/shared-core" +import { FeatureFlag } from "@budibase/types" + +interface MenuItem { + title: string + href: string + subPages?: MenuItem[] +} + +export const menu: Readable<MenuItem[]> = derived( + [admin, auth], + ([$admin, $auth]) => { + const user = $auth?.user + const isAdmin = user != null && sdk.users.isAdmin(user) + const isGlobalBuilder = user != null && sdk.users.isGlobalBuilder(user) + const cloud = $admin?.cloud + + // Determine user sub pages + let userSubPages: MenuItem[] = [ + { + title: "Users", + href: "/builder/portal/users/users", + }, + ] + userSubPages.push({ + title: "Groups", + href: "/builder/portal/users/groups", + }) + + // Pages that all devs and admins can access + let menu: MenuItem[] = [ + { + title: "Apps", + href: "/builder/portal/apps", + }, + ] + if (isGlobalBuilder) { + menu.push({ + title: "Users", + href: "/builder/portal/users", + subPages: userSubPages, + }) + menu.push({ + title: "Plugins", + href: "/builder/portal/plugins", + }) + } + + // Add settings page for admins + if (isAdmin) { + let settingsSubPages: MenuItem[] = [ + { + title: "Auth", + href: "/builder/portal/settings/auth", + }, + { + title: "Email", + href: "/builder/portal/settings/email", + }, + { + title: "Organisation", + href: "/builder/portal/settings/organisation", + }, + { + title: "Branding", + href: "/builder/portal/settings/branding", + }, + { + title: "Environment", + href: "/builder/portal/settings/environment", + }, + ] + if (isEnabled(FeatureFlag.AI_CUSTOM_CONFIGS)) { + settingsSubPages.push({ + title: "AI", + href: "/builder/portal/settings/ai", + }) + } + + if (!cloud) { + settingsSubPages.push({ + title: "Version", + href: "/builder/portal/settings/version", + }) + settingsSubPages.push({ + title: "Diagnostics", + href: "/builder/portal/settings/diagnostics", + }) + } + menu.push({ + title: "Settings", + href: "/builder/portal/settings", + subPages: [...settingsSubPages].sort((a, b) => + a.title.localeCompare(b.title) + ), + }) + } + + // Add account page + let accountSubPages: MenuItem[] = [ + { + title: "Usage", + href: "/builder/portal/account/usage", + }, + ] + if (isAdmin) { + accountSubPages.push({ + title: "Audit Logs", + href: "/builder/portal/account/auditLogs", + }) + + if (!cloud) { + accountSubPages.push({ + title: "System Logs", + href: "/builder/portal/account/systemLogs", + }) + } + } + if (cloud && user?.accountPortalAccess) { + accountSubPages.push({ + title: "Upgrade", + href: $admin?.accountPortalUrl + "/portal/upgrade", + }) + } else if (!cloud && isAdmin) { + accountSubPages.push({ + title: "Upgrade", + href: "/builder/portal/account/upgrade", + }) + } + // add license check here + if (user?.accountPortalAccess && user?.account?.stripeCustomerId) { + accountSubPages.push({ + title: "Billing", + href: $admin?.accountPortalUrl + "/portal/billing", + }) + } + menu.push({ + title: "Account", + href: "/builder/portal/account", + subPages: accountSubPages, + }) + + return menu + } +) From e08e1a7b0dcc7c201f2780da0be217d7dc35afd9 Mon Sep 17 00:00:00 2001 From: Andrew Kingston <andrew@kingston.dev> Date: Mon, 6 Jan 2025 11:10:06 +0000 Subject: [PATCH 002/131] Convert portal OIDC store to TS --- packages/builder/src/stores/portal/oidc.js | 31 ---------------------- packages/builder/src/stores/portal/oidc.ts | 25 +++++++++++++++++ 2 files changed, 25 insertions(+), 31 deletions(-) delete mode 100644 packages/builder/src/stores/portal/oidc.js create mode 100644 packages/builder/src/stores/portal/oidc.ts diff --git a/packages/builder/src/stores/portal/oidc.js b/packages/builder/src/stores/portal/oidc.js deleted file mode 100644 index 65d8eac04c..0000000000 --- a/packages/builder/src/stores/portal/oidc.js +++ /dev/null @@ -1,31 +0,0 @@ -import { writable, get } from "svelte/store" -import { API } from "@/api" -import { auth } from "@/stores/portal" - -const OIDC_CONFIG = { - logo: undefined, - name: undefined, - uuid: undefined, -} - -export function createOidcStore() { - const store = writable(OIDC_CONFIG) - const { set, subscribe } = store - return { - subscribe, - set, - init: async () => { - const tenantId = get(auth).tenantId - const config = await API.getOIDCConfig(tenantId) - if (Object.keys(config || {}).length) { - // Just use the first config for now. - // We will be support multiple logins buttons later on. - set(...config) - } else { - set(OIDC_CONFIG) - } - }, - } -} - -export const oidc = createOidcStore() diff --git a/packages/builder/src/stores/portal/oidc.ts b/packages/builder/src/stores/portal/oidc.ts new file mode 100644 index 0000000000..a914645135 --- /dev/null +++ b/packages/builder/src/stores/portal/oidc.ts @@ -0,0 +1,25 @@ +import { get } from "svelte/store" +import { API } from "@/api" +import { auth } from "@/stores/portal" +import { BudiStore } from "../BudiStore" +import { PublicOIDCConfig } from "@budibase/types" + +class OIDCStore extends BudiStore<PublicOIDCConfig> { + constructor() { + super({}) + } + + async init() { + const tenantId = get(auth).tenantId + const config = await API.getOIDCConfig(tenantId) + if (Object.keys(config || {}).length) { + // Just use the first config for now. + // We will be support multiple logins buttons later on. + this.set(config[0]) + } else { + this.set({}) + } + } +} + +export const oidc = new OIDCStore() From 5d999b8e3a6add4a42718a34af3ee4d911584498 Mon Sep 17 00:00:00 2001 From: Andrew Kingston <andrew@kingston.dev> Date: Mon, 6 Jan 2025 11:53:21 +0000 Subject: [PATCH 003/131] Convert portal organisation store to TS --- .../builder/src/stores/portal/organisation.js | 66 ----------------- .../builder/src/stores/portal/organisation.ts | 71 +++++++++++++++++++ packages/types/src/documents/global/config.ts | 3 +- 3 files changed, 72 insertions(+), 68 deletions(-) delete mode 100644 packages/builder/src/stores/portal/organisation.js create mode 100644 packages/builder/src/stores/portal/organisation.ts diff --git a/packages/builder/src/stores/portal/organisation.js b/packages/builder/src/stores/portal/organisation.js deleted file mode 100644 index 6d41620c9f..0000000000 --- a/packages/builder/src/stores/portal/organisation.js +++ /dev/null @@ -1,66 +0,0 @@ -import { writable, get } from "svelte/store" -import { API } from "@/api" -import { auth } from "@/stores/portal" -import _ from "lodash" - -const DEFAULT_CONFIG = { - platformUrl: "", - logoUrl: undefined, - faviconUrl: undefined, - emailBrandingEnabled: true, - testimonialsEnabled: true, - platformTitle: "Budibase", - loginHeading: undefined, - loginButton: undefined, - metaDescription: undefined, - metaImageUrl: undefined, - metaTitle: undefined, - docsUrl: undefined, - company: "Budibase", - oidc: undefined, - google: undefined, - googleDatasourceConfigured: undefined, - oidcCallbackUrl: "", - googleCallbackUrl: "", - isSSOEnforced: false, - loaded: false, -} - -export function createOrganisationStore() { - const store = writable(DEFAULT_CONFIG) - const { subscribe, set } = store - - async function init() { - const tenantId = get(auth).tenantId - const settingsConfigDoc = await API.getTenantConfig(tenantId) - set({ ...DEFAULT_CONFIG, ...settingsConfigDoc.config, loaded: true }) - } - - async function save(config) { - // Delete non-persisted fields - const storeConfig = _.cloneDeep(get(store)) - delete storeConfig.oidc - delete storeConfig.google - delete storeConfig.googleDatasourceConfigured - delete storeConfig.oidcCallbackUrl - delete storeConfig.googleCallbackUrl - - // delete internal store field - delete storeConfig.loaded - - await API.saveConfig({ - type: "settings", - config: { ...storeConfig, ...config }, - }) - await init() - } - - return { - subscribe, - set, - save, - init, - } -} - -export const organisation = createOrganisationStore() diff --git a/packages/builder/src/stores/portal/organisation.ts b/packages/builder/src/stores/portal/organisation.ts new file mode 100644 index 0000000000..219245807a --- /dev/null +++ b/packages/builder/src/stores/portal/organisation.ts @@ -0,0 +1,71 @@ +import { get } from "svelte/store" +import { API } from "@/api" +import { auth } from "@/stores/portal" +import { + ConfigType, + PublicSettingsInnerConfig, + SettingsBrandingConfig, + SettingsInnerConfig, +} from "@budibase/types" +import { BudiStore } from "../BudiStore" + +interface LocalOrganisationState { + loaded: boolean +} + +type SavedOrganisationState = SettingsInnerConfig & SettingsBrandingConfig +type OrganisationState = SavedOrganisationState & + PublicSettingsInnerConfig & + LocalOrganisationState + +const DEFAULT_STATE: OrganisationState = { + platformUrl: "", + emailBrandingEnabled: true, + testimonialsEnabled: true, + platformTitle: "Budibase", + company: "Budibase", + google: false, + googleDatasourceConfigured: false, + oidc: false, + oidcCallbackUrl: "", + googleCallbackUrl: "", + loaded: false, +} + +class OrganisationStore extends BudiStore<OrganisationState> { + constructor() { + super(DEFAULT_STATE) + } + + async init() { + const tenantId = get(auth).tenantId + const settingsConfigDoc = await API.getTenantConfig(tenantId) + this.set({ ...DEFAULT_STATE, ...settingsConfigDoc.config, loaded: true }) + } + + async save(changes: Partial<SavedOrganisationState>) { + // Strip non persisted fields + const { + oidc, + google, + googleDatasourceConfigured, + oidcCallbackUrl, + googleCallbackUrl, + loaded, + ...config + } = get(this.store) + + // Save new config + const newConfig: SavedOrganisationState = { + ...config, + ...changes, + } + await API.saveConfig({ + type: ConfigType.SETTINGS, + config: newConfig, + }) + await this.init() + } +} + +export const organisation = new OrganisationStore() diff --git a/packages/types/src/documents/global/config.ts b/packages/types/src/documents/global/config.ts index d51ca9d54d..1ad20e291f 100644 --- a/packages/types/src/documents/global/config.ts +++ b/packages/types/src/documents/global/config.ts @@ -26,13 +26,11 @@ export interface SMTPConfig extends Config<SMTPInnerConfig> {} export interface SettingsBrandingConfig { faviconUrl?: string faviconUrlEtag?: string - emailBrandingEnabled?: boolean testimonialsEnabled?: boolean platformTitle?: string loginHeading?: string loginButton?: string - metaDescription?: string metaImageUrl?: string metaTitle?: string @@ -42,6 +40,7 @@ export interface SettingsInnerConfig { platformUrl?: string company?: string logoUrl?: string // Populated on read + docsUrl?: string logoUrlEtag?: string uniqueTenantId?: string analyticsEnabled?: boolean From 52916f11a8129cd8dec61dcd73fc30c2f894f741 Mon Sep 17 00:00:00 2001 From: Andrew Kingston <andrew@kingston.dev> Date: Tue, 7 Jan 2025 14:06:03 +0000 Subject: [PATCH 004/131] Convert portal user store to TS --- .../_components/BuilderSidePanel.svelte | 7 +- .../src/stores/portal/{users.js => users.ts} | 144 ++++++++++-------- packages/frontend-core/src/api/user.ts | 2 +- packages/types/src/api/web/user.ts | 2 +- 4 files changed, 85 insertions(+), 70 deletions(-) rename packages/builder/src/stores/portal/{users.js => users.ts} (55%) diff --git a/packages/builder/src/pages/builder/app/[application]/_components/BuilderSidePanel.svelte b/packages/builder/src/pages/builder/app/[application]/_components/BuilderSidePanel.svelte index 37abd7f1eb..2260892913 100644 --- a/packages/builder/src/pages/builder/app/[application]/_components/BuilderSidePanel.svelte +++ b/packages/builder/src/pages/builder/app/[application]/_components/BuilderSidePanel.svelte @@ -442,13 +442,11 @@ const onUpdateUserInvite = async (invite, role) => { let updateBody = { - code: invite.code, apps: { ...invite.apps, [prodAppId]: role, }, } - if (role === Constants.Roles.CREATOR) { updateBody.builder = updateBody.builder || {} updateBody.builder.apps = [...(updateBody.builder.apps ?? []), prodAppId] @@ -456,7 +454,7 @@ } else if (role !== Constants.Roles.CREATOR && invite?.builder?.apps) { invite.builder.apps = [] } - await users.updateInvite(updateBody) + await users.updateInvite(invite.code, updateBody) await filterInvites(query) } @@ -470,8 +468,7 @@ let updated = { ...invite } delete updated.info.apps[prodAppId] - return await users.updateInvite({ - code: updated.code, + return await users.updateInvite(updated.code, { apps: updated.apps, }) } diff --git a/packages/builder/src/stores/portal/users.js b/packages/builder/src/stores/portal/users.ts similarity index 55% rename from packages/builder/src/stores/portal/users.js rename to packages/builder/src/stores/portal/users.ts index 99ead22317..7c0bec296e 100644 --- a/packages/builder/src/stores/portal/users.js +++ b/packages/builder/src/stores/portal/users.ts @@ -1,41 +1,68 @@ -import { writable } from "svelte/store" import { API } from "@/api" -import { update } from "lodash" import { licensing } from "." import { sdk } from "@budibase/shared-core" import { Constants } from "@budibase/frontend-core" +import { + DeleteInviteUsersRequest, + InviteUsersRequest, + SearchUsersRequest, + SearchUsersResponse, + UpdateInviteRequest, + User, +} from "@budibase/types" +import { BudiStore } from "../BudiStore" -export function createUsersStore() { - const { subscribe, set } = writable({}) +type UserState = SearchUsersResponse & SearchUsersRequest - // opts can contain page and search params - async function search(opts = {}) { +class UserStore extends BudiStore<UserState> { + constructor() { + super({ + data: [], + }) + + // Update quotas after any add or remove operation + this.create = this.refreshUsage(this.create) + this.save = this.refreshUsage(this.save) + this.delete = this.refreshUsage(this.delete) + this.bulkDelete = this.refreshUsage(this.bulkDelete) + } + + async search(opts: SearchUsersRequest = {}) { const paged = await API.searchUsers(opts) - set({ + this.set({ ...paged, ...opts, }) return paged } - async function get(userId) { + async get(userId: string) { try { return await API.getUser(userId) } catch (err) { return null } } - const fetch = async () => { + + async fetch() { return await API.getUsers() } - // One or more users. - async function onboard(payload) { + async onboard(payload: InviteUsersRequest) { return await API.onboardUsers(payload) } - async function invite(payload) { - const users = payload.map(user => { + async invite( + payload: { + admin?: boolean + builder?: boolean + creator?: boolean + email: string + apps?: any[] + groups?: any[] + }[] + ) { + const users: InviteUsersRequest = payload.map(user => { let builder = undefined if (user.admin || user.builder) { builder = { global: true } @@ -55,11 +82,16 @@ export function createUsersStore() { return API.inviteUsers(users) } - async function removeInvites(payload) { + async removeInvites(payload: DeleteInviteUsersRequest) { return API.removeUserInvites(payload) } - async function acceptInvite(inviteCode, password, firstName, lastName) { + async acceptInvite( + inviteCode: string, + password: string, + firstName: string, + lastName?: string + ) { return API.acceptInvite({ inviteCode, password, @@ -68,21 +100,25 @@ export function createUsersStore() { }) } - async function fetchInvite(inviteCode) { + async fetchInvite(inviteCode: string) { return API.getUserInvite(inviteCode) } - async function getInvites() { + async getInvites() { return API.getUserInvites() } - async function updateInvite(invite) { - return API.updateUserInvite(invite.code, invite) + async updateInvite(code: string, invite: UpdateInviteRequest) { + return API.updateUserInvite(code, invite) } - async function create(data) { - let mappedUsers = data.users.map(user => { - const body = { + async getUserCountByApp(appId: string) { + return await API.getUserCountByApp(appId) + } + + async create(data: any) { + let mappedUsers: Omit<User, "tenantId">[] = data.users.map((user: any) => { + const body: Omit<User, "tenantId"> = { email: user.email, password: user.password, roles: {}, @@ -113,41 +149,44 @@ export function createUsersStore() { const response = await API.createUsers(mappedUsers, data.groups) // re-search from first page - await search() + await this.search() return response } - async function del(id) { + async delete(id: string) { await API.deleteUser(id) - update(users => users.filter(user => user._id !== id)) } - async function getUserCountByApp(appId) { - return await API.getUserCountByApp(appId) - } - - async function bulkDelete(users) { + async bulkDelete( + users: Array<{ + userId: string + email: string + }> + ) { return API.deleteUsers(users) } - async function save(user) { + async save(user: User) { return await API.saveUser(user) } - async function addAppBuilder(userId, appId) { + async addAppBuilder(userId: string, appId: string) { return await API.addAppBuilder(userId, appId) } - async function removeAppBuilder(userId, appId) { + async removeAppBuilder(userId: string, appId: string) { return await API.removeAppBuilder(userId, appId) } - async function getAccountHolder() { + async getAccountHolder() { return await API.getAccountHolder() } - const getUserRole = user => { - if (user && user.email === user.tenantOwnerEmail) { + getUserRole(user?: User & { tenantOwnerEmail?: string }) { + if (!user) { + return Constants.BudibaseRoles.AppUser + } + if (user.email === user.tenantOwnerEmail) { return Constants.BudibaseRoles.Owner } else if (sdk.users.isAdmin(user)) { return Constants.BudibaseRoles.Admin @@ -160,37 +199,16 @@ export function createUsersStore() { } } - const refreshUsage = - fn => - async (...args) => { + foo = this.refreshUsage(this.create) + bar = this.refreshUsage(this.save) + + refreshUsage<T extends any[], U>(fn: (...args: T) => Promise<U>) { + return async function (...args: T) { const response = await fn(...args) await licensing.setQuotaUsage() return response } - - return { - subscribe, - search, - get, - getUserRole, - fetch, - invite, - onboard, - fetchInvite, - getInvites, - removeInvites, - updateInvite, - getUserCountByApp, - addAppBuilder, - removeAppBuilder, - // any operation that adds or deletes users - acceptInvite, - create: refreshUsage(create), - save: refreshUsage(save), - bulkDelete: refreshUsage(bulkDelete), - delete: refreshUsage(del), - getAccountHolder, } } -export const users = createUsersStore() +export const users = new UserStore() diff --git a/packages/frontend-core/src/api/user.ts b/packages/frontend-core/src/api/user.ts index 84ec68644d..7464b1ec4a 100644 --- a/packages/frontend-core/src/api/user.ts +++ b/packages/frontend-core/src/api/user.ts @@ -60,7 +60,7 @@ export interface UserEndpoints { getAccountHolder: () => Promise<LookupAccountHolderResponse> searchUsers: (data: SearchUsersRequest) => Promise<SearchUsersResponse> createUsers: ( - users: User[], + users: Omit<User, "tenantId">[], groups: any[] ) => Promise<BulkUserCreated | undefined> updateUserInvite: ( diff --git a/packages/types/src/api/web/user.ts b/packages/types/src/api/web/user.ts index a42449d550..8b0dfef34b 100644 --- a/packages/types/src/api/web/user.ts +++ b/packages/types/src/api/web/user.ts @@ -124,7 +124,7 @@ export interface AcceptUserInviteRequest { inviteCode: string password: string firstName: string - lastName: string + lastName?: string } export interface AcceptUserInviteResponse { From af83f1b2e15a0268efb7bcac7abcd784575c9208 Mon Sep 17 00:00:00 2001 From: Sam Rose <hello@samwho.dev> Date: Tue, 7 Jan 2025 17:18:36 +0000 Subject: [PATCH 005/131] Delete old feature flags. --- packages/builder/src/stores/portal/menu.js | 10 +++------- packages/pro | 2 +- .../src/api/controllers/row/staticFormula.ts | 17 ++++------------- packages/server/src/automations/actions.ts | 12 ++---------- packages/server/src/automations/steps/openai.ts | 11 +++-------- packages/types/src/sdk/featureFlag.ts | 8 -------- 6 files changed, 13 insertions(+), 47 deletions(-) diff --git a/packages/builder/src/stores/portal/menu.js b/packages/builder/src/stores/portal/menu.js index 75a9b363be..4f87a5bfde 100644 --- a/packages/builder/src/stores/portal/menu.js +++ b/packages/builder/src/stores/portal/menu.js @@ -1,9 +1,7 @@ import { derived } from "svelte/store" import { admin } from "./admin" import { auth } from "./auth" -import { isEnabled } from "@/helpers/featureFlags" import { sdk } from "@budibase/shared-core" -import { FeatureFlag } from "@budibase/types" export const menu = derived([admin, auth], ([$admin, $auth]) => { const user = $auth?.user @@ -63,13 +61,11 @@ export const menu = derived([admin, auth], ([$admin, $auth]) => { title: "Environment", href: "/builder/portal/settings/environment", }, - ] - if (isEnabled(FeatureFlag.AI_CUSTOM_CONFIGS)) { - settingsSubPages.push({ + { title: "AI", href: "/builder/portal/settings/ai", - }) - } + }, + ] if (!cloud) { settingsSubPages.push({ diff --git a/packages/pro b/packages/pro index 32d84f109d..23fdd50b7e 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit 32d84f109d4edc526145472a7446327312151442 +Subproject commit 23fdd50b7ef28cf320716ed2c46e15d63185daa7 diff --git a/packages/server/src/api/controllers/row/staticFormula.ts b/packages/server/src/api/controllers/row/staticFormula.ts index b81a164807..678e824b94 100644 --- a/packages/server/src/api/controllers/row/staticFormula.ts +++ b/packages/server/src/api/controllers/row/staticFormula.ts @@ -4,15 +4,8 @@ import { processAIColumns, processFormulas, } from "../../../utilities/rowProcessor" -import { context, features } from "@budibase/backend-core" -import { - Table, - Row, - FeatureFlag, - FormulaType, - FieldType, - ViewV2, -} from "@budibase/types" +import { context } from "@budibase/backend-core" +import { Table, Row, FormulaType, FieldType, ViewV2 } from "@budibase/types" import * as linkRows from "../../../db/linkedRows" import isEqual from "lodash/isEqual" import { cloneDeep, merge } from "lodash/fp" @@ -163,10 +156,8 @@ export async function finaliseRow( contextRows: [enrichedRow], }) const aiEnabled = - ((await features.isEnabled(FeatureFlag.BUDIBASE_AI)) && - (await pro.features.isBudibaseAIEnabled())) || - ((await features.isEnabled(FeatureFlag.AI_CUSTOM_CONFIGS)) && - (await pro.features.isAICustomConfigsEnabled())) + (await pro.features.isBudibaseAIEnabled()) || + (await pro.features.isAICustomConfigsEnabled()) if (aiEnabled) { row = await processAIColumns(table, row, { contextRows: [enrichedRow], diff --git a/packages/server/src/automations/actions.ts b/packages/server/src/automations/actions.ts index 1c201d1f64..537b6befc3 100644 --- a/packages/server/src/automations/actions.ts +++ b/packages/server/src/automations/actions.ts @@ -27,11 +27,9 @@ import { Hosting, ActionImplementation, AutomationStepDefinition, - FeatureFlag, } from "@budibase/types" import sdk from "../sdk" import { getAutomationPlugin } from "../utilities/fileSystem" -import { features } from "@budibase/backend-core" type ActionImplType = ActionImplementations< typeof env.SELF_HOSTED extends "true" ? Hosting.SELF : Hosting.CLOUD @@ -78,6 +76,7 @@ export const BUILTIN_ACTION_DEFINITIONS: Record< LOOP: loop.definition, COLLECT: collect.definition, TRIGGER_AUTOMATION_RUN: triggerAutomationRun.definition, + BRANCH: branch.definition, // these used to be lowercase step IDs, maintain for backwards compat discord: discord.definition, slack: slack.definition, @@ -105,14 +104,7 @@ if (env.SELF_HOSTED) { export async function getActionDefinitions(): Promise< Record<keyof typeof AutomationActionStepId, AutomationStepDefinition> > { - if (await features.isEnabled(FeatureFlag.AUTOMATION_BRANCHING)) { - BUILTIN_ACTION_DEFINITIONS["BRANCH"] = branch.definition - } - if ( - env.SELF_HOSTED || - (await features.isEnabled(FeatureFlag.BUDIBASE_AI)) || - (await features.isEnabled(FeatureFlag.AI_CUSTOM_CONFIGS)) - ) { + if (env.SELF_HOSTED) { BUILTIN_ACTION_DEFINITIONS["OPENAI"] = openai.definition } diff --git a/packages/server/src/automations/steps/openai.ts b/packages/server/src/automations/steps/openai.ts index 19595cc0d0..53e41ceb09 100644 --- a/packages/server/src/automations/steps/openai.ts +++ b/packages/server/src/automations/steps/openai.ts @@ -7,9 +7,8 @@ import { AutomationIOType, OpenAIStepInputs, OpenAIStepOutputs, - FeatureFlag, } from "@budibase/types" -import { env, features } from "@budibase/backend-core" +import { env } from "@budibase/backend-core" import * as automationUtils from "../automationUtils" import * as pro from "@budibase/pro" @@ -99,12 +98,8 @@ export async function run({ try { let response - const customConfigsEnabled = - (await features.isEnabled(FeatureFlag.AI_CUSTOM_CONFIGS)) && - (await pro.features.isAICustomConfigsEnabled()) - const budibaseAIEnabled = - (await features.isEnabled(FeatureFlag.BUDIBASE_AI)) && - (await pro.features.isBudibaseAIEnabled()) + const customConfigsEnabled = await pro.features.isAICustomConfigsEnabled() + const budibaseAIEnabled = await pro.features.isBudibaseAIEnabled() let llmWrapper if (budibaseAIEnabled || customConfigsEnabled) { diff --git a/packages/types/src/sdk/featureFlag.ts b/packages/types/src/sdk/featureFlag.ts index 725ae0feb1..7b61b70772 100644 --- a/packages/types/src/sdk/featureFlag.ts +++ b/packages/types/src/sdk/featureFlag.ts @@ -1,16 +1,8 @@ export enum FeatureFlag { - AUTOMATION_BRANCHING = "AUTOMATION_BRANCHING", - AI_CUSTOM_CONFIGS = "AI_CUSTOM_CONFIGS", - DEFAULT_VALUES = "DEFAULT_VALUES", - BUDIBASE_AI = "BUDIBASE_AI", USE_ZOD_VALIDATOR = "USE_ZOD_VALIDATOR", } export const FeatureFlagDefaults = { - [FeatureFlag.DEFAULT_VALUES]: true, - [FeatureFlag.AUTOMATION_BRANCHING]: true, - [FeatureFlag.AI_CUSTOM_CONFIGS]: true, - [FeatureFlag.BUDIBASE_AI]: true, [FeatureFlag.USE_ZOD_VALIDATOR]: false, } From 913cefaf175d0c112c4132c973ce726b7b165648 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 31 Dec 2024 13:24:26 +0100 Subject: [PATCH 006/131] Type index --- .../src/fetch/{index.js => index.ts} | 42 ++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) rename packages/frontend-core/src/fetch/{index.js => index.ts} (67%) diff --git a/packages/frontend-core/src/fetch/index.js b/packages/frontend-core/src/fetch/index.ts similarity index 67% rename from packages/frontend-core/src/fetch/index.js rename to packages/frontend-core/src/fetch/index.ts index 903810ac25..d9eac9482e 100644 --- a/packages/frontend-core/src/fetch/index.js +++ b/packages/frontend-core/src/fetch/index.ts @@ -10,6 +10,7 @@ import UserFetch from "./UserFetch.js" import GroupUserFetch from "./GroupUserFetch.js" import CustomFetch from "./CustomFetch.js" import QueryArrayFetch from "./QueryArrayFetch.js" +import { UIDatasource, UIFetchAPI } from "@budibase/types" const DataFetchMap = { table: TableFetch, @@ -29,15 +30,30 @@ const DataFetchMap = { } // Constructs a new fetch model for a certain datasource -export const fetchData = ({ API, datasource, options }) => { - const Fetch = DataFetchMap[datasource?.type] || TableFetch +export const fetchData = ({ + API, + datasource, + options, +}: { + API: UIFetchAPI + datasource: UIDatasource + options: {} +}) => { + const Fetch = + DataFetchMap[datasource?.type as keyof typeof DataFetchMap] || TableFetch return new Fetch({ API, datasource, ...options }) } // Creates an empty fetch instance with no datasource configured, so no data // will initially be loaded -const createEmptyFetchInstance = ({ API, datasource }) => { - const handler = DataFetchMap[datasource?.type] +const createEmptyFetchInstance = ({ + API, + datasource, +}: { + API: UIFetchAPI + datasource: UIDatasource +}) => { + const handler = DataFetchMap[datasource?.type as keyof typeof DataFetchMap] if (!handler) { return null } @@ -45,13 +61,27 @@ const createEmptyFetchInstance = ({ API, datasource }) => { } // Fetches the definition of any type of datasource -export const getDatasourceDefinition = async ({ API, datasource }) => { +export const getDatasourceDefinition = async ({ + API, + datasource, +}: { + API: UIFetchAPI + datasource: UIDatasource +}) => { const instance = createEmptyFetchInstance({ API, datasource }) return await instance?.getDefinition(datasource) } // Fetches the schema of any type of datasource -export const getDatasourceSchema = ({ API, datasource, definition }) => { +export const getDatasourceSchema = ({ + API, + datasource, + definition, +}: { + API: UIFetchAPI + datasource: UIDatasource + definition: {} +}) => { const instance = createEmptyFetchInstance({ API, datasource }) return instance?.getSchema(datasource, definition) } From ecfc248e606f85ddd3a60e462e07427003216855 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 31 Dec 2024 15:43:08 +0100 Subject: [PATCH 007/131] Type datafetch --- .../src/fetch/{DataFetch.js => DataFetch.ts} | 178 ++++++++++++------ packages/frontend-core/src/fetch/index.ts | 4 +- packages/shared-core/src/filters.ts | 8 +- packages/types/src/ui/stores/grid/fetch.ts | 2 + 4 files changed, 128 insertions(+), 64 deletions(-) rename packages/frontend-core/src/fetch/{DataFetch.js => DataFetch.ts} (76%) diff --git a/packages/frontend-core/src/fetch/DataFetch.js b/packages/frontend-core/src/fetch/DataFetch.ts similarity index 76% rename from packages/frontend-core/src/fetch/DataFetch.js rename to packages/frontend-core/src/fetch/DataFetch.ts index 175365a442..d8a8e15245 100644 --- a/packages/frontend-core/src/fetch/DataFetch.js +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -1,25 +1,86 @@ -import { writable, derived, get } from "svelte/store" +import { writable, derived, get, Writable, Readable } from "svelte/store" import { cloneDeep } from "lodash/fp" import { QueryUtils } from "../utils" import { convertJSONSchemaToTableSchema } from "../utils/json" -import { FieldType, SortOrder, SortType } from "@budibase/types" +import { + FieldType, + LegacyFilter, + SearchFilters, + SortOrder, + SortType, + Table, + TableSchema, + UIDatasource, + UIFetchAPI, + UIRow, + UISearchFilter, +} from "@budibase/types" const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils +interface DataFetchStore { + rows: UIRow[] + info: null + schema: TableSchema | null + loading: boolean + loaded: boolean + query: SearchFilters | null + pageNumber: number + cursor: null + cursors: any[] + resetKey: number + error: null +} + +interface DataFetchDerivedStore extends DataFetchStore { + hasNextPage: boolean + hasPrevPage: boolean + supportsSearch: boolean + supportsSort: boolean + supportsPagination: boolean +} + /** * Parent class which handles the implementation of fetching data from an * internal table or datasource plus. * For other types of datasource, this class is overridden and extended. */ -export default class DataFetch { +export default abstract class DataFetch { + API: UIFetchAPI + features: { + supportsSearch: boolean + supportsSort: boolean + supportsPagination: boolean + } + options: { + datasource: UIDatasource | null + limit: number + // Search config + filter: UISearchFilter | LegacyFilter[] | null + query: SearchFilters | null + // Sorting config + sortColumn: string | null + sortOrder: SortOrder + sortType: SortType | null + // Pagination config + paginate: boolean + // Client side feature customisation + clientSideSearching: boolean + clientSideSorting: boolean + clientSideLimiting: boolean + } + store: Writable<DataFetchStore> + derivedStore: Readable<DataFetchDerivedStore> + /** * Constructs a new DataFetch instance. * @param opts the fetch options */ - constructor(opts) { - // API client - this.API = null - + constructor(opts: { + API: UIFetchAPI + datasource?: UIDatasource + options?: {} + }) { // Feature flags this.features = { supportsSearch: false, @@ -118,7 +179,10 @@ export default class DataFetch { /** * Gets the default sort column for this datasource */ - getDefaultSortColumn(definition, schema) { + getDefaultSortColumn( + definition: { primaryDisplay?: string } | null, + schema: Record<string, any> + ) { if (definition?.primaryDisplay && schema[definition.primaryDisplay]) { return definition.primaryDisplay } else { @@ -144,7 +208,7 @@ export default class DataFetch { } // Fetch and enrich schema - let schema = this.getSchema(datasource, definition) + let schema = this.getSchema(datasource, definition) ?? null schema = this.enrichSchema(schema) if (!schema) { return @@ -172,7 +236,7 @@ export default class DataFetch { if ( fieldSchema?.type === FieldType.NUMBER || fieldSchema?.type === FieldType.BIGINT || - fieldSchema?.calculationType + ("calculationType" in fieldSchema && fieldSchema?.calculationType) ) { this.options.sortType = SortType.NUMBER } @@ -185,7 +249,7 @@ export default class DataFetch { // Build the query let query = this.options.query if (!query) { - query = buildQuery(filter) + query = buildQuery(filter ?? undefined) } // Update store @@ -239,7 +303,7 @@ export default class DataFetch { // If we don't support sorting, do a client-side sort if (!this.features.supportsSort && clientSideSorting) { - rows = sort(rows, sortColumn, sortOrder, sortType) + rows = sort(rows, sortColumn as any, sortOrder, sortType) } // If we don't support pagination, do a client-side limit @@ -256,18 +320,13 @@ export default class DataFetch { } } - /** - * Fetches a single page of data from the remote resource. - * Must be overridden by a datasource specific child class. - */ - async getData() { - return { - rows: [], - info: null, - hasNextPage: false, - cursor: null, - } - } + abstract getData(): Promise<{ + rows: UIRow[] + info: any + hasNextPage: boolean + cursor: any + error: any + }> /** * Gets the definition for this datasource. @@ -275,13 +334,13 @@ export default class DataFetch { * @param datasource * @return {object} the definition */ - async getDefinition(datasource) { + async getDefinition(datasource: UIDatasource | null) { if (!datasource?.tableId) { return null } try { return await this.API.fetchTableDefinition(datasource.tableId) - } catch (error) { + } catch (error: any) { this.store.update(state => ({ ...state, error, @@ -293,11 +352,11 @@ export default class DataFetch { /** * Gets the schema definition for a datasource. * Defaults to getting the "schema" property of the definition. - * @param datasource the datasource + * @param _datasource the datasource * @param definition the datasource definition * @return {object} the schema */ - getSchema(datasource, definition) { + getSchema(_datasource: UIDatasource | null, definition: Table | null) { return definition?.schema } @@ -307,44 +366,48 @@ export default class DataFetch { * @param schema the datasource schema * @return {object} the enriched datasource schema */ - enrichSchema(schema) { + enrichSchema(schema: TableSchema | null): TableSchema | null { if (schema == null) { return null } // Check for any JSON fields so we can add any top level properties - let jsonAdditions = {} - Object.keys(schema).forEach(fieldKey => { + let jsonAdditions: Record<string, { type: string; nestedJSON: true }> = {} + for (const fieldKey of Object.keys(schema)) { const fieldSchema = schema[fieldKey] if (fieldSchema?.type === FieldType.JSON) { const jsonSchema = convertJSONSchemaToTableSchema(fieldSchema, { squashObjects: true, - }) - Object.keys(jsonSchema).forEach(jsonKey => { - jsonAdditions[`${fieldKey}.${jsonKey}`] = { - type: jsonSchema[jsonKey].type, - nestedJSON: true, + }) as Record<string, { type: string }> | null // TODO: remove when convertJSONSchemaToTableSchema is typed + if (jsonSchema) { + for (const jsonKey of Object.keys(jsonSchema)) { + jsonAdditions[`${fieldKey}.${jsonKey}`] = { + type: jsonSchema[jsonKey].type, + nestedJSON: true, + } } - }) + } } - }) - schema = { ...schema, ...jsonAdditions } + } // Ensure schema is in the correct structure - let enrichedSchema = {} - Object.entries(schema).forEach(([fieldName, fieldSchema]) => { - if (typeof fieldSchema === "string") { - enrichedSchema[fieldName] = { - type: fieldSchema, - name: fieldName, - } - } else { - enrichedSchema[fieldName] = { - ...fieldSchema, - name: fieldName, + let enrichedSchema: TableSchema = {} + Object.entries({ ...schema, ...jsonAdditions }).forEach( + ([fieldName, fieldSchema]) => { + if (typeof fieldSchema === "string") { + enrichedSchema[fieldName] = { + type: fieldSchema, + name: fieldName, + } + } else { + enrichedSchema[fieldName] = { + ...fieldSchema, + type: fieldSchema.type as any, // TODO: check type union definition conflicts + name: fieldName, + } } } - }) + ) return enrichedSchema } @@ -353,7 +416,7 @@ export default class DataFetch { * Determine the feature flag for this datasource definition * @param definition */ - determineFeatureFlags(_definition) { + determineFeatureFlags(_definition: Table | null) { return { supportsSearch: false, supportsSort: false, @@ -365,12 +428,11 @@ export default class DataFetch { * Resets the data set and updates options * @param newOptions any new options */ - async update(newOptions) { + async update(newOptions: any) { // Check if any settings have actually changed let refresh = false - const entries = Object.entries(newOptions || {}) - for (let [key, value] of entries) { - const oldVal = this.options[key] == null ? null : this.options[key] + for (const [key, value] of Object.entries(newOptions || {})) { + const oldVal = this.options[key as keyof typeof this.options] ?? null const newVal = value == null ? null : value if (JSON.stringify(newVal) !== JSON.stringify(oldVal)) { refresh = true @@ -437,7 +499,7 @@ export default class DataFetch { * @param state the current store state * @return {boolean} whether there is a next page of data or not */ - hasNextPage(state) { + hasNextPage(state: DataFetchStore): boolean { return state.cursors[state.pageNumber + 1] != null } @@ -447,7 +509,7 @@ export default class DataFetch { * @param state the current store state * @return {boolean} whether there is a previous page of data or not */ - hasPrevPage(state) { + hasPrevPage(state: { pageNumber: number }): boolean { return state.pageNumber > 0 } diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index d9eac9482e..a08748a77e 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -10,7 +10,7 @@ import UserFetch from "./UserFetch.js" import GroupUserFetch from "./GroupUserFetch.js" import CustomFetch from "./CustomFetch.js" import QueryArrayFetch from "./QueryArrayFetch.js" -import { UIDatasource, UIFetchAPI } from "@budibase/types" +import { Table, UIDatasource, UIFetchAPI } from "@budibase/types" const DataFetchMap = { table: TableFetch, @@ -80,7 +80,7 @@ export const getDatasourceSchema = ({ }: { API: UIFetchAPI datasource: UIDatasource - definition: {} + definition: Table }) => { const instance = createEmptyFetchInstance({ API, datasource }) return instance?.getSchema(datasource, definition) diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index a023015b7e..a1e8534a95 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -552,7 +552,7 @@ export function search<T extends Record<string, any>>( */ export function runQuery<T extends Record<string, any>>( docs: T[], - query: SearchFilters + query: SearchFilters | null ): T[] { if (!docs || !Array.isArray(docs)) { return [] @@ -876,7 +876,7 @@ export function sort<T extends Record<string, any>>( docs: T[], sort: keyof T, sortOrder: SortOrder, - sortType = SortType.STRING + sortType: SortType | null = SortType.STRING ): T[] { if (!sort || !sortOrder || !sortType) { return docs @@ -911,8 +911,8 @@ export function sort<T extends Record<string, any>>( * @param docs the data * @param limit the number of docs to limit to */ -export function limit<T>(docs: T[], limit: string): T[] { - const numLimit = parseFloat(limit) +export function limit<T>(docs: T[], limit: string | number): T[] { + const numLimit = typeof limit === "number" ? limit : parseFloat(limit) if (isNaN(numLimit)) { return docs } diff --git a/packages/types/src/ui/stores/grid/fetch.ts b/packages/types/src/ui/stores/grid/fetch.ts index 8901acc08b..0be9ca17b4 100644 --- a/packages/types/src/ui/stores/grid/fetch.ts +++ b/packages/types/src/ui/stores/grid/fetch.ts @@ -1,12 +1,14 @@ import { Row, SortOrder, + Table, UIDatasource, UILegacyFilter, UISearchFilter, } from "@budibase/types" export interface UIFetchAPI { + fetchTableDefinition(tableId: string): Promise<Table> definition: UIDatasource getInitialData: () => Promise<void> From d7cfd51caf50ba6603a711e8a0295e1ee3f7417b Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 2 Jan 2025 10:23:39 +0100 Subject: [PATCH 008/131] Type tablefetch --- packages/frontend-core/src/fetch/DataFetch.ts | 6 +++--- .../src/fetch/{TableFetch.js => TableFetch.ts} | 2 +- packages/types/src/ui/stores/grid/fetch.ts | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) rename packages/frontend-core/src/fetch/{TableFetch.js => TableFetch.ts} (96%) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index d8a8e15245..67ed07a835 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -322,10 +322,10 @@ export default abstract class DataFetch { abstract getData(): Promise<{ rows: UIRow[] - info: any + info?: any hasNextPage: boolean - cursor: any - error: any + cursor?: any + error?: any }> /** diff --git a/packages/frontend-core/src/fetch/TableFetch.js b/packages/frontend-core/src/fetch/TableFetch.ts similarity index 96% rename from packages/frontend-core/src/fetch/TableFetch.js rename to packages/frontend-core/src/fetch/TableFetch.ts index 777d16aa45..0615f83dc2 100644 --- a/packages/frontend-core/src/fetch/TableFetch.js +++ b/packages/frontend-core/src/fetch/TableFetch.ts @@ -14,7 +14,7 @@ export default class TableFetch extends DataFetch { async getData() { const { datasource, limit, sortColumn, sortOrder, sortType, paginate } = this.options - const { tableId } = datasource + const { tableId } = datasource! const { cursor, query } = get(this.store) // Search table diff --git a/packages/types/src/ui/stores/grid/fetch.ts b/packages/types/src/ui/stores/grid/fetch.ts index 0be9ca17b4..772e68eb14 100644 --- a/packages/types/src/ui/stores/grid/fetch.ts +++ b/packages/types/src/ui/stores/grid/fetch.ts @@ -1,6 +1,8 @@ import { Row, + SearchFilters, SortOrder, + SortType, Table, UIDatasource, UILegacyFilter, @@ -15,6 +17,19 @@ export interface UIFetchAPI { loading: any loaded: boolean + searchTable( + tableId: string, + arg1: { + query: SearchFilters | null + limit: number + sort: string | null + sortOrder: string + sortType: SortType | null + paginate: boolean + bookmark: null + } + ): any + resetKey: string | null error: any From 89eba3189703454ed1055251713b98629cb53a14 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 2 Jan 2025 12:16:53 +0100 Subject: [PATCH 009/131] Use tempaltes --- packages/frontend-core/src/fetch/DataFetch.ts | 14 ++++------- .../frontend-core/src/fetch/TableFetch.ts | 6 ++--- packages/types/src/ui/stores/grid/fetch.ts | 23 +++++++++---------- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index 67ed07a835..7355afd967 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -45,7 +45,7 @@ interface DataFetchDerivedStore extends DataFetchStore { * internal table or datasource plus. * For other types of datasource, this class is overridden and extended. */ -export default abstract class DataFetch { +export default abstract class DataFetch<T extends UIDatasource | null> { API: UIFetchAPI features: { supportsSearch: boolean @@ -53,7 +53,7 @@ export default abstract class DataFetch { supportsPagination: boolean } options: { - datasource: UIDatasource | null + datasource: T limit: number // Search config filter: UISearchFilter | LegacyFilter[] | null @@ -76,11 +76,7 @@ export default abstract class DataFetch { * Constructs a new DataFetch instance. * @param opts the fetch options */ - constructor(opts: { - API: UIFetchAPI - datasource?: UIDatasource - options?: {} - }) { + constructor(opts: { API: UIFetchAPI; datasource: T; options?: {} }) { // Feature flags this.features = { supportsSearch: false, @@ -90,7 +86,7 @@ export default abstract class DataFetch { // Config this.options = { - datasource: null, + datasource: opts.datasource, limit: 10, // Search config @@ -182,7 +178,7 @@ export default abstract class DataFetch { getDefaultSortColumn( definition: { primaryDisplay?: string } | null, schema: Record<string, any> - ) { + ): string | null { if (definition?.primaryDisplay && schema[definition.primaryDisplay]) { return definition.primaryDisplay } else { diff --git a/packages/frontend-core/src/fetch/TableFetch.ts b/packages/frontend-core/src/fetch/TableFetch.ts index 0615f83dc2..e3a2e317be 100644 --- a/packages/frontend-core/src/fetch/TableFetch.ts +++ b/packages/frontend-core/src/fetch/TableFetch.ts @@ -1,8 +1,8 @@ import { get } from "svelte/store" import DataFetch from "./DataFetch.js" -import { SortOrder } from "@budibase/types" +import { SortOrder, UITable } from "@budibase/types" -export default class TableFetch extends DataFetch { +export default class TableFetch extends DataFetch<UITable> { determineFeatureFlags() { return { supportsSearch: true, @@ -14,7 +14,7 @@ export default class TableFetch extends DataFetch { async getData() { const { datasource, limit, sortColumn, sortOrder, sortType, paginate } = this.options - const { tableId } = datasource! + const { tableId } = datasource const { cursor, query } = get(this.store) // Search table diff --git a/packages/types/src/ui/stores/grid/fetch.ts b/packages/types/src/ui/stores/grid/fetch.ts index 772e68eb14..a81f436fde 100644 --- a/packages/types/src/ui/stores/grid/fetch.ts +++ b/packages/types/src/ui/stores/grid/fetch.ts @@ -9,6 +9,16 @@ import { UISearchFilter, } from "@budibase/types" +interface SearchOptions { + query: SearchFilters | null + limit: number + sort: string | null + sortOrder: string + sortType: SortType | null + paginate: boolean + bookmark: null +} + export interface UIFetchAPI { fetchTableDefinition(tableId: string): Promise<Table> definition: UIDatasource @@ -17,18 +27,7 @@ export interface UIFetchAPI { loading: any loaded: boolean - searchTable( - tableId: string, - arg1: { - query: SearchFilters | null - limit: number - sort: string | null - sortOrder: string - sortType: SortType | null - paginate: boolean - bookmark: null - } - ): any + searchTable(tableId: string, options: SearchOptions): any resetKey: string | null error: any From 97eb4a2e79824923b41c47d3a51f9f2d76175a2a Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 2 Jan 2025 12:33:54 +0100 Subject: [PATCH 010/131] Type view fetch --- packages/frontend-core/src/fetch/DataFetch.ts | 22 ++++++++++------- .../fetch/{ViewV2Fetch.js => ViewV2Fetch.ts} | 24 +++++++++++-------- .../types/src/ui/stores/grid/datasource.ts | 4 +--- packages/types/src/ui/stores/grid/fetch.ts | 9 +++++-- packages/types/src/ui/stores/grid/view.ts | 3 ++- 5 files changed, 37 insertions(+), 25 deletions(-) rename packages/frontend-core/src/fetch/{ViewV2Fetch.js => ViewV2Fetch.ts} (70%) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index 7355afd967..ea28cd7240 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -8,7 +8,6 @@ import { SearchFilters, SortOrder, SortType, - Table, TableSchema, UIDatasource, UIFetchAPI, @@ -18,7 +17,7 @@ import { const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils -interface DataFetchStore { +interface DataFetchStore<T extends UIDatasource | null> { rows: UIRow[] info: null schema: TableSchema | null @@ -30,9 +29,11 @@ interface DataFetchStore { cursors: any[] resetKey: number error: null + definition?: T | null } -interface DataFetchDerivedStore extends DataFetchStore { +interface DataFetchDerivedStore<T extends UIDatasource | null> + extends DataFetchStore<T> { hasNextPage: boolean hasPrevPage: boolean supportsSearch: boolean @@ -69,8 +70,8 @@ export default abstract class DataFetch<T extends UIDatasource | null> { clientSideSorting: boolean clientSideLimiting: boolean } - store: Writable<DataFetchStore> - derivedStore: Readable<DataFetchDerivedStore> + store: Writable<DataFetchStore<T>> + derivedStore: Readable<DataFetchDerivedStore<T>> /** * Constructs a new DataFetch instance. @@ -335,7 +336,7 @@ export default abstract class DataFetch<T extends UIDatasource | null> { return null } try { - return await this.API.fetchTableDefinition(datasource.tableId) + return (await this.API.fetchTableDefinition(datasource.tableId)) as T } catch (error: any) { this.store.update(state => ({ ...state, @@ -352,7 +353,10 @@ export default abstract class DataFetch<T extends UIDatasource | null> { * @param definition the datasource definition * @return {object} the schema */ - getSchema(_datasource: UIDatasource | null, definition: Table | null) { + getSchema( + _datasource: UIDatasource | null, + definition: T | null + ): TableSchema | undefined { return definition?.schema } @@ -412,7 +416,7 @@ export default abstract class DataFetch<T extends UIDatasource | null> { * Determine the feature flag for this datasource definition * @param definition */ - determineFeatureFlags(_definition: Table | null) { + determineFeatureFlags(_definition: T | null) { return { supportsSearch: false, supportsSort: false, @@ -495,7 +499,7 @@ export default abstract class DataFetch<T extends UIDatasource | null> { * @param state the current store state * @return {boolean} whether there is a next page of data or not */ - hasNextPage(state: DataFetchStore): boolean { + hasNextPage(state: DataFetchStore<T>): boolean { return state.cursors[state.pageNumber + 1] != null } diff --git a/packages/frontend-core/src/fetch/ViewV2Fetch.js b/packages/frontend-core/src/fetch/ViewV2Fetch.ts similarity index 70% rename from packages/frontend-core/src/fetch/ViewV2Fetch.js rename to packages/frontend-core/src/fetch/ViewV2Fetch.ts index 8436646077..337c090c66 100644 --- a/packages/frontend-core/src/fetch/ViewV2Fetch.js +++ b/packages/frontend-core/src/fetch/ViewV2Fetch.ts @@ -1,8 +1,9 @@ -import { ViewV2Type } from "@budibase/types" +import { SortOrder, UIView, ViewV2Type } from "@budibase/types" import DataFetch from "./DataFetch.js" import { get } from "svelte/store" +import { isCalculationField } from "packages/shared-core/src/helpers/views.js" -export default class ViewV2Fetch extends DataFetch { +export default class ViewV2Fetch extends DataFetch<UIView> { determineFeatureFlags() { return { supportsSearch: true, @@ -11,18 +12,18 @@ export default class ViewV2Fetch extends DataFetch { } } - getSchema(datasource, definition) { + getSchema(_datasource: UIView | null, definition: UIView | null) { return definition?.schema } - async getDefinition(datasource) { + async getDefinition(datasource: UIView | null): Promise<UIView | null> { if (!datasource?.id) { return null } try { const res = await this.API.viewV2.fetchDefinition(datasource.id) return res?.data - } catch (error) { + } catch (error: any) { this.store.update(state => ({ ...state, error, @@ -31,7 +32,10 @@ export default class ViewV2Fetch extends DataFetch { } } - getDefaultSortColumn() { + getDefaultSortColumn( + _definition: { primaryDisplay?: string } | null, + _schema: Record<string, any> + ) { return null } @@ -42,8 +46,8 @@ export default class ViewV2Fetch extends DataFetch { // If this is a calculation view and we have no calculations, return nothing if ( - definition.type === ViewV2Type.CALCULATION && - !Object.values(definition.schema || {}).some(x => x.calculationType) + definition?.type === ViewV2Type.CALCULATION && + !Object.values(definition.schema || {}).some(isCalculationField) ) { return { rows: [], @@ -56,9 +60,9 @@ export default class ViewV2Fetch extends DataFetch { // If sort/filter params are not defined, update options to store the // params built in to this view. This ensures that we can accurately // compare old and new params and skip a redundant API call. - if (!sortColumn && definition.sort?.field) { + if (!sortColumn && definition?.sort?.field) { this.options.sortColumn = definition.sort.field - this.options.sortOrder = definition.sort.order + this.options.sortOrder = definition.sort.order || SortOrder.ASCENDING } try { diff --git a/packages/types/src/ui/stores/grid/datasource.ts b/packages/types/src/ui/stores/grid/datasource.ts index 9533bbb8f0..9927518133 100644 --- a/packages/types/src/ui/stores/grid/datasource.ts +++ b/packages/types/src/ui/stores/grid/datasource.ts @@ -1,8 +1,6 @@ import { UITable, UIView } from "@budibase/types" -export type UIDatasource = (UITable | UIView) & { - type: string -} +export type UIDatasource = UITable | UIView export interface UIFieldMutation { visible?: boolean diff --git a/packages/types/src/ui/stores/grid/fetch.ts b/packages/types/src/ui/stores/grid/fetch.ts index a81f436fde..a8732c66e3 100644 --- a/packages/types/src/ui/stores/grid/fetch.ts +++ b/packages/types/src/ui/stores/grid/fetch.ts @@ -10,10 +10,10 @@ import { } from "@budibase/types" interface SearchOptions { - query: SearchFilters | null + query?: SearchFilters | null | undefined limit: number sort: string | null - sortOrder: string + sortOrder: string | undefined sortType: SortType | null paginate: boolean bookmark: null @@ -29,6 +29,11 @@ export interface UIFetchAPI { searchTable(tableId: string, options: SearchOptions): any + viewV2: { + fetchDefinition: (datasourceId: string) => Promise<any> + fetch: (datasourceId: string, options: SearchOptions) => any + } + resetKey: string | null error: any diff --git a/packages/types/src/ui/stores/grid/view.ts b/packages/types/src/ui/stores/grid/view.ts index f81cc34aaf..270faaa160 100644 --- a/packages/types/src/ui/stores/grid/view.ts +++ b/packages/types/src/ui/stores/grid/view.ts @@ -1,6 +1,7 @@ import { ViewV2 } from "@budibase/types" import { UIFieldSchema } from "./table" -export interface UIView extends ViewV2 { +export interface UIView extends Omit<ViewV2, "type"> { + type: string schema: Record<string, UIFieldSchema> } From 163ca349a91d335c8344867761f892bcac994da1 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 2 Jan 2025 13:21:36 +0100 Subject: [PATCH 011/131] Create apis --- packages/types/src/ui/stores/grid/fetch.ts | 35 +++++++++++++++++----- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/packages/types/src/ui/stores/grid/fetch.ts b/packages/types/src/ui/stores/grid/fetch.ts index a8732c66e3..5f42db24b0 100644 --- a/packages/types/src/ui/stores/grid/fetch.ts +++ b/packages/types/src/ui/stores/grid/fetch.ts @@ -19,20 +19,41 @@ interface SearchOptions { bookmark: null } -export interface UIFetchAPI { +interface TableAPI { fetchTableDefinition(tableId: string): Promise<Table> + searchTable(tableId: string, options: SearchOptions): any +} + +interface ViewV2API { + fetchDefinition: (datasourceId: string) => Promise<any> + fetch: (datasourceId: string, options: SearchOptions) => any +} + +interface UserAPI { + searchUsers: (opts: { + bookmark: null + query: + | SearchFilters + | { + string: { + email: null + } + } + | null + appId: string + paginate: boolean + limit: number + }) => Promise<any> +} + +export interface UIFetchAPI extends TableAPI, UserAPI { definition: UIDatasource getInitialData: () => Promise<void> loading: any loaded: boolean - searchTable(tableId: string, options: SearchOptions): any - - viewV2: { - fetchDefinition: (datasourceId: string) => Promise<any> - fetch: (datasourceId: string, options: SearchOptions) => any - } + viewV2: ViewV2API resetKey: string | null error: any From 1899af919077c8ca87030c2c27517ddb4565f549 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 2 Jan 2025 13:33:24 +0100 Subject: [PATCH 012/131] More types --- packages/frontend-core/src/fetch/DataFetch.ts | 57 ++++++++----------- .../frontend-core/src/fetch/TableFetch.ts | 23 +++++++- .../frontend-core/src/fetch/ViewV2Fetch.ts | 8 +-- 3 files changed, 49 insertions(+), 39 deletions(-) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index ea28cd7240..389ddd2f17 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -17,7 +17,7 @@ import { const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils -interface DataFetchStore<T extends UIDatasource | null> { +interface DataFetchStore<T> { rows: UIRow[] info: null schema: TableSchema | null @@ -32,8 +32,7 @@ interface DataFetchStore<T extends UIDatasource | null> { definition?: T | null } -interface DataFetchDerivedStore<T extends UIDatasource | null> - extends DataFetchStore<T> { +interface DataFetchDerivedStore<T> extends DataFetchStore<T> { hasNextPage: boolean hasPrevPage: boolean supportsSearch: boolean @@ -46,7 +45,10 @@ interface DataFetchDerivedStore<T extends UIDatasource | null> * internal table or datasource plus. * For other types of datasource, this class is overridden and extended. */ -export default abstract class DataFetch<T extends UIDatasource | null> { +export default abstract class DataFetch< + TDatasource extends UIDatasource | null, + TDefinition extends { primaryDisplay?: string } +> { API: UIFetchAPI features: { supportsSearch: boolean @@ -54,7 +56,7 @@ export default abstract class DataFetch<T extends UIDatasource | null> { supportsPagination: boolean } options: { - datasource: T + datasource: TDatasource limit: number // Search config filter: UISearchFilter | LegacyFilter[] | null @@ -70,14 +72,18 @@ export default abstract class DataFetch<T extends UIDatasource | null> { clientSideSorting: boolean clientSideLimiting: boolean } - store: Writable<DataFetchStore<T>> - derivedStore: Readable<DataFetchDerivedStore<T>> + store: Writable<DataFetchStore<TDefinition>> + derivedStore: Readable<DataFetchDerivedStore<TDefinition>> /** * Constructs a new DataFetch instance. * @param opts the fetch options */ - constructor(opts: { API: UIFetchAPI; datasource: T; options?: {} }) { + constructor(opts: { + API: UIFetchAPI + datasource: TDatasource + options?: {} + }) { // Feature flags this.features = { supportsSearch: false, @@ -327,38 +333,23 @@ export default abstract class DataFetch<T extends UIDatasource | null> { /** * Gets the definition for this datasource. - * Defaults to fetching a table definition. * @param datasource * @return {object} the definition */ - async getDefinition(datasource: UIDatasource | null) { - if (!datasource?.tableId) { - return null - } - try { - return (await this.API.fetchTableDefinition(datasource.tableId)) as T - } catch (error: any) { - this.store.update(state => ({ - ...state, - error, - })) - return null - } - } + abstract getDefinition( + datasource: UIDatasource | null + ): Promise<TDefinition | null> /** * Gets the schema definition for a datasource. - * Defaults to getting the "schema" property of the definition. - * @param _datasource the datasource + * @param datasource the datasource * @param definition the datasource definition * @return {object} the schema */ - getSchema( - _datasource: UIDatasource | null, - definition: T | null - ): TableSchema | undefined { - return definition?.schema - } + abstract getSchema( + datasource: UIDatasource | null, + definition: TDefinition | null + ): any /** * Enriches a datasource schema with nested fields and ensures the structure @@ -416,7 +407,7 @@ export default abstract class DataFetch<T extends UIDatasource | null> { * Determine the feature flag for this datasource definition * @param definition */ - determineFeatureFlags(_definition: T | null) { + determineFeatureFlags(_definition: TDefinition | null) { return { supportsSearch: false, supportsSort: false, @@ -499,7 +490,7 @@ export default abstract class DataFetch<T extends UIDatasource | null> { * @param state the current store state * @return {boolean} whether there is a next page of data or not */ - hasNextPage(state: DataFetchStore<T>): boolean { + hasNextPage(state: DataFetchStore<TDefinition>): boolean { return state.cursors[state.pageNumber + 1] != null } diff --git a/packages/frontend-core/src/fetch/TableFetch.ts b/packages/frontend-core/src/fetch/TableFetch.ts index e3a2e317be..3c4a1b7abc 100644 --- a/packages/frontend-core/src/fetch/TableFetch.ts +++ b/packages/frontend-core/src/fetch/TableFetch.ts @@ -1,8 +1,8 @@ import { get } from "svelte/store" import DataFetch from "./DataFetch.js" -import { SortOrder, UITable } from "@budibase/types" +import { SortOrder, Table, UITable } from "@budibase/types" -export default class TableFetch extends DataFetch<UITable> { +export default class TableFetch extends DataFetch<UITable, Table> { determineFeatureFlags() { return { supportsSearch: true, @@ -11,6 +11,25 @@ export default class TableFetch extends DataFetch<UITable> { } } + async getDefinition(datasource: UITable | null) { + if (!datasource?.tableId) { + return null + } + try { + return await this.API.fetchTableDefinition(datasource.tableId) + } catch (error: any) { + this.store.update(state => ({ + ...state, + error, + })) + return null + } + } + + getSchema(_datasource: UITable | null, definition: Table | null) { + return definition?.schema + } + async getData() { const { datasource, limit, sortColumn, sortOrder, sortType, paginate } = this.options diff --git a/packages/frontend-core/src/fetch/ViewV2Fetch.ts b/packages/frontend-core/src/fetch/ViewV2Fetch.ts index 337c090c66..d880b3a549 100644 --- a/packages/frontend-core/src/fetch/ViewV2Fetch.ts +++ b/packages/frontend-core/src/fetch/ViewV2Fetch.ts @@ -1,9 +1,9 @@ -import { SortOrder, UIView, ViewV2Type } from "@budibase/types" +import { SortOrder, UIView, ViewV2, ViewV2Type } from "@budibase/types" import DataFetch from "./DataFetch.js" import { get } from "svelte/store" import { isCalculationField } from "packages/shared-core/src/helpers/views.js" -export default class ViewV2Fetch extends DataFetch<UIView> { +export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> { determineFeatureFlags() { return { supportsSearch: true, @@ -12,11 +12,11 @@ export default class ViewV2Fetch extends DataFetch<UIView> { } } - getSchema(_datasource: UIView | null, definition: UIView | null) { + getSchema(_datasource: UIView, definition: ViewV2) { return definition?.schema } - async getDefinition(datasource: UIView | null): Promise<UIView | null> { + async getDefinition(datasource: UIView | null): Promise<ViewV2 | null> { if (!datasource?.id) { return null } From c7255362b0b8b1a72f785602526515d16669c62a Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 2 Jan 2025 13:36:38 +0100 Subject: [PATCH 013/131] Cleanup --- packages/frontend-core/src/fetch/DataFetch.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index 389ddd2f17..2dc42c2425 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -9,7 +9,6 @@ import { SortOrder, SortType, TableSchema, - UIDatasource, UIFetchAPI, UIRow, UISearchFilter, @@ -46,8 +45,8 @@ interface DataFetchDerivedStore<T> extends DataFetchStore<T> { * For other types of datasource, this class is overridden and extended. */ export default abstract class DataFetch< - TDatasource extends UIDatasource | null, - TDefinition extends { primaryDisplay?: string } + TDatasource extends {}, + TDefinition extends {} > { API: UIFetchAPI features: { @@ -337,7 +336,7 @@ export default abstract class DataFetch< * @return {object} the definition */ abstract getDefinition( - datasource: UIDatasource | null + datasource: TDatasource | null ): Promise<TDefinition | null> /** @@ -347,7 +346,7 @@ export default abstract class DataFetch< * @return {object} the schema */ abstract getSchema( - datasource: UIDatasource | null, + datasource: TDatasource | null, definition: TDefinition | null ): any From 54d5047b34932b4beab6f582d3a2b7059d6d488f Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 2 Jan 2025 13:38:38 +0100 Subject: [PATCH 014/131] Convert UserFetch --- .../src/fetch/{UserFetch.js => UserFetch.ts} | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) rename packages/frontend-core/src/fetch/{UserFetch.js => UserFetch.ts} (77%) diff --git a/packages/frontend-core/src/fetch/UserFetch.js b/packages/frontend-core/src/fetch/UserFetch.ts similarity index 77% rename from packages/frontend-core/src/fetch/UserFetch.js rename to packages/frontend-core/src/fetch/UserFetch.ts index 36f61542b5..730d96ebd5 100644 --- a/packages/frontend-core/src/fetch/UserFetch.js +++ b/packages/frontend-core/src/fetch/UserFetch.ts @@ -2,9 +2,10 @@ import { get } from "svelte/store" import DataFetch from "./DataFetch.js" import { TableNames } from "../constants" import { utils } from "@budibase/shared-core" +import { Table, UIFetchAPI } from "@budibase/types" -export default class UserFetch extends DataFetch { - constructor(opts) { +export default class UserFetch extends DataFetch<{ tableId: string }, {}> { + constructor(opts: { API: UIFetchAPI; datasource: Table; options?: {} }) { super({ ...opts, datasource: { @@ -27,12 +28,16 @@ export default class UserFetch extends DataFetch { } } + getSchema(_datasource: any, definition: Table | null) { + return definition?.schema + } + async getData() { const { limit, paginate } = this.options const { cursor, query } = get(this.store) // Convert old format to new one - we now allow use of the lucene format - const { appId, paginated, ...rest } = query || {} + const { appId, paginated, ...rest } = query || ({} as any) // TODO const finalQuery = utils.isSupportedUserSearch(rest) ? query : { string: { email: null } } From 65fa3e04346985c2d9f4293fb0e007d0c79f41e0 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 2 Jan 2025 15:36:27 +0100 Subject: [PATCH 015/131] Use APIClient --- packages/frontend-core/src/fetch/DataFetch.ts | 16 ++++++---------- packages/frontend-core/src/fetch/TableFetch.ts | 4 ++-- packages/frontend-core/src/fetch/ViewV2Fetch.ts | 8 +++++--- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index 2dc42c2425..ad709c9c77 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -5,19 +5,19 @@ import { convertJSONSchemaToTableSchema } from "../utils/json" import { FieldType, LegacyFilter, + Row, SearchFilters, SortOrder, SortType, TableSchema, - UIFetchAPI, - UIRow, UISearchFilter, } from "@budibase/types" +import { APIClient } from "../api/types" const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils interface DataFetchStore<T> { - rows: UIRow[] + rows: Row[] info: null schema: TableSchema | null loading: boolean @@ -48,7 +48,7 @@ export default abstract class DataFetch< TDatasource extends {}, TDefinition extends {} > { - API: UIFetchAPI + API: APIClient features: { supportsSearch: boolean supportsSort: boolean @@ -78,11 +78,7 @@ export default abstract class DataFetch< * Constructs a new DataFetch instance. * @param opts the fetch options */ - constructor(opts: { - API: UIFetchAPI - datasource: TDatasource - options?: {} - }) { + constructor(opts: { API: APIClient; datasource: TDatasource; options?: {} }) { // Feature flags this.features = { supportsSearch: false, @@ -323,7 +319,7 @@ export default abstract class DataFetch< } abstract getData(): Promise<{ - rows: UIRow[] + rows: Row[] info?: any hasNextPage: boolean cursor?: any diff --git a/packages/frontend-core/src/fetch/TableFetch.ts b/packages/frontend-core/src/fetch/TableFetch.ts index 3c4a1b7abc..08dc111b28 100644 --- a/packages/frontend-core/src/fetch/TableFetch.ts +++ b/packages/frontend-core/src/fetch/TableFetch.ts @@ -39,10 +39,10 @@ export default class TableFetch extends DataFetch<UITable, Table> { // Search table try { const res = await this.API.searchTable(tableId, { - query, + query: query ?? undefined, limit, sort: sortColumn, - sortOrder: sortOrder?.toLowerCase() ?? SortOrder.ASCENDING, + sortOrder: sortOrder ?? SortOrder.ASCENDING, sortType, paginate, bookmark: cursor, diff --git a/packages/frontend-core/src/fetch/ViewV2Fetch.ts b/packages/frontend-core/src/fetch/ViewV2Fetch.ts index d880b3a549..91d2385d3c 100644 --- a/packages/frontend-core/src/fetch/ViewV2Fetch.ts +++ b/packages/frontend-core/src/fetch/ViewV2Fetch.ts @@ -1,7 +1,7 @@ import { SortOrder, UIView, ViewV2, ViewV2Type } from "@budibase/types" import DataFetch from "./DataFetch.js" import { get } from "svelte/store" -import { isCalculationField } from "packages/shared-core/src/helpers/views.js" +import { helpers } from "@budibase/shared-core" export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> { determineFeatureFlags() { @@ -47,7 +47,9 @@ export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> { // If this is a calculation view and we have no calculations, return nothing if ( definition?.type === ViewV2Type.CALCULATION && - !Object.values(definition.schema || {}).some(isCalculationField) + !Object.values(definition.schema || {}).some( + helpers.views.isCalculationField + ) ) { return { rows: [], @@ -72,7 +74,7 @@ export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> { limit, bookmark: cursor, sort: sortColumn, - sortOrder: sortOrder?.toLowerCase(), + sortOrder: sortOrder, sortType, }) return { From 1f51489368acccbbeda3deb6ed219f4a60a3c723 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 2 Jan 2025 15:48:04 +0100 Subject: [PATCH 016/131] Type views --- packages/frontend-core/src/api/viewsV2.ts | 13 ++++++--- .../frontend-core/src/fetch/ViewV2Fetch.ts | 28 +++++++++++++++---- .../server/src/api/controllers/row/views.ts | 11 ++++++-- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/packages/frontend-core/src/api/viewsV2.ts b/packages/frontend-core/src/api/viewsV2.ts index 5018448e8c..4a867e8f6a 100644 --- a/packages/frontend-core/src/api/viewsV2.ts +++ b/packages/frontend-core/src/api/viewsV2.ts @@ -1,6 +1,7 @@ import { CreateViewRequest, CreateViewResponse, + PaginatedSearchRowResponse, SearchRowResponse, SearchViewRowRequest, UpdateViewRequest, @@ -13,10 +14,14 @@ export interface ViewV2Endpoints { fetchDefinition: (viewId: string) => Promise<ViewResponseEnriched> create: (view: CreateViewRequest) => Promise<CreateViewResponse> update: (view: UpdateViewRequest) => Promise<UpdateViewResponse> - fetch: ( + fetch: <T extends SearchViewRowRequest>( viewId: string, - opts: SearchViewRowRequest - ) => Promise<SearchRowResponse> + opts: T + ) => Promise< + T extends { paginate: true } + ? PaginatedSearchRowResponse + : SearchRowResponse + > delete: (viewId: string) => Promise<void> } @@ -59,7 +64,7 @@ export const buildViewV2Endpoints = (API: BaseAPIClient): ViewV2Endpoints => ({ * @param viewId the id of the view * @param opts the search options */ - fetch: async (viewId, opts) => { + fetch: async (viewId, opts: SearchViewRowRequest) => { return await API.post({ url: `/api/v2/views/${encodeURIComponent(viewId)}/search`, body: opts, diff --git a/packages/frontend-core/src/fetch/ViewV2Fetch.ts b/packages/frontend-core/src/fetch/ViewV2Fetch.ts index 91d2385d3c..d3b607a171 100644 --- a/packages/frontend-core/src/fetch/ViewV2Fetch.ts +++ b/packages/frontend-core/src/fetch/ViewV2Fetch.ts @@ -68,7 +68,7 @@ export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> { } try { - const res = await this.API.viewV2.fetch(datasource.id, { + const request = { ...(query ? { query } : {}), paginate, limit, @@ -76,11 +76,27 @@ export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> { sort: sortColumn, sortOrder: sortOrder, sortType, - }) - return { - rows: res?.rows || [], - hasNextPage: res?.hasNextPage || false, - cursor: res?.bookmark || null, + } + if (paginate) { + const res = await this.API.viewV2.fetch(datasource.id, { + ...request, + paginate, + }) + return { + rows: res?.rows || [], + hasNextPage: res?.hasNextPage || false, + cursor: res?.bookmark || null, + } + } else { + const res = await this.API.viewV2.fetch(datasource.id, { + ...request, + paginate, + }) + return { + rows: res?.rows || [], + hasNextPage: false, + cursor: null, + } } } catch (error) { return { diff --git a/packages/server/src/api/controllers/row/views.ts b/packages/server/src/api/controllers/row/views.ts index 0655a3b38f..dcf8680348 100644 --- a/packages/server/src/api/controllers/row/views.ts +++ b/packages/server/src/api/controllers/row/views.ts @@ -1,16 +1,16 @@ import { UserCtx, ViewV2, - SearchRowResponse, SearchViewRowRequest, RequiredKeys, RowSearchParams, + PaginatedSearchRowResponse, } from "@budibase/types" import sdk from "../../../sdk" import { context } from "@budibase/backend-core" export async function searchView( - ctx: UserCtx<SearchViewRowRequest, SearchRowResponse> + ctx: UserCtx<SearchViewRowRequest, PaginatedSearchRowResponse> ) { const { viewId } = ctx.params @@ -49,7 +49,12 @@ export async function searchView( user: sdk.users.getUserContextBindings(ctx.user), }) result.rows.forEach(r => (r._viewId = view.id)) - ctx.body = result + + ctx.body = { + rows: result.rows, + bookmark: result.bookmark, + hasNextPage: result.hasNextPage, + } } function getSortOptions(request: SearchViewRowRequest, view: ViewV2) { From f0d60c606329558308bd9024865b5046e6f12f05 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 2 Jan 2025 15:55:50 +0100 Subject: [PATCH 017/131] Fix user types --- packages/frontend-core/src/fetch/UserFetch.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/frontend-core/src/fetch/UserFetch.ts b/packages/frontend-core/src/fetch/UserFetch.ts index 730d96ebd5..be73793768 100644 --- a/packages/frontend-core/src/fetch/UserFetch.ts +++ b/packages/frontend-core/src/fetch/UserFetch.ts @@ -2,10 +2,11 @@ import { get } from "svelte/store" import DataFetch from "./DataFetch.js" import { TableNames } from "../constants" import { utils } from "@budibase/shared-core" -import { Table, UIFetchAPI } from "@budibase/types" +import { BasicOperator, SearchUsersRequest, Table } from "@budibase/types" +import { APIClient } from "../api/types.js" export default class UserFetch extends DataFetch<{ tableId: string }, {}> { - constructor(opts: { API: UIFetchAPI; datasource: Table; options?: {} }) { + constructor(opts: { API: APIClient; datasource: Table; options?: {} }) { super({ ...opts, datasource: { @@ -40,12 +41,12 @@ export default class UserFetch extends DataFetch<{ tableId: string }, {}> { const { appId, paginated, ...rest } = query || ({} as any) // TODO const finalQuery = utils.isSupportedUserSearch(rest) ? query - : { string: { email: null } } + : { [BasicOperator.EMPTY]: { email: true } } // TODO: check try { const opts = { - bookmark: cursor, - query: finalQuery, + bookmark: cursor ?? undefined, + query: finalQuery ?? undefined, appId: appId, paginate: paginated || paginate, limit, From 69ad15f79cb1b3dc482bf1557c35868d401f090c Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 2 Jan 2025 16:25:25 +0100 Subject: [PATCH 018/131] Fix types --- packages/frontend-core/src/api/views.ts | 2 +- packages/frontend-core/src/fetch/DataFetch.ts | 2 +- packages/frontend-core/src/fetch/TableFetch.ts | 2 +- packages/frontend-core/src/fetch/UserFetch.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/frontend-core/src/api/views.ts b/packages/frontend-core/src/api/views.ts index 3f8ac8aa41..083a3890e8 100644 --- a/packages/frontend-core/src/api/views.ts +++ b/packages/frontend-core/src/api/views.ts @@ -3,7 +3,7 @@ import { BaseAPIClient } from "./types" export interface ViewEndpoints { // Missing request or response types - fetchViewData: (name: string, opts: any) => Promise<Row[]> + fetchViewData: (name: string, opts?: any) => Promise<Row[]> exportView: (name: string, format: string) => Promise<any> saveView: (view: any) => Promise<any> deleteView: (name: string) => Promise<any> diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index ad709c9c77..73f4e8bd11 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -321,7 +321,7 @@ export default abstract class DataFetch< abstract getData(): Promise<{ rows: Row[] info?: any - hasNextPage: boolean + hasNextPage?: boolean cursor?: any error?: any }> diff --git a/packages/frontend-core/src/fetch/TableFetch.ts b/packages/frontend-core/src/fetch/TableFetch.ts index 08dc111b28..3ea6063882 100644 --- a/packages/frontend-core/src/fetch/TableFetch.ts +++ b/packages/frontend-core/src/fetch/TableFetch.ts @@ -11,7 +11,7 @@ export default class TableFetch extends DataFetch<UITable, Table> { } } - async getDefinition(datasource: UITable | null) { + async getDefinition(datasource: UITable) { if (!datasource?.tableId) { return null } diff --git a/packages/frontend-core/src/fetch/UserFetch.ts b/packages/frontend-core/src/fetch/UserFetch.ts index be73793768..571825cd3b 100644 --- a/packages/frontend-core/src/fetch/UserFetch.ts +++ b/packages/frontend-core/src/fetch/UserFetch.ts @@ -2,7 +2,7 @@ import { get } from "svelte/store" import DataFetch from "./DataFetch.js" import { TableNames } from "../constants" import { utils } from "@budibase/shared-core" -import { BasicOperator, SearchUsersRequest, Table } from "@budibase/types" +import { BasicOperator, Table } from "@budibase/types" import { APIClient } from "../api/types.js" export default class UserFetch extends DataFetch<{ tableId: string }, {}> { From b420fda5249943dbe52eb2c6485cdd09bec71046 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 2 Jan 2025 16:25:37 +0100 Subject: [PATCH 019/131] Convert ViewFetch --- .../src/fetch/{ViewFetch.js => ViewFetch.ts} | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) rename packages/frontend-core/src/fetch/{ViewFetch.js => ViewFetch.ts} (50%) diff --git a/packages/frontend-core/src/fetch/ViewFetch.js b/packages/frontend-core/src/fetch/ViewFetch.ts similarity index 50% rename from packages/frontend-core/src/fetch/ViewFetch.js rename to packages/frontend-core/src/fetch/ViewFetch.ts index eb89f9b67a..65fad90564 100644 --- a/packages/frontend-core/src/fetch/ViewFetch.js +++ b/packages/frontend-core/src/fetch/ViewFetch.ts @@ -1,7 +1,25 @@ +import { Table, View } from "@budibase/types" import DataFetch from "./DataFetch.js" -export default class ViewFetch extends DataFetch { - getSchema(datasource, definition) { +type ViewV1 = View & { name: string } + +export default class ViewFetch extends DataFetch<ViewV1, Table> { + async getDefinition(datasource: ViewV1) { + if (!datasource?.tableId) { + return null + } + try { + return await this.API.fetchTableDefinition(datasource.tableId) + } catch (error: any) { + this.store.update(state => ({ + ...state, + error, + })) + return null + } + } + + getSchema(datasource: ViewV1, definition: Table) { return definition?.views?.[datasource.name]?.schema } From 2b863cca61ac9096fcf132e8af6c36e46ddac163 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 2 Jan 2025 16:27:43 +0100 Subject: [PATCH 020/131] Fix imports --- packages/client/src/utils/schema.js | 6 +++--- packages/frontend-core/src/fetch/CustomFetch.js | 2 +- packages/frontend-core/src/fetch/FieldFetch.js | 2 +- packages/frontend-core/src/fetch/GroupUserFetch.js | 2 +- packages/frontend-core/src/fetch/NestedProviderFetch.js | 2 +- packages/frontend-core/src/fetch/QueryFetch.js | 2 +- packages/frontend-core/src/fetch/RelationshipFetch.js | 2 +- packages/frontend-core/src/fetch/TableFetch.ts | 2 +- packages/frontend-core/src/fetch/UserFetch.ts | 2 +- packages/frontend-core/src/fetch/ViewFetch.ts | 2 +- packages/frontend-core/src/fetch/ViewV2Fetch.ts | 2 +- 11 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/client/src/utils/schema.js b/packages/client/src/utils/schema.js index ec1cef53ce..9e8b9f3d4c 100644 --- a/packages/client/src/utils/schema.js +++ b/packages/client/src/utils/schema.js @@ -1,12 +1,12 @@ import { API } from "api" -import TableFetch from "@budibase/frontend-core/src/fetch/TableFetch.js" -import ViewFetch from "@budibase/frontend-core/src/fetch/ViewFetch.js" +import TableFetch from "@budibase/frontend-core/src/fetch/TableFetch" +import ViewFetch from "@budibase/frontend-core/src/fetch/ViewFetch" import QueryFetch from "@budibase/frontend-core/src/fetch/QueryFetch.js" import RelationshipFetch from "@budibase/frontend-core/src/fetch/RelationshipFetch.js" import NestedProviderFetch from "@budibase/frontend-core/src/fetch/NestedProviderFetch.js" import FieldFetch from "@budibase/frontend-core/src/fetch/FieldFetch.js" import JSONArrayFetch from "@budibase/frontend-core/src/fetch/JSONArrayFetch.js" -import ViewV2Fetch from "@budibase/frontend-core/src/fetch/ViewV2Fetch.js" +import ViewV2Fetch from "@budibase/frontend-core/src/fetch/ViewV2Fetch" import QueryArrayFetch from "@budibase/frontend-core/src/fetch/QueryArrayFetch" /** diff --git a/packages/frontend-core/src/fetch/CustomFetch.js b/packages/frontend-core/src/fetch/CustomFetch.js index fc62d790e2..39d3bd6f4c 100644 --- a/packages/frontend-core/src/fetch/CustomFetch.js +++ b/packages/frontend-core/src/fetch/CustomFetch.js @@ -1,4 +1,4 @@ -import DataFetch from "./DataFetch.js" +import DataFetch from "./DataFetch" export default class CustomFetch extends DataFetch { // Gets the correct Budibase type for a JS value diff --git a/packages/frontend-core/src/fetch/FieldFetch.js b/packages/frontend-core/src/fetch/FieldFetch.js index 9402a45a83..7c9d715761 100644 --- a/packages/frontend-core/src/fetch/FieldFetch.js +++ b/packages/frontend-core/src/fetch/FieldFetch.js @@ -1,4 +1,4 @@ -import DataFetch from "./DataFetch.js" +import DataFetch from "./DataFetch" export default class FieldFetch extends DataFetch { async getDefinition(datasource) { diff --git a/packages/frontend-core/src/fetch/GroupUserFetch.js b/packages/frontend-core/src/fetch/GroupUserFetch.js index bd2cf264c5..e40b565728 100644 --- a/packages/frontend-core/src/fetch/GroupUserFetch.js +++ b/packages/frontend-core/src/fetch/GroupUserFetch.js @@ -1,5 +1,5 @@ import { get } from "svelte/store" -import DataFetch from "./DataFetch.js" +import DataFetch from "./DataFetch" import { TableNames } from "../constants" export default class GroupUserFetch extends DataFetch { diff --git a/packages/frontend-core/src/fetch/NestedProviderFetch.js b/packages/frontend-core/src/fetch/NestedProviderFetch.js index 0a08b00cb4..06c74cb6c4 100644 --- a/packages/frontend-core/src/fetch/NestedProviderFetch.js +++ b/packages/frontend-core/src/fetch/NestedProviderFetch.js @@ -1,4 +1,4 @@ -import DataFetch from "./DataFetch.js" +import DataFetch from "./DataFetch" export default class NestedProviderFetch extends DataFetch { async getDefinition(datasource) { diff --git a/packages/frontend-core/src/fetch/QueryFetch.js b/packages/frontend-core/src/fetch/QueryFetch.js index 9fac9704d3..4f7954c068 100644 --- a/packages/frontend-core/src/fetch/QueryFetch.js +++ b/packages/frontend-core/src/fetch/QueryFetch.js @@ -1,4 +1,4 @@ -import DataFetch from "./DataFetch.js" +import DataFetch from "./DataFetch" import { Helpers } from "@budibase/bbui" import { get } from "svelte/store" diff --git a/packages/frontend-core/src/fetch/RelationshipFetch.js b/packages/frontend-core/src/fetch/RelationshipFetch.js index 0dec535724..61f0207558 100644 --- a/packages/frontend-core/src/fetch/RelationshipFetch.js +++ b/packages/frontend-core/src/fetch/RelationshipFetch.js @@ -1,4 +1,4 @@ -import DataFetch from "./DataFetch.js" +import DataFetch from "./DataFetch" export default class RelationshipFetch extends DataFetch { async getData() { diff --git a/packages/frontend-core/src/fetch/TableFetch.ts b/packages/frontend-core/src/fetch/TableFetch.ts index 3ea6063882..58ad554d6a 100644 --- a/packages/frontend-core/src/fetch/TableFetch.ts +++ b/packages/frontend-core/src/fetch/TableFetch.ts @@ -1,5 +1,5 @@ import { get } from "svelte/store" -import DataFetch from "./DataFetch.js" +import DataFetch from "./DataFetch" import { SortOrder, Table, UITable } from "@budibase/types" export default class TableFetch extends DataFetch<UITable, Table> { diff --git a/packages/frontend-core/src/fetch/UserFetch.ts b/packages/frontend-core/src/fetch/UserFetch.ts index 571825cd3b..a0f95afe3e 100644 --- a/packages/frontend-core/src/fetch/UserFetch.ts +++ b/packages/frontend-core/src/fetch/UserFetch.ts @@ -1,5 +1,5 @@ import { get } from "svelte/store" -import DataFetch from "./DataFetch.js" +import DataFetch from "./DataFetch" import { TableNames } from "../constants" import { utils } from "@budibase/shared-core" import { BasicOperator, Table } from "@budibase/types" diff --git a/packages/frontend-core/src/fetch/ViewFetch.ts b/packages/frontend-core/src/fetch/ViewFetch.ts index 65fad90564..cbdd1d425b 100644 --- a/packages/frontend-core/src/fetch/ViewFetch.ts +++ b/packages/frontend-core/src/fetch/ViewFetch.ts @@ -1,5 +1,5 @@ import { Table, View } from "@budibase/types" -import DataFetch from "./DataFetch.js" +import DataFetch from "./DataFetch" type ViewV1 = View & { name: string } diff --git a/packages/frontend-core/src/fetch/ViewV2Fetch.ts b/packages/frontend-core/src/fetch/ViewV2Fetch.ts index d3b607a171..7973dbf298 100644 --- a/packages/frontend-core/src/fetch/ViewV2Fetch.ts +++ b/packages/frontend-core/src/fetch/ViewV2Fetch.ts @@ -1,5 +1,5 @@ import { SortOrder, UIView, ViewV2, ViewV2Type } from "@budibase/types" -import DataFetch from "./DataFetch.js" +import DataFetch from "./DataFetch" import { get } from "svelte/store" import { helpers } from "@budibase/shared-core" From 543660dc2e9448730eb89e71402d5f753acf16b3 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 3 Jan 2025 12:34:36 +0100 Subject: [PATCH 021/131] Convert fieldFetch --- .../fetch/{FieldFetch.js => FieldFetch.ts} | 30 +++++++++++++++---- .../frontend-core/src/fetch/JSONArrayFetch.js | 2 +- .../src/fetch/QueryArrayFetch.js | 2 +- packages/frontend-core/src/fetch/index.ts | 2 +- 4 files changed, 28 insertions(+), 8 deletions(-) rename packages/frontend-core/src/fetch/{FieldFetch.js => FieldFetch.ts} (52%) diff --git a/packages/frontend-core/src/fetch/FieldFetch.js b/packages/frontend-core/src/fetch/FieldFetch.ts similarity index 52% rename from packages/frontend-core/src/fetch/FieldFetch.js rename to packages/frontend-core/src/fetch/FieldFetch.ts index 7c9d715761..7f0dfdf331 100644 --- a/packages/frontend-core/src/fetch/FieldFetch.js +++ b/packages/frontend-core/src/fetch/FieldFetch.ts @@ -1,9 +1,29 @@ +import { Row, TableSchema } from "@budibase/types" import DataFetch from "./DataFetch" -export default class FieldFetch extends DataFetch { - async getDefinition(datasource) { +interface FieldDatasource { + fieldType: "attachment" | "array" + value: string[] | Row[] +} + +function isArrayOfStrings(value: string[] | Row[]): value is string[] { + return Array.isArray(value) && !!value[0] && typeof value[0] !== "object" +} + +export default class FieldFetch extends DataFetch< + FieldDatasource, + { schema?: Record<string, { type: string }> } +> { + getSchema( + _datasource: FieldDatasource, + definition: { schema?: TableSchema } + ) { + return definition?.schema + } + + async getDefinition(datasource: FieldDatasource) { // Field sources have their schema statically defined - let schema + let schema: Record<string, { type: string }> | undefined if (datasource.fieldType === "attachment") { schema = { url: { @@ -28,8 +48,8 @@ export default class FieldFetch extends DataFetch { // These sources will be available directly from context const data = datasource?.value || [] - let rows - if (Array.isArray(data) && data[0] && typeof data[0] !== "object") { + let rows: Row[] + if (isArrayOfStrings(data)) { rows = data.map(value => ({ value })) } else { rows = data diff --git a/packages/frontend-core/src/fetch/JSONArrayFetch.js b/packages/frontend-core/src/fetch/JSONArrayFetch.js index ab2af3e2c7..f7de74a4b8 100644 --- a/packages/frontend-core/src/fetch/JSONArrayFetch.js +++ b/packages/frontend-core/src/fetch/JSONArrayFetch.js @@ -1,4 +1,4 @@ -import FieldFetch from "./FieldFetch.js" +import FieldFetch from "./FieldFetch" import { getJSONArrayDatasourceSchema } from "../utils/json" export default class JSONArrayFetch extends FieldFetch { diff --git a/packages/frontend-core/src/fetch/QueryArrayFetch.js b/packages/frontend-core/src/fetch/QueryArrayFetch.js index 0b36b640a6..222697b78f 100644 --- a/packages/frontend-core/src/fetch/QueryArrayFetch.js +++ b/packages/frontend-core/src/fetch/QueryArrayFetch.js @@ -1,4 +1,4 @@ -import FieldFetch from "./FieldFetch.js" +import FieldFetch from "./FieldFetch" import { getJSONArrayDatasourceSchema, generateQueryArraySchemas, diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index a08748a77e..502b2d3162 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -4,7 +4,7 @@ import ViewV2Fetch from "./ViewV2Fetch.js" import QueryFetch from "./QueryFetch.js" import RelationshipFetch from "./RelationshipFetch.js" import NestedProviderFetch from "./NestedProviderFetch.js" -import FieldFetch from "./FieldFetch.js" +import FieldFetch from "./FieldFetch" import JSONArrayFetch from "./JSONArrayFetch.js" import UserFetch from "./UserFetch.js" import GroupUserFetch from "./GroupUserFetch.js" From 97b0883c6b8b1da5ee8912fe7a861129e932b2f2 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 3 Jan 2025 12:34:43 +0100 Subject: [PATCH 022/131] Convert fieldFetch --- packages/client/src/utils/schema.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/utils/schema.js b/packages/client/src/utils/schema.js index 9e8b9f3d4c..2b5dae0acf 100644 --- a/packages/client/src/utils/schema.js +++ b/packages/client/src/utils/schema.js @@ -4,7 +4,7 @@ import ViewFetch from "@budibase/frontend-core/src/fetch/ViewFetch" import QueryFetch from "@budibase/frontend-core/src/fetch/QueryFetch.js" import RelationshipFetch from "@budibase/frontend-core/src/fetch/RelationshipFetch.js" import NestedProviderFetch from "@budibase/frontend-core/src/fetch/NestedProviderFetch.js" -import FieldFetch from "@budibase/frontend-core/src/fetch/FieldFetch.js" +import FieldFetch from "@budibase/frontend-core/src/fetch/FieldFetch" import JSONArrayFetch from "@budibase/frontend-core/src/fetch/JSONArrayFetch.js" import ViewV2Fetch from "@budibase/frontend-core/src/fetch/ViewV2Fetch" import QueryArrayFetch from "@budibase/frontend-core/src/fetch/QueryArrayFetch" From 550cdd7268d102117dcba98ec210383b6e65c5b4 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 3 Jan 2025 12:48:35 +0100 Subject: [PATCH 023/131] Fix field selector on datasource picker --- .../controls/DataSourceSelect/DataSourceSelect.svelte | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/builder/src/components/design/settings/controls/DataSourceSelect/DataSourceSelect.svelte b/packages/builder/src/components/design/settings/controls/DataSourceSelect/DataSourceSelect.svelte index e7a30e68dd..b23ef5348d 100644 --- a/packages/builder/src/components/design/settings/controls/DataSourceSelect/DataSourceSelect.svelte +++ b/packages/builder/src/components/design/settings/controls/DataSourceSelect/DataSourceSelect.svelte @@ -43,7 +43,6 @@ export let showDataProviders = true const dispatch = createEventDispatcher() - const arrayTypes = ["attachment", "array"] let anchorRight, dropdownRight let drawer @@ -116,8 +115,11 @@ } }) $: fields = bindings - .filter(x => arrayTypes.includes(x.fieldSchema?.type)) - .filter(x => x.fieldSchema?.tableId != null) + .filter( + x => + x.fieldSchema?.type === "attachment" || + (x.fieldSchema?.type === "array" && x.tableId) + ) .map(binding => { const { providerId, readableBinding, runtimeBinding } = binding const { name, type, tableId } = binding.fieldSchema From 1d661c6290cc54c5b5fd27669fee530375a5a64b Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 3 Jan 2025 15:37:04 +0100 Subject: [PATCH 024/131] Fix sort issue (with broken ts) --- packages/frontend-core/src/fetch/TableFetch.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend-core/src/fetch/TableFetch.ts b/packages/frontend-core/src/fetch/TableFetch.ts index 58ad554d6a..344ce30e54 100644 --- a/packages/frontend-core/src/fetch/TableFetch.ts +++ b/packages/frontend-core/src/fetch/TableFetch.ts @@ -42,7 +42,7 @@ export default class TableFetch extends DataFetch<UITable, Table> { query: query ?? undefined, limit, sort: sortColumn, - sortOrder: sortOrder ?? SortOrder.ASCENDING, + sortOrder: sortOrder?.toLowerCase() ?? SortOrder.ASCENDING, sortType, paginate, bookmark: cursor, From af22eb30a60da04b39dc44834974a3bfcc06ac68 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 10:14:18 +0100 Subject: [PATCH 025/131] Type relationship fetch --- packages/client/src/utils/schema.js | 2 +- .../src/fetch/RelationshipFetch.js | 20 -------- .../src/fetch/RelationshipFetch.ts | 50 +++++++++++++++++++ packages/frontend-core/src/fetch/index.ts | 2 +- 4 files changed, 52 insertions(+), 22 deletions(-) delete mode 100644 packages/frontend-core/src/fetch/RelationshipFetch.js create mode 100644 packages/frontend-core/src/fetch/RelationshipFetch.ts diff --git a/packages/client/src/utils/schema.js b/packages/client/src/utils/schema.js index 2b5dae0acf..87859f1ef8 100644 --- a/packages/client/src/utils/schema.js +++ b/packages/client/src/utils/schema.js @@ -2,7 +2,7 @@ import { API } from "api" import TableFetch from "@budibase/frontend-core/src/fetch/TableFetch" import ViewFetch from "@budibase/frontend-core/src/fetch/ViewFetch" import QueryFetch from "@budibase/frontend-core/src/fetch/QueryFetch.js" -import RelationshipFetch from "@budibase/frontend-core/src/fetch/RelationshipFetch.js" +import RelationshipFetch from "@budibase/frontend-core/src/fetch/RelationshipFetch" import NestedProviderFetch from "@budibase/frontend-core/src/fetch/NestedProviderFetch.js" import FieldFetch from "@budibase/frontend-core/src/fetch/FieldFetch" import JSONArrayFetch from "@budibase/frontend-core/src/fetch/JSONArrayFetch.js" diff --git a/packages/frontend-core/src/fetch/RelationshipFetch.js b/packages/frontend-core/src/fetch/RelationshipFetch.js deleted file mode 100644 index 61f0207558..0000000000 --- a/packages/frontend-core/src/fetch/RelationshipFetch.js +++ /dev/null @@ -1,20 +0,0 @@ -import DataFetch from "./DataFetch" - -export default class RelationshipFetch extends DataFetch { - async getData() { - const { datasource } = this.options - if (!datasource?.rowId || !datasource?.rowTableId) { - return { rows: [] } - } - try { - const res = await this.API.fetchRelationshipData( - datasource.rowTableId, - datasource.rowId, - datasource.fieldName - ) - return { rows: res } - } catch (error) { - return { rows: [] } - } - } -} diff --git a/packages/frontend-core/src/fetch/RelationshipFetch.ts b/packages/frontend-core/src/fetch/RelationshipFetch.ts new file mode 100644 index 0000000000..ab50285b4b --- /dev/null +++ b/packages/frontend-core/src/fetch/RelationshipFetch.ts @@ -0,0 +1,50 @@ +import { Table } from "@budibase/types" +import DataFetch from "./DataFetch" + +interface RelationshipDatasource { + tableId: string + rowId: string + rowTableId: string + fieldName: string +} + +export default class RelationshipFetch extends DataFetch< + RelationshipDatasource, + Table +> { + getSchema(_datasource: any, definition: any) { + return definition?.schema + } + + async getDefinition(datasource: RelationshipDatasource) { + if (!datasource?.tableId) { + return null + } + try { + return await this.API.fetchTableDefinition(datasource.tableId) + } catch (error: any) { + this.store.update(state => ({ + ...state, + error, + })) + return null + } + } + + async getData() { + const { datasource } = this.options + if (!datasource?.rowId || !datasource?.rowTableId) { + return { rows: [] } + } + try { + const res = await this.API.fetchRelationshipData( + datasource.rowTableId, + datasource.rowId, + datasource.fieldName + ) + return { rows: res } + } catch (error) { + return { rows: [] } + } + } +} diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index 502b2d3162..0fbda7a414 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -2,7 +2,7 @@ import TableFetch from "./TableFetch.js" import ViewFetch from "./ViewFetch.js" import ViewV2Fetch from "./ViewV2Fetch.js" import QueryFetch from "./QueryFetch.js" -import RelationshipFetch from "./RelationshipFetch.js" +import RelationshipFetch from "./RelationshipFetch" import NestedProviderFetch from "./NestedProviderFetch.js" import FieldFetch from "./FieldFetch" import JSONArrayFetch from "./JSONArrayFetch.js" From 8d748338739adc9cb66bf80497a0e8723ac4a32c Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 11:02:09 +0100 Subject: [PATCH 026/131] Type groupUserFetch --- packages/frontend-core/src/fetch/DataFetch.ts | 35 ++++++++++++------- .../{GroupUserFetch.js => GroupUserFetch.ts} | 25 +++++++++++-- packages/frontend-core/src/fetch/index.ts | 2 +- 3 files changed, 45 insertions(+), 17 deletions(-) rename packages/frontend-core/src/fetch/{GroupUserFetch.js => GroupUserFetch.ts} (67%) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index 73f4e8bd11..ef05f7b8ef 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -16,22 +16,23 @@ import { APIClient } from "../api/types" const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils -interface DataFetchStore<T> { +interface DataFetchStore<TDefinition, TQuery> { rows: Row[] info: null schema: TableSchema | null loading: boolean loaded: boolean - query: SearchFilters | null + query: TQuery pageNumber: number cursor: null cursors: any[] resetKey: number error: null - definition?: T | null + definition?: TDefinition | null } -interface DataFetchDerivedStore<T> extends DataFetchStore<T> { +interface DataFetchDerivedStore<TDefinition, TQuery> + extends DataFetchStore<TDefinition, TQuery> { hasNextPage: boolean hasPrevPage: boolean supportsSearch: boolean @@ -39,6 +40,13 @@ interface DataFetchDerivedStore<T> extends DataFetchStore<T> { supportsPagination: boolean } +interface DataFetchParams<TDatasource, TQuery = SearchFilters | undefined> { + API: APIClient + datasource: TDatasource + query: TQuery + options?: {} +} + /** * Parent class which handles the implementation of fetching data from an * internal table or datasource plus. @@ -46,7 +54,8 @@ interface DataFetchDerivedStore<T> extends DataFetchStore<T> { */ export default abstract class DataFetch< TDatasource extends {}, - TDefinition extends {} + TDefinition extends {}, + TQuery extends {} = SearchFilters > { API: APIClient features: { @@ -59,7 +68,7 @@ export default abstract class DataFetch< limit: number // Search config filter: UISearchFilter | LegacyFilter[] | null - query: SearchFilters | null + query: TQuery // Sorting config sortColumn: string | null sortOrder: SortOrder @@ -71,14 +80,14 @@ export default abstract class DataFetch< clientSideSorting: boolean clientSideLimiting: boolean } - store: Writable<DataFetchStore<TDefinition>> - derivedStore: Readable<DataFetchDerivedStore<TDefinition>> + store: Writable<DataFetchStore<TDefinition, TQuery>> + derivedStore: Readable<DataFetchDerivedStore<TDefinition, TQuery>> /** * Constructs a new DataFetch instance. * @param opts the fetch options */ - constructor(opts: { API: APIClient; datasource: TDatasource; options?: {} }) { + constructor(opts: DataFetchParams<TDatasource, TQuery>) { // Feature flags this.features = { supportsSearch: false, @@ -93,7 +102,7 @@ export default abstract class DataFetch< // Search config filter: null, - query: null, + query: opts.query, // Sorting config sortColumn: null, @@ -116,7 +125,7 @@ export default abstract class DataFetch< schema: null, loading: false, loaded: false, - query: null, + query: opts.query, pageNumber: 0, cursor: null, cursors: [], @@ -247,7 +256,7 @@ export default abstract class DataFetch< // Build the query let query = this.options.query if (!query) { - query = buildQuery(filter ?? undefined) + query = buildQuery(filter ?? undefined) as TQuery } // Update store @@ -485,7 +494,7 @@ export default abstract class DataFetch< * @param state the current store state * @return {boolean} whether there is a next page of data or not */ - hasNextPage(state: DataFetchStore<TDefinition>): boolean { + hasNextPage(state: DataFetchStore<TDefinition, TQuery>): boolean { return state.cursors[state.pageNumber + 1] != null } diff --git a/packages/frontend-core/src/fetch/GroupUserFetch.js b/packages/frontend-core/src/fetch/GroupUserFetch.ts similarity index 67% rename from packages/frontend-core/src/fetch/GroupUserFetch.js rename to packages/frontend-core/src/fetch/GroupUserFetch.ts index e40b565728..77b5e1de43 100644 --- a/packages/frontend-core/src/fetch/GroupUserFetch.js +++ b/packages/frontend-core/src/fetch/GroupUserFetch.ts @@ -1,9 +1,23 @@ import { get } from "svelte/store" import DataFetch from "./DataFetch" import { TableNames } from "../constants" +import { APIClient } from "../api/types" -export default class GroupUserFetch extends DataFetch { - constructor(opts) { +interface GroupUserQuery { + groupId: string + emailSearch: string +} + +export default class GroupUserFetch extends DataFetch< + any, + any, + GroupUserQuery +> { + constructor(opts: { + API: APIClient + datasource: any + query: GroupUserQuery + }) { super({ ...opts, datasource: { @@ -12,6 +26,10 @@ export default class GroupUserFetch extends DataFetch { }) } + getSchema(_datasource: any, definition: any) { + return definition?.schema + } + determineFeatureFlags() { return { supportsSearch: true, @@ -28,11 +46,12 @@ export default class GroupUserFetch extends DataFetch { async getData() { const { query, cursor } = get(this.store) + try { const res = await this.API.getGroupUsers({ id: query.groupId, emailSearch: query.emailSearch, - bookmark: cursor, + bookmark: cursor ?? undefined, }) return { diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index 0fbda7a414..ef471aa8e4 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -7,7 +7,7 @@ import NestedProviderFetch from "./NestedProviderFetch.js" import FieldFetch from "./FieldFetch" import JSONArrayFetch from "./JSONArrayFetch.js" import UserFetch from "./UserFetch.js" -import GroupUserFetch from "./GroupUserFetch.js" +import GroupUserFetch from "./GroupUserFetch" import CustomFetch from "./CustomFetch.js" import QueryArrayFetch from "./QueryArrayFetch.js" import { Table, UIDatasource, UIFetchAPI } from "@budibase/types" From c52dd568723ae2c1933a1bb40eb91f5782421f39 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 11:17:42 +0100 Subject: [PATCH 027/131] Fix userFetch query --- packages/frontend-core/src/fetch/UserFetch.ts | 36 ++++++++++++++----- packages/shared-core/src/utils.ts | 4 ++- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/packages/frontend-core/src/fetch/UserFetch.ts b/packages/frontend-core/src/fetch/UserFetch.ts index a0f95afe3e..e276af3592 100644 --- a/packages/frontend-core/src/fetch/UserFetch.ts +++ b/packages/frontend-core/src/fetch/UserFetch.ts @@ -2,11 +2,30 @@ import { get } from "svelte/store" import DataFetch from "./DataFetch" import { TableNames } from "../constants" import { utils } from "@budibase/shared-core" -import { BasicOperator, Table } from "@budibase/types" +import { + BasicOperator, + SearchFilters, + SearchUsersRequest, + Table, +} from "@budibase/types" import { APIClient } from "../api/types.js" -export default class UserFetch extends DataFetch<{ tableId: string }, {}> { - constructor(opts: { API: APIClient; datasource: Table; options?: {} }) { +interface UserFetchQuery { + appId: string + paginated: boolean +} + +export default class UserFetch extends DataFetch< + { tableId: string }, + {}, + UserFetchQuery +> { + constructor(opts: { + API: APIClient + datasource: Table + options?: {} + query: UserFetchQuery + }) { super({ ...opts, datasource: { @@ -38,13 +57,14 @@ export default class UserFetch extends DataFetch<{ tableId: string }, {}> { const { cursor, query } = get(this.store) // Convert old format to new one - we now allow use of the lucene format - const { appId, paginated, ...rest } = query || ({} as any) // TODO - const finalQuery = utils.isSupportedUserSearch(rest) - ? query - : { [BasicOperator.EMPTY]: { email: true } } // TODO: check + const { appId, paginated, ...rest } = query || {} + + const finalQuery: SearchFilters = utils.isSupportedUserSearch(rest) + ? rest + : { [BasicOperator.EMPTY]: { email: true } } try { - const opts = { + const opts: SearchUsersRequest = { bookmark: cursor ?? undefined, query: finalQuery ?? undefined, appId: appId, diff --git a/packages/shared-core/src/utils.ts b/packages/shared-core/src/utils.ts index e2c40a8849..fac8fa61ee 100644 --- a/packages/shared-core/src/utils.ts +++ b/packages/shared-core/src/utils.ts @@ -109,7 +109,9 @@ export function trimOtherProps(object: any, allowedProps: string[]) { return result } -export function isSupportedUserSearch(query: SearchFilters) { +export function isSupportedUserSearch( + query: SearchFilters +): query is SearchFilters { const allowed = [ { op: BasicOperator.STRING, key: "email" }, { op: BasicOperator.EQUAL, key: "_id" }, From dedf2e58594841fc62cc10897f5208614fec479c Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 11:22:22 +0100 Subject: [PATCH 028/131] Fix types --- packages/frontend-core/src/fetch/index.ts | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index ef471aa8e4..4d5b2f147a 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -10,7 +10,8 @@ import UserFetch from "./UserFetch.js" import GroupUserFetch from "./GroupUserFetch" import CustomFetch from "./CustomFetch.js" import QueryArrayFetch from "./QueryArrayFetch.js" -import { Table, UIDatasource, UIFetchAPI } from "@budibase/types" +import { Table, UIDatasource } from "@budibase/types" +import { APIClient } from "../api/types.js" const DataFetchMap = { table: TableFetch, @@ -30,15 +31,7 @@ const DataFetchMap = { } // Constructs a new fetch model for a certain datasource -export const fetchData = ({ - API, - datasource, - options, -}: { - API: UIFetchAPI - datasource: UIDatasource - options: {} -}) => { +export const fetchData = ({ API, datasource, options }: any) => { const Fetch = DataFetchMap[datasource?.type as keyof typeof DataFetchMap] || TableFetch return new Fetch({ API, datasource, ...options }) @@ -50,14 +43,14 @@ const createEmptyFetchInstance = ({ API, datasource, }: { - API: UIFetchAPI - datasource: UIDatasource + API: APIClient + datasource: any }) => { const handler = DataFetchMap[datasource?.type as keyof typeof DataFetchMap] if (!handler) { return null } - return new handler({ API }) + return new handler({ API, datasource: null as any, query: null as any }) } // Fetches the definition of any type of datasource @@ -65,7 +58,7 @@ export const getDatasourceDefinition = async ({ API, datasource, }: { - API: UIFetchAPI + API: APIClient datasource: UIDatasource }) => { const instance = createEmptyFetchInstance({ API, datasource }) @@ -78,7 +71,7 @@ export const getDatasourceSchema = ({ datasource, definition, }: { - API: UIFetchAPI + API: APIClient datasource: UIDatasource definition: Table }) => { From 0eddc1a00eb1819800471eabed148adbefbebb96 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 11:47:10 +0100 Subject: [PATCH 029/131] Type QueryFetch --- packages/client/src/utils/schema.js | 2 +- packages/frontend-core/src/fetch/DataFetch.ts | 11 ++++++-- .../fetch/{QueryFetch.js => QueryFetch.ts} | 28 +++++++++++++++---- packages/frontend-core/src/fetch/index.ts | 2 +- 4 files changed, 34 insertions(+), 9 deletions(-) rename packages/frontend-core/src/fetch/{QueryFetch.js => QueryFetch.ts} (82%) diff --git a/packages/client/src/utils/schema.js b/packages/client/src/utils/schema.js index 87859f1ef8..3a1b5acaaa 100644 --- a/packages/client/src/utils/schema.js +++ b/packages/client/src/utils/schema.js @@ -1,7 +1,7 @@ import { API } from "api" import TableFetch from "@budibase/frontend-core/src/fetch/TableFetch" import ViewFetch from "@budibase/frontend-core/src/fetch/ViewFetch" -import QueryFetch from "@budibase/frontend-core/src/fetch/QueryFetch.js" +import QueryFetch from "@budibase/frontend-core/src/fetch/QueryFetch" import RelationshipFetch from "@budibase/frontend-core/src/fetch/RelationshipFetch" import NestedProviderFetch from "@budibase/frontend-core/src/fetch/NestedProviderFetch.js" import FieldFetch from "@budibase/frontend-core/src/fetch/FieldFetch" diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index ef05f7b8ef..60cb96cf8c 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -40,7 +40,10 @@ interface DataFetchDerivedStore<TDefinition, TQuery> supportsPagination: boolean } -interface DataFetchParams<TDatasource, TQuery = SearchFilters | undefined> { +export interface DataFetchParams< + TDatasource, + TQuery = SearchFilters | undefined +> { API: APIClient datasource: TDatasource query: TQuery @@ -411,7 +414,11 @@ export default abstract class DataFetch< * Determine the feature flag for this datasource definition * @param definition */ - determineFeatureFlags(_definition: TDefinition | null) { + determineFeatureFlags(_definition: TDefinition | null): { + supportsPagination: boolean + supportsSearch?: boolean + supportsSort?: boolean + } { return { supportsSearch: false, supportsSort: false, diff --git a/packages/frontend-core/src/fetch/QueryFetch.js b/packages/frontend-core/src/fetch/QueryFetch.ts similarity index 82% rename from packages/frontend-core/src/fetch/QueryFetch.js rename to packages/frontend-core/src/fetch/QueryFetch.ts index 4f7954c068..c671781fcd 100644 --- a/packages/frontend-core/src/fetch/QueryFetch.js +++ b/packages/frontend-core/src/fetch/QueryFetch.ts @@ -1,9 +1,21 @@ import DataFetch from "./DataFetch" import { Helpers } from "@budibase/bbui" +import { Query } from "@budibase/types" import { get } from "svelte/store" -export default class QueryFetch extends DataFetch { - determineFeatureFlags(definition) { +interface QueryDatasource { + _id: string + fields: any + queryParams: any + parameters: any +} + +export default class QueryFetch extends DataFetch<QueryDatasource, Query> { + getSchema(_datasource: any, definition: any) { + return definition?.schema + } + + determineFeatureFlags(definition: Query) { const supportsPagination = !!definition?.fields?.pagination?.type && !!definition?.fields?.pagination?.location && @@ -11,7 +23,7 @@ export default class QueryFetch extends DataFetch { return { supportsPagination } } - async getDefinition(datasource) { + async getDefinition(datasource: QueryDatasource) { if (!datasource?._id) { return null } @@ -48,9 +60,15 @@ export default class QueryFetch extends DataFetch { } // Add pagination to query if supported - let queryPayload = { parameters } + const queryPayload: { + parameters: any + pagination?: { + page: number | null + limit: number + } + } = { parameters } if (paginate && supportsPagination) { - const requestCursor = type === "page" ? parseInt(cursor || 1) : cursor + const requestCursor = type === "page" ? parseInt(cursor || "1") : cursor queryPayload.pagination = { page: requestCursor, limit } } diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index 4d5b2f147a..fc029324ba 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -1,7 +1,7 @@ import TableFetch from "./TableFetch.js" import ViewFetch from "./ViewFetch.js" import ViewV2Fetch from "./ViewV2Fetch.js" -import QueryFetch from "./QueryFetch.js" +import QueryFetch from "./QueryFetch" import RelationshipFetch from "./RelationshipFetch" import NestedProviderFetch from "./NestedProviderFetch.js" import FieldFetch from "./FieldFetch" From 8c0e7a12d681c3c95aaba9f61e3b2cad166ad2b4 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 11:54:42 +0100 Subject: [PATCH 030/131] Remove anys --- packages/server/src/api/controllers/query/index.ts | 4 ++-- packages/types/src/documents/app/query.ts | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/controllers/query/index.ts b/packages/server/src/api/controllers/query/index.ts index 7084d065fa..b522008cad 100644 --- a/packages/server/src/api/controllers/query/index.ts +++ b/packages/server/src/api/controllers/query/index.ts @@ -355,7 +355,7 @@ async function execute( ExecuteQueryRequest, ExecuteV2QueryResponse | ExecuteV1QueryResponse >, - opts: any = { rowsOnly: false, isAutomation: false } + opts = { rowsOnly: false, isAutomation: false } ) { const db = context.getAppDB() @@ -416,7 +416,7 @@ export async function executeV1( export async function executeV2( ctx: UserCtx<ExecuteQueryRequest, ExecuteV2QueryResponse> ) { - return execute(ctx, { rowsOnly: false }) + return execute(ctx, { rowsOnly: false, isAutomation: false }) } export async function executeV2AsAutomation( diff --git a/packages/types/src/documents/app/query.ts b/packages/types/src/documents/app/query.ts index a545ca144e..43e7563b31 100644 --- a/packages/types/src/documents/app/query.ts +++ b/packages/types/src/documents/app/query.ts @@ -1,4 +1,5 @@ import { Document } from "../document" +import { Row } from "./row" export interface QuerySchema { name?: string @@ -29,7 +30,7 @@ export interface QueryParameter { } export interface QueryResponse { - rows: any[] + rows: Row[] keys: string[] info: any extra: any From f4ed2176c96bd62cad45afcdbc68cd40eac29a2a Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 12:06:37 +0100 Subject: [PATCH 031/131] Fix types --- packages/frontend-core/src/fetch/QueryFetch.ts | 2 +- packages/types/src/api/web/app/query.ts | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/frontend-core/src/fetch/QueryFetch.ts b/packages/frontend-core/src/fetch/QueryFetch.ts index c671781fcd..d85b5ffbcd 100644 --- a/packages/frontend-core/src/fetch/QueryFetch.ts +++ b/packages/frontend-core/src/fetch/QueryFetch.ts @@ -83,7 +83,7 @@ export default class QueryFetch extends DataFetch<QueryDatasource, Query> { if (paginate && supportsPagination) { if (type === "page") { // For "page number" pagination, increment the existing page number - nextCursor = queryPayload.pagination.page + 1 + nextCursor = queryPayload.pagination!.page! + 1 hasNextPage = data?.length === limit && limit > 0 } else { // For "cursor" pagination, the cursor should be in the response diff --git a/packages/types/src/api/web/app/query.ts b/packages/types/src/api/web/app/query.ts index 302f0d03e5..75cc37e1a9 100644 --- a/packages/types/src/api/web/app/query.ts +++ b/packages/types/src/api/web/app/query.ts @@ -40,6 +40,10 @@ export interface ExecuteQueryRequest { export type ExecuteV1QueryResponse = Record<string, any>[] export interface ExecuteV2QueryResponse { data: Record<string, any>[] + pagination?: { + page: number + cursor: string + } } export interface DeleteQueryResponse { From 89485aa9d11aca31163e97e971a9579959b5e3e8 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 12:39:40 +0100 Subject: [PATCH 032/131] Extend types --- packages/frontend-core/src/fetch/FieldFetch.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/frontend-core/src/fetch/FieldFetch.ts b/packages/frontend-core/src/fetch/FieldFetch.ts index 7f0dfdf331..5e89d6e881 100644 --- a/packages/frontend-core/src/fetch/FieldFetch.ts +++ b/packages/frontend-core/src/fetch/FieldFetch.ts @@ -1,7 +1,8 @@ import { Row, TableSchema } from "@budibase/types" import DataFetch from "./DataFetch" -interface FieldDatasource { +export interface FieldDatasource { + tableId: string fieldType: "attachment" | "array" value: string[] | Row[] } From 364e997fc2f338d051cfd32c36f3a066c4323375 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 12:41:37 +0100 Subject: [PATCH 033/131] Type jsonArrayFetch --- packages/client/src/utils/schema.js | 2 +- packages/frontend-core/src/fetch/FieldFetch.ts | 12 +++++++++--- .../fetch/{JSONArrayFetch.js => JSONArrayFetch.ts} | 9 ++++++--- packages/frontend-core/src/fetch/index.ts | 2 +- 4 files changed, 17 insertions(+), 8 deletions(-) rename packages/frontend-core/src/fetch/{JSONArrayFetch.js => JSONArrayFetch.ts} (63%) diff --git a/packages/client/src/utils/schema.js b/packages/client/src/utils/schema.js index 3a1b5acaaa..f9cd7dbc60 100644 --- a/packages/client/src/utils/schema.js +++ b/packages/client/src/utils/schema.js @@ -5,7 +5,7 @@ import QueryFetch from "@budibase/frontend-core/src/fetch/QueryFetch" import RelationshipFetch from "@budibase/frontend-core/src/fetch/RelationshipFetch" import NestedProviderFetch from "@budibase/frontend-core/src/fetch/NestedProviderFetch.js" import FieldFetch from "@budibase/frontend-core/src/fetch/FieldFetch" -import JSONArrayFetch from "@budibase/frontend-core/src/fetch/JSONArrayFetch.js" +import JSONArrayFetch from "@budibase/frontend-core/src/fetch/JSONArrayFetch" import ViewV2Fetch from "@budibase/frontend-core/src/fetch/ViewV2Fetch" import QueryArrayFetch from "@budibase/frontend-core/src/fetch/QueryArrayFetch" diff --git a/packages/frontend-core/src/fetch/FieldFetch.ts b/packages/frontend-core/src/fetch/FieldFetch.ts index 5e89d6e881..6809dee00e 100644 --- a/packages/frontend-core/src/fetch/FieldFetch.ts +++ b/packages/frontend-core/src/fetch/FieldFetch.ts @@ -7,13 +7,17 @@ export interface FieldDatasource { value: string[] | Row[] } +export interface FieldDefinition { + schema?: Record<string, { type: string }> | null +} + function isArrayOfStrings(value: string[] | Row[]): value is string[] { return Array.isArray(value) && !!value[0] && typeof value[0] !== "object" } export default class FieldFetch extends DataFetch< FieldDatasource, - { schema?: Record<string, { type: string }> } + FieldDefinition > { getSchema( _datasource: FieldDatasource, @@ -22,9 +26,11 @@ export default class FieldFetch extends DataFetch< return definition?.schema } - async getDefinition(datasource: FieldDatasource) { + async getDefinition( + datasource: FieldDatasource + ): Promise<FieldDefinition | null> { // Field sources have their schema statically defined - let schema: Record<string, { type: string }> | undefined + let schema if (datasource.fieldType === "attachment") { schema = { url: { diff --git a/packages/frontend-core/src/fetch/JSONArrayFetch.js b/packages/frontend-core/src/fetch/JSONArrayFetch.ts similarity index 63% rename from packages/frontend-core/src/fetch/JSONArrayFetch.js rename to packages/frontend-core/src/fetch/JSONArrayFetch.ts index f7de74a4b8..a254bc3ae4 100644 --- a/packages/frontend-core/src/fetch/JSONArrayFetch.js +++ b/packages/frontend-core/src/fetch/JSONArrayFetch.ts @@ -1,13 +1,16 @@ -import FieldFetch from "./FieldFetch" +import FieldFetch, { FieldDatasource } from "./FieldFetch" import { getJSONArrayDatasourceSchema } from "../utils/json" export default class JSONArrayFetch extends FieldFetch { - async getDefinition(datasource) { + async getDefinition(datasource: FieldDatasource) { // JSON arrays need their table definitions fetched. // We can then extract their schema as a subset of the table schema. try { const table = await this.API.fetchTableDefinition(datasource.tableId) - const schema = getJSONArrayDatasourceSchema(table?.schema, datasource) + const schema: Record<string, any> | null = getJSONArrayDatasourceSchema( + table?.schema, + datasource + ) return { schema } } catch (error) { return null diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index fc029324ba..3976adc3d9 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -5,7 +5,7 @@ import QueryFetch from "./QueryFetch" import RelationshipFetch from "./RelationshipFetch" import NestedProviderFetch from "./NestedProviderFetch.js" import FieldFetch from "./FieldFetch" -import JSONArrayFetch from "./JSONArrayFetch.js" +import JSONArrayFetch from "./JSONArrayFetch" import UserFetch from "./UserFetch.js" import GroupUserFetch from "./GroupUserFetch" import CustomFetch from "./CustomFetch.js" From 690a442e61106ad250cd7838772ec6072f229722 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 13:08:17 +0100 Subject: [PATCH 034/131] convert QueryArrayFetch --- .../fetch/{QueryArrayFetch.js => QueryArrayFetch.ts} | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) rename packages/frontend-core/src/fetch/{QueryArrayFetch.js => QueryArrayFetch.ts} (68%) diff --git a/packages/frontend-core/src/fetch/QueryArrayFetch.js b/packages/frontend-core/src/fetch/QueryArrayFetch.ts similarity index 68% rename from packages/frontend-core/src/fetch/QueryArrayFetch.js rename to packages/frontend-core/src/fetch/QueryArrayFetch.ts index 222697b78f..15f86ae7fc 100644 --- a/packages/frontend-core/src/fetch/QueryArrayFetch.js +++ b/packages/frontend-core/src/fetch/QueryArrayFetch.ts @@ -1,11 +1,11 @@ -import FieldFetch from "./FieldFetch" +import FieldFetch, { FieldDatasource } from "./FieldFetch" import { getJSONArrayDatasourceSchema, generateQueryArraySchemas, } from "../utils/json" export default class QueryArrayFetch extends FieldFetch { - async getDefinition(datasource) { + async getDefinition(datasource: FieldDatasource) { if (!datasource?.tableId) { return null } @@ -17,7 +17,11 @@ export default class QueryArrayFetch extends FieldFetch { table?.schema, table?.nestedSchemaFields ) - return { schema: getJSONArrayDatasourceSchema(schema, datasource) } + const result: { + schema: Record<string, any> | null + } = { schema: getJSONArrayDatasourceSchema(schema, datasource) } + + return result } catch (error) { return null } From f76ec8d2c9602960e84fe8858dcfa5156c3595c1 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 13:32:44 +0100 Subject: [PATCH 035/131] Fix types --- packages/server/src/threads/definitions.ts | 5 ++++- packages/types/src/documents/app/query.ts | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/server/src/threads/definitions.ts b/packages/server/src/threads/definitions.ts index 85e546280d..44a76a60a3 100644 --- a/packages/server/src/threads/definitions.ts +++ b/packages/server/src/threads/definitions.ts @@ -3,7 +3,10 @@ import { Datasource, Row, Query } from "@budibase/types" export type WorkerCallback = (error: any, response?: any) => void export interface QueryEvent - extends Omit<Query, "datasourceId" | "name" | "parameters" | "readable"> { + extends Omit< + Query, + "datasourceId" | "name" | "parameters" | "readable" | "nestedSchemaFields" + > { appId?: string datasource: Datasource pagination?: any diff --git a/packages/types/src/documents/app/query.ts b/packages/types/src/documents/app/query.ts index 43e7563b31..477a7787b9 100644 --- a/packages/types/src/documents/app/query.ts +++ b/packages/types/src/documents/app/query.ts @@ -14,6 +14,7 @@ export interface Query extends Document { fields: RestQueryFields | any transformer: string | null schema: Record<string, QuerySchema | string> + nestedSchemaFields: Record<string, Record<string, QuerySchema | string>> readable: boolean queryVerb: string // flag to state whether the default bindings are empty strings (old behaviour) or null From 91300c54e903bf4f925a778011bb756092873286 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 13:34:42 +0100 Subject: [PATCH 036/131] Type nestedProvider --- packages/client/src/utils/schema.js | 2 +- .../{NestedProviderFetch.js => NestedProviderFetch.ts} | 8 ++++++-- packages/frontend-core/src/fetch/index.ts | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) rename packages/frontend-core/src/fetch/{NestedProviderFetch.js => NestedProviderFetch.ts} (69%) diff --git a/packages/client/src/utils/schema.js b/packages/client/src/utils/schema.js index f9cd7dbc60..ffab142cf3 100644 --- a/packages/client/src/utils/schema.js +++ b/packages/client/src/utils/schema.js @@ -3,7 +3,7 @@ import TableFetch from "@budibase/frontend-core/src/fetch/TableFetch" import ViewFetch from "@budibase/frontend-core/src/fetch/ViewFetch" import QueryFetch from "@budibase/frontend-core/src/fetch/QueryFetch" import RelationshipFetch from "@budibase/frontend-core/src/fetch/RelationshipFetch" -import NestedProviderFetch from "@budibase/frontend-core/src/fetch/NestedProviderFetch.js" +import NestedProviderFetch from "@budibase/frontend-core/src/fetch/NestedProviderFetch" import FieldFetch from "@budibase/frontend-core/src/fetch/FieldFetch" import JSONArrayFetch from "@budibase/frontend-core/src/fetch/JSONArrayFetch" import ViewV2Fetch from "@budibase/frontend-core/src/fetch/ViewV2Fetch" diff --git a/packages/frontend-core/src/fetch/NestedProviderFetch.js b/packages/frontend-core/src/fetch/NestedProviderFetch.ts similarity index 69% rename from packages/frontend-core/src/fetch/NestedProviderFetch.js rename to packages/frontend-core/src/fetch/NestedProviderFetch.ts index 06c74cb6c4..a442e7ec07 100644 --- a/packages/frontend-core/src/fetch/NestedProviderFetch.js +++ b/packages/frontend-core/src/fetch/NestedProviderFetch.ts @@ -1,7 +1,11 @@ import DataFetch from "./DataFetch" -export default class NestedProviderFetch extends DataFetch { - async getDefinition(datasource) { +export default class NestedProviderFetch extends DataFetch<any, any> { + getSchema(_datasource: any, definition: any) { + return definition?.schema + } + + async getDefinition(datasource: any) { // Nested providers should already have exposed their own schema return { schema: datasource?.value?.schema, diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index 3976adc3d9..dd03e715ed 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -3,7 +3,7 @@ import ViewFetch from "./ViewFetch.js" import ViewV2Fetch from "./ViewV2Fetch.js" import QueryFetch from "./QueryFetch" import RelationshipFetch from "./RelationshipFetch" -import NestedProviderFetch from "./NestedProviderFetch.js" +import NestedProviderFetch from "./NestedProviderFetch" import FieldFetch from "./FieldFetch" import JSONArrayFetch from "./JSONArrayFetch" import UserFetch from "./UserFetch.js" From bf02515ff0c546440ce9a9c16e0f5e7cfe304459 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 13:42:51 +0100 Subject: [PATCH 037/131] Fix types --- .../frontend-core/src/components/grid/stores/rows.ts | 7 ++++--- packages/frontend-core/src/fetch/DataFetch.ts | 11 +++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/frontend-core/src/components/grid/stores/rows.ts b/packages/frontend-core/src/components/grid/stores/rows.ts index d6b80df885..b9c9b3fe1e 100644 --- a/packages/frontend-core/src/components/grid/stores/rows.ts +++ b/packages/frontend-core/src/components/grid/stores/rows.ts @@ -10,9 +10,10 @@ import { import { tick } from "svelte" import { Helpers } from "@budibase/bbui" import { sleep } from "../../../utils/utils" -import { FieldType, Row, UIFetchAPI, UIRow } from "@budibase/types" +import { FieldType, Row, UIRow } from "@budibase/types" import { getRelatedTableValues } from "../../../utils" import { Store as StoreContext } from "." +import DataFetch from "../../../fetch/DataFetch" interface IndexedUIRow extends UIRow { __idx: number @@ -20,7 +21,7 @@ interface IndexedUIRow extends UIRow { interface RowStore { rows: Writable<UIRow[]> - fetch: Writable<UIFetchAPI | null> + fetch: Writable<DataFetch<any, any, any> | null> loaded: Writable<boolean> refreshing: Writable<boolean> loading: Writable<boolean> @@ -225,7 +226,7 @@ export const createActions = (context: StoreContext): RowActionStore => { }) // Subscribe to changes of this fetch model - unsubscribe = newFetch.subscribe(async ($fetch: UIFetchAPI) => { + unsubscribe = newFetch.subscribe(async $fetch => { if ($fetch.error) { // Present a helpful error to the user let message = "An unknown error occurred" diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index 60cb96cf8c..68cff15040 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -26,8 +26,11 @@ interface DataFetchStore<TDefinition, TQuery> { pageNumber: number cursor: null cursors: any[] - resetKey: number - error: null + resetKey: string + error: { + message: string + status: number + } | null definition?: TDefinition | null } @@ -132,7 +135,7 @@ export default abstract class DataFetch< pageNumber: 0, cursor: null, cursors: [], - resetKey: Math.random(), + resetKey: Math.random().toString(), error: null, }) @@ -284,7 +287,7 @@ export default abstract class DataFetch< info: page.info, cursors: paginate && page.hasNextPage ? [null, page.cursor] : [null], error: page.error, - resetKey: Math.random(), + resetKey: Math.random().toString(), })) } From 30cf6ff2ad2332cca6d54a9375c1ebcf6b842ef4 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 13:46:00 +0100 Subject: [PATCH 038/131] Type customFetch --- .../fetch/{CustomFetch.js => CustomFetch.ts} | 22 +++++++++++-------- packages/frontend-core/src/fetch/index.ts | 2 +- 2 files changed, 14 insertions(+), 10 deletions(-) rename packages/frontend-core/src/fetch/{CustomFetch.js => CustomFetch.ts} (89%) diff --git a/packages/frontend-core/src/fetch/CustomFetch.js b/packages/frontend-core/src/fetch/CustomFetch.ts similarity index 89% rename from packages/frontend-core/src/fetch/CustomFetch.js rename to packages/frontend-core/src/fetch/CustomFetch.ts index 39d3bd6f4c..64739ad6f1 100644 --- a/packages/frontend-core/src/fetch/CustomFetch.js +++ b/packages/frontend-core/src/fetch/CustomFetch.ts @@ -1,8 +1,12 @@ import DataFetch from "./DataFetch" -export default class CustomFetch extends DataFetch { +export default class CustomFetch extends DataFetch<any, any> { + getSchema(_datasource: any, definition: any) { + return definition?.schema + } + // Gets the correct Budibase type for a JS value - getType(value) { + getType(value: any) { if (value == null) { return "string" } @@ -22,7 +26,7 @@ export default class CustomFetch extends DataFetch { } // Parses the custom data into an array format - parseCustomData(data) { + parseCustomData(data: any) { if (!data) { return [] } @@ -55,7 +59,7 @@ export default class CustomFetch extends DataFetch { } // Enriches the custom data to ensure the structure and format is usable - enrichCustomData(data) { + enrichCustomData(data: any[]) { if (!data?.length) { return [] } @@ -72,7 +76,7 @@ export default class CustomFetch extends DataFetch { // Try parsing strings if (typeof value === "string") { const split = value.split(",").map(x => x.trim()) - let obj = {} + let obj: Record<string, string> = {} for (let i = 0; i < split.length; i++) { const suffix = i === 0 ? "" : ` ${i + 1}` const key = `Value${suffix}` @@ -87,13 +91,13 @@ export default class CustomFetch extends DataFetch { } // Extracts and parses the custom data from the datasource definition - getCustomData(datasource) { + getCustomData(datasource: { data: any }) { return this.enrichCustomData(this.parseCustomData(datasource?.data)) } - async getDefinition(datasource) { + async getDefinition(datasource: any) { // Try and work out the schema from the array provided - let schema = {} + let schema: any = {} const data = this.getCustomData(datasource) if (!data?.length) { return { schema } @@ -107,7 +111,7 @@ export default class CustomFetch extends DataFetch { } if (!schema[key]) { let type = this.getType(datum[key]) - let constraints = {} + let constraints: any = {} // Determine whether we should render text columns as options instead if (type === "string") { diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index dd03e715ed..3afeec7684 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -8,7 +8,7 @@ import FieldFetch from "./FieldFetch" import JSONArrayFetch from "./JSONArrayFetch" import UserFetch from "./UserFetch.js" import GroupUserFetch from "./GroupUserFetch" -import CustomFetch from "./CustomFetch.js" +import CustomFetch from "./CustomFetch" import QueryArrayFetch from "./QueryArrayFetch.js" import { Table, UIDatasource } from "@budibase/types" import { APIClient } from "../api/types.js" From 090429fc56b242f931b61275377ad77e09ed7e0b Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 14:01:55 +0100 Subject: [PATCH 039/131] Fix sort order enum type --- packages/frontend-core/src/fetch/DataFetch.ts | 5 +++++ packages/frontend-core/src/fetch/TableFetch.ts | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index 68cff15040..0dcda4e150 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -253,9 +253,14 @@ export default abstract class DataFetch< ) { this.options.sortType = SortType.NUMBER } + // If no sort order, default to ascending if (!this.options.sortOrder) { this.options.sortOrder = SortOrder.ASCENDING + } else { + // Ensure sortOrder matches the enum + this.options.sortOrder = + this.options.sortOrder.toLowerCase() as SortOrder } } diff --git a/packages/frontend-core/src/fetch/TableFetch.ts b/packages/frontend-core/src/fetch/TableFetch.ts index 344ce30e54..58ad554d6a 100644 --- a/packages/frontend-core/src/fetch/TableFetch.ts +++ b/packages/frontend-core/src/fetch/TableFetch.ts @@ -42,7 +42,7 @@ export default class TableFetch extends DataFetch<UITable, Table> { query: query ?? undefined, limit, sort: sortColumn, - sortOrder: sortOrder?.toLowerCase() ?? SortOrder.ASCENDING, + sortOrder: sortOrder ?? SortOrder.ASCENDING, sortType, paginate, bookmark: cursor, From dcf52b5dc96670ad0cfc31750f059110cf42b231 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 15:33:05 +0100 Subject: [PATCH 040/131] Fix typings --- packages/frontend-core/src/components/grid/stores/config.ts | 2 +- .../frontend-core/src/components/grid/stores/datasource.ts | 6 +++--- packages/frontend-core/src/fetch/index.ts | 4 ++-- packages/types/src/ui/stores/grid/view.ts | 3 +-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/frontend-core/src/components/grid/stores/config.ts b/packages/frontend-core/src/components/grid/stores/config.ts index e334b58495..2ddaf1b65c 100644 --- a/packages/frontend-core/src/components/grid/stores/config.ts +++ b/packages/frontend-core/src/components/grid/stores/config.ts @@ -69,7 +69,7 @@ export const deriveStores = (context: StoreContext): ConfigDerivedStore => { } // Disable features for non DS+ - if (!["table", "viewV2"].includes(type)) { + if (type && !["table", "viewV2"].includes(type)) { config.canAddRows = false config.canEditRows = false config.canDeleteRows = false diff --git a/packages/frontend-core/src/components/grid/stores/datasource.ts b/packages/frontend-core/src/components/grid/stores/datasource.ts index 74101701ed..fe8ff60fd3 100644 --- a/packages/frontend-core/src/components/grid/stores/datasource.ts +++ b/packages/frontend-core/src/components/grid/stores/datasource.ts @@ -74,7 +74,7 @@ export const deriveStores = (context: StoreContext): DerivedDatasourceStore => { let schema: Record<string, UIFieldSchema> = getDatasourceSchema({ API, datasource: get(datasource), - definition: $definition, + definition: $definition ?? undefined, }) if (!schema) { return null @@ -136,7 +136,7 @@ export const deriveStores = (context: StoreContext): DerivedDatasourceStore => { if (type === "viewV2" && $definition?.type === ViewV2Type.CALCULATION) { return false } - return ["table", "viewV2", "link"].includes(type) + return !!type && ["table", "viewV2", "link"].includes(type) } ) @@ -186,7 +186,7 @@ export const createActions = (context: StoreContext): ActionDatasourceStore => { API, datasource: get(datasource), }) - definition.set(def) + definition.set((def as any) ?? null) } // Saves the datasource definition diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index 3afeec7684..4f4eafe8e8 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -10,7 +10,7 @@ import UserFetch from "./UserFetch.js" import GroupUserFetch from "./GroupUserFetch" import CustomFetch from "./CustomFetch" import QueryArrayFetch from "./QueryArrayFetch.js" -import { Table, UIDatasource } from "@budibase/types" +import { TableSchema, UIDatasource } from "@budibase/types" import { APIClient } from "../api/types.js" const DataFetchMap = { @@ -73,7 +73,7 @@ export const getDatasourceSchema = ({ }: { API: APIClient datasource: UIDatasource - definition: Table + definition?: { schema?: TableSchema } }) => { const instance = createEmptyFetchInstance({ API, datasource }) return instance?.getSchema(datasource, definition) diff --git a/packages/types/src/ui/stores/grid/view.ts b/packages/types/src/ui/stores/grid/view.ts index 270faaa160..f81cc34aaf 100644 --- a/packages/types/src/ui/stores/grid/view.ts +++ b/packages/types/src/ui/stores/grid/view.ts @@ -1,7 +1,6 @@ import { ViewV2 } from "@budibase/types" import { UIFieldSchema } from "./table" -export interface UIView extends Omit<ViewV2, "type"> { - type: string +export interface UIView extends ViewV2 { schema: Record<string, UIFieldSchema> } From ed2e35dea06285cdf7677418cb676a3add90189f Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Tue, 7 Jan 2025 16:02:34 +0100 Subject: [PATCH 041/131] Dry --- .../src/components/grid/stores/datasource.ts | 4 ++-- packages/frontend-core/src/fetch/CustomFetch.ts | 4 ---- packages/frontend-core/src/fetch/DataFetch.ts | 12 +++++++----- packages/frontend-core/src/fetch/FieldFetch.ts | 9 +-------- packages/frontend-core/src/fetch/GroupUserFetch.ts | 4 ---- .../frontend-core/src/fetch/NestedProviderFetch.ts | 4 ---- packages/frontend-core/src/fetch/QueryFetch.ts | 4 ---- .../frontend-core/src/fetch/RelationshipFetch.ts | 4 ---- packages/frontend-core/src/fetch/TableFetch.ts | 4 ---- packages/frontend-core/src/fetch/UserFetch.ts | 4 ---- packages/frontend-core/src/fetch/ViewV2Fetch.ts | 4 ---- 11 files changed, 10 insertions(+), 47 deletions(-) diff --git a/packages/frontend-core/src/components/grid/stores/datasource.ts b/packages/frontend-core/src/components/grid/stores/datasource.ts index fe8ff60fd3..4c20e9493f 100644 --- a/packages/frontend-core/src/components/grid/stores/datasource.ts +++ b/packages/frontend-core/src/components/grid/stores/datasource.ts @@ -71,7 +71,7 @@ export const deriveStores = (context: StoreContext): DerivedDatasourceStore => { } = context const schema = derived(definition, $definition => { - let schema: Record<string, UIFieldSchema> = getDatasourceSchema({ + const schema: Record<string, any> | null | undefined = getDatasourceSchema({ API, datasource: get(datasource), definition: $definition ?? undefined, @@ -82,7 +82,7 @@ export const deriveStores = (context: StoreContext): DerivedDatasourceStore => { // Ensure schema is configured as objects. // Certain datasources like queries use primitives. - Object.keys(schema || {}).forEach(key => { + Object.keys(schema).forEach(key => { if (typeof schema[key] !== "object") { schema[key] = { name: key, type: schema[key] } } diff --git a/packages/frontend-core/src/fetch/CustomFetch.ts b/packages/frontend-core/src/fetch/CustomFetch.ts index 64739ad6f1..9db9a935a5 100644 --- a/packages/frontend-core/src/fetch/CustomFetch.ts +++ b/packages/frontend-core/src/fetch/CustomFetch.ts @@ -1,10 +1,6 @@ import DataFetch from "./DataFetch" export default class CustomFetch extends DataFetch<any, any> { - getSchema(_datasource: any, definition: any) { - return definition?.schema - } - // Gets the correct Budibase type for a JS value getType(value: any) { if (value == null) { diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index 0dcda4e150..f34f6ddeb7 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -60,7 +60,10 @@ export interface DataFetchParams< */ export default abstract class DataFetch< TDatasource extends {}, - TDefinition extends {}, + TDefinition extends { + schema?: Record<string, any> | null + primaryDisplay?: string + }, TQuery extends {} = SearchFilters > { API: APIClient @@ -361,10 +364,9 @@ export default abstract class DataFetch< * @param definition the datasource definition * @return {object} the schema */ - abstract getSchema( - datasource: TDatasource | null, - definition: TDefinition | null - ): any + getSchema(_datasource: TDatasource | null, definition: TDefinition | null) { + return definition?.schema + } /** * Enriches a datasource schema with nested fields and ensures the structure diff --git a/packages/frontend-core/src/fetch/FieldFetch.ts b/packages/frontend-core/src/fetch/FieldFetch.ts index 6809dee00e..636fb63f3d 100644 --- a/packages/frontend-core/src/fetch/FieldFetch.ts +++ b/packages/frontend-core/src/fetch/FieldFetch.ts @@ -1,4 +1,4 @@ -import { Row, TableSchema } from "@budibase/types" +import { Row } from "@budibase/types" import DataFetch from "./DataFetch" export interface FieldDatasource { @@ -19,13 +19,6 @@ export default class FieldFetch extends DataFetch< FieldDatasource, FieldDefinition > { - getSchema( - _datasource: FieldDatasource, - definition: { schema?: TableSchema } - ) { - return definition?.schema - } - async getDefinition( datasource: FieldDatasource ): Promise<FieldDefinition | null> { diff --git a/packages/frontend-core/src/fetch/GroupUserFetch.ts b/packages/frontend-core/src/fetch/GroupUserFetch.ts index 77b5e1de43..0c5ac7486d 100644 --- a/packages/frontend-core/src/fetch/GroupUserFetch.ts +++ b/packages/frontend-core/src/fetch/GroupUserFetch.ts @@ -26,10 +26,6 @@ export default class GroupUserFetch extends DataFetch< }) } - getSchema(_datasource: any, definition: any) { - return definition?.schema - } - determineFeatureFlags() { return { supportsSearch: true, diff --git a/packages/frontend-core/src/fetch/NestedProviderFetch.ts b/packages/frontend-core/src/fetch/NestedProviderFetch.ts index a442e7ec07..71eb5177db 100644 --- a/packages/frontend-core/src/fetch/NestedProviderFetch.ts +++ b/packages/frontend-core/src/fetch/NestedProviderFetch.ts @@ -1,10 +1,6 @@ import DataFetch from "./DataFetch" export default class NestedProviderFetch extends DataFetch<any, any> { - getSchema(_datasource: any, definition: any) { - return definition?.schema - } - async getDefinition(datasource: any) { // Nested providers should already have exposed their own schema return { diff --git a/packages/frontend-core/src/fetch/QueryFetch.ts b/packages/frontend-core/src/fetch/QueryFetch.ts index d85b5ffbcd..dec7cd2183 100644 --- a/packages/frontend-core/src/fetch/QueryFetch.ts +++ b/packages/frontend-core/src/fetch/QueryFetch.ts @@ -11,10 +11,6 @@ interface QueryDatasource { } export default class QueryFetch extends DataFetch<QueryDatasource, Query> { - getSchema(_datasource: any, definition: any) { - return definition?.schema - } - determineFeatureFlags(definition: Query) { const supportsPagination = !!definition?.fields?.pagination?.type && diff --git a/packages/frontend-core/src/fetch/RelationshipFetch.ts b/packages/frontend-core/src/fetch/RelationshipFetch.ts index ab50285b4b..7b6e93fbcc 100644 --- a/packages/frontend-core/src/fetch/RelationshipFetch.ts +++ b/packages/frontend-core/src/fetch/RelationshipFetch.ts @@ -12,10 +12,6 @@ export default class RelationshipFetch extends DataFetch< RelationshipDatasource, Table > { - getSchema(_datasource: any, definition: any) { - return definition?.schema - } - async getDefinition(datasource: RelationshipDatasource) { if (!datasource?.tableId) { return null diff --git a/packages/frontend-core/src/fetch/TableFetch.ts b/packages/frontend-core/src/fetch/TableFetch.ts index 58ad554d6a..48a3413716 100644 --- a/packages/frontend-core/src/fetch/TableFetch.ts +++ b/packages/frontend-core/src/fetch/TableFetch.ts @@ -26,10 +26,6 @@ export default class TableFetch extends DataFetch<UITable, Table> { } } - getSchema(_datasource: UITable | null, definition: Table | null) { - return definition?.schema - } - async getData() { const { datasource, limit, sortColumn, sortOrder, sortType, paginate } = this.options diff --git a/packages/frontend-core/src/fetch/UserFetch.ts b/packages/frontend-core/src/fetch/UserFetch.ts index e276af3592..43efc7767f 100644 --- a/packages/frontend-core/src/fetch/UserFetch.ts +++ b/packages/frontend-core/src/fetch/UserFetch.ts @@ -48,10 +48,6 @@ export default class UserFetch extends DataFetch< } } - getSchema(_datasource: any, definition: Table | null) { - return definition?.schema - } - async getData() { const { limit, paginate } = this.options const { cursor, query } = get(this.store) diff --git a/packages/frontend-core/src/fetch/ViewV2Fetch.ts b/packages/frontend-core/src/fetch/ViewV2Fetch.ts index 7973dbf298..d541a692b6 100644 --- a/packages/frontend-core/src/fetch/ViewV2Fetch.ts +++ b/packages/frontend-core/src/fetch/ViewV2Fetch.ts @@ -12,10 +12,6 @@ export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> { } } - getSchema(_datasource: UIView, definition: ViewV2) { - return definition?.schema - } - async getDefinition(datasource: UIView | null): Promise<ViewV2 | null> { if (!datasource?.id) { return null From a1ac0ac0b08c8595b78d031ca9299722fe1ef5a9 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 12:22:17 +0100 Subject: [PATCH 042/131] Fix type --- packages/types/src/documents/app/query.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/types/src/documents/app/query.ts b/packages/types/src/documents/app/query.ts index 477a7787b9..d287a5b2c3 100644 --- a/packages/types/src/documents/app/query.ts +++ b/packages/types/src/documents/app/query.ts @@ -14,7 +14,7 @@ export interface Query extends Document { fields: RestQueryFields | any transformer: string | null schema: Record<string, QuerySchema | string> - nestedSchemaFields: Record<string, Record<string, QuerySchema | string>> + nestedSchemaFields?: Record<string, Record<string, QuerySchema | string>> readable: boolean queryVerb: string // flag to state whether the default bindings are empty strings (old behaviour) or null From 022df7cda41710e276f98aa12a1bfd240c8b1dba Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 12:28:36 +0100 Subject: [PATCH 043/131] Lint --- packages/frontend-core/src/fetch/ViewFetch.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend-core/src/fetch/ViewFetch.ts b/packages/frontend-core/src/fetch/ViewFetch.ts index cbdd1d425b..fe891b9e66 100644 --- a/packages/frontend-core/src/fetch/ViewFetch.ts +++ b/packages/frontend-core/src/fetch/ViewFetch.ts @@ -34,7 +34,7 @@ export default class ViewFetch extends DataFetch<ViewV1, Table> { }) return { rows: res || [] } } catch (error) { - console.error(error) + console.error(error, { datasource }) return { rows: [] } } } From d465f7e0574297e8bb01a41b6cd757986fc82a41 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 12:42:33 +0100 Subject: [PATCH 044/131] Type anys --- packages/frontend-core/src/api/views.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/frontend-core/src/api/views.ts b/packages/frontend-core/src/api/views.ts index 083a3890e8..83f7e97df0 100644 --- a/packages/frontend-core/src/api/views.ts +++ b/packages/frontend-core/src/api/views.ts @@ -3,7 +3,15 @@ import { BaseAPIClient } from "./types" export interface ViewEndpoints { // Missing request or response types - fetchViewData: (name: string, opts?: any) => Promise<Row[]> + fetchViewData: ( + name: string, + opts: { + calculation?: string + field?: string + groupBy?: string + tableId: string + } + ) => Promise<Row[]> exportView: (name: string, format: string) => Promise<any> saveView: (view: any) => Promise<any> deleteView: (name: string) => Promise<any> @@ -20,7 +28,7 @@ export const buildViewEndpoints = (API: BaseAPIClient): ViewEndpoints => ({ fetchViewData: async (name, { field, groupBy, calculation }) => { const params = new URLSearchParams() if (calculation) { - params.set("field", field) + params.set("field", field!) params.set("calculation", calculation) } if (groupBy) { From 7d7c27fa928640d075ce9233932b2761e6d740d7 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 12:49:17 +0100 Subject: [PATCH 045/131] Simplify determineFeatureFlags --- packages/frontend-core/src/fetch/DataFetch.ts | 9 ++++----- packages/frontend-core/src/fetch/GroupUserFetch.ts | 2 +- packages/frontend-core/src/fetch/QueryFetch.ts | 3 ++- packages/frontend-core/src/fetch/TableFetch.ts | 2 +- packages/frontend-core/src/fetch/UserFetch.ts | 2 +- packages/frontend-core/src/fetch/ViewV2Fetch.ts | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index f34f6ddeb7..a7ed7237ba 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -216,7 +216,7 @@ export default abstract class DataFetch< const definition = await this.getDefinition(datasource) // Determine feature flags - const features = this.determineFeatureFlags(definition) + const features = await this.determineFeatureFlags() this.features = { supportsSearch: !!features?.supportsSearch, supportsSort: !!features?.supportsSort, @@ -421,14 +421,13 @@ export default abstract class DataFetch< } /** - * Determine the feature flag for this datasource definition - * @param definition + * Determine the feature flag for this datasource */ - determineFeatureFlags(_definition: TDefinition | null): { + async determineFeatureFlags(): Promise<{ supportsPagination: boolean supportsSearch?: boolean supportsSort?: boolean - } { + }> { return { supportsSearch: false, supportsSort: false, diff --git a/packages/frontend-core/src/fetch/GroupUserFetch.ts b/packages/frontend-core/src/fetch/GroupUserFetch.ts index 0c5ac7486d..2f5cacd1a2 100644 --- a/packages/frontend-core/src/fetch/GroupUserFetch.ts +++ b/packages/frontend-core/src/fetch/GroupUserFetch.ts @@ -26,7 +26,7 @@ export default class GroupUserFetch extends DataFetch< }) } - determineFeatureFlags() { + async determineFeatureFlags() { return { supportsSearch: true, supportsSort: false, diff --git a/packages/frontend-core/src/fetch/QueryFetch.ts b/packages/frontend-core/src/fetch/QueryFetch.ts index dec7cd2183..a6ddcd8f01 100644 --- a/packages/frontend-core/src/fetch/QueryFetch.ts +++ b/packages/frontend-core/src/fetch/QueryFetch.ts @@ -11,7 +11,8 @@ interface QueryDatasource { } export default class QueryFetch extends DataFetch<QueryDatasource, Query> { - determineFeatureFlags(definition: Query) { + async determineFeatureFlags() { + const definition = await this.getDefinition(this.options.datasource) const supportsPagination = !!definition?.fields?.pagination?.type && !!definition?.fields?.pagination?.location && diff --git a/packages/frontend-core/src/fetch/TableFetch.ts b/packages/frontend-core/src/fetch/TableFetch.ts index 48a3413716..433de69b59 100644 --- a/packages/frontend-core/src/fetch/TableFetch.ts +++ b/packages/frontend-core/src/fetch/TableFetch.ts @@ -3,7 +3,7 @@ import DataFetch from "./DataFetch" import { SortOrder, Table, UITable } from "@budibase/types" export default class TableFetch extends DataFetch<UITable, Table> { - determineFeatureFlags() { + async determineFeatureFlags() { return { supportsSearch: true, supportsSort: true, diff --git a/packages/frontend-core/src/fetch/UserFetch.ts b/packages/frontend-core/src/fetch/UserFetch.ts index 43efc7767f..199dacde00 100644 --- a/packages/frontend-core/src/fetch/UserFetch.ts +++ b/packages/frontend-core/src/fetch/UserFetch.ts @@ -34,7 +34,7 @@ export default class UserFetch extends DataFetch< }) } - determineFeatureFlags() { + async determineFeatureFlags() { return { supportsSearch: true, supportsSort: false, diff --git a/packages/frontend-core/src/fetch/ViewV2Fetch.ts b/packages/frontend-core/src/fetch/ViewV2Fetch.ts index d541a692b6..74ad08f2f4 100644 --- a/packages/frontend-core/src/fetch/ViewV2Fetch.ts +++ b/packages/frontend-core/src/fetch/ViewV2Fetch.ts @@ -4,7 +4,7 @@ import { get } from "svelte/store" import { helpers } from "@budibase/shared-core" export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> { - determineFeatureFlags() { + async determineFeatureFlags() { return { supportsSearch: true, supportsSort: true, From f053438a649694233f7849e94df0bdd22ab60859 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 12:55:26 +0100 Subject: [PATCH 046/131] Clean classes --- packages/frontend-core/src/fetch/DataFetch.ts | 18 +++++++----------- .../frontend-core/src/fetch/ViewV2Fetch.ts | 5 +---- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index a7ed7237ba..3e2b0d7dcf 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -223,13 +223,13 @@ export default abstract class DataFetch< supportsPagination: paginate && !!features?.supportsPagination, } - // Fetch and enrich schema - let schema = this.getSchema(datasource, definition) ?? null - schema = this.enrichSchema(schema) - if (!schema) { + if (!definition?.schema) { return } + // Fetch and enrich schema + const schema = this.enrichSchema(definition.schema) + // If an invalid sort column is specified, delete it if (this.options.sortColumn && !schema[this.options.sortColumn]) { this.options.sortColumn = null @@ -374,11 +374,7 @@ export default abstract class DataFetch< * @param schema the datasource schema * @return {object} the enriched datasource schema */ - enrichSchema(schema: TableSchema | null): TableSchema | null { - if (schema == null) { - return null - } - + private enrichSchema(schema: TableSchema): TableSchema { // Check for any JSON fields so we can add any top level properties let jsonAdditions: Record<string, { type: string; nestedJSON: true }> = {} for (const fieldKey of Object.keys(schema)) { @@ -510,7 +506,7 @@ export default abstract class DataFetch< * @param state the current store state * @return {boolean} whether there is a next page of data or not */ - hasNextPage(state: DataFetchStore<TDefinition, TQuery>): boolean { + private hasNextPage(state: DataFetchStore<TDefinition, TQuery>): boolean { return state.cursors[state.pageNumber + 1] != null } @@ -520,7 +516,7 @@ export default abstract class DataFetch< * @param state the current store state * @return {boolean} whether there is a previous page of data or not */ - hasPrevPage(state: { pageNumber: number }): boolean { + private hasPrevPage(state: { pageNumber: number }): boolean { return state.pageNumber > 0 } diff --git a/packages/frontend-core/src/fetch/ViewV2Fetch.ts b/packages/frontend-core/src/fetch/ViewV2Fetch.ts index 74ad08f2f4..197b5b4ae5 100644 --- a/packages/frontend-core/src/fetch/ViewV2Fetch.ts +++ b/packages/frontend-core/src/fetch/ViewV2Fetch.ts @@ -28,10 +28,7 @@ export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> { } } - getDefaultSortColumn( - _definition: { primaryDisplay?: string } | null, - _schema: Record<string, any> - ) { + getDefaultSortColumn() { return null } From 5d63fe251f050e406692b3e302e8be7abcb58cbb Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 12:57:25 +0100 Subject: [PATCH 047/131] Simplify --- packages/frontend-core/src/fetch/DataFetch.ts | 5 ++--- packages/frontend-core/src/fetch/ViewFetch.ts | 3 ++- packages/frontend-core/src/fetch/index.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index 3e2b0d7dcf..b5eb774e45 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -360,12 +360,11 @@ export default abstract class DataFetch< /** * Gets the schema definition for a datasource. - * @param datasource the datasource * @param definition the datasource definition * @return {object} the schema */ - getSchema(_datasource: TDatasource | null, definition: TDefinition | null) { - return definition?.schema + getSchema(definition: TDefinition | null): Record<string, any> | undefined { + return definition?.schema ?? undefined } /** diff --git a/packages/frontend-core/src/fetch/ViewFetch.ts b/packages/frontend-core/src/fetch/ViewFetch.ts index fe891b9e66..2238d226ab 100644 --- a/packages/frontend-core/src/fetch/ViewFetch.ts +++ b/packages/frontend-core/src/fetch/ViewFetch.ts @@ -19,7 +19,8 @@ export default class ViewFetch extends DataFetch<ViewV1, Table> { } } - getSchema(datasource: ViewV1, definition: Table) { + getSchema(definition: Table) { + const { datasource } = this.options return definition?.views?.[datasource.name]?.schema } diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index 4f4eafe8e8..52233bd3fb 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -76,5 +76,5 @@ export const getDatasourceSchema = ({ definition?: { schema?: TableSchema } }) => { const instance = createEmptyFetchInstance({ API, datasource }) - return instance?.getSchema(datasource, definition) + return instance?.getSchema(definition) } From 3741b7144e8b635ce11578fdf597c8f1e758a786 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 13:05:27 +0100 Subject: [PATCH 048/131] Clean code --- packages/frontend-core/src/fetch/UserFetch.ts | 7 ++----- packages/frontend-core/src/fetch/ViewV2Fetch.ts | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/frontend-core/src/fetch/UserFetch.ts b/packages/frontend-core/src/fetch/UserFetch.ts index 199dacde00..b865c32d63 100644 --- a/packages/frontend-core/src/fetch/UserFetch.ts +++ b/packages/frontend-core/src/fetch/UserFetch.ts @@ -23,7 +23,6 @@ export default class UserFetch extends DataFetch< constructor(opts: { API: APIClient datasource: Table - options?: {} query: UserFetchQuery }) { super({ @@ -43,9 +42,7 @@ export default class UserFetch extends DataFetch< } async getDefinition() { - return { - schema: {}, - } + return { schema: {} } } async getData() { @@ -53,7 +50,7 @@ export default class UserFetch extends DataFetch< const { cursor, query } = get(this.store) // Convert old format to new one - we now allow use of the lucene format - const { appId, paginated, ...rest } = query || {} + const { appId, paginated, ...rest } = query const finalQuery: SearchFilters = utils.isSupportedUserSearch(rest) ? rest diff --git a/packages/frontend-core/src/fetch/ViewV2Fetch.ts b/packages/frontend-core/src/fetch/ViewV2Fetch.ts index 197b5b4ae5..3bb04d5bc4 100644 --- a/packages/frontend-core/src/fetch/ViewV2Fetch.ts +++ b/packages/frontend-core/src/fetch/ViewV2Fetch.ts @@ -62,7 +62,7 @@ export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> { try { const request = { - ...(query ? { query } : {}), + query, paginate, limit, bookmark: cursor, From 265b22f2b8a93d2f8a00358da206299609baadea Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 13:23:18 +0100 Subject: [PATCH 049/131] Type query --- packages/bbui/src/helpers.d.ts | 1 + packages/frontend-core/src/fetch/QueryFetch.ts | 16 +++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 packages/bbui/src/helpers.d.ts diff --git a/packages/bbui/src/helpers.d.ts b/packages/bbui/src/helpers.d.ts new file mode 100644 index 0000000000..98c6060590 --- /dev/null +++ b/packages/bbui/src/helpers.d.ts @@ -0,0 +1 @@ +export const cloneDeep: <T>(obj: T) => T diff --git a/packages/frontend-core/src/fetch/QueryFetch.ts b/packages/frontend-core/src/fetch/QueryFetch.ts index a6ddcd8f01..f8506fefec 100644 --- a/packages/frontend-core/src/fetch/QueryFetch.ts +++ b/packages/frontend-core/src/fetch/QueryFetch.ts @@ -5,9 +5,15 @@ import { get } from "svelte/store" interface QueryDatasource { _id: string - fields: any - queryParams: any - parameters: any + fields: Record<string, any> & { + pagination?: { + type: string + location: string + pageParam: string + } + } + queryParams: Record<string, string> + parameters: { name: string; default: string }[] } export default class QueryFetch extends DataFetch<QueryDatasource, Query> { @@ -49,8 +55,8 @@ export default class QueryFetch extends DataFetch<QueryDatasource, Query> { const type = definition?.fields?.pagination?.type // Set the default query params - let parameters = Helpers.cloneDeep(datasource?.queryParams || {}) - for (let param of datasource?.parameters || {}) { + const parameters = Helpers.cloneDeep(datasource.queryParams) + for (const param of datasource?.parameters || []) { if (!parameters[param.name]) { parameters[param.name] = param.default } From 0112087af15e5b3a903adee612ca33f08fe1d184 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 13:37:28 +0100 Subject: [PATCH 050/131] Improve typing --- packages/frontend-core/src/fetch/QueryFetch.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/packages/frontend-core/src/fetch/QueryFetch.ts b/packages/frontend-core/src/fetch/QueryFetch.ts index f8506fefec..0825d39660 100644 --- a/packages/frontend-core/src/fetch/QueryFetch.ts +++ b/packages/frontend-core/src/fetch/QueryFetch.ts @@ -1,6 +1,6 @@ import DataFetch from "./DataFetch" import { Helpers } from "@budibase/bbui" -import { Query } from "@budibase/types" +import { ExecuteQueryRequest, Query } from "@budibase/types" import { get } from "svelte/store" interface QueryDatasource { @@ -12,7 +12,7 @@ interface QueryDatasource { pageParam: string } } - queryParams: Record<string, string> + queryParams?: Record<string, string> parameters: { name: string; default: string }[] } @@ -55,7 +55,7 @@ export default class QueryFetch extends DataFetch<QueryDatasource, Query> { const type = definition?.fields?.pagination?.type // Set the default query params - const parameters = Helpers.cloneDeep(datasource.queryParams) + const parameters = Helpers.cloneDeep(datasource.queryParams || {}) for (const param of datasource?.parameters || []) { if (!parameters[param.name]) { parameters[param.name] = param.default @@ -63,13 +63,7 @@ export default class QueryFetch extends DataFetch<QueryDatasource, Query> { } // Add pagination to query if supported - const queryPayload: { - parameters: any - pagination?: { - page: number | null - limit: number - } - } = { parameters } + const queryPayload: ExecuteQueryRequest = { parameters } if (paginate && supportsPagination) { const requestCursor = type === "page" ? parseInt(cursor || "1") : cursor queryPayload.pagination = { page: requestCursor, limit } From af0312e5fe501e1b0f739b1c391877023f388186 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 13:48:45 +0100 Subject: [PATCH 051/131] Proper type QueryArrayFetch --- packages/frontend-core/src/fetch/QueryArrayFetch.ts | 10 +++++----- packages/frontend-core/src/utils/json.d.ts | 13 +++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 packages/frontend-core/src/utils/json.d.ts diff --git a/packages/frontend-core/src/fetch/QueryArrayFetch.ts b/packages/frontend-core/src/fetch/QueryArrayFetch.ts index 15f86ae7fc..ce9177e554 100644 --- a/packages/frontend-core/src/fetch/QueryArrayFetch.ts +++ b/packages/frontend-core/src/fetch/QueryArrayFetch.ts @@ -14,12 +14,12 @@ export default class QueryArrayFetch extends FieldFetch { try { const table = await this.API.fetchQueryDefinition(datasource.tableId) const schema = generateQueryArraySchemas( - table?.schema, - table?.nestedSchemaFields + table.schema, + table.nestedSchemaFields ) - const result: { - schema: Record<string, any> | null - } = { schema: getJSONArrayDatasourceSchema(schema, datasource) } + const result = { + schema: getJSONArrayDatasourceSchema(schema, datasource), + } return result } catch (error) { diff --git a/packages/frontend-core/src/utils/json.d.ts b/packages/frontend-core/src/utils/json.d.ts new file mode 100644 index 0000000000..ebda694c15 --- /dev/null +++ b/packages/frontend-core/src/utils/json.d.ts @@ -0,0 +1,13 @@ +import { QuerySchema } from "@budibase/types" + +type Schema = Record<string, QuerySchema | string> + +export const getJSONArrayDatasourceSchema: ( + tableSchema: Schema, + datasource: any +) => Record<string, { type: string; name: string; prefixKeys: string }> + +export const generateQueryArraySchemas: ( + schema: Schema, + nestedSchemaFields?: Record<string, Schema> +) => Schema From fc4336a9f30d52197f1ff40304be455bf346e8f4 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 13:54:21 +0100 Subject: [PATCH 052/131] Add typings --- packages/frontend-core/src/utils/json.d.ts | 10 +++++++++- packages/types/src/documents/app/table/schema.ts | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/frontend-core/src/utils/json.d.ts b/packages/frontend-core/src/utils/json.d.ts index ebda694c15..4f26f4b264 100644 --- a/packages/frontend-core/src/utils/json.d.ts +++ b/packages/frontend-core/src/utils/json.d.ts @@ -1,4 +1,4 @@ -import { QuerySchema } from "@budibase/types" +import { JsonFieldMetadata, QuerySchema } from "@budibase/types" type Schema = Record<string, QuerySchema | string> @@ -11,3 +11,11 @@ export const generateQueryArraySchemas: ( schema: Schema, nestedSchemaFields?: Record<string, Schema> ) => Schema + +export const convertJSONSchemaToTableSchema: ( + jsonSchema: JsonFieldMetadata, + options: { + squashObjects?: boolean + prefixKeys?: string + } +) => Record<string, { type: string; name: string; prefixKeys: string }> diff --git a/packages/types/src/documents/app/table/schema.ts b/packages/types/src/documents/app/table/schema.ts index 771192e2f5..58af430f7e 100644 --- a/packages/types/src/documents/app/table/schema.ts +++ b/packages/types/src/documents/app/table/schema.ts @@ -227,6 +227,7 @@ interface OtherFieldMetadata extends BaseFieldSchema { | FieldType.OPTIONS | FieldType.BOOLEAN | FieldType.BIGINT + | FieldType.JSON > } From 88760d473e960668e57537597824652bbd946d31 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 13:54:37 +0100 Subject: [PATCH 053/131] Proper type nestedProvider --- .../src/fetch/NestedProviderFetch.ts | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/frontend-core/src/fetch/NestedProviderFetch.ts b/packages/frontend-core/src/fetch/NestedProviderFetch.ts index 71eb5177db..4bcdd697a2 100644 --- a/packages/frontend-core/src/fetch/NestedProviderFetch.ts +++ b/packages/frontend-core/src/fetch/NestedProviderFetch.ts @@ -1,7 +1,23 @@ +import { Row, TableSchema } from "@budibase/types" import DataFetch from "./DataFetch" -export default class NestedProviderFetch extends DataFetch<any, any> { - async getDefinition(datasource: any) { +interface NestedProviderDatasource { + value?: { + schema: TableSchema + primaryDisplay: string + rows: Row[] + } +} + +interface NestedProviderDefinition { + schema?: TableSchema + primaryDisplay?: string +} +export default class NestedProviderFetch extends DataFetch< + NestedProviderDatasource, + NestedProviderDefinition +> { + async getDefinition(datasource: NestedProviderDatasource) { // Nested providers should already have exposed their own schema return { schema: datasource?.value?.schema, From fae3c6b3eb76119a88448be10c177b603dbdb85c Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 13:58:20 +0100 Subject: [PATCH 054/131] Type GroupUserFetch --- packages/frontend-core/src/constants.ts | 4 ++-- packages/frontend-core/src/fetch/GroupUserFetch.ts | 14 +++++++------- packages/frontend-core/src/fetch/JSONArrayFetch.ts | 5 +---- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/packages/frontend-core/src/constants.ts b/packages/frontend-core/src/constants.ts index 8a39e8c106..907d91825f 100644 --- a/packages/frontend-core/src/constants.ts +++ b/packages/frontend-core/src/constants.ts @@ -32,8 +32,8 @@ export const Cookies = { } // Table names -export const TableNames = { - USERS: "ta_users", +export const enum TableNames { + USERS = "ta_users", } export const BudibaseRoles = { diff --git a/packages/frontend-core/src/fetch/GroupUserFetch.ts b/packages/frontend-core/src/fetch/GroupUserFetch.ts index 2f5cacd1a2..bc7688330a 100644 --- a/packages/frontend-core/src/fetch/GroupUserFetch.ts +++ b/packages/frontend-core/src/fetch/GroupUserFetch.ts @@ -8,16 +8,16 @@ interface GroupUserQuery { emailSearch: string } +interface GroupUserDatasource { + tableId: TableNames.USERS +} + export default class GroupUserFetch extends DataFetch< - any, - any, + GroupUserDatasource, + {}, GroupUserQuery > { - constructor(opts: { - API: APIClient - datasource: any - query: GroupUserQuery - }) { + constructor(opts: { API: APIClient; query: GroupUserQuery }) { super({ ...opts, datasource: { diff --git a/packages/frontend-core/src/fetch/JSONArrayFetch.ts b/packages/frontend-core/src/fetch/JSONArrayFetch.ts index a254bc3ae4..f0cbaa87c5 100644 --- a/packages/frontend-core/src/fetch/JSONArrayFetch.ts +++ b/packages/frontend-core/src/fetch/JSONArrayFetch.ts @@ -7,10 +7,7 @@ export default class JSONArrayFetch extends FieldFetch { // We can then extract their schema as a subset of the table schema. try { const table = await this.API.fetchTableDefinition(datasource.tableId) - const schema: Record<string, any> | null = getJSONArrayDatasourceSchema( - table?.schema, - datasource - ) + const schema = getJSONArrayDatasourceSchema(table?.schema, datasource) return { schema } } catch (error) { return null From 2fb243a7c7881489590c4b31e768cbc7affb856a Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 14:09:05 +0100 Subject: [PATCH 055/131] Fix customFetch --- .../frontend-core/src/fetch/CustomFetch.ts | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/frontend-core/src/fetch/CustomFetch.ts b/packages/frontend-core/src/fetch/CustomFetch.ts index 9db9a935a5..176d878a54 100644 --- a/packages/frontend-core/src/fetch/CustomFetch.ts +++ b/packages/frontend-core/src/fetch/CustomFetch.ts @@ -1,6 +1,15 @@ import DataFetch from "./DataFetch" -export default class CustomFetch extends DataFetch<any, any> { +interface CustomDatasource { + data: any +} + +type CustomDefinition = Record<string, any> + +export default class CustomFetch extends DataFetch< + CustomDatasource, + CustomDefinition +> { // Gets the correct Budibase type for a JS value getType(value: any) { if (value == null) { @@ -55,7 +64,7 @@ export default class CustomFetch extends DataFetch<any, any> { } // Enriches the custom data to ensure the structure and format is usable - enrichCustomData(data: any[]) { + enrichCustomData(data: (string | any)[]) { if (!data?.length) { return [] } @@ -72,7 +81,7 @@ export default class CustomFetch extends DataFetch<any, any> { // Try parsing strings if (typeof value === "string") { const split = value.split(",").map(x => x.trim()) - let obj: Record<string, string> = {} + const obj: Record<string, string> = {} for (let i = 0; i < split.length; i++) { const suffix = i === 0 ? "" : ` ${i + 1}` const key = `Value${suffix}` @@ -87,27 +96,27 @@ export default class CustomFetch extends DataFetch<any, any> { } // Extracts and parses the custom data from the datasource definition - getCustomData(datasource: { data: any }) { + getCustomData(datasource: CustomDatasource) { return this.enrichCustomData(this.parseCustomData(datasource?.data)) } - async getDefinition(datasource: any) { + async getDefinition(datasource: CustomDatasource) { // Try and work out the schema from the array provided - let schema: any = {} + const schema: CustomDefinition = {} const data = this.getCustomData(datasource) if (!data?.length) { return { schema } } // Go through every object and extract all valid keys - for (let datum of data) { - for (let key of Object.keys(datum)) { + for (const datum of data) { + for (const key of Object.keys(datum)) { if (key === "_id") { continue } if (!schema[key]) { let type = this.getType(datum[key]) - let constraints: any = {} + const constraints: any = {} // Determine whether we should render text columns as options instead if (type === "string") { From 819ca2129e1c9d6f138951314af08ed86ab9b226 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 14:17:10 +0100 Subject: [PATCH 056/131] Clean types --- .../frontend-core/src/fetch/GroupUserFetch.ts | 5 ++--- packages/frontend-core/src/fetch/UserFetch.ts | 16 +++++++--------- packages/frontend-core/src/fetch/ViewV2Fetch.ts | 5 +---- packages/frontend-core/src/fetch/index.ts | 6 +++--- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/packages/frontend-core/src/fetch/GroupUserFetch.ts b/packages/frontend-core/src/fetch/GroupUserFetch.ts index bc7688330a..a14623bfb0 100644 --- a/packages/frontend-core/src/fetch/GroupUserFetch.ts +++ b/packages/frontend-core/src/fetch/GroupUserFetch.ts @@ -1,7 +1,6 @@ import { get } from "svelte/store" -import DataFetch from "./DataFetch" +import DataFetch, { DataFetchParams } from "./DataFetch" import { TableNames } from "../constants" -import { APIClient } from "../api/types" interface GroupUserQuery { groupId: string @@ -17,7 +16,7 @@ export default class GroupUserFetch extends DataFetch< {}, GroupUserQuery > { - constructor(opts: { API: APIClient; query: GroupUserQuery }) { + constructor(opts: DataFetchParams<GroupUserDatasource, GroupUserQuery>) { super({ ...opts, datasource: { diff --git a/packages/frontend-core/src/fetch/UserFetch.ts b/packages/frontend-core/src/fetch/UserFetch.ts index b865c32d63..8f1ef36cac 100644 --- a/packages/frontend-core/src/fetch/UserFetch.ts +++ b/packages/frontend-core/src/fetch/UserFetch.ts @@ -1,30 +1,28 @@ import { get } from "svelte/store" -import DataFetch from "./DataFetch" +import DataFetch, { DataFetchParams } from "./DataFetch" import { TableNames } from "../constants" import { utils } from "@budibase/shared-core" import { BasicOperator, SearchFilters, SearchUsersRequest, - Table, } from "@budibase/types" -import { APIClient } from "../api/types.js" interface UserFetchQuery { appId: string paginated: boolean } +interface UserDatasource { + tableId: string +} + export default class UserFetch extends DataFetch< - { tableId: string }, + UserDatasource, {}, UserFetchQuery > { - constructor(opts: { - API: APIClient - datasource: Table - query: UserFetchQuery - }) { + constructor(opts: DataFetchParams<UserDatasource, UserFetchQuery>) { super({ ...opts, datasource: { diff --git a/packages/frontend-core/src/fetch/ViewV2Fetch.ts b/packages/frontend-core/src/fetch/ViewV2Fetch.ts index 3bb04d5bc4..1be1ba295c 100644 --- a/packages/frontend-core/src/fetch/ViewV2Fetch.ts +++ b/packages/frontend-core/src/fetch/ViewV2Fetch.ts @@ -12,10 +12,7 @@ export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> { } } - async getDefinition(datasource: UIView | null): Promise<ViewV2 | null> { - if (!datasource?.id) { - return null - } + async getDefinition(datasource: UIView) { try { const res = await this.API.viewV2.fetchDefinition(datasource.id) return res?.data diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index 52233bd3fb..1577f76034 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -10,7 +10,7 @@ import UserFetch from "./UserFetch.js" import GroupUserFetch from "./GroupUserFetch" import CustomFetch from "./CustomFetch" import QueryArrayFetch from "./QueryArrayFetch.js" -import { TableSchema, UIDatasource } from "@budibase/types" +import { UIDatasource } from "@budibase/types" import { APIClient } from "../api/types.js" const DataFetchMap = { @@ -59,7 +59,7 @@ export const getDatasourceDefinition = async ({ datasource, }: { API: APIClient - datasource: UIDatasource + datasource: any }) => { const instance = createEmptyFetchInstance({ API, datasource }) return await instance?.getDefinition(datasource) @@ -73,7 +73,7 @@ export const getDatasourceSchema = ({ }: { API: APIClient datasource: UIDatasource - definition?: { schema?: TableSchema } + definition?: any }) => { const instance = createEmptyFetchInstance({ API, datasource }) return instance?.getSchema(definition) From b9fb4416bb428acab70288fdc7966454670b738f Mon Sep 17 00:00:00 2001 From: Sam Rose <hello@samwho.dev> Date: Wed, 8 Jan 2025 13:18:40 +0000 Subject: [PATCH 057/131] Allow use of AI fields in view calculations. --- packages/pro | 2 +- .../src/api/controllers/row/staticFormula.ts | 6 ++ .../server/src/api/routes/tests/row.spec.ts | 40 ++++---- .../src/api/routes/tests/viewV2.spec.ts | 97 ++++++++++++++++++- .../src/tests/utilities/mocks/openai.ts | 46 +++++++++ .../src/utilities/rowProcessor/utils.ts | 2 +- packages/types/src/documents/app/row.ts | 1 + .../types/src/documents/app/table/schema.ts | 2 +- 8 files changed, 170 insertions(+), 26 deletions(-) create mode 100644 packages/server/src/tests/utilities/mocks/openai.ts diff --git a/packages/pro b/packages/pro index 32d84f109d..45f5b6fe9b 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit 32d84f109d4edc526145472a7446327312151442 +Subproject commit 45f5b6fe9bbdbdf502581740ab43b82e8153260f diff --git a/packages/server/src/api/controllers/row/staticFormula.ts b/packages/server/src/api/controllers/row/staticFormula.ts index b81a164807..afa3a1f239 100644 --- a/packages/server/src/api/controllers/row/staticFormula.ts +++ b/packages/server/src/api/controllers/row/staticFormula.ts @@ -162,6 +162,12 @@ export async function finaliseRow( dynamic: false, contextRows: [enrichedRow], }) + + const flag1 = await features.isEnabled(FeatureFlag.BUDIBASE_AI) + const flag2 = await pro.features.isBudibaseAIEnabled() + const flag3 = await features.isEnabled(FeatureFlag.AI_CUSTOM_CONFIGS) + const flag4 = await pro.features.isAICustomConfigsEnabled() + const aiEnabled = ((await features.isEnabled(FeatureFlag.BUDIBASE_AI)) && (await pro.features.isBudibaseAIEnabled())) || diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index a3012c3760..968ce9c798 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -8,7 +8,13 @@ import { import tk from "timekeeper" import emitter from "../../../../src/events" import { outputProcessing } from "../../../utilities/rowProcessor" -import { context, InternalTable, tenancy, utils } from "@budibase/backend-core" +import { + context, + setEnv, + InternalTable, + tenancy, + utils, +} from "@budibase/backend-core" import { quotas } from "@budibase/pro" import { AIOperationEnum, @@ -42,19 +48,8 @@ import { InternalTables } from "../../../db/utils" import { withEnv } from "../../../environment" import { JsTimeoutError } from "@budibase/string-templates" import { isDate } from "../../../utilities" - -jest.mock("@budibase/pro", () => ({ - ...jest.requireActual("@budibase/pro"), - ai: { - LargeLanguageModel: { - forCurrentTenant: async () => ({ - llm: {}, - run: jest.fn(() => `Mock LLM Response`), - buildPromptFromAIOperation: jest.fn(), - }), - }, - }, -})) +import nock from "nock" +import { mockChatGPTResponse } from "../../../tests/utilities/mocks/openai" const timestamp = new Date("2023-01-26T11:48:57.597Z").toISOString() tk.freeze(timestamp) @@ -99,6 +94,8 @@ if (descriptions.length) { const ds = await dsProvider() datasource = ds.datasource client = ds.client + + mocks.licenses.useCloudFree() }) afterAll(async () => { @@ -172,10 +169,6 @@ if (descriptions.length) { ) } - beforeEach(async () => { - mocks.licenses.useCloudFree() - }) - const getRowUsage = async () => { const { total } = await config.doInContext(undefined, () => quotas.getCurrentUsageValues( @@ -3224,10 +3217,17 @@ if (descriptions.length) { isInternal && describe("AI fields", () => { let table: Table + let envCleanup: () => void beforeAll(async () => { mocks.licenses.useBudibaseAI() mocks.licenses.useAICustomConfigs() + envCleanup = setEnv({ + OPENAI_API_KEY: "sk-abcdefghijklmnopqrstuvwxyz1234567890abcd", + }) + + mockChatGPTResponse("Mock LLM Response") + table = await config.api.table.save( saveTableRequest({ schema: { @@ -3251,7 +3251,9 @@ if (descriptions.length) { }) afterAll(() => { - jest.unmock("@budibase/pro") + nock.cleanAll() + envCleanup() + mocks.licenses.useCloudFree() }) it("should be able to save a row with an AI column", async () => { diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 6ace7e256b..57efc868e9 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -1,4 +1,5 @@ import { + AIOperationEnum, ArrayOperator, BasicOperator, BBReferenceFieldSubType, @@ -42,7 +43,9 @@ import { } from "../../../integrations/tests/utils" import merge from "lodash/merge" import { quotas } from "@budibase/pro" -import { context, db, events, roles } from "@budibase/backend-core" +import { context, db, events, roles, setEnv } from "@budibase/backend-core" +import { mockChatGPTResponse } from "../../../tests/utilities/mocks/openai" +import nock from "nock" const descriptions = datasourceDescribe({ exclude: [DatabaseName.MONGODB] }) @@ -100,6 +103,7 @@ if (descriptions.length) { beforeAll(async () => { await config.init() + mocks.licenses.useCloudFree() const ds = await dsProvider() rawDatasource = ds.rawDatasource @@ -109,7 +113,6 @@ if (descriptions.length) { beforeEach(() => { jest.clearAllMocks() - mocks.licenses.useCloudFree() }) describe("view crud", () => { @@ -507,7 +510,6 @@ if (descriptions.length) { }) it("readonly fields can be used on free license", async () => { - mocks.licenses.useCloudFree() const table = await config.api.table.save( saveTableRequest({ schema: { @@ -933,6 +935,94 @@ if (descriptions.length) { } ) }) + + describe("AI fields", () => { + let envCleanup: () => void + beforeAll(() => { + mocks.licenses.useBudibaseAI() + mocks.licenses.useAICustomConfigs() + envCleanup = setEnv({ + OPENAI_API_KEY: "sk-abcdefghijklmnopqrstuvwxyz1234567890abcd", + }) + + mockChatGPTResponse(prompt => { + if (prompt.includes("elephant")) { + return "big" + } + if (prompt.includes("mouse")) { + return "small" + } + if (prompt.includes("whale")) { + return "big" + } + return "unknown" + }) + }) + + afterAll(() => { + nock.cleanAll() + envCleanup() + mocks.licenses.useCloudFree() + }) + + it("can use AI fields in view calculations", async () => { + const table = await config.api.table.save( + saveTableRequest({ + schema: { + animal: { + name: "animal", + type: FieldType.STRING, + }, + bigOrSmall: { + name: "bigOrSmall", + type: FieldType.AI, + operation: AIOperationEnum.CATEGORISE_TEXT, + categories: "big,small", + columns: ["animal"], + }, + }, + }) + ) + + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + type: ViewV2Type.CALCULATION, + schema: { + bigOrSmall: { + visible: true, + }, + count: { + visible: true, + calculationType: CalculationType.COUNT, + field: "animal", + }, + }, + }) + + await config.api.row.save(table._id!, { + animal: "elephant", + }) + + await config.api.row.save(table._id!, { + animal: "mouse", + }) + + await config.api.row.save(table._id!, { + animal: "whale", + }) + + const { rows } = await config.api.row.search(view.id, { + sort: "bigOrSmall", + sortOrder: SortOrder.ASCENDING, + }) + expect(rows).toHaveLength(2) + expect(rows[0].bigOrSmall).toEqual("big") + expect(rows[1].bigOrSmall).toEqual("small") + expect(rows[0].count).toEqual(2) + expect(rows[1].count).toEqual(1) + }) + }) }) describe("update", () => { @@ -1836,7 +1926,6 @@ if (descriptions.length) { }, }) - mocks.licenses.useCloudFree() const view = await getDelegate(res) expect(view.schema?.one).toEqual( expect.objectContaining({ visible: true, readonly: true }) diff --git a/packages/server/src/tests/utilities/mocks/openai.ts b/packages/server/src/tests/utilities/mocks/openai.ts new file mode 100644 index 0000000000..b17491808c --- /dev/null +++ b/packages/server/src/tests/utilities/mocks/openai.ts @@ -0,0 +1,46 @@ +import nock from "nock" + +let chatID = 1 + +export function mockChatGPTResponse( + response: string | ((prompt: string) => string) +) { + return nock("https://api.openai.com") + .post("/v1/chat/completions") + .reply(200, (uri, requestBody) => { + let content = response + if (typeof response === "function") { + const messages = (requestBody as any).messages + content = response(messages[0].content) + } + + chatID++ + + return { + id: `chatcmpl-${chatID}`, + object: "chat.completion", + created: Math.floor(Date.now() / 1000), + model: "gpt-4o-mini", + system_fingerprint: `fp_${chatID}`, + choices: [ + { + index: 0, + message: { role: "assistant", content }, + logprobs: null, + finish_reason: "stop", + }, + ], + usage: { + prompt_tokens: 0, + completion_tokens: 0, + total_tokens: 0, + completion_tokens_details: { + reasoning_tokens: 0, + accepted_prediction_tokens: 0, + rejected_prediction_tokens: 0, + }, + }, + } + }) + .persist() +} diff --git a/packages/server/src/utilities/rowProcessor/utils.ts b/packages/server/src/utilities/rowProcessor/utils.ts index 09d3324ded..7d2f8b49f4 100644 --- a/packages/server/src/utilities/rowProcessor/utils.ts +++ b/packages/server/src/utilities/rowProcessor/utils.ts @@ -160,7 +160,7 @@ export async function processAIColumns<T extends Row | Row[]>( return tracer.trace("processAIColumn", {}, async span => { span?.addTags({ table_id: table._id, column }) - const llmResponse = await llmWrapper.run(prompt!) + const llmResponse = await llmWrapper.run(prompt) return { ...row, [column]: llmResponse, diff --git a/packages/types/src/documents/app/row.ts b/packages/types/src/documents/app/row.ts index 6b6b38a5cf..bb58933b65 100644 --- a/packages/types/src/documents/app/row.ts +++ b/packages/types/src/documents/app/row.ts @@ -154,6 +154,7 @@ export const GroupByTypes = [ FieldType.BOOLEAN, FieldType.DATETIME, FieldType.BIGINT, + FieldType.AI, ] export function canGroupBy(type: FieldType) { diff --git a/packages/types/src/documents/app/table/schema.ts b/packages/types/src/documents/app/table/schema.ts index 771192e2f5..f4a6d8481d 100644 --- a/packages/types/src/documents/app/table/schema.ts +++ b/packages/types/src/documents/app/table/schema.ts @@ -123,7 +123,7 @@ export interface AIFieldMetadata extends BaseFieldSchema { operation: AIOperationEnum columns?: string[] column?: string - categories?: string[] + categories?: string prompt?: string language?: string } From 10fca945d27f282c2e765f035f4b1582a60fb3a7 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 14:27:13 +0100 Subject: [PATCH 058/131] Cleanups --- .../frontend-core/src/fetch/CustomFetch.ts | 4 ++- packages/frontend-core/src/fetch/DataFetch.ts | 15 ++++++----- .../frontend-core/src/fetch/FieldFetch.ts | 6 ++--- .../frontend-core/src/fetch/JSONArrayFetch.ts | 6 +++-- .../src/fetch/NestedProviderFetch.ts | 4 ++- .../src/fetch/QueryArrayFetch.ts | 6 +++-- .../frontend-core/src/fetch/QueryFetch.ts | 6 +++-- .../src/fetch/RelationshipFetch.ts | 4 ++- .../frontend-core/src/fetch/TableFetch.ts | 4 ++- packages/frontend-core/src/fetch/ViewFetch.ts | 4 ++- .../frontend-core/src/fetch/ViewV2Fetch.ts | 4 ++- packages/frontend-core/src/fetch/index.ts | 27 +++++++++++++------ 12 files changed, 60 insertions(+), 30 deletions(-) diff --git a/packages/frontend-core/src/fetch/CustomFetch.ts b/packages/frontend-core/src/fetch/CustomFetch.ts index 176d878a54..afd3d18ba9 100644 --- a/packages/frontend-core/src/fetch/CustomFetch.ts +++ b/packages/frontend-core/src/fetch/CustomFetch.ts @@ -100,7 +100,9 @@ export default class CustomFetch extends DataFetch< return this.enrichCustomData(this.parseCustomData(datasource?.data)) } - async getDefinition(datasource: CustomDatasource) { + async getDefinition() { + const { datasource } = this.options + // Try and work out the schema from the array provided const schema: CustomDefinition = {} const data = this.getCustomData(datasource) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index b5eb774e45..e5d899d596 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -11,6 +11,7 @@ import { SortType, TableSchema, UISearchFilter, + ViewSchema, } from "@budibase/types" import { APIClient } from "../api/types" @@ -210,10 +211,10 @@ export default abstract class DataFetch< * Fetches a fresh set of data from the server, resetting pagination */ async getInitialData() { - const { datasource, filter, paginate } = this.options + const { filter, paginate } = this.options // Fetch datasource definition and extract sort properties if configured - const definition = await this.getDefinition(datasource) + const definition = await this.getDefinition() // Determine feature flags const features = await this.determineFeatureFlags() @@ -351,19 +352,19 @@ export default abstract class DataFetch< /** * Gets the definition for this datasource. - * @param datasource + * @return {object} the definition */ - abstract getDefinition( - datasource: TDatasource | null - ): Promise<TDefinition | null> + abstract getDefinition(): Promise<TDefinition | null> /** * Gets the schema definition for a datasource. * @param definition the datasource definition * @return {object} the schema */ - getSchema(definition: TDefinition | null): Record<string, any> | undefined { + getSchema( + definition: TDefinition | null + ): ViewSchema | Record<string, any> | undefined { return definition?.schema ?? undefined } diff --git a/packages/frontend-core/src/fetch/FieldFetch.ts b/packages/frontend-core/src/fetch/FieldFetch.ts index 636fb63f3d..ac1e683c51 100644 --- a/packages/frontend-core/src/fetch/FieldFetch.ts +++ b/packages/frontend-core/src/fetch/FieldFetch.ts @@ -19,9 +19,9 @@ export default class FieldFetch extends DataFetch< FieldDatasource, FieldDefinition > { - async getDefinition( - datasource: FieldDatasource - ): Promise<FieldDefinition | null> { + async getDefinition(): Promise<FieldDefinition | null> { + const { datasource } = this.options + // Field sources have their schema statically defined let schema if (datasource.fieldType === "attachment") { diff --git a/packages/frontend-core/src/fetch/JSONArrayFetch.ts b/packages/frontend-core/src/fetch/JSONArrayFetch.ts index f0cbaa87c5..cae9a1e521 100644 --- a/packages/frontend-core/src/fetch/JSONArrayFetch.ts +++ b/packages/frontend-core/src/fetch/JSONArrayFetch.ts @@ -1,8 +1,10 @@ -import FieldFetch, { FieldDatasource } from "./FieldFetch" +import FieldFetch from "./FieldFetch" import { getJSONArrayDatasourceSchema } from "../utils/json" export default class JSONArrayFetch extends FieldFetch { - async getDefinition(datasource: FieldDatasource) { + async getDefinition() { + const { datasource } = this.options + // JSON arrays need their table definitions fetched. // We can then extract their schema as a subset of the table schema. try { diff --git a/packages/frontend-core/src/fetch/NestedProviderFetch.ts b/packages/frontend-core/src/fetch/NestedProviderFetch.ts index 4bcdd697a2..666340610f 100644 --- a/packages/frontend-core/src/fetch/NestedProviderFetch.ts +++ b/packages/frontend-core/src/fetch/NestedProviderFetch.ts @@ -17,7 +17,9 @@ export default class NestedProviderFetch extends DataFetch< NestedProviderDatasource, NestedProviderDefinition > { - async getDefinition(datasource: NestedProviderDatasource) { + async getDefinition() { + const { datasource } = this.options + // Nested providers should already have exposed their own schema return { schema: datasource?.value?.schema, diff --git a/packages/frontend-core/src/fetch/QueryArrayFetch.ts b/packages/frontend-core/src/fetch/QueryArrayFetch.ts index ce9177e554..9142000fe6 100644 --- a/packages/frontend-core/src/fetch/QueryArrayFetch.ts +++ b/packages/frontend-core/src/fetch/QueryArrayFetch.ts @@ -1,11 +1,13 @@ -import FieldFetch, { FieldDatasource } from "./FieldFetch" +import FieldFetch from "./FieldFetch" import { getJSONArrayDatasourceSchema, generateQueryArraySchemas, } from "../utils/json" export default class QueryArrayFetch extends FieldFetch { - async getDefinition(datasource: FieldDatasource) { + async getDefinition() { + const { datasource } = this.options + if (!datasource?.tableId) { return null } diff --git a/packages/frontend-core/src/fetch/QueryFetch.ts b/packages/frontend-core/src/fetch/QueryFetch.ts index 0825d39660..0754edd267 100644 --- a/packages/frontend-core/src/fetch/QueryFetch.ts +++ b/packages/frontend-core/src/fetch/QueryFetch.ts @@ -18,7 +18,7 @@ interface QueryDatasource { export default class QueryFetch extends DataFetch<QueryDatasource, Query> { async determineFeatureFlags() { - const definition = await this.getDefinition(this.options.datasource) + const definition = await this.getDefinition() const supportsPagination = !!definition?.fields?.pagination?.type && !!definition?.fields?.pagination?.location && @@ -26,7 +26,9 @@ export default class QueryFetch extends DataFetch<QueryDatasource, Query> { return { supportsPagination } } - async getDefinition(datasource: QueryDatasource) { + async getDefinition() { + const { datasource } = this.options + if (!datasource?._id) { return null } diff --git a/packages/frontend-core/src/fetch/RelationshipFetch.ts b/packages/frontend-core/src/fetch/RelationshipFetch.ts index 7b6e93fbcc..f853a753cd 100644 --- a/packages/frontend-core/src/fetch/RelationshipFetch.ts +++ b/packages/frontend-core/src/fetch/RelationshipFetch.ts @@ -12,7 +12,9 @@ export default class RelationshipFetch extends DataFetch< RelationshipDatasource, Table > { - async getDefinition(datasource: RelationshipDatasource) { + async getDefinition() { + const { datasource } = this.options + if (!datasource?.tableId) { return null } diff --git a/packages/frontend-core/src/fetch/TableFetch.ts b/packages/frontend-core/src/fetch/TableFetch.ts index 433de69b59..c1152f2869 100644 --- a/packages/frontend-core/src/fetch/TableFetch.ts +++ b/packages/frontend-core/src/fetch/TableFetch.ts @@ -11,7 +11,9 @@ export default class TableFetch extends DataFetch<UITable, Table> { } } - async getDefinition(datasource: UITable) { + async getDefinition() { + const { datasource } = this.options + if (!datasource?.tableId) { return null } diff --git a/packages/frontend-core/src/fetch/ViewFetch.ts b/packages/frontend-core/src/fetch/ViewFetch.ts index 2238d226ab..b6830e7118 100644 --- a/packages/frontend-core/src/fetch/ViewFetch.ts +++ b/packages/frontend-core/src/fetch/ViewFetch.ts @@ -4,7 +4,9 @@ import DataFetch from "./DataFetch" type ViewV1 = View & { name: string } export default class ViewFetch extends DataFetch<ViewV1, Table> { - async getDefinition(datasource: ViewV1) { + async getDefinition() { + const { datasource } = this.options + if (!datasource?.tableId) { return null } diff --git a/packages/frontend-core/src/fetch/ViewV2Fetch.ts b/packages/frontend-core/src/fetch/ViewV2Fetch.ts index 1be1ba295c..cdd3bab6ed 100644 --- a/packages/frontend-core/src/fetch/ViewV2Fetch.ts +++ b/packages/frontend-core/src/fetch/ViewV2Fetch.ts @@ -12,7 +12,9 @@ export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> { } } - async getDefinition(datasource: UIView) { + async getDefinition() { + const { datasource } = this.options + try { const res = await this.API.viewV2.fetchDefinition(datasource.id) return res?.data diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index 1577f76034..4accb0b5ec 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -10,7 +10,6 @@ import UserFetch from "./UserFetch.js" import GroupUserFetch from "./GroupUserFetch" import CustomFetch from "./CustomFetch" import QueryArrayFetch from "./QueryArrayFetch.js" -import { UIDatasource } from "@budibase/types" import { APIClient } from "../api/types.js" const DataFetchMap = { @@ -39,12 +38,16 @@ export const fetchData = ({ API, datasource, options }: any) => { // Creates an empty fetch instance with no datasource configured, so no data // will initially be loaded -const createEmptyFetchInstance = ({ +const createEmptyFetchInstance = < + TDatasource extends { + type: keyof typeof DataFetchMap + } +>({ API, datasource, }: { API: APIClient - datasource: any + datasource: TDatasource }) => { const handler = DataFetchMap[datasource?.type as keyof typeof DataFetchMap] if (!handler) { @@ -54,25 +57,33 @@ const createEmptyFetchInstance = ({ } // Fetches the definition of any type of datasource -export const getDatasourceDefinition = async ({ +export const getDatasourceDefinition = async < + TDatasource extends { + type: keyof typeof DataFetchMap + } +>({ API, datasource, }: { API: APIClient - datasource: any + datasource: TDatasource }) => { const instance = createEmptyFetchInstance({ API, datasource }) - return await instance?.getDefinition(datasource) + return await instance?.getDefinition() } // Fetches the schema of any type of datasource -export const getDatasourceSchema = ({ +export const getDatasourceSchema = < + TDatasource extends { + type: keyof typeof DataFetchMap + } +>({ API, datasource, definition, }: { API: APIClient - datasource: UIDatasource + datasource: TDatasource definition?: any }) => { const instance = createEmptyFetchInstance({ API, datasource }) From 95d3238d1e83cb5d840055edbb0214a16b81ff97 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 14:29:28 +0100 Subject: [PATCH 059/131] Fix declarations --- packages/bbui/src/helpers.d.ts | 4 ++- packages/frontend-core/src/utils/json.d.ts | 32 ++++++++++++---------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/packages/bbui/src/helpers.d.ts b/packages/bbui/src/helpers.d.ts index 98c6060590..79e08657b7 100644 --- a/packages/bbui/src/helpers.d.ts +++ b/packages/bbui/src/helpers.d.ts @@ -1 +1,3 @@ -export const cloneDeep: <T>(obj: T) => T +declare module "./helpers" { + export const cloneDeep: <T>(obj: T) => T +} diff --git a/packages/frontend-core/src/utils/json.d.ts b/packages/frontend-core/src/utils/json.d.ts index 4f26f4b264..e9b6ac5703 100644 --- a/packages/frontend-core/src/utils/json.d.ts +++ b/packages/frontend-core/src/utils/json.d.ts @@ -2,20 +2,22 @@ import { JsonFieldMetadata, QuerySchema } from "@budibase/types" type Schema = Record<string, QuerySchema | string> -export const getJSONArrayDatasourceSchema: ( - tableSchema: Schema, - datasource: any -) => Record<string, { type: string; name: string; prefixKeys: string }> +declare module "./json" { + export const getJSONArrayDatasourceSchema: ( + tableSchema: Schema, + datasource: any + ) => Record<string, { type: string; name: string; prefixKeys: string }> -export const generateQueryArraySchemas: ( - schema: Schema, - nestedSchemaFields?: Record<string, Schema> -) => Schema + export const generateQueryArraySchemas: ( + schema: Schema, + nestedSchemaFields?: Record<string, Schema> + ) => Schema -export const convertJSONSchemaToTableSchema: ( - jsonSchema: JsonFieldMetadata, - options: { - squashObjects?: boolean - prefixKeys?: string - } -) => Record<string, { type: string; name: string; prefixKeys: string }> + export const convertJSONSchemaToTableSchema: ( + jsonSchema: JsonFieldMetadata, + options: { + squashObjects?: boolean + prefixKeys?: string + } + ) => Record<string, { type: string; name: string; prefixKeys: string }> +} From 83bc2e17dbc4bbf30ae234d44a58a431d5851ff8 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 14:45:00 +0100 Subject: [PATCH 060/131] Fix types --- .../frontend-core/src/components/grid/stores/datasource.ts | 4 ++-- packages/frontend-core/src/components/grid/stores/rows.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/frontend-core/src/components/grid/stores/datasource.ts b/packages/frontend-core/src/components/grid/stores/datasource.ts index 4c20e9493f..7fca6ace49 100644 --- a/packages/frontend-core/src/components/grid/stores/datasource.ts +++ b/packages/frontend-core/src/components/grid/stores/datasource.ts @@ -73,7 +73,7 @@ export const deriveStores = (context: StoreContext): DerivedDatasourceStore => { const schema = derived(definition, $definition => { const schema: Record<string, any> | null | undefined = getDatasourceSchema({ API, - datasource: get(datasource), + datasource: get(datasource) as any, definition: $definition ?? undefined, }) if (!schema) { @@ -184,7 +184,7 @@ export const createActions = (context: StoreContext): ActionDatasourceStore => { const refreshDefinition = async () => { const def = await getDatasourceDefinition({ API, - datasource: get(datasource), + datasource: get(datasource) as any, }) definition.set((def as any) ?? null) } diff --git a/packages/frontend-core/src/components/grid/stores/rows.ts b/packages/frontend-core/src/components/grid/stores/rows.ts index b9c9b3fe1e..72502b3dbd 100644 --- a/packages/frontend-core/src/components/grid/stores/rows.ts +++ b/packages/frontend-core/src/components/grid/stores/rows.ts @@ -254,7 +254,7 @@ export const createActions = (context: StoreContext): RowActionStore => { // Reset state properties when dataset changes if (!$instanceLoaded || resetRows) { - definition.set($fetch.definition) + definition.set($fetch.definition as any) } // Reset scroll state when data changes From d3ba4b103e27b58547b7ccf495c967d0edc8d012 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 15:51:48 +0100 Subject: [PATCH 061/131] Fix return type --- packages/server/src/api/controllers/row/views.ts | 1 + packages/types/src/api/web/pagination.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/server/src/api/controllers/row/views.ts b/packages/server/src/api/controllers/row/views.ts index dcf8680348..418aa462c4 100644 --- a/packages/server/src/api/controllers/row/views.ts +++ b/packages/server/src/api/controllers/row/views.ts @@ -54,6 +54,7 @@ export async function searchView( rows: result.rows, bookmark: result.bookmark, hasNextPage: result.hasNextPage, + totalRows: result.totalRows, } } diff --git a/packages/types/src/api/web/pagination.ts b/packages/types/src/api/web/pagination.ts index 48588bf6a1..f87bc97824 100644 --- a/packages/types/src/api/web/pagination.ts +++ b/packages/types/src/api/web/pagination.ts @@ -24,4 +24,5 @@ export interface PaginationRequest extends BasicPaginationRequest { export interface PaginationResponse { bookmark: string | number | undefined hasNextPage?: boolean + totalRows?: number } From 5f82a395174c6afe4930c6f04e8a65139fa4bf71 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 15:57:15 +0100 Subject: [PATCH 062/131] Undo some typings --- packages/frontend-core/src/fetch/DataFetch.ts | 2 +- packages/shared-core/src/filters.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index e5d899d596..c0be73dd2f 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -324,7 +324,7 @@ export default abstract class DataFetch< } // If we don't support sorting, do a client-side sort - if (!this.features.supportsSort && clientSideSorting) { + if (!this.features.supportsSort && clientSideSorting && sortType) { rows = sort(rows, sortColumn as any, sortOrder, sortType) } diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index a1e8534a95..b711d4cb61 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -552,7 +552,7 @@ export function search<T extends Record<string, any>>( */ export function runQuery<T extends Record<string, any>>( docs: T[], - query: SearchFilters | null + query: SearchFilters ): T[] { if (!docs || !Array.isArray(docs)) { return [] @@ -876,7 +876,7 @@ export function sort<T extends Record<string, any>>( docs: T[], sort: keyof T, sortOrder: SortOrder, - sortType: SortType | null = SortType.STRING + sortType = SortType.STRING ): T[] { if (!sort || !sortOrder || !sortType) { return docs From a8abe5bc432da6b7644095d9ebb2cf533f3bdb32 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 15:58:55 +0100 Subject: [PATCH 063/131] Remove deprecated fetch --- packages/types/src/ui/stores/grid/fetch.ts | 80 ---------------------- packages/types/src/ui/stores/grid/index.ts | 1 - 2 files changed, 81 deletions(-) delete mode 100644 packages/types/src/ui/stores/grid/fetch.ts diff --git a/packages/types/src/ui/stores/grid/fetch.ts b/packages/types/src/ui/stores/grid/fetch.ts deleted file mode 100644 index 5f42db24b0..0000000000 --- a/packages/types/src/ui/stores/grid/fetch.ts +++ /dev/null @@ -1,80 +0,0 @@ -import { - Row, - SearchFilters, - SortOrder, - SortType, - Table, - UIDatasource, - UILegacyFilter, - UISearchFilter, -} from "@budibase/types" - -interface SearchOptions { - query?: SearchFilters | null | undefined - limit: number - sort: string | null - sortOrder: string | undefined - sortType: SortType | null - paginate: boolean - bookmark: null -} - -interface TableAPI { - fetchTableDefinition(tableId: string): Promise<Table> - searchTable(tableId: string, options: SearchOptions): any -} - -interface ViewV2API { - fetchDefinition: (datasourceId: string) => Promise<any> - fetch: (datasourceId: string, options: SearchOptions) => any -} - -interface UserAPI { - searchUsers: (opts: { - bookmark: null - query: - | SearchFilters - | { - string: { - email: null - } - } - | null - appId: string - paginate: boolean - limit: number - }) => Promise<any> -} - -export interface UIFetchAPI extends TableAPI, UserAPI { - definition: UIDatasource - - getInitialData: () => Promise<void> - loading: any - loaded: boolean - - viewV2: ViewV2API - - resetKey: string | null - error: any - - hasNextPage: boolean - nextPage: () => Promise<void> - - rows: Row[] - - options?: { - datasource?: { - tableId: string - id: string - } - } - update: ({ - sortOrder, - sortColumn, - }: { - sortOrder?: SortOrder - sortColumn?: string - filter?: UILegacyFilter[] | UISearchFilter - }) => any -} diff --git a/packages/types/src/ui/stores/grid/index.ts b/packages/types/src/ui/stores/grid/index.ts index f419134452..7c3b6d4cb4 100644 --- a/packages/types/src/ui/stores/grid/index.ts +++ b/packages/types/src/ui/stores/grid/index.ts @@ -6,4 +6,3 @@ export * from "./view" export * from "./user" export * from "./filters" export * from "./rows" -export * from "./fetch" From 0ca0ba64336e8a1514ba18e5fbd3f7f887be5127 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 16:04:39 +0100 Subject: [PATCH 064/131] Type nulls --- packages/frontend-core/src/fetch/DataFetch.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index c0be73dd2f..74450c6254 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -19,14 +19,14 @@ const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils interface DataFetchStore<TDefinition, TQuery> { rows: Row[] - info: null + info: any schema: TableSchema | null loading: boolean loaded: boolean query: TQuery pageNumber: number - cursor: null - cursors: any[] + cursor: string | null + cursors: string[] resetKey: string error: { message: string From a414505eefdcfad7da76f8422f7539effd71bc4e Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 16:13:43 +0100 Subject: [PATCH 065/131] Cleanups --- .../frontend-core/src/components/grid/stores/datasource.ts | 4 ++-- packages/frontend-core/src/fetch/TableFetch.ts | 2 +- packages/frontend-core/src/fetch/UserFetch.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/frontend-core/src/components/grid/stores/datasource.ts b/packages/frontend-core/src/components/grid/stores/datasource.ts index 7fca6ace49..0b07796fde 100644 --- a/packages/frontend-core/src/components/grid/stores/datasource.ts +++ b/packages/frontend-core/src/components/grid/stores/datasource.ts @@ -71,7 +71,7 @@ export const deriveStores = (context: StoreContext): DerivedDatasourceStore => { } = context const schema = derived(definition, $definition => { - const schema: Record<string, any> | null | undefined = getDatasourceSchema({ + const schema: Record<string, any> | undefined = getDatasourceSchema({ API, datasource: get(datasource) as any, definition: $definition ?? undefined, @@ -186,7 +186,7 @@ export const createActions = (context: StoreContext): ActionDatasourceStore => { API, datasource: get(datasource) as any, }) - definition.set((def as any) ?? null) + definition.set(def as any) } // Saves the datasource definition diff --git a/packages/frontend-core/src/fetch/TableFetch.ts b/packages/frontend-core/src/fetch/TableFetch.ts index c1152f2869..f5927262cb 100644 --- a/packages/frontend-core/src/fetch/TableFetch.ts +++ b/packages/frontend-core/src/fetch/TableFetch.ts @@ -37,7 +37,7 @@ export default class TableFetch extends DataFetch<UITable, Table> { // Search table try { const res = await this.API.searchTable(tableId, { - query: query ?? undefined, + query, limit, sort: sortColumn, sortOrder: sortOrder ?? SortOrder.ASCENDING, diff --git a/packages/frontend-core/src/fetch/UserFetch.ts b/packages/frontend-core/src/fetch/UserFetch.ts index 8f1ef36cac..656cd840fe 100644 --- a/packages/frontend-core/src/fetch/UserFetch.ts +++ b/packages/frontend-core/src/fetch/UserFetch.ts @@ -52,7 +52,7 @@ export default class UserFetch extends DataFetch< const finalQuery: SearchFilters = utils.isSupportedUserSearch(rest) ? rest - : { [BasicOperator.EMPTY]: { email: true } } + : { [BasicOperator.EMPTY]: { email: null } } try { const opts: SearchUsersRequest = { From 0784a9571273abd3bddd74e14776df3f4bf23d18 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 16:33:29 +0100 Subject: [PATCH 066/131] Remove ! usage --- packages/frontend-core/src/api/views.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/frontend-core/src/api/views.ts b/packages/frontend-core/src/api/views.ts index 83f7e97df0..aa0f797f58 100644 --- a/packages/frontend-core/src/api/views.ts +++ b/packages/frontend-core/src/api/views.ts @@ -28,7 +28,9 @@ export const buildViewEndpoints = (API: BaseAPIClient): ViewEndpoints => ({ fetchViewData: async (name, { field, groupBy, calculation }) => { const params = new URLSearchParams() if (calculation) { - params.set("field", field!) + if (field) { + params.set("field", field) + } params.set("calculation", calculation) } if (groupBy) { From 6bd4cb47c204d1c2626c0d8d93c71b2b695679a4 Mon Sep 17 00:00:00 2001 From: Andrew Kingston <andrew@kingston.dev> Date: Wed, 8 Jan 2025 15:54:09 +0000 Subject: [PATCH 067/131] Add new UnsavedUser type and update controllers --- packages/builder/src/stores/portal/users.ts | 35 ++++++++++--------- packages/frontend-core/src/api/user.ts | 14 +++----- packages/types/src/api/web/user.ts | 4 ++- .../src/api/controllers/global/users.ts | 16 ++++++--- 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/packages/builder/src/stores/portal/users.ts b/packages/builder/src/stores/portal/users.ts index 7c0bec296e..9284ad2992 100644 --- a/packages/builder/src/stores/portal/users.ts +++ b/packages/builder/src/stores/portal/users.ts @@ -9,9 +9,18 @@ import { SearchUsersResponse, UpdateInviteRequest, User, + UserIdentifier, + UnsavedUser, } from "@budibase/types" import { BudiStore } from "../BudiStore" +interface UserInfo { + email: string + password: string + forceResetPassword?: boolean + role: keyof typeof Constants.BudibaseRoles +} + type UserState = SearchUsersResponse & SearchUsersRequest class UserStore extends BudiStore<UserState> { @@ -116,9 +125,9 @@ class UserStore extends BudiStore<UserState> { return await API.getUserCountByApp(appId) } - async create(data: any) { - let mappedUsers: Omit<User, "tenantId">[] = data.users.map((user: any) => { - const body: Omit<User, "tenantId"> = { + async create(data: { users: UserInfo[]; groups: any[] }) { + let mappedUsers: UnsavedUser[] = data.users.map((user: any) => { + const body: UnsavedUser = { email: user.email, password: user.password, roles: {}, @@ -128,17 +137,17 @@ class UserStore extends BudiStore<UserState> { } switch (user.role) { - case "appUser": + case Constants.BudibaseRoles.AppUser: body.builder = { global: false } body.admin = { global: false } break - case "developer": + case Constants.BudibaseRoles.Developer: body.builder = { global: true } break - case "creator": + case Constants.BudibaseRoles.Creator: body.builder = { creator: true, global: false } break - case "admin": + case Constants.BudibaseRoles.Admin: body.admin = { global: true } body.builder = { global: true } break @@ -157,12 +166,7 @@ class UserStore extends BudiStore<UserState> { await API.deleteUser(id) } - async bulkDelete( - users: Array<{ - userId: string - email: string - }> - ) { + async bulkDelete(users: UserIdentifier[]) { return API.deleteUsers(users) } @@ -199,9 +203,8 @@ class UserStore extends BudiStore<UserState> { } } - foo = this.refreshUsage(this.create) - bar = this.refreshUsage(this.save) - + // Wrapper function to refresh quota usage after an operation, + // persisting argument and return types refreshUsage<T extends any[], U>(fn: (...args: T) => Promise<U>) { return async function (...args: T) { const response = await fn(...args) diff --git a/packages/frontend-core/src/api/user.ts b/packages/frontend-core/src/api/user.ts index 7464b1ec4a..cf66751078 100644 --- a/packages/frontend-core/src/api/user.ts +++ b/packages/frontend-core/src/api/user.ts @@ -21,11 +21,12 @@ import { SaveUserResponse, SearchUsersRequest, SearchUsersResponse, + UnsavedUser, UpdateInviteRequest, UpdateInviteResponse, UpdateSelfMetadataRequest, UpdateSelfMetadataResponse, - User, + UserIdentifier, } from "@budibase/types" import { BaseAPIClient } from "./types" @@ -38,14 +39,9 @@ export interface UserEndpoints { createAdminUser: ( user: CreateAdminUserRequest ) => Promise<CreateAdminUserResponse> - saveUser: (user: User) => Promise<SaveUserResponse> + saveUser: (user: UnsavedUser) => Promise<SaveUserResponse> deleteUser: (userId: string) => Promise<DeleteUserResponse> - deleteUsers: ( - users: Array<{ - userId: string - email: string - }> - ) => Promise<BulkUserDeleted | undefined> + deleteUsers: (users: UserIdentifier[]) => Promise<BulkUserDeleted | undefined> onboardUsers: (data: InviteUsersRequest) => Promise<InviteUsersResponse> getUserInvite: (code: string) => Promise<CheckInviteResponse> getUserInvites: () => Promise<GetUserInvitesResponse> @@ -60,7 +56,7 @@ export interface UserEndpoints { getAccountHolder: () => Promise<LookupAccountHolderResponse> searchUsers: (data: SearchUsersRequest) => Promise<SearchUsersResponse> createUsers: ( - users: Omit<User, "tenantId">[], + users: UnsavedUser[], groups: any[] ) => Promise<BulkUserCreated | undefined> updateUserInvite: ( diff --git a/packages/types/src/api/web/user.ts b/packages/types/src/api/web/user.ts index 8b0dfef34b..c1f37fd3f0 100644 --- a/packages/types/src/api/web/user.ts +++ b/packages/types/src/api/web/user.ts @@ -22,6 +22,8 @@ export interface UserDetails { password?: string } +export type UnsavedUser = Omit<User, "tenantId"> + export interface BulkUserRequest { delete?: { users: Array<{ @@ -31,7 +33,7 @@ export interface BulkUserRequest { } create?: { roles?: any[] - users: User[] + users: UnsavedUser[] groups: any[] } } diff --git a/packages/worker/src/api/controllers/global/users.ts b/packages/worker/src/api/controllers/global/users.ts index a028f4fd33..4f9135e79e 100644 --- a/packages/worker/src/api/controllers/global/users.ts +++ b/packages/worker/src/api/controllers/global/users.ts @@ -33,6 +33,7 @@ import { SaveUserResponse, SearchUsersRequest, SearchUsersResponse, + UnsavedUser, UpdateInviteRequest, UpdateInviteResponse, User, @@ -49,6 +50,7 @@ import { tenancy, db, locks, + context, } from "@budibase/backend-core" import { checkAnyUserExists } from "../../../utilities/users" import { isEmailConfigured } from "../../../utilities/email" @@ -66,10 +68,11 @@ const generatePassword = (length: number) => { .slice(0, length) } -export const save = async (ctx: UserCtx<User, SaveUserResponse>) => { +export const save = async (ctx: UserCtx<UnsavedUser, SaveUserResponse>) => { try { const currentUserId = ctx.user?._id - const requestUser = ctx.request.body + const tenantId = context.getTenantId() + const requestUser: User = { ...ctx.request.body, tenantId } // Do not allow the account holder role to be changed const accountMetadata = await users.getExistingAccounts([requestUser.email]) @@ -149,7 +152,12 @@ export const bulkUpdate = async ( let created, deleted try { if (input.create) { - created = await bulkCreate(input.create.users, input.create.groups) + const tenantId = context.getTenantId() + const users: User[] = input.create.users.map(user => ({ + ...user, + tenantId, + })) + created = await bulkCreate(users, input.create.groups) } if (input.delete) { deleted = await bulkDelete(input.delete.users, currentUserId) @@ -441,7 +449,6 @@ export const checkInvite = async (ctx: UserCtx<void, CheckInviteResponse>) => { } catch (e) { console.warn("Error getting invite from code", e) ctx.throw(400, "There was a problem with the invite") - return } ctx.body = { email: invite.email, @@ -472,7 +479,6 @@ export const updateInvite = async ( invite = await cache.invite.getCode(code) } catch (e) { ctx.throw(400, "There was a problem with the invite") - return } let updated = { From 3a8942f487eb0fdd625d7815aa641977ac8b078c Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 17:02:34 +0100 Subject: [PATCH 068/131] Add todos --- .../src/components/grid/stores/datasource.ts | 14 ++++++++------ .../src/components/grid/stores/rows.ts | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/frontend-core/src/components/grid/stores/datasource.ts b/packages/frontend-core/src/components/grid/stores/datasource.ts index 0b07796fde..805ace5a8f 100644 --- a/packages/frontend-core/src/components/grid/stores/datasource.ts +++ b/packages/frontend-core/src/components/grid/stores/datasource.ts @@ -1,3 +1,5 @@ +// TODO: datasource and defitions are unions of the different implementations. At this point, the datasource does not know what type is being used, and the assignations will cause TS exceptions. Casting it "as any" for now. This should be fixed improving the type usages. + import { derived, get, Readable, Writable } from "svelte/store" import { getDatasourceDefinition, getDatasourceSchema } from "../../../fetch" import { enrichSchemaWithRelColumns, memo } from "../../../utils" @@ -73,7 +75,7 @@ export const deriveStores = (context: StoreContext): DerivedDatasourceStore => { const schema = derived(definition, $definition => { const schema: Record<string, any> | undefined = getDatasourceSchema({ API, - datasource: get(datasource) as any, + datasource: get(datasource) as any, // TODO: see line 1 definition: $definition ?? undefined, }) if (!schema) { @@ -130,7 +132,7 @@ export const deriveStores = (context: StoreContext): DerivedDatasourceStore => { ([$datasource, $definition]) => { let type = $datasource?.type if (type === "provider") { - type = ($datasource as any).value?.datasource?.type + type = ($datasource as any).value?.datasource?.type // TODO: see line 1 } // Handle calculation views if (type === "viewV2" && $definition?.type === ViewV2Type.CALCULATION) { @@ -184,9 +186,9 @@ export const createActions = (context: StoreContext): ActionDatasourceStore => { const refreshDefinition = async () => { const def = await getDatasourceDefinition({ API, - datasource: get(datasource) as any, + datasource: get(datasource) as any, // TODO: see line 1 }) - definition.set(def as any) + definition.set(def as any) // TODO: see line 1 } // Saves the datasource definition @@ -231,7 +233,7 @@ export const createActions = (context: StoreContext): ActionDatasourceStore => { if ("default" in newDefinition.schema[column]) { delete newDefinition.schema[column].default } - return await saveDefinition(newDefinition as any) + return await saveDefinition(newDefinition as any) // TODO: see line 1 } // Adds a schema mutation for a single field @@ -307,7 +309,7 @@ export const createActions = (context: StoreContext): ActionDatasourceStore => { await saveDefinition({ ...$definition, schema: newSchema, - } as any) + } as any) // TODO: see line 1 resetSchemaMutations() } diff --git a/packages/frontend-core/src/components/grid/stores/rows.ts b/packages/frontend-core/src/components/grid/stores/rows.ts index 72502b3dbd..d227fc70df 100644 --- a/packages/frontend-core/src/components/grid/stores/rows.ts +++ b/packages/frontend-core/src/components/grid/stores/rows.ts @@ -254,7 +254,7 @@ export const createActions = (context: StoreContext): RowActionStore => { // Reset state properties when dataset changes if (!$instanceLoaded || resetRows) { - definition.set($fetch.definition as any) + definition.set($fetch.definition as any) // TODO: datasource and defitions are unions of the different implementations. At this point, the datasource does not know what type is being used, and the assignations will cause TS exceptions. Casting it "as any" for now. This should be fixed improving the type usages. } // Reset scroll state when data changes From 23f9e3f3fe02584503a6eaebf58d21a077de22c8 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 17:06:19 +0100 Subject: [PATCH 069/131] Add todo --- packages/frontend-core/src/components/grid/stores/rows.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend-core/src/components/grid/stores/rows.ts b/packages/frontend-core/src/components/grid/stores/rows.ts index d227fc70df..07fbf02134 100644 --- a/packages/frontend-core/src/components/grid/stores/rows.ts +++ b/packages/frontend-core/src/components/grid/stores/rows.ts @@ -21,7 +21,7 @@ interface IndexedUIRow extends UIRow { interface RowStore { rows: Writable<UIRow[]> - fetch: Writable<DataFetch<any, any, any> | null> + fetch: Writable<DataFetch<any, any, any> | null> // TODO: type this properly, having a union of all the possible options loaded: Writable<boolean> refreshing: Writable<boolean> loading: Writable<boolean> From 7d6b822b8a76014d03ccad31c227857b660990b1 Mon Sep 17 00:00:00 2001 From: Sam Rose <hello@samwho.dev> Date: Wed, 8 Jan 2025 16:12:02 +0000 Subject: [PATCH 070/131] Improve typing around AI prompts. --- packages/pro | 2 +- .../src/api/controllers/row/staticFormula.ts | 5 -- .../types/src/documents/app/table/schema.ts | 57 ++++++++++++++++--- 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/packages/pro b/packages/pro index 45f5b6fe9b..788173a024 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit 45f5b6fe9bbdbdf502581740ab43b82e8153260f +Subproject commit 788173a024fd5ef98d3122b26dbc06d39fb51349 diff --git a/packages/server/src/api/controllers/row/staticFormula.ts b/packages/server/src/api/controllers/row/staticFormula.ts index afa3a1f239..e2743da366 100644 --- a/packages/server/src/api/controllers/row/staticFormula.ts +++ b/packages/server/src/api/controllers/row/staticFormula.ts @@ -163,11 +163,6 @@ export async function finaliseRow( contextRows: [enrichedRow], }) - const flag1 = await features.isEnabled(FeatureFlag.BUDIBASE_AI) - const flag2 = await pro.features.isBudibaseAIEnabled() - const flag3 = await features.isEnabled(FeatureFlag.AI_CUSTOM_CONFIGS) - const flag4 = await pro.features.isAICustomConfigsEnabled() - const aiEnabled = ((await features.isEnabled(FeatureFlag.BUDIBASE_AI)) && (await pro.features.isBudibaseAIEnabled())) || diff --git a/packages/types/src/documents/app/table/schema.ts b/packages/types/src/documents/app/table/schema.ts index f4a6d8481d..47a63e0dd9 100644 --- a/packages/types/src/documents/app/table/schema.ts +++ b/packages/types/src/documents/app/table/schema.ts @@ -118,16 +118,59 @@ export interface FormulaFieldMetadata extends BaseFieldSchema { responseType?: FormulaResponseType } -export interface AIFieldMetadata extends BaseFieldSchema { +interface AITranslateFieldMetadata extends BaseFieldSchema { type: FieldType.AI - operation: AIOperationEnum - columns?: string[] - column?: string - categories?: string - prompt?: string - language?: string + operation: AIOperationEnum.TRANSLATE + column: string + language: string } +interface AICleanDataFieldMetadata extends BaseFieldSchema { + type: FieldType.AI + operation: AIOperationEnum.CLEAN_DATA + column: string +} + +interface AICategoriseTextFieldMetadata extends BaseFieldSchema { + type: FieldType.AI + operation: AIOperationEnum.CATEGORISE_TEXT + columns: string[] + categories: string +} + +interface AISentimentAnalysisFieldMetadata extends BaseFieldSchema { + type: FieldType.AI + operation: AIOperationEnum.SENTIMENT_ANALYSIS + column: string +} + +interface AISearchWebFieldMetadata extends BaseFieldSchema { + type: FieldType.AI + operation: AIOperationEnum.SEARCH_WEB + columns: string[] +} + +interface AIPromptFieldMetadata extends BaseFieldSchema { + type: FieldType.AI + operation: AIOperationEnum.PROMPT + prompt: string +} + +interface AISummariseTextFieldMetadata extends BaseFieldSchema { + type: FieldType.AI + operation: AIOperationEnum.SUMMARISE_TEXT + columns: string[] +} + +export type AIFieldMetadata = + | AITranslateFieldMetadata + | AICleanDataFieldMetadata + | AICategoriseTextFieldMetadata + | AISentimentAnalysisFieldMetadata + | AIPromptFieldMetadata + | AISearchWebFieldMetadata + | AISummariseTextFieldMetadata + export interface BBReferenceFieldMetadata extends Omit<BaseFieldSchema, "subtype"> { type: FieldType.BB_REFERENCE From eb6a2434b6bfe635a5afa2b4a08e146fd0e0ef75 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Wed, 8 Jan 2025 17:18:33 +0100 Subject: [PATCH 071/131] Simplify code --- packages/frontend-core/src/fetch/DataFetch.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index 74450c6254..9312c57637 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -11,7 +11,6 @@ import { SortType, TableSchema, UISearchFilter, - ViewSchema, } from "@budibase/types" import { APIClient } from "../api/types" @@ -224,12 +223,12 @@ export default abstract class DataFetch< supportsPagination: paginate && !!features?.supportsPagination, } - if (!definition?.schema) { + // Fetch and enrich schema + let schema = this.getSchema(definition) + if (!schema) { return } - - // Fetch and enrich schema - const schema = this.enrichSchema(definition.schema) + schema = this.enrichSchema(schema) // If an invalid sort column is specified, delete it if (this.options.sortColumn && !schema[this.options.sortColumn]) { @@ -362,9 +361,7 @@ export default abstract class DataFetch< * @param definition the datasource definition * @return {object} the schema */ - getSchema( - definition: TDefinition | null - ): ViewSchema | Record<string, any> | undefined { + getSchema(definition: TDefinition | null): Record<string, any> | undefined { return definition?.schema ?? undefined } @@ -379,7 +376,7 @@ export default abstract class DataFetch< let jsonAdditions: Record<string, { type: string; nestedJSON: true }> = {} for (const fieldKey of Object.keys(schema)) { const fieldSchema = schema[fieldKey] - if (fieldSchema?.type === FieldType.JSON) { + if (fieldSchema.type === FieldType.JSON) { const jsonSchema = convertJSONSchemaToTableSchema(fieldSchema, { squashObjects: true, }) as Record<string, { type: string }> | null // TODO: remove when convertJSONSchemaToTableSchema is typed From 41e3e2d77415d153c50679eb38e597fc09429ac5 Mon Sep 17 00:00:00 2001 From: Andrew Kingston <andrew@kingston.dev> Date: Wed, 8 Jan 2025 16:52:33 +0000 Subject: [PATCH 072/131] Improve handling of configs --- packages/builder/src/stores/portal/oidc.ts | 12 ++++-------- packages/frontend-core/src/api/configs.ts | 4 ++-- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/builder/src/stores/portal/oidc.ts b/packages/builder/src/stores/portal/oidc.ts index a914645135..6c3609f9d5 100644 --- a/packages/builder/src/stores/portal/oidc.ts +++ b/packages/builder/src/stores/portal/oidc.ts @@ -11,14 +11,10 @@ class OIDCStore extends BudiStore<PublicOIDCConfig> { async init() { const tenantId = get(auth).tenantId - const config = await API.getOIDCConfig(tenantId) - if (Object.keys(config || {}).length) { - // Just use the first config for now. - // We will be support multiple logins buttons later on. - this.set(config[0]) - } else { - this.set({}) - } + const configs = await API.getOIDCConfigs(tenantId) + // Just use the first config for now. + // We will be support multiple logins buttons later on. + this.set(configs[0] || {}) } } diff --git a/packages/frontend-core/src/api/configs.ts b/packages/frontend-core/src/api/configs.ts index 82f08e58a7..408180a859 100644 --- a/packages/frontend-core/src/api/configs.ts +++ b/packages/frontend-core/src/api/configs.ts @@ -16,7 +16,7 @@ import { BaseAPIClient } from "./types" export interface ConfigEndpoints { getConfig: (type: ConfigType) => Promise<FindConfigResponse> getTenantConfig: (tentantId: string) => Promise<GetPublicSettingsResponse> - getOIDCConfig: (tenantId: string) => Promise<GetPublicOIDCConfigResponse> + getOIDCConfigs: (tenantId: string) => Promise<GetPublicOIDCConfigResponse> getOIDCLogos: () => Promise<Config<OIDCLogosConfig>> saveConfig: (config: SaveConfigRequest) => Promise<SaveConfigResponse> deleteConfig: (id: string, rev: string) => Promise<DeleteConfigResponse> @@ -73,7 +73,7 @@ export const buildConfigEndpoints = (API: BaseAPIClient): ConfigEndpoints => ({ * Gets the OIDC config for a certain tenant. * @param tenantId the tenant ID to get the config for */ - getOIDCConfig: async tenantId => { + getOIDCConfigs: async tenantId => { return await API.get({ url: `/api/global/configs/public/oidc?tenantId=${tenantId}`, }) From 68c2b29e6225225589c65c41a866a64670d17715 Mon Sep 17 00:00:00 2001 From: Sam Rose <hello@samwho.dev> Date: Wed, 8 Jan 2025 17:03:40 +0000 Subject: [PATCH 073/131] Admit defeat. --- packages/pro | 2 +- .../types/src/documents/app/table/schema.ts | 57 +++---------------- 2 files changed, 8 insertions(+), 51 deletions(-) diff --git a/packages/pro b/packages/pro index 788173a024..71922a7d97 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit 788173a024fd5ef98d3122b26dbc06d39fb51349 +Subproject commit 71922a7d979ddca6f51d9e450abcd81ba9a5b6fa diff --git a/packages/types/src/documents/app/table/schema.ts b/packages/types/src/documents/app/table/schema.ts index 47a63e0dd9..f4a6d8481d 100644 --- a/packages/types/src/documents/app/table/schema.ts +++ b/packages/types/src/documents/app/table/schema.ts @@ -118,59 +118,16 @@ export interface FormulaFieldMetadata extends BaseFieldSchema { responseType?: FormulaResponseType } -interface AITranslateFieldMetadata extends BaseFieldSchema { +export interface AIFieldMetadata extends BaseFieldSchema { type: FieldType.AI - operation: AIOperationEnum.TRANSLATE - column: string - language: string + operation: AIOperationEnum + columns?: string[] + column?: string + categories?: string + prompt?: string + language?: string } -interface AICleanDataFieldMetadata extends BaseFieldSchema { - type: FieldType.AI - operation: AIOperationEnum.CLEAN_DATA - column: string -} - -interface AICategoriseTextFieldMetadata extends BaseFieldSchema { - type: FieldType.AI - operation: AIOperationEnum.CATEGORISE_TEXT - columns: string[] - categories: string -} - -interface AISentimentAnalysisFieldMetadata extends BaseFieldSchema { - type: FieldType.AI - operation: AIOperationEnum.SENTIMENT_ANALYSIS - column: string -} - -interface AISearchWebFieldMetadata extends BaseFieldSchema { - type: FieldType.AI - operation: AIOperationEnum.SEARCH_WEB - columns: string[] -} - -interface AIPromptFieldMetadata extends BaseFieldSchema { - type: FieldType.AI - operation: AIOperationEnum.PROMPT - prompt: string -} - -interface AISummariseTextFieldMetadata extends BaseFieldSchema { - type: FieldType.AI - operation: AIOperationEnum.SUMMARISE_TEXT - columns: string[] -} - -export type AIFieldMetadata = - | AITranslateFieldMetadata - | AICleanDataFieldMetadata - | AICategoriseTextFieldMetadata - | AISentimentAnalysisFieldMetadata - | AIPromptFieldMetadata - | AISearchWebFieldMetadata - | AISummariseTextFieldMetadata - export interface BBReferenceFieldMetadata extends Omit<BaseFieldSchema, "subtype"> { type: FieldType.BB_REFERENCE From 352e16f347172ece0a69f73fcc97d8456ec6c848 Mon Sep 17 00:00:00 2001 From: Sam Rose <hello@samwho.dev> Date: Wed, 8 Jan 2025 17:09:02 +0000 Subject: [PATCH 074/131] Explain the weirdness with enriched view FieldType.AI columns in a comment for the next person to stumble on this. --- packages/server/src/sdk/app/views/index.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 7fc78b9085..4f978253d6 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -432,6 +432,21 @@ export async function enrichSchema( ...tableSchema[key], ...ui, order: anyViewOrder ? ui?.order ?? undefined : tableSchema[key]?.order, + // When this was written, the only column types in FieldSchema to have columns + // field were the relationship columns. We blank this out here to make sure it's + // not set on non-relationship columns, then below we populate it by calling + // populateRelSchema. + // + // For Budibase 3.0 we introduced the FieldType.AI fields. Some of these fields + // have `columns: string[]` and it flew under the radar here because the + // AIFieldMetadata type isn't a union on its subtypes, it has a collection of + // optional fields. So columns is `columns?: string[]` which allows undefined, + // and doesn't fail this type check. + // + // What this means in practice is when FieldType.AI fields get enriched, we + // delete their `columns`. At the time of writing, I don't believe anything in + // the frontend depends on this, but it is odd and will probably bite us at + // some point. columns: undefined, } From a18256a9cfe018c993d4c53b3e68f1a6189a520b Mon Sep 17 00:00:00 2001 From: Sam Rose <hello@samwho.dev> Date: Wed, 8 Jan 2025 17:10:02 +0000 Subject: [PATCH 075/131] Limit view AI column tests to internal tables. --- .../src/api/routes/tests/viewV2.spec.ts | 145 +++++++++--------- 1 file changed, 73 insertions(+), 72 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 57efc868e9..9531737d30 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -936,93 +936,94 @@ if (descriptions.length) { ) }) - describe("AI fields", () => { - let envCleanup: () => void - beforeAll(() => { - mocks.licenses.useBudibaseAI() - mocks.licenses.useAICustomConfigs() - envCleanup = setEnv({ - OPENAI_API_KEY: "sk-abcdefghijklmnopqrstuvwxyz1234567890abcd", + isInternal && + describe("AI fields", () => { + let envCleanup: () => void + beforeAll(() => { + mocks.licenses.useBudibaseAI() + mocks.licenses.useAICustomConfigs() + envCleanup = setEnv({ + OPENAI_API_KEY: "sk-abcdefghijklmnopqrstuvwxyz1234567890abcd", + }) + + mockChatGPTResponse(prompt => { + if (prompt.includes("elephant")) { + return "big" + } + if (prompt.includes("mouse")) { + return "small" + } + if (prompt.includes("whale")) { + return "big" + } + return "unknown" + }) }) - mockChatGPTResponse(prompt => { - if (prompt.includes("elephant")) { - return "big" - } - if (prompt.includes("mouse")) { - return "small" - } - if (prompt.includes("whale")) { - return "big" - } - return "unknown" + afterAll(() => { + nock.cleanAll() + envCleanup() + mocks.licenses.useCloudFree() }) - }) - afterAll(() => { - nock.cleanAll() - envCleanup() - mocks.licenses.useCloudFree() - }) - - it("can use AI fields in view calculations", async () => { - const table = await config.api.table.save( - saveTableRequest({ - schema: { - animal: { - name: "animal", - type: FieldType.STRING, + it("can use AI fields in view calculations", async () => { + const table = await config.api.table.save( + saveTableRequest({ + schema: { + animal: { + name: "animal", + type: FieldType.STRING, + }, + bigOrSmall: { + name: "bigOrSmall", + type: FieldType.AI, + operation: AIOperationEnum.CATEGORISE_TEXT, + categories: "big,small", + columns: ["animal"], + }, }, + }) + ) + + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + type: ViewV2Type.CALCULATION, + schema: { bigOrSmall: { - name: "bigOrSmall", - type: FieldType.AI, - operation: AIOperationEnum.CATEGORISE_TEXT, - categories: "big,small", - columns: ["animal"], + visible: true, + }, + count: { + visible: true, + calculationType: CalculationType.COUNT, + field: "animal", }, }, }) - ) - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - type: ViewV2Type.CALCULATION, - schema: { - bigOrSmall: { - visible: true, - }, - count: { - visible: true, - calculationType: CalculationType.COUNT, - field: "animal", - }, - }, - }) + await config.api.row.save(table._id!, { + animal: "elephant", + }) - await config.api.row.save(table._id!, { - animal: "elephant", - }) + await config.api.row.save(table._id!, { + animal: "mouse", + }) - await config.api.row.save(table._id!, { - animal: "mouse", - }) + await config.api.row.save(table._id!, { + animal: "whale", + }) - await config.api.row.save(table._id!, { - animal: "whale", + const { rows } = await config.api.row.search(view.id, { + sort: "bigOrSmall", + sortOrder: SortOrder.ASCENDING, + }) + expect(rows).toHaveLength(2) + expect(rows[0].bigOrSmall).toEqual("big") + expect(rows[1].bigOrSmall).toEqual("small") + expect(rows[0].count).toEqual(2) + expect(rows[1].count).toEqual(1) }) - - const { rows } = await config.api.row.search(view.id, { - sort: "bigOrSmall", - sortOrder: SortOrder.ASCENDING, - }) - expect(rows).toHaveLength(2) - expect(rows[0].bigOrSmall).toEqual("big") - expect(rows[1].bigOrSmall).toEqual("small") - expect(rows[0].count).toEqual(2) - expect(rows[1].count).toEqual(1) }) - }) }) describe("update", () => { From 2a9e4c307825cf6e97265a8436cfc9a58832ff07 Mon Sep 17 00:00:00 2001 From: Sam Rose <hello@samwho.dev> Date: Wed, 8 Jan 2025 17:34:08 +0000 Subject: [PATCH 076/131] Remove double processing of AI columns. --- packages/server/src/api/controllers/row/staticFormula.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/server/src/api/controllers/row/staticFormula.ts b/packages/server/src/api/controllers/row/staticFormula.ts index e2743da366..bd842ebfd0 100644 --- a/packages/server/src/api/controllers/row/staticFormula.ts +++ b/packages/server/src/api/controllers/row/staticFormula.ts @@ -185,11 +185,6 @@ export async function finaliseRow( enrichedRow = await processFormulas(table, enrichedRow, { dynamic: false, }) - if (aiEnabled) { - enrichedRow = await processAIColumns(table, enrichedRow, { - contextRows: [enrichedRow], - }) - } // this updates the related formulas in other rows based on the relations to this row if (updateFormula) { From 45ee1570370c9e41fe503a12401ac5664081a832 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 11:24:37 +0100 Subject: [PATCH 077/131] Rename file --- packages/client/src/stores/index.js | 2 +- packages/client/src/stores/{notification.js => notification.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename packages/client/src/stores/{notification.js => notification.ts} (100%) diff --git a/packages/client/src/stores/index.js b/packages/client/src/stores/index.js index f2b80ed732..e099434b3d 100644 --- a/packages/client/src/stores/index.js +++ b/packages/client/src/stores/index.js @@ -1,6 +1,6 @@ export { authStore } from "./auth" export { appStore } from "./app" -export { notificationStore } from "./notification" +export { notificationStore } from "./notification.ts" export { routeStore } from "./routes" export { screenStore } from "./screens" export { builderStore } from "./builder" diff --git a/packages/client/src/stores/notification.js b/packages/client/src/stores/notification.ts similarity index 100% rename from packages/client/src/stores/notification.js rename to packages/client/src/stores/notification.ts From 96cee21792413d638191e4a6872bf5e388d1786a Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 11:32:10 +0100 Subject: [PATCH 078/131] Type store --- packages/client/src/stores/notification.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/client/src/stores/notification.ts b/packages/client/src/stores/notification.ts index 054117aaba..6f5fcc9ef4 100644 --- a/packages/client/src/stores/notification.ts +++ b/packages/client/src/stores/notification.ts @@ -6,7 +6,7 @@ const DEFAULT_NOTIFICATION_TIMEOUT = 3000 const createNotificationStore = () => { let block = false - const store = writable([]) + const store = writable<{ id: string; message: string; count: number }[]>([]) const blockNotifications = (timeout = 1000) => { block = true @@ -14,17 +14,18 @@ const createNotificationStore = () => { } const send = ( - message, + message: string, type = "info", - icon, + icon: string, autoDismiss = true, - duration, + duration: number, count = 1 ) => { if (block) { return } + // @ts-expect-error if (get(routeStore).queryParams?.peek) { window.parent.postMessage({ type: "notification", @@ -66,7 +67,7 @@ const createNotificationStore = () => { } } - const dismiss = id => { + const dismiss = (id: string) => { store.update(state => { return state.filter(n => n.id !== id) }) @@ -76,13 +77,13 @@ const createNotificationStore = () => { subscribe: store.subscribe, actions: { send, - info: (msg, autoDismiss, duration) => + info: (msg: string, autoDismiss: boolean, duration: number) => send(msg, "info", "Info", autoDismiss ?? true, duration), - success: (msg, autoDismiss, duration) => + success: (msg: string, autoDismiss: boolean, duration: number) => send(msg, "success", "CheckmarkCircle", autoDismiss ?? true, duration), - warning: (msg, autoDismiss, duration) => + warning: (msg: string, autoDismiss: boolean, duration: number) => send(msg, "warning", "Alert", autoDismiss ?? true, duration), - error: (msg, autoDismiss, duration) => + error: (msg: string, autoDismiss: boolean, duration: number) => send(msg, "error", "Alert", autoDismiss ?? false, duration), blockNotifications, dismiss, From f938eb3297e5dd3de249d41c36ef7c0565f1996a Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 11:34:15 +0100 Subject: [PATCH 079/131] api to ts --- packages/client/src/api/{api.js => api.ts} | 6 +++++- packages/client/src/api/index.js | 2 +- packages/client/src/stores/notification.ts | 10 +++++----- 3 files changed, 11 insertions(+), 7 deletions(-) rename packages/client/src/api/{api.js => api.ts} (96%) diff --git a/packages/client/src/api/api.js b/packages/client/src/api/api.ts similarity index 96% rename from packages/client/src/api/api.js rename to packages/client/src/api/api.ts index d4c8faa4d2..59430e4ebd 100644 --- a/packages/client/src/api/api.js +++ b/packages/client/src/api/api.ts @@ -1,6 +1,10 @@ import { createAPIClient } from "@budibase/frontend-core" import { authStore } from "../stores/auth.js" -import { notificationStore, devToolsEnabled, devToolsStore } from "../stores/" +import { + notificationStore, + devToolsEnabled, + devToolsStore, +} from "../stores/index.js" import { get } from "svelte/store" export const API = createAPIClient({ diff --git a/packages/client/src/api/index.js b/packages/client/src/api/index.js index 5eb6b2b6f4..a63e19bfbb 100644 --- a/packages/client/src/api/index.js +++ b/packages/client/src/api/index.js @@ -1,4 +1,4 @@ -import { API } from "./api.js" +import { API } from "./api.ts" import { patchAPI } from "./patches.js" // Certain endpoints which return rows need patched so that they transform diff --git a/packages/client/src/stores/notification.ts b/packages/client/src/stores/notification.ts index 6f5fcc9ef4..5e1a4774c1 100644 --- a/packages/client/src/stores/notification.ts +++ b/packages/client/src/stores/notification.ts @@ -18,7 +18,7 @@ const createNotificationStore = () => { type = "info", icon: string, autoDismiss = true, - duration: number, + duration?: number, count = 1 ) => { if (block) { @@ -77,13 +77,13 @@ const createNotificationStore = () => { subscribe: store.subscribe, actions: { send, - info: (msg: string, autoDismiss: boolean, duration: number) => + info: (msg: string, autoDismiss?: boolean, duration?: number) => send(msg, "info", "Info", autoDismiss ?? true, duration), - success: (msg: string, autoDismiss: boolean, duration: number) => + success: (msg: string, autoDismiss?: boolean, duration?: number) => send(msg, "success", "CheckmarkCircle", autoDismiss ?? true, duration), - warning: (msg: string, autoDismiss: boolean, duration: number) => + warning: (msg: string, autoDismiss?: boolean, duration?: number) => send(msg, "warning", "Alert", autoDismiss ?? true, duration), - error: (msg: string, autoDismiss: boolean, duration: number) => + error: (msg: string, autoDismiss?: boolean, duration?: number) => send(msg, "error", "Alert", autoDismiss ?? false, duration), blockNotifications, dismiss, From a0950f15d9bb4d77dbdcc98d8f96fa93b78ef085 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 11:38:16 +0100 Subject: [PATCH 080/131] Add window typings --- packages/client/src/index.d.ts | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 packages/client/src/index.d.ts diff --git a/packages/client/src/index.d.ts b/packages/client/src/index.d.ts new file mode 100644 index 0000000000..7e13670b33 --- /dev/null +++ b/packages/client/src/index.d.ts @@ -0,0 +1,5 @@ +interface Window { + "##BUDIBASE_APP_ID##": string + "##BUDIBASE_IN_BUILDER##": string + MIGRATING_APP: boolean +} From 60c23ae021614dee53eb62a08e0ae9c4da974ff9 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 11:42:35 +0100 Subject: [PATCH 081/131] Type auth store --- packages/client/src/stores/{auth.js => auth.ts} | 4 +++- packages/types/src/api/web/global/self.ts | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) rename packages/client/src/stores/{auth.js => auth.ts} (94%) diff --git a/packages/client/src/stores/auth.js b/packages/client/src/stores/auth.ts similarity index 94% rename from packages/client/src/stores/auth.js rename to packages/client/src/stores/auth.ts index 214cc7bce2..15f44e7c32 100644 --- a/packages/client/src/stores/auth.js +++ b/packages/client/src/stores/auth.ts @@ -2,7 +2,9 @@ import { API } from "api" import { writable } from "svelte/store" const createAuthStore = () => { - const store = writable(null) + const store = writable<{ + csrfToken?: string + } | null>(null) // Fetches the user object if someone is logged in and has reloaded the page const fetchUser = async () => { diff --git a/packages/types/src/api/web/global/self.ts b/packages/types/src/api/web/global/self.ts index 517559d1ca..9d99a1f1a5 100644 --- a/packages/types/src/api/web/global/self.ts +++ b/packages/types/src/api/web/global/self.ts @@ -15,5 +15,5 @@ export interface GetGlobalSelfResponse extends User { license: License budibaseAccess: boolean accountPortalAccess: boolean - csrfToken: boolean + csrfToken: string } From 6e816fb6a2ae3aa8ec13f8d5fc887bc88b3c25fe Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 11:51:27 +0100 Subject: [PATCH 082/131] Fixes --- packages/client/src/api/api.ts | 2 +- packages/client/src/stores/derived/currentRole.js | 2 +- packages/client/src/stores/notification.ts | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/client/src/api/api.ts b/packages/client/src/api/api.ts index 59430e4ebd..93f59f6e9a 100644 --- a/packages/client/src/api/api.ts +++ b/packages/client/src/api/api.ts @@ -1,5 +1,5 @@ import { createAPIClient } from "@budibase/frontend-core" -import { authStore } from "../stores/auth.js" +import { authStore } from "../stores/auth" import { notificationStore, devToolsEnabled, diff --git a/packages/client/src/stores/derived/currentRole.js b/packages/client/src/stores/derived/currentRole.js index 8bb4c5a25d..056a05f8ab 100644 --- a/packages/client/src/stores/derived/currentRole.js +++ b/packages/client/src/stores/derived/currentRole.js @@ -1,7 +1,7 @@ import { derived } from "svelte/store" import { Constants } from "@budibase/frontend-core" import { devToolsStore } from "../devTools.js" -import { authStore } from "../auth.js" +import { authStore } from "../auth" import { devToolsEnabled } from "./devToolsEnabled.js" // Derive the current role of the logged-in user diff --git a/packages/client/src/stores/notification.ts b/packages/client/src/stores/notification.ts index 5e1a4774c1..fa28b9f40a 100644 --- a/packages/client/src/stores/notification.ts +++ b/packages/client/src/stores/notification.ts @@ -25,7 +25,6 @@ const createNotificationStore = () => { return } - // @ts-expect-error if (get(routeStore).queryParams?.peek) { window.parent.postMessage({ type: "notification", From 1b6dc51f018c80938c8f45aaf2852734f3981076 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 11:51:38 +0100 Subject: [PATCH 083/131] Type routes --- .../src/stores/{routes.js => routes.ts} | 30 ++++++++++++++----- packages/types/src/documents/app/screen.ts | 1 + 2 files changed, 24 insertions(+), 7 deletions(-) rename packages/client/src/stores/{routes.js => routes.ts} (82%) diff --git a/packages/client/src/stores/routes.js b/packages/client/src/stores/routes.ts similarity index 82% rename from packages/client/src/stores/routes.js rename to packages/client/src/stores/routes.ts index 8e318af2e3..3f200a9c88 100644 --- a/packages/client/src/stores/routes.js +++ b/packages/client/src/stores/routes.ts @@ -4,8 +4,24 @@ import { API } from "api" import { peekStore } from "./peek" import { builderStore } from "./builder" +interface Route { + path: string + screenId: string +} + +interface StoreType { + routes: Route[] + routeParams: {} + activeRoute?: Route | null + routeSessionId: number + routerLoaded: boolean + queryParams?: { + peek?: boolean + } +} + const createRouteStore = () => { - const initialState = { + const initialState: StoreType = { routes: [], routeParams: {}, activeRoute: null, @@ -22,7 +38,7 @@ const createRouteStore = () => { } catch (error) { routeConfig = null } - let routes = [] + const routes: Route[] = [] Object.values(routeConfig?.routes || {}).forEach(route => { Object.entries(route.subpaths || {}).forEach(([path, config]) => { routes.push({ @@ -43,13 +59,13 @@ const createRouteStore = () => { return state }) } - const setRouteParams = routeParams => { + const setRouteParams = (routeParams: StoreType["routeParams"]) => { store.update(state => { state.routeParams = routeParams return state }) } - const setQueryParams = queryParams => { + const setQueryParams = (queryParams: { peek?: boolean }) => { store.update(state => { state.queryParams = { ...queryParams, @@ -60,13 +76,13 @@ const createRouteStore = () => { return state }) } - const setActiveRoute = route => { + const setActiveRoute = (route: string) => { store.update(state => { state.activeRoute = state.routes.find(x => x.path === route) return state }) } - const navigate = (url, peek, externalNewTab) => { + const navigate = (url: string, peek: boolean, externalNewTab: boolean) => { if (get(builderStore).inBuilder) { return } @@ -93,7 +109,7 @@ const createRouteStore = () => { const setRouterLoaded = () => { store.update(state => ({ ...state, routerLoaded: true })) } - const createFullURL = relativeURL => { + const createFullURL = (relativeURL: string) => { if (!relativeURL?.startsWith("/")) { return relativeURL } diff --git a/packages/types/src/documents/app/screen.ts b/packages/types/src/documents/app/screen.ts index b2cedf31a9..e85fd5953b 100644 --- a/packages/types/src/documents/app/screen.ts +++ b/packages/types/src/documents/app/screen.ts @@ -36,6 +36,7 @@ export type ScreenRoutingJson = Record< subpaths: Record< string, { + screenId: string screens: Record<string, string> } > From 59bdd201585b90c1e87e6cfaa7b82becf2a3ef01 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 12:12:43 +0100 Subject: [PATCH 084/131] Type onError --- packages/frontend-core/src/api/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend-core/src/api/types.ts b/packages/frontend-core/src/api/types.ts index 0db1049591..940927f499 100644 --- a/packages/frontend-core/src/api/types.ts +++ b/packages/frontend-core/src/api/types.ts @@ -46,7 +46,7 @@ export type Headers = Record<string, string> export type APIClientConfig = { enableCaching?: boolean attachHeaders?: (headers: Headers) => void - onError?: (error: any) => void + onError?: (error: APIError) => void onMigrationDetected?: (migration: string) => void } From e6c45d934b70ae496a8827b5f17d75cc5c8af5b8 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 12:39:44 +0100 Subject: [PATCH 085/131] Stringify api errors --- packages/frontend-core/src/api/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend-core/src/api/index.ts b/packages/frontend-core/src/api/index.ts index f7b05c338a..44e85e5a68 100644 --- a/packages/frontend-core/src/api/index.ts +++ b/packages/frontend-core/src/api/index.ts @@ -74,7 +74,7 @@ export const createAPIClient = (config: APIClientConfig = {}): APIClient => { if (json?.message) { message = json.message } else if (json?.error) { - message = json.error + message = JSON.stringify(json.error) } } catch (error) { // Do nothing From 2cbf7a47c16194741d49dc654d1d0ea059c3d54d Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 12:40:23 +0100 Subject: [PATCH 086/131] Type --- packages/frontend-core/src/api/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend-core/src/api/index.ts b/packages/frontend-core/src/api/index.ts index 44e85e5a68..b0e1970e6f 100644 --- a/packages/frontend-core/src/api/index.ts +++ b/packages/frontend-core/src/api/index.ts @@ -226,7 +226,7 @@ export const createAPIClient = (config: APIClientConfig = {}): APIClient => { return await handler(callConfig) } catch (error) { if (config?.onError) { - config.onError(error) + config.onError(error as APIError) } throw error } From 9fade65b4a3895c7ab0609b0654d5ac3d44d455d Mon Sep 17 00:00:00 2001 From: Sam Rose <hello@samwho.dev> Date: Thu, 9 Jan 2025 11:46:45 +0000 Subject: [PATCH 087/131] Update pro reference. --- packages/pro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pro b/packages/pro index 71922a7d97..15b7f0907e 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit 71922a7d979ddca6f51d9e450abcd81ba9a5b6fa +Subproject commit 15b7f0907e66e4144338404bb071bc1ccfc98137 From 2b90d972b21325608f0f1e4f51d0d1a850be2e26 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 12:50:19 +0100 Subject: [PATCH 088/131] Clean code --- packages/frontend-core/src/api/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend-core/src/api/index.ts b/packages/frontend-core/src/api/index.ts index b0e1970e6f..92dc2d9ea3 100644 --- a/packages/frontend-core/src/api/index.ts +++ b/packages/frontend-core/src/api/index.ts @@ -68,7 +68,7 @@ export const createAPIClient = (config: APIClientConfig = {}): APIClient => { ): Promise<APIError> => { // Try to read a message from the error let message = response.statusText - let json: any = null + let json = null try { json = await response.json() if (json?.message) { From 71c6480c1b3ea08cd69bc8a0cfe015aadbaa773b Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 13:00:02 +0100 Subject: [PATCH 089/131] Remove unused function --- packages/frontend-core/src/api/index.ts | 6 +----- packages/frontend-core/src/api/types.ts | 3 +-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/frontend-core/src/api/index.ts b/packages/frontend-core/src/api/index.ts index 92dc2d9ea3..6efc90023a 100644 --- a/packages/frontend-core/src/api/index.ts +++ b/packages/frontend-core/src/api/index.ts @@ -93,7 +93,7 @@ export const createAPIClient = (config: APIClientConfig = {}): APIClient => { // Generates an error object from a string const makeError = ( message: string, - url?: string, + url: string, method?: HTTPMethod ): APIError => { return { @@ -239,13 +239,9 @@ export const createAPIClient = (config: APIClientConfig = {}): APIClient => { patch: requestApiCall(HTTPMethod.PATCH), delete: requestApiCall(HTTPMethod.DELETE), put: requestApiCall(HTTPMethod.PUT), - error: (message: string) => { - throw makeError(message) - }, invalidateCache: () => { cache = {} }, - // Generic utility to extract the current app ID. Assumes that any client // that exists in an app context will be attaching our app ID header. getAppID: (): string => { diff --git a/packages/frontend-core/src/api/types.ts b/packages/frontend-core/src/api/types.ts index 940927f499..4819b4cd3b 100644 --- a/packages/frontend-core/src/api/types.ts +++ b/packages/frontend-core/src/api/types.ts @@ -86,14 +86,13 @@ export type BaseAPIClient = { patch: <RequestT = null, ResponseT = void>( params: APICallParams<RequestT, ResponseT> ) => Promise<ResponseT> - error: (message: string) => void invalidateCache: () => void getAppID: () => string } export type APIError = { message?: string - url?: string + url: string method?: HTTPMethod json: any status: number From ace5099a8cf25f6eb73878d295568cb4130ea344 Mon Sep 17 00:00:00 2001 From: Sam Rose <hello@samwho.dev> Date: Thu, 9 Jan 2025 12:05:45 +0000 Subject: [PATCH 090/131] Update pro reference. --- packages/pro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pro b/packages/pro index 23fdd50b7e..193476cdfa 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit 23fdd50b7ef28cf320716ed2c46e15d63185daa7 +Subproject commit 193476cdfade6d3c613e6972f16ee0c527e01ff6 From 70a8b9e4688249211f5e9d56463e3524a97b2539 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 13:55:00 +0100 Subject: [PATCH 091/131] Remove screen id --- packages/types/src/documents/app/screen.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/types/src/documents/app/screen.ts b/packages/types/src/documents/app/screen.ts index e85fd5953b..b2cedf31a9 100644 --- a/packages/types/src/documents/app/screen.ts +++ b/packages/types/src/documents/app/screen.ts @@ -36,7 +36,6 @@ export type ScreenRoutingJson = Record< subpaths: Record< string, { - screenId: string screens: Record<string, string> } > From 88e245a5bb7a97eedfc07ea6104e44c5d2dfbd2a Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 14:01:07 +0100 Subject: [PATCH 092/131] Fix typing --- packages/types/src/documents/app/screen.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/types/src/documents/app/screen.ts b/packages/types/src/documents/app/screen.ts index b2cedf31a9..a8c32118d3 100644 --- a/packages/types/src/documents/app/screen.ts +++ b/packages/types/src/documents/app/screen.ts @@ -33,11 +33,6 @@ export interface ScreenRoutesViewOutput extends Document { export type ScreenRoutingJson = Record< string, { - subpaths: Record< - string, - { - screens: Record<string, string> - } - > + subpaths: Record<string, any> } > From 02b20f1102ebc05f3c292eae68df8c8a9e2b7417 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 14:24:59 +0100 Subject: [PATCH 093/131] Fix getSchema from forms --- packages/client/src/utils/schema.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/utils/schema.js b/packages/client/src/utils/schema.js index ffab142cf3..4a65d2d099 100644 --- a/packages/client/src/utils/schema.js +++ b/packages/client/src/utils/schema.js @@ -52,7 +52,7 @@ export const fetchDatasourceSchema = async ( // Get the normal schema as long as we aren't wanting a form schema let schema if (datasource?.type !== "query" || !options?.formSchema) { - schema = instance.getSchema(datasource, definition) + schema = instance.getSchema(definition) } else if (definition.parameters?.length) { schema = {} definition.parameters.forEach(param => { From ff0a25f0ffadf727418f757736fcc0b6091ff412 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 14:32:15 +0100 Subject: [PATCH 094/131] Pass datasource to instance --- packages/client/src/utils/schema.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/utils/schema.js b/packages/client/src/utils/schema.js index 4a65d2d099..60800afc53 100644 --- a/packages/client/src/utils/schema.js +++ b/packages/client/src/utils/schema.js @@ -31,7 +31,7 @@ const getDatasourceFetchInstance = datasource => { if (!handler) { return null } - return new handler({ API }) + return new handler({ API, datasource }) } /** From a5ec51028099325a288ab9fc4bca5e1978d634cb Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 15:58:59 +0100 Subject: [PATCH 095/131] Explicitly fetch the data from fetch --- packages/frontend-core/src/fetch/DataFetch.ts | 3 --- packages/frontend-core/src/fetch/index.ts | 7 ++++++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index 9312c57637..0079fec057 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -179,9 +179,6 @@ export default abstract class DataFetch< this.store.update($store => ({ ...$store, loaded: true })) return } - - // Initially fetch data but don't bother waiting for the result - this.getInitialData() } /** diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index 4accb0b5ec..c1f35abef2 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -33,7 +33,12 @@ const DataFetchMap = { export const fetchData = ({ API, datasource, options }: any) => { const Fetch = DataFetchMap[datasource?.type as keyof typeof DataFetchMap] || TableFetch - return new Fetch({ API, datasource, ...options }) + const fetch = new Fetch({ API, datasource, ...options }) + + // Initially fetch data but don't bother waiting for the result + fetch.getInitialData() + + return fetch } // Creates an empty fetch instance with no datasource configured, so no data From 5c933c7f80ef4f0e631fafed7dc16d8691823079 Mon Sep 17 00:00:00 2001 From: Michael Drury <me@michaeldrury.co.uk> Date: Thu, 9 Jan 2025 16:27:59 +0000 Subject: [PATCH 096/131] Revert "Fix issue with forms after cleanup" --- packages/client/src/utils/schema.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/client/src/utils/schema.js b/packages/client/src/utils/schema.js index 60800afc53..ffab142cf3 100644 --- a/packages/client/src/utils/schema.js +++ b/packages/client/src/utils/schema.js @@ -31,7 +31,7 @@ const getDatasourceFetchInstance = datasource => { if (!handler) { return null } - return new handler({ API, datasource }) + return new handler({ API }) } /** @@ -52,7 +52,7 @@ export const fetchDatasourceSchema = async ( // Get the normal schema as long as we aren't wanting a form schema let schema if (datasource?.type !== "query" || !options?.formSchema) { - schema = instance.getSchema(definition) + schema = instance.getSchema(datasource, definition) } else if (definition.parameters?.length) { schema = {} definition.parameters.forEach(param => { From 26c0861e6899b196eed64e0396080a6a6e905d3f Mon Sep 17 00:00:00 2001 From: Sam Rose <hello@samwho.dev> Date: Thu, 9 Jan 2025 17:05:44 +0000 Subject: [PATCH 097/131] Fix date-only searching. --- .../src/middleware/errorHandling.ts | 6 +- packages/backend-core/src/sql/sql.ts | 54 ++++++++- packages/backend-core/src/sql/utils.ts | 12 +- .../src/api/routes/tests/search.spec.ts | 106 ++++++++++++++++++ .../src/utilities/rowProcessor/index.ts | 9 ++ packages/shared-core/src/filters.ts | 22 +++- 6 files changed, 196 insertions(+), 13 deletions(-) diff --git a/packages/backend-core/src/middleware/errorHandling.ts b/packages/backend-core/src/middleware/errorHandling.ts index 6ceda9cd3a..5a5a25b461 100644 --- a/packages/backend-core/src/middleware/errorHandling.ts +++ b/packages/backend-core/src/middleware/errorHandling.ts @@ -32,8 +32,12 @@ export async function errorHandling(ctx: any, next: any) { } if (environment.isTest() && ctx.headers["x-budibase-include-stacktrace"]) { + let rootErr = err + while (rootErr.cause) { + rootErr = rootErr.cause + } // @ts-ignore - error.stack = err.stack + error.stack = rootErr.stack } ctx.body = error diff --git a/packages/backend-core/src/sql/sql.ts b/packages/backend-core/src/sql/sql.ts index 5f462ee144..9b0c49d9f8 100644 --- a/packages/backend-core/src/sql/sql.ts +++ b/packages/backend-core/src/sql/sql.ts @@ -816,14 +816,29 @@ class InternalBuilder { filters.oneOf, ArrayOperator.ONE_OF, (q, key: string, array) => { + const schema = this.getFieldSchema(key) + const values = Array.isArray(array) ? array : [array] if (shouldOr) { q = q.or } if (this.client === SqlClient.ORACLE) { // @ts-ignore key = this.convertClobs(key) + } else if ( + this.client === SqlClient.SQL_LITE && + schema?.type === FieldType.DATETIME && + schema.dateOnly + ) { + for (const value of values) { + if (value != null) { + q = q.or.whereLike(key, `${value.toISOString().slice(0, 10)}%`) + } else { + q = q.or.whereNull(key) + } + } + return q } - return q.whereIn(key, Array.isArray(array) ? array : [array]) + return q.whereIn(key, values) }, (q, key: string[], array) => { if (shouldOr) { @@ -882,6 +897,19 @@ class InternalBuilder { let high = value.high let low = value.low + if ( + this.client === SqlClient.SQL_LITE && + schema?.type === FieldType.DATETIME && + schema.dateOnly + ) { + if (high != null) { + high = `${high.toISOString().slice(0, 10)}T23:59:59.999Z` + } + if (low != null) { + low = low.toISOString().slice(0, 10) + } + } + if (this.client === SqlClient.ORACLE) { rawKey = this.convertClobs(key) } else if ( @@ -914,6 +942,7 @@ class InternalBuilder { } if (filters.equal) { iterate(filters.equal, BasicOperator.EQUAL, (q, key, value) => { + const schema = this.getFieldSchema(key) if (shouldOr) { q = q.or } @@ -928,6 +957,16 @@ class InternalBuilder { // @ts-expect-error knex types are wrong, raw is fine here subq.whereNotNull(identifier).andWhere(identifier, value) ) + } else if ( + this.client === SqlClient.SQL_LITE && + schema?.type === FieldType.DATETIME && + schema.dateOnly + ) { + if (value != null) { + return q.whereLike(key, `${value.toISOString().slice(0, 10)}%`) + } else { + return q.whereNull(key) + } } else { return q.whereRaw(`COALESCE(?? = ?, FALSE)`, [ this.rawQuotedIdentifier(key), @@ -938,6 +977,7 @@ class InternalBuilder { } if (filters.notEqual) { iterate(filters.notEqual, BasicOperator.NOT_EQUAL, (q, key, value) => { + const schema = this.getFieldSchema(key) if (shouldOr) { q = q.or } @@ -959,6 +999,18 @@ class InternalBuilder { // @ts-expect-error knex types are wrong, raw is fine here .or.whereNull(identifier) ) + } else if ( + this.client === SqlClient.SQL_LITE && + schema?.type === FieldType.DATETIME && + schema.dateOnly + ) { + if (value != null) { + return q.not + .whereLike(key, `${value.toISOString().slice(0, 10)}%`) + .or.whereNull(key) + } else { + return q.not.whereNull(key) + } } else { return q.whereRaw(`COALESCE(?? != ?, TRUE)`, [ this.rawQuotedIdentifier(key), diff --git a/packages/backend-core/src/sql/utils.ts b/packages/backend-core/src/sql/utils.ts index 16b352995b..b07854b2a0 100644 --- a/packages/backend-core/src/sql/utils.ts +++ b/packages/backend-core/src/sql/utils.ts @@ -14,7 +14,7 @@ import environment from "../environment" const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}` const ROW_ID_REGEX = /^\[.*]$/g const ENCODED_SPACE = encodeURIComponent(" ") -const ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/ +const ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}:\d{2}.\d{3}Z)?$/ const TIME_REGEX = /^(?:\d{2}:)?(?:\d{2}:)(?:\d{2})$/ export function isExternalTableID(tableId: string) { @@ -149,15 +149,7 @@ export function isInvalidISODateString(str: string) { } export function isValidISODateString(str: string) { - const trimmedValue = str.trim() - if (!ISO_DATE_REGEX.test(trimmedValue)) { - return false - } - let d = new Date(trimmedValue) - if (isNaN(d.getTime())) { - return false - } - return d.toISOString() === trimmedValue + return ISO_DATE_REGEX.test(str.trim()) } export function isValidFilter(value: any) { diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index e94e567b43..dca1bf9394 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -1683,6 +1683,112 @@ if (descriptions.length) { }) }) + describe.only("datetime - date only", () => { + const JAN_1ST = "2020-01-01" + const JAN_10TH = "2020-01-10" + const JAN_30TH = "2020-01-30" + const UNEXISTING_DATE = "2020-01-03" + const NULL_DATE__ID = `null_date__id` + + beforeAll(async () => { + tableOrViewId = await createTableOrView({ + dateid: { name: "dateid", type: FieldType.STRING }, + date: { + name: "date", + type: FieldType.DATETIME, + dateOnly: true, + }, + }) + + await createRows([ + { dateid: NULL_DATE__ID, date: null }, + { date: JAN_1ST }, + { date: `${JAN_10TH}T00:00:00.000Z` }, + ]) + }) + + describe("equal", () => { + it("successfully finds a row", async () => { + await expectQuery({ + equal: { date: JAN_1ST }, + }).toContainExactly([{ date: JAN_1ST }]) + }) + + it("successfully finds an ISO8601 row", async () => { + await expectQuery({ + equal: { date: JAN_10TH }, + }).toContainExactly([{ date: JAN_10TH }]) + }) + + it("finds a row with ISO8601 timestamp", async () => { + await expectQuery({ + equal: { date: `${JAN_1ST}T00:00:00.000Z` }, + }).toContainExactly([{ date: JAN_1ST }]) + }) + + it("fails to find nonexistent row", async () => { + await expectQuery({ + equal: { date: UNEXISTING_DATE }, + }).toFindNothing() + }) + }) + + describe("notEqual", () => { + it("successfully finds a row", async () => { + await expectQuery({ + notEqual: { date: JAN_1ST }, + }).toContainExactly([ + { date: JAN_10TH }, + { dateid: NULL_DATE__ID }, + ]) + }) + + it("fails to find nonexistent row", async () => { + await expectQuery({ + notEqual: { date: JAN_30TH }, + }).toContainExactly([ + { date: JAN_1ST }, + { date: JAN_10TH }, + { dateid: NULL_DATE__ID }, + ]) + }) + }) + + describe("oneOf", () => { + it("successfully finds a row", async () => { + await expectQuery({ + oneOf: { date: [JAN_1ST] }, + }).toContainExactly([{ date: JAN_1ST }]) + }) + + it("fails to find nonexistent row", async () => { + await expectQuery({ + oneOf: { date: [UNEXISTING_DATE] }, + }).toFindNothing() + }) + }) + + describe("range", () => { + it("successfully finds a row", async () => { + await expectQuery({ + range: { date: { low: JAN_1ST, high: JAN_1ST } }, + }).toContainExactly([{ date: JAN_1ST }]) + }) + + it("successfully finds multiple rows", async () => { + await expectQuery({ + range: { date: { low: JAN_1ST, high: JAN_10TH } }, + }).toContainExactly([{ date: JAN_1ST }, { date: JAN_10TH }]) + }) + + it("successfully finds no rows", async () => { + await expectQuery({ + range: { date: { low: JAN_30TH, high: JAN_30TH } }, + }).toFindNothing() + }) + }) + }) + isInternal && !isInMemory && describe("AI Column", () => { diff --git a/packages/server/src/utilities/rowProcessor/index.ts b/packages/server/src/utilities/rowProcessor/index.ts index 14b524fd95..8595a3483e 100644 --- a/packages/server/src/utilities/rowProcessor/index.ts +++ b/packages/server/src/utilities/rowProcessor/index.ts @@ -411,6 +411,15 @@ export async function coreOutputProcessing( row[property] = `${hours}:${minutes}:${seconds}` } } + } else if (column.type === FieldType.DATETIME && column.dateOnly) { + for (const row of rows) { + if (typeof row[property] === "string") { + row[property] = new Date(row[property]) + } + if (row[property] instanceof Date) { + row[property] = row[property].toISOString().slice(0, 10) + } + } } else if (column.type === FieldType.LINK) { for (let row of rows) { // if relationship is empty - remove the array, this has been part of the API for some time diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index b711d4cb61..afe99d9565 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -699,7 +699,27 @@ export function runQuery<T extends Record<string, any>>( return docValue._id === testValue } - return docValue === testValue + if (docValue === testValue) { + return true + } + + if (docValue == null && testValue != null) { + return false + } + + if (docValue != null && testValue == null) { + return false + } + + const leftDate = dayjs(docValue) + if (leftDate.isValid()) { + const rightDate = dayjs(testValue) + if (rightDate.isValid()) { + return leftDate.isSame(rightDate) + } + } + + return false } const not = From 7ad5879c3eb19783bd5a9d4beb36b835fe02fc3e Mon Sep 17 00:00:00 2001 From: Sam Rose <hello@samwho.dev> Date: Thu, 9 Jan 2025 17:16:47 +0000 Subject: [PATCH 098/131] Create search cross product on whether we save/search with timestamp. --- .../src/api/routes/tests/search.spec.ts | 219 +++++++++++------- 1 file changed, 129 insertions(+), 90 deletions(-) diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index dca1bf9394..69c68aad97 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -1684,109 +1684,148 @@ if (descriptions.length) { }) describe.only("datetime - date only", () => { - const JAN_1ST = "2020-01-01" - const JAN_10TH = "2020-01-10" - const JAN_30TH = "2020-01-30" - const UNEXISTING_DATE = "2020-01-03" - const NULL_DATE__ID = `null_date__id` + describe.each([true, false])( + "saved with timestamp: %s", + saveWithTimestamp => { + describe.each([true, false])( + "search with timestamp: %s", + searchWithTimestamp => { + const SAVE_SUFFIX = saveWithTimestamp + ? "T00:00:00.000Z" + : "" + const SEARCH_SUFFIX = searchWithTimestamp + ? "T00:00:00.000Z" + : "" - beforeAll(async () => { - tableOrViewId = await createTableOrView({ - dateid: { name: "dateid", type: FieldType.STRING }, - date: { - name: "date", - type: FieldType.DATETIME, - dateOnly: true, - }, - }) + const JAN_1ST = `2020-01-01` + const JAN_10TH = `2020-01-10` + const JAN_30TH = `2020-01-30` + const UNEXISTING_DATE = `2020-01-03` + const NULL_DATE__ID = `null_date__id` - await createRows([ - { dateid: NULL_DATE__ID, date: null }, - { date: JAN_1ST }, - { date: `${JAN_10TH}T00:00:00.000Z` }, - ]) - }) + beforeAll(async () => { + tableOrViewId = await createTableOrView({ + dateid: { name: "dateid", type: FieldType.STRING }, + date: { + name: "date", + type: FieldType.DATETIME, + dateOnly: true, + }, + }) - describe("equal", () => { - it("successfully finds a row", async () => { - await expectQuery({ - equal: { date: JAN_1ST }, - }).toContainExactly([{ date: JAN_1ST }]) - }) + await createRows([ + { dateid: NULL_DATE__ID, date: null }, + { date: `${JAN_1ST}${SAVE_SUFFIX}` }, + { date: `${JAN_10TH}${SAVE_SUFFIX}` }, + ]) + }) - it("successfully finds an ISO8601 row", async () => { - await expectQuery({ - equal: { date: JAN_10TH }, - }).toContainExactly([{ date: JAN_10TH }]) - }) + describe("equal", () => { + it("successfully finds a row", async () => { + await expectQuery({ + equal: { date: `${JAN_1ST}${SEARCH_SUFFIX}` }, + }).toContainExactly([{ date: JAN_1ST }]) + }) - it("finds a row with ISO8601 timestamp", async () => { - await expectQuery({ - equal: { date: `${JAN_1ST}T00:00:00.000Z` }, - }).toContainExactly([{ date: JAN_1ST }]) - }) + it("successfully finds an ISO8601 row", async () => { + await expectQuery({ + equal: { date: `${JAN_10TH}${SEARCH_SUFFIX}` }, + }).toContainExactly([{ date: JAN_10TH }]) + }) - it("fails to find nonexistent row", async () => { - await expectQuery({ - equal: { date: UNEXISTING_DATE }, - }).toFindNothing() - }) - }) + it("finds a row with ISO8601 timestamp", async () => { + await expectQuery({ + equal: { date: `${JAN_1ST}${SEARCH_SUFFIX}` }, + }).toContainExactly([{ date: JAN_1ST }]) + }) - describe("notEqual", () => { - it("successfully finds a row", async () => { - await expectQuery({ - notEqual: { date: JAN_1ST }, - }).toContainExactly([ - { date: JAN_10TH }, - { dateid: NULL_DATE__ID }, - ]) - }) + it("fails to find nonexistent row", async () => { + await expectQuery({ + equal: { + date: `${UNEXISTING_DATE}${SEARCH_SUFFIX}`, + }, + }).toFindNothing() + }) + }) - it("fails to find nonexistent row", async () => { - await expectQuery({ - notEqual: { date: JAN_30TH }, - }).toContainExactly([ - { date: JAN_1ST }, - { date: JAN_10TH }, - { dateid: NULL_DATE__ID }, - ]) - }) - }) + describe("notEqual", () => { + it("successfully finds a row", async () => { + await expectQuery({ + notEqual: { date: `${JAN_1ST}${SEARCH_SUFFIX}` }, + }).toContainExactly([ + { date: JAN_10TH }, + { dateid: NULL_DATE__ID }, + ]) + }) - describe("oneOf", () => { - it("successfully finds a row", async () => { - await expectQuery({ - oneOf: { date: [JAN_1ST] }, - }).toContainExactly([{ date: JAN_1ST }]) - }) + it("fails to find nonexistent row", async () => { + await expectQuery({ + notEqual: { date: `${JAN_30TH}${SEARCH_SUFFIX}` }, + }).toContainExactly([ + { date: JAN_1ST }, + { date: JAN_10TH }, + { dateid: NULL_DATE__ID }, + ]) + }) + }) - it("fails to find nonexistent row", async () => { - await expectQuery({ - oneOf: { date: [UNEXISTING_DATE] }, - }).toFindNothing() - }) - }) + describe("oneOf", () => { + it("successfully finds a row", async () => { + await expectQuery({ + oneOf: { date: [`${JAN_1ST}${SEARCH_SUFFIX}`] }, + }).toContainExactly([{ date: JAN_1ST }]) + }) - describe("range", () => { - it("successfully finds a row", async () => { - await expectQuery({ - range: { date: { low: JAN_1ST, high: JAN_1ST } }, - }).toContainExactly([{ date: JAN_1ST }]) - }) + it("fails to find nonexistent row", async () => { + await expectQuery({ + oneOf: { + date: [`${UNEXISTING_DATE}${SEARCH_SUFFIX}`], + }, + }).toFindNothing() + }) + }) - it("successfully finds multiple rows", async () => { - await expectQuery({ - range: { date: { low: JAN_1ST, high: JAN_10TH } }, - }).toContainExactly([{ date: JAN_1ST }, { date: JAN_10TH }]) - }) + describe("range", () => { + it("successfully finds a row", async () => { + await expectQuery({ + range: { + date: { + low: `${JAN_1ST}${SEARCH_SUFFIX}`, + high: `${JAN_1ST}${SEARCH_SUFFIX}`, + }, + }, + }).toContainExactly([{ date: JAN_1ST }]) + }) - it("successfully finds no rows", async () => { - await expectQuery({ - range: { date: { low: JAN_30TH, high: JAN_30TH } }, - }).toFindNothing() - }) - }) + it("successfully finds multiple rows", async () => { + await expectQuery({ + range: { + date: { + low: `${JAN_1ST}${SEARCH_SUFFIX}`, + high: `${JAN_10TH}${SEARCH_SUFFIX}`, + }, + }, + }).toContainExactly([ + { date: JAN_1ST }, + { date: JAN_10TH }, + ]) + }) + + it("successfully finds no rows", async () => { + await expectQuery({ + range: { + date: { + low: `${JAN_30TH}${SEARCH_SUFFIX}`, + high: `${JAN_30TH}${SEARCH_SUFFIX}`, + }, + }, + }).toFindNothing() + }) + }) + } + ) + } + ) }) isInternal && From b9a5a71fd8e8be5282f459280c140effa812139b Mon Sep 17 00:00:00 2001 From: Sam Rose <hello@samwho.dev> Date: Thu, 9 Jan 2025 17:17:24 +0000 Subject: [PATCH 099/131] Remove focus test. --- packages/server/src/api/routes/tests/search.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index 69c68aad97..4de92f21e5 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -1683,7 +1683,7 @@ if (descriptions.length) { }) }) - describe.only("datetime - date only", () => { + describe("datetime - date only", () => { describe.each([true, false])( "saved with timestamp: %s", saveWithTimestamp => { From d15a5f330c4d8bb3c9b2dc9269b21554cf8145b6 Mon Sep 17 00:00:00 2001 From: Sam Rose <hello@samwho.dev> Date: Thu, 9 Jan 2025 17:25:31 +0000 Subject: [PATCH 100/131] Fix row.spec.ts --- packages/server/src/api/routes/tests/row.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 968ce9c798..e5cd54e5a5 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -2341,7 +2341,7 @@ if (descriptions.length) { [FieldType.ARRAY]: ["options 2", "options 4"], [FieldType.NUMBER]: generator.natural(), [FieldType.BOOLEAN]: generator.bool(), - [FieldType.DATETIME]: generator.date().toISOString(), + [FieldType.DATETIME]: generator.date().toISOString().slice(0, 10), [FieldType.ATTACHMENTS]: [setup.structures.basicAttachment()], [FieldType.ATTACHMENT_SINGLE]: setup.structures.basicAttachment(), [FieldType.FORMULA]: undefined, // generated field From 40192097d819564573343d47cd85a63b00d8be39 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 14:32:15 +0100 Subject: [PATCH 101/131] Pass datasource to instance --- packages/client/src/utils/schema.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/utils/schema.js b/packages/client/src/utils/schema.js index ffab142cf3..988c2e3316 100644 --- a/packages/client/src/utils/schema.js +++ b/packages/client/src/utils/schema.js @@ -31,7 +31,7 @@ const getDatasourceFetchInstance = datasource => { if (!handler) { return null } - return new handler({ API }) + return new handler({ API, datasource }) } /** From cbad9303d962f3b118186099aaf5c50f092b9668 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 14:24:59 +0100 Subject: [PATCH 102/131] Fix getSchema from forms --- packages/client/src/utils/schema.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/utils/schema.js b/packages/client/src/utils/schema.js index 988c2e3316..60800afc53 100644 --- a/packages/client/src/utils/schema.js +++ b/packages/client/src/utils/schema.js @@ -52,7 +52,7 @@ export const fetchDatasourceSchema = async ( // Get the normal schema as long as we aren't wanting a form schema let schema if (datasource?.type !== "query" || !options?.formSchema) { - schema = instance.getSchema(datasource, definition) + schema = instance.getSchema(definition) } else if (definition.parameters?.length) { schema = {} definition.parameters.forEach(param => { From aadc4da0b124cbdeaae04f85a008f41160dcb67e Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 10 Jan 2025 10:15:34 +0100 Subject: [PATCH 103/131] Remove duplicated utils --- .../src/components/grid/lib/utils.js | 32 ------------------- 1 file changed, 32 deletions(-) delete mode 100644 packages/frontend-core/src/components/grid/lib/utils.js diff --git a/packages/frontend-core/src/components/grid/lib/utils.js b/packages/frontend-core/src/components/grid/lib/utils.js deleted file mode 100644 index ee74a14bf0..0000000000 --- a/packages/frontend-core/src/components/grid/lib/utils.js +++ /dev/null @@ -1,32 +0,0 @@ -// TODO: remove when all stores are typed - -import { GeneratedIDPrefix, CellIDSeparator } from "./constants" -import { Helpers } from "@budibase/bbui" - -export const parseCellID = cellId => { - if (!cellId) { - return { rowId: undefined, field: undefined } - } - const parts = cellId.split(CellIDSeparator) - const field = parts.pop() - return { rowId: parts.join(CellIDSeparator), field } -} - -export const getCellID = (rowId, fieldName) => { - return `${rowId}${CellIDSeparator}${fieldName}` -} - -export const parseEventLocation = e => { - return { - x: e.clientX ?? e.touches?.[0]?.clientX, - y: e.clientY ?? e.touches?.[0]?.clientY, - } -} - -export const generateRowID = () => { - return `${GeneratedIDPrefix}${Helpers.uuid()}` -} - -export const isGeneratedRowID = id => { - return id?.startsWith(GeneratedIDPrefix) -} From 96052679cb5bbdda0b082ee6c471b6ce7cbaff06 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 10 Jan 2025 10:26:24 +0100 Subject: [PATCH 104/131] Convert constants.js --- .../src/components/grid/lib/{constants.js => constants.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/frontend-core/src/components/grid/lib/{constants.js => constants.ts} (100%) diff --git a/packages/frontend-core/src/components/grid/lib/constants.js b/packages/frontend-core/src/components/grid/lib/constants.ts similarity index 100% rename from packages/frontend-core/src/components/grid/lib/constants.js rename to packages/frontend-core/src/components/grid/lib/constants.ts From bb1811558570b1f29d3bd064640c6249df818e81 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 10 Jan 2025 10:26:56 +0100 Subject: [PATCH 105/131] Convert renderer --- .../grid/lib/{renderers.js => renderers.ts} | 18 ++++++++++++++---- packages/types/src/ui/stores/grid/columns.ts | 1 + 2 files changed, 15 insertions(+), 4 deletions(-) rename packages/frontend-core/src/components/grid/lib/{renderers.js => renderers.ts} (82%) diff --git a/packages/frontend-core/src/components/grid/lib/renderers.js b/packages/frontend-core/src/components/grid/lib/renderers.ts similarity index 82% rename from packages/frontend-core/src/components/grid/lib/renderers.js rename to packages/frontend-core/src/components/grid/lib/renderers.ts index a860d01b53..b009806cc4 100644 --- a/packages/frontend-core/src/components/grid/lib/renderers.js +++ b/packages/frontend-core/src/components/grid/lib/renderers.ts @@ -1,4 +1,4 @@ -import { FieldType } from "@budibase/types" +import { FieldType, UIColumn } from "@budibase/types" import OptionsCell from "../cells/OptionsCell.svelte" import DateCell from "../cells/DateCell.svelte" @@ -40,13 +40,23 @@ const TypeComponentMap = { // Custom types for UI only role: RoleCell, } -export const getCellRenderer = column => { + +function getCellRendererByType(type: FieldType | "role" | undefined) { + if (!type) { + return + } + + return TypeComponentMap[type as keyof typeof TypeComponentMap] +} + +export const getCellRenderer = (column: UIColumn) => { if (column.calculationType) { return NumberCell } + return ( - TypeComponentMap[column?.schema?.cellRenderType] || - TypeComponentMap[column?.schema?.type] || + getCellRendererByType(column.schema?.cellRenderType) || + getCellRendererByType(column.schema?.type) || TextCell ) } diff --git a/packages/types/src/ui/stores/grid/columns.ts b/packages/types/src/ui/stores/grid/columns.ts index 7f20145246..2517d2a3e0 100644 --- a/packages/types/src/ui/stores/grid/columns.ts +++ b/packages/types/src/ui/stores/grid/columns.ts @@ -14,6 +14,7 @@ export type UIColumn = FieldSchema & { type: FieldType readonly: boolean autocolumn: boolean + cellRenderType?: FieldType | "role" } calculationType: CalculationType __idx: number From e23be5acb8ccb0d0425b39d14180817355bc7e6c Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 10 Jan 2025 10:30:19 +0100 Subject: [PATCH 106/131] Websocket to ts --- .../components/grid/lib/{websocket.js => websocket.ts} | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) rename packages/frontend-core/src/components/grid/lib/{websocket.js => websocket.ts} (89%) diff --git a/packages/frontend-core/src/components/grid/lib/websocket.js b/packages/frontend-core/src/components/grid/lib/websocket.ts similarity index 89% rename from packages/frontend-core/src/components/grid/lib/websocket.js rename to packages/frontend-core/src/components/grid/lib/websocket.ts index e7b89ff58a..437b93270d 100644 --- a/packages/frontend-core/src/components/grid/lib/websocket.js +++ b/packages/frontend-core/src/components/grid/lib/websocket.ts @@ -1,12 +1,14 @@ import { get } from "svelte/store" import { createWebsocket } from "../../../utils" import { SocketEvent, GridSocketEvent } from "@budibase/shared-core" +import { Store } from "../stores" +import { UIDatasource } from "@budibase/types" -export const createGridWebsocket = context => { +export const createGridWebsocket = (context: Store) => { const { rows, datasource, users, focusedCellId, definition, API } = context const socket = createWebsocket("/socket/grid") - const connectToDatasource = datasource => { + const connectToDatasource = (datasource: UIDatasource) => { if (!socket.connected) { return } @@ -65,7 +67,7 @@ export const createGridWebsocket = context => { GridSocketEvent.DatasourceChange, ({ datasource: newDatasource }) => { // Listen builder renames, as these aren't handled otherwise - if (newDatasource?.name !== get(definition).name) { + if (newDatasource?.name !== get(definition)?.name) { definition.set(newDatasource) } } From 535f2d68031efcb84f51493644e2d6c2fa8d7de7 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 10 Jan 2025 10:45:35 +0100 Subject: [PATCH 107/131] Type --- packages/frontend-core/src/components/grid/lib/websocket.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/frontend-core/src/components/grid/lib/websocket.ts b/packages/frontend-core/src/components/grid/lib/websocket.ts index 437b93270d..bc41d594f4 100644 --- a/packages/frontend-core/src/components/grid/lib/websocket.ts +++ b/packages/frontend-core/src/components/grid/lib/websocket.ts @@ -2,7 +2,7 @@ import { get } from "svelte/store" import { createWebsocket } from "../../../utils" import { SocketEvent, GridSocketEvent } from "@budibase/shared-core" import { Store } from "../stores" -import { UIDatasource } from "@budibase/types" +import { UIDatasource, UIUser } from "@budibase/types" export const createGridWebsocket = (context: Store) => { const { rows, datasource, users, focusedCellId, definition, API } = context @@ -20,7 +20,7 @@ export const createGridWebsocket = (context: Store) => { datasource, appId, }, - ({ users: gridUsers }) => { + ({ users: gridUsers }: { users: UIUser[] }) => { users.set(gridUsers) } ) From 7d9debd3191668da83dac17c0f0b551a17cea0b5 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 14:49:07 +0100 Subject: [PATCH 108/131] Export DataFetchMap --- packages/frontend-core/src/fetch/index.ts | 14 +++++++------- packages/frontend-core/src/index.ts | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index c1f35abef2..e343b351bc 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -1,18 +1,18 @@ -import TableFetch from "./TableFetch.js" -import ViewFetch from "./ViewFetch.js" -import ViewV2Fetch from "./ViewV2Fetch.js" +import TableFetch from "./TableFetch" +import ViewFetch from "./ViewFetch" +import ViewV2Fetch from "./ViewV2Fetch" import QueryFetch from "./QueryFetch" import RelationshipFetch from "./RelationshipFetch" import NestedProviderFetch from "./NestedProviderFetch" import FieldFetch from "./FieldFetch" import JSONArrayFetch from "./JSONArrayFetch" -import UserFetch from "./UserFetch.js" +import UserFetch from "./UserFetch" import GroupUserFetch from "./GroupUserFetch" import CustomFetch from "./CustomFetch" -import QueryArrayFetch from "./QueryArrayFetch.js" -import { APIClient } from "../api/types.js" +import QueryArrayFetch from "./QueryArrayFetch" +import { APIClient } from "../api/types" -const DataFetchMap = { +export const DataFetchMap = { table: TableFetch, view: ViewFetch, viewV2: ViewV2Fetch, diff --git a/packages/frontend-core/src/index.ts b/packages/frontend-core/src/index.ts index 37951dc776..5b24a9ac50 100644 --- a/packages/frontend-core/src/index.ts +++ b/packages/frontend-core/src/index.ts @@ -1,5 +1,5 @@ export { createAPIClient } from "./api" -export { fetchData } from "./fetch" +export { fetchData, DataFetchMap } from "./fetch" export * as Constants from "./constants" export * from "./stores" export * from "./utils" From 7df69f154a594a5250b511ef62de29a2063870ad Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 14:50:21 +0100 Subject: [PATCH 109/131] Basic ts conversion --- packages/client/src/sdk.js | 2 +- .../client/src/utils/{schema.js => schema.ts} | 24 +++---------------- 2 files changed, 4 insertions(+), 22 deletions(-) rename packages/client/src/utils/{schema.js => schema.ts} (73%) diff --git a/packages/client/src/sdk.js b/packages/client/src/sdk.js index 40d066f2ee..40f35b2ba7 100644 --- a/packages/client/src/sdk.js +++ b/packages/client/src/sdk.js @@ -29,7 +29,7 @@ import { ActionTypes } from "./constants" import { fetchDatasourceSchema, fetchDatasourceDefinition, -} from "./utils/schema.js" +} from "./utils/schema.ts" import { getAPIKey } from "./utils/api.js" import { enrichButtonActions } from "./utils/buttonActions.js" import { processStringSync, makePropSafe } from "@budibase/string-templates" diff --git a/packages/client/src/utils/schema.js b/packages/client/src/utils/schema.ts similarity index 73% rename from packages/client/src/utils/schema.js rename to packages/client/src/utils/schema.ts index 60800afc53..53b31b5495 100644 --- a/packages/client/src/utils/schema.js +++ b/packages/client/src/utils/schema.ts @@ -1,13 +1,5 @@ import { API } from "api" -import TableFetch from "@budibase/frontend-core/src/fetch/TableFetch" -import ViewFetch from "@budibase/frontend-core/src/fetch/ViewFetch" -import QueryFetch from "@budibase/frontend-core/src/fetch/QueryFetch" -import RelationshipFetch from "@budibase/frontend-core/src/fetch/RelationshipFetch" -import NestedProviderFetch from "@budibase/frontend-core/src/fetch/NestedProviderFetch" -import FieldFetch from "@budibase/frontend-core/src/fetch/FieldFetch" -import JSONArrayFetch from "@budibase/frontend-core/src/fetch/JSONArrayFetch" -import ViewV2Fetch from "@budibase/frontend-core/src/fetch/ViewV2Fetch" -import QueryArrayFetch from "@budibase/frontend-core/src/fetch/QueryArrayFetch" +import { DataFetchMap } from "@budibase/frontend-core" /** * Constructs a fetch instance for a given datasource. @@ -16,18 +8,8 @@ import QueryArrayFetch from "@budibase/frontend-core/src/fetch/QueryArrayFetch" * @param datasource the datasource * @returns */ -const getDatasourceFetchInstance = datasource => { - const handler = { - table: TableFetch, - view: ViewFetch, - viewV2: ViewV2Fetch, - query: QueryFetch, - link: RelationshipFetch, - provider: NestedProviderFetch, - field: FieldFetch, - jsonarray: JSONArrayFetch, - queryarray: QueryArrayFetch, - }[datasource?.type] +const getDatasourceFetchInstance = (datasource: { type: string }) => { + const handler = DataFetchMap[datasource?.type] if (!handler) { return null } From e25f26d28d9d41bb2e46f7e0823e87b19c3c3986 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 15:04:56 +0100 Subject: [PATCH 110/131] Reuse type --- packages/frontend-core/src/fetch/index.ts | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index e343b351bc..21832e3ede 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -12,6 +12,8 @@ import CustomFetch from "./CustomFetch" import QueryArrayFetch from "./QueryArrayFetch" import { APIClient } from "../api/types" +export type DataFetchType = keyof typeof DataFetchMap + export const DataFetchMap = { table: TableFetch, view: ViewFetch, @@ -31,8 +33,7 @@ export const DataFetchMap = { // Constructs a new fetch model for a certain datasource export const fetchData = ({ API, datasource, options }: any) => { - const Fetch = - DataFetchMap[datasource?.type as keyof typeof DataFetchMap] || TableFetch + const Fetch = DataFetchMap[datasource?.type as DataFetchType] || TableFetch const fetch = new Fetch({ API, datasource, ...options }) // Initially fetch data but don't bother waiting for the result @@ -43,18 +44,14 @@ export const fetchData = ({ API, datasource, options }: any) => { // Creates an empty fetch instance with no datasource configured, so no data // will initially be loaded -const createEmptyFetchInstance = < - TDatasource extends { - type: keyof typeof DataFetchMap - } ->({ +const createEmptyFetchInstance = <TDatasource extends { type: DataFetchType }>({ API, datasource, }: { API: APIClient datasource: TDatasource }) => { - const handler = DataFetchMap[datasource?.type as keyof typeof DataFetchMap] + const handler = DataFetchMap[datasource?.type as DataFetchType] if (!handler) { return null } @@ -63,9 +60,7 @@ const createEmptyFetchInstance = < // Fetches the definition of any type of datasource export const getDatasourceDefinition = async < - TDatasource extends { - type: keyof typeof DataFetchMap - } + TDatasource extends { type: DataFetchType } >({ API, datasource, @@ -79,9 +74,7 @@ export const getDatasourceDefinition = async < // Fetches the schema of any type of datasource export const getDatasourceSchema = < - TDatasource extends { - type: keyof typeof DataFetchMap - } + TDatasource extends { type: DataFetchType } >({ API, datasource, From 4f06592685522e7d1d58af1a43e7549b24e9002a Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 15:23:20 +0100 Subject: [PATCH 111/131] Use trimmed and typed datasources --- packages/frontend-core/src/fetch/DataFetch.ts | 3 ++- packages/frontend-core/src/fetch/FieldFetch.ts | 9 ++++++--- packages/frontend-core/src/fetch/GroupUserFetch.ts | 2 ++ packages/frontend-core/src/fetch/JSONArrayFetch.ts | 2 +- .../frontend-core/src/fetch/NestedProviderFetch.ts | 1 + packages/frontend-core/src/fetch/QueryArrayFetch.ts | 2 +- packages/frontend-core/src/fetch/QueryFetch.ts | 1 + .../frontend-core/src/fetch/RelationshipFetch.ts | 1 + packages/frontend-core/src/fetch/TableFetch.ts | 9 +++++++-- packages/frontend-core/src/fetch/UserFetch.ts | 8 ++++++-- packages/frontend-core/src/fetch/ViewFetch.ts | 11 +++++++++-- packages/frontend-core/src/fetch/ViewV2Fetch.ts | 12 ++++++++++-- packages/frontend-core/src/fetch/index.ts | 8 ++++++-- packages/frontend-core/src/index.ts | 1 + 14 files changed, 54 insertions(+), 16 deletions(-) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index 0079fec057..8f475339b4 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -13,6 +13,7 @@ import { UISearchFilter, } from "@budibase/types" import { APIClient } from "../api/types" +import { DataFetchType } from "." const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils @@ -59,7 +60,7 @@ export interface DataFetchParams< * For other types of datasource, this class is overridden and extended. */ export default abstract class DataFetch< - TDatasource extends {}, + TDatasource extends { type: DataFetchType }, TDefinition extends { schema?: Record<string, any> | null primaryDisplay?: string diff --git a/packages/frontend-core/src/fetch/FieldFetch.ts b/packages/frontend-core/src/fetch/FieldFetch.ts index ac1e683c51..694443a5dc 100644 --- a/packages/frontend-core/src/fetch/FieldFetch.ts +++ b/packages/frontend-core/src/fetch/FieldFetch.ts @@ -1,7 +1,10 @@ import { Row } from "@budibase/types" import DataFetch from "./DataFetch" -export interface FieldDatasource { +type Types = "field" | "queryarray" | "jsonarray" + +export interface FieldDatasource<TType extends Types> { + type: TType tableId: string fieldType: "attachment" | "array" value: string[] | Row[] @@ -15,8 +18,8 @@ function isArrayOfStrings(value: string[] | Row[]): value is string[] { return Array.isArray(value) && !!value[0] && typeof value[0] !== "object" } -export default class FieldFetch extends DataFetch< - FieldDatasource, +export default class FieldFetch<TType extends Types> extends DataFetch< + FieldDatasource<TType>, FieldDefinition > { async getDefinition(): Promise<FieldDefinition | null> { diff --git a/packages/frontend-core/src/fetch/GroupUserFetch.ts b/packages/frontend-core/src/fetch/GroupUserFetch.ts index a14623bfb0..e07e5331d4 100644 --- a/packages/frontend-core/src/fetch/GroupUserFetch.ts +++ b/packages/frontend-core/src/fetch/GroupUserFetch.ts @@ -8,6 +8,7 @@ interface GroupUserQuery { } interface GroupUserDatasource { + type: "groupUser" tableId: TableNames.USERS } @@ -20,6 +21,7 @@ export default class GroupUserFetch extends DataFetch< super({ ...opts, datasource: { + type: "groupUser", tableId: TableNames.USERS, }, }) diff --git a/packages/frontend-core/src/fetch/JSONArrayFetch.ts b/packages/frontend-core/src/fetch/JSONArrayFetch.ts index cae9a1e521..d746c923f8 100644 --- a/packages/frontend-core/src/fetch/JSONArrayFetch.ts +++ b/packages/frontend-core/src/fetch/JSONArrayFetch.ts @@ -1,7 +1,7 @@ import FieldFetch from "./FieldFetch" import { getJSONArrayDatasourceSchema } from "../utils/json" -export default class JSONArrayFetch extends FieldFetch { +export default class JSONArrayFetch extends FieldFetch<"jsonarray"> { async getDefinition() { const { datasource } = this.options diff --git a/packages/frontend-core/src/fetch/NestedProviderFetch.ts b/packages/frontend-core/src/fetch/NestedProviderFetch.ts index 666340610f..af121fcef8 100644 --- a/packages/frontend-core/src/fetch/NestedProviderFetch.ts +++ b/packages/frontend-core/src/fetch/NestedProviderFetch.ts @@ -2,6 +2,7 @@ import { Row, TableSchema } from "@budibase/types" import DataFetch from "./DataFetch" interface NestedProviderDatasource { + type: "provider" value?: { schema: TableSchema primaryDisplay: string diff --git a/packages/frontend-core/src/fetch/QueryArrayFetch.ts b/packages/frontend-core/src/fetch/QueryArrayFetch.ts index 9142000fe6..7f4d34aaa6 100644 --- a/packages/frontend-core/src/fetch/QueryArrayFetch.ts +++ b/packages/frontend-core/src/fetch/QueryArrayFetch.ts @@ -4,7 +4,7 @@ import { generateQueryArraySchemas, } from "../utils/json" -export default class QueryArrayFetch extends FieldFetch { +export default class QueryArrayFetch extends FieldFetch<"queryarray"> { async getDefinition() { const { datasource } = this.options diff --git a/packages/frontend-core/src/fetch/QueryFetch.ts b/packages/frontend-core/src/fetch/QueryFetch.ts index 0754edd267..09dde86cbd 100644 --- a/packages/frontend-core/src/fetch/QueryFetch.ts +++ b/packages/frontend-core/src/fetch/QueryFetch.ts @@ -4,6 +4,7 @@ import { ExecuteQueryRequest, Query } from "@budibase/types" import { get } from "svelte/store" interface QueryDatasource { + type: "query" _id: string fields: Record<string, any> & { pagination?: { diff --git a/packages/frontend-core/src/fetch/RelationshipFetch.ts b/packages/frontend-core/src/fetch/RelationshipFetch.ts index f853a753cd..89a85ab0e4 100644 --- a/packages/frontend-core/src/fetch/RelationshipFetch.ts +++ b/packages/frontend-core/src/fetch/RelationshipFetch.ts @@ -2,6 +2,7 @@ import { Table } from "@budibase/types" import DataFetch from "./DataFetch" interface RelationshipDatasource { + type: "link" tableId: string rowId: string rowTableId: string diff --git a/packages/frontend-core/src/fetch/TableFetch.ts b/packages/frontend-core/src/fetch/TableFetch.ts index f5927262cb..67cac6b6a7 100644 --- a/packages/frontend-core/src/fetch/TableFetch.ts +++ b/packages/frontend-core/src/fetch/TableFetch.ts @@ -1,8 +1,13 @@ import { get } from "svelte/store" import DataFetch from "./DataFetch" -import { SortOrder, Table, UITable } from "@budibase/types" +import { SortOrder, Table } from "@budibase/types" -export default class TableFetch extends DataFetch<UITable, Table> { +interface TableDatasource { + type: "table" + tableId: string +} + +export default class TableFetch extends DataFetch<TableDatasource, Table> { async determineFeatureFlags() { return { supportsSearch: true, diff --git a/packages/frontend-core/src/fetch/UserFetch.ts b/packages/frontend-core/src/fetch/UserFetch.ts index 656cd840fe..54147fbccf 100644 --- a/packages/frontend-core/src/fetch/UserFetch.ts +++ b/packages/frontend-core/src/fetch/UserFetch.ts @@ -14,18 +14,22 @@ interface UserFetchQuery { } interface UserDatasource { - tableId: string + type: "user" + tableId: TableNames.USERS } +interface UserDefinition {} + export default class UserFetch extends DataFetch< UserDatasource, - {}, + UserDefinition, UserFetchQuery > { constructor(opts: DataFetchParams<UserDatasource, UserFetchQuery>) { super({ ...opts, datasource: { + type: "user", tableId: TableNames.USERS, }, }) diff --git a/packages/frontend-core/src/fetch/ViewFetch.ts b/packages/frontend-core/src/fetch/ViewFetch.ts index b6830e7118..c075d80ce0 100644 --- a/packages/frontend-core/src/fetch/ViewFetch.ts +++ b/packages/frontend-core/src/fetch/ViewFetch.ts @@ -1,7 +1,14 @@ -import { Table, View } from "@budibase/types" +import { Table } from "@budibase/types" import DataFetch from "./DataFetch" -type ViewV1 = View & { name: string } +type ViewV1 = { + type: "view" + name: string + tableId: string + calculation: string + field: string + groupBy: string +} export default class ViewFetch extends DataFetch<ViewV1, Table> { async getDefinition() { diff --git a/packages/frontend-core/src/fetch/ViewV2Fetch.ts b/packages/frontend-core/src/fetch/ViewV2Fetch.ts index cdd3bab6ed..aa5fbd60a2 100644 --- a/packages/frontend-core/src/fetch/ViewV2Fetch.ts +++ b/packages/frontend-core/src/fetch/ViewV2Fetch.ts @@ -1,9 +1,17 @@ -import { SortOrder, UIView, ViewV2, ViewV2Type } from "@budibase/types" +import { SortOrder, ViewV2Enriched, ViewV2Type } from "@budibase/types" import DataFetch from "./DataFetch" import { get } from "svelte/store" import { helpers } from "@budibase/shared-core" -export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> { +interface ViewDatasource { + type: "viewV2" + id: string +} + +export default class ViewV2Fetch extends DataFetch< + ViewDatasource, + ViewV2Enriched +> { async determineFeatureFlags() { return { supportsSearch: true, diff --git a/packages/frontend-core/src/fetch/index.ts b/packages/frontend-core/src/fetch/index.ts index 21832e3ede..d80aa10df6 100644 --- a/packages/frontend-core/src/fetch/index.ts +++ b/packages/frontend-core/src/fetch/index.ts @@ -26,7 +26,7 @@ export const DataFetchMap = { // Client specific datasource types provider: NestedProviderFetch, - field: FieldFetch, + field: FieldFetch<"field">, jsonarray: JSONArrayFetch, queryarray: QueryArrayFetch, } @@ -55,7 +55,11 @@ const createEmptyFetchInstance = <TDatasource extends { type: DataFetchType }>({ if (!handler) { return null } - return new handler({ API, datasource: null as any, query: null as any }) + return new handler({ + API, + datasource: null as never, + query: null as any, + }) } // Fetches the definition of any type of datasource diff --git a/packages/frontend-core/src/index.ts b/packages/frontend-core/src/index.ts index 5b24a9ac50..c0baa63ab6 100644 --- a/packages/frontend-core/src/index.ts +++ b/packages/frontend-core/src/index.ts @@ -1,5 +1,6 @@ export { createAPIClient } from "./api" export { fetchData, DataFetchMap } from "./fetch" +export type { DataFetchType } from "./fetch" export * as Constants from "./constants" export * from "./stores" export * from "./utils" From eb73370460f9b3fb98303f086bf21e29a1ae9bc7 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Thu, 9 Jan 2025 15:35:16 +0100 Subject: [PATCH 112/131] Fix types --- packages/client/src/utils/schema.ts | 56 +++++++++++++------ packages/frontend-core/src/fetch/DataFetch.ts | 2 +- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/packages/client/src/utils/schema.ts b/packages/client/src/utils/schema.ts index 53b31b5495..5400d62087 100644 --- a/packages/client/src/utils/schema.ts +++ b/packages/client/src/utils/schema.ts @@ -1,5 +1,5 @@ import { API } from "api" -import { DataFetchMap } from "@budibase/frontend-core" +import { DataFetchMap, DataFetchType } from "@budibase/frontend-core" /** * Constructs a fetch instance for a given datasource. @@ -8,12 +8,20 @@ import { DataFetchMap } from "@budibase/frontend-core" * @param datasource the datasource * @returns */ -const getDatasourceFetchInstance = (datasource: { type: string }) => { +const getDatasourceFetchInstance = < + TDatasource extends { type: DataFetchType } +>( + datasource: TDatasource +) => { const handler = DataFetchMap[datasource?.type] if (!handler) { return null } - return new handler({ API, datasource }) + return new handler({ + API, + datasource: datasource as never, + query: null as any, + }) } /** @@ -21,21 +29,23 @@ const getDatasourceFetchInstance = (datasource: { type: string }) => { * @param datasource the datasource to fetch the schema for * @param options options for enriching the schema */ -export const fetchDatasourceSchema = async ( - datasource, +export const fetchDatasourceSchema = async < + TDatasource extends { type: DataFetchType } +>( + datasource: TDatasource, options = { enrichRelationships: false, formSchema: false } ) => { const instance = getDatasourceFetchInstance(datasource) - const definition = await instance?.getDefinition(datasource) - if (!definition) { + const definition = await instance?.getDefinition() + if (!instance || !definition) { return null } // Get the normal schema as long as we aren't wanting a form schema - let schema + let schema: any if (datasource?.type !== "query" || !options?.formSchema) { - schema = instance.getSchema(definition) - } else if (definition.parameters?.length) { + schema = instance.getSchema(definition as any) + } else if ("parameters" in definition && definition.parameters?.length) { schema = {} definition.parameters.forEach(param => { schema[param.name] = { ...param, type: "string" } @@ -55,7 +65,12 @@ export const fetchDatasourceSchema = async ( } // Enrich schema with relationships if required - if (definition?.sql && options?.enrichRelationships) { + if ( + definition && + "sql" in definition && + definition.sql && + options?.enrichRelationships + ) { const relationshipAdditions = await getRelationshipSchemaAdditions(schema) schema = { ...schema, @@ -71,20 +86,26 @@ export const fetchDatasourceSchema = async ( * Fetches the definition of any kind of datasource. * @param datasource the datasource to fetch the schema for */ -export const fetchDatasourceDefinition = async datasource => { +export const fetchDatasourceDefinition = async < + TDatasource extends { type: DataFetchType } +>( + datasource: TDatasource +) => { const instance = getDatasourceFetchInstance(datasource) - return await instance?.getDefinition(datasource) + return await instance?.getDefinition() } /** * Fetches the schema of relationship fields for a SQL table schema * @param schema the schema to enrich */ -export const getRelationshipSchemaAdditions = async schema => { +export const getRelationshipSchemaAdditions = async ( + schema: Record<string, any> +) => { if (!schema) { return null } - let relationshipAdditions = {} + let relationshipAdditions: Record<string, any> = {} for (let fieldKey of Object.keys(schema)) { const fieldSchema = schema[fieldKey] if (fieldSchema?.type === "link") { @@ -92,7 +113,10 @@ export const getRelationshipSchemaAdditions = async schema => { type: "table", tableId: fieldSchema?.tableId, }) - Object.keys(linkSchema || {}).forEach(linkKey => { + if (!linkSchema) { + continue + } + Object.keys(linkSchema).forEach(linkKey => { relationshipAdditions[`${fieldKey}.${linkKey}`] = { type: linkSchema[linkKey].type, externalType: linkSchema[linkKey].externalType, diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index 8f475339b4..b10a8b0a69 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -369,7 +369,7 @@ export default abstract class DataFetch< * @param schema the datasource schema * @return {object} the enriched datasource schema */ - private enrichSchema(schema: TableSchema): TableSchema { + enrichSchema(schema: TableSchema): TableSchema { // Check for any JSON fields so we can add any top level properties let jsonAdditions: Record<string, { type: string; nestedJSON: true }> = {} for (const fieldKey of Object.keys(schema)) { From be41a2ae6c2c087deea46c2d8b1861c7448afec2 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 10 Jan 2025 10:07:45 +0100 Subject: [PATCH 113/131] Add type to customDatasource --- packages/frontend-core/src/fetch/CustomFetch.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/frontend-core/src/fetch/CustomFetch.ts b/packages/frontend-core/src/fetch/CustomFetch.ts index afd3d18ba9..dfd29c4a02 100644 --- a/packages/frontend-core/src/fetch/CustomFetch.ts +++ b/packages/frontend-core/src/fetch/CustomFetch.ts @@ -1,6 +1,7 @@ import DataFetch from "./DataFetch" interface CustomDatasource { + type: "custom" data: any } From 8520ad2aeb9cc0424301950cebd0bc8a597bb2e4 Mon Sep 17 00:00:00 2001 From: Peter Clement <peter@budibase.com> Date: Fri, 10 Jan 2025 11:31:14 +0000 Subject: [PATCH 114/131] convert queries store to typescript --- .../builder/src/stores/builder/queries.js | 130 --------------- .../builder/src/stores/builder/queries.ts | 156 ++++++++++++++++++ 2 files changed, 156 insertions(+), 130 deletions(-) delete mode 100644 packages/builder/src/stores/builder/queries.js create mode 100644 packages/builder/src/stores/builder/queries.ts diff --git a/packages/builder/src/stores/builder/queries.js b/packages/builder/src/stores/builder/queries.js deleted file mode 100644 index 7aeb9ff8fd..0000000000 --- a/packages/builder/src/stores/builder/queries.js +++ /dev/null @@ -1,130 +0,0 @@ -import { writable, get, derived } from "svelte/store" -import { datasources } from "./datasources" -import { integrations } from "./integrations" -import { API } from "@/api" -import { duplicateName } from "@/helpers/duplicate" - -const sortQueries = queryList => { - queryList.sort((q1, q2) => { - return q1.name.localeCompare(q2.name) - }) -} - -export function createQueriesStore() { - const store = writable({ - list: [], - selectedQueryId: null, - }) - const derivedStore = derived(store, $store => ({ - ...$store, - selected: $store.list?.find(q => q._id === $store.selectedQueryId), - })) - - const fetch = async () => { - const queries = await API.getQueries() - sortQueries(queries) - store.update(state => ({ - ...state, - list: queries, - })) - } - - const save = async (datasourceId, query) => { - const _integrations = get(integrations) - const dataSource = get(datasources).list.filter( - ds => ds._id === datasourceId - ) - // Check if readable attribute is found - if (dataSource.length !== 0) { - const integration = _integrations[dataSource[0].source] - const readable = integration.query[query.queryVerb].readable - if (readable) { - query.readable = readable - } - } - query.datasourceId = datasourceId - const savedQuery = await API.saveQuery(query) - store.update(state => { - const idx = state.list.findIndex(query => query._id === savedQuery._id) - const queries = state.list - if (idx >= 0) { - queries.splice(idx, 1, savedQuery) - } else { - queries.push(savedQuery) - } - sortQueries(queries) - return { - list: queries, - selectedQueryId: savedQuery._id, - } - }) - return savedQuery - } - - const importQueries = async ({ data, datasourceId }) => { - return await API.importQueries(datasourceId, data) - } - - const select = id => { - store.update(state => ({ - ...state, - selectedQueryId: id, - })) - } - - const preview = async query => { - const result = await API.previewQuery(query) - // Assume all the fields are strings and create a basic schema from the - // unique fields returned by the server - const schema = {} - for (let [field, metadata] of Object.entries(result.schema)) { - schema[field] = metadata || { type: "string" } - } - return { ...result, schema, rows: result.rows || [] } - } - - const deleteQuery = async query => { - await API.deleteQuery(query._id, query._rev) - store.update(state => { - state.list = state.list.filter(existing => existing._id !== query._id) - return state - }) - } - - const duplicate = async query => { - let list = get(store).list - const newQuery = { ...query } - const datasourceId = query.datasourceId - - delete newQuery._id - delete newQuery._rev - newQuery.name = duplicateName( - query.name, - list.map(q => q.name) - ) - - return await save(datasourceId, newQuery) - } - - const removeDatasourceQueries = datasourceId => { - store.update(state => ({ - ...state, - list: state.list.filter(table => table.datasourceId !== datasourceId), - })) - } - - return { - subscribe: derivedStore.subscribe, - fetch, - init: fetch, - select, - save, - import: importQueries, - delete: deleteQuery, - preview, - duplicate, - removeDatasourceQueries, - } -} - -export const queries = createQueriesStore() diff --git a/packages/builder/src/stores/builder/queries.ts b/packages/builder/src/stores/builder/queries.ts new file mode 100644 index 0000000000..c6511dc346 --- /dev/null +++ b/packages/builder/src/stores/builder/queries.ts @@ -0,0 +1,156 @@ +import { derived, get, Writable } from "svelte/store" +import { datasources } from "./datasources" +import { integrations } from "./integrations" +import { API } from "@/api" +import { duplicateName } from "@/helpers/duplicate" +import { DerivedBudiStore } from "@/stores/BudiStore" +import { + Query, + QueryPreview, + PreviewQueryResponse, + SaveQueryRequest, + ImportRestQueryRequest, + QuerySchema, +} from "@budibase/types" + +const sortQueries = (queryList: Query[]) => { + queryList.sort((q1, q2) => { + return q1.name.localeCompare(q2.name) + }) +} + +interface BuilderQueryStore { + list: Query[] + selectedQueryId: string | null +} + +interface DerivedQueryStore extends BuilderQueryStore { + selected?: Query +} + +export class QueryStore extends DerivedBudiStore< + BuilderQueryStore, + DerivedQueryStore +> { + constructor() { + const makeDerivedStore = (store: Writable<BuilderQueryStore>) => { + return derived(store, ($store): DerivedQueryStore => { + return { + list: $store.list, + selectedQueryId: $store.selectedQueryId, + selected: $store.list?.find(q => q._id === $store.selectedQueryId), + } + }) + } + + super( + { + list: [], + selectedQueryId: null, + }, + makeDerivedStore + ) + + this.select = this.select.bind(this) + } + + async fetch() { + const queries = await API.getQueries() + sortQueries(queries) + this.store.update(state => ({ + ...state, + list: queries, + })) + } + + async save(datasourceId: string, query: SaveQueryRequest) { + const _integrations = get(integrations) + const dataSource = get(datasources).list.filter( + ds => ds._id === datasourceId + ) + // Check if readable attribute is found + if (dataSource.length !== 0) { + const integration = _integrations[dataSource[0].source] + const readable = integration.query[query.queryVerb].readable + if (readable) { + query.readable = readable + } + } + query.datasourceId = datasourceId + const savedQuery = await API.saveQuery(query) + this.store.update(state => { + const idx = state.list.findIndex(query => query._id === savedQuery._id) + const queries = state.list + if (idx >= 0) { + queries.splice(idx, 1, savedQuery) + } else { + queries.push(savedQuery) + } + sortQueries(queries) + return { + list: queries, + selectedQueryId: savedQuery._id || null, + } + }) + return savedQuery + } + + async importQueries(data: ImportRestQueryRequest) { + return await API.importQueries(data) + } + + select(id: string | null) { + this.store.update(state => ({ + ...state, + selectedQueryId: id, + })) + } + + async preview(query: QueryPreview): Promise<PreviewQueryResponse> { + const result = await API.previewQuery(query) + // Assume all the fields are strings and create a basic schema from the + // unique fields returned by the server + const schema: Record<string, QuerySchema> = {} + for (let [field, metadata] of Object.entries(result.schema)) { + schema[field] = (metadata as QuerySchema) || { type: "string" } + } + return { ...result, schema, rows: result.rows || [] } + } + + async delete(query: Query) { + if (!query._id || !query._rev) { + throw new Error("Query ID or Revision is missing") + } + await API.deleteQuery(query._id, query._rev) + this.store.update(state => ({ + ...state, + list: state.list.filter(existing => existing._id !== query._id), + })) + } + + async duplicate(query: Query) { + let list = get(this.store).list + const newQuery = { ...query } + const datasourceId = query.datasourceId + + delete newQuery._id + delete newQuery._rev + newQuery.name = duplicateName( + query.name, + list.map(q => q.name) + ) + + return await this.save(datasourceId, newQuery) + } + + removeDatasourceQueries(datasourceId: string) { + this.store.update(state => ({ + ...state, + list: state.list.filter(table => table.datasourceId !== datasourceId), + })) + } + + init = this.fetch +} + +export const queries = new QueryStore() From 616e89716c8ea89bcd1b34b8e16a72463b089924 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 10 Jan 2025 12:47:49 +0100 Subject: [PATCH 115/131] Remove unneeded extension --- packages/client/src/sdk.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/sdk.js b/packages/client/src/sdk.js index 40f35b2ba7..68d75d2806 100644 --- a/packages/client/src/sdk.js +++ b/packages/client/src/sdk.js @@ -29,7 +29,7 @@ import { ActionTypes } from "./constants" import { fetchDatasourceSchema, fetchDatasourceDefinition, -} from "./utils/schema.ts" +} from "./utils/schema" import { getAPIKey } from "./utils/api.js" import { enrichButtonActions } from "./utils/buttonActions.js" import { processStringSync, makePropSafe } from "@budibase/string-templates" From b54eb876d81007c37b918e33f61ded9db8c4ef1f Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 10 Jan 2025 12:50:08 +0100 Subject: [PATCH 116/131] Renames --- packages/frontend-core/src/fetch/ViewFetch.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/frontend-core/src/fetch/ViewFetch.ts b/packages/frontend-core/src/fetch/ViewFetch.ts index c075d80ce0..df00b9bbfc 100644 --- a/packages/frontend-core/src/fetch/ViewFetch.ts +++ b/packages/frontend-core/src/fetch/ViewFetch.ts @@ -1,7 +1,7 @@ import { Table } from "@budibase/types" import DataFetch from "./DataFetch" -type ViewV1 = { +type ViewV1Datasource = { type: "view" name: string tableId: string @@ -10,7 +10,7 @@ type ViewV1 = { groupBy: string } -export default class ViewFetch extends DataFetch<ViewV1, Table> { +export default class ViewFetch extends DataFetch<ViewV1Datasource, Table> { async getDefinition() { const { datasource } = this.options From ed6adbae08cb76f4278084b6d84fc9dcf0f65d45 Mon Sep 17 00:00:00 2001 From: Budibase Staging Release Bot <> Date: Fri, 10 Jan 2025 11:52:48 +0000 Subject: [PATCH 117/131] Bump version to 3.2.38 --- lerna.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lerna.json b/lerna.json index 647c9f202d..ff69a18459 100644 --- a/lerna.json +++ b/lerna.json @@ -1,6 +1,6 @@ { "$schema": "node_modules/lerna/schemas/lerna-schema.json", - "version": "3.2.37", + "version": "3.2.38", "npmClient": "yarn", "concurrency": 20, "command": { From 6ecb01ae830d7ea94fe14b4e2696cfbb29f82cb1 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 10 Jan 2025 14:47:09 +0100 Subject: [PATCH 118/131] Fix close modal on grid+modal generation --- packages/frontend-core/src/utils/utils.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/frontend-core/src/utils/utils.js b/packages/frontend-core/src/utils/utils.js index c424aea5b2..55603b0129 100644 --- a/packages/frontend-core/src/utils/utils.js +++ b/packages/frontend-core/src/utils/utils.js @@ -209,6 +209,9 @@ export const buildFormBlockButtonConfig = props => { { "##eventHandlerType": "Close Side Panel", }, + { + "##eventHandlerType": "Close Modal", + }, ...(actionUrl ? [ From 23025f46396b828e27c356ae21c7bddb473c90c9 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 10 Jan 2025 15:19:55 +0100 Subject: [PATCH 119/131] Clean --- packages/client/src/api/api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/api/api.ts b/packages/client/src/api/api.ts index 93f59f6e9a..b944f7bd7c 100644 --- a/packages/client/src/api/api.ts +++ b/packages/client/src/api/api.ts @@ -4,7 +4,7 @@ import { notificationStore, devToolsEnabled, devToolsStore, -} from "../stores/index.js" +} from "../stores/index" import { get } from "svelte/store" export const API = createAPIClient({ From 9d0e3a17222f303e1f3a952e7c5e298d7187f070 Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 10 Jan 2025 15:22:34 +0100 Subject: [PATCH 120/131] Remove unnecessary extensions --- packages/client/src/api/index.js | 4 ++-- packages/client/src/stores/index.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/client/src/api/index.js b/packages/client/src/api/index.js index a63e19bfbb..3c53045cfd 100644 --- a/packages/client/src/api/index.js +++ b/packages/client/src/api/index.js @@ -1,5 +1,5 @@ -import { API } from "./api.ts" -import { patchAPI } from "./patches.js" +import { API } from "./api" +import { patchAPI } from "./patches" // Certain endpoints which return rows need patched so that they transform // and enrich the row docs, so that they can be correctly handled by the diff --git a/packages/client/src/stores/index.js b/packages/client/src/stores/index.js index e099434b3d..f2b80ed732 100644 --- a/packages/client/src/stores/index.js +++ b/packages/client/src/stores/index.js @@ -1,6 +1,6 @@ export { authStore } from "./auth" export { appStore } from "./app" -export { notificationStore } from "./notification.ts" +export { notificationStore } from "./notification" export { routeStore } from "./routes" export { screenStore } from "./screens" export { builderStore } from "./builder" From 9e2915ff0fc0a7279b95b1b05f9dd8e8baec1cbc Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 10 Jan 2025 16:24:55 +0100 Subject: [PATCH 121/131] Fix wrong conversion --- packages/frontend-core/src/fetch/UserFetch.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend-core/src/fetch/UserFetch.ts b/packages/frontend-core/src/fetch/UserFetch.ts index 656cd840fe..58aa4f5a96 100644 --- a/packages/frontend-core/src/fetch/UserFetch.ts +++ b/packages/frontend-core/src/fetch/UserFetch.ts @@ -52,7 +52,7 @@ export default class UserFetch extends DataFetch< const finalQuery: SearchFilters = utils.isSupportedUserSearch(rest) ? rest - : { [BasicOperator.EMPTY]: { email: null } } + : { [BasicOperator.STRING]: { email: null as any } } try { const opts: SearchUsersRequest = { From f54d917f3a1b375948491757298bc71fdb968192 Mon Sep 17 00:00:00 2001 From: melohagan <101575380+melohagan@users.noreply.github.com> Date: Fri, 10 Jan 2025 15:27:50 +0000 Subject: [PATCH 122/131] account-portal login v2 feature flag (#15343) --- packages/types/src/sdk/featureFlag.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/types/src/sdk/featureFlag.ts b/packages/types/src/sdk/featureFlag.ts index 7b61b70772..996d3bba8d 100644 --- a/packages/types/src/sdk/featureFlag.ts +++ b/packages/types/src/sdk/featureFlag.ts @@ -1,9 +1,15 @@ export enum FeatureFlag { USE_ZOD_VALIDATOR = "USE_ZOD_VALIDATOR", + + // Account-portal + DIRECT_LOGIN_TO_ACCOUNT_PORTAL = "DIRECT_LOGIN_TO_ACCOUNT_PORTAL", } export const FeatureFlagDefaults = { [FeatureFlag.USE_ZOD_VALIDATOR]: false, + + // Account-portal + [FeatureFlag.DIRECT_LOGIN_TO_ACCOUNT_PORTAL]: false, } export type FeatureFlags = typeof FeatureFlagDefaults From e7400f982cc6bab2bada801e94a76b167332b84e Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 10 Jan 2025 16:29:42 +0100 Subject: [PATCH 123/131] Add comment --- packages/frontend-core/src/fetch/UserFetch.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend-core/src/fetch/UserFetch.ts b/packages/frontend-core/src/fetch/UserFetch.ts index e8e7dc8721..1fe9c0a383 100644 --- a/packages/frontend-core/src/fetch/UserFetch.ts +++ b/packages/frontend-core/src/fetch/UserFetch.ts @@ -56,7 +56,7 @@ export default class UserFetch extends DataFetch< const finalQuery: SearchFilters = utils.isSupportedUserSearch(rest) ? rest - : { [BasicOperator.STRING]: { email: null as any } } + : { [BasicOperator.STRING]: { email: null as any } } // TODO: chech. Left as any to not change the behaviour it had when it was js try { const opts: SearchUsersRequest = { From 337339d6e5366b36d437dc384f29963506cee6df Mon Sep 17 00:00:00 2001 From: Adria Navarro <adria@budibase.com> Date: Fri, 10 Jan 2025 16:39:55 +0100 Subject: [PATCH 124/131] Remove confusing empty filter --- packages/frontend-core/src/fetch/UserFetch.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/frontend-core/src/fetch/UserFetch.ts b/packages/frontend-core/src/fetch/UserFetch.ts index 1fe9c0a383..36aebac506 100644 --- a/packages/frontend-core/src/fetch/UserFetch.ts +++ b/packages/frontend-core/src/fetch/UserFetch.ts @@ -2,11 +2,7 @@ import { get } from "svelte/store" import DataFetch, { DataFetchParams } from "./DataFetch" import { TableNames } from "../constants" import { utils } from "@budibase/shared-core" -import { - BasicOperator, - SearchFilters, - SearchUsersRequest, -} from "@budibase/types" +import { SearchFilters, SearchUsersRequest } from "@budibase/types" interface UserFetchQuery { appId: string @@ -56,7 +52,7 @@ export default class UserFetch extends DataFetch< const finalQuery: SearchFilters = utils.isSupportedUserSearch(rest) ? rest - : { [BasicOperator.STRING]: { email: null as any } } // TODO: chech. Left as any to not change the behaviour it had when it was js + : {} try { const opts: SearchUsersRequest = { From 6ab389edde98138f90b85f216cea44b6af804875 Mon Sep 17 00:00:00 2001 From: Budibase Staging Release Bot <> Date: Fri, 10 Jan 2025 15:51:18 +0000 Subject: [PATCH 125/131] Bump version to 3.2.39 --- lerna.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lerna.json b/lerna.json index ff69a18459..0dc09b27be 100644 --- a/lerna.json +++ b/lerna.json @@ -1,6 +1,6 @@ { "$schema": "node_modules/lerna/schemas/lerna-schema.json", - "version": "3.2.38", + "version": "3.2.39", "npmClient": "yarn", "concurrency": 20, "command": { From fcb3a3a1986eae17208ecf44cfa0d9b3e513d09c Mon Sep 17 00:00:00 2001 From: Andrew Kingston <andrew@kingston.dev> Date: Mon, 13 Jan 2025 10:24:23 +0000 Subject: [PATCH 126/131] Fix this reference --- packages/builder/src/stores/portal/users.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/builder/src/stores/portal/users.ts b/packages/builder/src/stores/portal/users.ts index 9284ad2992..1beb73a6c0 100644 --- a/packages/builder/src/stores/portal/users.ts +++ b/packages/builder/src/stores/portal/users.ts @@ -29,6 +29,8 @@ class UserStore extends BudiStore<UserState> { data: [], }) + this.search = this.search.bind(this) + // Update quotas after any add or remove operation this.create = this.refreshUsage(this.create) this.save = this.refreshUsage(this.save) From a6ac76eb05f37c2402cd86c693334b36c6bee757 Mon Sep 17 00:00:00 2001 From: Andrew Kingston <andrew@kingston.dev> Date: Mon, 13 Jan 2025 10:24:37 +0000 Subject: [PATCH 127/131] Fix group actions error --- .../builder/portal/users/groups/_components/GroupUsers.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/builder/src/pages/builder/portal/users/groups/_components/GroupUsers.svelte b/packages/builder/src/pages/builder/portal/users/groups/_components/GroupUsers.svelte index 8d99d406fd..71fd4c0be3 100644 --- a/packages/builder/src/pages/builder/portal/users/groups/_components/GroupUsers.svelte +++ b/packages/builder/src/pages/builder/portal/users/groups/_components/GroupUsers.svelte @@ -52,7 +52,7 @@ ] const removeUser = async id => { - await groups.actions.removeUser(groupId, id) + await groups.removeUser(groupId, id) fetchGroupUsers.refresh() } From 1ec4b4c6b7fc3f0a7f91b52797669612c061761e Mon Sep 17 00:00:00 2001 From: Andrew Kingston <andrew@kingston.dev> Date: Mon, 13 Jan 2025 10:38:00 +0000 Subject: [PATCH 128/131] Fix this reference --- .../src/pages/builder/portal/users/users/index.svelte | 1 + packages/builder/src/stores/portal/users.ts | 10 ++++------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/builder/src/pages/builder/portal/users/users/index.svelte b/packages/builder/src/pages/builder/portal/users/users/index.svelte index 97120c55d4..c77e40c964 100644 --- a/packages/builder/src/pages/builder/portal/users/users/index.svelte +++ b/packages/builder/src/pages/builder/portal/users/users/index.svelte @@ -251,6 +251,7 @@ passwordModal.show() await fetch.refresh() } catch (error) { + console.error(error) notifications.error("Error creating user") } } diff --git a/packages/builder/src/stores/portal/users.ts b/packages/builder/src/stores/portal/users.ts index 1beb73a6c0..6503fc9280 100644 --- a/packages/builder/src/stores/portal/users.ts +++ b/packages/builder/src/stores/portal/users.ts @@ -29,13 +29,11 @@ class UserStore extends BudiStore<UserState> { data: [], }) - this.search = this.search.bind(this) - // Update quotas after any add or remove operation - this.create = this.refreshUsage(this.create) - this.save = this.refreshUsage(this.save) - this.delete = this.refreshUsage(this.delete) - this.bulkDelete = this.refreshUsage(this.bulkDelete) + this.create = this.refreshUsage(this.create.bind(this)) + this.save = this.refreshUsage(this.save.bind(this)) + this.delete = this.refreshUsage(this.delete.bind(this)) + this.bulkDelete = this.refreshUsage(this.bulkDelete.bind(this)) } async search(opts: SearchUsersRequest = {}) { From bd378f0bd44f24bdd22c7fa55657ff96f4051d62 Mon Sep 17 00:00:00 2001 From: Andrew Kingston <andrew@kingston.dev> Date: Mon, 13 Jan 2025 10:50:23 +0000 Subject: [PATCH 129/131] Simplify usage quota refreshing when doing user CRUD --- packages/builder/src/stores/portal/users.ts | 26 +++++++-------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/packages/builder/src/stores/portal/users.ts b/packages/builder/src/stores/portal/users.ts index 6503fc9280..605f8612aa 100644 --- a/packages/builder/src/stores/portal/users.ts +++ b/packages/builder/src/stores/portal/users.ts @@ -28,12 +28,6 @@ class UserStore extends BudiStore<UserState> { super({ data: [], }) - - // Update quotas after any add or remove operation - this.create = this.refreshUsage(this.create.bind(this)) - this.save = this.refreshUsage(this.save.bind(this)) - this.delete = this.refreshUsage(this.delete.bind(this)) - this.bulkDelete = this.refreshUsage(this.bulkDelete.bind(this)) } async search(opts: SearchUsersRequest = {}) { @@ -156,6 +150,7 @@ class UserStore extends BudiStore<UserState> { return body }) const response = await API.createUsers(mappedUsers, data.groups) + licensing.setQuotaUsage() // re-search from first page await this.search() @@ -164,14 +159,19 @@ class UserStore extends BudiStore<UserState> { async delete(id: string) { await API.deleteUser(id) + licensing.setQuotaUsage() } async bulkDelete(users: UserIdentifier[]) { - return API.deleteUsers(users) + const res = API.deleteUsers(users) + licensing.setQuotaUsage() + return res } async save(user: User) { - return await API.saveUser(user) + const res = await API.saveUser(user) + licensing.setQuotaUsage() + return res } async addAppBuilder(userId: string, appId: string) { @@ -202,16 +202,6 @@ class UserStore extends BudiStore<UserState> { return Constants.BudibaseRoles.AppUser } } - - // Wrapper function to refresh quota usage after an operation, - // persisting argument and return types - refreshUsage<T extends any[], U>(fn: (...args: T) => Promise<U>) { - return async function (...args: T) { - const response = await fn(...args) - await licensing.setQuotaUsage() - return response - } - } } export const users = new UserStore() From 53b12075bb1b35bc7839f46384c6b7e907a92d15 Mon Sep 17 00:00:00 2001 From: Andrew Kingston <andrew@kingston.dev> Date: Mon, 13 Jan 2025 10:57:10 +0000 Subject: [PATCH 130/131] Fix unrelated automation log error message --- .../src/pages/builder/portal/apps/index.svelte | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/builder/src/pages/builder/portal/apps/index.svelte b/packages/builder/src/pages/builder/portal/apps/index.svelte index f94bad2147..5c3ee674e9 100644 --- a/packages/builder/src/pages/builder/portal/apps/index.svelte +++ b/packages/builder/src/pages/builder/portal/apps/index.svelte @@ -176,6 +176,8 @@ notifications.error("Error getting init info") } }) + + $: console.log(automationErrors) </script> <Page> @@ -191,8 +193,14 @@ ? "View errors" : "View error"} on:dismiss={async () => { - await automationStore.actions.clearLogErrors({ appId }) - await appsStore.load() + const automationId = Object.keys(automationErrors[appId] || {})[0] + if (automationId) { + await automationStore.actions.clearLogErrors({ + appId, + automationId, + }) + await appsStore.load() + } }} message={automationErrorMessage(appId)} /> From 79a74ff709706ed37aa3a3a9084f0ba7f4fcb133 Mon Sep 17 00:00:00 2001 From: Andrew Kingston <andrew@kingston.dev> Date: Mon, 13 Jan 2025 10:58:31 +0000 Subject: [PATCH 131/131] Remove log --- packages/builder/src/pages/builder/portal/apps/index.svelte | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/builder/src/pages/builder/portal/apps/index.svelte b/packages/builder/src/pages/builder/portal/apps/index.svelte index 5c3ee674e9..bcd59cd948 100644 --- a/packages/builder/src/pages/builder/portal/apps/index.svelte +++ b/packages/builder/src/pages/builder/portal/apps/index.svelte @@ -176,8 +176,6 @@ notifications.error("Error getting init info") } }) - - $: console.log(automationErrors) </script> <Page>