Handle view creation on new table requests

This commit is contained in:
Adria Navarro 2024-02-12 11:59:05 +01:00
parent f1a75b84b4
commit 2c26b55a7c
4 changed files with 35 additions and 10 deletions

View File

@ -75,9 +75,10 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
const table = ctx.request.body
const isImport = table.rows
const savedTable = await pickApi({ table }).save(ctx)
let savedTable = await pickApi({ table }).save(ctx)
if (!table._id) {
await events.table.created(savedTable)
savedTable = sdk.tables.enrichViewSchemas(savedTable)
} else {
await events.table.updated(savedTable)
}

View File

@ -18,10 +18,11 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
_rename?: RenameColumn
} = {
_id: generateTableID(),
...rest,
type: "table",
sourceType: TableSourceType.INTERNAL,
views: {},
...rest,
// Ensure these fields are populated, even if not sent in the request
type: rest.type || "table",
sourceType: rest.sourceType || TableSourceType.INTERNAL,
}
const renaming = tableToSave._rename
delete tableToSave._rename

View File

@ -5,6 +5,7 @@ import {
FieldType,
INTERNAL_TABLE_SOURCE_ID,
InternalTable,
NumberFieldMetadata,
RelationshipType,
Row,
SaveTableRequest,
@ -83,7 +84,7 @@ describe("/tables", () => {
},
},
views: {
view1: {
"table view": {
id: "viewId",
version: 2,
name: "table view",
@ -96,9 +97,29 @@ describe("/tables", () => {
const expected: Table = {
...tableData,
type: "table",
views: {
"table view": {
...tableData.views!["table view"],
schema: {
autoId: {
autocolumn: true,
constraints: {
presence: false,
type: "number",
},
name: "id",
type: FieldType.NUMBER,
subtype: AutoFieldSubType.AUTO_ID,
visible: false,
} as NumberFieldMetadata,
},
},
},
sourceType: TableSourceType.INTERNAL,
sourceId: expect.any(String),
_rev: expect.stringMatching(/^1-.+/),
_id: expect.any(String),
createdAt: mocks.date.MOCK_DATE.toISOString(),
updatedAt: mocks.date.MOCK_DATE.toISOString(),
}
expect(testTable).toEqual(expected)

View File

@ -75,11 +75,13 @@ export async function save(
if (!tableView) continue
if (viewsSdk.isV2(tableView)) {
table.views[view] = viewsSdk.syncSchema(
oldTable!.views![view] as ViewV2,
table.schema,
renaming
)
if (oldTable?.views && oldTable.views[view]) {
table.views[view] = viewsSdk.syncSchema(
oldTable.views[view] as ViewV2,
table.schema,
renaming
)
}
continue
}