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 table = ctx.request.body
const isImport = table.rows const isImport = table.rows
const savedTable = await pickApi({ table }).save(ctx) let savedTable = await pickApi({ table }).save(ctx)
if (!table._id) { if (!table._id) {
await events.table.created(savedTable) await events.table.created(savedTable)
savedTable = sdk.tables.enrichViewSchemas(savedTable)
} else { } else {
await events.table.updated(savedTable) await events.table.updated(savedTable)
} }

View File

@ -18,10 +18,11 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
_rename?: RenameColumn _rename?: RenameColumn
} = { } = {
_id: generateTableID(), _id: generateTableID(),
...rest,
type: "table",
sourceType: TableSourceType.INTERNAL,
views: {}, 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 const renaming = tableToSave._rename
delete tableToSave._rename delete tableToSave._rename

View File

@ -5,6 +5,7 @@ import {
FieldType, FieldType,
INTERNAL_TABLE_SOURCE_ID, INTERNAL_TABLE_SOURCE_ID,
InternalTable, InternalTable,
NumberFieldMetadata,
RelationshipType, RelationshipType,
Row, Row,
SaveTableRequest, SaveTableRequest,
@ -83,7 +84,7 @@ describe("/tables", () => {
}, },
}, },
views: { views: {
view1: { "table view": {
id: "viewId", id: "viewId",
version: 2, version: 2,
name: "table view", name: "table view",
@ -96,9 +97,29 @@ describe("/tables", () => {
const expected: Table = { const expected: Table = {
...tableData, ...tableData,
type: "table", 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, sourceType: TableSourceType.INTERNAL,
sourceId: expect.any(String), sourceId: expect.any(String),
_rev: expect.stringMatching(/^1-.+/), _rev: expect.stringMatching(/^1-.+/),
_id: expect.any(String),
createdAt: mocks.date.MOCK_DATE.toISOString(),
updatedAt: mocks.date.MOCK_DATE.toISOString(), updatedAt: mocks.date.MOCK_DATE.toISOString(),
} }
expect(testTable).toEqual(expected) expect(testTable).toEqual(expected)

View File

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