From 2a56b562eba091f56bce4e93ab25d4cc2c7d9912 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Mon, 8 Jul 2024 15:56:23 +0200 Subject: [PATCH] changeset: support save document to SQL --- packages/datalib/src/ChangeSet.ts | 50 +++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/packages/datalib/src/ChangeSet.ts b/packages/datalib/src/ChangeSet.ts index 28396351..e7c92b67 100644 --- a/packages/datalib/src/ChangeSet.ts +++ b/packages/datalib/src/ChangeSet.ts @@ -229,13 +229,23 @@ export function batchUpdateChangeSet( return changeSet; } -function extractFields(item: ChangeSetItem, allowNulls = true): UpdateField[] { - return _.keys(item.fields) - .filter(targetColumn => allowNulls || item.fields[targetColumn] != null) +function extractFields(item: ChangeSetItem, allowNulls = true, allowedDocumentColumns: string[] = []): UpdateField[] { + const allFields = { + ...item.fields, + }; + + for (const docField in item.document || {}) { + if (allowedDocumentColumns.includes(docField)) { + allFields[docField] = item.document[docField]; + } + } + + return _.keys(allFields) + .filter(targetColumn => allowNulls || allFields[targetColumn] != null) .map(targetColumn => ({ targetColumn, exprType: 'value', - value: item.fields[targetColumn], + value: allFields[targetColumn], })); } @@ -243,17 +253,19 @@ function changeSetInsertToSql( item: ChangeSetItem, dbinfo: DatabaseInfo = null ): [AllowIdentityInsert, Insert, AllowIdentityInsert] { - const fields = extractFields(item, false); + const table = dbinfo?.tables?.find(x => x.schemaName == item.schemaName && x.pureName == item.pureName); + const fields = extractFields( + item, + false, + table?.columns?.map(x => x.columnName) + ); if (fields.length == 0) return null; let autoInc = false; - if (dbinfo) { - const table = dbinfo.tables.find(x => x.schemaName == item.schemaName && x.pureName == item.pureName); - if (table) { - const autoIncCol = table.columns.find(x => x.autoIncrement); - // console.log('autoIncCol', autoIncCol); - if (autoIncCol && fields.find(x => x.targetColumn == autoIncCol.columnName)) { - autoInc = true; - } + if (table) { + const autoIncCol = table.columns.find(x => x.autoIncrement); + // console.log('autoIncCol', autoIncCol); + if (autoIncCol && fields.find(x => x.targetColumn == autoIncCol.columnName)) { + autoInc = true; } } const targetTable = { @@ -320,7 +332,9 @@ export function extractChangeSetCondition(item: ChangeSetItem, alias?: string): }; } -function changeSetUpdateToSql(item: ChangeSetItem): Update { +function changeSetUpdateToSql(item: ChangeSetItem, dbinfo: DatabaseInfo = null): Update { + const table = dbinfo?.tables?.find(x => x.schemaName == item.schemaName && x.pureName == item.pureName); + return { from: { name: { @@ -329,7 +343,11 @@ function changeSetUpdateToSql(item: ChangeSetItem): Update { }, }, commandType: 'update', - fields: extractFields(item), + fields: extractFields( + item, + true, + table?.columns?.map(x => x.columnName) + ), where: extractChangeSetCondition(item), }; } @@ -351,7 +369,7 @@ export function changeSetToSql(changeSet: ChangeSet, dbinfo: DatabaseInfo): Comm return _.compact( _.flatten([ ...(changeSet.inserts.map(item => changeSetInsertToSql(item, dbinfo)) as any), - ...changeSet.updates.map(changeSetUpdateToSql), + ...changeSet.updates.map(item => changeSetUpdateToSql(item, dbinfo)), ...changeSet.deletes.map(changeSetDeleteToSql), ]) );