This commit is contained in:
陆柯 2022-07-23 13:04:16 +08:00
parent 852e82d5a9
commit 13c101a451

View File

@ -20,21 +20,22 @@ module.exports = {
let newObj = {} let newObj = {}
if (schemaColumns) { if (schemaColumns) {
for (let key in schemaColumns) { for (let key in schemaColumns) {
if (schemaColumns.hasOwnProperty(key) && !schemaColumns[key].hashExclusive && !excludeKeys.includes(key)) { // JSON.stringify (包括本函数)会把 NaN 或 Infinity 输出为 null会把 undefined 忽略掉。
// JSON.stringify (包括本函数)会把 NaN 或 Infinity 输出为 null会把 undefined 忽略掉。 // 而在 typeorm sqlite 数据库中undefined 会自动存为 schemaColumns[key].default 或 null。从数据库读出时就会和事先JSON.stringify的结果不一致。
// 而在 typeorm sqlite 数据库中undefined 会自动存为 schemaColumns[key].default 或 null。从数据库读出时就会和事先JSON.stringify的结果不一致。 // 为了和 torm 数据库保持一致习惯对schemaColumns里的键值应当把 undefined 也设为 default 或 null。
// 为了和 torm 数据库保持一致习惯对schemaColumns里的键值应当把 undefined 也设为 default 或 null。 if (obj[key] === undefined || Number.isNaN(obj[key]) || obj[key] === Infinity) {
if (obj[key] === undefined || Number.isNaN(obj[key]) || obj[key] === Infinity) { newObj[key] = typeof schemaColumns[key].default === 'undefined' ? null : schemaColumns[key].default
newObj[key] = typeof schemaColumns[key].default === 'undefined' ? null : schemaColumns[key].default obj[key] = newObj[key] // 确保内存中的数据和数据库保持一致
obj[key] = newObj[key] // 确保内存中的数据和数据库保持一致 } else {
} else { newObj[key] = obj[key]
newObj[key] = obj[key]
}
} }
} }
} else { } else {
newObj = obj newObj = obj
} }
for (let exkey of excludeKeys) {
delete newObj[exkey]
}
/* 以下代码来自 https://github.com/substack/json-stable-stringify 可把嵌套的复杂值也按顺序输出,例如 { c: 8, b: [{z:6,y:5,x:4},7], a: 3 } */ /* 以下代码来自 https://github.com/substack/json-stable-stringify 可把嵌套的复杂值也按顺序输出,例如 { c: 8, b: [{z:6,y:5,x:4},7], a: 3 } */
if (typeof space === 'number') space = Array(space + 1).join(' ') if (typeof space === 'number') space = Array(space + 1).join(' ')