服务器端功能:仅保留XX个最新条目并删除所有其余条目

时间:2017-06-01 06:56:35

标签: android node.js firebase firebase-realtime-database cron

我回到this question

我有以下结构。每个用户都有自己的数据。

---+ FB_ROOT_REFERENCE
   |
   +---+ Gy7FXRbRjDfAKWu7a95NgiGIZUk1  (Firebase User Id)
       |
       |
       +---+ KlNlb71qtQUXIGA4cNa (random key, generated by Firebase)
       |   |
       |   +--- (data field ...)
       |
       |
       +---+ KlNlcmfMTDjxQ0BwW1K
       |   |
       |   +--- (data field ...)
       |
       +---+ (...)

我对源代码做了一些更改,但我不知道接下来要去哪里。

// Max number of lines
const MAX_RECORD_COUNT = 5;
const FB_ROOT_REFERENCE = '/locations';
// Removes siblings of the node that element that triggered the function if there are more than MAX_RECORD_COUNT.
// In this example we'll keep the max number of chat message history to MAX_RECORD_COUNT.
exports.truncate = functions.https.onRequest((req, res) => {

    const cron_key = req.query.key;

    // Exit if the keys don't match
      if (!secureCompare(cron_key, functions.config().cron.key)) {
        console.log('The cron_key provided in the request does not match the cron_key set in the environment. Check that', cron_key,
            'matches the cron.key attribute in `firebase env:get`');
        res.status(403).send('Security cron_key does not match. Make sure your cron_key URL query parameter matches the ' +
            'cron.key environment variable.');
        return;
      }

// Next
// need iterate all the children in (FB_ROOT_REFERENCE + user id)
// and keep MAX_RECORD_COUNT data children
// most recent and delete all the rest (older ones)
// ... 
// Here your code is required....

});

如果有人能帮助我编写源代码,我将非常感激。谢谢。

此示例中的代码可能非常有用“Limit number of child nodes

0 个答案:

没有答案