我想創建一個Rethinkdb數據庫與node.js中的審計跟蹤。Rethinkdb審計跟蹤與字段時間戳和更新/插入時的狀態
我的表包含以下字段:
訂單ID數量價格狀態時間戳
我有我上傳以下數據爲例文件:
訂單ID ......數量.....價格
1 ....................'1000'........ .100
2 ....................'500'............ 100
3 ..... ...............'1575'.......... 100
當我在空表中上傳所有這些行將隨着時間&日期和狀態設置爲「有效」。
假設我有更新的數據(訂單編號1的變化量),例如重新上傳文件:
訂單ID ......數量價格.....
1 .................... '' .......... 100
2 ... .................'500'............ 100
3 ............ ........'1575'.......... 100
Rethinkdb查詢應將訂單ID爲1的行的狀態設置爲「替換」並更新時間戳。接下來,它應該插入一個更新數據的新行,將狀態設置爲「活動」併爲其添加時間戳。
所以我的新更新的數據表應該是這樣的:
訂單ID ......數量.....價格.....狀態...... .......時間戳
1 ....................'1000'.......... 100 .. ....'替換'.....'12:03 AM 4/22/2016'
1 ....................'1010' .......... 100 .......'active'........'12:03 AM 4/22/2016'
2 ...... .............. '500' ............ 1 00 ......'active'.........'12:00 AM 4/22/2016'
3 ................ ....'1575'.......... 100 ......'active'.........'12:00 AM 4/22/2016'
現在我有一個審計跟蹤,以查看更改/插入的內容和時間。
這是可能與rethinkdb在node.js?
@dalanmiller
我試圖測試代碼,但我不明白的前插和第0。
你所做的一個假設是隻有數量可以改變。如果Price也可以同時改變或兩者同時發生,會發生什麼?有可能我想添加更多的字段/列,這些字段/列也可以隨時間更新。
也可能是訂單更改偶數次。
數據位於名爲'message'的變量中。
這是我現在和它不更新:
r.db('testing').table('orders').filter({Order_ID: message[ticks].Order_ID}).count().run() //count how many rows with this Order_ID
.then(function(feed){
if (JSON.stringify(feed)==1){ //check if the order_id already exist
r.db('testing').table('orders').filter({Order_ID: message[ticks].Order_ID}).update(message[ticks]).do(
function (post) {
return r.branch(post('replaced').eq(0),
false, //if nothing changes, leave it as it is
r.db('testing').table('orders').post('orders').prepend(post('orders').nth(0).merge({message[ticks].Quantity})) // <-- I don't understand how this works
)
}).run()
}
else if (JSON.stringify(feed)==0){
//new order. Insert the order
message[ticks].Timestamp = new Date(); //add timestamp
message[ticks].Status = 'Active'; // add status as 'Active'
r.db('testing').table('orders').insert(message[ticks]).run()
}
嗨大瘦,看看我的答案,讓我知道這是否爲你工作, – dalanmiller
你好,感謝您的輸入時間。我將測試即將到來的日子並回到你身邊。 –
@dalanmiller,請你檢查我做錯了什麼? –