Move internal creation to sdk
This commit is contained in:
parent
9d06c705ac
commit
15d124bfaf
|
@ -106,14 +106,18 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
|
||||||
const isImport = table.rows
|
const isImport = table.rows
|
||||||
const renaming = ctx.request.body._rename
|
const renaming = ctx.request.body._rename
|
||||||
|
|
||||||
|
const isCreate = !table._id
|
||||||
|
|
||||||
checkDefaultFields(table)
|
checkDefaultFields(table)
|
||||||
|
|
||||||
const api = pickApi({ table })
|
let savedTable: Table
|
||||||
let savedTable = await api.save(ctx, renaming)
|
if (isCreate) {
|
||||||
if (!table._id) {
|
savedTable = await sdk.tables.create(table)
|
||||||
savedTable = await sdk.tables.enrichViewSchemas(savedTable)
|
savedTable = await sdk.tables.enrichViewSchemas(savedTable)
|
||||||
await events.table.created(savedTable)
|
await events.table.created(savedTable)
|
||||||
} else {
|
} else {
|
||||||
|
const api = pickApi({ table })
|
||||||
|
savedTable = await api.save(ctx, renaming)
|
||||||
await events.table.updated(savedTable)
|
await events.table.updated(savedTable)
|
||||||
}
|
}
|
||||||
if (renaming) {
|
if (renaming) {
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
import { Row, Table } from "@budibase/types"
|
||||||
|
|
||||||
|
// import * as external from "./external"
|
||||||
|
import * as internal from "./internal"
|
||||||
|
import { isExternal } from "./utils"
|
||||||
|
|
||||||
|
export async function create(
|
||||||
|
table: Omit<Table, "_id" | "_rev">,
|
||||||
|
rows?: Row[],
|
||||||
|
userId?: string
|
||||||
|
): Promise<Table> {
|
||||||
|
if (isExternal({ table })) {
|
||||||
|
// const datasourceId = table.sourceId!
|
||||||
|
throw "not implemented"
|
||||||
|
// return await external.create(table, rows, userId)
|
||||||
|
}
|
||||||
|
return await internal.create(table, rows, userId)
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
import { populateExternalTableSchemas } from "./validation"
|
import { populateExternalTableSchemas } from "./validation"
|
||||||
import * as getters from "./getters"
|
import * as getters from "./getters"
|
||||||
|
import * as create from "./create"
|
||||||
import * as updates from "./update"
|
import * as updates from "./update"
|
||||||
import * as utils from "./utils"
|
import * as utils from "./utils"
|
||||||
import { migrate } from "./migration"
|
import { migrate } from "./migration"
|
||||||
|
@ -7,6 +8,7 @@ import * as sqs from "./internal/sqs"
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
populateExternalTableSchemas,
|
populateExternalTableSchemas,
|
||||||
|
...create,
|
||||||
...updates,
|
...updates,
|
||||||
...getters,
|
...getters,
|
||||||
...utils,
|
...utils,
|
||||||
|
|
|
@ -5,6 +5,7 @@ import {
|
||||||
ViewStatisticsSchema,
|
ViewStatisticsSchema,
|
||||||
ViewV2,
|
ViewV2,
|
||||||
Row,
|
Row,
|
||||||
|
TableSourceType,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import {
|
import {
|
||||||
hasTypeChanged,
|
hasTypeChanged,
|
||||||
|
@ -15,14 +16,48 @@ import { EventType, updateLinks } from "../../../../db/linkedRows"
|
||||||
import { cloneDeep } from "lodash/fp"
|
import { cloneDeep } from "lodash/fp"
|
||||||
import isEqual from "lodash/isEqual"
|
import isEqual from "lodash/isEqual"
|
||||||
import { runStaticFormulaChecks } from "../../../../api/controllers/table/bulkFormula"
|
import { runStaticFormulaChecks } from "../../../../api/controllers/table/bulkFormula"
|
||||||
import { context } from "@budibase/backend-core"
|
import { context, HTTPError } from "@budibase/backend-core"
|
||||||
import { findDuplicateInternalColumns } from "@budibase/shared-core"
|
import { findDuplicateInternalColumns } from "@budibase/shared-core"
|
||||||
import { getTable } from "../getters"
|
import { getTable } from "../getters"
|
||||||
import { checkAutoColumns } from "./utils"
|
import { checkAutoColumns } from "./utils"
|
||||||
import * as viewsSdk from "../../views"
|
import * as viewsSdk from "../../views"
|
||||||
import { getRowParams } from "../../../../db/utils"
|
import { generateTableID, getRowParams } from "../../../../db/utils"
|
||||||
import { quotas } from "@budibase/pro"
|
import { quotas } from "@budibase/pro"
|
||||||
|
|
||||||
|
export async function create(table: Table, rows?: Row[], userId?: string) {
|
||||||
|
const tableId = generateTableID()
|
||||||
|
|
||||||
|
let tableToSave: Table = {
|
||||||
|
_id: tableId,
|
||||||
|
...table,
|
||||||
|
// Ensure these fields are populated, even if not sent in the request
|
||||||
|
type: table.type || "table",
|
||||||
|
sourceType: TableSourceType.INTERNAL,
|
||||||
|
}
|
||||||
|
|
||||||
|
const isImport = !!rows
|
||||||
|
|
||||||
|
if (!tableToSave.views) {
|
||||||
|
tableToSave.views = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { table } = await save(tableToSave, {
|
||||||
|
userId,
|
||||||
|
rowsToImport: rows,
|
||||||
|
isImport,
|
||||||
|
})
|
||||||
|
|
||||||
|
return table
|
||||||
|
} catch (err: any) {
|
||||||
|
if (err instanceof Error) {
|
||||||
|
throw new HTTPError(err.message, 400)
|
||||||
|
} else {
|
||||||
|
throw new HTTPError(err.message || err, err.status || 500)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function save(
|
export async function save(
|
||||||
table: Table,
|
table: Table,
|
||||||
opts?: {
|
opts?: {
|
||||||
|
|
Loading…
Reference in New Issue