From efd87c762507a9541557a07ebc42485709aad161 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 15 Apr 2018 12:11:09 +0900 Subject: [PATCH] :v: --- cli/delete-invalid-users.js | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 cli/delete-invalid-users.js diff --git a/cli/delete-invalid-users.js b/cli/delete-invalid-users.js new file mode 100644 index 0000000000..41bbc2baba --- /dev/null +++ b/cli/delete-invalid-users.js @@ -0,0 +1,40 @@ +const { default: User, deleteUser } = require('../built/models/user'); +const { default: zip } = require('@prezzemolo/zip') + +const migrate = async (user) => { + try { + await deleteUser(user._id); + return true; + } catch (e) { + return false; + } +} + +async function main() { + const count = await User.count({ + uri: /#/ + }); + + const dop = 1 + const idop = ((count - (count % dop)) / dop) + 1 + + return zip( + 1, + async (time) => { + console.log(`${time} / ${idop}`) + const doc = await User.find({ + uri: /#/ + }, { + limit: dop, skip: time * dop + }) + return Promise.all(doc.map(migrate)) + }, + idop + ).then(a => { + const rv = [] + a.forEach(e => rv.push(...e)) + return rv + }) +} + +main().then(console.dir).catch(console.error)