Initial typing
This commit is contained in:
parent
90b6639125
commit
10979c43f7
|
@ -1,10 +1,47 @@
|
||||||
import { derived, get } from "svelte/store"
|
import { derived, get, Readable, Writable } from "svelte/store"
|
||||||
import { getDatasourceDefinition, getDatasourceSchema } from "../../../fetch"
|
import { getDatasourceDefinition, getDatasourceSchema } from "../../../fetch"
|
||||||
import { enrichSchemaWithRelColumns, memo } from "../../../utils"
|
import { enrichSchemaWithRelColumns, memo } from "../../../utils"
|
||||||
import { cloneDeep } from "lodash"
|
import { cloneDeep } from "lodash"
|
||||||
import { ViewV2Type } from "@budibase/types"
|
import {
|
||||||
|
FieldSchema,
|
||||||
|
SaveTableRequest,
|
||||||
|
UIDatasource,
|
||||||
|
UpdateViewRequest,
|
||||||
|
ViewV2Type,
|
||||||
|
} from "@budibase/types"
|
||||||
|
import { Store as StoreContext } from "."
|
||||||
|
import { DatasourceActions } from "./datasources"
|
||||||
|
|
||||||
export const createStores = () => {
|
interface DatasourceStore {
|
||||||
|
definition: Writable<UIDatasource>
|
||||||
|
schemaMutations: Writable<Record<string, {}>>
|
||||||
|
subSchemaMutations: Writable<Record<string, {}>>
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DerivedDatasourceStore {
|
||||||
|
schema: Readable<Record<string, FieldSchema>>
|
||||||
|
enrichedSchema: Readable<Record<string, FieldSchema>>
|
||||||
|
hasBudibaseIdentifiers: Readable<boolean>
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ActionDatasourceStore {
|
||||||
|
datasource: DatasourceStore["definition"] & {
|
||||||
|
actions: DatasourceActions<UpdateViewRequest | SaveTableRequest> & {
|
||||||
|
refreshDefinition: () => Promise<void>
|
||||||
|
changePrimaryDisplay: any
|
||||||
|
addSchemaMutation: any
|
||||||
|
addSubSchemaMutation: any
|
||||||
|
saveSchemaMutations: any
|
||||||
|
resetSchemaMutations: any
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Store = DatasourceStore &
|
||||||
|
DerivedDatasourceStore &
|
||||||
|
ActionDatasourceStore
|
||||||
|
|
||||||
|
export const createStores = (): DatasourceStore => {
|
||||||
const definition = memo(null)
|
const definition = memo(null)
|
||||||
const schemaMutations = memo({})
|
const schemaMutations = memo({})
|
||||||
const subSchemaMutations = memo({})
|
const subSchemaMutations = memo({})
|
||||||
|
@ -16,7 +53,7 @@ export const createStores = () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const deriveStores = context => {
|
export const deriveStores = (context: StoreContext): DerivedDatasourceStore => {
|
||||||
const {
|
const {
|
||||||
API,
|
API,
|
||||||
definition,
|
definition,
|
||||||
|
@ -27,7 +64,7 @@ export const deriveStores = context => {
|
||||||
} = context
|
} = context
|
||||||
|
|
||||||
const schema = derived(definition, $definition => {
|
const schema = derived(definition, $definition => {
|
||||||
let schema = getDatasourceSchema({
|
let schema: Record<string, FieldSchema> = getDatasourceSchema({
|
||||||
API,
|
API,
|
||||||
datasource: get(datasource),
|
datasource: get(datasource),
|
||||||
definition: $definition,
|
definition: $definition,
|
||||||
|
@ -40,7 +77,7 @@ export const deriveStores = context => {
|
||||||
// Certain datasources like queries use primitives.
|
// Certain datasources like queries use primitives.
|
||||||
Object.keys(schema || {}).forEach(key => {
|
Object.keys(schema || {}).forEach(key => {
|
||||||
if (typeof schema[key] !== "object") {
|
if (typeof schema[key] !== "object") {
|
||||||
schema[key] = { type: schema[key] }
|
schema[key] = { type: schema[key] } as any // TODO
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -68,9 +105,8 @@ export const deriveStores = context => {
|
||||||
|
|
||||||
if ($subSchemaMutations[field]) {
|
if ($subSchemaMutations[field]) {
|
||||||
enrichedSchema[field].columns ??= {}
|
enrichedSchema[field].columns ??= {}
|
||||||
for (const [fieldName, mutation] of Object.entries(
|
for (const fieldName of Object.keys($subSchemaMutations[field])) {
|
||||||
$subSchemaMutations[field]
|
const mutation = $subSchemaMutations[field][fieldName]
|
||||||
)) {
|
|
||||||
enrichedSchema[field].columns[fieldName] = {
|
enrichedSchema[field].columns[fieldName] = {
|
||||||
...enrichedSchema[field].columns[fieldName],
|
...enrichedSchema[field].columns[fieldName],
|
||||||
...mutation,
|
...mutation,
|
||||||
|
@ -104,7 +140,7 @@ export const deriveStores = context => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createActions = context => {
|
export const createActions = (context: StoreContext): ActionDatasourceStore => {
|
||||||
const {
|
const {
|
||||||
API,
|
API,
|
||||||
datasource,
|
datasource,
|
||||||
|
@ -147,7 +183,9 @@ export const createActions = context => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Saves the datasource definition
|
// Saves the datasource definition
|
||||||
const saveDefinition = async newDefinition => {
|
const saveDefinition = async (
|
||||||
|
newDefinition: UpdateViewRequest | SaveTableRequest
|
||||||
|
) => {
|
||||||
// Update local state
|
// Update local state
|
||||||
const originalDefinition = get(definition)
|
const originalDefinition = get(definition)
|
||||||
definition.set(newDefinition)
|
definition.set(newDefinition)
|
||||||
|
@ -155,7 +193,7 @@ export const createActions = context => {
|
||||||
// Update server
|
// Update server
|
||||||
if (get(config).canSaveSchema) {
|
if (get(config).canSaveSchema) {
|
||||||
try {
|
try {
|
||||||
await getAPI()?.actions.saveDefinition(newDefinition)
|
await getAPI()?.actions.saveDefinition(newDefinition as any)
|
||||||
|
|
||||||
// Broadcast change so external state can be updated, as this change
|
// Broadcast change so external state can be updated, as this change
|
||||||
// will not be received by the builder websocket because we caused it
|
// will not be received by the builder websocket because we caused it
|
||||||
|
@ -242,9 +280,8 @@ export const createActions = context => {
|
||||||
}
|
}
|
||||||
if ($subSchemaMutations[column]) {
|
if ($subSchemaMutations[column]) {
|
||||||
newSchema[column].columns ??= {}
|
newSchema[column].columns ??= {}
|
||||||
for (const [fieldName, mutation] of Object.entries(
|
for (const fieldName of Object.keys($subSchemaMutations[column])) {
|
||||||
$subSchemaMutations[column]
|
const mutation = $subSchemaMutations[column][fieldName]
|
||||||
)) {
|
|
||||||
newSchema[column].columns[fieldName] = {
|
newSchema[column].columns[fieldName] = {
|
||||||
...newSchema[column].columns[fieldName],
|
...newSchema[column].columns[fieldName],
|
||||||
...mutation,
|
...mutation,
|
|
@ -59,7 +59,8 @@ export type Store = BaseStore &
|
||||||
Columns.Store &
|
Columns.Store &
|
||||||
Table.Store &
|
Table.Store &
|
||||||
ViewV2.Store &
|
ViewV2.Store &
|
||||||
NonPlus.Store & {
|
NonPlus.Store &
|
||||||
|
Datasource.Store & {
|
||||||
// TODO while typing the rest of stores
|
// TODO while typing the rest of stores
|
||||||
datasource: Writable<any> & { actions: any }
|
datasource: Writable<any> & { actions: any }
|
||||||
definition: Writable<any>
|
definition: Writable<any>
|
||||||
|
@ -75,6 +76,9 @@ export type Store = BaseStore &
|
||||||
rows: Writable<any> & { actions: any }
|
rows: Writable<any> & { actions: any }
|
||||||
subscribe: any
|
subscribe: any
|
||||||
config: Writable<any>
|
config: Writable<any>
|
||||||
|
dispatch: (event: string, data: any) => any
|
||||||
|
notifications: Writable<any>
|
||||||
|
schemaOverrides: Writable<any>
|
||||||
}
|
}
|
||||||
|
|
||||||
export const attachStores = (context: Store): Store => {
|
export const attachStores = (context: Store): Store => {
|
||||||
|
@ -106,5 +110,5 @@ export const attachStores = (context: Store): Store => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return context
|
return context as Store
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
|
import { SortOrder } from "@budibase/types"
|
||||||
|
|
||||||
export interface UIDatasource {
|
export interface UIDatasource {
|
||||||
type: string
|
type: string
|
||||||
id: string
|
id: string
|
||||||
tableId: string
|
tableId: string
|
||||||
|
sort?: {
|
||||||
|
field: string
|
||||||
|
order?: SortOrder
|
||||||
|
}
|
||||||
|
queryUI: any // TODO
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue