Use trimmed and typed datasources
This commit is contained in:
parent
e25f26d28d
commit
4f06592685
|
@ -13,6 +13,7 @@ import {
|
||||||
UISearchFilter,
|
UISearchFilter,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { APIClient } from "../api/types"
|
import { APIClient } from "../api/types"
|
||||||
|
import { DataFetchType } from "."
|
||||||
|
|
||||||
const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils
|
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.
|
* For other types of datasource, this class is overridden and extended.
|
||||||
*/
|
*/
|
||||||
export default abstract class DataFetch<
|
export default abstract class DataFetch<
|
||||||
TDatasource extends {},
|
TDatasource extends { type: DataFetchType },
|
||||||
TDefinition extends {
|
TDefinition extends {
|
||||||
schema?: Record<string, any> | null
|
schema?: Record<string, any> | null
|
||||||
primaryDisplay?: string
|
primaryDisplay?: string
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
import { Row } from "@budibase/types"
|
import { Row } from "@budibase/types"
|
||||||
import DataFetch from "./DataFetch"
|
import DataFetch from "./DataFetch"
|
||||||
|
|
||||||
export interface FieldDatasource {
|
type Types = "field" | "queryarray" | "jsonarray"
|
||||||
|
|
||||||
|
export interface FieldDatasource<TType extends Types> {
|
||||||
|
type: TType
|
||||||
tableId: string
|
tableId: string
|
||||||
fieldType: "attachment" | "array"
|
fieldType: "attachment" | "array"
|
||||||
value: string[] | Row[]
|
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"
|
return Array.isArray(value) && !!value[0] && typeof value[0] !== "object"
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class FieldFetch extends DataFetch<
|
export default class FieldFetch<TType extends Types> extends DataFetch<
|
||||||
FieldDatasource,
|
FieldDatasource<TType>,
|
||||||
FieldDefinition
|
FieldDefinition
|
||||||
> {
|
> {
|
||||||
async getDefinition(): Promise<FieldDefinition | null> {
|
async getDefinition(): Promise<FieldDefinition | null> {
|
||||||
|
|
|
@ -8,6 +8,7 @@ interface GroupUserQuery {
|
||||||
}
|
}
|
||||||
|
|
||||||
interface GroupUserDatasource {
|
interface GroupUserDatasource {
|
||||||
|
type: "groupUser"
|
||||||
tableId: TableNames.USERS
|
tableId: TableNames.USERS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +21,7 @@ export default class GroupUserFetch extends DataFetch<
|
||||||
super({
|
super({
|
||||||
...opts,
|
...opts,
|
||||||
datasource: {
|
datasource: {
|
||||||
|
type: "groupUser",
|
||||||
tableId: TableNames.USERS,
|
tableId: TableNames.USERS,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import FieldFetch from "./FieldFetch"
|
import FieldFetch from "./FieldFetch"
|
||||||
import { getJSONArrayDatasourceSchema } from "../utils/json"
|
import { getJSONArrayDatasourceSchema } from "../utils/json"
|
||||||
|
|
||||||
export default class JSONArrayFetch extends FieldFetch {
|
export default class JSONArrayFetch extends FieldFetch<"jsonarray"> {
|
||||||
async getDefinition() {
|
async getDefinition() {
|
||||||
const { datasource } = this.options
|
const { datasource } = this.options
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { Row, TableSchema } from "@budibase/types"
|
||||||
import DataFetch from "./DataFetch"
|
import DataFetch from "./DataFetch"
|
||||||
|
|
||||||
interface NestedProviderDatasource {
|
interface NestedProviderDatasource {
|
||||||
|
type: "provider"
|
||||||
value?: {
|
value?: {
|
||||||
schema: TableSchema
|
schema: TableSchema
|
||||||
primaryDisplay: string
|
primaryDisplay: string
|
||||||
|
|
|
@ -4,7 +4,7 @@ import {
|
||||||
generateQueryArraySchemas,
|
generateQueryArraySchemas,
|
||||||
} from "../utils/json"
|
} from "../utils/json"
|
||||||
|
|
||||||
export default class QueryArrayFetch extends FieldFetch {
|
export default class QueryArrayFetch extends FieldFetch<"queryarray"> {
|
||||||
async getDefinition() {
|
async getDefinition() {
|
||||||
const { datasource } = this.options
|
const { datasource } = this.options
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import { ExecuteQueryRequest, Query } from "@budibase/types"
|
||||||
import { get } from "svelte/store"
|
import { get } from "svelte/store"
|
||||||
|
|
||||||
interface QueryDatasource {
|
interface QueryDatasource {
|
||||||
|
type: "query"
|
||||||
_id: string
|
_id: string
|
||||||
fields: Record<string, any> & {
|
fields: Record<string, any> & {
|
||||||
pagination?: {
|
pagination?: {
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { Table } from "@budibase/types"
|
||||||
import DataFetch from "./DataFetch"
|
import DataFetch from "./DataFetch"
|
||||||
|
|
||||||
interface RelationshipDatasource {
|
interface RelationshipDatasource {
|
||||||
|
type: "link"
|
||||||
tableId: string
|
tableId: string
|
||||||
rowId: string
|
rowId: string
|
||||||
rowTableId: string
|
rowTableId: string
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
import { get } from "svelte/store"
|
import { get } from "svelte/store"
|
||||||
import DataFetch from "./DataFetch"
|
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() {
|
async determineFeatureFlags() {
|
||||||
return {
|
return {
|
||||||
supportsSearch: true,
|
supportsSearch: true,
|
||||||
|
|
|
@ -14,18 +14,22 @@ interface UserFetchQuery {
|
||||||
}
|
}
|
||||||
|
|
||||||
interface UserDatasource {
|
interface UserDatasource {
|
||||||
tableId: string
|
type: "user"
|
||||||
|
tableId: TableNames.USERS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface UserDefinition {}
|
||||||
|
|
||||||
export default class UserFetch extends DataFetch<
|
export default class UserFetch extends DataFetch<
|
||||||
UserDatasource,
|
UserDatasource,
|
||||||
{},
|
UserDefinition,
|
||||||
UserFetchQuery
|
UserFetchQuery
|
||||||
> {
|
> {
|
||||||
constructor(opts: DataFetchParams<UserDatasource, UserFetchQuery>) {
|
constructor(opts: DataFetchParams<UserDatasource, UserFetchQuery>) {
|
||||||
super({
|
super({
|
||||||
...opts,
|
...opts,
|
||||||
datasource: {
|
datasource: {
|
||||||
|
type: "user",
|
||||||
tableId: TableNames.USERS,
|
tableId: TableNames.USERS,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,7 +1,14 @@
|
||||||
import { Table, View } from "@budibase/types"
|
import { Table } from "@budibase/types"
|
||||||
import DataFetch from "./DataFetch"
|
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> {
|
export default class ViewFetch extends DataFetch<ViewV1, Table> {
|
||||||
async getDefinition() {
|
async getDefinition() {
|
||||||
|
|
|
@ -1,9 +1,17 @@
|
||||||
import { SortOrder, UIView, ViewV2, ViewV2Type } from "@budibase/types"
|
import { SortOrder, ViewV2Enriched, ViewV2Type } from "@budibase/types"
|
||||||
import DataFetch from "./DataFetch"
|
import DataFetch from "./DataFetch"
|
||||||
import { get } from "svelte/store"
|
import { get } from "svelte/store"
|
||||||
import { helpers } from "@budibase/shared-core"
|
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() {
|
async determineFeatureFlags() {
|
||||||
return {
|
return {
|
||||||
supportsSearch: true,
|
supportsSearch: true,
|
||||||
|
|
|
@ -26,7 +26,7 @@ export const DataFetchMap = {
|
||||||
|
|
||||||
// Client specific datasource types
|
// Client specific datasource types
|
||||||
provider: NestedProviderFetch,
|
provider: NestedProviderFetch,
|
||||||
field: FieldFetch,
|
field: FieldFetch<"field">,
|
||||||
jsonarray: JSONArrayFetch,
|
jsonarray: JSONArrayFetch,
|
||||||
queryarray: QueryArrayFetch,
|
queryarray: QueryArrayFetch,
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,11 @@ const createEmptyFetchInstance = <TDatasource extends { type: DataFetchType }>({
|
||||||
if (!handler) {
|
if (!handler) {
|
||||||
return null
|
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
|
// Fetches the definition of any type of datasource
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
export { createAPIClient } from "./api"
|
export { createAPIClient } from "./api"
|
||||||
export { fetchData, DataFetchMap } from "./fetch"
|
export { fetchData, DataFetchMap } from "./fetch"
|
||||||
|
export type { DataFetchType } from "./fetch"
|
||||||
export * as Constants from "./constants"
|
export * as Constants from "./constants"
|
||||||
export * from "./stores"
|
export * from "./stores"
|
||||||
export * from "./utils"
|
export * from "./utils"
|
||||||
|
|
Loading…
Reference in New Issue