我正在开发一个将为用户发布定期 XML 更新的项目。我试图压制自上次更新以来没有改变的信息。
在 node.js 中,我组装了一个用于生成 XML 的用户对象:
users =
# actual application has 25 properties per user
[
{
USERID: 150927,
EMAIL: 'irving.block@email.net',
FIRSTNAME: 'Irving',
LASTNAME: 'Block',
COLOR: 'Green'
},
{
USERID: 1246007,
EMAIL: 'allen.adler@email.net',
FIRSTNAME: 'Adler',
LASTNAME: 'Allen',
COLOR: 'Blue'
},
{
CLKEY: 1248350,
EMAIL: 'walter.pidgeon@email.net',
FIRSTNAME: 'Walter',
LASTNAME: 'Pidgeon',
COLOR: 'Red'
}
]
我还加载了一个对象,它有助于将用户数据映射到最终的 XML 字段:
xml_map =
[
{
ID: 1,
ELEMENT: 'xml_map_assigned_id',
MAPPPED: 'USERID'
},
{
ID: 2,
ELEMENT: 'xml_map_email_address',
MAPPPED: 'EMAIL'
},
{
ID: 3,
ELEMENT: 'xml_map_user_firstname',
MAPPPED: 'FIRSTNAME'
},
{
ID: 4,
ELEMENT: 'xml_map_user_lastname',
MAPPPED: 'LASTNAME'
},
{
ID: 5,
ELEMENT: 'xml_map_user_color',
MAPPED: 'COLOR'
}
]
使用这些对象,我需要与上次更新进行比较,并删除匹配的属性(因为我只想发布已更改数据的更新)。这是最后一次更新(存储在数据库中)的示例:
# nothing like this is used in the real application
# just showing here to give an idea of what the DB could look like
db_data =
[
{
USERID: 150927,
EMAIL: 'irving.block@email.net',
FIRSTNAME: 'IRVING',
LASTNAME: 'Block',
COLOR: 'Orange'
},
{
USERID: 1246007,
EMAIL: 'new.email@somewhere.com',
FIRSTNAME: 'Adler',
LASTNAME: 'Allen',
COLOR: 'Blue'
},
{
USERID: 1248350,
EMAIL: 'walter.pidgeon@email.net',
FIRSTNAME: 'Walt',
LASTNAME: 'Pidgeon',
COLOR: 'Red'
}
]
使用这些示例,我想最终得到一个这样的 User 对象(因为每个用户只有一件事发生了变化):
users_final =
[
{
USERID: 150927,
COLOR: 'Orange'
},
{
USERID: 1246007,
EMAIL: 'new.email@somewhere.com',
},
{
USERID: 1248350,
FIRSTNAME: 'Walt',
}
]
现在对于显示我一直在尝试完成此操作的伪代码:
async = require "async"
handleChanges = (users, map, callback) ->
sql1 = "select..."
sql2 = "update..."
sql3 = "insert..."
db.query sql1, [], (err, rows, def) ->
if err? then console.log err
if rows[0]
if rows[0].ELEMENT_VALUE.toString() is users[map.MAPPED].toString()
# nothing has changed since last update
db.query sql2, ['Old'], (err, rows, def) ->
if err? then console.log err
callback null, map.MAPPED
else # this value has changed since the last update. Run update query
db.query sql2, ['New'], (err, rows, def) ->
if err? then console.log err
callback null, null
else # no value has ever been saved for this combo. Run insert query
db.query sql3, [], (err, rows, def) ->
if err? then console.log err
callback null, null
updateStore = (users, callback) ->
for map in xml_map
do(map) ->
handleChanges users, map, (err, del) ->
if del? then delete r[del]
callback null, r
async.map users, updateStore, (err, results) ->
console.log results
这显然是行不通的。我确定 updateStore 函数需要做一些不同的事情,但我不知所措。
在此先感谢您的帮助!