From e70f54687d5673371663222af4b87b919071bb22 Mon Sep 17 00:00:00 2001 From: bigfengyu Date: Mon, 30 Dec 2019 11:11:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=97=A0=E9=99=90=E5=BE=AA=E7=8E=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scripts/setToken.ts | 44 +++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/scripts/setToken.ts b/src/scripts/setToken.ts index da208c2..4ab6930 100644 --- a/src/scripts/setToken.ts +++ b/src/scripts/setToken.ts @@ -1,44 +1,50 @@ // 补数据 token -import nanoid = require("nanoid"); -import sequelize from "../models/sequelize"; -import { Repository } from "../models"; +import nanoid = require("nanoid") +import sequelize from "../models/sequelize" +import { Repository } from "../models" const chalk = require("chalk"); (async () => { - sequelize.model(Repository); + sequelize.model(Repository) const cnt = await Repository.count({ where: { + // tslint:disable-next-line: no-null-keyword token: null } - }); + }) + const limit = 500 + const iteration = 1 - const limit = 500; + console.log(`共有${cnt}条数据`) - console.log(`共有${cnt}条数据`); - - - for (let offset = 0; offset < cnt; offset += limit) { - console.log(`正在处理offset ${offset}, limit ${limit}`); + while (true) { const rows = await Repository.findAll({ where: { + // tslint:disable-next-line: no-null-keyword token: null }, - limit, - offset - }); + limit + }) + console.log(`正在处理第 ${iteration} 轮, length ${rows.length}`) + + if (rows.length === 0) { + console.log('全部处理完成') + break + } + await Promise.all( rows.map(async repo => { if (!repo.token) { - repo.token = nanoid(32); - await repo.save(); + repo.token = nanoid(32) + await repo.save() console.log( chalk.green(repo.name + "添加了默认token:" + repo.token) - ); + ) } }) - ); + ) } process.exit(0) -})(); +})()