changeset: support save document to SQL

This commit is contained in:
Jan Prochazka 2024-07-08 15:56:23 +02:00
parent 2199a49126
commit 2a56b562eb

View File

@ -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),
])
);