| 1 |
import { serializeDateTime, serializeDate } from "@web/core/l10n/dates"; |
| 2 |
import { X2MANY_TYPES, DATE_TIME_TYPE } from "./utils"; |
| 3 |
|
| 4 |
const deepSerialization = ( |
| 5 |
record, |
| 6 |
opts, |
| 7 |
{ serialized = {}, uuidMapping = {}, parentRelInverseName = null, stack = [] } |
| 8 |
) => { |
| 9 |
const result = {}; |
| 10 |
const { fields, name: currentModel } = record.model; |
| 11 |
const DYNAMIC_MODELS = opts.dynamicModels; |
| 12 |
const recursiveSerialize = (childRecord, parentRelInverseName) => |
| 13 |
deepSerialization(childRecord, opts, { |
| 14 |
serialized, |
| 15 |
uuidMapping, |
| 16 |
parentRelInverseName, |
| 17 |
stack, |
| 18 |
}); |
| 19 |
|
| 20 |
// We only care about the fields present in python model |
| 21 |
for (const [fieldName, field] of Object.entries(fields)) { |
| 22 |
if (field.local || field.related || field.compute || field.dummy) { |
| 23 |
continue; |
| 24 |
} |
| 25 |
|
| 26 |
const relatedModel = field.relation; |
| 27 |
const targetModel = field.model; |
| 28 |
const modelCommands = record.models.commands[currentModel]; |
| 29 |
|
| 30 |
if (relatedModel) { |
| 31 |
if (!record.models[relatedModel]) { |
| 32 |
// Ignore not "loaded" model |
| 33 |
continue; |
| 34 |
} |
| 35 |
|
| 36 |
if (DYNAMIC_MODELS.includes(relatedModel) && !serialized[relatedModel]) { |
| 37 |
serialized[relatedModel] = {}; |
| 38 |
} |
| 39 |
} |
| 40 |
if (DYNAMIC_MODELS.includes(currentModel) && !serialized[currentModel]) { |
| 41 |
serialized[currentModel] = { [record.uuid]: record.uuid }; |
| 42 |
} |
| 43 |
if (DYNAMIC_MODELS.includes(targetModel) && !uuidMapping[targetModel]) { |
| 44 |
uuidMapping[targetModel] = {}; |
| 45 |
} |
| 46 |
if (X2MANY_TYPES.has(field.type) && record[fieldName]) { |
| 47 |
if (DYNAMIC_MODELS.includes(relatedModel)) { |
| 48 |
const toUpdate = []; |
| 49 |
const toCreate = []; |
| 50 |
|
| 51 |
for (const childRecord of record[fieldName]) { |
| 52 |
if (serialized[relatedModel][childRecord.uuid]) { |
| 53 |
continue; |
| 54 |
} |
| 55 |
|
| 56 |
if (childRecord.isSynced && childRecord._dirty) { |
| 57 |
toUpdate.push(childRecord); |
| 58 |
|
| 59 |
if (!opts.keepCommands) { |
| 60 |
childRecord._dirty = false; |
| 61 |
} |
| 62 |
} else if (!childRecord.isSynced) { |
| 63 |
toCreate.push(childRecord); |
| 64 |
} |
| 65 |
serialized[relatedModel][childRecord.uuid] = childRecord.uuid; |
| 66 |
} |
| 67 |
// The stack defers processing of x2many relationships to ensure objects are only serialized |
| 68 |
// once in their first encountered parent, preventing redundant serialization. |
| 69 |
stack.push([ |
| 70 |
result, |
| 71 |
fieldName, |
| 72 |
() => [ |
| 73 |
...(result[fieldName] || []), |
| 74 |
...toUpdate.map((childRecord) => [ |
| 75 |
1, |
| 76 |
childRecord.id, |
| 77 |
recursiveSerialize(childRecord, field.inverse_name), |
| 78 |
]), |
| 79 |
...toCreate.map((childRecord) => [ |
| 80 |
0, |
| 81 |
0, |
| 82 |
recursiveSerialize(childRecord, field.inverse_name), |
| 83 |
]), |
| 84 |
], |
| 85 |
]); |
| 86 |
} else { |
| 87 |
result[fieldName] = record[fieldName] |
| 88 |
.filter((childRecord) => childRecord.id) |
| 89 |
.map((childRecord) => { |
| 90 |
if (!childRecord.isSynced) { |
| 91 |
throw new Error( |
| 92 |
`Trying to create a non serializable record '${relatedModel}'` |
| 93 |
); |
| 94 |
} |
| 95 |
return childRecord.id; |
| 96 |
}); |
| 97 |
} |
| 98 |
|
| 99 |
if (modelCommands.unlink.has(fieldName) || modelCommands.delete.has(fieldName)) { |
| 100 |
result[fieldName] = result[fieldName] || []; |
| 101 |
const processRecords = (records, cmdCode) => { |
| 102 |
for (const { id, parentId } of records) { |
| 103 |
const isAlreadyDeleted = serialized[relatedModel]?.["_deleted_" + id]; |
| 104 |
if (parentId === record.id && !isAlreadyDeleted) { |
| 105 |
const isCascadeDelete = |
| 106 |
record.models[relatedModel]?.fields[field.inverse_name]?.ondelete; |
| 107 |
if (isCascadeDelete) { |
| 108 |
serialized[relatedModel]["_deleted_" + id] = true; |
| 109 |
} |
| 110 |
result[fieldName].push([cmdCode, id]); |
| 111 |
} |
| 112 |
} |
| 113 |
}; |
| 114 |
processRecords(modelCommands.unlink.get(fieldName) || [], 3); |
| 115 |
processRecords(modelCommands.delete.get(fieldName) || [], 2); |
| 116 |
|
| 117 |
for (const commands of [modelCommands.unlink, modelCommands.delete]) { |
| 118 |
const commandList = commands.get(fieldName) || []; |
| 119 |
const remainingCommands = commandList.filter( |
| 120 |
({ parentId }) => parentId !== record.id |
| 121 |
); |
| 122 |
|
| 123 |
if (opts.keepCommands) { |
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
if (remainingCommands.length) { |
| 128 |
commands.set(fieldName, remainingCommands); |
| 129 |
} else { |
| 130 |
commands.delete(fieldName); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
continue; |
| 135 |
} |
| 136 |
|
| 137 |
if (field.type === "many2one") { |
| 138 |
const recordId = record[fieldName]?.id; |
| 139 |
if (DYNAMIC_MODELS.includes(relatedModel) && record[fieldName]) { |
| 140 |
if ( |
| 141 |
fieldName !== parentRelInverseName && //mapping not needed for direct child |
| 142 |
record.uuid && |
| 143 |
serialized[relatedModel][record[fieldName].uuid] |
| 144 |
) { |
| 145 |
if (!record[fieldName].isSynced) { |
| 146 |
// mapping is only needed for newly created records |
| 147 |
uuidMapping[targetModel][record.uuid] ??= {}; |
| 148 |
uuidMapping[targetModel][record.uuid][fieldName] = record[fieldName].uuid; |
| 149 |
} |
| 150 |
} |
| 151 |
serialized[relatedModel][record[fieldName].uuid] = record[fieldName].uuid; |
| 152 |
} |
| 153 |
if (typeof recordId === "number" && recordId >= 0) { |
| 154 |
result[fieldName] = recordId; |
| 155 |
} else if (record[fieldName] === undefined) { |
| 156 |
result[fieldName] = false; |
| 157 |
} |
| 158 |
continue; |
| 159 |
} |
| 160 |
if (DATE_TIME_TYPE.has(field.type) && typeof record[fieldName] === "object") { |
| 161 |
result[fieldName] = |
| 162 |
field.type === "datetime" |
| 163 |
? serializeDateTime(record[fieldName]) |
| 164 |
: serializeDate(record[fieldName]); |
| 165 |
continue; |
| 166 |
} |
| 167 |
if (fieldName === "id") { |
| 168 |
if (typeof record[fieldName] === "number") { |
| 169 |
result[fieldName] = record[fieldName]; |
| 170 |
} |
| 171 |
continue; |
| 172 |
} |
| 173 |
result[fieldName] = record[fieldName] !== undefined ? record[fieldName] : false; |
| 174 |
} |
| 175 |
|
| 176 |
while (stack.length) { |
| 177 |
const [res, key, getValue] = stack.pop(); |
| 178 |
res[key] = getValue(); |
| 179 |
} |
| 180 |
|
| 181 |
if (!opts.keepCommands) { |
| 182 |
record._dirty = false; |
| 183 |
} |
| 184 |
|
| 185 |
// Cleanup: remove empty entries from uuidMapping. |
| 186 |
for (const key in uuidMapping) { |
| 187 |
if ( |
| 188 |
uuidMapping[key] && |
| 189 |
typeof uuidMapping[key] === "object" && |
| 190 |
Object.keys(uuidMapping[key]).length === 0 |
| 191 |
) { |
| 192 |
delete uuidMapping[key]; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
return result; |
| 197 |
}; |
| 198 |
|
| 199 |
export const ormSerialization = (record, opts) => { |
| 200 |
const uuidMapping = {}; |
| 201 |
const result = deepSerialization(record, opts, { |
| 202 |
uuidMapping, |
| 203 |
}); |
| 204 |
if (Object.keys(uuidMapping).length !== 0) { |
| 205 |
result.relations_uuid_mapping = uuidMapping; |
| 206 |
} |
| 207 |
return result; |
| 208 |
}; |